$(document).ready(function() {
	$('#Phone', 'form').change(function() {
		$('.note').remove();
		if($(this).val() != '')
			$('#Phone', 'form').after('<span class="note">Please indicate in your message if you prefer to be contacted by phone or email.</span>');
	});
	
	$('button', 'form').click(function() {
		var speed = 'fast';	// speed for animations
		
		// Slide the Error message up and out of the way, then remove them
		$('#Errors').slideUp(speed, function(){
			$('#Errors').remove();
		});
		
		// Fade the "Required" messages, then remove them
		$('.fail', 'form').fadeOut(speed, function(){
			$('.fail', 'form').remove();
		});
		
		$('button', 'form').hide();		// Hide the Submit button
		
		// Show the Processing message
		$('#ButtonContainer').append('<div id="Processing"><span>Processing request...</span></div>');
		
		// Add a hidden input that tells the PHP Mailer that we are using AJAX
		$('form').append('<input name="ajax" type="hidden" value="true" />');
		
		$.post(
			$('form').attr('action'),					// Post the form to the PHP Mailer
			$('input, textarea').serialize(),			// Turns the inputs into strings of data
			function(response) {
				if(response == 'Success') {
					// Add a message indicating the Email was sent successfully
					$('form').after('<div id="Success"><h2>Your Message was Sent Successfully</h2><p>We will try to send a response within <b>2 business days</b>. For your record, we have sent a copy of your message to the email address you provided.</p><p>Please call us at <b>(307) 577-7094</b> if you would like immediate assistance.</p></div>');
					$('#Success').hide().slideDown(speed);	// Slide the Success Message into place
					$('form').slideUp(speed);			// Slide the form up and out of the way
				} else {
					// Empty Name
					if(response.indexOf('Empty Name') != -1)
						$("label[for='Name']").append('<span class="fail">Required</span>');
					// Empty Email
					if(response.indexOf('Empty Email') != -1)
						$("label[for='Email']").append('<span class="fail">Required</span>');
					// Invalid Email
					else if(response.indexOf('Invalid Email') != -1)
						$("label[for='Email']").append('<span class="fail">Invalid Email</span>');
					// Empty Subject
					if(response.indexOf('Empty Subject') != -1)
						$("label[for='Subject']").append('<span class="fail">Required</span>');
					//Empty Message
					if(response.indexOf('Empty Message') != -1)
						$("label[for='Message']").append('<span class="fail">Required</span>');
					
					// Change the text of the Submit button and show it
					$('button').text('OK, try again now').show();
					
					// Add a message indicating the Email was not sent
					$('form').prepend('<div id="Errors"><h2>Your Message Could not be Sent</h2><ul><li><span>Please fill out all required fields.</span></li><li><span>Check your email address for errors.</span></li><li><span>If you think the form is broken, our email address is <a href="mailto:contact@thecarshopwy.com">contact@thecarshopwy.com</a>.</span></li></ul></div>');
					$('#Errors', 'form').hide().slideDown(speed);
				}
				$('#Processing').remove();				// Hide the Processing message
			});
		return false;
	});
});