// JavaScript Document
var errors="";
function verify(f)
{		
	
	errors="";
	var empty_fields="";
	//var errors="";
	var checkOk=true;
	for (var i=0;i<f.length;i++){
		var e=f.elements[i];	
//		alert(e.name);
		if (((e.type=="text") || (e.type=="textarea"))){
			//controllo se il campo è vuoto 
			if ((e.controlla) && ((e.value==null) || (e.value=="") || isblank(e.value))){
				empty_fields+="\n          "+e.name;
				checkOk=false;
				continue;
			}
			
			//Controllo che il campo sia pieno....
			if ((e.value==null) || (e.value=="") || isblank(e.value)){
				continue;	
			}
			//Controllo i campi che devono essere numerici
			if (e.numeric || (e.min!=null) || (e.max!=null)){
				var v=parseFloat(e.value);
				if (isNaN(v) || ((e.min!=null) && (v<e.min)) || ((e.max!=null) && (v.value>e.max))){
					if (e.controlla) checkOk=false;
					errors+="-Il campo "+e.name+" deve essere un numero";
					if (e.min!=null) errors+=" maggiore di "+e.min;
					if (e.max!=null && e.min!=null) 
						errors+=" e minore di "+e.max;
					else if(e.max!=null)
						errors+=" minore di "+e.max;
					errors+=".\n";
				}
			}else if(e.value.length>0){
				if (e.mail){
					Filtro = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
					if (!Filtro.test(e.value)){
						errors+="Il campo mail "+e.name+" contiene un indirizzo non valido!!\n";
						checkOk=false;
					}
				}else if(e.telefonico){
					
					checkOk=isTelefonico(e.value);
					if(!checkOk) errors+="Il campo "+e.name+" contiene dati non validi!!\n";
					
				}else if(e.checkPIVA){
				
					checkOk=ControllaPIVA(e.value);
					//if(!checkOk) errors+="Il campo "+e.name+" contiene dati non validi!!\n";
				}else if(e.codiceFiscale){
					
					checkOk=ControllaCF(e.value);
					//if(!checkOk) errors+="Il campo "+e.name+" contiene dati non validi!!\n";
				}else if(e.user){
					
					checkOk=isTelefonico(e.value);
					if(!checkOk) errors+="Il campo "+e.name+" contiene dati non validi!!\n";
				}
			}
		}
	}
	if (errors || empty_fields){
		msg="____________________________________________________\n";
		if (empty_fields){
			msg+="- I seguenti campi richiesti risultano vuoti:"+empty_fields+"\n";
			if (errors) msg+="\n";
		}
		msg+=errors;
		msg+="____________________________________________________";
		alert(msg);
	}
	if (checkOk){ 
		return true;
	}else{
		return false;
	}
}

function ControllaCF(cf)
{
    var validi, i, s, set1, set2, setpari, setdisp;
    if( cf == '' )  return false;
    cf = cf.toUpperCase();
    if( cf.length != 16 )
        errors+="La lunghezza del codice fiscale non è corretta: il codice fiscale dovrebbe essere lungo esattamente 16 caratteri.\n";
    validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    for( i = 0; i < 16; i++ ){
        if( validi.indexOf( cf.charAt(i) ) == -1 ){
            errors+="<strong>Il codice fiscale</strong> contiene un carattere non valido `" + cf.charAt(i) + "'.I caratteri validi sono le lettere e le cifre.\n";
			return false;
		}	
    }
    set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
    s = 0;
    for( i = 1; i <= 13; i += 2 )
        s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    for( i = 0; i <= 14; i += 2 )
        s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
    if( s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) ){
        errors+="Il codice fiscale non è corretto: il codice di controllo non corrisponde.\n";
		return false;
	}
    return true;
}


function ControllaPIVA(pi)
{
    if( pi == '' )  return false;
    if( pi.length != 11 ){
        errors+="La lunghezza della partita IVA non è corretta: la partita IVA dovrebbe essere lunga esattamente 11 caratteri.\n";
			return false;
	}
    validi = "0123456789";
    for( i = 0; i < 11; i++ ){
        if( validi.indexOf( pi.charAt(i) ) == -1 ){
            errors+="La partita IVA contiene un carattere non valido `" + pi.charAt(i) + "'.I caratteri validi sono le cifre.\n";
			return false;
		}
    }
    s = 0;
    for( i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for( i = 1; i <= 9; i += 2 ){
        c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if( ( 10 - s%10 )%10 != pi.charCodeAt(10) - '0'.charCodeAt(0) ){
        errors+="La partita IVA non ?alida: il codice di controllo non corrisponde.\n";
		return false;
	}		
    return true;
}


function isblank(s){
	for(var i=0;i<s.length;i++){
		var c=s.charAt(i);
		if ((c!=' ') && (c!='\n') && (c!='\t')) return false;
	}
	return true;
}

function isNumeric(stringa){
	
	if (isNaN(stringa)){
		return true;	
	}else{
		return false;
	}
	
}

function isTelefonico(stringa){

	if (!isNaN(stringa)){
		return true;	
	}else{
		return false;
	}
	
}

//questa funzione controlla i caratteri di un campo confrontandoli con i caratteri della maschera
function controllaDaMaschera(stringa,maschera){
	
	
	
}

