 
		//-------------------------------------------------------------------------
		// Nome:
		//	checkvaluecampo
		//
		// Descrizione:
		//	Confronta che il campo non sia vuoto
		//-------------------------------------------------------------------------	
		function checkvaluecampo(obj, nome)
		{
				if (obj == "")
				{
					alert("Fill the field " + nome +"!");
					return true;
				}else{
				    return false;
				}	
		}
		
		//-------------------------------------------------------------------------
		// Nome:
		//	checklenghtcampo
		//
		// Descrizione:
		//	Confronta la lunghezza di un campo
		//-------------------------------------------------------------------------	
		function checklenghtcampo(obj, nome, lunghezza)
		{
				if (obj > lunghezza)
				{
					alert("The field " + nome + " exceeded\nthe maximum length of " +
							 lunghezza + " characters!");
					return true;
				}else{
				    return false;
				}	
		}
		

		
		//-------------------------------------------------------------------------
		// Nome:
		//	checklenghtcampo
		//
		// Descrizione:
		//	Confronta la lunghezza di un campo
		//-------------------------------------------------------------------------
			
		function checkLunghezzaCampo(campo, nomeCampo, lunghezzaMax){
				
						
			if (campo.value.length > lunghezzaMax){
			    alert("The field " + nomeCampo + " exceeded\nthe maximum length of " + lunghezza + " characters!");
			    return false;
			}else{
			    return true;
			}	
		}
		
		//-------------------------------------------------------------------------
		// Nome:
		//	checkConfrontaCampi
		//
		// Descrizione:
		//	Confronta che non siano diversi due campi
		//-------------------------------------------------------------------------
		function checkConfrontaCampi(obj1, obj2, messaggio)
		{
				if (obj1 != obj2)
				{	
					alert(messaggio);
					return true;
				}else{
				    return false;
				}	
		}
		
		
		//-------------------------------------------------------------------------
		// Nome:
		//	checkFloat
		//
		// Descrizione:
		//	Controlla che nel campo sia stato inserito un valore a virgola mobile
		//-------------------------------------------------------------------------
		function checkFloat(campo, nome_campo) {
			var num = 0;
			var len = 0;
			num = campo;
			len = campo.length;
			i = 0;
			do {
				if (!isNaN(parseFloat(num.substr(i,i+1))))
					i=i+1;
				else
					return("Inserire un valore numerico in " + nome_campo);
			}
			while (i<len);
			return("-1");
		}
		
		
		//-------------------------------------------------------------------------
		// Nome:
		//	controlla
		//
		// Descrizione:
		//	Controlla che il campo passato come parametro contenga valori numerici.
		//-------------------------------------------------------------------------
		function controlla(obj,obj_name)
		{
			if (obj.value!="")
			{
				var numero = obj.value.replace(',','.');
				if ((tmpStr=checkFloat(numero,obj_name))!="-1")
				{
					alert(tmpStr);
					obj.focus();
				}
			}
		}
		
	//-------------------------------------------------------------------------
	// Nome: valDate
	//
	// Descrizione: Controlla che la data sia valida.
	//-------------------------------------------------------------------------	
	  function valDate(D, M, Y)
	  {
    	  Months= new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    	  Leap  = false;
          
          if(!isNumeric(strD) || !isNumeric(strM) || !isNumeric(strY))
            return(false);
    	  
    	  if((Y % 4 == 0) && ((Y % 100 != 0) || (Y % 400 == 0)))
    	    Leap = true;
    	  
    	  if((D < 1) || (D > 31) || (M < 1) || (M > 12) || (Y < 0))
    	    return(false);
    	  
    	  if((D > Months[M-1]) && !((M == 2) && (D > 28)))
    	    return(false);
    	  
    	  if(!(Leap) && (M == 2) && (D > 28))
    	    return(false);
    	  
    	  if((Leap)  && (M == 2) && (D > 29))
    	    return(false);
	  }
	
	
	
	  //DATA-------------------------------------------------------------------------
		// 
		//	valDate	//
		// Descrizione:
		//	Formatta la data
		//-------------------------------------------------------------------------
	
	function printData(dataNear){
	data = dataNear;
	l = data.length;
	year = data.substr(0,4);
	mm	 = data.substr(5,2);
	dd	 = data.substr(8,2);
	ora  = data.substr(10,6);
	document.write(dd+"-"+mm+"-"+year+"&nbsp;<BR>"+ora);
	}
	
	function printDate(dataNear){
	data = dataNear;
	l = data.length;
	year = data.substr(0,4);
	mm	 = data.substr(5,2);
	dd	 = data.substr(8,2);
	ora  = data.substr(10,6);
	document.write(dd+"-"+mm+"-"+year+"&nbsp;&nbsp;&nbsp;"+ora);
	}
	
	
	function printDataIntera(dataNear){
	data = dataNear;
	l = data.length;
	year = data.substr(0,4);
	mm	 = data.substr(5,2);
	dd	 = data.substr(8,2);

        if(year == "1900" ){
         data ="";
        }
        else{
	data = ""+dd+"-"+mm+"-"+year;
        }

	//data = ""+dd+"-"+mm+"-"+year;
	document.manage_ticket.data_chiusura_prevista.value = data;
	document.manage_ticket.data_chiusura_prevista1.value = data;
	
	}
	
	
	function printDataSenzaOra(dataNear){
	data = dataNear;
	l = data.length;
	year = data.substr(0,4);
	mm	 = data.substr(5,2);
	dd	 = data.substr(8,2);
	ora  = data.substr(10,6);
	document.write(dd+"-"+mm+"-"+year);
	}


//-ceckTelefono------------------------------------------------------------------------
// Nome:
//	checkTelefono        
//
// Descrizione:
//	Parserizza il numero di telefono.
//-------------------------------------------------------------------------

	
	
function checkTelefono(elemento){
	if (elemento == "fax"){
		if ((document.insert_ticket.fax.value!="") && (document.insert_ticket.prefFax.value=="")){
		alert("Insert the prefix");
		document.insert_ticket.prefFax.focus();
		}
	else{
			var fax =  document.insert_ticket.fax.value;
			var prefFax =  document.insert_ticket.prefFax.value;
			var faxParsed = ""+prefFax+"/"+fax ;
			document.insert_ticket.opt_fax.value = faxParsed ;
		}
	}
	
	if (elemento == "telefono"){ 
	if ((document.insert_ticket.telefono.value!="") && (document.insert_ticket.prefTelefono.value=="")){
		alert("Insert the prefix");
		document.insert_ticket.prefTelefono.focus();
		}
		
	
	else{ 
			var telefono =  document.insert_ticket.telefono.value;
			var prefTelefono =  document.insert_ticket.prefTelefono.value;
			var telParsed = ""+prefTelefono+"/"+telefono;
			document.insert_ticket.opt_telefono.value = telParsed;
			//alert(telParsed);
		}
	}
				
	 if (elemento == "prefTelefono"){
	 
	 if((document.insert_ticket.telefono.value=="") && (document.insert_ticket.prefTelefono.value!=""))
		{
			document.insert_ticket.telefono.focus();
		}	
	 }
	 
	  if (elemento == "prefFax"){
	 
	 if((document.insert_ticket.fax.value=="") && (document.insert_ticket.prefFax.value!=""))
		{
			document.insert_ticket.fax.focus();
		}	
	 }
}



	
function checkTelefonoUtente(elemento){
	if (elemento == "visFax"){
		if ((document.insert_utente.visFax.value!="") && (document.insert_utente.prefFax.value=="")){
		alert("Insert the prefix");
		document.insert_utente.prefFax.focus();
		}
	else{
			var fax =  document.insert_utente.visFax.value;
			var prefFax =  document.insert_utente.prefFax.value;
			var faxParsed = ""+prefFax+"/"+fax ;
			document.insert_utente.fax.value = faxParsed ;
		}
	}
	
	if (elemento == "telefono_fisso"){ 
	if ((document.insert_utente.telefono_fisso.value!="") && (document.insert_utente.pref_tel_fisso.value=="")){
		alert("Insert the prefix");
		document.insert_utente.pref_tel_fisso.focus();
		}
		
	 
	else{ 
			var telefono =  document.insert_utente.telefono_fisso.value;
			var prefTelefono =  document.insert_utente.pref_tel_fisso.value;
			var telParsed = ""+prefTelefono+"/"+telefono;
			document.insert_utente.tel_fisso.value = telParsed;
		}
	}
	
	if (elemento == "telefono_cell"){ 
	if ((document.insert_utente.telefono_cell.value!="") && (document.insert_utente.pref_tel_cell.value=="")){
		alert("Insert the prefix");
		document.insert_utente.pref_tel_cell.focus();
		}
		
	
	else{ 
			var telefono =  document.insert_utente.telefono_cell.value;
			var prefTelefono =  document.insert_utente.pref_tel_cell.value;
			var telParsed = ""+prefTelefono+"/"+telefono;
			document.insert_utente.telefono_cell.value = telParsed;
		}
	}
				
	 if (elemento == "prefTelefono"){
	 
	 if((document.insert_utente.telefono.value=="") && (document.insert_utente.prefTelefono.value!=""))
		{
			document.insert_utente.telefono.focus();
		}	
	 }
	 
//	  if (elemento == "prefFax"){
	 
//     if((document.insert_utente.fax.value=="") && (document.insert_utente.prefFax.value!=""))
//	 	{
//			document.insert_utente.fax.focus();
//		}	
//	 }
	 
	
//	if (elemento =="pref_tel_cell"){
	 
//	 if((document.insert_utente.telefono_cell.value=="") && (document.insert_utente.pref_tel_cell.value!=""))
//		{
//			document.insert_utente.fax.focus();
//		}	
//	 }
}




function parseTelefono(){
	var telefono = "";
	
}
							
							

  //-------------------------------------------------------------------------
  // Nome:
  //	valDate
  //
  // Descrizione:
  //	Controlla che la data in italiano sia valida.
  //-------------------------------------------------------------------------
	
	
	function formatDate(dateForm){
	   cDate = trim(dateForm.value);
	  

	   dSize = cDate.length;
	   sCount= 0;
	 
	    if(cDate=='') return;

	   	//replace "/" char with "-" and goes on.
	   	while( cDate.indexOf("/")!="-1" )
	   	{   
	      	ind = cDate.indexOf("/");
	      	cDate = cDate.substr(0,ind)+ "-" +cDate.substr(ind+1,dSize);
	   	}
	   	
	   //Check out the dashes
	   for(var i=0; i < dSize; i++)
	      (cDate.substr(i,1) == "-") ? sCount++ : sCount;
	   
	   
	   
	   if (sCount != 2){
	      alert("Il formato della data deve essere \n ''dd-mm-yyyy''");
	      dateForm.select();
		  //dateForm.value = "";
	      return(false);
	   };
	   //Check if the year is a 2 or 4 digits string
	   ySize = cDate.substring(cDate.lastIndexOf("-")+1,dSize).length
	   if(ySize<2 || ySize>4 || ySize == 3){
         //alert('year '+ySize);
	     alert('Data non valida!');
	     dateForm.select();
		 //dateForm.value = "";
	     return false;
	   };
	   //Cut the date string into three parts (Month, Day & Year)
	   idxBarI = cDate.indexOf("-");
	   idxBarII= cDate.lastIndexOf("-");
	   strD    = cDate.substring(0,idxBarI);
	   strM    = cDate.substring(idxBarI+1,idxBarII);
	   strY    = cDate.substring(idxBarII+1,dSize);

	   strM = (strM.length < 2 ? '0'+strM : strM);   
	   strD = (strD.length < 2 ? '0'+strD : strD);
	   if(strY.length == 2)
	      strY = (strY > 50  ? '19'+strY : '20'+strY);
	   dateForm.value = strD+'-'+strM+'-'+strY;
       ok = valDate(strD, strM, strY);
	   if(ok==false )
	   { 
	      //alert(strD+','+strM+','+strY);
		  alert("Data non valida!");
	      dateForm.select();
		  //dateForm.value = "";
		  return false;	
	   }
		return true;
};

//-------------------------------------------------------------------------
  // Nome:
  //	valDate
  //
  // Descrizione:
  //	Controlla che la data in inglese sia valida.
  //-------------------------------------------------------------------------
	
	
	function formatDateUs(dateForm){
	   cDate = dateForm.value;
	  

	   dSize = cDate.length;
	   sCount= 0;
	 
	    if(cDate=='') return;

	   	//replace "/" char with "-" and goes on.
	   	while( cDate.indexOf("/")!="-1" )
	   	{   
	      	ind = cDate.indexOf("/");
	      	cDate = cDate.substr(0,ind)+ "-" +cDate.substr(ind+1,dSize);
	   	}
	   	
	   //Check out the dashes
	   for(var i=0; i < dSize; i++)
	      (cDate.substr(i,1) == "-") ? sCount++ : sCount;
	   
	   
	   
	   if (sCount != 2){
	      alert("Date must be in 'mm-dd-yyyy' format");
	      dateForm.select();
		  dateForm.value = "";
	      return(false);
	   };
	   //Check if the year is a 2 or 4 digits string
	   ySize = cDate.substring(cDate.lastIndexOf("-")+1,dSize).length
	   if(ySize<2 || ySize>4 || ySize == 3){
	     alert('Data non valida!');
	     dateForm.select();
		 dateForm.value = "";
	     return false;
	   };
	   //Cut the date string into three parts (Month, Day & Year)
	   idxBarI = cDate.indexOf("-");
	   idxBarII= cDate.lastIndexOf("-");
	   strD    = cDate.substring(0,idxBarI);
	   strM    = cDate.substring(idxBarI+1,idxBarII);
	   strY    = cDate.substring(idxBarII+1,dSize);

	   strM = (strM.length < 2 ? '0'+strM : strM);   
	   strD = (strD.length < 2 ? '0'+strD : strD);
	   if(strY.length == 2)
	      strY = (strY > 50  ? '19'+strY : '20'+strY);
	   dateForm.value = strD+'-'+strM+'-'+strY;
       ok = valDate(strM, strD, strY);
	   if(ok==false )
	   { 
	      alert("Date must be in 'mm-dd-yyyy' format");
	      dateForm.select();
		  dateForm.value = "";
		  return false;	
	   }
		return true;
};



function formatDateConSeparatore(dateForm, separatore){
	   
	   
	   cDate = dateForm.value;
	  
	   dSize = cDate.length;
	   sCount= 0;
	 
	   if(cDate=='') return;

	   	//replace "-" char with "/" and goes on.
	   	while( cDate.indexOf("-")!="-1" )  
	   	{   
	      	ind = cDate.indexOf("-");
	      	cDate = cDate.substr(0,ind)+ "/" +cDate.substr(ind+1,dSize);
	   	}
	   	
	   //Check out the dashes
	   for(var i=0; i < dSize; i++)
	      (cDate.substr(i,1) == "/") ? sCount++ : sCount;
	   
	     
	   if (sCount != 2){
	      alert("Il formato della data deve essere \n ''dd-mm-yyyy''");
	      dateForm.select();
		  dateForm.value = "";
	      return(false);
	   };
	   //Check if the year is a 2 or 4 digits string
	   ySize = cDate.substring(cDate.lastIndexOf("/")+1,dSize).length
	   if(ySize<2 || ySize>4 || ySize == 3){
	     alert('Data non valida!');
	     dateForm.select();
		 dateForm.value = "";
	     return false;
	   };
	   //Cut the date string into three parts (Month, Day & Year)
	   idxBarI = cDate.indexOf("/");
	   idxBarII= cDate.lastIndexOf("/");
	   strD    = cDate.substring(0,idxBarI);
	   strM    = cDate.substring(idxBarI+1,idxBarII);
	   strY    = cDate.substring(idxBarII+1,dSize);

	   strM = (strM.length < 2 ? '0'+strM : strM);   
	   strD = (strD.length < 2 ? '0'+strD : strD);
	   if(strY.length == 2)
	      strY = (strY > 50  ? '19'+strY : '20'+strY);
	   
	   dateForm.value = strD.toString() + separatore.toString() + strM.toString() +  separatore.toString() + strY.toString();
	   
	   
	   ok = valDate(strD, strM, strY);
	   
	   if(ok==false ) { 
	      alert("Data non valida!");
	      dateForm.select();
		  dateForm.value = "";
	      return false;	
	   }
		return true;
	};

function isAlphabetical(obj)
{
	var val= new String(obj.value);
	val = val.toLowerCase();
	for(var i=0; i<val.length; i++ )
	{
		var num = val.charAt(i);
		
		if ( num =='a' || num =='b' || num =='c' || num =='d' || num =='e' 
		  || num =='f' || num =='g' || num =='h' || num =='i' || num =='j' 
		  || num =='l' || num =='m' || num =='n' || num =='o' || num =='p' 
		  || num =='q' || num =='r' || num =='s' || num =='t' || num =='u' 
		  || num =='v' || num =='z' || num =='w' || num =='y' || num =='k' 
		  || num =='x' )
		
			ok = 1;
		else
			return false;
	}	
	if (ok == 1)
			return true;
		else
			return false;		
}

function isAlphanumerical(obj)
{
	var val= new String(obj.value);
	val = val.toLowerCase();
	for(var i=0; i<val.length; i++ )
	{
				var num = val.charAt(i);
				
				if ( num =='a' || num =='b' || num =='c' || num =='d' || num =='e' || num =='f' 
				|| num =='g' || num =='h' || num =='i' || num =='j' || num =='l' || num =='m' 
						|| num =='n' || num =='o' 
						|| num =='p' || num =='q' || num =='r' || num =='s' || num =='t' || num =='u' 
						|| num =='v' || num =='z'
						|| num =='w' || num =='y' || num =='k' || num =='x' 
						|| num =='0' || num =='1' || num =='2' || num =='3' || num =='4' || num =='5' || num =='6' || num =='7' || num =='8' || num =='9')
				
						ok = 1;
				else
					return false;
	}	
	if (ok == 1)
			return true;
		else
			return false;		
}

	

function isNumeric(v)
{
		var ok = 0;
		val = new String(v);
		if (val == null || val.length == 0 )
			return false;
		
		//alert('val.length'+val.length)
					
		for ( i=0; i < val.length; i++ )
		{
				var num = val.charAt(i);
				if ( num =='0' || num =='1' || num =='2' || num =='3' || num =='4' || num =='5' || num =='6' || num =='7' || num =='8' || num =='9' || num ==',' || num =='.')
					ok = 1;
				else
					return false;
				 
		}
		if (ok == 1)
			return true;
		else
			return false;
}

function isAlfa_2(text){
	var c = "";
	text = text.toUpperCase();
	for(var i = 0; i < text.length; i++){
		c = text.charAt(i);
		if((c < "A") || (c > "Z")){
			return false;
		}
	}
	return true;
}

function isNumeric_2(text){
	var c = "";
	for(var i = 0; i < text.length; i++){
		c = text.charAt(i);
		if(( c < "0") || (c > "9")){
			return false;
		}
	}
	return true;
}

function isFloat(v)
{
		var ok = 0;
		var virgola=0;
		val = new String(v);
		if(val.length==0)		
			return false;
		else
    		for ( i=0; i < val.length; i++ )
    		{
    			var num = val.charAt(i);
    			if (num=='.')
    				virgola++;
    			if (num==','){
    				val = v.replace(',','.');	
    				virgola++;
    			}
    			if (num =='0' || num =='1' || num =='2' || num =='3' || num =='4' || num =='5' || num =='6' || num =='7' || num =='8' || num =='9' || (i>0 && i<val.length-1 && virgola==1 && (num=='.' || num==',')) || (i==0 && i<val.length-1 && num=='-'))
    				ok++;							
    			else
    				return false;
    		}
		return true;
}



//funzione utilizzata da funzioni.jsp
function safeAlert (s)
{
	var f = null;
	
	if (eval ("typeof (" + s + ")") != "undefined")
		alert (eval (s));
	else
		alert ("DEVELOPER WARNING!!!\n\nThe client-side resource\n'" + s + "'\nwas not found!!!");
}


/******************************************************************************
validates an e-mail. this is a generic check - not fool- or lie-proof.
if the email is invalid and an error message is supplied, it is alerted to
the user. if a return field is supplied, the fxn focuses the field
******************************************************************************/
function checkEmail (sEmail, sMsg, oFld)
{
	var fOK, re;
	
	// allow user to enter spaces at the left and right and the mailto: prefix
	// later, we will remove it when the form submition takes place
	
	re = /\s*(mailto:)?[\w|\-|_]+([\.|\w|\-|_]+)*@[\w|\-|_]+(\.[\w|\-|_]+)*(\.\w{2,3})+\s*/gi;
	
	fOK = (sEmail.replace (re, "") == "");

	if (!fOK)			// something wrong?
	{
		if (sMsg && sMsg != "")		// can we alert the user?
			safeAlert (sMsg);
		if (g_fFocus && oFld)	// do we have a field to focus?
		{
			oFld.value = "";
			oFld.focus ();
		}
	}
		
	if (fOK)
		if (g_fSetValid)
			oFld.value = getEmail (sEmail);
	
	return (fOK);
}

/******************************************************************************
returns the email from the passed string
******************************************************************************/
function getEmail (sEmail)
{
	var re = /^(mailto:)/gi;
	sEmail = trim (sEmail);
	sEmail = sEmail.replace (re, "");
	return (sEmail);
}


 //-------------------------------------------------------------------------
  // Nome:
  //	Sostituisci(el,prima,dopo)
  //
  // Descrizione:
  //	Controlla che la data sia valida.
  //-------------------------------------------------------------------------


	function sostituisci(el,dopo)
	{	        
        cEl=el;    
		while( cEl.indexOf("(") != -1 )
	   	{   
                        if( cEl.indexOf(",")!= -1 )
			cEl=cEl.replace(",",dopo);
	      		idx1 = cEl.indexOf("(");
                        idx2 = cEl.indexOf(")");
			if(idx2 >= idx1)	
                           len = idx2 - idx1;

                        separa = cEl.substr(idx1,len+1);

                        cEl=cEl.replace( separa," "); 		     	
	   	}			
		//alert(" sostituisci?");			
		return cEl;
	}
/*
	funzione per arrotondare il valore di un campo alla cifra decimale scelta
*/
	function arrotondaCampo(valForm,decimali)
	{
		v=valForm.value;
		if(isFloat(v))
		{
			divid = 1;
			for(i=0; i<decimali;i++)
			{
				divid = (divid*10);
			}	
			valore = (Math.round(val*divid))/divid;
			valForm.value=valore;
		}	
	}	
/*
	funzione per arrotondare il valore di un campo alla cifra decimale scelta
*/
	function arrotondaCampoPositivo(valForm,decimali)
	{
		v=valForm.value;
		if(isFloat(v) && v>0)
		{
			divid = 1;
			for(i=0; i<decimali;i++)
			{
				divid = (divid*10);
			}	
			valore = (Math.round(val*divid))/divid;
			if(valore<0)
			    valore=0;
			valForm.value=valore;
		}
		else 
		    return (0);		
	}	
/*
	funzione per arrotondare un valore alla cifra decimale scelta
*/	
	function arrotondaNumero(val,decimali)
	{
		if(isFloat(val))
		{
			g = 1;
			for(i=0; i<decimali;i++)
			{
				g = g*10;
			}	
			valore = (Math.round(val*g))/g;
			return (valore);
		}	
	}
/*
	funzione per arrotondare un valore alla cifra decimale scelta
*/	
	function arrotondaNumeroPositivo(val,decimali)
	{
		if(isFloat(val) && val>0)
		{
			g = 1;
			for(i=0; i<decimali;i++)
			{
				g = g*10;
			}	
			valore = (Math.round(val*g))/g;
			if(valore<0)
			    valore = 0;
			return (valore);
		}
		else 
		    return (0);	
	}
/******************************************************************************************
    Funzione per richiamare la pagina del calendario e selezionare una data.
    IMPORTANTE come campo bisogna passare il nome del form oltre al nome del campo in cui riportare la data.
    ES: javascript:calendario(root,'nomeForm.nomeCampoData');
*/
	
	function calendario(root,campo)
    {
        window.open(root+'/includes/calendario.jsp?field='+campo,'date','width=239,height=170,toolbar=no,location=no,menubar=no,resizable=no');
    }		
    
    
/**
*  getFieldNext 
*  serve per ottenere il focus su un field successivo,
*  quando il field corrente ha raggiunto una certa lunghezza
*/

function getFieldNext(field, fieldNext, lunghezza ) {
    if ( field.value.length == lunghezza ) {
        fieldNext.focus();
    }
}


/**
*  aggiungiCaratteriIniziali
*  serve per normalizzare la lunghezza delle stringhe,
*  aggiunge un carattere specificato fino a raggiungere la lunghezza desiderata
*/

function aggiungiCaratteriIniziali(carattere, stringa, lunghezza ){
    while ( stringa.length < lunghezza ) {
        stringa ="" + carattere.toString() + stringa;
    }
    return stringa;
}    



//Eliminazione di possibili spazi in testa e in coda ad una stringa
function trim(stringa)
{
  str=stringa;
  //elimino gli spazi all'inizio della stringa
  while( ( str.charAt(0)==" " ) || ( str.charCodeAt(0)==255 ) || ( str.charAt(0)=="\t" ))
  {
    str=str.substr(1);
  }
  //elimino gli spazi in fondo alla stringa
  while( ( str.charAt(str.length-1)==" " ) || ( str.charCodeAt(str.length-1)==255 ) || ( str.charAt(str.length-1)=="\t" ))
  {
    str=str.substr(0,str.length-1);
  }
  //restituisco la stringa
 //if(trim.arguments[1]) stringa.value=str;
  return str;
}

function trimcrlf(stringa)
{
  srt=stringa;
  while( ( str.charCodeAt(0)==10 ) || ( str.charCodeAt(0)==13 ) || ( str.charAt(0)==" " ) || ( str.charCodeAt(0)==255 ) || ( str.charAt(0)=="\t" ))
  {
    str=str.substr(1);
  }
  //elimino gli spazi in fondo alla stringa
  while( ( str.charCodeAt(str.length-1)==10 ) || ( str.charCodeAt(str.length-1)==13 ) || ( str.charAt(str.length-1)==" " ) || ( str.charCodeAt(str.length-1)==255 ) || ( str.charAt(str.length-1)=="\t" ))
  {
    str=str.substr(0,str.length-1);
  }
  //restituisco la stringa
  //if(trim.arguments[1])stringa.value=str;
  return str;
}





//--- funzione per sito spagnolo
	function formatDateEs(dateForm){
	   cDate = trim(dateForm.value);
	  

	   dSize = cDate.length;
	   sCount= 0;
	 
	    if(cDate=='') return;

	   	//replace "/" char with "-" and goes on.
	   	while( cDate.indexOf("/")!="-1" )
	   	{   
	      	ind = cDate.indexOf("/");
	      	cDate = cDate.substr(0,ind)+ "-" +cDate.substr(ind+1,dSize);
	   	}
	   	
	   //Check out the dashes
	   for(var i=0; i < dSize; i++)
	      (cDate.substr(i,1) == "-") ? sCount++ : sCount;
	   
	   
	   
	   if (sCount != 2){
	      alert("El formato de la fecha deben ser \n ''dd-mm-yyyy''");
	      dateForm.select();
		  //dateForm.value = "";
	      return(false);
	   };
	   //Check if the year is a 2 or 4 digits string
	   ySize = cDate.substring(cDate.lastIndexOf("-")+1,dSize).length
	   if(ySize<2 || ySize>4 || ySize == 3){
         //alert('year '+ySize);
	     alert('Data  no válida!');
	     dateForm.select();
		 //dateForm.value = "";
	     return false;
	   };
	   //Cut the date string into three parts (Month, Day & Year)
	   idxBarI = cDate.indexOf("-");
	   idxBarII= cDate.lastIndexOf("-");
	   strD    = cDate.substring(0,idxBarI);
	   strM    = cDate.substring(idxBarI+1,idxBarII);
	   strY    = cDate.substring(idxBarII+1,dSize);

	   strM = (strM.length < 2 ? '0'+strM : strM);   
	   strD = (strD.length < 2 ? '0'+strD : strD);
	   if(strY.length == 2)
	      strY = (strY > 50  ? '19'+strY : '20'+strY);
	   dateForm.value = strD+'-'+strM+'-'+strY;
       ok = valDate(strD, strM, strY);
	   if(ok==false )
	   { 
	      //alert(strD+','+strM+','+strY);
		  alert("Data no valída!");
	      dateForm.select();
		  //dateForm.value = "";
		  return false;	
	   }
		return true;
};

function validaCF(cf, nome, cognome, sesso, dataNascita){
	var cfPartNome = "";	
	var cfPartCognome = "";	
	var cfPartData = "";	
	var cfPartCode = "";	

	cf = cf.toUpperCase();
	cf = trim(cf);
	if(cf.length != 16){
		return false;
	}
	if(!isNumeric_2(cf.substr(12,3))){
		return false;
	}
	if(!isAlfa_2(cf.substr(11,1))){
		return false;
	}
	cfPartCognome = getCfPartNome(cognome, "cognome");
	if(cfPartCognome.length == 0){
		return false;
	}
	if(cfPartCognome != cf.substr(0,3)){
		return false;
	}
	cfPartNome = getCfPartNome(nome, "nome");
	if(cfPartNome.length == 0){
		return false;
	}
	if(cfPartNome != cf.substr(3,3)){
		return false;
	}
	cfPartData = getCfPartData(dataNascita, sesso);
	if(cfPartData.length == 0){
		return false;
	}
	if(cfPartData != cf.substr(6,5)){
		return false;
	}
	cfPartCode = getCfPartCrc(cf);
	if(cfPartCode.length == 0){
		return false;
	}
	if(cfPartCode != cf.substr(15,1)){
		return false;
	}
	return true;
}

function getCfPartNome(text, tipo){
	var cfCons = new Array();
	var tmpCons = "";
	var tmpVoc = "";
	var outText = "";
	var i = 0;
	var c = "";
	switch (tipo.toLowerCase()){
		case "nome":
			cfCons = [1,3,4];
			break;
		case "cognome":
			cfCons = [1,2,3];
			break;
		default:
			return outText;
	}
	
	text = text.toUpperCase();
	for(i = 0; i < text.length; i++){
		c = text.substr(i,1);
		if (isAlfa_2(c)){
			if (!isVocale(c)){
				tmpCons += c;
			}else{
				tmpVoc += c;
			}
		}
	}
	if((tmpCons + tmpVoc).length > 0) {
		c = "";
		if(tmpCons.length <= 3){
			outText = tmpCons;
		}
		if(tmpCons.length > 3){
			for(i = 0; i < cfCons.length; i++){
				if(cfCons[i] <= tmpCons.length){
					outText += tmpCons.substr(cfCons[i] - 1, 1)
				}else{
					break;
				}
			}
		}
		outText += tmpVoc.substr(0, 3 - (outText.length))
		for(i=0; i < 3 - outText.length; i++){
			outText += "X";
		}
	}
	return outText;
}
function getCfPartData(data, sesso){
	var cfMesi = new Array();
	cfMesi["01"]= "A";
	cfMesi["02"]= "B";
	cfMesi["03"]= "C";
	cfMesi["04"]= "D";
	cfMesi["05"]= "E";
	cfMesi["06"]= "H";
	cfMesi["07"]= "L";
	cfMesi["08"]= "M";
	cfMesi["09"]= "P";
	cfMesi["10"]= "R";
	cfMesi["11"]= "S";
	cfMesi["12"]= "T";
	var outText = "";
	var day = "";
	var month = "";
	var year = "";
	sesso = trim(sesso);
	sesso = sesso.toUpperCase();
	data = cleanDate(data);
	if(((sesso == "M") || (sesso == "F")) && (data.length == 10)){
		day = data.substr(0,2);
		if(sesso == "F"){
			day = parseInt(day,10) + 40;
		}
		month = data.substr(3,2);
		year = data.substr(8);
		outText = year +  cfMesi[month] + day;
	}
	return outText;
}
function getCfPartCrc(cf){
	var i = 0;
	var code = 0;
	var c = "";
	cfCode = new Array();
	cfCode["A"] = 1;
	cfCode["B"] = 0;
	cfCode["C"] = 5;
	cfCode["D"] = 7;
	cfCode["E"] = 9;
	cfCode["F"] = 13;
	cfCode["G"] = 15;
	cfCode["H"] = 17;
	cfCode["I"] = 19;
	cfCode["J"] = 21;
	cfCode["K"] = 2;
	cfCode["L"] = 4;
	cfCode["M"] = 18;
	cfCode["N"] = 20;
	cfCode["O"] = 11;
	cfCode["P"] = 3;
	cfCode["Q"] = 6;
	cfCode["R"] = 8;
	cfCode["S"] = 12;
	cfCode["T"] = 14;
	cfCode["U"] = 16;
	cfCode["V"] = 10;
	cfCode["W"] = 22;
	cfCode["X"] = 25;
	cfCode["Y"] = 24;
	cfCode["Z"] = 23;
	cfCode["0"] = 1;
	cfCode["1"] = 0;
	cfCode["2"] = 5;
	cfCode["3"] = 7;
	cfCode["4"] = 9;
	cfCode["5"] = 13;
	cfCode["6"] = 15;
	cfCode["7"] = 17;
	cfCode["8"] = 19;
	cfCode["9"] = 21;
	
	cf = cf.toUpperCase();
	for(i = 0; i < 15; i++){
		c = cf.substr(i,1);
		if(isAlfa_2(c) || isNumeric_2(c)){
			if(i % 2 > 0){
				if (isAlfa_2(c)){
					code -= "A".charCodeAt();
				}else{
					code -= "0".charCodeAt();
				}
				code += c.charCodeAt();
			}else{
				code += cfCode[c];
			}
		}else{
			code = "";
			break;
		}
	}
	if (code != ""){
		code = String.fromCharCode((code % 26) + "A".charCodeAt());
	}
	return code;
}

function isVocale(c){
	var vocale = false;	
	var cfVocali = ["A", "E", "I", "O", "U"];
	for(var j = 0; j < cfVocali.length; j++){
		if (c == cfVocali[j]){
			vocale = true;
			break;
		}
	}
	return vocale;
}

function cleanDate(data){
	var err = 1;
	if (data.length == 0){
		return "";
	}
	if (data.length > 7){
		var tmp = new Array();
		var p = 0;
		for(var i = 0; i < data.length; i++){
			var c = data.slice(i, i+1);
			if(c == "-" || c == "/" || c == "." || c == ","){
				tmp.push(data.slice(p, i));
				p = i + 1;
			}
		}
		tmp.push(data.slice(p));
		if(tmp.length == 3){
			err = 0;
			for(i = 0; i < tmp.length; i++){
				if(isNaN(tmp[i])){
					err = 1;
					break;
				}
			}
		}
		if(err == 0){
			if((tmp[2] > 2100) || (tmp[2] < 1900) || (tmp[1] < 1) || (tmp[1] > 12) || (tmp[0] < 1) || (tmp[0] > 31)){
				err = 1;
			}
		}
		if(err == 0){
			if (tmp[0] > maxdays(tmp[1], tmp[2])){
				err = 1;
			}
		}
	}
	if(err){
		return "";
	}else{
		if(tmp[0].length < 2 ){
			tmp[0] = "0" + tmp[0];
		}
		if(tmp[1].length < 2  ){
			tmp[1] = "0" + tmp[1];
		}
		data = tmp[0] + "/" + tmp[1] + "/" + tmp[2];
		return data;
	}
}

function maxdays(month, year)
{
	var mtds = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	if( year%4==0 )
	{
		if( ( year%100==0 ) && ( year%400!=0 ) )
		{
			mtds[1]=28;
		}
		else
		{
			mtds[1]=29;
		}
	}
	return mtds[month-1];
}