var loginForm;

window.onload = init;

function init() {
	loginForm = document.getElementById('form_reg');
	if (loginForm.privato.checked) {
		loginForm.ragione_sociale.disabled = true;
		loginForm.partita_iva.disabled = true;
	}
	loginForm.onsubmit = function () {
		return canSubmit(this);
	}
	loginForm.e_mail.focus();
}

//Definisce il valore del campo VUOTO
function filled(field) {
	if (field.value == "" || field.value == " " || field.value == null || field.value == 0 || field.value == '00' || field.value == 'anno') {
	//if (field.value == 0 || field.value == null) {
		return false;
	} else {
		return true;
	}
}
	
function canSubmit(form) {
	
	form.e_mail.style.border = '#FFF';
	form.email_conf.style.border = '#FFF';
	form.password.style.border = '#FFF';
	form.conf_pass.style.border = '#FFF';
	form.nome.style.border = '#FFF';
	form.cognome.style.border = '#FFF';
	form.indirizzo.style.border = '#FFF';
	form.ragione_sociale.style.border = '#FFF';
	form.partita_iva.style.border = '#FFF';
	form.id_nazione.style.border = '#FFF';
	form.id_provincia.style.border = '#FFF';
	form.citta.style.border = '#FFF';
	form.telefono.style.border = '#FFF';
	form.cellulare.style.border = '#FFF';
	form.privacy.style.border = '#FFF';
	form.privacy2.style.border = '#FFF';
	form.verifica_cod.style.border = '#FFF';
	
	
	
	if (!filled(form.e_mail)) {
		alert("Email is a mandatory fields.");
		form.e_mail.focus();
		document.getElementById('e_mail').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.email_conf)) {
		alert("Repeat Email is a mandatory fields.");
		form.email_conf.focus();
		document.getElementById('email_conf').style.border = '1px solid #ff9933';
		return false;
	} else if ( !(form.e_mail.value == form.email_conf.value) ){
		alert("The 2 email address inserted are different");
		form.e_mail.focus();
		document.getElementById('e_mail').style.border = '1px solid #ff9933';
		document.getElementById('email_conf').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.password)) {
		alert("password is a mandatory fields.");
		form.password.focus();
		document.getElementById('password').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.conf_pass)) {
		alert("Repeat password is a mandatory fields.");
		form.conf_pass.focus();
		document.getElementById('conf_pass').style.border = '1px solid #ff9933';
		return false;
	} else if ( !(form.password.value == form.conf_pass.value) ){
		alert("The two password are different");
		form.password.focus();
		document.getElementById('password').style.border = '1px solid #ff9933';
		document.getElementById('conf_pass').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.nome)) {
		alert("First name is a mandatory field.");
		form.nome.focus();
		document.getElementById('nome').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.cognome)) {
		alert("Last name is a mandatory field.");
		form.cognome.focus();
		document.getElementById('cognome').style.border = '1px solid #ff9933';
		return false;
	}

	for (var i = form.tipologia.length - 1; i > -1; i--) {
		if (form.tipologia[i].checked) { // Se viene selezionato "Azienda"
			var pos = form.tipologia[i].value;
		}
	}
	if (pos && pos=='azienda') {
		if (!filled(form.ragione_sociale)) {
			alert("Company name is a mandatory field.");
			form.ragione_sociale.focus();
			document.getElementById('ragione_sociale').style.border = '1px solid #ff9933';
			return false;
		}
		if (!filled(form.partita_iva)) {
			alert("VAT number is a mandatory field.");
			form.partita_iva.focus();
			document.getElementById('partita_iva').style.border = '1px solid #ff9933';
			return false;
		}
	} else if (pos && pos=='privato') {
	} else {
		alert("Select Company or person.");
		form.privato.focus();
		document.getElementById('privato').style.border = '1px solid #ff9933';
		return false;
	}

	if (!filled(form.indirizzo)) {
		alert("Address is a mandatory field.");
		form.indirizzo.focus();
		document.getElementById('indirizzo').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.id_nazione)) {
		alert("Country is a mandatory field.");
		form.id_nazione.focus();
		document.getElementById('id_nazione').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.id_provincia)) {
		alert("State is a mandatory field.");
		form.id_provincia.focus();
		document.getElementById('id_provincia').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.citta)) {
		alert("City is a mandatory field.");
		form.citta.focus();
		document.getElementById('citta').style.border = '1px solid #ff9933';
		return false;
	}
	if (form.cap && !filled(form.cap)) {
		alert("Zip code is a mandatory field.");
		form.cap.focus();
		document.getElementById('cap').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.telefono) & !filled(form.cellulare)) {
		alert("At least 1 telephone number is required.");
		form.telefono.focus();
		document.getElementById('telefono').style.border = '1px solid #ff9933';
		document.getElementById('cellulare').style.border = '1px solid #ff9933';
		return false;
	}
	if (!(form.privacy2.checked) ) {
		alert("You must accept our privacy policy.");
		form.privacy2.focus();
		document.getElementById('privacy2').style.border = '1px solid #ff9933';
		return false;
	}
	if (!(form.privacy.checked) ) {
		alert("You must accept our Sales conditions and terms of use.");
		form.privacy.focus();
		document.getElementById('privacy').style.border = '1px solid #ff9933';
		return false;
	}
	if (!filled(form.verifica_cod)) {
		alert("You must insert the security code as you view.");
		form.verifica_cod.focus();
		document.getElementById('verifica_cod').style.border = '1px solid #ff9933';
		return false;
	} else {
		codVer = document.getElementById('verifica_cod');
		c1 = document.getElementById('code').value;
		c2 = document.getElementById('code2').value;
		if (codVer.value != c2 + "R2m" + c1){
			alert("The security code is invalid.");
			form.verifica_cod.focus();
			codVer.style.border = '1px solid #ff9933';
			return false;
		}
	}

	return true;
}