
function campoEmail(emailStr) {
	// 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;
}


// 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)
function campoData(gg,mm,aaaa){ 
	// 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)    )
}


// ritorna true se l'argomento &egrave; una stringa non 
// vuota e non costituita da soli spazi
// Argomenti: str stringa
function campoStringa(str) {
	if (notNull(str)&& notBlank(str)) 
		return true;
	return false;
}

// ritorna false se l'argomento ha lunghezza 0, true altrimenti
// Argomenti: str tipo stringa
function notNull(str) {
	if (str.length == 0 )
		return false;
	else 
		return true;
}

// 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
function nellIntervallo(str, num1, num2) {
	var i = parseInt(str,10);
	
	return((i >= num1) && (i <= num2));
}

// ritorna false se l'argomento è costituito da soli spazi o ha lunghezza 0, true altrimenti
// Argomenti: str tipo stringa
function notBlank(str) {
	for (i = 0; i < str.length; i++) {
		if (str.charAt(i) != " ")
			return true;
	}
	return false;
}

// ritorna true se l'argomento &egrave; una stringa costituita solo da caratteri numerici, false altrimenti
// Argomenti: str stringa
function campoNumero(str){ 
	var myExp=/^[0-9]*$/;
	
	return myExp.test(str);
}

// ritorna true se l'argomento &egrave; una stringa costituita solo da caratteri numerici, il segno + e lo spazio, false altrimenti
// Argomenti: str stringa
function campoCellulare(str){ 
	var myExp=/^[0-9 + /]*$/;
	
	return myExp.test(str);
}

// ritorna true se il primo argomento è una stringa di lunghezza superiore al valore del secondo argomento, false altrimenti
// Argomenti: str stringa, maxLength numerico
function piuLungo(str,maxLength) {
	if (str.length>maxLength)
		return true;
	return false;
}

// ritorna true se str è è una stringa più corta di minLength, false altrimenti
// Argomenti: str stringa, minLength numerico
function piuCorto(str,minLength) {
	if (str.length<minLength)
		return true;
	return false;
}

// 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
function isInside(str,minLength,maxLength) {
	if (!piuCorto(str,minLength) && !piuLungo(str,maxLength))
		return true;
	return false;
}

// ritorna true se l'argomento &egrave; una stringa composta unicamente
// da lettere
// Argomenti: str stringa
function campoLettera(str){
	var myExp=/^[a-z]*$/i;
	
	return myExp.test(str);
}

// 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.




// ritorna true se il primo argomento &egrave; una stringa di 
// lunghezza pari al valore del secondo argomento
// Argomenti: str tipo stringa, size intero
function isSize(str, size) {
	if (str.length == size) 
		return true;
	else
		return false;
}

// ritorna true se nella stringa passata per argomento
// sono contenuti 5 caratteri numerici (es.CAP)
// Argomenti: str stringa
function campoCAP(str) {
	if (campoNumero(str) && isSize(str,5))
		{
		   return true;
	        }
	return false;
}

// ritorna true se nella stringa passata per argomento
// sono contenute 2 lettere (es. Provincia)
// Argomenti: str stringa
function isProv(str) {
	if (notNull(str) && campoLettera(str)) {
		
		if (isSize(str,2)) 
			return true;
	}
	return false;
}

// ritorna true se nella stringa passata per argomento
// sono contenuti 11 caratteri numerici (es. P.IVA)
// Argomenti: str stringa
function isPIVA(str) {
	if (campoNumero(str) && isSize(str,11))
		{
		   return true;
	        }
	return false;
}


// ritorna true se nella stringa passata per argomento
// sono contenuti 16 caratteri alfanumerici (es. Cod.Fiscale)
// Argomenti: str stringa
function isCodFisc(str) {
	if (isAlfanumeric(str) && isSize(str,16))
		{
		   return true;
	        }
	return false;
}

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;
   }
   
