$(document).ready(function(){
	
	$("#sendmail").click(function(){
		var valid = '';
		var errcount = 0;
		var isr = "a value is required.";
		var name = $("#name").val();
		var dept = $("#dept").val();
		var phone = $("#phone").val();
		var email = $("#email").val();
		var question = $("#question").val();
		var secret = $("#secret").val();
		var spam = $("#state").val();
		
		$(":input").removeClass("form_error");
		
		if(name.length < 1){
			valid += "<li>Name - "+ isr + "</li>";
			$("#name").addClass("form_error");
			errcount++;
		}
		if(email.length < 1){
			valid += "<li>Email - "+ isr + "</li>";
			$("#email").addClass("form_error");
			errcount++;
		}
		if((email.length) && (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i))){
			valid += "<li>Email - invalid email address</<li>";
			$("#email").addClass("form_error");
			errcount++;
		}
		if(question.length < 1){
			valid += "<li>Question - "+ isr + "</li>";
			$("#question").addClass("form_error");
			errcount++;
		}
		if((secret.length < 1) || (secret.length > 2) || (!secret.match(/[0-9]/))){
			valid += "<li>Verification failed.  Try again.</li>";
			$("#secret").addClass("form_error");
			errcount++;
		}
		if(valid!=''){
			$("#form_notice").fadeIn("slow");
			$("#form_notice").html("<p id=\"error\"></p><p>Your form contains "+ errcount +" errors.  Please correct the highlighted fields and send again.</p><ul>"+ valid + "</ul>");
		}
		else{
			var datastr ="&name=" + name + "&email=" + email + "&dept=" + dept + "&phone=" + phone + "&question=" + question + "&secret=" + secret + "&spam=" + spam;
			$("#form_response").css("display", "block");
			$("#form_response").html("<p>Sending message .... </p>");
			$("#form_response").fadeIn("slow");
			setTimeout("send('"+ datastr +"')",2000);
		}
	return false;
	});
});

function send(datastr){
	$.ajax({
		type: "POST",
		url: "contact/mail.php",
		data: datastr,
		cache: false,
		success: function(html){
		$("#form_response").fadeIn("slow");
		$("#form_response").html("<p id=\"success\"></p><p>Your message has been sent!</p>");
		setTimeout('$("#form_response").fadeOut("slow")',2000);
	}
	});
}