/*-----------------------------------------------*/
/*------------ CARRELLO DELLA SPESA -------------*/
/*-----------------------------------------------*/

function EvidenziaCarrello(elemento){

// EVIDENZIO IL DIV DEL CARRELLO DELLA SPESA

new Element.hide(elemento);
new Effect.Appear(elemento);

// -----------------------------------------

}

function ApriPopUpMessaggio(elemento,id_prodotto){

Messaggio = "";

if (document.getElementById("qta_"+id_prodotto+"_agg").value > 1) {
	Messaggio = Messaggio + "Sono stati aggiunti al carrello n° <strong>"+document.getElementById("qta_"+id_prodotto+"_agg").value+"</strong> articoli.<br>"
}else{
	Messaggio = Messaggio + "E' stato aggiunto al carrello n° <strong>"+document.getElementById("qta_"+id_prodotto+"_agg").value+"</strong> articolo.<br>"
}

Messaggio = Messaggio + "Codice articolo aggiunto <strong>"+document.getElementById("cod_"+id_prodotto+"_agg").value+"</strong>"

document.getElementById(elemento).innerHTML = Messaggio;

// EVIDENZIO IL DIV DEL MESSAGGIO DEL CARRELLO DELLA SPESA
//new Effect.Appear(elemento);
document.getElementById(elemento).style.display = "block";
// -------------------------------------------------------
//setTimeout("new Effect.Fade("+elemento+");",5000);

	if ( document.getElementById(elemento) != null ) {
		setTimeout("document.getElementById('"+elemento+"').style.display = 'none';",3000);
	}

	if ( document.getElementById("qta_"+id_prodotto+"_agg") != null ) {
		setTimeout("document.getElementById('qta_"+id_prodotto+"_agg').value = '1';",3000);
	}

}

function GestioneCarrello(UrlOperazione,id_prodotto,Quantita,Misura,azione){

createXMLHttpRequest();
								
											if (azione == 'aggiungi'){
																								
												quantita_selezionata_agg = Quantita;
												id_misura_selezionata = Misura;

												if (quantita_selezionata_agg == ''){
													alert("Inserire la quantita' !");
													document.getElementById("qta_"+id_prodotto+"_agg").focus();
													return false;
												}	
										
												if (!IsNumber(quantita_selezionata_agg)){
													alert("La quantita' non e' corretta!");
													document.getElementById("qta_"+id_prodotto+"_agg").focus();
													return false;
												}
							
												if (quantita_selezionata_agg < 0){
													alert("La quantita' deve essere maggiore di 0!");
													document.getElementById("qta_"+id_prodotto+"_agg").focus();
													return false;
												}
												
												UrlOperazione = UrlOperazione + "&quantita="+quantita_selezionata_agg+"&id_misura="+id_misura_selezionata+"&azione="+azione
												
													xmlHttp.onreadystatechange = handleStateChangeCarrelloAdd;
													xmlHttp.open("GET",UrlOperazione, true);
													xmlHttp.send(null);
												
												document.getElementById("qta_"+id_prodotto+"_agg").value="1";
												
//												EvidenziaCarrello('ListaProdottiCarrello');
												ApriPopUpMessaggio('Messaggio_Carrello_'+id_prodotto,id_prodotto);

											}
											
											
											if (azione == 'cancella'){
											
												quantita_selezionata_rem = document.getElementById("qta_"+id_prodotto+"_rem").value;
												id_misura_selezionata = document.getElementById("id_misura_"+id_prodotto).value;

												var agree=confirm('Eliminare articolo dal carrello?');
												
													if (agree == true){
										
														UrlOperazione = UrlOperazione + "&quantita="+quantita_selezionata_rem+"&id_misura="+id_misura_selezionata+"&azione="+azione

														xmlHttp.onreadystatechange = handleStateChangeCarrello;
														xmlHttp.open("GET",UrlOperazione, true);
														xmlHttp.send(null);
														
													}
											
											}
											
											if (azione == 'ricalcola'){
							
												quantita_selezionata_nuova = document.getElementById("qta_"+id_prodotto+"_nuova").value;
												id_misura_selezionata = document.getElementById("id_misura_"+id_prodotto).value;

												if ( document.getElementById("id_misura_"+id_prodotto+"_old") != null ) {
													id_misura_selezionata_vecchia = document.getElementById("id_misura_"+id_prodotto+"_old").value;
												}
							
													if (quantita_selezionata_nuova == ''){
														alert("Inserire la quantita' !");
														document.getElementById("qta_"+id_prodotto+"_nuova").focus();
														return false;
													}	
											
													if (!IsNumber(quantita_selezionata_nuova)){
														alert("La quantita' non e' corretta!");
														document.getElementById("qta_"+id_prodotto+"_nuova").focus();
														return false;
													}
							
													if (quantita_selezionata_nuova < 0){
														alert("La quantita' deve essere maggiore di 0!");
														document.getElementById("qta_"+id_prodotto+"_nuova").focus();
														return false;
													}
													
														UrlOperazione = UrlOperazione + "&quantita="+quantita_selezionata_nuova+"&id_misura_nuova="+id_misura_selezionata+"&id_misura_vecchia="+id_misura_selezionata_vecchia+"&azione="+azione

														xmlHttp.onreadystatechange = handleStateChangeCarrello;
														xmlHttp.open("GET",UrlOperazione, true);
														xmlHttp.send(null);
											
											}
											
											if (azione == 'svuota'){
												
												var agree=confirm('Svuotare il carrello?');

													if (agree == true){
														
														UrlOperazione = UrlOperazione + "&azione="+azione+"&logout=1"
												
														xmlHttp.onreadystatechange = handleStateChangeCarrello;
														xmlHttp.open("GET", UrlOperazione, true);
														xmlHttp.send(null);
														
													}
											
											}
											
}

function handleStateChangeCarrelloAdd(){

Messaggio = "";
Messaggio = Messaggio + "<table border='0' cellpadding='0' cellspacing='0' style='width:100%; FONT-SIZE: 11px;COLOR: #000000; FONT-FAMILY: Trebuchet MS, Verdana;'>"
Messaggio = Messaggio + "  <tr><td  style='padding-top:25px;'>"
Messaggio = Messaggio + "<center>Impossibile caricare lista prodotti ....</center>"
Messaggio = Messaggio + "  </td></tr>"
Messaggio = Messaggio + "</table>"

Caricamento = "";
Caricamento = Caricamento + "<table border='0' cellpadding='0' cellspacing='0' style='width:100%; FONT-SIZE: 11px;COLOR: #000000; FONT-FAMILY: Trebuchet MS, Verdana;'>"
Caricamento = Caricamento + "  <tr><td>"
Caricamento = Caricamento + "<center>loading products ...</center>"
Caricamento = Caricamento + "  </td></tr>"
Caricamento = Caricamento + "</table>"


		// if xmlHttp shows "loaded"
		if (xmlHttp.readyState == 4){

			if (xmlHttp.status == 200){				

				document.getElementById("ListaProdottiCarrello").innerHTML = xmlHttp.responseText;
				
			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------

				if (document.getElementById("ListaProdottiCarrello") != null){
					document.getElementById("ListaProdottiCarrello").innerHTML = Messaggio;
				}

			}
		
		}else{
			
				if (document.getElementById("ListaProdottiCarrello") != null){
					document.getElementById("ListaProdottiCarrello").innerHTML = Caricamento
				}
			
		}

}

function handleStateChangeCarrello(){

Messaggio = "";
Messaggio = Messaggio + "<table border='0' cellpadding='0' cellspacing='0' style='width:100%; FONT-SIZE: 11px;COLOR: #000000; FONT-FAMILY: Trebuchet MS, Verdana;'>"
Messaggio = Messaggio + "  <tr><td  style='padding-top:25px;'>"
Messaggio = Messaggio + "<center>Impossibile caricare lista prodotti ....</center>"
Messaggio = Messaggio + "  </td></tr>"
Messaggio = Messaggio + "</table>"

Caricamento = "";
Caricamento = Caricamento + "<table border='0' cellpadding='0' cellspacing='0' style='width:100%; FONT-SIZE: 11px;COLOR: #000000; FONT-FAMILY: Trebuchet MS, Verdana;'>"
Caricamento = Caricamento + "  <tr><td>"
Caricamento = Caricamento + "<center>loading products ...</center>"
Caricamento = Caricamento + "  </td></tr>"
Caricamento = Caricamento + "</table>"


		// if xmlHttp shows "loaded"
		if (xmlHttp.readyState == 4){

			if (xmlHttp.status == 200){				

				document.getElementById("ListaProdottiCarrello").innerHTML = xmlHttp.responseText;

				location.href = 'default.asp?id_pagina='+document.getElementById("id_pagina_carrello").value;
				
			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------

				if (document.getElementById("ListaProdottiCarrello") != null){
					document.getElementById("ListaProdottiCarrello").innerHTML = Messaggio;
				}

			}
		
		}else{
			
				if (document.getElementById("ListaProdottiCarrello") != null){
					document.getElementById("ListaProdottiCarrello").innerHTML = Caricamento
				}
			
		}

}

/*-----------------------------------------------*/
/*-----------------------------------------------*/
/*-----------------------------------------------*/

function IsNumber(num)
{
   var i = 0;
   var letter;

   while (i < num.length)
   {
      letter = num.charAt(i);

      if ((letter == '0') || (letter == '1') || (letter == '2') ||
          (letter == '3') || (letter == '4') || (letter == '5') ||
          (letter == '6') || (letter == '7') || (letter == '8') ||
          (letter == '9') || (letter == '+') || (letter == '.') ||
          (letter == '/') || (letter == ',') || (letter == ' '))
      {
         i++;
      }
      else
      {
         return false;
      }
   }

   return true;
}
