$(document).ready(function(){
	$("#submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		var fromNameVal = $("#fromName").val();
		if(fromNameVal == '') {
			$("#fromName").after('<span class="error">You forgot to enter your name.</span>');
			hasError = true;
		}
		
		var fromVal = $("#from").val();
		if(fromVal == '') {
			$("#from").after('<span class="error">You forgot to enter your email address.</span>');
			hasError = true;
		} else if(!emailReg.test(fromVal)) {	
			$("#from").after('<span class="error">Enter a valid email address to send from.</span>');
			hasError = true;
		}
		
		var textoVal = $("#texto").val();
		if(textoVal == '') {
			$("#texto").after('<span class="error">You forgot to enter the message.</span>');
			hasError = true;
		}
		
		if(hasError == false) {
			$(this).hide();
			$(".block-message").html('<img src="/imgs/loading.gif" alt="Loading" id="loading" />');
			
			$.post("theMail/sendMail.php",
   				{ from: fromVal, fromName: fromNameVal, texto: textoVal },
   					function(data){
						$("#from").after(sprintf('<span class="status">%s</span>',data));
   					}
				 );
		}
		
		return false;
	});						   
});