

// VERIFICA NOME,COGNOME,CITTA
function isName(what,fname)
{
   var i=new RegExp("([a-zA-Z\ ]{2,})");
   if(!i.test(what)) { alert(fname); return false}
   return true;
}

// VERIFICA IMMISSIONE ALFANUMERICA
function isAlfnm(what,fname)
{
   var i=new RegExp("([a-zA-Z0-9\ ]{1,})");
   if(!i.test(what)) { alert("Il campo "+fname+" non è valido."); return false}
   return true;
}

// VERIFICA CONFRONTO VALORI (TROPPO ALTO)
function troppoAlto(c1,c2,messaggio)

{
if (eval(c1)>eval(c2))
//if (c1>c2)
	{

    alert (messaggio); return false}
}

// VERIFICA NUMERI(???)
function isNum(what,fname)
{
   var i=new RegExp("([0-9,.]{1,})");
   if(!i.test(what)) { alert("Il campo "+fname+" non è valido."); return false}
   return true;
}


// VERIFICA NUMERI(???)
function isTel(what,fname)
{
   var i=new RegExp("([0-9,.]{10,25})");
   if(!i.test(what)) { alert("Il campo "+fname+" non è valido."); return false}
   return true;
}


// VERIFICA EMAIL
function isEmail(what,messaggio)
{
   var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
   if(!i.test(what)) { alert(messaggio); return false}
   return true;
}

// VERIFICA EMAIL CON DESCRIZIONE DELL'ERRORE
function isEmailDes(what,des)
{
   if (what == "" || what== null )
   return true;
   else
   var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
   if(!i.test(what)) { alert(des); return false}
   return true;
}
// VERIFICA EMAIL CON DESCRIZIONE DELL'ERRORE
function isEmailDes2(what,des2)
{
   if (what == "" || what== null )
   return true;
   else
   var i=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
   if(!i.test(what)) { alert(des2); return false}
   return true;
}


// VERIFICA PASSWORD
function isPassword(what,fname)
{
   var i=new RegExp("([a-zA-Z0-9]{7,20})");
   if(!i.test(what)) { alert("Il campo "+fname+" non è valido."); return false}
   return true;
}

// VERIFICA URL
function isURL(what)
{
   var i=new RegExp("HTTP:\/\/([a-zA-Z\.0-9]{2,})\.([a-zA-Z\.0-9]{2,})");
   if(!i.test(what)) { alert("Il campo SITO WEB non è valido."); return false}
   return true;
}

// VERIFICA ZERO
function isZero(valore,nome)
{
	if (valore <= 0 || valore== null || valore== "0")
	{

    alert ("Campo "+ nome + " è non può essere "+ valore +" !"); return false}
}


// VERIFICA TEL/FAX
function isTelFax(what,fname)
{
   var i=new RegExp("([0-9]{2,})\/([0-9]{5,})");
   if(!i.test(what)) { alert("Il campo "+fname+" non è valido."); return false}
   return true;
}

//controllo obbligatorio
function vuoto(valore,nome)
{

	if (valore == "" || valore== null )
	{

    alert ("Campo "+ nome + " è obbligatorio"); return false}
}
  // vuoto


// VERIFICA Partita IVA
function isPIVA(what)
{
   if(what.length>0)
   {
  var i=new RegExp("[0-9]{11}");
  if(!i.test(what)) { alert("Il campo PARTITA IVA non è valido."); }
   }
   return true;
}

// VERIFICA Partita IVA 1
function ControllaPIVA(pi)
{
  if( pi == '' )  return '';
  if( pi.length != 11 )
    return "La lunghezza della partita IVA non è\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 è valida:\n" +
      "il codice di controllo non corrisponde.\n";
  return '';
}

// VERIFICA CAP
function isCAP(what)
{
   var i=new RegExp("^[0-9]{5}$");
   if(!i.test(what)) { alert("Il campo CAP non è corretto."); return false }
   return true;
}

// VERIFICA CF
function isCF(what)
{
   var i=new RegExp("([a-zA-Z0-9]{16})");
   if(!i.test(what)) { alert("Il campo CODICE FISCALE non è valido."); return false }
   return true;
}




/*****************************************
	Controllo del CODICE FISCALE
 fonte http://digilander.iol.it/salsi/cf-pi/
******************************************/



function ControllaCF(cf)
{
	var validi, i, s, set1, set2, setpari, setdisp;
	if( cf == '' )  return '';
	cf = cf.toUpperCase();
	if( cf.length != 16 )
		return "La lunghezza del codice fiscale non è\n"
		+"corretta: il codice fiscale dovrebbe essere lungo\n"
		+"esattamente 16 caratteri.\n";
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for( i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 )
			return "Il codice fiscale contiene un carattere non valido `" +
				cf.charAt(i) +
				"'.\nI caratteri validi sono le lettere e le cifre.\n";
	}
	set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	s = 0;
	for( i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for( i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return "Il codice fiscale non è corretto:\n"+
			"il codice di controllo non corrisponde.\n";
	return "";
}




/*****************************************
	Controllo della Partita I.V.A.

  fonte http://digilander.iol.it/salsi/cf-pi/
 ******************************************/

function ControllaPIVA(pi)
{
	if( pi == '' )  return '';
	if( pi.length != 11 )
		return "La lunghezza della partita IVA non è\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 è valida:\n" +
			"il codice di controllo non corrisponde.\n";
	return '';
}







// VERIFICA Indirizzo
function isAddress(what)
{
   var i=new RegExp("([a-zA-Z0-9\ ]{3,})");
   if(!i.test(what)) { alert("Il campo INDIRIZZO non è valido."); return false }
   return true;
}

// Controllo campo data.
function isDate(str,fname)

   {
   // Ritorna falso se i caratteri non sono '0-9' o '/' . 
   for (var i = 0; i < str.length; i++) 
      {
      var ch = str.substring(i, i + 1);
      if ((ch < "0" || "9" < ch) && ch != '../index.html') 
	           {
			   	   alert(fname);
         return false;
         }
      }
   // Controllo lunghezza campo valore ammesso gg/mm/yyyy.
   if (str.length != 10) 
   	   {
	   alert(fname);
         return false;
         }
   // Controllo se la data e' valida.
   giorno = str.substring(0, 2) 
   mese = str.substring(3, 5) 
   anno = str.substring(6, 10)
   bar1 = str.substring(2, 3)
   bar2 = str.substring(5, 6)
   if (giorno<1 || giorno>31)
   	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
         return false;
         }
   if (mese<1 || mese>12)
   	   {
	   	   	   alert("Nel campo "+fname+", "+mese+" non è corretto come mese.");
         return false;
         }
   if (anno<1900 || anno>2100)
   	   {
	   alert("Nel campo "+fname+", "+anno+" non è un dato corretto.");
         return false;
         }
   if (bar1 != '../index.html')
   	   {
	   	   alert("Il campo "+fname+" non è corretto (controllare spaziatura).");
         return false;
         }
   if (bar2 != '../index.html' )
   	   {
	   	   alert("Il campo "+fname+" non è corretto (controllare spaziatura).");
         return false;
         }
   // Controllo mesi con 31 giorni.  
   if (mese==4 || mese==6 || mese==9 || mese==11)
         {
		          if (giorno==31) 
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
			  return false;
              }
         }
   // Controllo se anno bisestile e se e' febbraio.  
   if (mese==2)
         {
         var bis=parseInt(anno/4)
         if (isNaN(bis))
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
              return false;
              }
         if (giorno>29)
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
              return false;
              }
         if (giorno==29 && ((anno/4)!=parseInt(anno/4)))
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
              return false;
              }
			
         }              
   return true;
   }

// Controllo campo data.
function isDatePoints(str,fname)

   {
   // Ritorna falso se i caratteri non sono '0-9' o '/' . 
   for (var i = 0; i < str.length; i++) 
      {
      var ch = str.substring(i, i + 1);
      if ((ch < "0" || "9" < ch) && ch != '.') 
	           {
			   	   alert(fname);
         return false;
         }
      }
   // Controllo lunghezza campo valore ammesso gg/mm/yyyy.
   if (str.length != 10) 
   	   {
	   alert(fname);
         return false;
         }
   // Controllo se la data e' valida.
   giorno = str.substring(0, 2) 
   mese = str.substring(3, 5) 
   anno = str.substring(6, 10)
   bar1 = str.substring(2, 3)
   bar2 = str.substring(5, 6)
   if (giorno<1 || giorno>31)
   	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
         return false;
         }
   if (mese<1 || mese>12)
   	   {
	   	   	   alert("Nel campo "+fname+", "+mese+" non è corretto come mese.");
         return false;
         }
   if (anno<1900 || anno>2100)
   	   {
	   alert("Nel campo "+fname+", "+anno+" non è un dato corretto.");
         return false;
         }
   if (bar1 != '.')
   	   {
	   	   alert("Il campo "+fname+" non è corretto (controllare spaziatura che deve essere nella forma gg.mm.aaaa).");
         return false;
         }
   if (bar2 != '.' )
   	   {
	   	   alert("Il campo "+fname+" non è corretto (controllare spaziatura che deve essere nella forma gg.mm.aaaa).");
         return false;
         }
   // Controllo mesi con 31 giorni.  
   if (mese==4 || mese==6 || mese==9 || mese==11)
         {
		          if (giorno==31) 
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
			  return false;
              }
         }
   // Controllo se anno bisestile e se e' febbraio.  
   if (mese==2)
         {
         var bis=parseInt(anno/4)
         if (isNaN(bis))
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
              return false;
              }
         if (giorno>29)
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
              return false;
              }
         if (giorno==29 && ((anno/4)!=parseInt(anno/4)))
		 	   {
	   	   alert("Nel campo "+fname+", "+giorno+" non è corretto come giorno del mese.");
              return false;
              }
			
         }              
   return true;
   }


function isDate2(what,fname)
{
        var i=new RegExp(/^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/);
   if(!i.test(what)) { alert("Il campo "+fname+" non è valido."); return false}
   return true;
}


// VERIFICA Titolare carta di credito
function isCCOwner(what)
{
   var i=new RegExp("([a-zA-Z\. ])");
   if(!i.test(what)) { alert("Il campo TITOLARE non è valido."); return false }
   return true;
}

// VERIFICA Numero carta di credito
function isCCNumber(what)
{
   var i=new RegExp("([0-9]{16,20})");
   if(!i.test(what)) { alert("Il campo NUMERO CARTA non è valido."); return false }
   return true;
}

// VERIFICA Scadenza carta di credito
function isCCExpiration(what)
{
   var i=new RegExp("([0-9]{1,2})/([0-9]{4})$");
   if(i.test(what))
   {
  datePart=what.split("../index.html");
  mese=(datePart[0]>=1) && (datePart[0]<=12);
  anno=(datePart[1]>=1900) && (datePart[1]<=9999);

  if(!mese || !anno) { alert("Il campo SCADENZA CARTA non è valido."); return false }

   }
   else
   {
  alert("Il campo SCADENZA CARTA non è valido"); return false
   }
   return true;
}

function confermi(frm)
 {
			 aa=1
        if (aa=2)
        {
                alert("ERRORE")
        }
        else  {
                var co=window.confirm("Confermi tutti i dati ?")
                if (co==true)
                {
                frm.submit()
                }
              }
 }


//VERIFICA SE DEGLI OGGETTI RADIO SONO SELEZIONATI OPPURE NO

function radiosel (descrizione,collezione,elementi)
{
	chk=0
	for ( i = 0 ; i < elementi ; i++ )
	{
	   		if (collezione[i].checked==true)
		 		{
		 				chk=1
				}

	}

 	if ( chk==0 ) {
 		alert ("Occorre selezionare un elemento nella riga "+descrizione)
		return false;
 	}


}



function combopiena (valore,what)
{
	//alert (valore)
	if ( valore=="0" )
	{
		alert ("Occorre selezionare il campo "+ what)
		return false;
	}
}

<!--
/*
 * La funzione "conta()", prende in input 2 parametri:
 * l'oggetto html chiamante, in questo caso la textarea, e
 * il valore massimo. Viene chiamata da tre eventi nel tag textarea
 * vedi il codice html sottostante
 */
function conta(oggetto,i)
  {
  /*
   * Ho necessità di importarmi l'oggetto nella funzione,
   * e non solamente il valore, perchè poi devo cancellare
   * sulla textarea i caratteri in più.
   */
  var testo=oggetto.value;
  if(testo.length>i)
    {
    oggetto.value=testo.substr(0,i);
    /*
     * Sovrascrivo il testo nella textarea, fino ad
     * un massimo di "i" caratteri.
     */
    }
  }




function vis(nome)
{

  if (document.all.item(nome).style.display=="none")
  {
  document.all.item(nome).style.display="block"
  }
  else
  {
  document.all.item(nome).style.display="none"
  }
}



//DATA

function checkMyDate(_form) {
    var _value = _form.myDate.value;
    var _test = Date.parse(_value)

    if (isNaN(_test)) {
        _form.myDate.value = "";
        alert("Not a valid date!");
        _form.myDate.focus();
    }
    else {
        nDate = new Date(_test);
        _form.myDate.value = nDate.toUTCString();
    }
}



// CARATTERI MASSIMI PER CAMPI TEXT

function textCounter(field, countfield, maxlimit)
{
    if (field.value.length > maxlimit)
    field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'caratteri rimanenti' counter
    else
    countfield.value = maxlimit - field.value.length;
}



  function normale()
  {
		document.onkeydown=niente
  }

  function niente()
  {

  }



function dataM(data,dataf,msg)
{
		var dy=data.substr(0,2)
		var mo=data.substr(3,2)
		var yr=data.substr(6,4)

		var dyf=dataf.substr(0,2)
		var mof=dataf.substr(3,2)
		var yrf=dataf.substr(6,4)

		var SECOND = 1000;
		var MINUTE = SECOND * 60;
		var HOUR = MINUTE * 60;
		var DAY = HOUR * 24;
		var WEEK = DAY * 7;

		  var nTime= Date.UTC(yrf, mof, dyf);

		  var dTime = Date.UTC(yr, mo , dy);
		  var bTime = nTime - dTime
		  var gg=Math.round(bTime / DAY)
			sign = (gg == (gg = Math.abs(gg)));
			if ( sign==false )
			 {
				alert (msg)
				return false;
			}
			return true
}
