$(document).ready(function(){
$("#sendmail").click(function(){prepare_email();return false;});
});

function prepare_email(){
var valid = '';
var isr = ' is required.';
var name = $("#name").val();
var mail = $("#mail").val();
var subject = $("#subject").val();
var text = $("#text").val();
if (name.length<1) {
valid += '<br />Name'+isr;
}
if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
valid += '<br />A valid Email'+isr;
}
if (subject.length<1) {
valid += '<br />Subject'+isr;
}
if (text.length<1) {
valid += '<br />Message'+isr;
}
if (valid!='') {
$("#response").fadeIn("slow");
$("#response").html("Error:"+valid);
}
else {
var datastr ='name=' + name + '&mail=' + mail + '&subject=' + subject + '&text=' + text;
$("#response").css("display", "block");
$("#response").html("Sending message …. ");
$("#response").fadeIn("slow");

setTimeout("send('"+datastr+"')",1000);
}

}

function send(datastr){

$.ajax({
type: "POST",
url: "mail.php",
data: datastr,
cache: false,
success: function(html){
$("#response").fadeIn("slow");
$("#response").html(html);
}
});
}
