//SABA - Controlli sul tipo di browser
browsername=navigator.appName;
if (browsername.indexOf("Mozilla")!=-1) {ns4=true;
 ie4=false;}
else
{ns4=false; 
 ie4=true;}
//------ Controlli sul tipo di browser
function datatostringa(nomedata)
{
  prim=nomedata.indexOf('/');
  prim_1=nomedata.substr(0,prim);
  nomedata=nomedata.substr(prim+1);
  seco=nomedata.indexOf('/');
  seco_1=nomedata.substr(0,prim);
  nomedata=nomedata.substr(seco+1);
  outputvalue=nomedata+ ',' + seco_1+','+prim_1;
  return outputvalue;
}
function ControllaOrario(contr)
	{
	if (contr.substr(2,1) != ".")
		{
		alert("L'orario immesso non è corretto");
		return false;
		}
	
	if (isNaN(contr.substr(0,2)))
		{
		alert("L'orario immesso non è corretto");
		return false;
		}
	if ((parseInt(contr.substr(0,2)) < 0) || (parseInt(contr.substr(0,2)) > 23))
		{
		alert("L'orario immesso non è corretto");
		return false;
		}
	
	if (isNaN(contr.substr(3,2)))
		{
		alert("L'orario immesso non è corretto");
		return false;
		}
	if ((parseInt(contr.substr(3,2)) < 0) || (parseInt(contr.substr(3,2)) > 59))
		{
		alert("L'orario immesso non è corretto");
		return false;
		}
	return true;
	}

function ControllaLunghezza(nomecampo,lung,nome)
{
	if (ie4)
		var valore = document.all[nomecampo].value;
	if (ns4)
		var valore =document.getElementById(nomecampo).value;
	if (valore == "")	 
	{
		return true; 
	}
	else
	{
		if (valore.length > lung)
		{	alert("Limite massimo di caratteri superato nel campo " + nome + ": la stringa sarà troncata al carattere "+ lung);
	if (ie4)
		document.all[nomecampo].value=valore.substring(0,lung-1);
	if (ns4)
		document.getElementById(nomecampo).value=valore.substring(0,lung-1);
	return false;
		} 
	}
	return true;
}


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;
}


function Verifica_Data(data_uno,simbolo,data_due,data_uno_alert,data_due_alert)    
{

  var prima
 var seconda
 if (data_uno_alert == "")
    { data_uno_alert = "Data odierna"; }

 if (data_due_alert == "")
    { data_due_alert = "Data odierna"; }

 if ((data_uno == "") || (data_due == ""))
    {
     var today = new Date(); 
     var giorno;
     if (today.getDate() <= 9)
        { giorno = "0" + today.getDate(); }
     else   
        { giorno = today.getDate(); } 
     if (today.getMonth() <= 9)
        { today = today.getFullYear() + ",0" + (today.getMonth() + 1) + "," + giorno; }
     else   
        { today = today.getFullYear() + "," + (today.getMonth() + 1) + "," + giorno; }  
    }

 if (data_uno == "")
    { prima = today; }
 else   
    { prima= datatostringa(data_uno);
      //prima = data_uno.split(['/']); 
      //prima = prima.reverse();
      } 
 if (data_due == "")
    { seconda = today;}
 else   
    { seconda=datatostringa(data_due);
		//seconda = data_due.split(['/']); 
        //seconda = seconda.reverse();
        }
 switch (simbolo)
        { case "maggiore": 
                  { if (prima > seconda)
                       { return true; } 
                    else
                       { alert("La1 " + data_uno_alert + " deve essere posteriore alla " + data_due_alert);
                         return false; }
                  }
          case "maggiore_uguale": 
                   {
                    if (!(prima < seconda))
                        { return true; } 
                     else
                        { alert("La " + data_uno_alert + " non deve essere anteriore alla " + data_due_alert);
                          return false; }
                   }
	  case "minore": 
                   { if (!(prima >= seconda))
                        { return true; } 
                     else
                        { alert("La " + data_uno_alert + " non deve essere posteriore alla " + data_due_alert);
                          return false; }
                   }
        }
}

function Date_Uguali (giorno1,mese1,anno1,giorno2,mese2,anno2)
{
	var data1;
 	var data2;
	data1 =anno1 + "," + mese1 + "," + giorno1;
	data2 =anno2 + "," + mese2 + "," + giorno2;
	if (data1 == data2)
		{return true;}
	else
		{return false;}
	
}

function Controlla_Ora (ora1,min1,ora2,min2)
{
	var orario1;
 	var orario2;

	orario1 =ora1 +  min1;
	orario2 =ora2 +  min2;

	if (orario2 > orario1)
		{return true;}
	else
		{return false;}

	
}


function Date_Nulle (giorno1,mese1,anno1,giorno2,mese2,anno2,stringa,obbligatorio)
{
	var data1;
 	var data2;
	data1 =anno1 +  mese1 + giorno1;
	data2 =anno2 +  mese2 + giorno2;
	if (obbligatorio == "no nullable")
	{
	if ((data1 == "") && (data2 == ""))
		{
			alert("Inserire l'intervallo di date " + stringa);
			return true;
		}
	if (data1 == "") 
		{
			alert("Inserire la data inizio dell'intervallo " + stringa);
			return true;
		}
	if (data2 == "") 
		{
			alert("Inserire la data fine dell'intervallo " + stringa);
			return true;
		}
	return false;
	}
	else
	{
	if ((data1.length == 0) && (data2.length == 0))
		{
			return 0;
		}
	if ((data1.length > 0) && (data2.length > 0)) 
		{
			return 2;
		}
	if ((data1.length == 0) && (data2.length > 0)) 
		{
			if (stringa.length > 0 )
				{alert("Inserire la data inizio dell'intervallo " + stringa);}
			return 1;
		}
	if ((data2.length == 0) && (data1.length > 0))  
		{
			if (stringa.length > 0 )
				{alert("Inserire la data fine dell'intervallo " + stringa);}
			return 1;
		}
	
	}
	
	
	
}


function Controlla_Data(giorno1,mese1,anno1,giorno2,mese2,anno2,campo1,campo2,operazione)
{

 var data1;
 var data2;
 if (campo1.length >0)
 { 
 	data1 = giorno1 + "/" + mese1 + "/" + anno1;
 	if ((data1.length < 10) && (data1.length > 2))
        {
			alert("Data [" +  campo1 + "] non corretta");
			return false;
        } 
	else
		{
			if (data1.length == 10)
			{
				if (!(ISdata(data1)))
					{alert("Data [" +  campo1 + "] non corretta");
					return false;}
			}
		}

	if (operazione == "I")   
	{
	  	if ((data1.length == 10))    
	    	{var str = "Data di " + campo1 + " ";
    			if (!(Verifica_Data(data1,'maggiore_uguale','',str ,'Data odierna')))
		     	 	{return false; } 
	    	}
	}  
	if (operazione == "O")   
	{
	  	if ((data1.length == 10))    
	    	{var str = "Data di " + campo1 + " ";
	    		if (!(Verifica_Data(data1,'minore','',str ,'Data odierna')))
		     	 	{return false; } 
	    	}
	}  	
 }
  if (campo2.length > 0) 
 {
 	data2 = giorno2 + "/" + mese2 + "/" + anno2;
 	if ((data2.length < 10) && (data2.length > 2))
        {alert("Data [" +  campo2 + "] non corretta");
        return false; } 
	else
	{if (data2.length == 10)
		{
			if (!(ISdata(data2)))
				{alert("Data [" +  campo2 + "] non corretta");
				return false;}
		}
	}
 }  
if ((campo1.length >0) &&  (campo2.length > 0)  )
{
 if ((data1.length >2) && (data2.length > 2))
 {

   var str1 = "Data di " + campo2 + " ";
   var str2 = "Data di " + campo1 + " ";
   
	 if (!(Verifica_Data(data2,'maggiore_uguale',data1,str1,str2)))
        {return false; } 
 }
}

   return true;     

}




/**************************************************************/
/* Controlla il numero di caratteri immessi nella descrizione */
/**************************************************************/
				
MassimoCarDes = 800
				
function IniziaDes(nomeconta,nomecampo)
		{
		 running = true
		 now = new Date()
		 now = now.getTime()
		 endTime = now + (1000 * 0.1 * 1)
		 ContaDes(nomeconta,nomecampo)
		}
		
function ContaDes(nomeconta,nomecampo)
		{
		 var now = new Date()
		 now = now.getTime()
		 if (endTime - now <= 0)
		 {
		  if(navigator.appVersion.substring(5,25) != "compatible; MSIE 5.0" && navigator.appVersion.substring(5,25) != "compatible; MSIE 5.5")
		  {
		   document.all[nomeconta].focus();
		   document.all[nomecampo].focus();
		  }
		  ControllaDes(document.all[nomecampo].value,nomeconta,nomecampo)
		 }
		 else
		 {
		  var delta = new Date(endTime - now)
		  var theMin = delta.getMinutes()
		  var theSec = delta.getSeconds()
		  var theTime = theMin
		  theTime += ((theSec < 10) ? ":0" : ":") + theSec
		  if (running)
		  {
		  timerID = setTimeout("ContaDes('"+ nomeconta+ "','" + nomecampo+ "')",1000)
		  }
		 }
		}
		
function FineDes()
		{
		 clearTimeout(timerID)
		 running = false
		}
		
function ControllaDes(inputStr,nomeconta,nomecampo)
		{
		 
		 strlength = inputStr.length
		 if (strlength > MassimoCarDes )
		 {
		  alert("Hai raggiunto il numero max di caratteri.Il paragrafo sarà troncato al carattere "+MassimoCarDes);
		  document.all[nomecampo].value=inputStr.substring(0,MassimoCarDes)
		  charleft = 0
		 }
		 else
		 {
		  charleft = MassimoCarDes - strlength
		 }
		 document.all[nomeconta].value=charleft
		 IniziaDes(nomeconta,nomecampo)
		}
		
function ControllaDes1(nomeconta,nomecampo)
		{
		
		 inputStr = document.all[nomecampo].value
		 strlength= inputStr.length
		 if (strlength > MassimoCarDes )
		 {
		 document.all[nomecampo].value= inputStr.substring(0,MassimoCarDes)
		  charleft = 0
		 }
		 else
		 {
		  charleft = MassimoCarDes - strlength
		 }
		 document.all[nomeconta].value=charleft
	    FineDes()
		}


/********** funzione di validazione della email ***************/
/********** controlla che sia stato inserito un carattere "@" e un carattere "." ***********/ 

function validate_email(txtEmail) {
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("Email non valida"); 
	//txtEmail.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_tel(valore,nome) 
{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("Nel campo "+ nome+ " sono ammessi solo numeri, spazi, '/','.'");
				return false;}
		}
	}
	return true;
}


/********** funzione di validazione della data ***************/
/* il parametro di input deve essere nel formato "dd/mm/yyyy" */
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;
		}




