// This is to stop MooTools and jQuery having conflict problems. 
// By calling this function, instead of using the '$' operator, you must now use 'jQuery'.
jQuery.noConflict();

 // When the document is ready.
jQuery(document).ready(function() {

	// Function to slide the hidden panel down and the button up.
	function nextStep() {
		jQuery(this).slideUp(500, function() {
			jQuery("#panel").slideDown(500);	
		});
	}

	// Function to slide the inner div up completely, append a loading gif, and perform the AJAX call.
	function submission() {

		// Making the checkbox variable either a one or zero based on it being set or not.
		if (jQuery('#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";
		}

		// Making the checkbox variable either a one or zero based on it being set or not.
		if (jQuery('#send_me_growth_programme_information_pack').attr('checked')) {
			send_me_growth_programme_information_pack = "1"; 
		}
		else {
			send_me_growth_programme_information_pack = "0";
		}

		// Making the checkbox variable either a one or zero based on it being set or not.
		if (jQuery('#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";
		}

		// The advisor field is a set of radio buttons. This code will decipher which radio button has been checked, and use its value.
		advisor = jQuery("input[name='advisor']:checked").val();

		// Slide the inner panel containing all of the form up.
		jQuery("#inner").slideUp(500, function() {

			// Blank any previous error messages and remove the highlight from any erroneous fields.
			jQuery("#errorMessage").html("");
			jQuery("#formHolder").children().removeClass("submissionError");

			// Make the hidden loader panel slide down.
			jQuery("#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.
				jQuery(this).children("img").fadeIn(1000, function() {

					// Perform an AJAX call.
					jQuery.ajax({
						type: "POST",

						url: "ajaxForm.aspx",

						data: "name=" + jQuery('#name').val() + 
							  "&surname=" + jQuery('#surname').val() + 
							  "&email=" + jQuery('#email').val() + 
							  "&phone_number=" + jQuery('#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)
							{
								jQuery("#loaderHolder img").fadeOut(1500, function() {
									jQuery("#loaderHolder").slideUp(500, function() {
										jQuery("#errorMessage").html("<p>Sorry but you need to specify a name.</p>");
										jQuery("#name").addClass("submissionError");
										jQuery("#inner").slideDown(500);
									});
								});
							}

							// If a two was brought back, the surname field was left empty.
							if (data == 2)
							{
								jQuery("#loaderHolder img").fadeOut(1500, function() {
									jQuery("#loaderHolder").slideUp(500, function() {
										jQuery("#errorMessage").html("<p>Sorry but you need to specify a surname.</p>");
										jQuery("#surname").addClass("submissionError");
										jQuery("#inner").slideDown(500);
									});
								});
							}

							// If a three was brought back, the email field was left empty.
							if (data == 3)
							{
								jQuery("#loaderHolder img").fadeOut(1500, function() {
									jQuery("#loaderHolder").slideUp(500, function() {
										jQuery("#errorMessage").html("<p>Sorry but you need to supply an email address.</p>");
										jQuery("#email").addClass("submissionError");
										jQuery("#inner").slideDown(500);
									});
								});
							}

							// If a four was brought back, the email specified was invalid.
							if (data == 4)
							{
								jQuery("#loaderHolder img").fadeOut(1500, function() {
									jQuery("#loaderHolder").slideUp(500, function() {
										jQuery("#errorMessage").html("<p>Sorry but the email address you entered is invalid.</p>");
										jQuery("#email").addClass("submissionError");
										jQuery("#inner").slideDown(500);
									});
								});
							}

							// If a five was brought back, the operation was completed successfully!
							if (data == 5)
							{
								jQuery("#loaderHolder img").fadeOut(1500, function() {
									jQuery("#loaderHolder").slideUp(500, function() {
										jQuery("#enquirySuccess").slideDown(500);
									});
								});
							}

						}
					});

				});

			});

		});
	}

	// Whenever someone clicks the checkbox.
	jQuery("#proceed").click(nextStep);

	// Whenever someone clicks the checkbox.
	jQuery("#foldUp").click(submission);

});