$(document).ready(function(){
	$("#submit").click(function(){					   				   
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		/*var emailToVal = $("#emailTo").val();
		if(emailToVal == '') {
			$("#emailTo").after('<span class="error">You forgot to enter the email address to send to.</span>');
			hasError = true;
		} else if(!emailReg.test(emailToVal)) {	
			$("#emailTo").after('<span class="error">Enter a valid email address to send to.</span>');
			hasError = true;
		}*/
		
		var emailFromVal = $("#emailFrom").val();
		if(emailFromVal == '') {
			$("#emailFrom").after('<span class="error">You forgot to enter your e-mail address.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {	
			$("#emailFrom").after('<span class="error">Your e-mail address doesn\'t seem to be valid.</span>');
			hasError = true;
		}
		
	/*	var subjectVal = $("#subject").val();
		if(subjectVal == '') {
			$("#subject").after('<span class="error">You forgot to enter a subject.</span>');
			hasError = true;
		}*/
		
		var messageVal = $("#message").val();
		if(messageVal == '') {
			$("#message").after('<span class="error">You forgot to enter a message.</span>');
			hasError = true;
		}
		
	var firstnameVal = $("#firstname").val();
		if(firstnameVal == '') {
			$("#firstname").after('<span class="error">You forgot your name. I mean, you forgot to input your name.</span>');
			hasError = true;
		}
		
		var cityVal = $("#city").val();
		if(cityVal == '') {
			$("#city").after('<span class="error">You forgot your city.</span>');
			hasError = true;
		}
		
		var stateVal = $("#state").val();
		if(stateVal == '') {
			$("#state").after('<span class="error">You forgot your state.</span>');
			hasError = true;
		}
		
		
		
		
		
		if(hasError == false) {
			$(this).hide();
			$("#sendEmail li.buttons").append('<img src="/images/loading.gif" alt="Loading" id="loading" />');
			
			$.post("/sendemail.php",
   				{ emailFrom: emailFromVal, firstname: firstnameVal, city: cityVal, state: stateVal, message: messageVal },
   					function(data){
						$("#sendEmail").slideUp("normal", function() {				   
							
							$("#sendEmail").before('<div class="success"><h1>Success!</h1><p>Your dead letter was sent!</p></div>');											
						});
   					}
				 );
		}
		
		return false;
	});						   
});
