/*
 *	checks the validity of an email address entered | returns true or false
 */
function validateEmail(email) {
	// a very simple email validation checking | you can add more complex email checking if it helps 
	var splitted=email.match("^(.+)@(.+)$");
	if(splitted==null) return false;
	if(splitted[1]!=null ) {
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user)==null) return false;
	}
	if(splitted[2] != null) {
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain)==null) {
			var regexp_ip=/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip)==null) return false;
		 }
		 return true;
	}
	return false;
}

/*
 *	function validateData
 *	Checks each field in a form
 *	Called from validateForm function
 */ 
function validateData(strValidateStr,objValue,strError) { 
	var epos=strValidateStr.search("=");
	var command="";
	var cmdvalue="";
	if(epos>=0) {
		command=strValidateStr.substring(0,epos);
		cmdvalue=strValidateStr.substr(epos+1);
	} else {
		command=strValidateStr;
	}
	switch(command) { 
		case "req":
		case "required":
			if(eval(objValue.value.length)==0){
				if(!strError||strError.length ==0){
					strError=objValue.name+" : Campo richiesto";
				}
				alert(strError);
				return false;
			}
			break;
		case "maxlength":
		case "maxlen":
			if(eval(objValue.value.length)>eval(cmdvalue)){
				if(!strError||strError.length ==0){
					strError=objValue.name+" : "+cmdvalue+" caratteri al massimo ";
				}
				alert(strError); /*+"\n[Current length="+objValue.value.length+" ]"*/
				return false;
			}
			break; 
		case "minlength": 
		case "minlen":
			if(eval(objValue.value.length)< eval(cmdvalue)){
				if(!strError||strError.length ==0){
					strError=objValue.name+" : Almeno "+cmdvalue+" caratteri";
				}
				alert(strError); /*+"\n[Current length="+objValue.value.length+" ]"*/
				return false;
			}
			break; 
		case "alnum":
		case "alphanumeric":
			var charpos=objValue.value.search("[^A-Za-z0-9]");
			if(objValue.value.length>0 &&  charpos >= 0){
				if(!strError||strError.length ==0){
					strError=objValue.name+": sono permessi solo caratteri alfanumerici ";
				}
				alert(strError+"\n[Carattere "+eval(charpos+1)+" errato]");
				return false;
			}
			break;
		case "num":
		case "numeric":
			var charpos=objValue.value.search("[^0-9]");
			if(objValue.value.length>0 &&  charpos >= 0){
				if(!strError||strError.length ==0){
					strError=objValue.name+": sono permessi solo numeri ";
				}
				alert(strError+"\n[Carattere "+eval(charpos+1)+" errato]");
				return false;
			}
			break;
		case "alphabetic":
		case "alpha":
			var charpos=objValue.value.search("[^A-Za-z]");
			if(objValue.value.length>0 &&  charpos >= 0){
				if(!strError||strError.length ==0){
					strError=objValue.name+": sono permessi solo caratteri alfabetici ";
				}
				alert(strError+"\n[Carattere "+eval(charpos+1)+" errato]");
				return false;
			}
			break;
		case "alnumhyphen":
			var charpos=objValue.value.search("[^A-Za-z0-9\-_]");
			if(objValue.value.length>0 &&  charpos >= 0){
				if(!strError||strError.length ==0){
					strError=objValue.name+": caratteri permessi: A-Z,a-z,0-9,- e _";
				}
				alert(strError+"\n[Carattere "+eval(charpos+1)+" errato]");
				return false;
			}
			break;
		case "email":
			if(!validateEmail(objValue.value)){
				if(!strError||strError.length ==0){
					strError=objValue.name+": indirizzo email non valido.";
				}
				alert(strError);
				return false;
			}
			break; 
		case "lt":
		case "lessthan":
			if(isNaN(objValue.value)){
				alert(objValue.name+": valore non numerico ");
				return false;
			}
			if(eval(objValue.value) >=  eval(cmdvalue)){
				if(!strError||strError.length ==0){
					strError=objValue.name+" : il valore dev'essere minore di "+ cmdvalue;
				}
				alert(strError);
				return false;
			}
			break;
		case "gt": 
		case "greaterthan":
			if(isNaN(objValue.value)){
				alert(objValue.name+": valore non numerico ");
				return false;
			}
			if(eval(objValue.value) <=  eval(cmdvalue)){
				if(!strError||strError.length ==0){
					strError=objValue.name+" : il valore dev'essere maggiore di "+ cmdvalue;
				}
				alert(strError);
				return false;
			}
			break;
		case "regexp":
			if(!objValue.value.match(cmdvalue)){
				if(!strError||strError.length==0){
					strError=objValue.name+": carattere non valido ";
				}
				alert(strError);
				return false;
			}
			break;
		case "dontselect":
			if(objValue.selectedIndex == null){
				alert("BUG: dontselect command for non-select Item");
				return false;
			}
			if(objValue.selectedIndex == eval(cmdvalue)){
				if(!strError||strError.length ==0){
					strError=objValue.name+": selezionare un'opzione tra quelle disponibili ";
				}
				alert(strError);
				return false;
			}
			break;
		case "isdate":
			if(objValue.value.length>0){
				if(isNaN(Date.parse(objValue.value))){
					if(!strError||strError.length ==0){
						strError=objValue.name+" : il valore dev'essere una data";
					}
					alert(strError);
					return false;
				}
			}
			break;
	}
	return true;
}

/* 
 * function validateForm 
 * the function that can be used to validate any form 
 * returns false if the validation fails; true if success 
 * arguments : 
 *   objFrm	 : the form object 
 *   arrObjDesc : an array of objects describing the validations to conduct on each input item. 
 *		  The array should consist of one object per input item in the order the input 
 *		  elements are present in the form. Each object consist of zero or more validation 
 *		  objects. Each of these validation object is a pair consisting of the validation 
 *		  descriptor string and an optional Error message. 
 */ 

function validateForm(objFrm,arrObjDesc){
	for(var itrobj=0; itrobj<arrObjDesc.length; itrobj++){
		if(objFrm.elements.length <= itrobj){
			alert("Errore: accesso ad un campo non esistente.\nContattare il webmaster!");
			return false;
		}
		for(var itrdesc=0; itrdesc<arrObjDesc[itrobj].length ;itrdesc++){
			if(validateData(arrObjDesc[itrobj][itrdesc][0],objFrm[itrobj],arrObjDesc[itrobj][itrdesc][1]) == false){
				objFrm[itrobj].focus();
				return false;
			}
		}
	}
	return true;
}

/*
 *	>>>	La funzione che segue si differenzia dalla precedente perchè 
 *	>>>	consente di specificare quali campi vanno controllati
 *	>>>	e in quale ordine.
 *	Un esempio dell'array che richiede (in questo caso per il login):
 *	var fvLogin01N=[
 *		'Login',[["required","Hai dienticato di inserire il login"],["minlen=6"],["maxlen=12", "Login troppo lungo!"]], // Validazione del login
 *		'Password',[["required","Hai dienticato di inserire la password"],["minlen=5"," La password dev'essere di almeno 5 caratteri"],["maxlen=12"]]  // Validazione per la mail
 *	]; 
 */

function validateFormN(objFrm,arrObjDesc){
	for(var itrobj=0; itrobj<arrObjDesc.length; itrobj+=2){
		if(objFrm.elements.length*2 <= itrobj){
			alert("Errore: accesso ad un campo non esistente.\nContattare il webmaster!");
			return false;
		}
		for(var itrdesc=0; itrdesc<arrObjDesc[itrobj+1].length ;itrdesc++){
			if(objFrm[arrObjDesc[itrobj]].style){ // riporta normali il colore di sfondo
				objFrm[arrObjDesc[itrobj]].style.backgroundColor="";
			}
			if(validateData(arrObjDesc[itrobj+1][itrdesc][0],objFrm[arrObjDesc[itrobj]],arrObjDesc[itrobj+1][itrdesc][1]) == false){
				objFrm[arrObjDesc[itrobj]].focus();
				if(objFrm[arrObjDesc[itrobj]].style){ // Evidenzia il campo che crea problemi
					objFrm[arrObjDesc[itrobj]].style.backgroundColor="#FFcc99";
				}
				return false;
			}
		}
	}
	return true;
}

/*
 *	Abilita e disabilita elementi di un form
 *	Gli elementi da abilitare e disabilitare sono contenuti in un array del tipo
 *	var elements = ['NomeElemento1','NomeElemento2', ... ];
 *	onOff indica se gli elementi vanno attivati o disattivati.
 */
function enableDisable(objFrm,arrElements,onOff){
	for(var i=0; i<arrElements.length; i++){
		if(objFrm[arrElements[i]].length){
			for(var j=0; j<objFrm[arrElements[i]].length;j++){
				if(onOff) objFrm[arrElements[i]][j].disabled=false
				else objFrm[arrElements[i]][j].disabled=true;
			}
		} else {
			if(onOff) objFrm[arrElements[i]].disabled=false
			else objFrm[arrElements[i]].disabled=true;			
		}
	}
} 