/* ---------------------------------------------------- */
/* JQuery												*/
/* ---------------------------------------------------- */
$jQuery(document).ready(function() 
{
	/* Set the global Ajax defaults */
	$jQuery.ajaxSetup( {
	   timeout: 0,
	   global: true,
	   type: "POST"
 	});
 	
	/* hides all non required displays as soon as the DOM is ready */
	$jQuery('#location_dropdown').hide();
	$jQuery('#section_two').hide();
	$jQuery('#section_three').hide();
	$jQuery('.section_two').hide();
	
	/* Hide and show relevant sections if they click continue step three */	
	$jQuery("#continue_to_two").click(function() { 
		$jQuery("#section_one").hide('slow', function() {
			$jQuery("#section_two").show('slow');
		});
	});
	
	/* Hide and show relevant sections if they click continue step three */	
	$jQuery("#continue_to_three").click(function() { 
		$jQuery("#section_two").hide('slow', function() {
			$jQuery("#section_three").show('slow');
		});
	});
	
	/* Hide and show relevant sections if they click continue step three */	
	$jQuery("#back_to_one").click(function() { 
		/* Hides all required fields in section two */
		hide_section_two();
		$jQuery('#section_one').show('slow');
	});
	
	/* Hide and show relevant sections if they click continue step three */	
	$jQuery("#back_to_two").click(function() { 
		/* Hides all required fields in section two */
		$jQuery('#section_three').hide( function() {
			$jQuery('#section_two').show('slow');
		});
	});
	
	/* Show the location drop-down when query type selected */	
	$jQuery("select#query").change(function(){
		$jQuery("#location_dropdown").show('slow');
		
		/* Hides all required fields in section two */
		hide_section_two();
	});
	
	/* When both query and location selected, hide section 1, and show section 2 */	
	$jQuery("select#location").change(function(){ 
		
		/* Check to see if we have selected a blank option */
		if ( $jQuery(this).val() == 0 )
		{
			/* Hides all required fields in section two */
			hide_section_two();
		}
		else
		{
			
			/* determine which element to show - numbers relate to the id of the section in the database */		
			switch($jQuery("select#query").val())
			{
				case "13":
					$jQuery('#comments_text').html('Any comments relating to your query:');
					$jQuery('#section_two_vehicle_selection').show('slow');
					// Hide the Fleet section if Motability option is selected
					$jQuery('#section_three_fleet').hide();
					break				
			
				case "2": case "3": case "4":
					$jQuery('#comments_text').html('Any comments relating to your query:');
					$jQuery('#section_two_vehicle_selection').show('slow');
					break
				case "9": case "10":
					$jQuery('#comments_text').html('Any comments relating to your query:');
					$jQuery('#section_two_vehicle_owned').show('slow');
					break
				case "8":
					$jQuery('#comments_text').html('Please state the dates you require the vehicle and any other comments relating to your query:');
					$jQuery('#section_two_rental').show('slow');
					break
				case "5":
					$jQuery('#comments_text').html('Please state your vehicle model and registration number; the service you require; the mileage of your vehicle; plus your preferred date and time for this booking:');
					$jQuery('#section_two_servicing').show('slow');
					break				
				default:
					$jQuery('#comments_text').html('Any comments relating to your query:');
					$jQuery('#section_two_default').show('slow');
			}
				
			/* forcibily set the default radio buttons here, as hiding the div in jQuery causes IE6 & 7 to crap itself */
			var counter = 0;
			$jQuery("input[@name='preferred_contact_type']").each(function() {
				/* loops through until it gets to the required radio button, then checks it */
				counter == 1 ? this.checked = true : this.checked = false;
				counter ++;
			});
			/* forcibily set the default radion buttons here, as hiding the div in jQuery causes IE6 & 7 to crap itself */
			var counter = 0;
			$jQuery("input[@name='preferred_contact_time']").each(function() {
				/* loops through until it gets to the required radio button, then checks it */
				counter == 0 ? this.checked = true : this.checked = false;
				counter ++;
			});
			
			$jQuery("#section_one").hide('slow', function() {
				$jQuery("#continue_to_two").show();
				$jQuery("#section_two").show('slow');
			});
		}
		
		
	});
	
	/* Dynamically create the location drop-down from query selection */
	$jQuery("#query").change(function()
	{
		var id = $jQuery(this).val();
		
		if ( id != 0 )
		{
			/* Run the AJAX Request */
			$jQuery.ajax({
				url: 'index.php?action=ajax.getLocations', 
				type: 'POST', 
				dataType: 'xml', 
				data: "id=" + id,
				success: function( xml ){

					/* Define the basic function vars */
					var options = "";
					var query_id = "#";
					var name = "No locations found";
										
					/* Loop through the return result and populate the form values */
					$jQuery("/root/location", xml ).each(function(){
					
						/* Define the page values to use */
						value = $jQuery("query_id", this).text();
						name = $jQuery("name", this).text();
											
						/* append the current options to the options var */
						options += '<option value="' + value + '">' + name + '<\/option>';
					});
					
					/* Lets now populate the select box */
					$jQuery("select#location").html( options );
					$jQuery("#location option:first").attr('selected', true);
				}
			});
		}
		else
		{
			options = '<option value=""><\/option>';
			$jQuery("select#location").html( options );
		}
		
		return false;
	 
	});
	
	/* Runs when the rental period drop-down is changed */
	$jQuery("select.form_element2").change(function()
	{
		var model = $jQuery("#rental_vehicle").val();
		var period = $jQuery("#rental_period").val();
		
		if ( model != 0 && period != "" )
		{
			/* Run the AJAX Request */
			$jQuery.ajax({
				url: 'index.php?action=ajax.getRentalAmounts', 
				type: 'POST', 
				dataType: 'xml', 
				data: "model=" + model + "&period=" + period,
				
				success: function( xml ){

					/* Define the basic function vars */
					var output = "";
					var rate = "#";
					var excess = "#";
										
					/* Loop through the return result and populate the form values */
					$jQuery("/root/display", xml ).each(function(){
						/* Define the page values to use */
						var rate_amount = $jQuery("rate", this).text();
						var excess_amount = $jQuery("excess", this).text();		
						/* append the current options to the options var */
						rate_display = rate_amount;
						excess_display = excess_amount;
					});
					
					/* show the div */
					$jQuery('#rental_header').show('slow');
					/* Lets now populate the select box */
					$jQuery("#rental_rate").html( '&pound;' + rate_display );
					$jQuery("#rental_excess").html( '&pound;' + excess_display );
					
					/* populate the hidden fields */
					$jQuery("#rate").val( rate_display );
					$jQuery("#excess").val( excess_display );
				}
			});
		}
		else
		{
			$jQuery("#rental_rate").html( 'No rates for the options you selected' );
			$jQuery("#rental_excess").html( '' );
			return false;
		}
		
		return false;
	 
	});
});

/* This hides any form options in section two */
function hide_section_two()
{
	$jQuery("#section_two").hide('slow');
	$jQuery('.section_two').hide();
	$jQuery('#rental_header').hide();
	$jQuery("#rental_rate").html( '' );
	$jQuery("#rental_excess").html( '' );
	$jQuery("#rental_vehicle option:first").attr('selected', true);
	$jQuery("#rental_period option:first").attr('selected', true);
	$jQuery("#make").attr('value','');
	$jQuery("#model").attr('value','');
}

/* Validate any required fields */
function CheckFormSection3( form )
{
	var counter=0;
	var  errMsg="The following fields are either missing or invalid:\n";
	
	errMsg += "--------------------------------\n\n"

	/*************** checks ***************/
		if ( form.title.value == "" )
		{
			errMsg += "Title\n";
			counter++;
		}
		if ( form.forenames.value == "" )
		{
			errMsg += "Forename(s)\n";
			counter++;
		}
		if ( form.surname.value == "" )
		{
			errMsg += "Surname\n";
			counter++;
		}
		/* If preferred contact is email, check email address is present and valid */
		if ( form["preferred_contact_type"][0].checked == true )
		{
			if ( form.email.value == "" )
			{
				errMsg += "Email address\n";
				counter++;
			}
			else
			{
				if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value)) {
					errMsg += "Email address is invalid\n";
					counter++;	
				}
			}
		}
		/* If preferred contact is phone, check at least one contact number is entered */
		if ( form["preferred_contact_type"][1].checked == true )
		{
			if ( form.homephone.value == "" && form.workphone.value == "" && form.mobile.value == "" && form.fleetphone.value == "")
			{
				errMsg += "You must enter at least one contact number\n";
				counter++;
			}
		}
		/* If preferred contact is fax, fax number must be entered */
		if ( form["preferred_contact_type"][2].checked == true )
		{
			if ( form.fax.value == "")
			{
				errMsg += "Fax number\n";
				counter++;
			}
		}
	/*************** checks ***************/

	/* return the results */
	if ( counter > 0 )
	{
		alert( errMsg );
		return false;
	}
	
	return true;
}
