// JavaScript Document

	var journeyCalculated = false;

	var strFromOrTo = "to";
	var strOutOrBack = "outbound";
	
	var fromLongLat;
	var toLongLat;
	
	var journey_duration_seconds = 0;
	
	var directions_mode = "base_pickup_dropoff_base";
	
	var fare_minimum = 0;
	var fare_starting = 0;
	
	var postcode_multiplier = 1.0;
	var discount_value = 0.0;
	var discount_percent = 0.0;
	
	var daisychain_postcodeMultiplier = false;
	

	function notifyJourneyLookup(fare)
	{
	//	alert("populate destination");
	
	
		actionAjax("journey_lookup_notify.php?from=" + document.form1.txt_from.value + "&to=" + document.form1.txt_to.value + "&date=" + document.form1.date_outbound.value + "&time=" + document.form1.time_outbound.value + "&return=" + document.form1.chk_return.checked + "&Chargeble_Mileage=" + document.form1.chargeable_miles.value + "&Fare=" + fare, notifyJourneyLookup_response);
			
			
	}
	
	function notifyJourneyLookup_response()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var response =  xmlHttp.responseText;
//			alert("@" + xmlHttp.responseText + "@");
		}
		
	}

	function lookupPostcodeFareMultiplier()
	{
	//	alert("Postcode Multiplier Lookup");
//	alert(document.form1.to_text.value);
		actionAjax("postcode_specific_multiplier.php?dropdown_pickup=" + document.form1.from_dropdown.value + "&dropdown_dropoff=" + document.form1.to_dropdown.value + "&postcode_pickup=" + document.form1.from_text.value + "&postcode_dropoff=" + document.form1.to_text.value , lookupPostcodeFareMultiplier_response);
			
			
	}
	
	function lookupPostcodeFareMultiplier_response()
	{
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
		{ 
			var response =  xmlHttp.responseText;
//			alert(response);
			
			
			response = response.split(",");
//alert(response);
			if (response[0] > 0 )
			{
				postcode_multiplier = response[0];
				discount_value = response[1];
				discount_percent = response[2];

			}
			
			//Now calcualte the fare		
			calculateFare();

				
//			alert("@" + xmlHttp.responseText + "@");
		}
		
	}


	function validate_reset()
	{
		document.form1.from_text_line1.className = "";
		document.form1.to_text_line1.className = "";
		document.form1.from_text.className = "";
		document.form1.to_text.className = "";

		document.form1.date_outbound.className = "";
		document.form1.date_return.className = "";
		document.form1.time_outbound.className = "";
		document.form1.time_return.className = "";

		
	}
	
	function validate_stage2() //Secondary validate function. This time called before the journey is finalised. For example, calculation can be made with just a postcode outcode but bookign requires greater address details
	{
		validate_reset();
		
		text = "";
		twentyfourhour_text = "";
		if (document.form1.date_outbound.value != "" && document.form1.time_outbound.value != "")
		{
			engDate = document.form1.date_outbound.value;
			splitDate = engDate.split("/");
			amDate = splitDate[1] + "/" + splitDate[0] + "/" + splitDate[2];

			var enteredDate = new Date(amDate + " " + document.form1.time_outbound.value);
			var nowDate = new Date();
			if (enteredDate.getTime() - nowDate.getTime() < 86400000)
			{
				twentyfourhour_text += "To book online, your journey date / time must be more than 24 hours in advance. For more immediate bookings, call our friendly team now on 0844 335 0705.\n\n From outside the UK, call +44 1293 774 118\n";
			}
			

		}
		if (document.form1.date_return.value != "" && document.form1.time_return.value != "")
		{
			engDate = document.form1.date_return.value;
			splitDate = engDate.split("/");
			amDate = splitDate[1] + "/" + splitDate[0] + "/" + splitDate[2];

			var enteredDate = new Date(amDate + " " + document.form1.time_return.value);
			var nowDate = new Date();
			if (enteredDate.getTime() - nowDate.getTime() < 86400000)
			{
				twentyfourhour_text += "To book online, your return journey date / time must be more than 24 hours in advance. For more immediate bookings, call our friendly team now on 0844 335 0705. \n\nFrom outside the UK, call +44 1293 774 118\n";
			}
			

		}


		if (document.form1.from_using_postcode.value == 1)
		{
			if ( document.form1.from_text_line1.value == "")
			{			
				text += "- Enter a valid pickup address\n";
				document.form1.from_text_line1.className = "validate_error";
			}
		}
		
		if (document.form1.to_using_postcode.value == 1)
		{
			if (document.form1.to_text_line1.value == "")
			{			
				text += "- Enter a valid dropoff address \n";
				document.form1.to_text_line1.className = "validate_error";
			}
		}
		

		if (text != "")
		{
			text = "Please correct the following problems:\n\n" + text + "\n\n" + twentyfourhour_text;	
			alert(text);
			return false;
		}
		else if (twentyfourhour_text != "")
		{
			alert(twentyfourhour_text);
			return false;
		}
		
		return true;

	}
	
	
	function validate_journeyReady()
	{
		validate_reset();

		text = "";


		//If we are in airport transfer mode but there are no airports, disaply error
		if (document.form1.taxi_mode.value == "airport" && (document.form1.from_radio[0].checked || document.form1.to_radio[0].checked) == false)
		{
			text += "- An airport transfer must pick up or drop off at an airport\n\n";
//			alert("Airport Transfer but no airports.");	
		}
		
		if (document.form1.from_using_postcode.value == "")
		{
			text += "- Choose a pickup location\n";
		}
		else if (document.form1.from_using_postcode.value == 1)
		{
/*			if (document.form1.from_text_line1.value == "" )
			{
				text += "- Enter a pickup address\n";
				document.form1.from_text_line1.className = "validate_error";
			}*/
			if (document.form1.from_text.value == "" && document.form1.from_text_line1.value == "")
			{			
				text += "- Enter a valid pickup and / or postcode\n";
				document.form1.from_text.className = "validate_error";
				document.form1.from_text_line1.className = "validate_error";
			}
		}

		if (document.form1.to_using_postcode.value == "")
		{
			text += "- Choose a dropoff location\n";
		}
		else if (document.form1.to_using_postcode.value == 1)
		{
			/*if (document.form1.to_text_line1.value == "" )
			{
				text += "- Enter a dropoff address\n";
				document.form1.to_text_line1.className = "validate_error";
			}*/
			if (document.form1.to_text.value == "" && document.form1.to_text_line1.value == "")
			{			
				text += "- Enter a valid dropoff location and / or postcode\n";
				document.form1.to_text.className = "validate_error";
				document.form1.to_text_line1.className = "validate_error";
			}
		}

		if (document.form1.date_outbound.value == "" )
		{
			text += "- Enter an outbound date\n";
			document.form1.date_outbound.className = "validate_error";
		}
		if (document.form1.time_outbound.value == "" )
		{
			text += "- Enter an outbound time\n";
			document.form1.time_outbound.className = "validate_error";
		}

		
		

		if (document.form1.chk_return.checked)
		{
		
			if (document.form1.date_return.value == "" )
			{
				text += "- Enter a return date (or untick the return journey option)\n";
				document.form1.date_return.className = "validate_error";
			}
			if (document.form1.time_return.value == "" )
			{
				text += "- Enter a return time (or untick the return journey option)\n";
				document.form1.time_return.className = "validate_error";
			}

	
			
		}

		if (text != "")
		{
			text = "Please correct the following problems:\n\n" + text;	
			alert(text);
			return false;
		}
		
		return true;
	
	
//		if (document.form1.)
		
		
	}
	
	function clearJourney()
	{
		var objJourneyDetails = document.getElementById("panel_display_journey_details_and_map");
		var objVehicleTypes = document.getElementById("vehicle_types")

		var objSpecials = document.getElementById("panel_airport_right");
		if (objSpecials != null)
			objSpecials.style.display= "block";
		


		if (objJourneyDetails != null)
			objJourneyDetails.style.visibility = "hidden";
			
		if (objVehicleTypes != null)
			objVehicleTypes.style.display = "none";
			
		postcode_multiplier = 1.0;
		
		daisychain_postcodeMultiplier = false;
		
	}
	
	function calculateJourney()
	{
	//	alert("clac journy");	

		var objSpecials = document.getElementById("panel_airport_right");
		if (objSpecials != null)
			objSpecials.style.display= "none";


		if (validate_journeyReady() == false)
		{
			clearJourney();
			return;
		}
		document.getElementById("panel_display_journey_details_and_map").style.visibility = "visible";


		journeyCalculated = false;
		
		//Set directions mode to initial base to pickup to drop off back to base / 2 mode
		directions_mode = "base_pickup_dropoff_base";
		toReady = false;
		fromReady = false;
		
		daisychain_postcodeMultiplier = true;
		populateVehicleTypes();


		calculateJourneyLength();
	}
	

	function calculateJourneyLength()
	{

		//NOTE ifs must be strcutured in this way to allow fromReady and toReady to be set before any Google callbacks occur (otherwsie calcaultion may never be performed)	
		usePointFromPostcode(document.form1.txt_from.value, "from", postCodeTranslated)	;
		
		//If to and from are both ready (should only be called here if both from and to are chosen from dropdown rather than having to be geoencoded
		if (toReady && fromReady)
		{
			directions.load(fromLongLat + " to " + toLongLat);
		}
		
	
	}	

	
	
	function addJourney(itemID, price, vehicle_type)
	{
		//alert("add");
		var comma = /,/g;
//		alert(googleDescription_from.replace(comma, " -"));
		var from = googleDescription_from.replace(comma, " -");
		var to = googleDescription_to.replace(comma, " -");

		document.form1.optionTypeID_1.value = from + ",0.00,Outbound Pickup Point,0";
		document.form1.optionTypeID_2.value = to + ",0.00,Outbound Dropoff Point,0";
		document.form1.optionTypeID_3.value = document.form1.date_outbound.value + ",0.00,Outbound Pickup Date,0";
		document.form1.optionTypeID_4.value = document.form1.time_outbound.value + ",0.00,Outbound Pickup Time,0";

		if (document.form1.chk_return.checked)
		{
			document.form1.optionTypeID_5.value = document.form1.date_return.value + ",0.00,Return Pickup Date,0";
			document.form1.optionTypeID_6.value = document.form1.time_return.value + ",0.00,Return Pickup Time,0";
		}
		else
		{
			document.form1.optionTypeID_5.value = "n/a,0.00,Return Pickup Date,0";
			document.form1.optionTypeID_6.value = "n/a,0.00,Return Pickup Time,0";
		}
		
		document.form1.optionTypeID_7.value = document.form1.no_passengers.value + ",0.00,No. of Passengers,0";
		document.form1.optionTypeID_8.value = vehicle_type + ",0.00,Vehicle Type,0";
//alert(document.form1.optionTypeID_2.value );


document.form1.doWhat.value = "add";
	document.form1.thingID.value = itemID;
//	document.form1.thingName.value = 
	document.form1.rrp.value = price;
	document.form1.action = "journey_basket.php";

	//Show final questions in js popup panel
	if (validate_stage2())	
	{
		var objQuestions = document.getElementById("objQuestions");
		objQuestions.style.display = "block";
	}

//	document.form1.submit();

//Form will now submit itself!
	}

function updateOption(optionID, optionLabel, optionValue)
{
	var objOptionID = document.getElementById(optionID);
	
	objOptionID.value = optionValue + ",0.00," + optionLabel + ",0";
}

function confirmBooking()
{
	document.form1.submit();	
}

function hideBookingConfirmation()
{
	//Hide final questions in js popup panel
	var objQuestions = document.getElementById("objQuestions");
	objQuestions.style.display = "none";

}

function g_directionsLoaded()
{
	
	if (directions_mode == "base_pickup_dropoff_base")
	{

		var dist = (((directions.getDistance().meters / 1000) * 0.621371192)/2.0).toFixed(1);
		var duration = directions.getDuration().html;
 		document.form1.chargeable_miles.value = dist;
		

		//Now load the directions to display to the user on map
		directions_mode ="pickup_dropoff";

	
		directions.load("from: " + fromLongLat + " to: " + toLongLat );



	}
	else //Now lets calcualte the fare
	{
	
		var dist = ((directions.getDistance().meters / 1000) * 0.621371192).toFixed(1);
		var duration = directions.getDuration().html;
		journey_duration_seconds = directions.getDuration().seconds;
		
		var td_dist = document.getElementById("td_distance");
		td_dist.innerHTML = dist + " miles";
	
		var td_duration = document.getElementById("td_journey_length");
		td_duration.innerHTML = duration + " ";
		
		//Lookup any postcode specific pricing
		lookupPostcodeFareMultiplier();
//		calculateFare(); 
	
		
	}
	

}


function calculateMinimumAndStarting()
{
	
	var global_starting_rate = parseFloat(document.form1.global_starting_rate.value);
	var global_minimum_fare = parseFloat(document.form1.global_minimum_fare.value);

	var starting = global_starting_rate;
	var minimum = global_minimum_fare;
	
	var starting_from = parseFloat(document.form1.from_starting.value);
	if (starting_from > starting)
		starting = starting_from;

	var starting_to = parseFloat(document.form1.to_starting.value);
	if (starting_to > starting)
		starting = starting_to;

	var minimum_from = parseFloat(document.form1.from_minimum.value);
	if (minimum_from > minimum)
		minimum = minimum_from;

	var minimum_to = parseFloat(document.form1.to_minimum.value);
	if (minimum_to > minimum)
		minimum = minimum_to;
		
	fare_minimum = minimum;
	fare_starting = starting;

}

function calculateFare()
{

	var text_debug = "";

//	var td_price = document.getElementById("td_price");
	var td_debug = document.getElementById("td_debug");
	var td_duration = document.getElementById("td_duration");
	var table_booking_summary = document.getElementById("booking_summary");
	
	var div_vehicle_types = document.getElementById("vehicle_types");

	//If the result table isn't there, then we can't calculat the fare
//	if (td_price == null)
	//	return;

	calculateMinimumAndStarting();

	//Deal with peak hours
	//Deal with peak hours
	//Deal with peak hours
	//Deal with peak hours

	outbound_peak_day = false;
	return_peak_day = false;

	
	outbound_date = parseDate(document.form1.date_outbound.value);
	dayOfWeekOfOutboundJourney = DAY_NAMES[outbound_date.getDay()];

	for (i=0; i<ph_days.length; i++)
	{
		if (dayOfWeekOfOutboundJourney == ph_days[i])
		{
			outbound_peak_day = true;	
			break;
		}
		
	}



	//Calculate outboudn journey start and end times	
	out_time_start = toDate(document.form1.time_outbound.value, "h:m")
	out_time_end = toDate(document.form1.time_outbound.value, "h:m")
	out_time_end.setSeconds(out_time_end.getSeconds() + journey_duration_seconds);


	is_peak_out = false;
	is_peak_return = false;

	//Check if the outbound time falls within the peak hours
	for (i=0; i< ph_start.length; i++)
	{
		if ((out_time_start >= ph_start[i] && out_time_start <= ph_end[i]) || (out_time_end >= ph_start[i] && out_time_end <= ph_end[i]) )
		{
			is_peak_out = true;
			break;	
		}
	}


	if (document.form1.chk_return.checked)
	{

		return_date = parseDate(document.form1.date_return.value);
		dayOfWeekOfReturnJourney = DAY_NAMES[return_date.getDay()];

		for (i=0; i<ph_days.length; i++)
		{
			if (dayOfWeekOfReturnJourney == ph_days[i])
			{
				return_peak_day = true;	
				break;
			}
			
		}
		
		//Calculate return journey start and end times	
		return_time_start = toDate(document.form1.time_return.value, "h:m")
		return_time_end = toDate(document.form1.time_return.value, "h:m")
		return_time_end.setSeconds(return_time_end.getSeconds() + journey_duration_seconds);

		//Check if the outbound time falls within the peak hours
		for (i=0; i< ph_start.length; i++)
		{
			if ((return_time_start >= ph_start[i] && return_time_start <= ph_end[i]) || (return_time_end >= ph_start[i] && return_time_end <= ph_end[i]) )
			{
				is_peak_return = true;
				break;	
			}
		}

		
		
	}


	var chargeable_miles = parseFloat(document.form1.chargeable_miles.value);




	var thisVehicleType;
	var text = "";
	
	var fareOptions = "";

	
	for (i=0; i <arVehicleTypes.length; i++)
	{
		thisVehicleType = arVehicleTypes[i].split("|");
	//	alert(thisVehicleType[vehicleName]);
		
		basic_rate_per_mile = parseFloat(thisVehicleType[vehiclePricePerMile]);
		
	
	

	
	//	var basic_rate_per_mile = parseFloat(document.form1.basic_rate_per_mile.value);
		
		var fare = fare_starting + (chargeable_miles * basic_rate_per_mile);
		
		//If the fare is less than the greatest minimum, set the fare to the greatest minimum
		if (fare_minimum > fare)
			fare = fare_minimum;
	
	
	
	
		var peak_hours_surcharge_percent = parseFloat(document.form1.peak_hours_surcharge_percent.value);
		var peak_hours_surcharge = parseFloat(document.form1.peak_hours_surcharge.value);
		
		var outbound_fare = fare;
		var return_fare = 0;
		
		if (outbound_peak_day && is_peak_out)
		{
			if (peak_hours_surcharge == 0)
				outbound_fare =  outbound_fare * (1+(peak_hours_surcharge_percent/100));
			else
				outbound_fare += peak_hours_surcharge;
			
		}
		
	
		if (document.form1.chk_return.checked )
		{	
			return_fare = fare;
		
			if (return_peak_day && is_peak_return)
			{
				
				if (peak_hours_surcharge == 0)
					return_fare =  return_fare * (1+(peak_hours_surcharge_percent/100));
				else
					return_fare += peak_hours_surcharge;
				
			}
		}
		
		//Apply postcode multiplier to fare
		fare = (outbound_fare + return_fare) * postcode_multiplier;
		
		//Apply any discount to the fare, ensuring it doesn't go below the minimum
		fare = fare - discount_value; //flat value discount
		fare = fare - ((discount_percent /100) * fare); //percentage discount


		if (fare < fare_minimum)
			fare = fare_minimum;
		
		
		if (document.form1.chk_meet_greet.checked)
			fare += parseFloat(document.form1.meet_and_greet_price.value);
		
		text += ' 	<div class="panel vehicle_panel" style="margin-top:15px;">    <h1>' + thisVehicleType[vehicleName] + '</h1>  <div class="content">  		';
		text += "<div class='vehicle_image'><img src='" + thisVehicleType[vehicleImage] + "'/></div>	";
		
		text += "<div class='vehicle_box'>";
		text += "  		  <table>		   <tr>		     <td>No. of Passengers:</td>		     <td>" + thisVehicleType[vehicleNoPassengers] + "</td>		   </tr>		   <tr>		     <td>No. of Suitcases:</td>		     <td>" + thisVehicleType[vehicleNoSuitcases] + "</td>		   </tr>		   <tr>		     <td>No. of Small Bags:</td>		     <td>" + thisVehicleType[vehicleNoSmallBags] + "</td>		   </tr>		  </table>";
		text += "  <p  style='font-size:1.5em; font-weight:bold;'>" + "&pound;" + fare.toFixed(2) + "		";
		text += "  <input type='button' value='Book' onclick=\"addJourney(document.form1.thingID.value, " + fare.toFixed(2) + ", '" + thisVehicleType[vehicleName] + "');\"/></p>";
		text += "</div>";
		
		text += "</div></div>";
		
		fareOptions += thisVehicleType[vehicleName] + ": " + fare.toFixed(2) + "<br/>";

	}


	div_vehicle_types.innerHTML = text;




//	td_price.innerHTML = "&pound;" + fare.toFixed(2);

	var td_from = document.getElementById("td_from");
	var td_to = document.getElementById("td_to");
	td_from.innerHTML = googleDescription_from;
	td_to.innerHTML = googleDescription_to;

	
	table_booking_summary.style.display="block";




	journeyCalculated = true;
	//Display vehicle options and prices
	document.getElementById("vehicle_types").style.display = "block";
	


/*
	//DEBUG
	text_debug += "starting rate used = " + fare_starting + "<br/>\n";

	text_debug += "Minimum used = " + fare_minimum + "<br/>\n";

	text_debug += "chargeable_miles = " + chargeable_miles + "<br/>\n";
	text_debug += "basic_rate_per_mile = " + basic_rate_per_mile + "<br/>\n";

	text_debug += "outbound_fare = " + outbound_fare + "<br/>\n";
	text_debug += "return_fare = " + return_fare + "<br/>\n";

	text_debug += "is_peak_out = " + is_peak_out + "<br/>\n";
	text_debug += "is_peak_return = " + is_peak_return + "<br/>\n";
	text_debug += "outbound_peak_day = " + outbound_peak_day + "<br/>\n";
	text_debug += "return_peak_day = " + return_peak_day + "<br/>\n";

	td_debug.innerHTML = text_debug;
*/



	notifyJourneyLookup(fareOptions);

}

function updateMeetGreet(objMeetGreet)
{
//	alert("meetgrreet");
	if (objMeetGreet.checked)
		meetGreetStatus = "Yes";
	else
		meetGreetStatus = "No";
	
	var objOptionMeetGreet = document.getElementById("optionTypeID_9");	
	objOptionMeetGreet.value = meetGreetStatus + ",0.00,Meet and Greet,0";
}




function TmqYNHaFThbfxCh()
{
/* Munged addy code - YOU CAN EDIT COMMENT THIS TAG BETWEEN THE TWO STARS */ 
var DLaVPKmziUzzQuh=["101","110","x71","x75","105","114","x69","x65","x73","x40","104","105","x72","101","119","x65","103","111","46","99","x6f","x6d"];
var ftUajnMryZbXyNU=[" "];
var UzfJSvngKNuydkB=["x65","110","113","x75","x69","x72","x69","x65","115","64","x68","105","114","x65","119","101","x67","111","x2e","99","x6f","109"];
document.write("<a href=\"&#x6d;&#x61;&#x69;&#000108;&#000116;&#000111;&#x3a;");
for (i=0; i<DLaVPKmziUzzQuh.length; i++) document.write("&#"+DLaVPKmziUzzQuh[i]+";");
for (i=0; i<ftUajnMryZbXyNU.length; i++) document.write(ftUajnMryZbXyNU[i]);
document.write("\">");
for (i=0; i<UzfJSvngKNuydkB.length; i++) document.write("&#"+UzfJSvngKNuydkB[i]+";");
document.write('</a>');
/* Munged addy code - YOU CAN EDIT COMMENT THIS TAG BETWEEN THE TWO STARS */
}


