// Controlla i campi form con classe "obb" e imposta lo sfondo rosso per evidenziare errore

//////////////////////////////////////////////
// Comportamento in caso di errore
//////////////////////////////////////////////
function setError (id_campo) {
	$('#'+id_campo).addClass("error");
}
function removeError (id_campo) {
	$('#'+id_campo).removeClass("error");
}


//////////////////////////////////////////////
// Controllo estensione
//////////////////////////////////////////////
function controllo_estensione(file, tipo){

if(tipo == 'img'){var reg_ext = new RegExp("(jpg)$|(jpeg)$","");}
if(tipo == 'file'){var reg_ext = new RegExp("(doc)$|(pdf)$|(txt)$","");}

var estensione = file.toLowerCase();

if(!estensione.match(reg_ext)){return false;}
else{return true;}
}


//////////////////////////////////////////////
// Controllo campi
//////////////////////////////////////////////
function controllo_invio(modulo) {

	var reg_email = new RegExp("^([-_.#$&a-zA-Z0-9]+)[@][-_.a-zA-Z0-9]+(\\.[a-zA-Z]{2,4})(\\.[a-zA-Z]{2,4})?$","");
	messaggi = new Array();
	messaggi['nome'] = 'Il campo Nome e Cognome &egrave; obbligatorio.<br />';
	messaggi['indirizzo'] = 'Il campo Indirizzo; &egrave; obbligatorio.<br />';
	messaggi['citta'] = 'Il campo Citt&agrave; &egrave; obbligatorio.<br />';
	messaggi['telefono'] = 'Il campo Telefono &egrave; obbligatorio.<br />';
	messaggi['posta_e'] = 'Il campo Email &egrave; obbligatorio.<br />';
	messaggi['richieste'] = 'Il campo Richieste &egrave; obbligatorio.<br />';
	messaggi['note'] = 'Il campo Note &egrave; obbligatorio.<br />';
	messaggi['privacy'] = '&Egrave; necessario accettare le condizioni di trattamento dei dati personali.<br />';
	messaggi['curriculum'] = '&Egrave; necessario allegare il curriculum.<br />';
	
	
	id_form = '#'+$(modulo).attr("id");
	avv = '';
	
	//Controllo tutti i campi con classe "obb"
	$(id_form).find(".obb").each(function () {
		if (!$(this).val()) {
			avv += messaggi[$(this).attr("name")];
			setError($(this).attr("id"));
		} else {
			//se è campo speciale 
			if ($(this).attr("name") == "telefono") {if(isNaN($(this).val())){avv += 'Devi inserire il Telefono in formato numerico e senza spazi.<br />';setError($(this).attr("id"));}}
			if($(this).attr("name") == "posta_e") {if (!$(this).val().match(reg_email)){avv+= 'Il campo E-mail deve contenere un indirizzo corretto.<br />';setError($(this).attr("id"));}}
			if($(this).attr("name") == "curriculum") {if (!controllo_estensione($(this).val(), "file")){avv+= 'Il curriculum &egrave; in un formato non consentito.<br />';setError($(this).attr("id"));}}
			if($(this).attr("name") == "privacy") {if (!$("#privacy:checked").val()){avv+= messaggi[$(this).attr("name")];}}
		}
	});
		
	$(".obb").change(function () {
		if ($(this).val() != '' && $(this).val() != 0 ) {
			removeError($(this).attr("id"));
		}
	})

	if (avv) {
		$(id_form+" fieldset").append('<div id="errori" style="clear: both; display: none;">'+avv+'</div>');
		$("#errori").fadeIn();
		azione = '$("#errori").fadeOut().remove()';
		setTimeout(azione, 3000);
	} else {
		modulo.submit();
	}
	
}
