/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var flyingSpeed = 25;
var url_addProductToBasket = 'addProduct.asp';
var url_removeProductFromBasket = 'removeProduct.php';
var txt_totalPrice = 'Total: ';


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 showAjaxBasketContent(ajaxIndex,id,qta)
{
	// Getting a reference to the shopping cart items table
	var itemBox = document.getElementById('box_carrello');
	
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
		// Product isn't allready in the basket - add a new row
		itemBox.innerHTML=productItems[0] + '<p class="carrello"><a href="carrello.asp" ><img src="images/vuoto.gif" width="130" height="20"></a></p></h6>';
		//td.innerHTML = '<a href="#" onclick=Iyt"removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';	



	
	
	ajaxObjects[ajaxIndex] = false;	
	// passare qta e id da ajaxaddproduct
	tb_show('Gametrade','ordinaajax.asp?id=' + id + '&qta=' + qta +'&height=225&width=430','');
	document.body.style.cursor='auto'
	//cambia lo stile alla riga dell'articolo ordinato
	var avviso = document.getElementById('riga' + id)
    avviso.className = 'prodotto_oggetti_consigliato'
    var carrello = document.getElementById('incarr' + id)
	var cqta = Number(productItems[1]) + Number(qta);
	carrello.innerHTML= '<br /><br /><div class="presente_carr"><p class="qta_presente_carrello">' + cqta+ '</p></div>'
	document.getElementById('QTA' + id).value=1
	return false;
}

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[2].innerHTML);
			
		}		
		document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2);
		
	}	
	
}

function removeProductFromBasket(productId)
{
	var productRow = document.getElementById('shopping_cart_items_product' + productId);
	
	var numberOfItemCell = productRow.cells[0];
	if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	}else{
		numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
	}
	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)
{
	document.body.style.cursor='wait'
	var qta = document.getElementById('QTA' + productId).value;
	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].setVar('qta',qta);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex,productId,qta); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();	
	
	// Execute AJAX function		
}
