function campoEmail(emailStr) {
	// Ritorna true se l'argomento &egrave; un indirizzo di email valido. E' valida la sintassi usuale user@domain,
	// ma sono consentiti anche i formati user@[ip] e "User with Spaces"@domain o [ip], (tutte sintassi legali secondo il W3C.)
	// Sono controllati anche errori quali multipli @ o . limitrofi nell'indirizzo (Es: user@a@b.com ed user@a..b.co.uk).
	// Argomenti: emailStr stringa.

	// Formato user@domain e separazione di username e dominio
	var emailPat=/^(.+)@(.+)$/ ;
	// Pattern per ritrovare i caratteri speciali (non consentiti)
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]" ;
	// Caratteri consentiti in username o domainname
	var validChars="\[^\\s" + specialChars + "\]" ;
	// Username contenente spazi
	var quotedUser="(\"[^\"]*\")" ;
	// domainName costituito da un indirizzo IP
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/ ;
	// Unit&agrave; 'atomo' ovvero una serie di caratteri non speciali
	var atom=validChars + '+' ;
	// Una 'word' dell'username. L'username pu&ograve; essere costituito da pi&ugrave; 'word'
	// separate da .
	var word="(" + atom + "|" + quotedUser + ")" ;
	// Struttura dell'username
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$") ;
	// Dominio sombolico
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$") ;

	// Controllo della sintassi username@domain e separazione dell'username dal domain
	var matchArray=emailStr.match(emailPat) ;
	if (matchArray==null) {
		//alert("Email address seems incorrect (check @ and .'s)")
		return false;
	}
	var user=matchArray[1] ;
	var domain=matchArray[2];
	
	// User valido.
	if (user.match(userPat)==null) {
	    //alert("The username doesn't seem to be valid.")
	    return false;
	}
	
	// Indirizzo IP valido (nel caso in cui il domain sia un IP.
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
			return false;
		    }
	    }
	    return true;
	}
	
	// Domain &egrave; un nome simbolico
	var domainArray=domain.match(domainPat);
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false;
	}
	
	
	// controllo sulla parte terminale del domain.
	
	// Spezzo il domain in 'atomi'
	var atomPat=new RegExp(atom,"g");
	var domArr=domain.match(atomPat);
	var len=domArr.length;
	if ((domArr[domArr.length-1].length<2) || 
	    (domArr[domArr.length-1].length>4)) {
	   // alert("The address must end in a three-letter domain, or two letter country.")
	   return false;
	}
	
	// Parte terminale del dominio preceduta da un host name.
	if (len<2) {
	   //var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false;
	}
	
	return true;
}

function campoData(gg,mm,aaaa){ 
	// ritorna true se la data rappresentata dal giorno pari al primo argomento, dal mese pari al secondo e dall'anno pari al terzo &egrave; valida, false altrimenti
	// Argomenti gg,mm,aaaa stringhe rappresentanti interi (aaaa anno in formato esteso a 4 cifre) gg, mm, aaaa sono stringhe tutte di caratteri numerici
	
	if (!campoNumero(gg)) return false;

	if (!campoNumero(mm)) return false;

	if (!campoNumero(aaaa)) return false;

	
	giorno = parseInt(gg,10);
	mese = parseInt(mm,10);
	anno = parseInt(aaaa,10);
	
	if (!nellIntervallo(giorno,1,31)) return false;

	if (!nellIntervallo(mese,1,12)) return false;

	if (!nellIntervallo(anno,1890,2050)) return false;

	
	testDate = new Date(anno,mese-1,giorno);

	
	return ( testDate.getMonth()==(mese-1) && 
			 testDate.getDate()==(giorno)    )
}


function campoStringa(str) {
	// ritorna true se l'argomento &egrave; una stringa non vuota e non costituita da soli spazi Argomenti: str stringa
	if (notNull(str)&& notBlank(str)) 
		return true;
	return false;
}

function notNull(str) {
	// ritorna false se l'argomento ha lunghezza 0, true altrimenti Argomenti: str tipo stringa
	if (str.length == 0 )
		return false;
	else 
		return true;
}

function nellIntervallo(str, num1, num2) {
	// ritorna true se il primo argomento rappresenta un numero compreso tra il secondo (valore minimo) e il terzo (valore massimo) argomento
	// Argomenti: str stringa,  num1,num2 numerici
	var i = parseInt(str,10);
	
	return((i >= num1) && (i <= num2));
}

function notBlank(str) {
	// ritorna false se l'argomento è costituito da soli spazi o ha lunghezza 0, true altrimenti
	// Argomenti: str tipo stringa
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true;
	}
	return false;
}

function campoNumero(str){ 
	// ritorna true se l'argomento &egrave; una stringa costituita solo da caratteri numerici, false altrimenti
	// Argomenti: str stringa
	var myExp=/^[0-9]*$/;
	
	return myExp.test(str);
}

function campoCellulare(str){ 
	// ritorna true se l'argomento &egrave; una stringa costituita solo da caratteri numerici, il segno + e lo spazio, false altrimenti
	// Argomenti: str stringa
	var myExp=/^[0-9 + /]*$/;
	
	return myExp.test(str);
}

function piuLungo(str,maxLength) {
	// ritorna true se il primo argomento è una stringa di lunghezza superiore al valore del secondo argomento, false altrimenti
	// Argomenti: str stringa, maxLength numerico
	if (str.length>maxLength)
		return true;
	return false;
}

function piuCorto(str,minLength) {
	// ritorna true se str è è una stringa più corta di minLength, false altrimenti
	// Argomenti: str stringa, minLength numerico
	if (str.length<minLength)
		return true;
	return false;
}

function isInside(str,minLength,maxLength) {
	// ritorna true se la lunghezza della stringa passata come primo argomento &egrave; compresa tra i valori rappresentati
	// dal secondo (minimo) e il terzo (massimo) argomento,false altrimenti
	// Argomenti: str stringa, minLength,maxLength numerici
	if (!piuCorto(str,minLength) && !piuLungo(str,maxLength))
		return true;
	return false;
}

function campoLettera(str){
	// ritorna true se l'argomento &egrave; una stringa composta unicamente da lettere
	// Argomenti: str stringa
	var myExp=/^[a-z]*$/i;
	
	return myExp.test(str);
}

function isSize(str, size) {
	// ritorna true se il primo argomento è una stringa di lunghezza pari al valore del secondo argomento
	// Argomenti: str tipo stringa, size intero
	if (str.length == size) 
		return true;
	else
		return false;
}

function campoCAP(str) {
	// ritorna true se nella stringa passata per argomento sono contenuti 5 caratteri numerici (es.CAP)
	// Argomenti: str stringa
	if (campoNumero(str) && isSize(str,5))
		{
		   return true;
	        }
	return false;
}

function campoPIVA(str) {
	// ritorna true se nella stringa passata per argomento sono contenuti 11 caratteri numerici (es. P.IVA)
	// Argomenti: str stringa
	if (campoStringa(str)) {
		if (campoNumero(str) && isSize(str,11))
			return true;
		else
			return false;
		}
	else
		return false;
}

function campoCF(str) {
	// ritorna true se nella stringa passata per argomento sono contenuti 16 caratteri alfanumerici (es. Cod.Fiscale)
	// Argomenti: str stringa
	if (campoStringa(str)) 
	{
		if (campoAlfaNumerico(str) && isSize(str,16))
			return true;
		else
			return false;
	}
	else
		return false;
}
// ritorna true se l'argomento è una stringa composta unicamente
// da caratteri alfanumerici
// Argomenti: str stringa
function campoAlfaNumerico(str){
	var myExp=/^[a-z0-9]*$/i;
	
	return myExp.test(str);
}
function campoNumerico(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }