var errore = "";
function checkTextForm(name,msg)
{
return checkTextForm(name,msg,3);
}
function checkTextForm(name,msg,length)
	{
		if(document.getElementById("input-"+name))
		{
			var value = document.getElementById("input-"+name).value;
			if((value=="") || (value.length < 3))
			{
				document.getElementById("msg-"+name).innerHTML = msg;
				document.getElementById("input-"+name).parentNode.parentNode.className="input_field_container input_no";
				errore=errore.replace("#"+name+"#","");
				errore+="#"+name+"#";
			}
			else
			{
				document.getElementById("msg-"+name).innerHTML = "OK";
				document.getElementById("input-"+name).parentNode.parentNode.className="input_field_container input_ok";
				errore=errore.replace("#"+name+"#","");
				
			}
		}
		else
		{
		alert("Errore Generico.");
		}
		
		
	}

function checkData()
{
//((anno % 4 == 0 && anno % 100 != 0) || anno % 400 == 0)
	var giorno = document.getElementById("sel-giorno").value;
	var mese = document.getElementById("sel-mese").value;
	var anno = document.getElementById("sel-anno").value;
	var mesi_giorni = new Array(31,31,28,31,30,31,30,31,31,30,31,30,31);
	if(giorno > mesi_giorni[mese])
	{
		document.getElementById("msg-data").innerHTML = "Data non Valida: "+giorno+"/"+mese+"/"+anno;
		document.getElementById("sel-giorno").parentNode.parentNode.className="input_field_container input_no";
		errore=errore.replace("#data#","");
		errore+="#data#";
		document.getElementById("dob").value="";
	}
	else
	{
		document.getElementById("msg-data").innerHTML = "OK";
		document.getElementById("sel-giorno").parentNode.parentNode.className="input_field_container input_ok";
		errore=errore.replace("#data#","");
		document.getElementById("dob").value=mese+"/"+giorno+"/"+anno;
	}
}

function checkMail()
{
	var x = document.getElementById("email_address").value;
	if (x=="")
	{
	/*var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(x))
	{*/
		document.getElementById("msg-email").innerHTML = "Email non Valida";
		document.getElementById("email_address").parentNode.parentNode.className="input_field_container input_no";
		errore=errore.replace("#email#","");
		errore+="#email#";
	}
	else
	{
		document.getElementById("msg-email").innerHTML = "OK";
		document.getElementById("email_address").parentNode.parentNode.className="input_field_container input_ok";
		errore=errore.replace("#email#","");
	}
}

function codiceFISCALE(cfins)
{
	var cf = cfins.toUpperCase();
	var cfReg = /^[A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z]$/;
	if (!cfReg.test(cf))
	return false;
	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) )
		return false;
   return true;
}


function checkCF()
{
	var CFok=codiceFISCALE(document.getElementById("cf").value)
	if (CFok==false)
	{
		document.getElementById("msg-cf").innerHTML = "Codice Fiscale non valido";
		document.getElementById("cf").parentNode.parentNode.className="input_field_container input_no";
		errore=errore.replace("#cf#","");
		errore+="#cf#";
	}
	else
	{
		document.getElementById("msg-cf").innerHTML = "OK";
		document.getElementById("cf").parentNode.parentNode.className="input_field_container input_ok";
		errore=errore.replace("#cf#","");
	}
}


function checkCAP()
	{
		var value = document.getElementById("input-cap").value;
		if((value=="") || (isNaN(value)) || (value.length > 5))
		{
			document.getElementById("msg-cap").innerHTML = "Inserire un C.A.P. valido";
			document.getElementById("input-cap").parentNode.parentNode.className="input_field_container input_no";
			errore=errore.replace("#cap#","");
			errore+="#cap#";
		}
		else
		{
			document.getElementById("msg-cap").innerHTML = "OK";
			document.getElementById("input-cap").parentNode.parentNode.className="input_field_container input_ok";
			errore=errore.replace("#cap#","");
			
		}
		
	}
	
function checkPassword()
{
	var pass = document.getElementById("password").value;
	var conf = document.getElementById("pass-confirmation").value;
	if(pass=="")
	{
			document.getElementById("msg-password").innerHTML = "Devi inserire una password";
			document.getElementById("password").parentNode.parentNode.className="input_field_container input_no";
			errore=errore.replace("#pass#","");
			errore+="#pass#";
	}
	else if(pass.length < 5)
	{
		document.getElementById("msg-password").innerHTML = "Lunghezza Minima Password: 5 caratteri";
		document.getElementById("password").parentNode.parentNode.className="input_field_container input_no";
		errore=errore.replace("#pass#","");
		errore+="#pass#";
	}
	else
	{
		if(pass!=conf)
		{
		document.getElementById("msg-password").innerHTML = "Attenzione: ";
		document.getElementById("msg-confirmation").innerHTML = "Le due password devono coincidere";
		document.getElementById("password").parentNode.parentNode.className="input_field_container input_no";
		document.getElementById("pass-confirmation").parentNode.parentNode.className="input_field_container input_no";
		errore=errore.replace("#pass#","");
		errore+="#pass#";
		errore=errore.replace("#passconf#","");
		errore+="#passconf#";
		}
		else
		{
			document.getElementById("msg-password").innerHTML = "OK";
			document.getElementById("password").parentNode.parentNode.className="input_field_container input_ok";
			errore=errore.replace("#pass#","");
			document.getElementById("msg-confirmation").innerHTML = "OK";
			document.getElementById("pass-confirmation").parentNode.parentNode.className="input_field_container input_ok";
			errore=errore.replace("#passconf#","");
		
		
		}
		
	}
}

function checkPrivacy()
{
	if(document.getElementById("checkPrivacy").checked==true)
	{
		document.getElementById("msg-privacy").innerHTML = "Ok, Direttive sulla privacy accettate";
		document.getElementById("checkPrivacy").parentNode.className="input_field_container input_ok";
		errore=errore.replace("#privacy#","");
	}
	else
	{
		document.getElementById("msg-privacy").innerHTML = "E' OBBLIGATORIO accettare le direttive sulla privacy e le condizioni di utilizzo.";
		document.getElementById("checkPrivacy").parentNode.className="input_field_container input_no";
		errore=errore.replace("#privacy#","");
		errore+="#privacy#";
	}
}





function validate_form()
{
	checkTextForm("nome","MIN 3 Caratteri");		
	checkTextForm("cognome","MIN 3 Caratteri");   
	checkData();          
	checkMail();
	checkCF();  
	checkTextForm("street_address","MIN 3 Caratteri");	
	checkCAP(); 
	checkTextForm("citta","MIN 3 Caratteri");
	checkTextForm("telefono","MIN 5 Caratteri",5);	
	checkPassword() 
	checkPrivacy();
	if(errore!="")
	{
		document.getElementById("messaggi_errore").innerHTML = "<font color ='red'>Attenzione,sono occorsi degli errori durante la compilazione del form; controllare tutti i campi</font>";
		return false;
	}
	else
	{
		document.getElementById("messaggi_errore").innerHTML = "<font color = 'green'>Form Compilato con successo. Attendere...</font>";
		setTimeout(function() {	return true; }, 650);
	}
	
}