function contatore(val) {
	var massimo=2000;
	var rest;
	if (val.comment.value.length > massimo) {
		val.comment.value = val.comment.value.substring(0,massimo);
		rest = 0;
	}else{
		rest = massimo - val.comment.value.length;
	}
	val.num.value = rest;

}


function LunghezzaMax(campo){ 
	if (campo.value.length>2000){ 
		alert("Inserire al massimo 2000 caratteri nella textarea."); 
		campo.value=campo.value.substring(0, 2000); 
	} 
} 

function contatoreContatti(val) {
	var massimo=2000;
	var rest;
	if (val.wpcf_msg.value.length > massimo) {
		val.wpcf_msg.value = val.wpcf_msg.value.substring(0,massimo);
		rest = 0;
	}else{
		rest = massimo - val.wpcf_msg.value.length;
	}
	val.num.value = rest;

}

function valida_form_contatti(mf){
	if(window.RegExp){
		var rexp=new RegExp("^[_a-zA-Z0-9-]+(\\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*(\\.([a-zA-Z]){2,4})$");
		if(!rexp.test(mf.email.value)) {
			alert("L\'e-mail deve avere questo formato: username@dominio.it \r\n\""+mf.email.value+"\" non č un indirizzo valido. Verificalo e prova di nuovo.");
			mf.email.focus();
			return false;
		};
	}else{
		if(!(mf.email.value.indexOf("@") > 0)){
			alert("L\'e-mail deve avere questo formato: username@dominio.it \r\n'"+mf.email.value+"' non č un indirizzo valido. Verificalo e prova di nuovo.");
			mf.email.focus();
	      	return false;
		}
	}	
	tmp = mf.messaggio.value.trim();
	if(tmp.length < 1){
		alert("Inserisci il Messaggio.");
		mf.messaggio.focus();
		return false;
	}	
	
	tmp1 = mf.nome.value.trim();
	if(tmp1.length < 1){
		alert("Inserisci il tuo nome.");
		mf.nome.focus();
		return false;
	}	
	
	tmp2 = mf.cognome.value.trim();
	if(tmp2.length < 1){
		alert("Inserisci il tuo cognome.");
		mf.cognome.focus();
		return false;
	}
	
	tmp3 = mf.citta.value.trim();
	if(tmp3.length < 1){
		alert("Inserisci la tua cittā.");
		mf.citta.focus();
		return false;
	}	
	tmp4 = mf.nazione.value.trim();
	if(tmp4.length < 1){
		alert("Inserisci la tua nazione.");
		mf.nazione.focus();
		return false;
	}	
	tmp5 = mf.cap.value.trim();
	if(tmp5.length < 1){
		alert("Inserisci il tuo codice di avviamento postale.");
		mf.cap.focus();
		return false;
	}	

	tmp6 = mf.indirizzo.value.trim();
		if(tmp6.length < 1){
			alert("Inserisci il tuo indirizzo completo.");
			mf.indirizzo.focus();
			return false;
		}	
	

	
	if(!mf.consenso[0].checked){
		alert("Per poter inviare il messaggio devi dare il consenso per il trattamento dei dati personali.");
		mf.consenso[0].focus();
		return false;
	}	
	if(!mf.consenso2[0].checked){
		alert("Per poter inviare il messaggio devi dare il consenso per il trattamento dei dati sensibili.");
		mf.consenso2[0].focus();
		return false;
	}	
	return true;
}
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
