function previewImg(field){
  var imgURL = 'file:///' + field.value.replace(/\\/gi,'/').replace(/:/gi,'|').replace(/"/gi,'').replace(/^\//,'');
  document.icona.src = imgURL;
}

function previewImgCartello(field, prevField){
  var imgURL = 'file:///' + field.value.replace(/\\/gi,'/').replace(/:/gi,'|').replace(/"/gi,'').replace(/^\//,'');
  prevField.src = imgURL;
}

/*function previewImg2(field){
  var imgURL = 'file:///' + field.value.replace(/\\/gi,'/').replace(/:/gi,'|').replace(/"/gi,'').replace(/^\//,'');
  document.immobileForm.img2.src = imgURL;
}

function previewImg3(field){
  var imgURL = 'file:///' + field.value.replace(/\\/gi,'/').replace(/:/gi,'|').replace(/"/gi,'').replace(/^\//,'');
  document.immobileForm.img3.src = imgURL;
}
*/

function controllaCodFisc(campo){
   caratteri= new Array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
   pari= new Array (0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25)
   dispari=new Array (1,0,5,7,9,13,15,17,19,21,1,0,5,7,9,13,15,17,19,21,2,4,18,20,11,3,6,8,12,14,16,10,22,25,24,23)
   cod=campo.value.toLowerCase();
   check=true;
   if (cod.length!=16){
      check=false
      }
     else
      {
      lettere=cod.substr(0,6)+cod.substr(8,1)+cod.substr(11,1)+cod.substr(15);
      numeri=cod.substr(6,2)+cod.substr(9,2)+cod.substr(12,3);
      for (i=0;i<10;i++){
         if (lettere.charCodeAt(i)<97 || lettere.charCodeAt(i)>122){
            check=false;}
      }
      for (i=0;i<8;i++){
         if (numeri.charCodeAt(i)<48 || numeri.charCodeAt(i)>57){
            check=false;
         }
      }
     }
   //checksum del codice fiscale
   test=cod.substr(15,1);
   var somma=0
   for (i=0;i<16;i=i+2){ //dispari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++){
          if (carattere==caratteri[k]){
             somma=somma+dispari[k]
             break
          }
       }
    }
    for (i=1;i<15;i=i+2){ //pari
       carattere=cod.substr(i,1)
       for (k=0;k<36;k++){
          if (carattere==caratteri[k]){
             somma=somma+pari[k]
             break
          }
       }
    }
   resto=somma % 26;
   var lettera=String.fromCharCode(97+resto);
   if (test != lettera){
      check=false;
      }
/*   if (check==false){
      alert("Codice fiscale errato!")
      }
      else
      {
      alert("codice fiscale corretto!!")
      }*/
   return check;
}


 function controllaPIVA(pi)
{
	if( pi == '' )
		return '';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non e'\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 e' valida:\n" +
			"il codice di controllo non corrisponde.\n";

	return '';
}


function changeDiv(oldDiv, newDiv){
  if (document.getElementById)
  {
  eval('document.getElementById("'+oldDiv+'").style.display="none";');
  eval('document.getElementById("'+newDiv+'").style.display="inline";');
  }
  else if (document.all)
  {
  eval('document.all["'+oldDiv+'"].style.display="none";');
  eval('document.all["'+newDiv+'"].style.display="inline";');
  }
  else if (document.layers)
  {
  eval('document.layers["'+oldDiv+'"].display="none";');
  eval('document.layers["'+newDiv+'"].display="inline";');
  }
}

function change(param){
  var azione,form;
  form = document.forms[0];
  azione = form.action + '?org.apache.struts.taglib.html.CANCEL=Annulla&change='+param;
  form.action = azione;
  form.submit();
}

function checkNum(e) {
  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter
  if (((whichCode>=48)&&(whichCode<=57))||(whichCode==8)||(whichCode==9)||(whichCode==13))
     {
     return true;
     }
  else
     return false;
}

function checkValuta(e) {
  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter
  if (((whichCode>=48)&&(whichCode<=57))||(whichCode==8)||(whichCode==9)||(whichCode==44)||(whichCode==46)||(whichCode==13))
     {
     return true;
     }
  else
     return false;
}

function checkValutaConSegno(e) {
  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter
  if (((whichCode>=48)&&(whichCode<=57))||(whichCode==8)||(whichCode==9)||(whichCode==44)||(whichCode==45)||(whichCode==46)||(whichCode==13))
     {
     return true;
     }
  else
     return false;
}

function checkPhone(e) {
  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter
 // whichCode=47 è lo slash /
  if (((whichCode>=48)&&(whichCode<=57))||(whichCode==8)||(whichCode==9)||(whichCode==43)||(whichCode==45)||(whichCode==46)||(whichCode==32)||(whichCode==13)||(whichCode==47))
     {
     return true;
     }
  else
     return false;
}

function checkFax(e) {
  var whichCode = (e.which == null) ? e.keyCode : e.which;
	// whichCode=13 è il tasto Enter
  if (((whichCode>=48)&&(whichCode<=57))||(whichCode==8)||(whichCode==9)||(whichCode==43)||(whichCode==45)||(whichCode==46)||(whichCode==32)||(whichCode==13))
     {
     return true;
     }
  else
     return false;
}

function checkCodFisc(e) {

  var isIE = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 0;

  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter, 8 è il tasto backspace per cancellare, 37 e 39 sono la navigazione a sinistra e destra
  if ((whichCode>=48 && whichCode<=57) || (whichCode >= 65 && whichCode < 91)||(whichCode==13)||(whichCode==32)||(whichCode==8)||(whichCode==37)||(whichCode==39))
  {
    return true;
  } else if (whichCode >= 97 && whichCode < 123)
  {
			if (isIE) {
    	   e.which = e.which -32;
    	   e.keyCode = e.keyCode -32;
         return true;
			} else {
         alert('Impossibile inserire caratteri in minuscolo');
         return false;
			}
  } else {
    return false;
  }
}

function checkUserid(e) {
  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter, 8 il backspace, 32 lo space,  45 il meno, 95 l'underscore,  
  if ((whichCode>=48 && whichCode<=57) || (whichCode >= 65 && whichCode < 91)|| (whichCode >= 97 && whichCode < 123) ||(whichCode==8) || (whichCode==13)||(whichCode==32)||(whichCode==45)||(whichCode==95))
     {
     return true;
     }
  else
     return false;
}

function uppercase(e) {

  var isIE = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 0;

  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
    whichCode = e.keyCode;
    if (!whichCode) {
      whichCode = e.which;
    }
  }
  // whichCode=13 è il tasto Enter, 32 barra spaziatrice, 34 doppi apici, 36 dollaro, 38 apmersand, 39 apostrofo,
  // 40 tonda aperta, 41 torna chiusa, 44 virgola, 46 punto, 58 due punti, 59 punto e virgola
  if ((whichCode>=48 && whichCode<=57) || (whichCode >= 65 && whichCode < 91))
  {
    return true;
  } else if (whichCode >= 97 && whichCode < 123)
  {
		if (isIE) {
    	   e.which = e.which -32;
    	   e.keyCode = e.keyCode -32;
         return true;
		} else {
         alert('Impossibile inserire caratteri in minuscolo');
         return false;
		}
  // 224=à, 232=é, 233=é, 236=ì, 242=ò, 249=ù
  } else if (	whichCode == 224 ||
  						whichCode == 232 ||
  						whichCode == 233 ||
             	whichCode == 236 ||
             	whichCode == 242 ||
             	whichCode == 249)
  {
    return false;
  } else {
    return true;
  }
}

function openCalculator(addr) {
  window.open(addr,'calculator','width=280,height=180,scrollbars=no,resizable=no');
}

function closeIf(){
if (opener && ((opener.closed) || (opener.logout_utente && opener.logout_utente == 'S'))){
  alert('chiudo');
  self.close();
  }
}

function openWindow(p_name, p_width, p_height){
   winService=window.open('vuota.jsp',p_name,'width='+p_width+',height='+p_height+',top=50,left=50,scrollbars=yes,resizable=yes');
   winService.focus();
}

function checkDate(day, month, year)
{
   var valid = "0123456789";

   var temp;

   for (j=0; j< day.length; j++)
   {
      temp = "" + day.substring(j, j+1);
      if (valid.indexOf(temp) == "-1")
      {
         //alert('Inserire solo carattere numerici nei campi GG e MM');
         return false;
      }
   }
   for (j=0; j< month.length; j++)
   {
      temp = "" + month.substring(j, j+1);
      if (valid.indexOf(temp) == "-1")
      {
         //alert('Inserire solo carattere numerici nei campi GG e MM');
         return false;
      }
   }
   for (j=0; j< year.length; j++)
   {
     temp = "" + year.substring(j, j+1);
     if (valid.indexOf(temp) == "-1")
      {
         //alert('Inserire solo carattere numerici nei campi GG e MM');
         return false;
      }
   }


   if (day < 1 || day > 31 || month <1 || month > 12)
   {
      //alert('Inserire valori plausibili nei campi GG e MM');
      return false;
   }

   if (month==4 || month==6 || month==9 || month==11)
   {
      if (day==31)
      {
         //alert('Il mese selezionato non ha 31 giorni');
         return false;
      }
   }

   if (month==2)
   {
      if (day>29)
      {
         //alert('Febbraio non ha mai un numero di giorni superiore a 29!');
         return false;
      }
      if (day==29 && ((year/4)!=parseInt(year/4)))
      {
         //alert('Quest\'anno Febbraio ha solo 28 giorni!');
         return false;
      }
   }

   return true;
}


/** Controlla che la stringa passata sia una data nel formato dd/mm/yyyy
*/
function checkDateItWithSlashSep(dateString)
{
   if(dateString != null && dateString.length > 0){
      var date_array = dateString.split("/");
      if (date_array.length != 3 || (!checkDate(date_array[0], date_array[1], date_array[2]))) {
         return false;
      }
   } else {
      return false;
   }
   return true;
}

/** Controlla che la stringa passata sia una data nel formato dd/mm/yyyy
*/
function checkDateNoFutura(dateString)
{
  	if (checkDateItWithSlashSep(dateString)) {
      var date_array = dateString.split("/");
      inputdate = new Date();
      today = new Date();
      inputdate.setDate(date_array[0]);
      inputdate.setMonth(date_array[1]-1);
      inputdate.setYear(date_array[2]);
      if (today.getTime() < inputdate.getTime()) {
        return false;
      }
      else {
        return true;
      }
  	}
}


/**
 * Sesso:
* 	1 - Maschio
* 	2 - Femmina
 */

var msgAlertCF = '';
var mesi_convertiti = new Array('A', 'B', 'C', 'D', 'E', 'H', 'L', 'M', 'P', 'R', 'S', 'T');

function controlloCompletoCodFisc(cf, cognome, nome, dataNascita, comune_nascita_code_cf, sesso) {

/********************************** INIZIO - CONTROLLO COD_FISC *****************************************/

  msgAlertCF = '';


	var cfOk = 1;

	if( cf == '' ) {
  	   //form.cod_fisc.focus();
      return false;
	}

	while (cognome.indexOf(' ') > -1) {
      cognome = cognome.replace(' ', '');
	}
	while (nome.indexOf(' ') > -1) {
      nome = nome.replace(' ', '');
	}

	if (cf.length != 16) {
	   return false;
	}
	var vocali = 'AEIOU';
	var consonanti = 'BCDFGHJKLMNPQRSTVWXYZ';
	/***** INIZIO - Verifica correttezza caratteri 1-3 COD FISC *****/
   var cognomeOk = 1;
   var cntCognome = 0;
   for( i = 0; (i < cognome.length) && (cntCognome < 3); i++ ) {
	   if (consonanti.indexOf( cognome.charAt(i) ) > -1 ) {
         if (cf.charAt(cntCognome) == cognome.charAt(i)) {
            cntCognome++;
         } else {
            cntCognome++;
            cognomeOk = 0;
            break;
         }
		}
  	}

   if (cntCognome < 3 && cognomeOk == 1) {
      for( i = 0; (i < cognome.length) && (cntCognome < 3); i++ ) {
   	   if (vocali.indexOf( cognome.charAt(i) ) > -1 ) {
            if (cf.charAt(cntCognome) == cognome.charAt(i)) {
               cntCognome++;
            } else {
               cntCognome++;
               cognomeOk = 0;
               break;
            }
   		}
      }
   }
   if (cntCognome < 3 && cognomeOk == 1) {
      if (cntCognome == 1) {
    	   if (cf.charAt(1) == 'X' && cf.charAt(2) == 'X') {
      		cntCognome = 3;
    		} else {
      	   cognomeOk = 0;
    		}
    	} else {
    		if (cf.charAt(2) == 'X') {
      		cntCognome = 3;
    		} else {
      		cognomeOk = 0;
    		}
    	}
   }
   if (cognomeOk == 0) {
  		//form.cod_fisc.focus();
    	//alert('Il valore del campo Cognome non corrisponde al Codice Fiscale inserito');
      msgAlertCF += 'Il valore del campo Cognome non corrisponde al Codice Fiscale inserito\n';
      cfOk = 0;
   }
	/***** FINE - Verifica correttezza caratteri 1-3 COD FISC *****/

		/***** INIZIO - Verifica correttezza caratteri 4-6 COD FISC *****/
    var nomeOk = 1;
    var cntNome = 0;
    var cntCons = 0;
    var nrConsonantiNome = 0;
    for( i = 0; i < nome.length; i++ ) {
      if (consonanti.indexOf( nome.charAt(i) ) > -1 ) {
        nrConsonantiNome++;
      }
    }
    for( i = 0; (i < nome.length) && (cntNome < 3); i++ ) {
			if (consonanti.indexOf( nome.charAt(i) ) > -1 ) {
        if ((nrConsonantiNome > 3) && (cntNome > 0)) {
          if (cntCons == 1) {
            cntCons++;
            continue;
          }
          if (cf.charAt(cntNome + 3) == nome.charAt(i)) {
          		cntCons++;
             	cntNome++;
           } else {
             cntNome++;
             cntCons++;
             nomeOk = 0;
             break;
           }
        } else {
           if (cf.charAt(cntNome + 3) == nome.charAt(i)) {
             cntNome++;
             cntCons++;
           } else {
             cntNome++;
             cntCons++;
             nomeOk = 0;
             break;
           }
        }
			}
  	}

    if (cntNome < 3 && nomeOk == 1) {
    	for( i = 0; (i < nome.length) && (cntNome < 3); i++ ) {
   			if (vocali.indexOf( nome.charAt(i) ) > -1 ) {
           if (cf.charAt(cntNome + 3) == nome.charAt(i)) {
             cntNome++;
           } else {
             cntNome++;
             nomeOk = 0;
             break;
           }
   			}
     	}
    }
    if (cntNome < 3 && nomeOk == 1) {
    	if (cntNome == 1) {
    		if (cf.charAt(4) == 'X' && cf.charAt(5) == 'X') {
      		cntNome = 3;
    		} else {
      		nomeOk = 0;
    		}
    	} else {
    		if (cf.charAt(5) == 'X') {
      		cntNome = 3;
    		} else {
      		nomeOk = 0;
    		}
    	}
    }
    if (nomeOk == 0) {
  		//form.cod_fisc.focus();
    	//alert('Nome: [' + nome + '] - CF: [' + cf + '] NON corrispondono');
    	//alert('Il valore del campo Nome non corrisponde al Codice Fiscale inserito');
      msgAlertCF += 'Il valore del campo Nome non corrisponde al Codice Fiscale inserito\n';
      cfOk = 0;
    }
		/***** FINE - Verifica correttezza caratteri 4-6 COD FISC *****/

    var dataNascitaOk = 1;
		/***** INIZIO - Verifica correttezza caratteri 7-8 COD FISC *****/
   if(dataNascita.length==0){
      //alert('Cerchiamo di immettere qualcosa in quella data di nascita...');
      //form.data_nascitaAsString.focus();
      //alert('Il valore del campo Data di nascita non corrisponde al Codice Fiscale inserito');
      msgAlertCF += 'Il valore del campo Data di nascita non corrisponde al Codice Fiscale inserito\n';
      return false;
   }
    var dataNascitaArray = dataNascita.split("/");
    if (dataNascitaArray.length != 3 || (!checkDate(dataNascitaArray[0], dataNascitaArray[1], dataNascitaArray[2]))) {
       //alert('Mettiamo la data di nascita corretta per favore');
       //form.data_nascitaAsString.focus();
       //alert('Il valore del campo Data di nascita non corrisponde al Codice Fiscale inserito');
      	msgAlertCF += 'Il valore del campo Data di nascita non corrisponde al Codice Fiscale inserito\n';
       return false;
    }
		// dataNascitaArray[2] deve essere l'anno espresso in 4 cifre (i controlli sono fatti a monte)
    if (!(dataNascitaArray[2].charAt(2) == cf.charAt(6) && dataNascitaArray[2].charAt(3) == cf.charAt(7))) {
  		//form.cod_fisc.focus();
    	//alert('Anno: [' + cf.charAt(6) + cf.charAt(7) + '] - CF: [' + cf + '] NON corrispondono');
    	dataNascitaOk = 0;
      cfOk = 0;
	 }
		/***** FINE - Verifica correttezza caratteri 7-8 COD FISC *****/

		/***** INIZIO - Verifica correttezza caratteri 9 COD FISC *****/
    if (!(cf.charAt(8) == mesi_convertiti[parseInt(dataNascitaArray[1], 10) - 1])) {
  		//form.cod_fisc.focus();
    	//alert('* ' + dataNascita);
      //alert('* ' + dataNascitaArray[1]);
      //alert('* ' + parseInt(dataNascitaArray[1], 10))
      //alert('Mese: [' + cf.charAt(8) + '] - CF: [' + mesi_convertiti[parseInt(dataNascitaArray[1], 10) - 1] + '] NON corrispondono');
      dataNascitaOk = 0;
      cfOk = 0;
	 }
		/***** FINE - Verifica correttezza caratteri 9 COD FISC *****/

		/***** INIZIO - Verifica correttezza caratteri 10-11 COD FISC *****/
		// dataNascitaArray[2] deve essere l'anno espresso in 4 cifre (i controlli sono fatti a monte)
    var ggCF = dataNascitaArray[0];
    if (sesso == '') {
    	//alert('Il valore del campo Sesso non corrisponde al Codice Fiscale inserito');
      msgAlertCF += 'Il valore del campo Sesso non corrisponde al Codice Fiscale inserito\n';
      cfOk = 0;
    } else {
    	if (sesso == '2') {
      	ggCF = "" + (parseInt(dataNascitaArray[0], 10) + 40);
    	}
    	if (!(ggCF.charAt(0) == cf.charAt(9) && ggCF.charAt(1) == cf.charAt(10))) {
  			//form.cod_fisc.focus();
    		//alert('Giorno: [' + cf.charAt(9) + cf.charAt(10) + '] - CF: [' + cf + '] NON corrispondono');
    		dataNascitaOk = 0;
        cfOk = 0;
	 		}
    }
		/***** FINE - Verifica correttezza caratteri 10-11 COD FISC *****/

		if (dataNascitaOk == 0) {
      //alert('Il valore del campo Data di nascita non corrisponde al Codice Fiscale inserito');
      msgAlertCF += 'Il valore dei campi Data di nascita e Sesso non corrispondono al Codice Fiscale inserito\n';
		}

		/***** INIZIO - Verifica correttezza caratteri 12-15 COD FISC *****/
  	if (cf.substring(11, 15) != comune_nascita_code_cf) {
    	//alert('Comune: [' + comune_nascita_code_cf + '] - CF: [' + cf + '] NON corrispondono');
      //alert('Il valore del campo Comune di nascita non corrisponde al Codice Fiscale inserito');
      msgAlertCF += 'Il valore del campo Comune di nascita non corrisponde al Codice Fiscale inserito\n';
    	cfOk = 0;
    }
		/***** FINE - Verifica correttezza caratteri 12-15 COD FISC *****/

   if (cfOk == 0) {
      return false;
   } else {
      return true;
	}

/********************************** FINE - CONTROLLO COD_FISC *****************************************/
}

function disableEnterKey(e) {

  var isIE = (navigator.appName == "Microsoft Internet Explorer") ? 1 : 0;

  var whichCode = (e.which == null) ? e.keyCode : e.which;
  if (!whichCode) {
	  whichCode = e.keyCode;
     if (!whichCode) {
       whichCode = e.which;
     }
  }
	// whichCode=13 è il tasto Enter, 8 è il tasto backspace per cancellare, 37 e 39 sonola navigazione a sinistra e destra
  if (whichCode==13)
  {
    return false;
  } else {
    return true;
  }
}

//::::: MM_function ::::: INIZIO

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n];
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

//::::: MM_function ::::: FINE

