function controllocampi()
	{var tuttoOK = true
	 var stringa_codice_fiscale = document.order_form.cli_codice_fiscale.value;
	 var stringa_partita_iva = document.order_form.cli_partita_iva.value;
	 stringa_codice_fiscale = stringa_codice_fiscale.toUpperCase();
	 document.order_form.cli_codice_fiscale.value = stringa_codice_fiscale;
		if (((document.order_form.cli_pagamento[0].checked == false) && (document.order_form.cli_pagamento[1].checked == false) && (document.order_form.cli_pagamento[2].checked == false)) && (tuttoOK == true))
			{
				window.alert('Indicare una modalitā di pagamento.');
				tuttoOK = false
			}
		if (((document.order_form.cli_ragione_sociale.value == '') && (document.order_form.cli_cognome.value == '')) && (tuttoOK == true))
			{
				window.alert('I campi \'Ragione Sociale\' e \'Cognome\' non possono essere contemporaneamente vuoti)');
				tuttoOK = false
			}
		if ((document.order_form.cli_cognome.value == '') && (tuttoOK == true))
			{
				window.alert('Cognome utente o responsabile azienda mancante');
				tuttoOK = false
			}
		if ((document.order_form.cli_nome.value == '') && (tuttoOK == true))
			{
				window.alert('Nome utente o responsabile azienda mancante');
				tuttoOK = false
			}
		if ((document.order_form.cli_indirizzo.value == '') && (tuttoOK == true))
			{
				window.alert('Manca l\'indirizzo');
				tuttoOK = false
			}
		if ((document.order_form.cli_cap.value == '') && (tuttoOK == true))
			{
				window.alert('Scrivere il cap');
				tuttoOK = false
			}
		if ((document.order_form.cli_citta.value == '') && (tuttoOK == true))
			{
				window.alert('Scrivere la cittā');
				tuttoOK = false
			}
		if ((document.order_form.cli_provincia.value == '') && (tuttoOK == true))
			{
				window.alert('Scrivere la provincia');
				tuttoOK = false
			}
		
		if (((document.order_form.cli_indirizzo2.value != '') && (document.order_form.cli_cap2.value == '')) && (tuttoOK == true))
			{
				window.alert('Manca il CAP per la consegna');
				tuttoOK = false
			}
		if (((document.order_form.cli_indirizzo2.value != '') && (document.order_form.cli_citta2.value == '')) && (tuttoOK == true))
			{
				window.alert('Manca la cittā per la consegna');
				tuttoOK = false
			}
		if (((document.order_form.cli_indirizzo2.value != '') && (document.order_form.cli_provincia2.value == '')) && (tuttoOK == true))
			{
				window.alert('Manca la provincia per la consegna');
				tuttoOK = false
			}
		
		if (((document.order_form.cli_indirizzo2.value == '') && (document.order_form.cli_provincia2.value != '' || document.order_form.cli_citta2.value != '' || document.order_form.cli_provincia2.value != '' || document.order_form.cli_cap2.value != '')) && (tuttoOK == true))
			{
				window.alert('L\'indirizzo per la spedizione non č completo.');
				tuttoOK = false
			}
		
		
		// ***********************
		
		if ((document.order_form.cli_ragione_sociale.value != '')){ //cliente aziendale

			if (((document.order_form.cli_codice_fiscale.value == '') || (document.order_form.cli_partita_iva.value == ''))  && (tuttoOK == true))
				{
					window.alert('Scrivere sia il codice fiscale che la partita IVA,\nanche se coincidono.');
					tuttoOK = false
				}
		} else { // cliente normale
			if (((document.order_form.cli_codice_fiscale.value == ''))  && (tuttoOK == true))
				{
					window.alert('Scrivere il codice fiscale');
					tuttoOK = false
				}
		}
				
		if ((document.order_form.cli_codice_fiscale.value !='') && (tuttoOK == true))
			{
				if (stringa_codice_fiscale.length < 16) 
					{
						if (stringa_codice_fiscale.length == 11) 
							{
								if (controllaPIVA(stringa_codice_fiscale) == 'KO')
									{
										window.alert('Il Codice Fiscale inserito non č corretto');
										tuttoOK = false
									}
							}
						else
							{
								window.alert('Il Codice Fiscale non rispetta i parametri di lunghezza richiesti');
								tuttoOK = false
							}
					}
				else
					{
					if (controllaCF(stringa_codice_fiscale) == 'KO')
						{
							window.alert('Il Codice Fiscale inserito non č corretto');
							tuttoOK = false
						}
					}
			}
			
			if (((document.order_form.cli_partita_iva.value != '') && (stringa_partita_iva.length < 11))  && (tuttoOK == true))
				{
					window.alert('La partita IVA contiene meno di 11 cifre');
					tuttoOK = false
				}
				
			if (((document.order_form.cli_partita_iva.value != '') && (controllaPIVA(stringa_partita_iva) == 'KO'))  && (tuttoOK == true))
				{
					window.alert('La partita IVA non č corretta');
					tuttoOK = false
				}
				
		
		
		
		if ((ControllaMail(document.order_form.cli_email.value) == 'KO') && (tuttoOK == true))
			{
				window.alert('L\'indirizzo di posta elettronica inserito\nnon sembra essere valido.\nVi preghiamo di controllarlo. Grazie');
				tuttoOK = false
			}
		if ((document.order_form.cli_email.value != document.order_form.cli_email2.value) && (tuttoOK == true))
			{
				window.alert('L\'email e la conferma non corrispondono, controllare gli indirizzi');
				tuttoOK = false
			}
		if ((document.order_form.cli_telefono.value == '') && (tuttoOK == true))
			{
				window.alert('Scrivere il telefono');
				tuttoOK = false
			}
		if ((document.order_form.cli_privacy[0].checked == false) && (tuttoOK == true))
			{
				window.alert('Per effettuare l\'ordine č necessario aver letto\ne approvato la normativa sulla privacy\nlegge 196/03.)');
				tuttoOK = false
			}
		if ((document.order_form.cli_privacy2[0].checked == false) && (tuttoOK == true))
			{
				window.alert('Per effettuare l\'ordine č necessario esprimere il consenso al trattamento dei dati\ncome indicato dalla normativa sulla privacy (legge 196/03).)');
				tuttoOK = false
			}
		if (tuttoOK == true)
			{document.order_form.submit();
			setTimeout('openSummary()',1000)
			}
	}

function controllaPIVA(piva) {
	var cifre = new Array;
	var i
	var calcolo = 0
	var totPari = 0
	var totDispari = 0
	var tot = 0
	var complemento
	var check = piva.charAt(10);
	
	//operazioni di verifica
	for(i=0;i<10;i++) 
		{
			cifre[i] = piva.charAt(i)
		}
		
	for(i=0;i<10;i++) 
		{
			if ((i==1) || (i==3) || (i==5) || (i==7) || (i==9)) 
				{
					calcolo = parseInt(cifre[i],10) * 2;
					if (calcolo > 9) 
						{
							totPari = totPari + Math.floor(calcolo/10);
							totPari = totPari + (calcolo - (Math.floor(calcolo/10))*10);
						}
					else
						{
							totPari = totPari + calcolo;
						}
				}
			else
				{
					totDispari = totDispari + parseInt(cifre[i],10);
				}
		}
	
	tot = totPari + totDispari;
	var totaleStringa = String(tot);
	
	if (totaleStringa.charAt(1) == '0')
		{
			if (check == totaleStringa.charAt(1))
				{
					return('OK')
				}
			else
				{
					return('KO')
				}
		}
	else
		{
			complemento = 10 - parseInt(totaleStringa.charAt(1),10);
			if (check == String(complemento))
				{
					return('OK')
				}
			else
				{
					return('KO')
				}
		}
}

function controllaCF(codice) {
	var cfctot
	var ndigit
	var ckd
	cfctot = 0
	ndigit = 0
   //Estrazione dei caratteri di posizione pari
	for(var i=1;i<14;i=i+2)
		{
			cfctot = cfctot + NumeriPari(codice.charAt(i))
   		}
   
   //Estrazione dei caratteri di posizione dispari
   for(var i=0;i<15;i=i+2)
		{
			cfctot = cfctot + NumeriDispari(codice.charAt(i))
   		}
  
   // Calcolo del check-digit di controllo
   ndigit = (cfctot / 26) - Math.floor(cfctot / 26)
   ndigit = Math.round(ndigit * 26)
   // check digit ricavato
   ckd = String.fromCharCode(65 + ndigit)
   if (ckd == codice.charAt(15))
   		{
			return 'OK'
		}
   else	
   		{
			return 'KO'
		}
}

function NumeriDispari(cifra) {
	var i
	var TD = new Array();
	TD[0] = new Array('B',"1");
	TD[1] = new Array('A','0');
	TD[2] = new Array('K','');
	TD[3] = new Array('P','');	
	TD[4] = new Array('L','');
	TD[5] = new Array('C','2');
	TD[6] = new Array('Q','');
	TD[7] = new Array('D','3');
	TD[8] = new Array('R','');
	TD[9] = new Array('E','4');
	TD[10] = new Array('V','');
	TD[11] = new Array('O','');
	TD[12] = new Array('S','');
	TD[13] = new Array('F','5');
	TD[14] = new Array('T','');
	TD[15] = new Array('G','6');
	TD[16] = new Array('U','');
	TD[17] = new Array('H','7');
	TD[18] = new Array('M','');
	TD[19] = new Array('I','8');
	TD[20] = new Array('N','');
	TD[21] = new Array('J','9');
	TD[22] = new Array('W','');
	TD[23] = new Array('Z','');
	TD[24] = new Array('Y','');
	TD[25] = new Array('X','');
	
	for(i=0;i<26;i++)
		{
			if ((TD[i][0] == cifra) || (TD[i][1] == cifra))
				{
					return i;
					//window.alert(i)
				}
		}
}

function NumeriPari(cifra) {
	var i
	var TP = new Array();
	TP[0] = new Array('A',"0");
	TP[1] = new Array('B','1');
	TP[2] = new Array('C','2');
	TP[3] = new Array('D','3');	
	TP[4] = new Array('E','4');
	TP[5] = new Array('F','5');
	TP[6] = new Array('G','6');
	TP[7] = new Array('H','7');
	TP[8] = new Array('I','8');
	TP[9] = new Array('J','9');
	TP[10] = new Array('K','');
	TP[11] = new Array('L','');
	TP[12] = new Array('M','');
	TP[13] = new Array('N','');
	TP[14] = new Array('O','');
	TP[15] = new Array('P','');
	TP[16] = new Array('Q','');
	TP[17] = new Array('R','');
	TP[18] = new Array('S','');
	TP[19] = new Array('T','');
	TP[20] = new Array('U','');
	TP[21] = new Array('V','');
	TP[22] = new Array('W','');
	TP[23] = new Array('X','');
	TP[24] = new Array('Y','');
	TP[25] = new Array('Z','');
	
	for(i=0;i<26;i++)
		{
			if ((TP[i][0] == cifra) || (TP[i][1] == cifra))
				{
					return i;
					//window.alert(i)
				}
		}
}

function ControllaMail(EmailAddr)
   {
   Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   if (Filtro.test(EmailAddr))
      return 'OK';
   else
      {
      return 'KO';
      }
   }

function VerificaTelefono()
   {
   	window.open('http://www.paginebianche.it/pb/numero?btt=1&nt=' + document.forms[0].cli_telefono.value,'_blank')
   }

function minimo(){
	if (document.forms[0].cli_packaging.value == '1' && document.forms[0].cli_licenze.value == '1'){
		window.alert('Il quantitativo minimo per ordinare licenze Corporate č pari a 2')	
	}
}	