
$(document).ready(function() {
	
	//EMAIL
	
	//attiva stati focus campi testo
	$('#email textarea#message').focus(function() { this.value = this.value=='write message' ? '' : this.value; });
	$('#email input#from').focus(function() { this.value = this.value=='your email' ? '' : this.value; });
	
		//attiva stati blur campi testo
	$('#email textarea#message').blur(function() { this.value = this.value !='' ? this.value : 'write message'; });
	$('#email input#from').blur(function() { this.value = this.value !='' ? this.value : 'your email'; });
	
	//attiva pulsanti invio email
	$("#send-btn").click(function() { if(isvalidform()) sendmail(); else rispondiCosi("There are fields incomplete or incorrect"); });
	
});





function isvalidform() {
	warn = 0;
	
	if($("#from").val() == "your email" || $("#from").val() == "" || !validEmail($("#from").val())) {
		warn++;
	}
	
	if($("#message").val() == "write message" || $("#message").val() == "") {
		warn++;
	}
	
	if($("#tdp").val() != 1)
		warn++;
		
		
	if(warn > 0)
		return false;
	
	return true;
}


function validEmail(email){
	  return  /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+.([a-zA-Z])+([a-zA-Z])+/.test(email);
}



function rispondiCosi(messaggio) {
	$("body").append("<div id='messaggio_popup'>"+messaggio+"</div>");
	$("#messaggio_popup").slideDown().delay(2000).slideUp();
	$("#messaggio_popup").remove;
}




function sendmail() {
	
	params = { from: $("#from").val(), 
			   to: $("#to").val(), 
			   subject: $("#subject").val(), 
			   message: $("#message").val() };

	exit = $.ajax({
					type: 'POST', 
					url: '../lib/sendmail.php', 
					data: params, 
					success:function(){ 
										$("#send-btn").addClass("sent");
									}
					}).responseText;
	
	
	window.setTimeout(function() { 	
									$("#message").val("write message"); 
									$("#from").val("your email");
									$("#tdp").val(0);
									$("#campotdp").removeClass("checked");
									$("#send-btn").removeClass("sent"); 
								}, 5000);

}
