var animationCartTime = 150;
var idProdottoCorrente;
var isAdd;
var isAddToCart;
var isPacchettoToCart;
var reopenCartInterval;
var hasFirstAdd = true;

function addToCart(idProdotto, add, isPacchetto)
{
	if(isPacchetto != null && isPacchetto != "undefined")
		isPacchettoToCart = isPacchetto;
	else
		isPacchettoToCart = 0;
	idProdottoCorrente = idProdotto;
	isAdd = add;
	jQuery.ajax({
	   type: "POST",
	   url: url_site + "templates/nessos/addToCart.php",
	   data: "idp="+idProdotto+"&add="+add+"&sesid="+sessionID+"&idu="+idUtente+"&isp="+isPacchettoToCart,
	   cache: false,
	   async: false,
	   success: function(html)
	   {
		   isAddToCart = parseInt(html);
		   if(isAddToCart == 1 && hasFirstAdd)
		   {
			   hasFirstAdd = false;
		   	   openDinamicAlert('dinamic_alert', 'Complimenti!', 'Il prodotto selezionato &egrave; stato aggiunto al suo carrello.');
		   }
		   loadCart();
	   }
	 });
}

function updateQuantitaClick(idProdotto, isPacchetto, quant)
{
	var quantita = parseInt(jQuery("#quantita_prodotto_"+isPacchetto+"_"+idProdotto).val());
	if(quantita <= 0 || isNaN(quantita))
	{
		jQuery("#quantita_prodotto_"+isPacchetto+"_"+idProdotto).val(quant);
		openDinamicAlert('dinamic_alert', 'Attenzione!', 'La quantit&agrave; minima selezionabile per un prodotto &egrave; 1');
	}else
	{
		var addTrasporto = 0;
		if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined") addTrasporto = 1;
		var indFatt = 1;
		if(jQuery("#indirizzoFatturazione").val() != null && jQuery("#indirizzoFatturazione").val() != "undefined" && jQuery("#indirizzoFatturazione").val() == 0) indFatt = 0;
		var expSped = 0;
		if(document.getElementById('spedizioneExp') != null && document.getElementById('spedizioneExp') != 'undefined' && document.getElementById('spedizioneExp').checked) expSped = 1;
		var contrassegno = 0;
		if(jQuery("#contrassegno").val() != null && jQuery("#contrassegno").val() != "undefined" && jQuery("#contrassegno").is(':checked')) contrassegno = 1;
		
		jQuery.ajax({
		   type: "POST",
		   url: url_site + "templates/nessos/updateCart.php",
		   data: "idp="+idProdotto+"&add=2&sesid="+sessionID+"&idu="+idUtente+"&q="+quantita+"&isp="+isPacchetto+"&addT="+addTrasporto+"&indFatt="+indFatt+"&expSped="+expSped+"&ct="+contrassegno,
		   cache: false,
		   async: false,
		   success: function(xml)
		   {
			   var idProdotto = jQuery(xml).find("idprodotto").text();
			   var quantita = jQuery(xml).find("quantita").text();
			   var totale = jQuery(xml).find("totale").text();
			   var totaleIVA = jQuery(xml).find("totaleIVA").text();
			   var totaleProdotto = jQuery(xml).find("totaleProdotto").text();
			   var totaleTrasporto = jQuery(xml).find("totaleTrasporto").text();
			   var scontoCarrello = jQuery(xml).find("sconto_carrello").text();
				
			   jQuery("#quantita_prodotto_"+isPacchetto+"_"+idProdotto).val(quantita);
			   
			   if(jQuery("#subtotale_carrello").html() != null && jQuery("#subtotale_carrello").html() != "undefined")
			      jQuery("#subtotale_carrello").html('&euro; ' + totale);
				  
			   if(jQuery("#totale_carrello").html() != null && jQuery("#totale_carrello").html() != "undefined")
				   jQuery("#totale_carrello").html('&euro; ' + totale);
				  
			   if(jQuery("#totale_carrello_iva").html() != null && jQuery("#totale_carrello_iva").html() != "undefined")
				   jQuery("#totale_carrello_iva").html('&euro; ' + totaleIVA);
				
			   if(jQuery("#totale_prezzo_prodotto_"+isPacchetto+"_"+idProdotto).html() != null && jQuery("#totale_prezzo_prodotto_"+isPacchetto+"_"+idProdotto).html() != "undefined")
				   jQuery("#totale_prezzo_prodotto_"+isPacchetto+"_"+idProdotto).html('&euro; ' + totaleProdotto);
				  
			   if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined")
				   jQuery("#costo_trasporto").html('&euro; ' + totaleTrasporto);
				
				if(jQuery("#sconto_carrello").html() != null && jQuery("#sconto_carrello").html() != "undefined")
			       jQuery("#sconto_carrello").html('&euro; ' + scontoCarrello);	
			}
		 });
	}
}

function updateCart(idProdotto, add, isPacchetto)
{
	//var quantita = parseInt(jQuery("#quantita_prodotto_"+isPacchetto+"_"+idProdotto).html());
	var quantita = parseInt(jQuery("#quantita_prodotto_"+isPacchetto+"_"+idProdotto).val());
	if(add == 0 && quantita == 1)
	{
		openDinamicAlert('dinamic_alert', 'Attenzione!', 'La quantit&agrave; minima selezionabile per un prodotto &egrave; 1');
	}else
	{
		var addTrasporto = 0;
		if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined") addTrasporto = 1;
		var indFatt = 1;
		if(jQuery("#indirizzoFatturazione").val() != null && jQuery("#indirizzoFatturazione").val() != "undefined" && jQuery("#indirizzoFatturazione").val() == 0) indFatt = 0;
		var expSped = 0;
		if(document.getElementById('spedizioneExp') != null && document.getElementById('spedizioneExp') != 'undefined' && document.getElementById('spedizioneExp').checked) expSped = 1;
		var contrassegno = 0;
		if(jQuery("#contrassegno").val() != null && jQuery("#contrassegno").val() != "undefined" && jQuery("#contrassegno").is(':checked')) contrassegno = 1;
		
		jQuery.ajax({
		   type: "POST",
		   url: url_site + "templates/nessos/updateCart.php",
		   data: "idp="+idProdotto+"&add="+add+"&sesid="+sessionID+"&idu="+idUtente+"&q="+quantita+"&isp="+isPacchetto+"&addT="+addTrasporto+"&indFatt="+indFatt+"&expSped="+expSped+"&ct="+contrassegno,
		   cache: false,
		   async: false,
		   success: function(xml)
		   {
			   var idProdotto = jQuery(xml).find("idprodotto").text();
			   var quantita = jQuery(xml).find("quantita").text();
			   var totale = jQuery(xml).find("totale").text();
			   var totaleIVA = jQuery(xml).find("totaleIVA").text();
			   var totaleProdotto = jQuery(xml).find("totaleProdotto").text();
			   var totaleTrasporto = jQuery(xml).find("totaleTrasporto").text();
			   var scontoCarrello = jQuery(xml).find("sconto_carrello").text();
					
			   jQuery("#quantita_prodotto_"+isPacchetto+"_"+idProdotto).val(quantita);
			   
			   if(jQuery("#subtotale_carrello").html() != null && jQuery("#subtotale_carrello").html() != "undefined")
			      jQuery("#subtotale_carrello").html('&euro; ' + totale);
			   
			   if(jQuery("#totale_carrello").html() != null && jQuery("#totale_carrello").html() != "undefined")
			      jQuery("#totale_carrello").html('&euro; ' + totale);
			      
			   if(jQuery("#totale_carrello_iva").html() != null && jQuery("#totale_carrello_iva").html() != "undefined")
				   jQuery("#totale_carrello_iva").html('&euro; ' + totaleIVA);
				
			   if(jQuery("#totale_prezzo_prodotto_"+isPacchetto+"_"+idProdotto).html() != null && jQuery("#totale_prezzo_prodotto_"+isPacchetto+"_"+idProdotto).html() != "undefined")
				   jQuery("#totale_prezzo_prodotto_"+isPacchetto+"_"+idProdotto).html('&euro; ' + totaleProdotto);
				   
			   if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined")
				   jQuery("#costo_trasporto").html('&euro; ' + totaleTrasporto);
				   
			   if(jQuery("#sconto_carrello").html() != null && jQuery("#sconto_carrello").html() != "undefined")
			       jQuery("#sconto_carrello").html('&euro; ' + scontoCarrello);	
		   }
		 });
	}
}

function reloadTrasporto()
{
	var addTrasporto = 0;
	if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined") addTrasporto = 1;
	var indFatt = 1;
	if(jQuery("#indirizzoFatturazione").val() != null && jQuery("#indirizzoFatturazione").val() != "undefined" && jQuery("#indirizzoFatturazione").val() == 0) indFatt = 0;
	var expSped = 0;
	if(document.getElementById('spedizioneExp') != null && document.getElementById('spedizioneExp') != 'undefined' && document.getElementById('spedizioneExp').checked) expSped = 1;
	var contrassegno = 0;
	if(jQuery("#contrassegno").is(':checked')) contrassegno = 1;
	jQuery.ajax({
	   type: "POST",
	   url: url_site + "templates/nessos/reloadTrasporto.php",
	   data: "sesid="+sessionID+"&idu="+idUtente+"&addT="+addTrasporto+"&indFatt="+indFatt+"&expSped="+expSped+"&ct="+contrassegno,
	   cache: false,
	   async: false,
	   success: function(xml)
	   {
		   var totale = jQuery(xml).find("totale").text();
		   var totaleIVA = jQuery(xml).find("totaleIVA").text();
		   var totaleTrasporto = jQuery(xml).find("totaleTrasporto").text();
			
		   if(jQuery("#totale_carrello").html() != null && jQuery("#totale_carrello").html() != "undefined")
			   jQuery("#totale_carrello").html('&euro; ' + totale);
			   
		   if(jQuery("#totale_carrello_iva").html() != null && jQuery("#totale_carrello_iva").html() != "undefined")
			   jQuery("#totale_carrello_iva").html('&euro; ' + totaleIVA);
			
		   if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined")
			   jQuery("#costo_trasporto").html('&euro; ' + totaleTrasporto);
	   }
	 });
}

function loadCart()
{	
	jQuery.ajax({
	   type: "POST",
	   url: url_site + "templates/nessos/loadCart.php",
	   data: "sesid="+sessionID+"&idu="+idUtente+"&idp="+idProdottoCorrente+"&add="+isAddToCart,
	   cache: false,
	   async: false,
	   success: function(html)
	   {
		   	if(isAdd == 0)
			{
		    	jQuery("#prodotto_carrello_dinamico_"+isPacchettoToCart+"_"+idProdottoCorrente).animate({height: "0px"}, animationCartTime, "linear", function(){
					jQuery("#carrello_dinamico").html("");
					jQuery("#carrello_dinamico").html(html);	
					reopenCartInterval = setInterval('reopenCart()', 20);
				});
			}else
			{
				jQuery(".carrello_dinamico_box_prodotti_overflow").animate({height: "0px"}, 300, "linear",
					function()
					{ 
						jQuery("#carrello_dinamico").html("");
						jQuery("#carrello_dinamico").html(html);
						reopenCartInterval = setInterval('reopenCart()', 20);
					});
			}
	   }
	 });
}

function reopenCart()
{
	if(jQuery("#consegna_gratuita").html() != null && jQuery("#consegna_gratuita").html() != "undefined")
	{
		clearInterval(reopenCartInterval);
		reopenCartInterval = null;
		
		if(jQuery(".carrello_dinamico_box_prodotti_overflow").children(".carrello_dinamico_box_prodotti").length > 0) 
		{
			//ABILITO LINK CARRELLO
			jQuery("#go_carrello_img").removeClass("carrello_vuoto");
			jQuery("#go_carrello").removeClass("carrello_vuoto");
			
			jQuery("#go_carrello_img").attr('href', url_site+"carrello");
			jQuery("#go_carrello").attr('href', url_site+"carrello");
			
			jQuery(".carello_dinamico_box_header a").css('visibility', 'visible');
			if(jQuery(".carello_dinamico_box_header a").hasClass("openCarDin"))
			{
				var newHeight = jQuery(".carrello_dinamico_box_prodotti_overflow").children(".carrello_dinamico_box_prodotti").height() + parseInt(jQuery(".carrello_dinamico_box_prodotti_overflow").children(".carrello_dinamico_box_prodotti").css('padding-bottom'));
				jQuery(".carrello_dinamico_box_prodotti_overflow").animate({height: newHeight+"px"}, 300, "linear",
					function()
					{ 
						jQuery(".carello_dinamico_box_header a").removeClass("closeCarDin");
						jQuery(".carello_dinamico_box_header a").addClass("openCarDin");
						jQuery(".carello_dinamico_box_header a").html("-");
					});	
			}
		}else
		{
			//DISABILITO LINK CARRELLO
			jQuery("#go_carrello_img").addClass("carrello_vuoto");
			jQuery("#go_carrello").addClass("carrello_vuoto");
			
			jQuery("#go_carrello_img").attr('href', 'javascript:void(0)');
			jQuery("#go_carrello").attr('href', 'javascript:void(0)');
			
			jQuery(".carello_dinamico_box_header a").css('visibility', 'hidden');
			jQuery(".carello_dinamico_box_header a").addClass("closeCarDin");
			jQuery(".carello_dinamico_box_header a").removeClass("openCarDin");
			jQuery(".carello_dinamico_box_header a").html("+");
		}
	}
}

function moveCart()
{
	if(jQuery("#consegna_gratuita").html() != null && jQuery("#consegna_gratuita").html() != "undefined")
	{
		clearInterval(reopenCartInterval);
		reopenCartInterval = null;
		reopenCart();
	}
}

function removeToCart(idProd, isPacchetto)
{
	isPacchettoToCart = isPacchetto;
	idProdottoCorrente = idProd;
	jQuery.ajax({
	   type: "POST",
	   url: url_site + "templates/nessos/addToCart.php",
	   data: "idp="+idProdottoCorrente+"&add=0&sesid="+sessionID+"&idu="+idUtente+"&isp="+isPacchetto,
	   cache: false,
	   async: false,
	   success: function(html)
	   {
		   readCart();
		   jQuery("#prodotto_sez_carrello_"+isPacchettoToCart+"_"+idProdottoCorrente).animate({height: "0px"}, 400, "linear",
		   	function()
			{
				jQuery("#prodotto_sez_carrello_"+isPacchettoToCart+"_"+idProdottoCorrente).remove();
			});
	   }
	 });
}

function readCart()
{
	var addTrasporto = 0;
	if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined") addTrasporto = 1;
	var indFatt = 1;
	if(jQuery("#indirizzoFatturazione").val() != null && jQuery("#indirizzoFatturazione").val() != "undefined" && jQuery("#indirizzoFatturazione").val() == 0) indFatt = 0;
	var expSped = 0;
	if(document.getElementById('spedizioneExp') != null && document.getElementById('spedizioneExp') != 'undefined' && document.getElementById('spedizioneExp').checked) expSped = 1;
	var contrassegno = 0;
	if(jQuery("#contrassegno").val() != null && jQuery("#contrassegno").val() != "undefined" && jQuery("#contrassegno").is(':checked')) contrassegno = 1;
	
	jQuery.ajax({
	   type: "POST",
	   url: url_site + "templates/nessos/readCart.php",
	   data: "sesid="+sessionID+"&idu="+idUtente+"&addT="+addTrasporto+"&indFatt="+indFatt+"&expSped="+expSped+"&ct="+contrassegno,
	   cache: false,
	   async: false,
	   success: function(xml)
	   {
		   var totale = jQuery(xml).find("totale").text();
		   var totaleIVA = jQuery(xml).find("totaleIVA").text();
		   var totaleTrasporto = jQuery(xml).find("totaleTrasporto").text();
		   var scontoCarrello = jQuery(xml).find("sconto_carrello").text();
			
		   if(jQuery("#totale_carrello").html() != null && jQuery("#totale_carrello").html() != "undefined")
			   jQuery("#totale_carrello").html('&euro; ' + totale);
			   
		   if(jQuery("#totale_carrello_iva").html() != null && jQuery("#totale_carrello_iva").html() != "undefined")
			   jQuery("#totale_carrello_iva").html('&euro; ' + totaleIVA);
			   
		   if(jQuery("#costo_trasporto").html() != null && jQuery("#costo_trasporto").html() != "undefined")
			   jQuery("#costo_trasporto").html('&euro; ' + totaleTrasporto);
			   
		   if(jQuery("#sconto_carrello").html() != null && jQuery("#sconto_carrello").html() != "undefined")
			   jQuery("#sconto_carrello").html('&euro; ' + scontoCarrello);
			 
			if(parseInt(totale) == 0)
			{
				jQuery("#go_carrello").attr('href', 'javascript:void(0)');
				jQuery("#go_carrello_img").attr('href', 'javascript:void(0)');
				jQuery("#container_carrello_utente").html("<h4 id=\"carrello_vuoto\">Il tuo carrello &egrave; vuoto</h4>");
			}
	   }
	 });
}
