$(function() {

	$('.formComponent').focus(function() {
		$(this).val('');
	});
	
	$('.formComponent').blur(function() {
		if ($(this).val() == "") {
			if ($(this).attr('id') == "name") {
				$(this).val('Your Name')
			} 
			else if ($(this).attr('id') == "comment") {
				$(this).val('Comments')
			}
			else if ($(this).attr('id') == "zip") {
				$(this).val('Your Zip Code')
			} 
			else if ($(this).attr('id') == "email") {
				$(this).val('Your Email Address')
			}
		}
	});
	
	$('#submit').click(function() {
		$('.error').hide().remove();
	
		var name = $('#name').val(),
			email = $('#email').val(),
			zip = $('#zipcode').val(),
			comment = $('#comment').val();
			
		var error_count = 0;
		
		var email_regex = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		if ((name == "") || (name == "Your Name")) {
			$('#contact_header').html('<p class="error">Please enter your name.</p>');
			error_count += 1;
		}
		
		if (zip.length != 5) {
			$('#contact_header').html('<p class="error">Please enter a 5 digit zip code.</p>');
			error_count += 1;		
		}
		
		//Test Email
		if (!email_regex.test(email)) {
			$('#contact_header').html('<p class="error">Please enter a real email address, bozo.</p>');
			error_count += 1;
		}

		//No Errors?
		if(error_count === 0) {
			$.ajax({
				type: "post",
				url: "send.php",
				data: "name=" + name + "&email=" + email + "&zip=" + zip + "&comment=" + comment,
				error: function() {
					$('.error').hide();
					$('#sendError').slideDown('slow');
				},
				success: function () {
					$('.error').hide();
					$('.success').slideDown('slow');
					$('form#contactForm').fadeOut('slow');
				}				
			});	
		}
		
		else {
			$('.error').show();
		}
			
		return false;
	});
	
});
