// JavaScript Document

//Imposto le variabili 
var spese_trasporto = 7.50;
var valuta = "&euro;";


/////////////////////////////////////////
//a. Funzione di controllo dell'ordine //

//a.1 Calcolo totale ordine
	var elenco_vini=1;
	var min_ord=1;

//Controllo della somma







/////////////////////////////////////////////////////////////
//b. Funzione per la disabilitazione/abilitazione del Form //

//b.1 Funzione per il cambio di stato del form
// true=disabilita il form
// false=lo attiva
	function form_disabled(status)
	{
		
//	alert(status);	
		
	  document.ordine.TES_ORD_NOME.readOnly=status;
	  document.ordine.TES_ORD_COGNOME.readOnly=status;
	  document.ordine.TES_ORD_INDIRIZZO.readOnly=status;
	  document.ordine.TES_ORD_CIVICO.readOnly=status;
	  document.ordine.TES_ORD_LOCALITA.readOnly=status;
	  document.ordine.TES_ORD_CAP.readOnly=status;
	  document.ordine.TES_ORD_NAZIONE.readOnly=status;
	  document.ordine.TES_ORD_FRAZIONE.readOnly=status;
	  document.ordine.TES_ORD_PRESSO.readOnly=status;
	  document.ordine.TES_ORD_TELEFONO.readOnly=status;
	  document.ordine.TES_ORD_EMAIL.readOnly=status;
	  
	  if (status == true) {
		  
		  document.ordine.TES_ORD_NOME.className = "inputC inputDisabled";
		 	document.ordine.TES_ORD_COGNOME.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_INDIRIZZO.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_CIVICO.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_LOCALITA.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_CAP.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_NAZIONE.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_FRAZIONE.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_PRESSO.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_TELEFONO.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_EMAIL.className = "inputC inputDisabled";
		  document.ordine.TES_ORD_PROV.className = "inputC inputDisabled";
		  
		  document.getElementById("showFattura").style.display = "none";
  	      document.getElementById("showConsegna").style.display = "none";
		  
		  
	  } else {
		  
		  
		  document.ordine.TES_ORD_NOME.className = "";
		  document.ordine.TES_ORD_COGNOME.className = "";
		  document.ordine.TES_ORD_INDIRIZZO.className = "";
		  document.ordine.TES_ORD_CIVICO.className = "";
		  document.ordine.TES_ORD_LOCALITA.className = "";
		  document.ordine.TES_ORD_CAP.className = "";
		  document.ordine.TES_ORD_NAZIONE.className = "";
		  document.ordine.TES_ORD_FRAZIONE.className = "";
		  document.ordine.TES_ORD_PRESSO.className = "";
		  document.ordine.TES_ORD_TELEFONO.className = "";
		  document.ordine.TES_ORD_EMAIL.className = "";
  		  document.ordine.TES_ORD_PROV.className = "";
		  
  		  document.getElementById("showFattura").style.display = "inline";
  	      document.getElementById("showConsegna").style.display = "inline";
		  
	  }
	  
	  
	  
	  
	}

/////////////////////////////////////////////////////////////
//c. Sezione per la convalida dei dati personali - vers. IT//


//c.1 Controllo di una mail
	function check_email(email){
		if	(
			 	(email.indexOf("@") == -1) || 
				(email.charAt  == ".") || 
				(email.charAt  == "@") || 
				(email.indexOf(".") == -1) || 
				(email.charAt(email.indexOf("@")+1) == ".") || 
				(email.charAt(email.indexOf("@")-1) == ".")
			)	return false;
		else return true;
	}



function ControllaCF(cf)
{

	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return false;
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		{
		alert( "La lunghezza del codice fiscale non è\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n");
						return false;
		}
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
		{
			alert( "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n");
								return false;
								}
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
	{
	alert("Il codice fiscale non è corretto:\n"+
			"il codice di controllo non corrisponde.\n");
							return false;
		}
	return true;
}


function ControllaPIVA(pi)
{
	if( pi == '' )  return false;
	if( pi.length != 11 )
	{
		alert ("La lunghezza della partita IVA non è\n" +
			"corretta: la partita IVA dovrebbe essere lunga\n" +
			"esattamente 11 caratteri.\n");
	return false;
	}
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
		if( validi.indexOf( pi.charAt(i) ) == -1 )
		{
			alert( "La partita IVA contiene un carattere non valido `" +
				pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n");
				return false;
		}
	}
	s = 0;
	for( i = 0; i <= 9; i += 2 )
		s += pi.charCodeAt(i) - '0'.charCodeAt(0);
	for( i = 1; i <= 9; i += 2 ){
		c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
		if( c > 9 )  c = c - 9;
		s += c;
	}
	if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) )
	{
		alert( "La partita IVA non è valida:\n" +
			"il codice di controllo non corrisponde.\n");
					return false;
	}
	return true;
}




//c.2 Controllo della privacy
	function check_privacy(){
	//	if (!document.ordine.TES_ORD_PRIVACY_01.checked)	return false;
	//	else return true;
	}
	
	
	function validaFattura()
	{
		
		if (document.getElementById("checkfattura").checked)
        {
		
			vsCodFisc = document.getElementById("codice_fiscale").value;
			vsPIva = document.getElementById("partita_iva").value;
			
			if (!document.getElementById("ORD_FATT_RAGSOC") || (document.getElementById("ORD_FATT_RAGSOC").value==""))
			{
				 alert("Il campo Ragione Sociale è obbligatorio");
				 return false;
			}
			else if (!document.getElementById("ORD_FATT_INDIRIZZO") || (document.getElementById("ORD_FATT_INDIRIZZO").value==""))
			{
				 alert("Il campo Indirizzo Fiscale è obbligatorio");
				 return false;
			}
			else if (!document.getElementById("ORD_FATT_LOCALITA") || (document.getElementById("ORD_FATT_LOCALITA").value==""))
			{
				 alert("Il campo Localita Fiscale è obbligatorio");
				 return false;
			}
			else if (!document.getElementById("ORD_FATT_PROV") || (document.getElementById("ORD_FATT_PROV").value==""))
			{
				 alert("Il campo Provincia Fiscale è obbligatorio");
				 return false;
			}
			else if (!document.getElementById("ORD_FATT_CAP") || (document.getElementById("ORD_FATT_CAP").value==""))
			{
				 alert("Il campo CAP Fiscale è obbligatorio");
				 return false;
			}
			else if (vsPIva == "")
			{
				 alert("Il campo Partita iva è obbligatorio");
				 return false;
			}
			else if (vsPIva == "")
			{
				 alert("Il campo Codice Fiscale è obbligatorio");
				 return false;
			}
			else if (ControllaPIVA(vsPIva))
			{
				if((vsCodFisc.length == 11) && ControllaPIVA(vsCodFisc))
					return true;
				else if((vsCodFisc.length == 16) && ControllaCF(vsCodFisc))
						return true;
				else
				{
					alert("Il codice fiscale non è corretto")
					return false; 
				}	   		
			}	
			else
				return false;
			
			return true;
		} else {
			return true;
		}
			
		
		
	}
	
function check_consegna() {
	
	
	if (document.getElementById("consegna1").checked)
    {
		

		if (document.getElementById("TES_CONS_NOME").value == "" || document.getElementById("TES_CONS_COGNOME").value == "" || document.getElementById("TES_CONS_INDIRIZZO").value == "" || document.getElementById("TES_CONS_LOCALITA").value == "")
		{
				 alert("I dati di consegna non sono corretti");
				 return false;
		} else {
				return true;	
		}
		 
	} else {
			return true;
	}
	

}

	

//c.3 Funzione di controllo dei dati personali
function data_check(){
		if	(document.ordine.TES_ORD_NOME.value=="") alert("Attenzione, devi inserire il Nome!");
		else if	(document.ordine.TES_ORD_COGNOME.value=="") alert("Attenzione, devi inserire il Cognome!");
		else if	(document.ordine.TES_ORD_INDIRIZZO.value=="") alert("Attenzione, devi inserire l'Indirizzo!");
		else if	(document.ordine.TES_ORD_CIVICO.value=="") alert("Attenzione, devi inserire il Numero Civico!");
		else if	(document.ordine.TES_ORD_CAP.value=="") alert("Attenzione, devi inserire il CAP!");
		else if	(document.ordine.TES_ORD_LOCALITA.value=="") alert("Attenzione, devi inserire la Localita'!");
		else if	(document.ordine.TES_ORD_TELEFONO.value=="") alert("Attenzione, devi inserire il Numero di Telefono!");
		else if	(!check_email(document.ordine.TES_ORD_EMAIL.value)) alert("Attenzione, devi inserire un indirizzo Email valido!");
		else if (!validaFattura()) return false;
		else if (!check_consegna()) return false;
	//	else if (!check_privacy()) alert("Attenzione, devi esprimere il tuo parere sulla Privacy!");	
		else return true;
	}
	

/////////////////////////////////////////////////////////////////
//d. Submit del Form
//
//  order_check() -> controllo del carrello
//  data_check() -> controllo dei dati personali


	function order_submit()
    {
		if (data_check())	document.ordine.submit();
	}
	

/////////////////////////////////////////////////////////////////
//e. Funzioni di informazioni varie fornite in landing pages

	function print_total_order(){		
	
		
	//	var totale_ordine_sconto=totale_ordine*0.9;
	//	totale_ordine_sconto=Math.round(totale_ordine_sconto*100)/100;
		
		
		var totale_finale=totale_ordine+spese_trasporto;
		totale_finale=Math.round(totale_finale*100)/100;
		if ((check_ordine_multiplo(totale_bottiglie))&&(check_ordine_minimo(totale_bottiglie))) {
				sign="<a href='#buono'><img src='immagini/layout/ok.jpg' width=20 height=20 border=0></a>";			
		} else { 
			sign="<a href='#' onClick='javascript:check_ordine();'><img src='immagini/layout/warning.gif' width=20 height=20 border=0></a>";
		}
		
		if (totale_bottiglie > 35) {
				//document.getElementById("regalo").style.visibility = "visible";
		} else { 

			//document.getElementById("regalo").style.visibility = "hidden";
		}
		
		
		document.getElementById("totale_bottiglie").innerHTML=sign+" "+totale_bottiglie;		
		document.getElementById("totale_ordine").innerHTML=valuta+" "+totale_ordine;
	//	document.getElementById("totale_ordine_sconto").innerHTML=valuta+" "+totale_ordine_sconto;
		document.getElementById("totale_finale").innerHTML=valuta+" "+totale_finale;
		}
