/*-----------------------------------------------*/
/*------------ CARRELLO DELLA SPESA -------------*/
/*-----------------------------------------------*/

function GestioneCarrello(id_prodotto,azione){

createXMLHttpRequest();

//	if ((document.getElementById("id_utente_accesso").value != '') && (document.getElementById("id_utente_accesso") != null)){
	if (document.getElementById("id_utente_accesso") != null){

			if (document.getElementById("id_utente_accesso").value != ''){
								
											if (azione == 'aggiungi'){
																								
												quantita_selezionata_agg = document.getElementById("qta_"+id_prodotto+"_agg").value;
												
												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;
												}
										
													xmlHttp.onreadystatechange = handleStateChangeCarrello;
													xmlHttp.open("GET", "carrello.asp?id_utente_carrello="+document.getElementById("id_utente_accesso").value+"&id_prodotto="+id_prodotto+"&quantita="+quantita_selezionata_agg+"&azione="+azione, true);
													xmlHttp.send(null);
													
													document.getElementById("qta_"+id_prodotto+"_agg").value = '';
											
											}
											
											
											if (azione == 'cancella'){
											
												quantita_selezionata_rem = document.getElementById("qta_"+id_prodotto+"_rem").value;
												var agree=confirm('Eliminare articolo dal carrello ?');
												
													if (agree == true){
										
														xmlHttp.onreadystatechange = handleStateChangeCarrello;
														xmlHttp.open("GET", "carrello.asp?id_utente_carrello="+document.getElementById("id_utente_accesso").value+"&id_prodotto="+id_prodotto+"&quantita="+quantita_selezionata_rem+"&azione="+azione, true);
														xmlHttp.send(null);
														
													}
											
											}
											
											if (azione == 'ricalcola'){
							
												quantita_selezionata_nuova = document.getElementById("qta_"+id_prodotto+"_nuova").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;
													}
													
														xmlHttp.onreadystatechange = handleStateChangeCarrello;
														xmlHttp.open("GET", "carrello.asp?id_utente_carrello="+document.getElementById("id_utente_accesso").value+"&id_prodotto="+id_prodotto+"&quantita="+quantita_selezionata_nuova+"&azione="+azione, true);
														xmlHttp.send(null);
											
											}
											
											if (azione == 'svuota'){
												
												var agree=confirm('Attenzione, tutti gli articoli presenti in carrello andranno persi.\nEffettuare logout ?');
												
													if (agree == true){
							
														if ((document.getElementById("visualizza_menu").value == '1') && (document.getElementById("visualizza_menu") != null)){ 
															new Effect.SlideUp('Ricerca');
															document.getElementById("visualizza_menu").value = "0";

															if (document.getElementById("id_utente_accesso") !=null) {
																document.getElementById("id_utente_accesso").value = '';
															}
															
														}
														
														xmlHttp.onreadystatechange = handleStateChangeCarrelloSvuota;
														xmlHttp.open("GET", "carrello.asp?id_utente_carrello="+document.getElementById("id_utente_accesso").value+"&azione="+azione+"&logout=1", true);
														xmlHttp.send(null);

													}
											
											}
				
			}else{

					var ElementoNascosto = document.getElementById("Effettuare_Login_0");
					ElementoNascosto.style.display = "block";
					
					var ElementoNascosto = document.getElementById("Effettuare_Login");
					ElementoNascosto.style.display = "block";
	
					xmlHttp.onreadystatechange = handleStateChangeLogoutPopUp;
					xmlHttp.open("GET", "login2.asp"+UrlPagina, true);
					xmlHttp.send(null);
	
					if (document.getElementById("qta_"+id_prodotto+"_agg") !=null) {
						document.getElementById("qta_"+id_prodotto+"_agg").value = '';
					}

				}
				
	}else{

				var ElementoNascosto = document.getElementById("Effettuare_Login_0");
				ElementoNascosto.style.display = "block";
				
				var ElementoNascosto = document.getElementById("Effettuare_Login");
				ElementoNascosto.style.display = "block";

				xmlHttp.onreadystatechange = handleStateChangeLogoutPopUp;
				xmlHttp.open("GET", "login2.asp"+UrlPagina, true);
				xmlHttp.send(null);

				if (document.getElementById("qta_"+id_prodotto+"_agg") !=null) {
					document.getElementById("qta_"+id_prodotto+"_agg").value = '';
				}

	}
}


function Chiudi_Pop_Up(){
	var ElementoNascosto = document.getElementById("Effettuare_Login_0");
	ElementoNascosto.style.display = "none";
	
	var ElementoNascosto = document.getElementById("Effettuare_Login");
	ElementoNascosto.style.display = "none";
}

// CASO IN CUI IL CREDITO E' INSUFFICIENTE
function Conferma_Ordine_Frase(){

	alert("Impossibile proseguire !\nSaldo punti insufficiente.");

}

//CASO IN CUI SVUOTO IL CARRELLO SENZA EFFETTUARE IL LOGOUT
function Svuota_Carrello(){

createXMLHttpRequest();

var agree=confirm("Eliminare tutti gli articoli dal carrello ?");

	if (agree == true){
		xmlHttp.onreadystatechange = handleStateChangeCarrelloSvuota2;
		xmlHttp.open("GET", "carrello.asp?id_utente_carrello="+document.getElementById("id_utente_accesso").value+"&azione=svuota", true);
		xmlHttp.send(null);
		
	}
}

//VISUALIZZA CONTENUTO CARRELLO
function Visualizza_Carrello_Aggiornato(){

createXMLHttpRequest();

	xmlHttp.onreadystatechange = handleStateChangeCarrelloAggiornato;
	xmlHttp.open("GET", "carrello.asp?azione=svuota", true);
	xmlHttp.send(null);

}

//COMPLETA ORDINE
function Completa_Ordine(){

createXMLHttpRequest();

var agree=confirm("Procedere con l' ordine ?");

	if (agree == true){

		CaricaPaginaGlobale("id_pagina_riepilogo_ordine");

	}

}

//CONCLUSIONE ORDINE
function Conferma_Ordine(){

createXMLHttpRequest();

var agree=confirm("Procedere con l' ordine ?");

	if (agree == true){

		UrlPagina = "?id_pagina="+document.getElementById("id_pagina_conferma_ordine").value+"&Lang="+document.getElementById("Lingua").value
		var	postData = "nuovo_saldo_punti="+document.getElementById("nuovo_saldo_punti").value

		xmlHttp.open("POST", "ordini.asp"+UrlPagina, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", postData.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.onreadystatechange = handleStateChangeCaricaOrdine;
		xmlHttp.send(postData);
		
	}

}


function Ricarica(Elemento){
	alert(Elemento);	
}

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;
				
					// CASO IN CUI SONO NELLA PAGINA DI RIEPILOGO DELL'ORDINE E CANCELLO UN ARTICOLO
					// RICARICO LA PAGINA STESSA CON LA QUANTITA DELLA RIGA AGGIORNATA
					if (document.getElementById("ControlloPaginaRiepilogo")!=null){
						CaricaPaginaGlobale("id_pagina_riepilogo_ordine");
					}


			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------
				
				document.getElementById("ListaProdottiCarrello").innerHTML = Messaggio;
			}
		
		}else{

				document.getElementById("ListaProdottiCarrello").innerHTML = Caricamento
				
		}

}


function handleStateChangeCarrelloSvuota(){

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;
				
				Check_Logout();

			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------
				
				document.getElementById("ListaProdottiCarrello").innerHTML = Messaggio;
			}
		
		}else{

				document.getElementById("ListaProdottiCarrello").innerHTML = Caricamento
				
		}

}

function handleStateChangeCarrelloSvuota2(){

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;

					// CASO IN CUI SONO NELLA PAGINA DI RIEPILOGO DELL'ORDINE E CANCELLO UN ARTICOLO
					// RICARICO LA PAGINA STESSA CON LA QUANTITA DELLA RIGA AGGIORNATA
					if (document.getElementById("ControlloPaginaRiepilogo")!=null){
						CaricaPaginaGlobale("id_pagina_riepilogo_ordine");
					}


			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------
				
				document.getElementById("ListaProdottiCarrello").innerHTML = Messaggio;
			}
		
		}else{

				document.getElementById("ListaProdottiCarrello").innerHTML = Caricamento
				
		}

}

function handleStateChangeCarrelloAggiornato(){

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;
				
//				Check_Accedi_Area();
//				CaricaPaginaGlobale("id_pagina_conferma_ordine");
				Check_Accedi_Area_Inserimento_Codice();
				
			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------
				
				document.getElementById("ListaProdottiCarrello").innerHTML = Messaggio;
			}
		
		}else{

				document.getElementById("ListaProdottiCarrello").innerHTML = Caricamento
				
		}

}

function handleStateChangeCaricaOrdine(){

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>"
Messaggio = Messaggio + "<center>Impossibile caricare la pagina richiesta ....</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 ...</center>"
Caricamento = Caricamento + "  </td></tr>"
Caricamento = Caricamento + "</table>"


		// if xmlHttp shows "loaded"
		if (xmlHttp.readyState == 4){

			if (xmlHttp.status == 200){				
				document.getElementById("Contenuto_Pagina").innerHTML = xmlHttp.responseText;

				Visualizza_Carrello_Aggiornato();

			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------
				
				document.getElementById("Contenuto_Pagina").innerHTML = Messaggio;
			}
		
		}else{
				document.getElementById("Contenuto_Pagina").innerHTML = Caricamento
		}

}

function handleStateChangeLogoutPopUp(){

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>"
Messaggio = Messaggio + "<center>Impossibile caricare la pagina richiesta ....</center>"
Messaggio = Messaggio + "  </td></tr>"
Messaggio = Messaggio + "</table>"

Caricamento = "";
Caricamento = Caricamento + "<table border='0' cellpadding='0' cellspacing='0' style='margin-top:8px; width:100%; FONT-SIZE: 11px;COLOR: #000000; FONT-FAMILY: Trebuchet MS, Verdana;'>"
Caricamento = Caricamento + "  <tr><td>"
Caricamento = Caricamento + "<center>loading ...</center>"
Caricamento = Caricamento + "  </td></tr>"
Caricamento = Caricamento + "</table>"


		// if xmlHttp shows "loaded"
		if (xmlHttp.readyState == 4){

			if (xmlHttp.status == 200){				

				document.getElementById("Effettuare_Login").innerHTML = xmlHttp.responseText;

			}else{	

				// INTERCETTO ERRORE E VISUALIZZO
				handleErrFullPage(xmlHttp.responseText);
				//-------------------------------
				
				document.getElementById("Effettuare_Login").innerHTML = Messaggio;
			}
		
		}else{
				document.getElementById("Effettuare_Login").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;
}