function test(data,mex,error) {
	if (error) {
		$(this).prevUntil("p").addClass("errore").attr("title",errmex).attr("onClick","alert('"+errmex+"')");
	} else {
		$(this).prevUntil("p").removeClass("errore").removeAttr("onclick");
	}
}

function controlla(formname) {
	errore = false;
	formname = "#"+formname;
	
	
	$(formname + " .req").each(function(){
        	if ($(this).val()=="") {
			errmex = "Compila questo campo!";
			$(this).prevUntil("p").addClass("errore").attr("title",errmex).attr("onClick","alert('"+errmex+"')");
			errore = true;
		} else {
			$(this).prevUntil("p").removeClass("errore").removeAttr("onclick");
		}
	});


	$(formname + " .req_chk").each(function(){
		if (!$(this).is(':checked')) {
			alert("Devi accettare "+$(this).attr("title"));
			errore = true;
		}
	});

	$(formname + " .req_num").each(function(){
		if (isNaN($(this).val())||($(this).val()=='')) {
			errmex = "Non è stato inserito un valore corretto!";
			$(this).prevUntil("p").addClass("errore").attr("title",errmex).attr("onClick","alert('"+errmex+"')");
			errore = true;
		} else {
			$(this).prevUntil("p").removeClass("errore").removeAttr("onclick");			
		}
   });

	$(formname + " .req_eml").each(function(){
		var espressione = /^[_a-z0-9+-]+(\.[_a-z0-9+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)+$/;
		if (!espressione.test($(this).val())) {
			errmex = "La mail inserita non è valida!";
			$(this).prevUntil("p").addClass("errore").attr("title",errmex).attr("onClick","alert('"+errmex+"')");
			errore = true;
		} else {
			$(this).prevUntil("p").removeClass("errore").removeAttr("onclick");
		}			
	});	
	
	$(formname + " .req_lng").each(function(){
		vmin = $(this).attr("min");
		vmax = $(this).attr("max");
		if (($(this).val().length>vmax)) {
				errmex = "La lunghezza massima consentita è "+vmax+ " caratteri";
				$(this).prevUntil("p").addClass("errore").attr("title",errmex).attr("onClick","alert('"+errmex+"')");
				errore = true;				
		}
		if (($(this).val().length<vmin)) {
				errmex = "La lunghezza minimia è "+vmin+ " caratteri";
				$(this).prevUntil("p").addClass("errore").attr("title",errmex).attr("onClick","alert('"+errmex+"')");
				errore = true;					
		}		
	});		


	if (errore==true) {
		$('.mex_alert').remove();
		$('#testo').before('<div class="mex_alert">Errore: Compila correttamente tutti i campi</div>');		
		return false;
	} else {
		return true;
	}

}
