// When the document is ready.
$(document).ready(function() {

	// Slides the hidden panel down after fading out the next button.
	function frontNextStep() {
		$(this).fadeOut(800, function() {
			$("#panel").slideDown(500);	
		});
	}

	// Animates and submits the form using an AJAX call.
	function frontSubmission() {

		// Make a variable 1 or 0 depending if the checkbox was set or not.
		if ($('#send_me_a_go_for_it_information_pack').attr('checked')) {
			send_me_a_go_for_it_information_pack = "1"; 
		}
		else {
			send_me_a_go_for_it_information_pack = "0";
		}

		// Make a variable 1 or 0 depending if the checkbox was set or not.
		if ($('#send_me_growth_programme_information_pack').attr('checked')) {
			send_me_growth_programme_information_pack = "1"; 
		}
		else {
			send_me_growth_programme_information_pack = "0";
		}

		// Make a variable 1 or 0 depending if the checkbox was set or not.
		if ($('#send_details_of_closest_go_for_it_advice_centre').attr('checked')) {
			send_details_of_closest_go_for_it_advice_centre = "1"; 
		}
		else {
			send_details_of_closest_go_for_it_advice_centre = "0";
		}

		// Deciphers which radio button has been checked, then uses its value.
		advisor = $("input[name='advisor']:checked").val();

		// Fade out the submit button.
		$("#foldUp").fadeOut(800, function() {

			// Slide the inner panel containing all of the form (and the submit button) up.
			$("#inner").slideUp(800, function() {

				// Blank any previous error messages.
				$("#errorMessage").html("");

				// Target all elements with this class, and then remove it.
				$(".submissionError").removeClass("submissionError");

				// Make the hidden loader panel slide down.
				$("#loaderHolder").slideDown(500, function() {

					// The image is preloaded as it was already in the DOM, just hidden with CSS. This fades it back onto the screen.
					$(this).children("img").fadeIn(800, function() {

						// Perform an AJAX call.
						$.ajax({
							type: "POST",

							url: "ajaxForm.aspx",

							data: "name=" + $('#name').val() + 
								  "&surname=" + $('#surname').val() + 
								  "&email=" + $('#email').val() + 
								  "&phone_number=" + $('#phone_number').val() + 
								  "&advisor=" + advisor + 
								  "&send_me_a_go_for_it_information_pack=" + send_me_a_go_for_it_information_pack + 
								  "&send_me_growth_programme_information_pack=" + send_me_growth_programme_information_pack + 
								  "&send_details_of_closest_go_for_it_advice_centre=" + send_details_of_closest_go_for_it_advice_centre,

							success: function(data, textStatus) {

								// If a one was brought back, the name field was left empty.
								if (data == 1)
								{
									$("#loaderHolder img").fadeOut(1000, function() {
										$("#foldUp").css({display: "block"});
										$("#errorMessage").css({display: "block"});
										$("#loaderHolder").slideUp(500, function() {
											$("#errorMessage").html("<p>Sorry but you need to specify a name.</p>");
											$("#name").addClass("submissionError");
											$("#inner").slideDown(500);
										});
									});
								}
	
								// If a two was brought back, the surname field was left empty.
								if (data == 2)
								{
									$("#loaderHolder img").fadeOut(1500, function() {
										$("#foldUp").css({display: "block"});
										$("#errorMessage").css({display: "block"});
										$("#loaderHolder").slideUp(500, function() {
											$("#errorMessage").html("<p>Sorry but you need to specify a surname.</p>");
											$("#surname").addClass("submissionError");
											$("#inner").slideDown(500);
										});
									});
								}
	
								// If a three was brought back, the email field was left empty.
								if (data == 3)
								{
									$("#loaderHolder img").fadeOut(1500, function() {
										$("#foldUp").css({display: "block"});
										$("#errorMessage").css({display: "block"});
										$("#loaderHolder").slideUp(500, function() {
											$("#errorMessage").html("<p>Sorry but you need to supply an email address.</p>");
											$("#email").addClass("submissionError");
											$("#inner").slideDown(500);
										});
									});
								}
	
								// If a four was brought back, the email specified was invalid.
								if (data == 4)
								{
									$("#loaderHolder img").fadeOut(1500, function() {
										$("#foldUp").css({display: "block"});
										$("#errorMessage").css({display: "block"});
										$("#loaderHolder").slideUp(500, function() {
											$("#errorMessage").html("<p>Sorry but the email address you entered is invalid.</p>");
											$("#email").addClass("submissionError");
											$("#inner").slideDown(500);
										});
									});
								}
	
								// If a five was brought back, the operation was completed successfully!
								if (data == 5)
								{
									$("#loaderHolder img").fadeOut(1500, function() {
										$("#foldUp").css({display: "block"});
										$("#errorMessage").css({display: "none"});
										$("#loaderHolder").slideUp(500, function() {
											$("#enquirySuccess").slideDown(500);
										});
									});
								}

							}
						});

					});

				});

			});
		});
	}


	// Whenever someone clicks the checkbox.
	$("#frontEnquiryContainer #proceed").click(frontNextStep);

	// Whenever someone clicks the checkbox.
	$("#frontEnquiryContainer #foldUp").click(frontSubmission);

});