function showStatus(error, message) {
	$('#status_bar').attr("class", error ? "box_Message txt_Error" : "box_Message txt_Info").html(message).show();
	scrollToElement($('#status_bar'));
}

function hideStatus() {
	$('#status_bar').hide();
}

function showError(where, message) {
	$('#error_' + where).html(message);
	$('#error_' + where).parent(".box_FormElement").addClass("box_Form_Error");
	scrollToElement($('#error_' + where).parent(".box_FormElement"));
}

function hideError(where) {
	$('#error_' + where).parent(".box_FormElement").removeClass("box_Form_Error");
}

function disableForm() {
	$('#mailForm').find("input, textarea").each(function () { this.disabled = 'disabled'; });
}
	
function enableForm() {
	$('#mailForm').find("input, textarea").each(function () { this.disabled = ''; });
}

function SubmitForm() {
	var selForm = '#mailForm';
	
	hideStatus();

	if ($(selForm).find("input[name='nome']").val() == '') {
		showError('nome', 'Campo "Il tuo nome" non valorizzato');
		$(selForm).find("input[name='nome']").focus();
		return;
	} else {
		hideError('nome');
	}
	
	if ($(selForm).find("input[name='mail']").val() == '') {
		showError('mail', 'Campo "La tua mail" non valorizzato');
		$(selForm).find("input[name='mail']").focus();
		return;
	} else if (!emailCheck($(selForm).find("input[name='mail']").val())) {
		showError('mail', "L'email inserita non \u00E8 un'email corretta");
		$(selForm).find("input[name='mail']").focus();
		return;
	} else {
		hideError('mail');
	}
	
	if ($(selForm).find("input[name='destinatario']:checked").length == 0) {
		showError('destinatario', 'Scegliere il destinatario del messaggio');
		$(selForm).find("input[name='destinatario']:eq(0)").focus();
		return;
	} else {
		hideError('destinatario');
	}
	
	if ($(selForm).find("textarea[name='corpo']").val().match(/[\w]+/) == null) {
		showError('corpo', 'Inserire il testo del messaggio');
		$(selForm).find("textarea[name='corpo']").focus();
		return;
	} else {
		hideError('corpo');
	}
	
	var strData = $(selForm).serialize();
	disableForm();
	showStatus(false, "Invio del messaggio in corso");

	$.ajax({
		url: '/dinamiche/Contattaci',
		data: strData,
		success: function (data) {
			eval(data); // defines status
			if (status.ok) {
				showStatus(false, "Messaggio inviato correttamente");
				$("#mailForm")[0].reset();
			} else if (status.msg) {
				showStatus(true, status.msg);
			} else {
				showStatus(true, "Errore nell'invio del messaggio");
			}
			enableForm();
		},
		error: function () {
			showStatus(true, "Errore nell'invio del messaggio");
			enableForm();
		}
	});
}
