var loginForm;

window.onload = init;

function init() {
	if (document.all) {
		loginForm = document.all["form_reg"];
	} else {
		loginForm = document.getElementById('form_reg');
	}
	loginForm.onsubmit = function () {
		return canSubmit(this);
	}
	//loginForm.email.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) {
	var cnt = -1;
	for (var i = form.tipologia.length - 1; i > -1; i--) {
		if (form.tipologia[i].checked) {
			cnt = i;
			i = -1;
		}
	}
	if (cnt > -1) { 
	} else {
		alert("Devi selezionare il tipo di annuncio");
		return false;
	}

	if (!filled(form.nome)) {
		alert("Il tuo Nome \u00E8 obbligatorio.");
		form.nome.focus();
		document.getElementById('nome').style.border = '1px solid #FF6600';
		return false;
	}
	if (!filled(form.cognome)) {
		alert("Il tuo Cognome \u00E8 obbligatorio.");
		form.cognome.focus();
		document.getElementById('cognome').style.border = '1px solid #FF6600';
		return false;
	}
	if (!filled(form.email)) {
		alert("L'email \u00E8 obbligatoria");
		document.getElementById('email').style.border = '1px solid #FF6600';
		form.email.focus();
		return false;
	} else {
		regExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
   		if (regExp.test(form.email.value)) {
   		} else {
			alert("L'email non risulta valida");
			document.getElementById('email').style.border = '1px solid #FF6600';
			form.email.focus();
			return false;
		}
	}
	if (!filled(form.titolo_annuncio)) {
		alert("Il titolo annuncio \u00E8 obbligatorio.");
		form.titolo_annuncio.focus();
		document.getElementById('titolo_annuncio').style.border = '1px solid #FF6600';
		return false;
	}
	if (!filled(form.richiesta)) {
		alert("Il messaggio \u00E8 obbligatorio");
		form.richiesta.focus();
		return false;
	}
	if (!(form.disclaimer.checked) ) {
		alert("Devi leggere ed accettare le nostre condizioni generali.");
		form.disclaimer.focus();
		document.getElementById('disclaimer').style.border = '1px solid #FF6600';
		return false;
	}
	if (!(form.privacy.checked) ) {
		alert("Devi prestare il consenso al trattamento dei dati personali.");
		form.privacy.focus();
		document.getElementById('privacy').style.border = '1px solid #FF6600';
		return false;
	}
	if (!filled(form.verifica_cod)) {
		alert("Non hai inserito il codice di verifica antirobot in basso nella pagina.");
		form.verifica_cod.focus();
		document.getElementById('verifica_cod').style.border = '1px solid #FF6600';
		return false;
	} else {
		codVer = document.getElementById('verifica_cod');
		c1 = document.getElementById('code').value;
		c2 = document.getElementById('code2').value;
		if (codVer.value != c2 + "R2n" + c1){
			alert("Il codice di verifica antirobot in basso nella pagina \u00E8 errato.");
			form.verifica_cod.focus();
			codVer.style.border = '1px solid #FF6600';
			return false;
		}
	}

	return true;
}
