function handleHomeForm() {
	$("#nome").focus(function(){ if($("#nome").val() == "Nome") $("#nome").val(""); });
	$("#nome").blur(function() { if($("#nome").val() == "") $("#nome").val("Nome"); });
	$("#cognome").focus(function(){ if($("#cognome").val() == "Cognome") $("#cognome").val(""); });
	$("#cognome").blur(function() { if($("#cognome").val() == "") $("#cognome").val("Cognome"); });
	$("#email").focus(function(){ if($("#email").val() == "Email") $("#email").val(""); });
	$("#email").blur(function() { if($("#email").val() == "") $("#email").val("Email"); });
	$("#datanascita").focus(function(){ if($("#datanascita").val() == "Data di nascita (gg/mm/aaaa)") $("#datanascita").val(""); });
	$("#datanascita").blur(function() { if($("#datanascita").val() == "") $("#datanascita").val("Data di nascita (gg/mm/aaaa)"); });
	$("#modelloauto").focus(function(){ if($("#modelloauto").val() == "Modello Auto") $("#modelloauto").val(""); });
	$("#modelloauto").blur(function() { if($("#modelloauto").val() == "") $("#modelloauto").val("Modello Auto"); });
	$("#scadenzaAssicurazione").focus(function(){ if($("#scadenzaAssicurazione").val() == "Scadenza Assicurazione (gg/mm/aaaa)") $("#scadenzaAssicurazione").val(""); });
	$("#scadenzaAssicurazione").blur(function() { if($("#scadenzaAssicurazione").val() == "") $("#scadenzaAssicurazione").val("Scadenza Assicurazione (gg/mm/aaaa)"); });
	
	$("#formindex").submit(function() {
		var bNome	= checkNome();
		if(bNome == false)
			return false;
		else
			{
				var bCognome	= checkCognome();
				if(bCognome == false)
					return false;
				else
					{
						var bEmail	= checkEmail();
						if(bEmail == false)
							return false
						else
							{
								var bDataNascita = checkDataNascita();
								if(bDataNascita == false)
									return false
								else
									{
										var bProvincia = checkProvincia();
										if(bProvincia == false)
											return false;
										else
											{
												var bMarca = checkMarca();
												if(bMarca == false)
													return false;
												else
													{
														var bModello = checkModello();
														if(bModello == false)
															return false;
														else
															{
																var bDataScadenza = checkDataScadenza();
																if(bDataScadenza == false)
																	return false;
																else
																	{
																		var bSesso = checkSesso();
																		if(bSesso == false)
																			return false;
																		else
																			{
																				var bPrivacy = checkPrivacy();
																				if(bPrivacy == false)
																					return false
																				else
																					return true;
																			}
																	}
															}
													}
											}
									}
							}
					}
			}
	});
}


$(function() {
	handleHomeForm();
	$("#datanascita").datepicker({
		showOn: "both",
		buttonImage: "/wp-content/themes/assicurazioniauto/images/datepicker_icon.png",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true,
		maxDate: "+1M"
	});
	$("#scadenzaAssicurazione" ).datepicker({
		showOn: "both",
		buttonImage: "/wp-content/themes/assicurazioniauto/images/datepicker_icon.png",
		buttonImageOnly: true
	});
	
	
	$('select#provincia').selectmenu({
		style:'dropdown',
		width:72,
		maxHeight:150
	});
	$('#marcaauto').selectmenu({
		style:'dropdown',
		width:291,
		maxHeight:150
	});
});

/* FUNCTIONS FORM */
function checkOnlyAlpha(stringa){
	if (stringa.match(/^([a-z '\-àèéìòù]{1,255})$/i))
		return true;
	else
		return false;
}

function checkNome() {
	nomeVal	= $("#nome").val();
	if(nomeVal == "" || nomeVal == "Nome")
		{
			alert("Inserire il nome per procedere");
			return false;
		}
	else
		{
			if(checkOnlyAlpha(nomeVal) == false)
				{
					alert("Inserire solo lettere nel campo nome per procedere");
					return false;
				}
			else
				return true;
		}
}

function checkCognome() {
	cognomeVal = $("#cognome").val();
	if( cognomeVal == "" || cognomeVal == "Cognome")
		{
			alert("Inserire il cognome per procedere");
			return false;
		}
	else
		{
			if(checkOnlyAlpha(cognomeVal) == false)
				{
					alert("Inserire solo lettere nel campo cognome per procedere");
					return false;
				}
			else
				return true;
		}
}

function checkEmail() {
	var emailAddressVal = $("#email").val();

	if(emailAddressVal == "" || emailAddressVal == "Email")
		{
			alert("Inserire l'email per procedere");
			return false;
		}
	else
		{
			var emailReg = /^([\w\-\.]+)@([\w\-]+)\.([\w]{2,4})$/;
			if( !emailReg.test(emailAddressVal) )
				{
					alert("Inserire un'email valida per procedere");
					return false;
				}
			else
				{
					return true;
				}
		}
}

function checkDataNascita() {
	var datanascitaVal = $("#datanascita").val();
	if(datanascitaVal == "" || datanascitaVal == "Data di nascita (gg/mm/aaaa)")
		{
			alert("Inserire la data di nascita per procedere");
			return false;
		}
	else
		{
			if (datanascitaVal.match(/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/))
				{
					return true;
				}
			else
				{
					alert("Inserire una data di nascita valida per procedere gg/mm/aaaa");
					return false;
				}
		}
}

function checkProvincia() {
	var provinciaVal = $("#provincia").val();
	if(provinciaVal == "-")
		{
			alert("Selezionare la provincia per procedere");
			return false;
		}
	else
		return true;
}

function checkMarca() {
	var marcaautoVal = $("#marcaauto").val();
	if(marcaautoVal == "-")
		{
			alert("Selezionare la marca dell'auto per procedere");
			return false;
		}
	else
		return true;
}

function checkModello() {
	modelloVal	= $("#modelloauto").val();
	if(modelloVal == "" || modelloVal == "Modello Auto")
		{
			alert("Inserire il modello dell'auto per procedere");
			return false;
		}
	else
		return true;
}

function checkDataScadenza() {
	var datascadenzaVal = $("#scadenzaAssicurazione").val();
	if(datascadenzaVal == "" || datascadenzaVal == "Scadenza Assicurazione (gg/mm/aaaa)")
		{
			alert("Inserire la data di scadenza assicurazione per procedere");
			return false;
		}
	else
		{
			if (datascadenzaVal.match(/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/))
				{
					return true;
				}
			else
				{
					alert("Inserire una data di scadenza assicurazione valida per procedere gg/mm/aaaa");
					return false;
				}
		}
}

function checkSesso() {
	var sessoVal = $("input:radio[name=sesso]:checked").val()
	if(sessoVal == "m" || sessoVal == "f")
		{
			return true;
		}
	else
		{
			alert("Selezionare il proprio sesso per procedere");
			return false;
		}
}

function checkPrivacy() {
	var privacyVal = $('#privacy').attr('checked');
	if(privacyVal == "checked")
		{
			return true;
		}
	else
		{
			alert("Devi accettare le condizioni per procedere.");
			return false;
		}
}
