
function eAlfabetico(stng, eccezione){
	if (eccezione==null) eccezione = "";
	for(j=0;j<stng.length;j++){
		cc = stng.charCodeAt(j);
		if((cc < 97 || cc > 122) && (cc < 65 || cc > 90) && (cc != 32))
			if(eccezione.indexOf(stng.charAt(j))==-1) return false;
	}
	return true;
}

function eNumerico(stng, eccezione){
	if (eccezione==null) eccezione = "";
	for(j=0;j<stng.length;j++){
		cc = stng.charCodeAt(j);
		if(cc < 48 || cc > 57)
			if(eccezione.indexOf(stng.charAt(j))==-1) return false;
	}
	return true;
}

function eAlfanumerico(stng, eccezione){
	if (eccezione==null) eccezione = "";
	for(j=0;j<stng.length;j++){
		cc = stng.charCodeAt(j);
		if((cc < 97 || cc > 122) && (cc < 65 || cc > 90) && (cc < 48 || cc > 57) && (cc != 32) && (cc != 39))
			if(eccezione.indexOf(stng.charAt(j))==-1) return false;
	}
	return true;
}

function eData(stng){
	var parsedDate = stng.split ("/");
	if (parsedDate.length != 3) return false;
	var day, month, year;
	month = parsedDate[1]-1;
	day = parsedDate[0];
	year = parsedDate[2];
	var objDate = new Date((month+1) + "/" + day + "/" + year);
	if (day != objDate.getDate()) return false;
	if (month != objDate.getMonth()) return false;
	if (year != objDate.getFullYear()) return false;
	return true;
}

function eMail(stng){
	p=stng.indexOf('@');
	if(p==-1) return false;
	s=stng.substr(0, p-1);
	if(s=='') return false;
	if(!eAlfanumerico(s, '_.')) return false;
	s=stng.substr(p+1);
	if(s=='') return false;
	p=s.indexOf('.');
	if(p<3 || p>s.length-3) return false;
	if(!eAlfanumerico(s, '-.')) return false;
	return true;
}

function verifica(v, t, o,l){
	var len = v.length;
	var msge = "";
	if(t == "o" && len == 0) msge = 'Campo obbligatorio.';
	else{
		if(len > 0){
			if(o == ">" && len <= l) msge = 'Il campo deve essere di almeno ' + (l*1+1) + ' carratteri.';
			if(o == "=" && len != l) msge = 'Il campo deve essere di ' + l + ' carratteri.';
		}
	}
	return msge;
}

function formValidator(thisForm){
	var isError=false;
	var label="";
	var msgError="";
	var pos1, pos2, caratterispeciali;
	for(i=0; i<thisForm.elements.length; i++){
		if(thisForm.elements[i].disabled) continue;
		elemento=thisForm.elements[i].id;
		ctgr=elemento.substr(0,1); //tipo data del campo
		tipo=elemento.substr(1,1); //campo obbligatorio o facoltativo
		oprd=elemento.substr(2,1); //operatore di controllo della lunghezza
		pos1=elemento.indexOf(':'); //caratteri speciali ammessi
		pos2=elemento.indexOf('/l/'); //label per la descrizione errore
		if(pos1==-1){
			lmax=((pos2!=-1) ? elemento.substring(3, pos2) : elemento.substr(3));
			caratterispeciali="";
		}else{
			lmax=elemento.substring(3, pos1);
			caratterispeciali=((pos2==-1) ? elemento.substr(pos1+1) : elemento.substring(pos1+1, pos2));
		}
		if(pos2==-1) label=''; else label=elemento.substr(pos2+3);
		valore=thisForm.elements[i].value;
		if(valore != ""){
			switch(ctgr){
				case "c": // alfabetico
					if(!eAlfabetico(valore, caratterispeciali)){ msgError = "Occorre inserire dei caratteri alfabetici"; if(caratterispeciali!="") msgError+=" o ["+caratterispeciali+"]"; msgError+="."; isError = true; }
					break;
				case "n": // numerico
					if(!eNumerico(valore, caratterispeciali)){ msgError = "Occorre inserire dei caratteri numerici (0..9)"; if(caratterispeciali!="") msgError+=" o ["+caratterispeciali+"]"; msgError+="."; isError = true; }
					break;
				case "d": // data in formato gg/mm/aaaa
					if(!eData(valore)){ msgError = "Occorre inserire una data valida."; isError = true; }
					break;
				case "x": // alfanumerico
					if(!eAlfanumerico(valore, caratterispeciali)){ msgError = "Occorre inserire caratteri alfanumerici"; if(caratterispeciali!="") msgError+=" o ["+caratterispeciali+"]"; msgError+="."; isError = true; }
					break;
				case "e": // email
					if(!eMail(valore)){ msgError = "Occorre inserire una e-mail corretta."; isError = true; }
					break;
					
			}
		}
		if(isError) break;
		msgError=verifica(valore, tipo, oprd, lmax);
		if(msgError != ""){ isError = true; break;}
	}
	if(isError){
		if(label=='') label='Dove si posizionerà il cursore.';
		alert(label+'\n'+msgError);
		thisForm.elements[i].focus();
		if(thisForm.elements[i].type=='text') thisForm.elements[i].select();
		return false;
	}
	return true;
}
function setRadioElement(radioName, radioValue){
	if(radioName==null || radioValue=='') return;
	rValue=radioValue
	if(radioValue=='Vero') rValue=1;
	if(radioValue=='Falso') rValue=0;
	if(radioName.length==null)
		radioName.checked=true;
	else
		for(iRadio=0; iRadio<radioName.length; iRadio++) if(radioName[iRadio].value==rValue){ radioName[iRadio].checked=true; break; }
}
function campoObbligatorio(campo, obbligatorio){
	v=((obbligatorio) ? 'o' : 'f');
	campo.id=campo.id.substring(0, 1)+v+campo.id.substr(2);
}
function RadioChecked(radioName){
	if(radioName!=null && radioName.length!=null){
		for(iRadio=0; iRadio<radioName.length; iRadio++) if(radioName[iRadio].checked) break;
		return (iRadio<radioName.length);
	}else return false;
}

