// Funzioni di supporto

function EvidenziaCampo(nomeEstesoCampo){	//Matteo 18-01-10
	nomeEstesoCampo.focus();
	//nomeEstesoCampo.style.background = "#FFE1E1";
	nomeEstesoCampo.style.borderColor="#FF0000";
	nomeEstesoCampo.style.borderWidth="3px";
}

function TogliEvidenziaCampo(nomeEstesoCampo){	//Matteo 19-01-10
	nomeEstesoCampo.style.borderColor="#CCC";
	nomeEstesoCampo.style.borderWidth="1px";
}

var
cifre = '0123456789'
telefono = '-+/.0123456789 '

function check_num(stringa){
  i=0
  buona=true
  while(i<stringa.length){
    car=stringa.substring(i,i+1);
    if(cifre.indexOf (car) == -1)
      {buona=false; break;}
  i++}
  return(buona);
}

function check_obbl(campo,nome_campo,lungh)
{
if (campo.value == "")
{
	alert("Sezione 1:\ninserire un valore per il campo " + nome_campo);
	campo.focus();
	return (false);
}
ok=WordCount(campo);
if (ok==0) return(false);
	if (campo.value.length > lungh)
	{
		alert("Sezione 1:\ninserire al massimo " + lungh + " caratteri nel campo "+ nome_campo);
		campo.focus();
		return (false);
	}
	return true;
}

function check_prov(campo,nome_campo)
{
	prov= "AOTOVCNOATCNALMICRSOBSBGPVMNCOVATSGOUDPNPDVEVRVIBLROTVFEPCPRREMOBOFORAIMSVGESPMSLUPTFILIPIARSIGRTRPGPSANAPMCPECHAQTERMRIVTLTFRCENAAVSABNCBISFGBABRLETAPZMTCSCZRCSRPATNSSNUCAORBZTPMEAGCLENCTRGSMLOPORNLCVVBIVBKR";

	if (campo.value.length != 2)
	{  
		alert("Sezione 1:\nil campo relativo alla " + nome_campo + " deve essere pari a 2 caratteri.");
		campo.focus();
		return (false);
	}  
	else
	{
		/*CONTROLLO ESISTENZA IN TABELLA*/
		for (var i=0,cp=0; i<prov.length; i=i+2)
		{
			thischar=prov.substring(i,i+2);
			if (thischar==campo.value.toUpperCase())
			cp=1;
		}
		if (cp==0)
		{
			alert("Sezione 1:\nil valore inserito nel campo " +nome_campo+ " non è la sigla di nessuna provincia italiana.");
			campo.focus();
			return (false);
		}
	}
	return true;
}

function check_data(campog,campom,campoa)
{
  var tod=new Date();
 
  if (campog.value.length != 2)
  {  
    alert("Sezione A:\nil campo relativo al 'Giorno' deve essere 2 cifre.");
    campog.focus();
    return (false);
  }  
  else
  {
    if (!isanum(campog.value)) 
    {
      alert("Sezione A:\nil campo 'Giorno' non è valido")
      campog.focus();
      return (false);  
     }
 
  }   
  if ((campog.value>31)||(campog.value<1))
  {  
    alert("Sezione A:\nil valore del campo relativo al 'Giorno' non è corretto.");
    campog.focus();
    return (false);
  }  

  /*MESE*/
  if (campom.value != "")
  {
    if (campom.value.length != 2)
    {  
      alert("Sezione A:\nil campo relativo al 'Mese' deve essere 2 cifre.");
      campom.focus();
      return (false);
    }  
    else
    {
      if (!isanum(campom.value)) 
      {
        alert("Sezione A:\nil campo 'Mese' non è valido");
        campom.focus();
        return (false);
       }
    }   
    if ((campom.value>12)||(campom.value<1))
    {  
      alert("Sezione A:\nil valore del campo relativo al 'Mese' non è corretto.");
      campom.focus();
      return (false);
    }  
  }

 /*ANNO*/
  if (campoa.value != "")
  {
    if (campoa.value.length != 4)
    {  
      alert("Sezione A:\nil campo relativo all' 'Anno' deve essere 4 cifre.");
      campoa.focus();
      return (false);
    }  
    else
    {
      if (!isanum(campoa.value))
      {
        alert("Sezione A:\nil campo 'Anno' non è valido");
        campoa.focus();
        return (false);
      }

      year=tod.getYear();
      var2000=946598400
      var1970=tod.getTime()/1000;
      if (var1970<var2000)
        year="19"+year;

//      if (campoa.value>year)
//      {
//        alert("Il campo relativo all' ANNO non è corretto.");
//        campoa.focus();
//        return (false);
//      }
    }   
  }
  /*ACCORPAMENTO VALORI DATA */
 
  if((campog.value!="")&&(campom.value!="")&&(campoa.value!=""))
  {
    // campoc.value=campog.value+"/"+campom.value+"/"+campoa.value;
  }
  else if((campog.value!="")||(campom.value!="")||(campoa.value!=""))
  {

    alert("Sezione A:\nla 'Data' è incompleta.");
    campog.focus();
    return (false);
  }
  else
  {
    // campoc.value="";
  }
return true;

}

function check_data_ipo(gg,mm,aaaa)
{

dataok=true;

//controllo che siano tutti numeri
	if(! ( check_num(gg) && check_num(mm) && check_num(aaaa) ) )
		dataok = false;

//controllo Giorno (compreso fra 1 e 31)
	if (! (gg>0) && (gg<32))
		dataok = false;
	   	
//controllo Mese (compreso fra 1 e 12)
	if (! (mm>0) && (mm<13))
		dataok = false;
	   	
	   	
//controllo Anno (maggiore di 1900)
	if (!(aaaa>1899))
		dataok = false;

//controllo Anno (minore di 1999)
	if (!(aaaa<9999))
		dataok = false;


  return(dataok);


}

function isanum(campo)
{

  for (var i=0,counter=0; i<campo.length; i++)
    {
      thischar=campo.substring(i,i+1);
     if (thischar=='0' || thischar=='1' || thischar=='2' || thischar=='3' || thischar=='4' || thischar=='5' || thischar=='6' || thischar=='7' || thischar=='8' || thischar=='9') counter++;
    }
  if (counter!=campo.length)
    {
      return (false);
    }
return true;
}

function WordCount(campo)
{
	var pos = 0;
	var count = 0;
	
	while (campo.value.charAt(pos)==" ")
	{
    	pos++;
	}
  	while ((pos < campo.value.length)&&(pos>=0)) 
	{
		pos = campo.value.indexOf(" ", pos);
		while ((campo.value.charAt(pos)==" ")&&(pos>=0))
		{
			pos++; 
		}
		count++;
	}
	return count;
}

function trim(sString) {
	while (sString.substring(0,1) == ' ')
		{
			sString = sString.substring(1, sString.length);
		}
	while (sString.substring(sString.length-1, sString.length) == ' ')
		{
			sString = sString.substring(0,sString.length-1);
		}
	return sString;
}
function codiceFISCALE(cfins)
   {
   var cf = cfins.toUpperCase();
   var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
   if (!cfReg.test(cf))
      return false;
   var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
   var 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 false;
   return true;
   }

function partitaIVA(iva) {
    var DUB;
    var DAB = new Array(2);
    var NUMO = new Array(11);
    var i;
    var TOTDIS = 0;
    var TOTPAR = 0;
    var NUMERO;
    var tmp;
    var n;
    
    for (i=0;i<=10;i=i+1) {    
    	NUMO[i] = parseInt(iva.charAt(i));
    }
    
    for (i=0;i<=8;i=i+2) {    
    	TOTDIS = TOTDIS + NUMO[i];
    }

    for (i=1;i<=9;i=i+2) {    
    	DUB = NUMO[i];
    	DUB = DUB * 2;
    	if (DUB<10) {
	    	TOTPAR = TOTPAR + DUB;    	
    	} else {
    		TOTPAR = TOTPAR + (DUB-10) + 1;    	
    	}
    }
	DUB = TOTPAR + TOTDIS;   
	
	tmp = DUB + "";
	if (DUB <= 10) {
		n=DUB; }
	else {
		n=parseInt(tmp.charAt(1))
	}
	NUMERO = 10 - n;    

    tmp = NUMERO + "";
    
	if (NUMERO <= 9) {
		n=NUMERO; }
	else {
		n=parseInt(tmp.charAt(1))
	}
    
    if (n == NUMO[10]) {
    	return true; } 
    else {
    	return false;
    }    	
}

function codiceFISCALE2(cfins, C, N) {
   var cf = cfins.toUpperCase();
   var Cog = C.toUpperCase();
   var Nom = N.toUpperCase();
   var strcognome;
   var strnome;
   var tmpcf;

strcognome = '';
strnome = '';
	
// verifico se il cod. fiscale inserito è congruo con cognome e nome

    for (i=0; i< Cog.length; i++) 
        {
         switch (Cog.charAt(i)) 
                {
                  case 'A':
                  case 'E':
                  case 'I':
                  case 'O':
                  case 'U': break;            
                  default : 
                  if((Cog.charAt(i)<='Z')&& (Cog.charAt(i)>'A'))
                   strcognome = strcognome + Cog.charAt(i);
                }
        }
    if (strcognome.length < 3) 
      {
       for (i=0; i<Cog.length; i++) 
          {
           switch (Cog.charAt(i)) 
                 {
                  case 'A':
                  case 'E':
                  case 'I':
                  case 'O':
                  case 'U': strcognome = strcognome + Cog.charAt(i);
                 }
          }
       if (strcognome.length < 3) 
         {
          for (i=strcognome.length; i<=3; i++) 
             { strcognome = strcognome + 'X'; }
         }
      }
   strcognome = strcognome.substring(0,3);
 //------------------------------------------------------------    
   
   
  // processa il nome
  //----------------------------------------------------------------
    for (i=0; i<Nom.length; i++) 
       {
        switch (Nom.charAt(i)) 
              {
               case 'A':
               case 'E':
               case 'I':
               case 'O':
               case 'U': break;
               default:
 if((Nom.charAt(i)<='Z')&& (Nom.charAt(i)>'A'))
                  strnome = strnome + Nom.charAt(i);
              }
       }
    if (strnome.length > 3) 
      {
        strnome = strnome.substring(0,1) + strnome.substring(2,4);
      } 
    else {
          if (strnome.length < 3) 
            {
             for (i=0; i<Nom.length; i++) 
                {
                  switch (Nom.charAt(i)) 
                        {
                         case 'A':
                         case 'E':
                         case 'I':
                         case 'O':
                         case 'U': strnome = strnome + Nom.charAt(i);
                        }
                }
             if (strnome.length < 3) 
               {
                for (i=strnome.length; i<=3; i++) 
                   {strnome = strnome + 'X';}
               }
            }
          strnome = strnome.substring(0,3);
         }
 //--------------------------------------- Fine processa nome   
   tmpcf = strcognome + strnome;
   
   if (tmpcf != cf.substring(0,6)) {   		
   		return false;
   }	
	
	
	return true;
}

function check_len(stringa,max) {
  return (stringa.length < max);} // falso se ko
  
function check_iva(stringa){
  i=0
  buona=true
  if(stringa.length != 11)
   {return(!buona);}
  while(i<=10){
    car=stringa.substring(i,i+1);
    if(cifre.indexOf (car) == -1)
      {buona=false; break;}
  i++}
  return(buona);
}

function check_letter(stringa){
  i=0
  buona=true
  while(i<stringa.length){
  	car=stringa.substring(i,i+1);
	if(cifre.indexOf (car) != -1){
		buona=false;
		break;
	}
  	i++
  }
  return(buona);
}


function check_tel(stringa){  
  i=0
  buona=true
  while(i<stringa.length){
    car=stringa.substring(i,i+1);
    if(telefono.indexOf (car) == -1)
      {buona=false;
      alert("Sezione B\nCarattere '" + car + "' non valido nel campo 'Telefono'");
      break;}
  i++}
  //if (stringa.substring(0,1) != '0') {
  //	buona=false;
  //}
return(buona);
}

function check_piva(campo,tipo)
{
  var mese="ABCDEHLMPRST"
  if (tipo=="pi") stringa="PARTITA IVA.";
  if (tipo=="pc") stringa="CODICE FISCALE/PARTITA IVA.";
  if (tipo=="cf") stringa="CODICE FISCALE.";

  if (campo.value == "")
  {
    alert("Inserire un valore per il campo "+stringa );
    campo.focus();
    return (false);
  }

  if ((campo.value.length == 11) && ((tipo=="pi") || (tipo=="pc")))
  { 
  /*CONTROLLO CHE SIA NUMERICO*/
  
    if (!isanum(campo.value))
    {
     alert("Il campo PARTITA IVA non è valido");
     campo.focus
     return (false);
    }
      
  }
  else
  {
    if ((campo.value.length == 16) && ((tipo=="cf")||(tipo=="pc")))
 
    {
      flag=true;

    /*   CONTROLLO NOME E COGNOME     */

      for (var i=0; i<6; i++)
 
        if ((campo.value.charAt(i).toUpperCase()<'A')||(campo.value.charAt(i).toUpperCase()>'Z')) flag=false;
 
    /*   CONTROLLO A_D_N   */

      anno=campo.value.substring(6,8);
      if (!isanum(anno)) flag=false;

    /*   CONTROLLO MESE */

      cmese=campo.value.charAt(8).toUpperCase();
      if (mese.indexOf(cmese)==-1) flag=false;

    /*   CONTROLLO GIORNO  1,31; 41,71  */

      giorno=campo.value.substring(9,11);
      if (!isanum(giorno))
        flag=false;
      else
        if ((giorno<1) || ((giorno>31)&&(giorno<41)) || (giorno>71)) flag=false;

    /*  CONTROLLO CARATTERI RIMANENTI   */

      if ((campo.value.charAt(11).toUpperCase()<'A')||(campo.value.charAt(11).toUpperCase()>'Z')) flag=false;

      if ((campo.value.charAt(15).toUpperCase()<'A')||(campo.value.charAt(15).toUpperCase()>'Z')) flag=false;

      num=campo.value.substring(12,15);

      if (!isanum(num))
        flag=false;

      if (!flag)
      {
        alert("Il CODICE FISCALE è errato.");
        campo.focus();
        return (false);
      }
    }

    if ((campo.value.length != 16)&&(campo.value.length != 11) && tipo=="pc")
    {
      alert("Il campo relativo alla PARTITA IVA / CODICE FISCALE deve essere pari ad 11 o 16 caratteri.");
      campo.focus();
      return (false);
    }
  }

  if ((campo.value.length != 11)&&(tipo=="pi")) 
  {  
    alert("Il campo relativo alla PARTITA IVA deve essere pari ad 11 caratteri.");
    campo.focus();
    return (false);
  }

  if ((campo.value.length != 16)&&(tipo=="cf")) 
  {
    alert("Il campo relativo al CODICE FISCALE deve essere pari a 16 caratteri.");
    campo.focus();
    return (false);
  }

return true;

}

function check_empty(testo) {
  return (testo.length>0);} //falso se vuoto