/*
questo script serve per rndere il carrello dinamico se si hanno i javascript abilitati.
autore: luca vicari
data: 15/05/2007

cart			: div con il riepologo del carrello.
numProdotti		: quantità prodotti nel carrello.
totProdotti		: totale della spesa in Euro.

popCarrello		: div per il popup del prodotto inserito.
prodInserito	: div contenente la descrizione del prodotto inserito.
pulsantiera		: div contenente i 2 pulsanti "continua shopping" e "vai al carrello".
goToCart		: link al carrello nella pop dinamica.
*/

function addEvent(obj,ev,fn){
	if(obj.addEventListener) {
		// metodo w3c
		obj.addEventListener(ev, fn, false);
	} else if(obj.attachEvent) {
		// metodo IE
		obj.attachEvent('on'+ev, fn);
	} else {
		// se i suddetti metodi non sono applicabili
		// se esiste gia' una funzione richiamata da quel gestore evento
		if(typeof(obj['on'+ev])=='function'){
			// salvo in variabile la funzione gia' associata al gestore
			var f=obj['on'+ev];
			// setto per quel gestore una nuova funzione 
			// che comprende la vecchia e la nuova
			obj['on'+ev]=function(){if(f)f();fn()}
		}
		// altrimenti setto la funzione per il gestore
		else obj['on'+ev]=fn;
	}
}

//addEvent(window,'load',init);

function init() {
    
    
	var data = new Date();
	var url = "";
	var browserName="";

	//posiziono la popCarrello nel posto giusto al click.
	Event.observe(document.body, 'click', function(event) {
		var element = Event.element(event);  
		if (element.parentNode.className == 'DA_MOD' || element.className == 'DA_MOD'
||element.parentNode.className == 'DA_MOD_PROD' || element.className == 'DA_MOD_PROD'
|| element.parentNode.className == 'DA_MODCOLCART' || element.className == 'DA_MODCOLCART') 
			
		{
			browserName= navigator.appName;
			
			if (browserName=='Microsoft Internet Explorer')
			{
				$('popCarrello').style.top = Event.pointerY(event) - 110;
				$('popCarrello').style.left = Event.pointerX(event) + 0;
			}

			if(Event.pointerX(event) > 800)
			{
				$('popCarrello').style.left = Event.pointerX(event) - 340;
			}
			if(Event.pointerY(event) < 120)
			{
				$('popCarrello').style.top = Event.pointerY(event) + 10;
			}
			
			$('caricamentoCart').style.top = Event.pointerY(event);
			$('caricamentoCart').style.left = Event.pointerX(event) + 10;
		}
	});
	
}

function cambiaClick(elem) {
	$(elem.name + "$@$").value = elem.id;
	//alert($(elem.name + "$@$").value);
	$('popCarrello').hide();
}

function verificaAndAddToCart(nascosto) {

	var temp = nascosto.id.replace("$@$","") + "$@$";
	//alert($(temp).value);
	if ($(temp).value == "") {
		alert('Selezionare un colore');
		return false;
	}
	
	addToCart($(temp).value)
	return false;
}

function verificaProdAndAddToCart(nascosto) {

	var temp = nascosto.id.replace("$@$","") + "$@$";
	//alert($(temp).value);
	if ($(temp).value == "") {
		alert('Selezionare un prodotto');
		return false;
	}

	addToCart($(temp).value)
	return false;
}


function addToCart(prodotto) {
	$('popCarrello').hide();
	$('caricamentoCart').show();
	var data = new Date();
	toRemove = prodotto;
	prodotto = prodotto.split("$@$");
	var url;
	
	//setto il link alcarrello in base al VT (oliocarli,lineamediterranea)
	$('goToCart').href = "/m/0/controllo_cart_"+prodotto[0]+".asp"
	
	
	//ajax:	controllo la  sessione ritorna OK se va bene, il link della pagina modulo chiuso altrimenti
	url = "/m/0/Cart/AjaxCheckSession.ashx?VT="+prodotto[0]+"&COD="+prodotto[1]+"&datetime="+data.getTime();
	new Ajax.Request(url, {   
		method: 'get',   
			onSuccess: function(transport) {   
				valore = transport.responseText;
				if (valore != "OK") {
					location.href = valore;
				}
				//ajax:	controllo i cookies ritorna OK se va bene, il link della pagina check cookies altrimenti
				url = "/m/0/Cart/AjaxCheckCookie.ashx?VT="+prodotto[0]+"&COD="+prodotto[1]+"&datetime="+data.getTime();
				new Ajax.Request(url, {   
					method: 'get',   
						onSuccess: function(transport) {   
							valore = transport.responseText;
							if (valore != "OK") {
								location.href = valore;
							}
							//ajax: 	prodInserito $@$ numProdotti $@$ totProdotti
							url = "/m/0/Cart/AjaxAddToCart.ashx?VT="+prodotto[0]+"&COD="+prodotto[1]+"&datetime="+data.getTime();
							new Ajax.Request(url, {   
								method: 'get',   
									onSuccess: function(transport) {   

									    if (transport.responseText=="SESSIONE_SCADUTA")
									    {
									        location.href = "/gesErr/sessione_scaduta.htm"
									        return;
									    }
										if (transport.responseText=="PAGAMENTO_IN_CORSO")
									    {
									        location.href = "/gesErr/pagamento_in_corso.htm"
									        return;
									    }
										if (transport.responseText=="ERRORI_PAG")
									    {
									        location.href = "/gesErr/errori_pag.htm"
									        return;
									    }
										aValori = transport.responseText.split("$@$");

										//visualizza nella sezione del carrello la quantita
										$('carrelloVuoto').hide();
										$('carrelloPieno').show();
										
										visualizza2('Carrello');
										
										$('prodInserito').update(aValori[0]);
										$('numProdotti').update(aValori[1]);
										$('totProdotti').update(aValori[2]);
										
										//visualizzo la pop
										if(aValori[3] == "--1") {
											$('deleteToCart').hide();
										}
										else {
											$('deleteToCart').href = "javascript:deleteToCart(\'"+toRemove+"\');";
											$('deleteToCart').show();
										}
										$('popCarrello').show();
										$('caricamentoCart').hide();
										$('cart').show();
									}, 
									onFailure: function(transport){
									    location.href="/htm/errore_ordine.htm";
									}
							});
							
						},
			            onFailure: function(transport){
							location.href="/htm/errore_ordine.htm";
			            }
						
				});
			},
			onFailure: function(transport){
			    location.href="/htm/errore_ordine.htm";
			}
			
			
	});
	
}



function deleteToCart(prodotto) {
	$('popCarrello').hide();
	$('caricamentoCart').show();
	var data = new Date();
	prodotto = prodotto.split("$@$");
	var url;
	
	//setto il link alcarrello in base al VT (oliocarli,lineamediterranea)
	$('goToCart').href = "/m/0/controllo_cart_"+prodotto[0]+".asp"
	
	
	//ajax:	controllo la  sessione ritorna OK se va bene, il link della pagina modulo chiuso altrimenti
	url = "/m/0/Cart/AjaxCheckSession.ashx?VT="+prodotto[0]+"&COD="+prodotto[1]+"&datetime="+data.getTime();
	new Ajax.Request(url, {   
		method: 'get',   
			onSuccess: function(transport) {   
				valore = transport.responseText;
				if (valore != "OK") {
					location.href = valore;
				}
				//ajax:	controllo i cookies ritorna OK se va bene, il link della pagina check cookies altrimenti
				url = "/m/0/Cart/AjaxCheckCookie.ashx?VT="+prodotto[0]+"&COD="+prodotto[1]+"&datetime="+data.getTime();
				new Ajax.Request(url, {   
					method: 'get',   
						onSuccess: function(transport) {
							valore = transport.responseText;
							if (valore != "OK") {
								location.href = valore;
							}
							//ajax: 	prodInserito $@$ numProdotti $@$ totProdotti
							url = "/m/0/Cart/AjaxDeleteToCart.ashx?VT="+prodotto[0]+"&COD="+prodotto[1]+"&datetime="+data.getTime();
							new Ajax.Request(url, {   
								method: 'get',   
									onSuccess: function(transport) {
									
									    if (transport.responseText=="SESSIONE_SCADUTA")
									    {
									        location.href = "/gesErr/sessione_scaduta.htm"
									        return;
									    }
										if (transport.responseText=="PAGAMENTO_IN_CORSO")
									    {
									        location.href = "/gesErr/pagamento_in_corso.htm"
									        return;
									    }
										if (transport.responseText=="ERRORI_PAG")
									    {
									        location.href = "/gesErr/errori_pag.htm"
									        return;
									    }									
										aValori = transport.responseText.split("$@$");
										$('prodInserito').update(aValori[0]);
										$('numProdotti').update(aValori[1]);
										$('totProdotti').update(aValori[2]);
										//visualizzo la pop
										$('deleteToCart').hide();
										$('popCarrello').show();
										$('caricamentoCart').hide();
										$('cart').show();
									},
			                        onFailure: function(transport){
			                            location.href="/htm/errore_ordine.htm";
			                        }
									
							});
							
						},
			            onFailure: function(transport){
			                location.href="/htm/errore_ordine.htm";
			            }
						 
				});
			},
			onFailure: function(transport){
			    location.href="/htm/errore_ordine.htm";
			}
			
	});
	
}





