// controllo dei moduli di registrazione
// per l'acquisto di singoli servizi
// nella cartella registrazione

var
cifre = '0123456789'
telefono = '-+/.0123456789 '
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 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 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 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 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 showfatt(iddiv) {
	if (iddiv=='P') {
		showDiv('P');
		offDiv('S');
		offDiv('D');
		offDiv('E');
		document.f.modulo_fatt.value = iddiv;
		document.f.TipoUtente.value = iddiv;		
	}
	if (iddiv=='S') {
		showDiv('S');
		offDiv('P');
		offDiv('D');
		offDiv('E');
		document.f.modulo_fatt.value = iddiv;
		document.f.TipoUtente.value = iddiv;		
	}
	if (iddiv=='D') {
		showDiv('D');
		offDiv('P');
		offDiv('S');
		offDiv('E');
		document.f.modulo_fatt.value = iddiv;
		document.f.TipoUtente.value = iddiv;		
	}
	if (iddiv=='E') {		
		showDiv('E');
		offDiv('P');
		offDiv('S');
		offDiv('D');
		document.f.modulo_fatt.value = iddiv;
		document.f.TipoUtente.value = iddiv;
	}	
}

function CFCheck() {
	MioForm = document.f;
	var tmp;
	tmp = trim(MioForm.CF2.value);
	if (!(check_num(MioForm.CF2.value))) {
		alert("Sezione C\n                                                     ATTENZIONE\nIn questo campo va inserito il Codice Fiscale della societā (anche se uguale alla Partita Iva)\ne non della persona che esegue l'acquisto.");
		MioForm.CF2.value = '';
		return false;
	}
}

function okdatifatt(sezione) {
	
	MioForm = document.f;
	var tmp;
	var lettera;
	
	lettera='C';	
	if (sezione == 'POS') {
		lettera='B';
	}
	
	if (document.f.modulo_fatt.value=='') {
		alert('Sezione ' + lettera + '\nIndicare la categoria cliente');
		return false;
	}else{
	
		// controllo Utente Privato	
		if (MioForm.modulo_fatt.value == 'P') {		
			if (trim(MioForm.Cognome1.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cognome'");
				return false;		
			}		
			if (trim(MioForm.Nome1.value) == '')  {
				alert('Sezione ' + lettera + "\nInserire il campo 'Nome'");
				return false;		
			}	
			if (trim(MioForm.CF1.value) == '') {
					alert('Sezione ' + lettera + "\nInserire il campo 'Codice Fiscale'");
					return false;
			}			
			tmp = trim(MioForm.CF1.value);
			if (tmp.length!= 16) {		
				alert('Sezione ' + lettera + '\nIl Codice Fiscale deve essere di 16 caratteri');
				return false;
			}
			if (codiceFISCALE(MioForm.CF1.value)==false) {
				alert('Sezione ' + lettera + '\nCodice Fiscale non corretto');
				return false;
			}
			if (codiceFISCALE2(MioForm.CF1.value, MioForm.Cognome1.value, MioForm.Nome1.value)==false) {
				alert('Sezione ' + lettera + '\nCodice Fiscale non compatibile con i Campi \'Cognome\' e \'Nome\'');
				return false;
			}			
			if (trim(MioForm.Indirizzo1.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Indirizzo'");
				return false;
			}			
			if (MioForm.Provincia1[0].selected) {
		        alert('Sezione ' + lettera + '\nIndicare la provincia');
		        return false;
		    }			
			if (MioForm.Cap1.value == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'CAP'");
				return false;
			}else{
				if ( !(check_num(MioForm.Cap1.value))){
					alert('Sezione ' + lettera + "\nControllare il campo 'CAP'");
					return false;
				}
			}
			if (trim(MioForm.Citta1.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cittā'");
				return false;
			}else{
				if (!(check_letter(MioForm.Citta1.value))){
				alert('Sezione ' + lettera + "\nControllare il campo 'Cittā'");
				return false;
				}
			}				
			if (trim(MioForm.Telefono1.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Telefono'");
				return false;
			}else{
				if(!(check_tel(MioForm.Telefono1.value))){
					return false;
				}
			}			
			if(trim(MioForm.Email1.value) == ''){
				alert('Sezione ' + lettera + "\nInserire il campo 'E-mail'");
				return false;			
			}else{
				if((MioForm.Email1.value.indexOf("@")<1) || (MioForm.Email1.value.indexOf(".")<1)){
					alert('Sezione ' + lettera + "\nL'e-mail non č stata digitata correttamente");
					return false;
				}
			}  
		}
		
		// controllo Societā
		if (MioForm.modulo_fatt.value == 'S') {	
			if (trim(MioForm.Cognome2.value) == '') {
				alert('Sezione ' + lettera + "\nInserire Cognome e Nome della persona che esegue l'acquisto del servizio");
				return false;		
			}	
			if (trim(MioForm.Nome2.value) == '')  {
				alert('Sezione ' + lettera + "\nInserire Cognome e Nome della persona che esegue l'acquisto del servizio");
				return false;		
			}
			if (trim(MioForm.Intestazione2.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Ragione Sociale'");
				MioForm.Intestazione2.value='';		
				return false;
			}		
			if (WordCount(MioForm.Intestazione2) <= 1) {
				alert('Sezione ' + lettera + "\nInserire correttamenente il campo 'Ragione Sociale'");
				return false;
			}			
			//***Controlli partita iva e codice fiscale
			if (trim(MioForm.IVA2.value) == '') {
					alert('Sezione ' + lettera + "\nInserire il campo 'Partita IVA'");
					return false;
			}							
			tmp = trim(MioForm.IVA2.value);
			if (tmp.length!= 11) {		
				alert('Sezione ' + lettera + '\nLa Partita Iva deve essere di 11 caratteri');
				return false;
			}
			if (partitaIVA(MioForm.IVA2.value)==false) {
				alert('Sezione ' + lettera + '\nPartita Iva non corretta');
				return false;
			}			
			if (trim(MioForm.CF2.value) == '') {
					alert('Sezione ' + lettera + "\nInserire il campo 'Codice Fiscale'");
					return false;
			}						
			tmp = trim(MioForm.CF2.value);
			if ((check_num(MioForm.CF2.value))) {
				if (tmp.length!= 11) {
					alert('Sezione ' + lettera + "\nIl Codice Fiscale deve essere di 11 caratteri");
					return false;
				}else{				
					if (partitaIVA(MioForm.CF2.value)==false) {
						alert('Sezione ' + lettera + '\nCodice Fiscale non corretto');
						return false;
					}
				}
			}			
			//***													
			if (trim(MioForm.Indirizzo2.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Indirizzo'");
				return false;
			}			
			if (MioForm.Provincia2[0].selected) {
		        alert('Sezione ' + lettera + '\nIndicare la provincia');
		        return false;
		    }			
			if (MioForm.Cap2.value == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'CAP'");
				return false;
			}else{
				if ( !(check_num(MioForm.Cap2.value))){
					alert('Sezione ' + lettera + "\nControllare il campo 'CAP'");
					return false;
				}
			}
			if (trim(MioForm.Citta2.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cittā'");
				return false;
			}else{
				if (!(check_letter(MioForm.Citta2.value))){
				alert('Sezione ' + lettera + "\nControllare il campo 'Cittā'");
				return false;
				}
			}		
			if (trim(MioForm.Telefono2.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Telefono'");
				return false;
			}else{
				if( !(check_tel(MioForm.Telefono2.value))){
					return false;
				}
			}			
			if(trim(MioForm.Email2.value) == ''){
				alert('Sezione ' + lettera + "\nInserire il campo 'E-mail'");
				return false;			
			}else{
				if((MioForm.Email2.value.indexOf("@")<1) || (MioForm.Email2.value.indexOf(".")<1)){
					alert('Sezione ' + lettera + "\nL'e-mail non č stata digitata correttamente");
					return false;
				}
			}
		}
		
		// controllo Ditta Individuale
		if (MioForm.modulo_fatt.value == 'D') {	
			if (trim(MioForm.Cognome3.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cognome'");
				return false;		
			}	
			if (trim(MioForm.Nome3.value) == '')  {
				alert('Sezione ' + lettera + "\nInserire il campo 'Nome'");
				return false;		
			}
			
			if (trim(MioForm.Intestazione3.value) == '') {
				alert('Sezione ' + lettera + "\nInserire la Denominazione Impresa anche se uguale al Cognome e Nome");
				MioForm.Intestazione3.value='';		
				return false;
			}		
			if (WordCount(MioForm.Intestazione3) <= 1) {
				alert('Sezione ' + lettera + "\nInserire correttamente il campo 'Denominazione Impresa'");
				return false;
			}
			
			if (trim(MioForm.IVA3.value) == '') {
					alert('Sezione ' + lettera + "\nInserire il campo 'Partita IVA'");
					return false;
			}	
			if (trim(MioForm.CF3.value) == '') {
					alert('Sezione ' + lettera + "\nInserire il campo 'Codice Fiscale'");
					return false;
			}				
			tmp = trim(MioForm.IVA3.value);
			if (tmp.length!= 11) {		
				alert('Sezione ' + lettera + '\nLa Partita Iva deve essere di 11 caratteri');
				return false;
			}
			if (partitaIVA(MioForm.IVA3.value)==false) {
				alert('Sezione ' + lettera + '\nPartita Iva non corretta');
				return false;
			}	
			tmp = trim(MioForm.CF3.value);
			if (tmp.length!= 16) {		
				alert('Sezione ' + lettera + '\nIl Codice Fiscale deve essere di 16 caratteri');
				return false;
			}	
			if (codiceFISCALE(MioForm.CF3.value)==false) {
				alert('Sezione ' + lettera + '\nCodice Fiscale non corretto');
				return false;
			}	
			if (codiceFISCALE2(MioForm.CF3.value, MioForm.Cognome3.value, MioForm.Nome3.value)==false) {
				alert('Sezione ' + lettera + '\nCodice Fiscale non compatibile con i Campi \'Cognome\' e \'Nome\'');
				return false;
			}			
			if (trim(MioForm.Indirizzo3.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Indirizzo'");
				return false;
			}
			if (MioForm.Provincia3[0].selected) {
		        alert('Sezione ' + lettera + "\nIndicare la provincia");
		        return false;
		    }			
			if (MioForm.Cap3.value == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'CAP'");
				return false;
			}else{
				if ( !(check_num(MioForm.Cap3.value))){
					alert('Sezione ' + lettera + "\nControllare il campo 'CAP'");
					return false;
				}
			}	
			if (trim(MioForm.Citta3.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cittā'");
				return false;
			}else{
				if (!(check_letter(MioForm.Citta3.value))){
				alert('Sezione ' + lettera + "\nControllare il campo 'Cittā'");
				return false;
				}
			}						
			if (trim(MioForm.Telefono3.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Telefono'");
				return false;
			}else{
				if( !(check_tel(MioForm.Telefono3.value))){
					return false;
				}
			}
			if(trim(MioForm.Email3.value) == ''){
				alert('Sezione ' + lettera + "\nInserire il campo 'E-mail'");
				return false;			
			}else{
				if((MioForm.Email3.value.indexOf("@")<1) || (MioForm.Email3.value.indexOf(".")<1)){
					alert('Sezione ' + lettera + "\nL'e-mail non č stata digitata correttamente");
					return false;
				}
			}			
		}
		
		// controllo cliente estero
		if (MioForm.modulo_fatt.value == 'E') {	
			if (trim(MioForm.Cognome4.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cognome'");
				return false;		
			}	
			if (trim(MioForm.Nome4.value) == '')  {
				alert('Sezione ' + lettera + "\nInserire il campo 'Nome'");
				return false;		
			}
				if (((MioForm.Intestazione4.value!='')&&(MioForm.IVA4.value=='')) || ((MioForm.Intestazione4.value=='')&&(MioForm.IVA4.value!=''))) {
				alert('Sezione ' + lettera + "\nPer le aziende inserire entrambi i campi: 'Ragione sociale' e 'Partita Iva'");
				return false;
			}			
			if ((MioForm.Intestazione4.value=='')&&(MioForm.CF4.value=='')) {
				alert('Sezione ' + lettera + "\nPer i privati inserire entrambi i campi: 'Cognome e Nome' e 'Codice Fiscale'");
				return false;
			}			
			if (trim(MioForm.Indirizzo4.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Indirizzo'");
				return false;
			}	
			if (trim(MioForm.Citta4.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Cittā'");
				return false;
			}else{
				if (!(check_letter(MioForm.Citta4.value))){
				alert('Sezione ' + lettera + "\nControllare il campo 'Cittā'");
				return false;
				}
			}			
			if (MioForm.Stato4[0].selected) {
		        alert('Sezione ' + lettera + "\nIndicare lo Stato estero di appartenenza");
		        return false;
		    }						
			if (trim(MioForm.Telefono4.value) == '') {
				alert('Sezione ' + lettera + "\nInserire il campo 'Telefono'");
				return false;
			}else{
				if( !(check_tel(MioForm.Telefono4.value))){
					return false;
				}
			}			
			if(trim(MioForm.Email4.value) == ''){
				alert('Sezione ' + lettera + "\nInserire il campo 'E-mail'");
				return false;			
			}else{
				if((MioForm.Email4.value.indexOf("@")<1) || (MioForm.Email4.value.indexOf(".")<1)){
					alert('Sezione ' + lettera + "\nL'e-mail non č stata digitata correttamente");
					return false;
				}
			}	
		}
	
	}
		
	if (document.f.codserv.value != 'POS') {
		if (!(MioForm.Vendita.checked)) {
			alert("Sezione D\nAccettare le condizioni di vendita per poter usufruire del servizio");
			return false;
		}
	}
	
	if (!(MioForm.Termini.checked)) {
		alert("Sezione D\nAccettare il trattamento dei dati personali per poter usufruire del servizio");
		return false;
	}	
	
	//Verifica IVA per i privati comunitari	
	if ((MioForm.TipoUtente.value == 'E') && ((MioForm.codserv.value != 'POS') || ((MioForm.codserv.value == 'POS') && (MioForm.AttPra.value != '')))) {			
		if ((MioForm.IVA4.value == '') && ((MioForm.Stato4.selectedIndex == 18) || (MioForm.Stato4.selectedIndex == 25) || (MioForm.Stato4.selectedIndex == 36) || (MioForm.Stato4.selectedIndex == 58) || (MioForm.Stato4.selectedIndex == 60) || (MioForm.Stato4.selectedIndex == 70) || (MioForm.Stato4.selectedIndex == 75) || (MioForm.Stato4.selectedIndex == 76) || (MioForm.Stato4.selectedIndex == 83) || (MioForm.Stato4.selectedIndex == 224) || (MioForm.Stato4.selectedIndex == 86) || (MioForm.Stato4.selectedIndex == 104) || (MioForm.Stato4.selectedIndex == 118) || (MioForm.Stato4.selectedIndex == 124) || (MioForm.Stato4.selectedIndex == 125) || (MioForm.Stato4.selectedIndex == 133) || (MioForm.Stato4.selectedIndex == 152) || (MioForm.Stato4.selectedIndex == 172) || (MioForm.Stato4.selectedIndex == 173) || (MioForm.Stato4.selectedIndex == 59) || (MioForm.Stato4.selectedIndex == 177) || (MioForm.Stato4.selectedIndex == 189) || (MioForm.Stato4.selectedIndex == 190) || (MioForm.Stato4.selectedIndex == 194) || (MioForm.Stato4.selectedIndex == 204) || (MioForm.Stato4.selectedIndex == 98))) {
			MioForm.PagaIva.value='si';
		}else{
			MioForm.PagaIva.value='no';
		}
	}	
	if ((document.f.PagaIva.value == 'si') && (document.f.TipoUtente.value == 'E') && ((MioForm.codserv.value != 'POS') || ((MioForm.codserv.value == 'POS') && (MioForm.AttPra.value != '')))) {
		if (confirm("                      ATTENZIONE:\nL' Iva verrā aggiunta all'importo totale del servizio") == true){
			return true;
		}else{
			return false;
		}
	}else{
		return true;
	}

}

// Funzioni di supporto
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_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_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 C\nCarattere '" + car + "' non valido nel campo 'Telefono'");
      break;}
  i++}
  //if (stringa.substring(0,1) != '0') {
  //	buona=false;
  //}
return(buona);
}

