function setfocus() {
   document.registrati.nome.focus();
   return;
  
 }
function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
	return "La lunghezza del codice fiscale non è\n"
	+"corretta: il codice fiscale dovrebbe essere lungo\n"
	+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
	if( validi.indexOf( cf.charAt(i) ) == -1 )
	return "Il codice fiscale contiene un carattere non valido `" +
	cf.charAt(i) +
	"'.\nI caratteri validi sono le lettere e le cifre.\n";
	}
	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) )
	return "Il codice fiscale non è corretto:\n"+
	"il codice di controllo non corrisponde.\n";
	return "";
}

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
	return "La lunghezza della partita IVA non è\n" +
	"corretta: la partita IVA dovrebbe essere lunga\n" +
	"esattamente 11 caratteri.\n";
	validi = "0123456789";
	for( i = 0; i < 11; i++ ){
	if( validi.indexOf( pi.charAt(i) ) == -1 )
	return "La partita IVA contiene un carattere non valido `" +
	pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
	}
	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) )
	return "La partita IVA non è valida:\n" +
	"il codice di controllo non corrisponde.\n";
	return '';
}


function isnumerictel(str)
   
   {
        var len= str.length;
        if (len==0)
        return false;
         //else
        var p=0;
        var ok= true;
        var ch= "";
         while (ok && p<len)
         {
               ch= str.charAt(p);
               if (('0'<=ch && ch<='9') || ch =='/' || ch =='-')
               p++;
         else
          ok= false;
         }
          return ok;
   } 

function test(src) 
{
	if (document.registrati.email.value == "") 
  	{
    		return true;
  	}
	else 
	{
  		var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
  		return regex.test(src);
	}
}


function isnumeric(str)
{
	var len= str.length;
  	if (len==0)
    	return false;
	var p=0;
  	var ok= true;
  	var ch= "";
  	while (ok && p<len)
  	{
    		ch= str.charAt(p);
    		if ('0'<=ch && ch<='9')
      			p++;
    		else
      			ok= false;
  	}
  	return ok;
}
function isnumericver(str)
{
	var len= str.length;
  	if (len==0)
    	return false;
	var p=0;
  	var ok= true;
  	var ch= "";
  	while (ok && p<len)
  	{
    		ch= str.charAt(p);
    		if (!('0'<=ch && ch<='9'))
      			p++;
    		else
      			ok= false;
  	}
  	return ok;
}

//funzione trim che elimina gli spazi
  function trim(strText) {  
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);
    return strText;
  } 
  
//*********Funzione per il controllo dell'obbligatorietà dei campi e sulle lunghezze
function cont1()
{
  str = "";
  ok = true;
  //if((document.registrati.tipo_utente[0].checked == true)){
	 //controllo sul ragsoc-------------------
	  if ((document.registrati.testo.value=="") && ok)
	  {
		 ok = false;
		 str = "Inserire la questione da sottoporre."
	  } 
	  else 
	  {
		  if ((document.registrati.testo.value.length > 1000) && ok)
		  {
		  ok = false;
		  str = "Il campo 'inserire la questione' contiene un numero elevato di caratteri"
		  }
	
	  }
  //}
 


//controllo sul cognome-------------------
  if ((document.registrati.nome.value=="") && ok)
  {
     ok = false;
     str = "Inserire il Nome"
  } 
  else 
  {
      if ((document.registrati.nome.value.length > 100) && ok)
      {
      ok = false;
      str = "Il campo nome contiene un numero elevato di caratteri"
      }

  }
  //controllo -------------------
  if ((document.registrati.luogo_nascita.value=="") && ok)
  {
     ok = false;
     str = "Inserire il Luogo di nascita"
  } 
  else 
  {
      if ((document.registrati.luogo_nascita.value.length > 150) && ok)
      {
      ok = false;
      str = "Il campo luogo di nascita contiene un numero elevato di caratteri"
      }

  }
  //controllo -------------------
  if ((document.registrati.data_giorno.value=="") && ok)
  {
     ok = false;
     str = "Inserire la data del giorno di nascita"
  } 
  else 
  {
  	if (((!isnumeric(document.registrati.data_giorno.value))||(document.registrati.data_giorno.value>31)) && ok)
		   {
			  ok = false;
			  str = "Giorno della data di nascita non corretto."
		   }
		   else 
		   {
			  
		   }     

  }
  if ((document.registrati.data_mese.value=="") && ok)
  {
     ok = false;
     str = "Inserire la data del mese di nascita"
  } 
  else 
  {
  	if (((!isnumeric(document.registrati.data_mese.value))||(document.registrati.data_mese.value>12)) && ok)
		   {
			  ok = false;
			  str = "Mese della data di nascita non corretto."
		   }
		   else 
		   {
			  
		   }     

  }
  if ((document.registrati.data_anno.value=="") && ok)
  {
     ok = false;
     str = "Inserire l'anno di nascita"
  } 
  else 
  {
  	if (((!isnumeric(document.registrati.data_anno.value))) && ok)
		   {
			  ok = false;
			  str = "Anno di nascita non corretto. Inserire solo caratteri numerici"
		   }
		   else 
		   {
			  
		   }     

  }
  //Controllo sul Campo Email------------------------------------------------------------------
 //controllo sul nome-------------------
  if ((document.registrati.citta.value=="") && ok)
  {
     ok = false;
     str = "Inserire la città"
  } 
  else 
  {
      if ((document.registrati.citta.value.length > 150) && ok)
      {
      ok = false;
      str = "Il campo città  contiene un numero elevato di caratteri"
      }

  }

//Fine controllo sul nome------------------- 
 //Controllo sul Campo Codice Fiscale----------------------------------------------------------------
if((document.registrati.tipologia.value == "privato")&& ok){    
  if ((document.registrati.codifisc.value=="") && ok) {
		ok = false;
     str = "Inserire il codice fiscale"
  } 
  else 
  {  
	
	   if ((ok==true)&&(document.registrati.codifisc.value!="")){
		  
	   cod = document.registrati.codifisc.value;
	   
		  
	   if(( cod.length == 16)  && ok){
			   err = ControllaCF(cod);
			
	   }else{
				   
					err = "Il codice introdotto non è valido:\n\n" +
						"  - un codice fiscale deve essere lungo 16 caratteri;\n\n"
					   
	   }
	  if((err > '') && ok==true){
			
			ok = false;
		str = "VALORE ERRATO\n\n" + err + "\nInserire un valore corretto!";
	   }
	 }
	}
}
 
 //Fine Controllo sul Campo Codice Fiscale----------
if((document.registrati.tipologia.value == "altro")&& ok){ 
  
  //Controllo sul Campo PARTITA IVA---------------------------------------------------------------
   	if ((document.registrati.codifisc.value=="") && ok) {
		ok = false;
     str = "Inserire la p.iva!"
	  } 
	  else 
	  {  
		   if ((ok==true)&&(document.registrati.codifisc.value!="")){
			 cod = document.registrati.codifisc.value;
			 if ((document.registrati.codifisc.value!="") && ok)
			 {
			  
				   if(( cod.length == 11)  && ok){
					   err = ControllaPIVA(cod);
					
				   }else{
						   
					   err = "La Partita IVA non è valida:\n\n" +
					   "  - una Partita IVA deve essere lunga 11 caratteri;\n\n"
							   
				   }
				   if((err > '') && ok==true){
					
					ok = false;
				str = "VALORE ERRATO\n\n" + err + "\nInserire un valore corretto!";
				   }
			}
		 }
  }
  }
 
//Fine Controllo sul Campo Partita IVA--------	
//controllo sul indirizzo------------------
  if ((document.registrati.indirizzo.value=="") && ok)
  {
     ok = false;
     str = "Inserire l'indirizzo!"
  } 
  else 
  {
      if ((document.registrati.indirizzo.value.length > 200) && ok)
      {
      ok = false;
      str = "Il campo indirizzo  contiene un numero elevato di caratteri"
      }

  }
  

	
 
 
 //Controllo sul Campo Telefono------------------------------------------------------------------
   
  document.registrati.telefono.value=trim(document.registrati.telefono.value);
if ((document.registrati.telefono.value!="") && ok)
  {

		if ((!isnumerictel(document.registrati.telefono.value))&&(document.registrati.telefono.value!="") && ok)
		   {
			  ok = false;
			  str = "Formato non corretto. Inserire il numero di telefono con uno dei seguenti formati: (Es:011/12345 o 011-12345)"
		   }
		   else 
		   {
			  if ((document.registrati.telefono.value.length > 18) && ok)
			  {
				 ok = false;
				 str = "Hai superato il numero di caratteri disponibili per il campo Telefono"
			  }
		   } 
  }
   //Fine Controllo sul Campo Telefono--------------------------------------------------------------

   //Controllo sul Campo Fax------------------------------------------------------------------
   
 document.registrati.fax.value=trim(document.registrati.fax.value);
if ((document.registrati.fax.value!="") && ok){
  
		if ((!isnumerictel(document.registrati.fax.value))&&(document.registrati.fax.value!="") && ok)
		   {
			  ok = false;
			  str = "Formato non corretto. Inserire il numero di fax con uno dei seguenti formati: (Es:011/12345 o 011-12345)"
		   }
		   else 
		   {
			  if ((document.registrati.fax.value.length > 18) && ok)
			  {
				 ok = false;
				 str = "Hai superato il numero di caratteri disponibili per il campo Fax"
			  }
		   }
  }

   //Fine Controllo sul Campo Fax-------------------
    
document.registrati.email.value=trim(document.registrati.email.value);
	 if ((document.registrati.email.value=="") && ok)
   {
      ok = false;
      str = "Inserire l'indirizzo email "
   } 
   else 
   { 

if (!test(document.registrati.email.value) && ok)
   {
      ok = false;
      str = "Attenzione!I caratteri nel campo E-mail non sono corretti!"
   }
   else 
   {
      if ((document.registrati.email.value.length > 100) && ok)
      {
         ok = false;
         str = "Hai superato il numero di caratteri disponibili per il campo E-mail"
      }
   } 
}

//fine controllo sul campo email-------------------   
 


//controllo sul consenso------------------
	if((document.registrati.consenso.checked == false)&& ok){
 
     ok = false;
     str = "Il consenso è obbligatorio per l'invio dei dati"
  } 
  else 
  {
      

  }
 
 
return str;
} 

function controllo_reg()
{ 

c = cont1();
if (c=="")
{
 document.registrati.action="./send_contatti.php";
 document.registrati.submit();
}
else
{
 alert(str)
}
}
//funzione per abilitare i campi p.iva e società se viene selezionato l'opzione azienda
 function abilita()
{
	
	if (document.registrati.tipo_utente[0].checked == true){
		//document.registrati.ragsoc.disabled=false;
		document.registrati.codifisc.disabled=false;
	}else{
		//document.registrati.ragsoc.value="";
		document.registrati.codifisc.value="";
		//document.registrati.ragsoc.disabled=true;
		document.registrati.codifisc.disabled=true;
		
	}
}

//funzioni per visualizzare il tasto di conferma per carta di credito o bonifico
function HideDiv(ID){
	document.getElementById(ID).style.display='none';
}
		
function ShowDiv(ID){
	document.getElementById(ID).style.display='';
}

function vai(ID,ID1){
		ShowDiv(ID);
		HideDiv(ID1);
}
function vai1(ID,ID1){
		ShowDiv(ID);
		HideDiv(ID1);
}
/*function vai1(campo,ID){

	if (campo=='bonifico'){
		ShowDiv(ID);
	}else{
		HideDiv(ID);
	}	
}*/	
