var loginForm;

window.onload = init;

function init() {
	loginForm = document.getElementById('form_reg');
	loginForm.onsubmit = function () {
		return canSubmit(this);
	}
}

//Definisce il valore del campo VUOTO
function filled(field) {
	if (field.value == "" || field.value == " " || field.value == null) {
	//if (field.value == 0 || field.value == null) {
		return false;
	} else {
		return true;
	}
}
	
function canSubmit(form) {
	if (!filled(form.nome)) {
		alert("Il tuo nome è obbligatorio.");
		form.nome.focus();
		return false;
	}
	if (!filled(form.cognome)) {
		alert("Il tuo cognome è obbligatorio.");
		form.cognome.focus();
		return false;
	}
	if (!filled(form.e_mail)) {
		alert("La tua email è obbligatoria.");
		form.e_mail.focus();
		return false;
	} else {
		var expr = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
		var mail = document.getElementById('e_mail').value;
		if (!expr.test(mail)) {
			alert("La mail inserita non è valida!");
			return false;
		}
	}
	if (!filled(form.oggetto)) {
		alert("Inserisci l'oggetto della mail.");
		form.oggetto.focus();
		return false;
	}
	if (!filled(form.messaggio)) {
		alert("Inserisci un messaggio per il tuo amico.");
		form.messaggio.focus();
		return false;
	}
	return true;
}