// tutti numeri
var isnumeric = 'isnumeric'
var m_isnumeric='non è NUMERICO'
//lettere senza spazi e numeri
var istext=/^[a-zA-Z]+$/
var m_istext='Non è LETTERALE'
//lettere MAIUSCOLE senza spazi e numeri
var isucasetext=/^[A-Z]+$/
var m_isucasetext='Non è composto da sole lettere MAIUSCOLE'
//tutto
var isstring=/ /
var m_isstring='Non è una stringa'
//lettere minuscole senza spazi e numeri
var islcasetest=/^[a-z]+$/
var m_islcasetest='Non è composto da sole lettere MINUSCOLE'
//range lunghezza parola
var isinrange=/^.{0,8}$/
var m_isinrange='Non è della LUNGHEZZA prevista'
var iscf=/^.{16,16}$/
var m_iscf='Il Codice Fiscale non è della LUNGHEZZA prevista'
var iscfiva=/^.{11,16}$/
var m_iscfiva='non è della LUNGHEZZA prevista'
var ispi=/^.{11,11}$/
var m_ispi='La Partita IVA non è della LUNGHEZZA prevista'
//verifica indirizzo di email
var isemail=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/
var m_isemail='Non è un INDIRIZZO DI E-MAIL'
//var isemail=/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/
//tutti numeri con decimali e segno
var issigneddecimal=/^[-+]?\d*\,?\d*$/
var m_issigneddecima='Non è composto da NUMERI,DECIMALI,SEGNO'
//intero con segno
var issignedinteger=/^[-+]?\d*$/
var m_issignedinteger='Non è composto da NUMERI INTERI e SEGNO'
//decimale senza segno
var isdecimal='isdecimal'
var m_isdecimal='Non è composto da NUMERI e DECIMALI'
//decimale massimo 0,2 decimali
var ismax2decimal='ismax2decimal'
var m_ismax2decimal='Non è composto da NUMERI,Massimo 2 DECIMALI,SEGNO'
//un indirizzo http
//var isaddress=/^[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}$/
var isaddress=/^(www+(.[\w]+\.[\w]+))|([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])$/
var m_isaddress='Non è un INDIRIZZO HTTP'
//stringa vuota
var isempty=/^.{0}$/
var m_isempty='Non è stato INSERITO'
//stringa piena
var isnotempty=/^.{1,}/
var m_isnotempty='Non è stato INSERITO'
var isdate='isdate'
var m_isdate='Non è una DATA'
//time ora e minuti
var istime=/^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/
var m_istime='Non è composto da ORE e MINUTI'
//anno
var isyyyy=/^[1-2]\d{3}$/
var m_isyyyy='Non è un ANNO valido'
//numero di telefono
var isntel=/^(\(?\+?[0-9]*\)?)?[0-9_\- \(/)]*$/
var m_isntel='Non è un NUMERO DI TELEFONO valido'
//numero decimale senza segno
var ismoney=/^\d*[,.]?\d*$/
var m_ismoney='Non un IMPORTO'
var iseuro = 'iseuro'
var m_iseuro='Non è un IMPORTO valido'
var ismax4decimal='ismax4decimal'
var iseuro9 = 'iseuro9'
var m_iseuro9='Non è un IMPORTO valido'
var iseuro10 = 'iseuro10'
var m_iseuro10='Non è un IMPORTO valido'

var m_ismax4decimal='Non è composto da NUMERI, massimo 4 DECIMALI'
var ismax6int4decimal = 'ismax6int4decimal'
var m_ismax6int4decimal = 'Non è composto da 6 INTERI e 4 DECIMALI'
var iseuronegativo = 'iseuronegativo'
var m_iseuronegativo='Non è un IMPORTO valido'
var iseuronegativo9 = 'iseuronegativo9'
var m_iseuronegativo9='Non è un IMPORTO valido'
var iseuronegativo10 = 'iseuronegativo10'
var m_iseuronegativo10='Non è un IMPORTO valido'

//var reg = new RegExp("^((\\d{1,6})|(\\d{1,6}\\.\\d{1,4}))$");
var isorario = 'isorario'
var m_isorario='Non è un VALORE valido'

// 7/06/2005 - CONTROLLO CHE IL NOME DEL FILE SIA COMPOSTO SOLO DA LATTERE, NUMERI o _
var isnomefile=/^[a-zA-Z0-9\_]+$/
var m_isnomefile='NON è valido, evitare di inserire spazi o caratteri speciali'

// 7/06/2005 - CONTROLLO CHE IL FILE SIA EXCEL
var isexcel=/([a-zA-Z]:(\\w+)*\\[a-zA-Z0_9]+)?.xls/
var m_isexcel='NON contiene il path di un file di tipo EXCEL'


function ConfElimina() {
	var msg=confirm("La scheda sarà eliminata  dall\'archivio, confermi?");
	if (msg==true) {
		return true;
	}
	else {
		return false;
	}
}

function ifnotok(mytest, myvalue, myerrorname) {
	var ok
	var myerrorstring=''
	var re;

	re = /\./g;

	switch(mytest)
	{

		case 'isdate':
			ok = checkdate(myvalue)
			break;	
		
		case 'isdecimal':
			myregexpr = /^\d*\.?\d*$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;
			
		case 'isnumeric':
			myregexpr = /^\d+$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;
			
		case 'ismax2decimal':
			myregexpr = /^\d*\,?\d{0,2}$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)		
			break;
		
		case 'iseuro':
			myregexpr = /^((\d{1,8})|(\d{1,8}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;

		case 'iseuro9':
			myregexpr = /^((\d{1,9})|(\d{1,9}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;

		case 'iseuro10':
			myregexpr = /^((\d{1,10})|(\d{1,10}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;

			
		case 'isorario':
			myregexpr = /^((\d{1,8})|(\d{1,8}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;
// Modifica 11-11-2003 Bartolucci
//
		case 'iseuronegativo':
			myregexpr = /^[-+]?((\d{1,8})|(\d{1,8}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;	
// Modifica 09-08-2007 Bartolucci
//
		case 'iseuronegativo9':
			myregexpr = /^[-+]?((\d{1,9})|(\d{1,9}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;				

		case 'iseuronegativo10':
			myregexpr = /^[-+]?((\d{1,10})|(\d{1,10}\,\d{1,2}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;				
			
// Fine Modifica 11-11-2003 Bartolucci	
		case 'ismax4decimal':		
			myregexpr = /^\d*\,?\d{0,4}$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;
		case 'ismax6int4decimal':
			myregexpr = /^((\d{1,6})|(\d{1,6}\,\d{1,4}))$/
			myvalue= myvalue.replace(re,'')	
			ok = myregexpr.test(myvalue)
			break;
		default:
			myregexpr = mytest
   			ok = myregexpr.test(myvalue)		
			break;	
	}	

	if (ok==false)
	{
		myerrorstring = myerrorname + '\r'
	}
	
	return myerrorstring  
}


function commitmessage(myheader,myerrorstring,myfooter) {
	if (myerrorstring == ''){
		return true
	}
	else {
	    alert (myheader + myerrorstring + myfooter)
		return false
	}
}


function checkdate(myvalue) {
   var str = myvalue
   var format = 0;
   var month = 0;
   var day = 0;
   var year = 0;
   var err = false;
   if(str == "") {
      return false;
   }
   if(str.length<6  || str.length>10){
      err=true;
   }  
   if(str.length>=6  && str.length==10){
      // dd/mm/yyyy format
      if(str.charAt(2) == "/" && str.charAt(5) == "/" && str.length == 10){
         format=1
         // Check that month is a number.
         for (var i = 3; i < 5; i++) { 
              var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that day is a number.
         for (var i = 0; i < 2; i++){ 
              var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 6; i < 10; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){ 
            month=eval(str.substring(3,5)); day=eval(str.substring(0,2)); year=eval(str.substring(6,str.length)); 
         }
      }
    }
 if(str.length>=6  && str.length==8){
      // dd/mm/yy format
      if(str.charAt(2) == "/" && str.charAt(5) == "/" && str.length == 8){
         format=2
         // Check that month is a number.
         for (var i = 3; i < 5; i++){
              var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that day is a number.
         for (var i = 0; i < 2; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 6; i < 8; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){
            month=eval(str.substring(3,5)); day=eval(str.substring(0,2)); year=eval(str.substring(6,str.length)) 
         }
      }
    }
         // d/mm/yyyy format
   
      if(str.charAt(1) == "/" && str.charAt(4) == "/" && str.length == 9){
         format=3;
         // Check that day is a number.
         for (var i = 0; i < 1; i++) {
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that month is a number.
         for (var i = 2; i < 4; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 5; i < 9; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){
            day=eval(str.substring(0,1)); month=eval(str.substring(2,4)); year=eval(str.substring(5,str.length)); 
         }
       }
     
	   // d/mm/yy format
      if(str.charAt(1) == "/" && str.charAt(4) == "/" && str.length == 7){
         format=4;
         // Check that day is a number.
         for (var i = 0; i < 1; i++){ 
             var ch = str.substring(i, i + 1) 
             if(ch < "0" || "9" < ch) {
             							err=true
             }            
         }
         // Check that month is a number.
         for (var i = 2; i < 4; i++){ 
            var ch = str.substring(i, i + 1) 
            if(ch < "0" || "9" < ch) {
            				err=true
            }				 
         }
         
         // Check that year is a number.
         for (var i = 5; i < 7; i++){ 
              var ch = str.substring(i, i + 1)
             if(ch < "0" || "9" < ch) {
             							err=true
             }							 
         }
         // Set variables
         if(err==false){
             day=eval(str.substring(0,1)); month=eval(str.substring(2,4)); year=eval(str.substring(5,str.length));
         }
      } 
    // ++++++++++++++++++++++++
      // dd/m/yyyy format
      if(str.charAt(2) == "/" && str.charAt(4) == "/" && str.length ==9){
           format=5;
         // Check that day is a number.
         for (var i = 0; i < 2; i++){ 
            var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that month is a number.
         for (var i = 3; i < 4; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 5; i < 9; i++){ 
            var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){
            day=eval(str.substring(0,2)); month=eval(str.substring(3,4)); year=eval(str.substring(5,str.length)); 
         }
      }

      // dd/m/yy format
      if(str.charAt(2) == "/" && str.charAt(4) == "/" && str.length == 7)
         {
         format=6;
         // Check that day is a number.
         for (var i = 0; i < 2; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true 
         }
         // Check that month is a number.
         for (var i = 3; i < 4; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 5; i < 7; i++){ 
              var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){ 
            day=eval(str.substring(0,2)); month=eval(str.substring(3,4)); year=eval(str.substring(5,str.length))
         }
      }   
      // d/m/yyyy format
      if(str.charAt(1) == "/" && str.charAt(3) == "/" && str.length == 8){
         format=7;
         // Check that month is a number.
         for (var i = 0; i < 1; i++){ 
              var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that day is a number.
         for (var i = 2; i < 3; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 4; i < 8; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){ 
            day=eval(str.substring(0,1)); month=eval(str.substring(2,3)); year=eval(str.substring(4,str.length)); 
         }
      }

      // d/m/yy format
      if(str.charAt(1) == "/" && str.charAt(3) == "/" && str.length == 6){
         format=8;
         // Check that month is a number.
         for (var i = 0; i < 1; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that day is a number.
         for (var i = 2; i < 3; i++){ 
             var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Check that year is a number.
         for (var i = 4; i < 6; i++) { 
              var ch = str.substring(i, i + 1); if(ch < "0" || "9" < ch) err=true; 
         }
         // Set variables
         if(err==false){ 
            day=eval(str.substring(0,1)); month=eval(str.substring(2,3)); year=eval(str.substring(4,str.length)); 
         }
      }
	 	
      // Incorrect format.
      if(format <1 || format >8){
         var err=true;
      }
   
   // Check that month is between 1 &12.
   if(month<=0 || month>=13) {
      err=true;
   }   

  // Check that day is right depending on month.
  // February has 29 days in any year evenly divisible by four,
  // EXCEPT for centurial years which are not also divisible by 400.
   var febday=(((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 )
   if( month==2 && (day<=0 || day>febday)) {
      err=true; 
   }
   if( month==4 || month==6 || month==9 || month==11 ){ 
       if(day<=0 || day>30) {
          err=true; 
       }
   }    
   if( month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12 ){
      if(day<=0 || day>31) {
         err=true
      }
   }
   
   // If anything appears incorrect, display error message.
   
   if(err==true) {
      return false;
   }

   // If all is OK, return true.
   
   return true;  
}  
  /******************************************************
   ******************************************************
   ******************************************************
         Esegue  funzione generale di controllo
   ******************************************************
   *****************************************************
   ******************************************************/ 

function autocheckform(myform,flaginif,inituserfunction,flagendf,enduserfunction){
header='l\'operazione non può essere completata per i seguenti errori di digitazione:\r\r'
displayerrors=''
footer=''
var te=myform.elements.length
var i
var x
var y
var errcaption
var errdetail
var noselected
var savelendisplay
        /******************************************************
         Esegue eventuale funzione di inizio controllo
        ******************************************************/
	if (inituserfunction!=""){
  		displayerrors=displayerrors + eval(inituserfunction)
        if ((flaginif==false) && (displayerrors!='')) {
			return commitmessage (header,displayerrors,footer)		
    	}  		
  	}
	for (i=0;i<te;i++){
    	  var thiselement=myform.elements[i]
    	  errcaption=''
       	  errcaption=thiselement.name
       	  if (thiselement.caption){
       		  errcaption=thiselement.caption
  		  }
    	  /******************************************
    	  campi testo (non hidden)
    	  ********************************************/
          if ((thiselement.type=='text') || (thiselement.type=='password') || (thiselement.type=='textarea')  || (thiselement.type=='file')) {
          	   
               if ((thiselement.skip=='no') || (thiselement.value!='')){
       				displayerrors=displayerrors + ifnotok(isnotempty, thiselement.value,'il campo ' + errcaption + '  non è stato inserito') 
       		   }
       		   if ((thiselement.value!='') && (thiselement.validif)) {
       		    	errdetail='m_' + thiselement.validif
       				savelendisplay=displayerrors.length
       				displayerrors=displayerrors + ifnotok(eval(thiselement.validif), thiselement.value,'il campo ' + errcaption + ' ' + eval(errdetail)) 
       		   		if(thiselement.error){
       		   		   if (savelendisplay != displayerrors.length){
       		   				thiselement.errorcolor="yes"
       		   		   }
       		   		   else {
       		   		   		thiselement.errorcolor="no"
       		   		   }

       		   	    }
       		   }    	
       	   }
       	   /******************************************
    	  combo e liste
    	  ********************************************/
   		   if (thiselement.type=='select-one')  {
    				if (thiselement.skip=='no') {
    				    noselected=-1
    				    /* combo o lista ??? */
    				    if (thiselement.size==1){
    				       noselected=0
    				    }
    					if (thiselement.selectedIndex==noselected){
    					    savelendisplay=displayerrors.length
    						displayerrors=displayerrors + 'Nella lista ' + errcaption + ' non è stato selezionato NESSUN ELEMENTO\r'
    						if (savelendisplay != displayerrors.length){
         		   					thiselement.error="yes"
       		   		   		}
       		   		   		else {
       		   		   				thiselement.error="no"
       		   		   		}
    					}
    				}
    		} 
	  /******************************************
    	  radio
    	  ********************************************/
   		   if (thiselement.type=='radio')  {
    				if (thiselement.skip=='no') {
    				
                                    
    				    if (thiselement.checked==false){
    					    savelendisplay=displayerrors.length
    					    displayerrors=displayerrors + 'Non è stata selezionata NESSUNA OPZIONE relativa a ' + errcaption + '\r'
					    if (savelendisplay != displayerrors.length){
         		   			thiselement.error="yes"
       		   		   	     }
       		   		   	     else {
       		   		   		thiselement.error="no"
       		   		   	     }
    				    }
				}
    		    }
    		
 	}
 	if (enduserfunction!=""){
		 if (flagendf==true){
  			displayerrors = displayerrors + eval(enduserfunction)
		 }
  	}
	return commitmessage (header,displayerrors,footer)
}

function chkpulisci(mytext1, mytext2, mychk) {
	if (eval(mychk).checked == true)
		eval(mytext2).value=""
}

function ReverseChk(mychk) {
	if (eval(mychk).checked == true) {
		eval(mychk).checked = false
	}
	
}
     				