var error_color = '#ff8888';
var error_color2 = '#EFC765';
var error_brd_color = 'red';
function validate(){

	//debug('validation begin');
	document.ret = true;
	var ret = true;

	var controlla_una_data = false;

	for(var valore in validates){
		//debug(valore,validates[valore]);
		var temp = controlla(valore,validates[valore]);

		if(temp == false) ret = false;

		//debug(ret);
	}
	//per la validazione del codice chaptcha
	//var ret2 = valida_code();
	//debug('ret2: '+ret2);
	//ret = ret && ret2;

	notify(!ret);
	document.ret =  ret;
	//debug('validation end');
}
function validateArrayed(arr){
	//debug('validation begin');

	var ret = true;
	for(var valore in arr){
		//debug(valore,arr[valore]);
		var temp = controlla(valore,arr[valore]);
		if(temp == false) ret = false;
		//console.debug(ret);
	}
	//per la validazione del codice chaptcha

	//debug('ret2: '+ret2);
	return ret;
	//notify(!ret);;
}

function controlladata(id){
	var annoP = jQuery("#"+id+"_a").val();
	var meseP = jQuery("#"+id+"_m").val();
	var giornoP = jQuery("#"+id+"_g").val();

	if(annoP=='' ||meseP=='' ||giornoP=='' ) return false;
	jQuery.post(
		"ajax_tools/checkData.php",
		{anno: ""+annoP+"",mese:""+meseP+"", giorno: ""+giornoP+""},
		function(data){
			var boole = (data.length ==0);
			document.ret_data = boole;
			//debug(boole);
			colora_bord(id+'_a',boole);
			colora_bord(id+'_m',boole);
			colora_bord(id+'_g',boole);
			return boole;
		}                		
	);
}
//come controlladata ..
function controlladata2(giornoP,meseP,annoP){
	if(annoP=='' ||meseP=='' ||giornoP=='' ) return false;
	jQuery.post(
		"ajax_tools/checkData.php",
		{anno: ""+annoP+"",mese:""+meseP+"", giorno: ""+giornoP+""},
		function(data){
			var boole = (data.length ==0);
			//debug(boole);
			document.ret_data = boole;
			colora_bord('anno',boole);
			colora_bord('mese',boole);
			colora_bord('giorno',boole);
			return boole;
		}
	);
}








//restituisce true in caso positivo
function controlla(id, tipo){	
	var ret = false;
	//"valore" lo metto nello switch in modo che caso per caso decido
	// un checkbox e un text hanno un "valore" differente da considerare
	switch(tipo){
		case 'string':
			var valore = jQuery("#"+id).val();
			ret = (valore != '');
			colora_bord(id,ret);
		break;
		case 'tel':
			var valore = jQuery("#"+id).val();
			//while(valore.search('/')!=-1)valore.replace('/','');
		
			ret = ValidateNo(valore,"1234567890+- /|\\");
			colora_bord(id,ret);
		break;
		case 'cell':
			var valore = jQuery("#"+id).val();
			ret = ValidateNo(valore,"1234567890+- /|\\");
			colora_bord(id,ret);
		break;
		case 'num_5':
			var valore = jQuery("#"+id).val();
			ret = (valore.match(/\d{5}/) != null) && valore.length == 5;
			//ret = (valore>10000)&&(valore<99999);
			colora_bord(id,ret);
		break;
		case 'string_4':
			var valore = jQuery("#"+id).val();
			ret = (valore.length == 4);
			colora_bord(id,ret);
		break;
		case 'email':
			var valore = jQuery("#"+id).val();
			ret = (valore.indexOf('@',0)!='-1')  && (valore.indexOf('.',0)!='-1');
			colora_bord(id,ret);
		break;
		case 'cfis':
			var cf = jQuery("#"+id).val();
			var ret = true;
			
			if( cf == '' ){ ret = false; colora_bord(id,ret);}// return '';
			cf = cf.toUpperCase();
			
			if( cf.length != 16 ){ ret = false; colora_bord(id,ret);}//return "La lunghezza del codice fiscale non è\n"+"corretta: il codice fiscale dovrebbe essere lungo\n"+"esattamente 16 caratteri.\n";
			validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			for( i = 0; i < 16; i++ ){
				if( validi.indexOf( cf.charAt(i) ) == -1 )
					ret = false;
					//return "Il codice fiscale contiene un carattere non valido `" +	cf.charAt(i) +	"'.\nI caratteri validi sono le lettere e le cifre.\n";
			}
			var set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			var set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
			var setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			var setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
			var 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) )
				ret = false;
				//return "Il codice fiscale non è corretto:\n"+"il codice di controllo non corrisponde.\n";
			
			colora_bord(id,ret);
			//return "";


		break;
		case 'cid':
			var valore = jQuery("#"+id).val();
			ret = (valore.length == 9) && ValidateNo(valore,"0123456789QWERTYUIOPASDFGHJKLZXCVBNM");
			colora_bord(id,ret);
		break;
		case 'date':
			var valore = jQuery("#"+id).val();
			ret = controlladata(id);
			colora_bord(id+'_a',ret);
			colora_bord(id+'_m',ret);
			colora_bord(id+'_g',ret);
		break;
		case 'iban':
			var valore = jQuery("#"+id).val();
			//ret = valida_iban(valore);
			////    IT\d{2}[ ][a-zA-Z]\d{3}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{4}[ ]\d{3}|IT\d{2}[a-zA-Z]\d{22}
			ret1 = (valore.match(/IT\d{2}[a-zA-Z]\w{22}/) != null);
			ret = (ret1  ) & (valore.length == 27);
			colora_bord(id,ret);
		break;
		case 'chbx':
			var valore = jQuery("#"+id).attr('checked');
			ret = (valore==true);
            colora_chbx(id,ret);
		break;
		
		case 'piva':
			var ret = true;
			var pi = jQuery("#"+id).val();
			if( pi == '' ){ ret = false; colora_bord(id,ret);}//return '';
			if( pi.length != 11 ){ ret = false; colora_bord(id,ret);}// return "La lunghezza della partita IVA non è\n" +	"corretta: la partita IVA dovrebbe essere lunga\n" +"esattamente 11 caratteri.\n";
			var validi = "0123456789";
			for( i = 0; i < 11; i++ ){
				if( validi.indexOf( pi.charAt(i) ) == -1 )
					ret = false;
					//return "La partita IVA contiene un carattere non valido `" +pi.charAt(i) + "'.\nI caratteri validi sono le cifre.\n";
			}
			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) )
				ret = false;
				//return "La partita IVA non è valida:\n" +"il codice di controllo non corrisponde.\n";

			//return ret;
			colora_bord(id,ret);
		break;
		// per un campo che contiene cfis o piva
		case 'cfispiva':
			var ret1 = true;
			var ret2 = true;
			var picfis1 = jQuery("#"+id).val();
			var picfis2 = jQuery("#"+id).val();



			if( picfis1 == '' ) ret1 = false; //return '';

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






			if( picfis2 == '' ) ret2 = false;// return '';
			picfis2 = picfis2.toUpperCase();

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





	}
	return ret;
}

//per i numeri di telefono
function ValidateNo(NumStr, String){
	if(!NumStr)return false;
    for(var Idx=0; Idx<NumStr.length; Idx++){
		var Char = NumStr.charAt(Idx);
        var Match = false;

        for(var Idx1=0; Idx1<String.length; Idx1++)
            if(Char == String.charAt (Idx1))
                Match = true;
        if (!Match)
            return false;
    }
    return true;

}

//lo aggiunge e se esiste sovrascrive il tipo
function add_element(el, ty){
	validates[el]=ty;
}

function rem_element(el){
	delete validates[el];
}

function rew_element(el, ty){
	add_element(el, ty);
}





function colora_bord(id,boole){
	//alert(id);
	//if(id==null) alert('nullo');
	document.getElementById(id).style.backgroundColor =  (boole==false)?error_color:"";
	//document.getElementById(id).style.borderColor =  (boole==false)?error_brd_color:"";
	//document.getElementById(id).style.borderStyle =  (boole==false)?"solid":"";
}
function colora_bord_opt(id,boole){
	//alert(id);
	//if(id==null) alert('nullo');
	document.getElementById(id).style.backgroundColor =  (boole==false)?error_color2:"";
	//document.getElementById(id).style.borderColor =  (boole==false)?error_brd_color:"";
	//document.getElementById(id).style.borderStyle =  (boole==false)?"solid":"";
}
function colora_chbx(id,boole){
	jQuery('#'+id).parent().css({'background-Color':''+((boole==false)?error_brd_color:"")});


	//document.getElementById(id).style.backgroundColor =  (boole==false)?error_color:"";
	//document.getElementById(id).style.borderColor =  (boole==false)?error_brd_color:"";
	//document.getElementById(id).style.borderStyle =  (boole==false)?"solid":"";
}

//
function reset_form(){
	for(var valore in validates){
		switch(validates[valore]){
        	case 'chbx':
				jQuery("#"+valore).attr({checked:''});
        	break;
        	default:
        		jQuery('#'+valore).val('');
		}
	}
	
	//infine
	jQuery('#contr_eq_regis').attr({checked:''});
	jQuery('#contr_eq_reg_res').attr({checked:''});
	jQuery('#contr_eq_reg_rec').attr({checked:''});
	jQuery('#allie_eq_regis').attr({checked:''});
	jQuery('#stu_eq_reg_res').attr({checked:''});
	jQuery('#stu_eq_reg_rec').attr({checked:''});
}






