jQuery(document).ready(function($) {


/* Ajax Contact form validation and submit */
$('form#contactForm').submit(function() {
	$('form#contactForm .error').remove();
	var hasError = false;
	$('.requiredField').each(function() {
		$(this).parent().parent().find('.field-error').remove();
		if(jQuery.trim($(this).val()) == '') {
			$(this).addClass('input-error');
			hasError = true;
		} else if($(this).hasClass('email')) {
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
			if(!emailReg.test(jQuery.trim($(this).val()))) {
				$(this).addClass('input-error');
				hasError = true;
			}
		}
	});
	if(!hasError) {
		$('form#contactForm #submit').fadeOut('normal', function() {
			$(this).parent().append('<p class="highlight">Sending your message...</p>');
		});
		var formInput = $(this).serialize();
		$.ajax({
			type: "POST",
		   	url: $(this).attr('action'),
		   	data: formInput,
		   	success: function(data){
				$('#contact-form').fadeOut("normal", function() {				   
					$(this).before('<p class="highlight"><strong>Thanks!</strong> Your email was successfully sent. I check my email all the time, so I should be in touch soon.</p>');
				});
		   	},
			error: function(data){
				$('#contact-form').fadeOut("normal", function() {
					$(this).before('<p class="highlight"><strong>There was an error sending your message.</strong> Please try again later.</p>');
				});
		   	}
		});
	}
	
	return false;
	
});

$('.requiredField').blur(function() {
	if(jQuery.trim($(this).val()) != '' && !$(this).hasClass('email')) {
		$(this).removeClass('input-error');
	} 
});

$('.email').blur(function() {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(emailReg.test(jQuery.trim($(this).val()))) {
		$(this).removeClass('input-error');
	} 
});
});
