// JavaScript Document

function fldValidation(fname,ftest){
	btn = document.getElementById('conferma');
	what = document.getElementById(fname);
	whatMsg = document.getElementById(fname+'_msg');
	errors=0;
	
	if(ftest=='isName'){
		var r=new RegExp("([a-zA-Z\ ]{2,})");
	}else if(ftest=='isCap'){
		var r=new RegExp("^[0-9]{4,10}$");
	}else if(ftest=='isPhone'){
		var r=new RegExp("([0-9\\-\\ \\/\\+]{6,})");
	}else if(ftest=='isEmail'){
		var r=new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");
	}else if(ftest=='isAlfaNum'){
		var r=new RegExp("^[a-zA-Z0-9]{1,}$");
	}
	try {
		if (r) {
			if(!r.test(what.value)) {
				errors=errors+1;
				what.style.backgroundColor='#FFFF99';
				whatMsg.innerHTML = "Verificare il testo inserito";
				whatMsg.style.display = 'block';
			}else{
				what.style.backgroundColor='#FFFFFF';
				whatMsg.innerHTML = "";
				whatMsg.style.display = 'none';
			}    
		}
	} catch (err) {
	}	

	
	if(ftest=='isDate'){
		var i=new RegExp("^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})$");
		if(i.test(what.value)) {
			datePart=what.value.split("/");
			giorno=(datePart[0]>=1) && (datePart[0]<=31);
			mese=(datePart[1]>=1) && (datePart[1]<=12);
			anno=(datePart[2]>=1900) && (datePart[2]<=9999);
			if(!giorno || !mese || !anno) { 
				errors=errors+1;
				what.style.backgroundColor='#FFFF99';
				whatMsg.innerHTML = "Verificare il testo inserito";
				whatMsg.style.display = 'block';
			}else{
				what.style.backgroundColor='#FFFFFF';
				whatMsg.innerHTML = "";
				whatMsg.style.display = 'none';
			}
		}else{
			errors=errors+1;
			what.style.backgroundColor='#FFFF99';
			whatMsg.innerHTML = "Verificare il testo inserito";
			whatMsg.style.display = 'block';
		}
	}
	
	if(ftest=='isCf'){
		var validi, i, s, set1, set2, setpari, setdisp;
		cf = what.value.toUpperCase();
		if( cf.length == 0){
			what.style.backgroundColor='#FFFFFF';
			whatMsg.innerHTML = "";
			whatMsg.style.display = 'none';	
			errors=0;
		}else if( cf.length != 16 ){
			errors=errors+1;
			what.style.backgroundColor='#FFFF99';
			whatMsg.innerHTML = "La lunghezza del codice fiscale non &egrave; corretta (non inserire spazi)";
			whatMsg.style.display = 'block';
		}else{
			validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
			for( i = 0; i < 16; i++ ){
				if( validi.indexOf( cf.charAt(i) ) == -1 ){
					errors=errors+1;
					what.style.backgroundColor='#FFFF99';
					whatMsg.innerHTML = "Il codice fiscale contiene un carattere non valido '" + cf.charAt(i) + "'.";
					whatMsg.style.display = 'block';
				}
				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=errors+1;
					what.style.backgroundColor='#FFFF99';
					whatMsg.innerHTML = "Il codice fiscale non &egrave; corretto.";
					whatMsg.style.display = 'block';
				}
			}
			if(errors==0){
				what.style.backgroundColor='#FFFFFF';
				whatMsg.innerHTML = "";
				whatMsg.style.display = 'none';	
			}
		}
	}
	
	
	if(ftest=='isPiva'){
		what.style.backgroundColor='#FFFFFF';
		whatMsg.innerHTML = "";
		whatMsg.style.display = 'none';
		pi = what.value.toUpperCase();
		if( pi.length == 0){
			what.style.backgroundColor='#FFFFFF';
			whatMsg.innerHTML = "";
			whatMsg.style.display = 'none';	
			errors=0;
		}else if(pi.length==11 || pi.length==13) {
			validi = "0123456789ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ";
			for(i=0;i<pi.length;i++){
				if(validi.indexOf(pi.charAt(i)) == -1) {
					errors=errors+1;
					what.style.backgroundColor='#FFFF99';
					whatMsg.innerHTML = "La partita IVA contiene un carattere non valido.";
					whatMsg.style.display = 'block';
				}
			}
			s = 0;
			if(pi.length==13){
				pi=pi.substr(2,11);
			}
			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=errors+1;
				what.style.backgroundColor='#FFFF99';
				whatMsg.innerHTML = "La partita IVA non e valida. Il codice di controllo non corrisponde.";
				whatMsg.style.display = 'block';
			}
		}else{
			errors=errors+1;
			what.style.backgroundColor='#FFFF99';
			whatMsg.innerHTML = "La lunghezza della Partita IVA non &egrave; corretta";
			whatMsg.style.display = 'block';
		}
	}
	
	
	if(ftest=='isIbanPostepay'){
		if(what.value.length==16){
			
			//Postepay
			var r=new RegExp("^[0-9]{16}$");
			if(!r.test(what.value)) {
				errors=errors+1;
				what.style.backgroundColor='#FFFF99';
				whatMsg.innerHTML = "Verificare il codice inserito";
				whatMsg.style.display = 'block';
			}else{
				what.style.backgroundColor='#FFFFFF';
				whatMsg.innerHTML = "";
				whatMsg.style.display = 'none';
			}
			
		}else if(what.value.length>16){
			
			//IBAN
			what.style.backgroundColor='#FFFFFF';
			whatMsg.innerHTML = "";
			whatMsg.style.display = 'none';
			var iban     = what.value.toUpperCase();
			var code     = iban.substring(0, 2);
			var checksum = iban.substring(2, 4);
			var bban     = iban.substring(4);
			var country  = CountryData(code);
			
			var err = null;
			  if (country == null){
					err = "Codice nazione sconosciuto: " + code;
			  }else if(InvalidIBANlength(country, iban)){
					err = "Lunghezza IBAN " + iban.length + " errata";
			  }else{
					var bank_lng = country.bank_lng;
					var bank     = bban.substring(0, bank_lng);
					var account  = bban.substring(bank_lng);
					if (InvalidBank(country, bank)){
						err = _("Codice Banca ") + bank + _(" errato: ") + country.name;
					}else if (InvalidAccount(country, account)){
				  		err = _("Account Number ") + account + _(" errato: ") + country.name;
					}else if (!IBANokay(iban)){
				  		err = _("Checksum IBAN errato");
					}
			  }
			
			  if (err) {
				errors=errors+1;
				what.style.backgroundColor='#FFFF99';
				whatMsg.innerHTML = err;
				whatMsg.style.display = 'block';
			  }else{
				what.style.backgroundColor='#FFFFFF';
				whatMsg.innerHTML = "";
				whatMsg.style.display = 'none';
				// Check for dispensable global variables in debug modus
				if (debug_output){
				  debug_check_vars();
				}
			  }
		}else if(what.value.length==0){
			what.style.backgroundColor='#FFFFFF';
			whatMsg.innerHTML = "";
			whatMsg.style.display = 'none';
		}else{
			errors=errors+1;
			what.style.backgroundColor='#FFFF99';
			whatMsg.innerHTML = "Verificare il testo inserito. Non inserire spazi!";
			whatMsg.style.display = 'block';
		}
	}
	
	if(ftest=='isPwd'){
		var minLength = 8; // Minimum length
		var pw1 = document.getElementById('pass').value;
		var pw2 = document.getElementById('pass2').value;
		document.getElementById('pass').style.backgroundColor='#FFFFFF';
		document.getElementById('pass_msg').innerHTML = "";
		document.getElementById('pass2').style.backgroundColor='#FFFFFF';
		document.getElementById('pass2_msg').innerHTML = "";
		if (pw1 != '') {
			// check for minimum length
			if (pw1.length < minLength) {
				errors=errors+1;
				document.getElementById('pass_msg').style.backgroundColor='#FFFF99';
				document.getElementById('pass_msg').innerHTML = "La password deve essere lunga almeno 8 caratteri e contenere solo lettere (A-Z a-z) e numeri (0-9)";
				whatMsg.style.display = 'block';
			}else if (pw1 == '12345678'){
				errors=errors+1;
				document.getElementById('pass_msg').style.backgroundColor='#FFFF99';
				document.getElementById('pass_msg').innerHTML = "Password non consentita";
				whatMsg.style.display = 'block';
			}else{
				// check for invalid chars
				var r=new RegExp("^[a-zA-Z0-9]{1,}$");
				if(!r.test(pw1)) {
					errors=errors+1;
					document.getElementById('pass_msg').style.backgroundColor='#FFFF99';
					whatMsg.innerHTML = "La password contiene caratteri non consentiti. Usare solo lettere (A-Z a-z) e numeri (0-9)";
					whatMsg.style.display = 'block';
				} 
			}
		}
		if(pw1 != '' && pw2 != '') {
			if (pw1 != pw2) {
				errors=errors+1;
				document.getElementById('pass2_msg').style.backgroundColor='#FFFF99';
				document.getElementById('pass2_msg').innerHTML = "La password di controllo non corrisponde";
				whatMsg.style.display = 'block';
			}
		}
   }

		
	if(errors==0){
		btn.disabled=false;
		return true;
	}else{
		btn.disabled=true;
		return false;
	}
}

function clearFld(fname){
	what = document.getElementById(fname);
	whatMsg = document.getElementById(fname+'_msg');
	what.style.backgroundColor='#FFFFFF';
	whatMsg.innerHTML = "";
	whatMsg.style.display = 'none';
}	

function chkPrivacy(fname){
	a = document.getElementById(fname);
	if(a.checked==true) {
		return true;
	}else{
		alert("Per proseguire con l\'invio e'\ indispensabile\n"
			+ "fornire il consenso al trattamento dei dati\n"
			+ "ai sensi del D.L. 196/2003 e succ. modifiche");
		return false;	
	}
}
