/* Abarkam Technology -- Level 3 */
/* ©2008 All rights reserved */
/* www.Frotel.com */

var flyingSpeed = 25;
var url_addProductToBasket = 'addProduct.php';
var url_removeProductFromBasket = 'removeProduct.php';
var txt_totalPrice = 'مجموع قيمت: ';


var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

var ajaxObjects = new Array();


function shoppingCart_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
	


function updateTotalPrice()
{
	var itemBox = document.getElementById('shopping_cart_items');
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	if(document.getElementById('shopping_cart_totalprice')){
		for(var no=1;no<itemBox.rows.length;no++){
			totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[3].innerHTML);
			
		}	
		if(totalPrice.toFixed(0)>0){
				document.getElementById('shopping_cart_totalprice').innerHTML = '<br>'+ txt_totalPrice + totalPrice.toFixed(0) + ' تومان <br>';
			}else{
				document.getElementById('shopping_cart_totalprice').innerHTML =  '<br><br> « سبد خريد شما خالي است » <br><br><a href=index.php target=_self class=link1><br> لطفاً كليك نمائيد »» &nbsp;&nbsp;&nbsp; « افزودن محصولات جديد به سبد خريد » &nbsp;&nbsp;&nbsp; «« لطفاً كليك نمائيد </a><br><br>';
			}
		
		if(totalPrice.toFixed(0)>0){
				document.getElementById('shopping_cart_checkout').innerHTML = '<a href=index.php target=_self class=link1><br>«« افزودن محصولات جديد ديگر به اين سبد خريد »»</a>';
			}else{
				document.getElementById('shopping_cart_checkout').innerHTML =  ' ';
			}
	}	
	
}


function removeProductFromBasket(productId)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	var n;
	var numberOfItemCell = productRow.cells[0];
	if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	}else{
		n=numberOfItemCell.innerHTML/1 - 1;
		//if(n==1){
			//numberOfItemCell.innerHTML = n;
			//productRow.cells[0].class = 'sabad';
			//td[numberOfItemCell].style.textAlign = 'center';
		//}else{
			numberOfItemCell.innerHTML = n;
		//}
	}
	updateTotalPrice();
	ajaxRemoveProduct(productId);	
}

function ajaxValidateRemovedProduct(ajaxIndex)
{
	if(ajaxObjects[ajaxIndex].response!='OK')alert('Error while removing product from the database');
	
}

function ajaxRemoveProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket;	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productId',productId);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

