/************************************************************************************************************
(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.php';
var url_removeProductFromBasket = 'removeProduct.php';

var lang = readCookie("lang_c");



var txt_totalPrice = '<span style="font-weight:bold">Totale prodotti nel carrello: </span>';
var txt_divSpeseSpedizione = '<span style="font-weight:bold">Spese di Spedizione: </span>';
var txt_divTotale = '<span style="font-weight:bold">Totale: </span>';
var txt_totalPrice2 = ' euro ';
var txt_divSpeseSpedizione2 = ' euro ';
var txt_divTotale2 = ' euro ';
var txt_addProdotto = 'Per un totale di  ';
var txt_addProdotto2 = ' €';
var txtaddProdotto3 = 'Clicca di nuovo per aumentare la quantità<br />';
var txt_noProds = 'Non ci sono prodotti nel carrello!';
var txt_prodAggiunto = 'Aggiungi ancora?!?!';
var imgCarrelloPiccola = '<a href="checkout.php"><img src="img/check_out.jpg" alt="Carrello" width="22" border="0" height="19" hspace="5" align="absmiddle" /> Checkout</a>';
//var imgCheckoutPiccola = '<a id="view_carrello" href="javascript:void(0)" title="Carrello" onclick="javascript:Lightview.show({ href: \'#mioCarrello\', options: { width: 400, height: 300 }});"><img src="img/ico_carrello.jpg" alt="Carrello" width="22" border="0" height="19" hspace="5" align="absmiddle" /> Carrello</a><br />';
var imgCheckoutPiccola = '<a id="view_carrello" href="javascript:void(0)" title="Carrello" onclick="javascript:Lightview.show({ href: \'carrello.php\', options: { width: 400, height: 300 }});"><img src="img/ico_carrello.jpg" alt="Carrello" width="22" border="0" height="19" hspace="5" align="absmiddle" /> Carrello</a><br />';


var txtquantitaProdotto=" quantità di questo prodotto presenti nel Carrello ";

var txt_goCheckout ='<a href="checkout.php" target="_parent">CHECKOUT / CASSA</a>';


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 addToBasket(productId)
{

//AGGIORNA IL BOTTONE AGGIUNGI PRODOTTO

var currentProductDiv = document.getElementById('Prod' + productId);
currentProductDiv.firstChild.setAttribute('value', txt_prodAggiunto);



ajaxAddProduct(productId);
	
}



function showAjaxBasketContent(ajaxIndex)
{

	
var itemBox = document.getElementById('shopping_cart_items');
var productItems = ajaxObjects[ajaxIndex].response.split('|||');




if (productItems[0]=="ERROR"){
alert("Spiacenti, non c'è ulteriore disponibilità per questo prodotto ");
}else{
if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// Già nel carrello
var row = document.getElementById('shopping_cart_items_product' + productItems[0]);

row.cells[1].innerHTML = productItems[4];
//productItems[3] è prezzo originale
if (productItems[3]==productItems[2]){
row.cells[2].innerHTML = ""+productItems[2]+" €";
}else{
row.cells[2].innerHTML = ""+productItems[2]+" € <div id='TextOldPrice'>"+productItems[3]+" €</div>";
}
//SET ID
row.cells[2].setAttribute('id', productItems[2]);
}else{	// non ancora nel carrello
if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
shopping_cart_div.style.Height = (shopping_cart_div.offsetHeight + 20) + "px";
var tr = itemBox.insertRow(-1);
tr.id = 'shopping_cart_items_product' + productItems[0]
var td = tr.insertCell(-1);
td.setAttribute('class', 'SpanCarrello');
td.innerHTML = productItems[1];
var td = tr.insertCell(-1);
td.innerHTML = productItems[4]; 	// Quantita
td.setAttribute('class', 'SpanCarrello2');
var td = tr.insertCell(-1);
td.setAttribute('class', 'SpanCarrello3');
td.id =productItems[2];
//	td.style.textAlign = 'right';
//td.style.color = '#FF9900';
//	td.style.fontWeight = 'bold';
if (productItems[3]==productItems[2]){
td.innerHTML = ""+productItems[2]+" €";
}else{
td.innerHTML = ""+productItems[2]+" € <div id='TextOldPrice'>"+productItems[3]+" €</div>";
}
var td = tr.insertCell(-1);
var a = document.createElement('A');
td.appendChild(a);
a.href = 'javascript:void(0)';
a.onclick = function(){ ajaxAddProduct2(productItems[0]); };
var img = document.createElement('IMG');
img.src = 'al_img/plus.gif';
img.setAttribute('border','0')
a.appendChild(img);
var td = tr.insertCell(-1);
var a = document.createElement('A');
td.appendChild(a);
a.href = 'javascript:void(0)';
a.onclick = function(){ removeProductFromBasket(productItems[0]); };
var img = document.createElement('IMG');
img.src = 'al_img/minus.gif';
img.setAttribute('border','0')
a.appendChild(img);
}


	updateTotalPrice(productItems[0]);
}
	ajaxObjects[ajaxIndex] = false;

}
//MIA


function showAjaxBasketContent2(ajaxIndex)
{
	

	
var itemBox = document.getElementById('shopping_cart_items');
var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens



if (productItems[0]=="ERROR"){

alert("quantita terminata");

}else{
var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
var items = row.cells[1].innerHTML /1;
items = productItems[4];
row.cells[1].innerHTML = items;
//productItems[3] è prezzo originale
if (productItems[3]==productItems[2]){
row.cells[2].innerHTML = ""+productItems[2]+" €";
}else{
row.cells[2].innerHTML = ""+productItems[2]+" € <div id='TextOldPrice'>"+productItems[3]+" €</div>";
}
//SET ID
row.cells[2].setAttribute('id', productItems[2]);
updateTotalPrice(productItems[0],1);
}
ajaxObjects[ajaxIndex] = false;
}




function updateTotalPrice(pageProduct,external){

var text_addProdotto = document.getElementById('text_addProdotto');
if(text_addProdotto){
text_addProdotto.innerHTML= '';
//mostro quantita del prodotto in pagina
if(pageProduct){
var productRow = document.getElementById('shopping_cart_items_product' + pageProduct);
if (productRow){


//text_addProdotto.innerHTML = txtaddProdotto3; //:Clicca di nuovo per aumentare la quantità

text_addProdotto.innerHTML+=productRow.cells[1].innerHTML+ txtquantitaProdotto+'<br />';//attualmente nel carrello ne hai X

}
}


}


var itemBox = document.getElementById('shopping_cart_items');

if(!external){
var numProdottiBox = document.getElementById('num_prodotti');
var num_prodotti = itemBox.rows.length-1;


if (num_prodotti <2)
var txt_nProdotti = " Prodotto";
else
var txt_nProdotti = " Prodotti";

if (num_prodotti <1){
var txt_nProdotti = "";
num_prodotti = "";
}


numProdottiBox.innerHTML = num_prodotti +txt_nProdotti; //header: 1 Prodotto o 2 Prodotti etc

}




	var totalPrice = 0;
	if(document.getElementById('shopping_cart_totalprice')){
		for(var no=1;no<itemBox.rows.length;no++){
			totalPrice = totalPrice + (itemBox.rows[no].cells[1].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].getAttribute('id'));
		}
		if (totalPrice.toFixed(2)!=="0.00"){
			if (document.getElementById('shopping_cart_checkoutButton'))
			document.getElementById('shopping_cart_checkoutButton').innerHTML = txt_goCheckout;
		document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2) + txt_totalPrice2;
		}else{
			if (document.getElementById('shopping_cart_checkoutButton'))
			document.getElementById('shopping_cart_checkoutButton').innerHTML = '';
			document.getElementById('shopping_cart_totalprice').innerHTML = txt_noProds;
		}


//AGGIORNA IL BOTTONE AGGIUNGI PRODOTTO



if(text_addProdotto){
if (totalPrice.toFixed(2)>0.00)
//text_addProdotto.innerHTML+= txt_addProdotto+totalPrice.toFixed(2)+txt_addProdotto2;//:e stai spendendo X €
text_addProdotto.innerHTML+= '';//:e stai spendendo X €
}

if (num_prodotti>0){
var text_addProdotto2 = document.getElementById('addProdottoLinkCheckout');
if(text_addProdotto2){
if(text_addProdotto2.childNodes.length < 3){
text_addProdotto2.innerHTML= imgCheckoutPiccola;//icona checkout
text_addProdotto2.innerHTML+= imgCarrelloPiccola;//icona carrello
}
}
}




	}

}



function totaleCompleto(spese_spedizione){
    var itemBox = document.getElementById('shopping_cart_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[1].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].getAttribute('id'));
		}
		if (totalPrice.toFixed(2)!=="0.00"){
        totale = (0.00 + parseFloat(spese_spedizione.toFixed(2)) + parseFloat(totalPrice.toFixed(2)));
		document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2) + txt_totalPrice2;
		document.getElementById('div_spese_spedizione').innerHTML = txt_divSpeseSpedizione+spese_spedizione.toFixed(2)+txt_divSpeseSpedizione2;
		document.getElementById('div_totale').innerHTML = txt_divTotale+totale.toFixed(2)+txt_divTotale2;
		document.getElementById('div_totale').innerHTML+= '<input type="hidden" name="totalPrice" value="'+totale.toFixed(2)+'" />';


		}
	}
}



function removeProductFromBasket(productId)
{
  var lang = readCookie('lang_c');
var itemUserID = document.getElementById('shopping_cart').attributes.rel.value;

	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].setVar('user_id',itemUserID);
	ajaxObjects[ajaxIndex].setVar('lang',lang);

	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();
}






function ajaxValidateRemovedProduct(ajaxIndex)
{
var itemBox = document.getElementById('shopping_cart_items');
var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
if (productItems[0]=="ERROR"){
if (productItems[1]=="QUANTITA"){
	
if (confirm("Rimuovo il prodotto?"))
{
	

	  var lang = readCookie('lang_c');
var itemUserID = document.getElementById('shopping_cart').attributes.rel.value;


	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = 'elimina_prodotto.php';	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productIdToRemove',productItems[2]);

	ajaxObjects[ajaxIndex].setVar('user_id',itemUserID);
	ajaxObjects[ajaxIndex].setVar('lang',lang);	
	ajaxObjects[ajaxIndex].setVar('elimina','1');
	ajaxObjects[ajaxIndex].onCompletion = function(){
ajaxEliminaProduct(ajaxIndex);
};	// Specify function that will be executed after file has been found

ajaxObjects[ajaxIndex].runAJAX();
	
	


}else{
//document.write("Procedura annullata")
};	
	
}

}else{


	if(row.cells[1].innerHTML == '1'){
		row.parentNode.removeChild(row);
	}else{
row.cells[1].innerHTML = row.cells[1].innerHTML/1 - 1;



if (productItems[3]==productItems[2]){
row.cells[2].innerHTML = ""+productItems[2]+" €";
}else{
row.cells[2].innerHTML = ""+productItems[2]+" € <div id='TextOldPrice'>"+productItems[3]+" €</div>";
}
//SET ID
row.cells[2].setAttribute('id', productItems[2]);



//row.cells[2].innerHTML = productItems[2];

	}

updateTotalPrice(productItems[0]);
}


	
}

function ajaxEliminaProduct(ajaxIndex)
{
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
row.parentNode.removeChild(row);
updateTotalPrice(productItems[0]);
}

function readCookie(name) {

	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function ajaxAddProduct(productId)
{

var lang = readCookie('lang_c');
var itemUserID = document.getElementById('shopping_cart').attributes.rel.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('user_id',itemUserID);
	ajaxObjects[ajaxIndex].setVar('lang',lang);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}


//MIA

function ajaxAddProduct2(productId)
{

	var itemUserID = document.getElementById('shopping_cart').attributes.rel.value;
    var lang = readCookie('lang_c');

	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('user_id',itemUserID);
	ajaxObjects[ajaxIndex].setVar('lang',lang);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent2(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function
}


function aggiungi_n_prodotti(productId,quantita)
{

	var itemUserID = document.getElementById('shopping_cart').attributes.rel.value;
    var lang = readCookie('lang_c');

	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = 'add_prodotti.php';	// Saving product in this file
	ajaxObjects[ajaxIndex].setVar('productId',productId);
	ajaxObjects[ajaxIndex].setVar('user_id',itemUserID);
	ajaxObjects[ajaxIndex].setVar('quantita',quantita);	
	ajaxObjects[ajaxIndex].setVar('lang',lang);
	ajaxObjects[ajaxIndex].onCompletion = function(){
		
		
//var itemBox = document.getElementById('shopping_cart_items');
var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens



if (productItems[0]=="ERROR"){

alert("quantita terminata");

}	
		
		
		
		
		};	
	ajaxObjects[ajaxIndex].runAJAX();	
}