$(document).ready(function() {
	$('button').click(function() {
		var $speed = 'fast';	// speed for animations
		$('#Errors').slideUp($speed, function(){
			$('#Errors').remove();
		});
		$('.Fail').fadeOut($speed, function(){
			$('.Fail').remove();
		});
		$('button').hide();
		if ($('#Processing').length == 0)
			$('#ButtonContainer').append('<div id="Processing"><span>Processing request...</span></div>');
		else
			$('#Processing').show();
		$('form').append('<input name="ajax" type="hidden" value="true" />');
		$.post(
			$('form').attr('action'),
			$('input, textarea').serialize(),
			function(response) {
				if(response == 'Success') {
					$('form').after('<div id="Success"><h2>Message <span>Sent Successfully</span></h2><p>We will try to send a response within <b>2 days</b>. For your record, we have sent a copy of your message to the email address you provided.</p></div>');
					$('form').slideUp($speed);
					$('#Success').slideDown($speed);
				} else {
					if(response.indexOf('Empty Name')!=-1)
						$('#Name').after('<span class="Fail">Required</span>');
					if(response.indexOf('Empty Email')!=-1)
						$('#Email').after('<span class="Fail">Required</span>');
					else if(response.indexOf('Invalid Email')!=-1)
						$('#Email').after('<span class="Fail">Invalid Email</span>');
					if(response.indexOf('Empty Subject')!=-1)
						$('#Subject').after('<span class="Fail">Required</span>');
					if(response.indexOf('Empty Message')!=-1)
						$('#Message').after('<span class="Fail">Required</span>');
						
					$('button').text('OK, try again now').show();
					$('#Errors').slideDown($speed);
				}
				$('#Processing').hide();
			});
		return false;
	});
});