function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
    	// Check that current character is number.
    	var c = s.charAt(i);
    	if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

// Fa la validazione dell'indirizzo di E-Mail
function ckEmailAddr(eAddr){
// Ritorna true se ok, false se non ok
	var DomainValidChars="1234567890abcdefghijklmnopqrstuvwxyz-.",UserValidChars=DomainValidChars+"_";
	var eInvalid=0,atPos,strCk;
	var msgEmpt="L'indirizzo di E-Mail è vuoto.",msgInva="L'indirizzo di E-Mail non è valido o contiene caratteri illegali.";
	if(eAddr==""){
		alert(msgEmpt);
		return false;
	}
	eAddr = eAddr.toLowerCase();
	atPos=eAddr.indexOf("@");
	atPos!=eAddr.lastIndexOf("@") || atPos<1 || atPos>(eAddr.length-5)?eInvalid+=1:null;
	strCk=eAddr.substring(0,atPos);
	for(i=0;i<strCk.length;i++){
		UserValidChars.indexOf(strCk.charAt(i))<0?eInvalid+= 1:null;
	}
	strCk=eAddr.slice(atPos+1);
	for(i=0;i<strCk.length;i++){
		DomainValidChars.indexOf(strCk.charAt(i))<0?eInvalid+=1:null;
	}
	strCk.indexOf(".")<0?eInvalid+=1:null;
	strCk=strCk.charAt(strCk.length-1);
	strCk=="." || strCk=="-"?eInvalid+=1:null;
	if(eInvalid!=0){
		alert(msgInva);
		return false;
	}
	return true;
}

// Fa la validazione del form
function FrmChk(which){
	var pass=true;
	if (document.images){		
		for (ff=0;ff<which.length;ff++){
			var tempobj=which.elements[ff];
			var strToTest=tempobj.name.substring(0,2);
			switch (strToTest) {
				case "t_": // Il campo da testare è un testo
					if (((tempobj.type=="text"||tempobj.type=="textarea")&&tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&(tempobj.selectedIndex==-1)||tempobj.value=="")){
						alert("Uno o più campi obbligatori mancanti o non validi.")
						return false;
					}
					break;
				case "e_": // Il campo da testare contiene un indirizzo di e-mail
					if (!ckEmailAddr(tempobj.value)) {
						return false;
					}
					break;
				case "d_": // Il campo da testare contiene una data ed è obbligatorio
					if (!isDate(tempobj.value)) {
						return false;
					}
					break;
				case "c_": // Il campo da testare contiene una data ma non è obbligatorio (viene testato solo se non è vuoto)
					if (!tempobj.value=='') {
						if (!isDate(tempobj.value)) {
							return false;
						}
					}
					break;
				case "v_": // Il campo da testare contiene una valuta ed è obbligatorio
					if (!isCurrency(tempobj.value)) {
						return false;
					}
					break;
				case "u_": // Il campo da testare contiene una valuta ma non è obbligatorio (viene testato solo se non è vuoto)
					if (!tempobj.value=='') {
						if (!isCurrency(tempobj.value)) {
							return false;
						}
					}				
				 break;
				case "m_": // Il campo da testare contiene un numero positivo ma NON è obbligatorio
					if (!tempobj.value=='') {
						tempobj.value = tempobj.value.replace(/(^\s*)|(\s*$)/g, "");
						if (!isInteger(tempobj.value) || tempobj.value.length < 1) {
							alert("Uno o più campi obbligatori mancanti o non validi.")
							return false;
						}
					}				
				 break;
				case "n_": // Il campo da testare contiene un numero positivo ed è obbligatorio
					tempobj.value = tempobj.value.replace(/(^\s*)|(\s*$)/g, "");
					if (!isInteger(tempobj.value) || tempobj.value.length < 1) {
						alert("Il numero di telefono non è valido o contiene spazi.")
						return false;
					}
				break;
			}
		}
	}
	return true;
}	
