//SABA - Controlli sul tipo di browser
browsername=navigator.appName;
if (browsername.indexOf("Netscape")!=-1) {ns4=true;
 ie4=false;}
else
{ns4=false; 
 ie4=true;}
//------ Controlli sul tipo di browser
function trim(stringa)
{
    // Utilizza un'espressione regolare per sostituire gli spazi 
    // iniziali e finali con la stringa vuota
    return stringa.replace(/(^\s*)|(\s*$)/g, "");
}


function ControlloNumerico(nomecampo,nome)
{
	if (ie4)
		var valore = document.all[nomecampo].value;
	if (ns4)
		var valore =document.getElementById(nomecampo).value;
	if (valore == "")	
	{
		return true;
	}
	else
	{
		if (isNaN(valore))
		{	alert("Inserire un valore numerico nel campo " + nome);
			if (ie4)
				document.all[nomecampo].value="";
			if (ns4)
				document.getElementById(nomecampo).value="";

			
			return false;
		}
	}
	return true;
}
/********** funzione di validazione dei campi contenenti i numeri di telefono e fax ***************/
/********** controlla che siano stati inseriti solo numeri, spazi e "/" ***********/ 
function validate_telx(valore,nome,nelcampo,ammessi) 
{
	var i;
	if (valore=="") {return true;}

	for(i=0;i<valore.length;i++) 
	{ 
		if ((isNaN(valore.toString().charAt(i))))
		{
			if (( valore.toString().charAt(i) != " " ) && ( valore.toString().charAt(i) != "/" ) && ( valore.toString().charAt(i) != "." ))
				{alert(nelcampo + " " + nome+ " " + ammessi );
				return false;}
		}
	}

	return true;
}

function validate_emailx(txtEmail,aler) 
{
	Email=txtEmail.value;
	if (Email=="") {return true;}
	counter_chiocciola=0;
	counter_punto=0;
	for(i=0;i<Email.length;i++) { 
		if ( Email.toString().charAt(i) == "@" ) {counter_chiocciola++};
		if ( Email.toString().charAt(i) == "." ) {counter_punto++};
		}
		if ((counter_chiocciola == 0) || ( counter_chiocciola > 1 ) || (counter_punto == 0)) {
		alert(aler); 
		return false;
		}
	return true;
}

function ISdata(stringa) 
	{
	var data = stringa; 
	if (data.length > 0) 
		{
		var ok = true; 
		var first = false; 
		var second = false; 
		var g = 0; 
		var m = 0; 
		var giorno; 
		var mese; 
		var anno; 
		for(i=0;i<data.length;i++) { 
		if (data.charAt(i) != '/' && (data.charAt(i) < '0') || (data.charAt(i) > '9')) ok = false; 
		} 
    
    if (!ok)
		{
		return false;
		}
    for(i=0;i<data.length;i++)
		{ 
		if (data.charAt(i) == '/' && !first)
			{
			first = true;
			g = i;
			}else if (data.charAt(i) == '/' && first)
				{ 
				second = true;
				m = i;
				} 
			} 
		if (!first || !second) ok = false; 
		
		if (!ok)
			{
			return false;
			}
		giorno = data.substring(0,g); 
		mese   = data.substring(g+1,m); 
		anno   = data.substring(m+1,data.length); 
		if (anno < 1000) ok = false;     
		if (mese < 1 || mese > 12) ok = false;     
		if (giorno < 1 || giorno > 31) ok = false; 
		if (giorno > 30 && (mese == '4' || mese == '04')) ok = false; 
		if (giorno > 30 && (mese == '6' || mese == '06')) ok = false; 
		if (giorno > 30 && (mese == '9' || mese == '09')) ok = false; 
		if (giorno > 30 &&  mese == '11') ok = false; 
		if (giorno >29 && (mese == '2' || mese == '02')) ok=false;
		if (giorno == '29' && (mese == '2' || mese == '02'))
			{
			if (anno % 4 == 0)
				{
				if (anno % 100 == 0)
					{
					if (anno % 400 != 0) ok = false; 
					}
				}else ok = false;
			} 
    
		if (!ok)
			{
			return false;
			}
		}
		return true;
		}
		
/********************************************************************
 INSIEME DI FUNZIONI JAVASCRIPT PER VARI CONTROLLI (DATA, E-MAIL ECC.)
 IL FILE DEVE ESSERE INCLUSO NELLE PAGINE ASP.
**********************************************************************/ 
var browser_type=navigator.appName
var browser_version=parseInt(navigator.appVersion)
var navigatore=browser_type+browser_version
var lung_campo;     //VARIABILE PER CONTROLLO LUNGHEZZA CAMPO
var controllo;      //VARIABILE PER TIPO DI CONTROLLO (NUMERO, DATA, LETT...)
var tastopremuto;   //VARIABILE PER TASTO PREMUTO
var d;

/*--- A SECONDA DEL BROWSER IMPOSTA IL NUMERO DI CARATTERI INSERIBILI PER IL CAMPO DATA ----*/
if (navigatore=="Microsoft Internet Explorer4") {lung_campo=10};
if (navigatore=="Netscape4") {lung_campo=8};
if (navigatore=="Netscape5") {lung_campo=8};

IE4 = (document.all);
NS4 = (document.layers);

if (NS4) document.captureEvents(Event.KEYPRESS);
/*---- Associa all'evento keypress la funzione keypress_int() -----*/
document.onkeypress = keypress_int;
/*---- Associa all'evento keydown la funzione intercettatasti() ----*/
document.onkeydown = intercettatasti;

//Nome					:	keypress_int
//Descrizione			:	Funzione di controllo caratteri inseriti. 
//Parametri di input	:	
//Parametri di output	:	
//Note					:	La funzione è eseguita ogni volta che l'utente digita un tasto
//                          su di in campo al quale è associato all'evento onkeypress tale funzione.
//                          In base al tipo di controllo (numerico, data, ecc..) esegue determinate
//                          istruzioni.
function keypress_int(e) { 
var tipoelemento =window.event.srcElement.type;
  whichASC = (NS4) ? e.which : event.keyCode;
  whichKey = String.fromCharCode(whichASC);

  /************ QUi bisogna definire il tipo di controllo che vogliamo e assegnargli un nome mediante la variabile controllo *****/
  if (whichASC == 13 && tipoelemento!='textarea' && tipoelemento!=null) {return false;}
	
	if (controllo=="num") {
  	if (((whichKey<"0") || (whichKey>"9")) && (whichKey!="\u0008") && (whichKey!=".")) {return false;}
       }
       
	if (controllo=="numdec") 
	{
		if (((whichKey<"0") || (whichKey>"9"))  && (whichASC!=44) && (whichKey!="\u0008")) {return false;}
        
       }
       
	if (controllo=="data") {
  	
	if (((whichKey<"0") || (whichKey>"9")) && (whichKey!="\u0008")) {return false;}
       }
	if (controllo=="ora") {
  	
	if (((whichKey<"0") || (whichKey>"9")) && (whichKey!="\u0008")) {return false;}
       }

	if (controllo=="lett") {
	if (((whichKey<"a") || (whichKey>"z")) && (whichKey!="\u0008") && (whichASC != 32) && (whichKey!="'") && (whichKey!="à") && (whichKey!="è") && (whichKey!="ì") && (whichKey!="ù") && (whichKey!="@") && ((whichKey<"A") || (whichKey>"Z"))) {return false;}
	    }
	    
	if (controllo=="email") {
	if (((whichKey<"a") || (whichKey>"z")) && ((whichKey<"0") || (whichKey>"9")) && (whichKey!="\u0008") && (whichKey!="@") && (whichKey!="_") && (whichKey!=".") && (whichKey!="-") && (whichKey!="+") && ((whichKey<"A") || (whichKey>"Z"))) {return false;}
	    }

	if (controllo=="num+lett") {
	//if (((whichKey<"a") || (whichKey>"z")) && ((whichKey<"0") || (whichKey>"9")) && (whichKey!="\u0008") && (whichASC != 32) && ((whichKey<"A") || (whichKey>"Z"))) {return false;}
	if (((whichKey<"a") || (whichKey>"z")) && ((whichKey<"0") || (whichKey>"9")) && (whichKey!="\u0008") && (whichASC != 32)&& (whichKey!="_") && (whichKey!=".") && (whichKey!=":") && (whichKey!=",") && (whichKey!=";") && (whichKey!="@") && (whichKey!="<") && (whichKey!=">") && (whichKey!="£") && (whichKey!="$") && (whichKey!="(") && (whichKey!=")") && (whichKey!="*") && (whichKey!="+") && (whichKey!="!") && (whichKey!="à") && (whichKey!="è") && (whichKey!="ì") && (whichKey!="ù") && (whichKey!="-") && ((whichKey<"A") || (whichKey>"Z"))) {return false;}
	    }
}

//Nome					:	intercettatasti
//Descrizione			:	Intercetta il tasto premuto. 
//Parametri di input	:	
//Parametri di output	:	
function intercettatasti() 
{   
 tastopremuto = (NS4) ? e.which : event.keyCode;
}

//Nome					:	textContatore
//Descrizione			:	Controlla se si è inserito il numero max di caratteri:in questo caso li tronca. 
//Parametri di input	:	1. Oggetto sul quale effettuare il controllo
//                          2. Numero max di caratteri da inserire.
//Parametri di output	:	
//Note                  :
function textContatore(campo, limitemassimo) 
{
	if (campo.value.length > limitemassimo) 
	  {campo.value = campo.value.substring(0, limitemassimo);}

    /*********** se si sta effettuando l'inserimento di una data la formatta direttamente aggiungendovi il car. "/"
	     oppure nel caso si stia cancellando il carattere "/" con il backspace elimina anche la cifra immediatamente
	     precedente *********/

		if ((controllo=="data") && (navigatore=="Microsoft Internet Explorer4")) {
			
			if ((campo.value.length == 2) && (tastopremuto!=8)) {campo.value=campo.value+"/";} 
			if ((campo.value.length == 5) && (tastopremuto!=8)) {campo.value=campo.value+"/";} 
			if ((campo.value.length == 2) && (tastopremuto==8)) {campo.value=campo.value.substring(0, campo.value.length-1);} 
			if ((campo.value.length == 5) && (tastopremuto==8)) {campo.value=campo.value.substring(0, campo.value.length-1);} 
		}


    /*********** se si sta effettuando l'inserimento di un ora la formatta direttamente aggiungendovi il car. ":"
	     oppure nel caso si stia cancellando il carattere ":" con il backspace elimina anche la cifra immediatamente
	     precedente *********/

		if ((controllo=="ora") && (navigatore=="Microsoft Internet Explorer4")) {
			
							
			if ((campo.value.length == 2) && (tastopremuto!=8)) {campo.value=campo.value+":";} 
			if ((campo.value.length == 2) && (tastopremuto==8)) {campo.value=campo.value.substring(0, campo.value.length-1);} 
			
		}
}

//Nome					:	validate_email
//Descrizione			:	Controlla se l'e-mail è stato inserito nel formato corretto. 
//Parametri di input	:	1. Oggetto sul quale effettuare il controllo
//Parametri di output	:	
//Note                  :   
function validate_email(txtEmail, Control) 
{
    var Email = txtEmail.value;
    if (Email=="") 
       {return true;}
    var counter_chiocciola=0;
    var counter_punto=0;
    for(i=0;i<Email.length;i++) 
      { 
	    if ( Email.toString().charAt(i) == "@" ) 
	       {counter_chiocciola++};
	    if ( Email.toString().charAt(i) == "." ) 
	       {counter_punto++};
	  }
	if ((counter_chiocciola == 0) || ( counter_chiocciola > 1 ) || (counter_punto == 0)) 
	 {
	  alert("Email non valida"); 
   	  if (Control == "F") {txtEmail.focus();}
   	  return false;
	 }
	
	return true; 
}

//Nome					:	validate_data
//Descrizione			:	Funzione di controllo data. 
//Parametri di input	:	1. Oggetto data da controllare
//                          2. Se la data è obbligaoria
//                          3. Tipo Controllo
//                             C => Non Viene eseguito il focus sul campo in caso di errore
//                             F => Viene eseguito il focus sul campo in caso di errore
//Parametri di output	:	Nessuno
//Note					:	Al momento il controllo avviene nel formato italiano gg/mm/anno
function validate_data(txtData,obbligatoria,Control) 
{
 if (Control=="C")
   {if (controllo=="lett") 
     {return true;}
   } 

  /*------- Se il browser e' netscape formatta la data, altrimenti e' già formattata -----*/
 if ((navigatore=="Netscape4") || (navigatore=="Netscape5")) 
 {
  var miadata  = txtData.value; 
  var gg       = miadata.substring(0,2);
  var mm       = miadata.substring(2,4);
  var anno     = miadata.substring(4,8);
  var d        = gg+"/"+mm+"/"+anno;
  txtData.value= d;
 }

 if (navigatore=="Microsoft Internet Explorer4") 
   {d=txtData.value;}

 /*----- Controlla se è stata inserita la stringa vuota ----*/   
 if ( d.toString() == "" )
 {
   /********* ...in tal caso verifica se la data è obbligatoria ***********/	 
   if  (obbligatoria == true) 
   {
    /********* Se la data è obbligatoria segnala messaggio di errore *******/	
	alert("ATTENZIONE!\nData Obbligatoria.\nEsempio: 01/01/1970");
	if (Control=="F") {txtData.focus();}
	return false;
   }
   /********* Se la data non è obbligatoria esce dalla funzione **********/		
   else 
     {return true;}
 }
 /*------ Controlla se è stata inserita una stringa che inizia con uno spazio...---- */	
 if ( d.toString().charAt(0) == " " ) 
 {
  /********* ...in tal caso verifica se la data è obbligatoria ************/			
  if  (obbligatoria == true) 
  {
   /********* Se la data è obbligatoria segnala messaggio di errore **********/	
   alert("ATTENZIONE!\nData Obbligatoria.\nEsempio: 01/01/1970");
   if (Control=="F") {txtData.focus();}
   return false;
  }	
  else 
  {
   /********** Se la data non è obbligatoria esce dalla funzione e setta il valore
    della data a stringa vuota **********/
   txtData.value = "";
   return true;
  }
 }
 /********** Accetta solo stringhe di 10 caratteri gg/mm/aaaa *************/
 if (d.length < 10 || d.charAt(2) != '/' || d.charAt(5) != '/')
 {
   alert("ATTENZIONE!\n Data non valida.\n Esempio 01/01/1970");
   if (Control=="F") {txtData.focus();}
   return false;
 }
 for(i=0;i<d.length;i++) 
 {
   if (d.charAt(i) != '/' && (d.charAt(i) < '0') || (d.charAt(i) > '9')) 
   {
     alert("ATTENZIONE!\n Data non valida. Esempio 01/01/1970");
	 if (Control=="F") {txtData.focus();}
     return false;
   }
 }
 /*********** Controlla se è una data lecita per giorni e per mesi ***********/    
 if (d.substring(0,2) < "01" || d.substring(0,2) > "31")
 {
   alert("ATTENZIONE!\nGiorno non valido (01..31).");
   if (Control=="F") {txtData.focus();}
   return false;	
 }
 if (d.substring(3,5) < "01" || d.substring(3,5) > "12")
 {
   alert("ATTENZIONE!\nMese non valido (01..12).");
   if (Control=="F") {txtData.focus();}
   return false;	
 }
 /************ Differenzia i mesi con 30 gg da quelli di 31 ***********/  
 if ((d.substring(3,5) == "02" || d.substring(3,5) == "04" || d.substring(3,5) == "06" || d.substring(3,5) == "09" || d.substring(3,5) == "11" ) && d.substring(0,2) > "30") 
 {
   if (d.substring(3,5) == "02")
	   {alert("ATTENZIONE!\nData non valida.\nMax giorni = 28");}
     else
	   {alert("ATTENZIONE!\nData non valida.\nMax giorni = 30");}
     if (Control=="F") {txtData.focus();}
     return false;
 }
 var mia = d.substring(6,10);
 /************* Verifica se si tratta di un anno bisestile nel caso il mese sia 02 e i giorni maggiori di 28 ***********/
 if ( (mia.valueOf() % 4 == 0) && (d.substring(3,5) == "02" ) && (d.substring(0,2) > "29")) 
 {
  	alert("ATTENZIONE!\nData non valida.\nMax giorni = 29");
    if (Control=="F") {txtData.focus();}
    return false;
 }
 else
 {
  /************ Nel caso l'anno non sia bisestile può accettare solo 28 gg ************************/   
  if ((mia.valueOf() % 4 != 0) &&  (d.substring(0,2) > "28") && (d.substring(3,5) == "02" )) 
  {
    alert("ATTENZIONE!\nData non valida.\nMax giorni = 28");
    if (Control=="F") {txtData.focus();}
    return false;
  }
 }
 
 controllo="none";
 return true;
}


//Nome					:	validate_ora
//Descrizione			:	Controlla la validità dell'ora inserita. 
//Parametri di input	:	1. Ora inserita
//Parametri di output	:	
//Note                  :   
function validate_ora(txtOra) 
{
if (txtOra.value=="") {return true;}

var ora=txtOra.value

/********** Accetta solo stringhe di 5 caratteri caratteri hh:mm *************/
    if ((ora.length < 5) || (ora.charAt(2) != ':'))
    	{
	    	
		alert("ATTENZIONE!\n Orario non valido.\n Esempio 08:00");
    		txtOra.value = "";
	       	txtOra.focus();
    		return false;
    	}

    for(i=0;i<ora.length;i++) 
    	{

      		if (ora.charAt(i) != ':')
		{
			if  ((ora.charAt(i) < '0') || (ora.charAt(i) > '9')) 
      			{
    				
				alert("ATTENZIONE!\n Orario non valido.\n Esempio 08:00");
     				txtOra.value = "";
		       		txtOra.focus();
	    			return false;
    			}	
		}
    	}


/*********** Controlla se è una un orariolecito per ore e per minuti ***********/    
    if (ora.substring(0,2) < "00" || ora.substring(0,2) > "23")
    {
     	alert("ATTENZIONE!\nOra non valida (01..23).");
     	txtOra.value = "";
     	txtOra.focus();
     	return false;	
    }
   if (ora.substring(3,5) < "00" || ora.substring(3,5) > "60")
    {
     	alert("ATTENZIONE!\nMinuti non validi (00..60).");
     	txtOra.value = "";
     	txtOra.focus();
     	return false;	
    }

  controllo="num+lett";
 }


//Nome					:	textContatoretextarea
//Descrizione			:	Conta numero di caratteri inseriti e quelli rimasti
//Parametri di input	:	1. Campo per il quale effettuare il controllo
//                          2. Caratteri rimasti
//                          3. Limite massimo da inserire
//Parametri di output	:	
//Note                  :   
function textContatoretextarea(campo, car_rimasti, limitemassimo) {
	if (campo.value.length > limitemassimo) 
	campo.value = campo.value.substring(0, limitemassimo);
	else 
	car_rimasti.value = limitemassimo - campo.value.length;
}

//Nome					:	ControlloNume
//Descrizione			:	All'uscita del campo numerico controllla se ci sono due .. e mette
//                          00 se non si inserisce nulla
//Parametri di input	:	1. Oggetto sul quale effettuare il controllo
//Parametri di output	:	
//Note                  :
function ControlloNume(campo) 
{
 var valore = campo.value;
 
 var re  = new RegExp("\\.", "ig");
 var po  = valore.search(re);
 if (po != -1)
  {
    var prima = valore.substr(0,po);
    var dopo  = valore.substr(po+1);
    var po    = dopo.search(re);    
    if (po != -1)
     {
      alert("Numero non corretto!");
      campo.focus(); 
      return false;
     }
    if (dopo.length == 0) 
     {campo.value = campo.value + "00";}
  }
 return true; 
}