var menu_div = null;

function clickToOpen(text_id) {
	if (text_id == "") return;
	var divs = document.getElementById("div_"+text_id);
	if (divs) {
		if ((menu_div != null)&&(menu_div != divs)) {
			menu_div.style.display = "none";
			menu_div = divs;
		}
		if (divs.style.display != "block") {
			divs.style.display = "block";
		} else {
			divs.style.display = "none";
		}
		menu_div = divs;
	}
	return false;
}

function check_email(email) {
	if (email.search(/^([^()<>@,;:\t\r\n\\"\.\[\]\x00-\x1f ]+\.)*[^()<>@,;:\t\r\n.\\"\[\]\x00-\x1f ]+@([^()<>@,;:\t\r\n\\"\.\[\]\x00-\x1f ]+\.)+[a-z][a-z]([a-z])?([a-z])?([a-z])?([a-z])?$/i) != 0) {
		alert("L'e-mail non sembra corretta");
		return false;
	}
	return true;
}

function controlla_validita(cf) {
	cf = cf + '';
	if (cf.length != 16) return false;
	if (cf.search(/^[a-z][a-z][a-z][a-z][a-z][a-z][0-9][0-9][abcdehlmprst](71|70|[0-6][0-9])[a-z][0-9][0-9][0-9][a-z]$/i) == 0) {
		// In realta il codice del comune (per adesso) arriva solo fino a m214 non z999
		return true;
	}
	return false;
}

function add_newsletter(form_instance) {
	if (!form_instance.privacy.checked) {
		alert("Si deve necessariamente accettare la policy sulla privacy per continuare");
		return false;
	}
	if (!check_email(form_instance.mail.value)) return false;
	if (form_instance.name.value == "") {
		alert("Manca il nome");
		return false;
	}
	return confirm('Sei sicuro di volerti iscrivere alla newsletter?');
}

function checkFormContatto(form_instance) {
	if (form_instance.nome.value == "") {
		alert("Manca il nome");
		return false;
	}
	if (form_instance.cognome.value == "") {
		alert("Manca il cognome");
		return false;
	}
	if (form_instance.citta.value == "") {
		alert("Manca la cittą");
		return false;
	}
	if (!check_email(form_instance.email.value)) return false;
	if (form_instance.note.value == "") {
		alert("Mancano le note");
		return false;
	}
	if (!form_instance.privacy.checked) {
		alert("Si deve necessariamente accettare la policy sulla privacy per continuare");
		return false;
	}
	return confirm('Sei sicuro di voler inviare i dati inseriti?');
}

function checkFormPreventivo(form_instance) {
	var tipo = document.getElementById("tipo").value;

	if (document.getElementById(tipo+"_"+"nomecognome").value == "") {
		alert("Manca il nome e il cognome");
		return false;
	}
	if (!controlla_validita(document.getElementById(tipo+"_"+"codicefiscale").value)) {
		alert("Il codice fiscale non sembra corretto");
		return false;
	}
	if (!check_email(document.getElementById(tipo+"_"+"email").value)) return false;
	if (document.getElementById(tipo+"_"+"marcamodello").value == "") {
		alert("Manca il modello e la marca");
		return false;
	}
	if (document.getElementById(tipo+"_"+"targa").value == "") {
		alert("Manca la targa");
		return false;
	}
	if (document.getElementById(tipo+"_"+"cilindrata").value == "") {
		alert("Manca la cilindrata");
		return false;
	}
	
	if (tipo == "auto") {
		if (document.getElementById(tipo+"_"+"kw").value == "") {
			alert("Mancano i Kw");
			return false;
		}
	}
	if (tipo == "autocarro") {
		if (document.getElementById(tipo+"_"+"peso").value == "") {
			alert("Manca il peso");
			return false;
		}
	}
	
	var primaassicura = document.getElementById(tipo+"_primaassicura");
	primaassicura = primaassicura.options[primaassicura.selectedIndex].value

	if (primaassicura == "NO") {
		if (document.getElementById(tipo+"_"+"scadenzapolizza").value == "") {
			alert("Manca la scadenza della polizza");
			return false;
		}
		if (document.getElementById(tipo+"_"+"compagniaprovenienza").value == "") {
			alert("Manca la compagnia di provenienza");
			return false;
		}
	}
	
	if (!form_instance.privacy.checked) {
		alert("Si deve necessariamente accettare la policy sulla privacy per continuare");
		return false;
	}
	
	return confirm('Sei sicuro di voler inviare i dati inseriti?');
}

function selectPrimoPreventivo(valore) {
	var tipo = document.getElementById("tipo").value;
	
	if (valore == "SI") {
		document.getElementById(tipo+"_scadenzapolizza").disabled = true;
		document.getElementById(tipo+"_compagniaprovenienza").disabled = true;
		document.getElementById(tipo+"_numerodisinistri").disabled = true;
		document.getElementById(tipo+"_scadenzapolizza").style.backgroundColor = "#aaaaaa";
		document.getElementById(tipo+"_compagniaprovenienza").style.backgroundColor = "#aaaaaa";
		document.getElementById(tipo+"_numerodisinistri").style.backgroundColor = "#aaaaaa";
		var testo = "Indicare la Classe di Merito maturata nell'anno in corso su un altra autovettura di tua proprietą o di un tuo familiare vivente (contrassegnata come \"classe di assegnazione CU\" sull'attestato di rischio):";
	} else {
		document.getElementById(tipo+"_scadenzapolizza").disabled = false;
		document.getElementById(tipo+"_compagniaprovenienza").disabled = false;
		document.getElementById(tipo+"_numerodisinistri").disabled = false;
		document.getElementById(tipo+"_scadenzapolizza").style.backgroundColor = "#dddddd";
		document.getElementById(tipo+"_compagniaprovenienza").style.backgroundColor = "#dddddd";
		document.getElementById(tipo+"_numerodisinistri").style.backgroundColor = "#ffffff";
		var testo = "Indicare la Classe di Merito maturata per il nuovo anno (contrassegnata come \"classe di assegnazione CU\" sull'attestato di rischio):";
	}
	document.getElementById(tipo+"_classedimerito_label").innerHTML = testo;
}

function changeOpzione(valore) {
	var tipo = document.getElementById("tipo").value;

	if (valore == "base") {
		document.getElementById(tipo+"_massimale").disabled = true;
		document.getElementById(tipo+"_rinunciarivalsa").disabled = true;
		document.getElementById(tipo+"_conducenteesclusivo").disabled = true;
		document.getElementById(tipo+"_tutelalegale").disabled = true;
		document.getElementById(tipo+"_chilometriannui").disabled = true;
		document.getElementById(tipo+"_massimale").style.backgroundColor = "#aaaaaa";
		document.getElementById(tipo+"_rinunciarivalsa").style.backgroundColor = "#aaaaaa";
		document.getElementById(tipo+"_conducenteesclusivo").style.backgroundColor = "#aaaaaa";
		document.getElementById(tipo+"_tutelalegale").style.backgroundColor = "#aaaaaa";
		document.getElementById(tipo+"_chilometriannui").style.backgroundColor = "#aaaaaa";
	} else {
		document.getElementById(tipo+"_massimale").disabled = false;
		document.getElementById(tipo+"_rinunciarivalsa").disabled = false;
		document.getElementById(tipo+"_conducenteesclusivo").disabled = false;
		document.getElementById(tipo+"_tutelalegale").disabled = false;
		document.getElementById(tipo+"_chilometriannui").disabled = false;
		document.getElementById(tipo+"_massimale").style.backgroundColor = "#ffffff";
		document.getElementById(tipo+"_rinunciarivalsa").style.backgroundColor = "#dddddd";
		document.getElementById(tipo+"_conducenteesclusivo").style.backgroundColor = "#dddddd";
		document.getElementById(tipo+"_tutelalegale").style.backgroundColor = "#dddddd";
		document.getElementById(tipo+"_chilometriannui").style.backgroundColor = "#dddddd";
	}
}

function selectPreventivo(tipo) {
	document.getElementById("div_auto").style.display = "none";
	document.getElementById("div_autocarro").style.display = "none";
	document.getElementById("div_motociclo").style.display = "none";
	
	document.getElementById("div_"+tipo).style.display = "block";
	
	var picon_auto = document.getElementById("picon_auto");
	var picon_autocarro = document.getElementById("picon_autocarro");
	var picon_motociclo = document.getElementById("picon_motociclo");

	if (tipo == "auto") picon_auto.className = "picon auto_sel";
	else picon_auto.className = "picon auto";
	if (tipo == "autocarro") picon_autocarro.className = "picon autocarro_sel";
	else picon_autocarro.className = "picon autocarro";
	if (tipo == "motociclo") picon_motociclo.className = "picon motociclo_sel";
	else picon_motociclo.className = "picon motociclo";

	document.getElementById("tipo").value = tipo;
	return false;
}
