// First part of this script does validation work on the form

var debug = false; // replace with false when ready to run ;

// This script examines the URL and extracts anything after a ? assigning it to variable 'formtype'
var formtype = '';

if (location.search.length > 0)
	formtype = location.search.substring(1);

function createReference(theDate)
{
		var day  = theDate.getDate();
		var month = theDate.getMonth() + 1;
		var yy = theDate.getYear();
		return day + "-" + month  + "-" + yy + "-" + Math.round(Math.random()*1000);
}

var currentDate = new Date();
var referenceNo = createReference(currentDate);

function Validator(theForm)
{
	  if (theForm.email.value == "" && theForm.telephoneNo.value == "" && theForm.mobileNo.value == "")
	  {
		alert("Please give us some contact information so that\rwe can return your enquiry.  We prefer contact by email.");
		theForm.email.focus();
		return (false);
	  }

	  if (theForm.pickup_location.selectedIndex <= 0)
	  {
		alert("Please select one of the options for the pickup location");
		theForm.pickup_location.focus();
		return (false);
	  }

	  if (theForm.pickup_location.value == "Other" && (theForm.pickup_address.value == "" && theForm.pickup_postcode0.value == ""))
	  {
		alert("The pickup location you have chosen is 'Other address'.\r\rPlease tell us that address.");
		theForm.pickup_address.focus();
		return (false);
	  }

	  if (theForm.destination_location.selectedIndex <= 0)
	  {
		alert("Please select one of the options for the journey destination");
		theForm.destination_location.focus();
		return (false);
	  }

	  if (theForm.destination_location.value == "Other" && theForm.destination_address.value == "" && theForm.destination_postcode0.value == "")
	  {
		alert("The destination you have chosen is 'Other address'.\r\rPlease tell us that address.");
		theForm.destination_address.focus();
		return (false);
	  }

	  if (theForm.date_of_travel.value == "")
	  {
		alert("Please tell us the date when you would like to travel");
		theForm.date_of_travel.focus();
		return (false);
	  }

	  if ((theForm.pickup_hour.selectedIndex == 0 && theForm.destination_hour.selectedIndex == 0))
	  {
		alert("Please tell us the time when you would like to travel");
		theForm.pickup_hour.focus();
		return (false);
	  }

//	  Note: Next bit commented out because now allow a booking to be made without deciding between online or cash.
//    If undecided or cash, tell the user that booking can be made online at a later date, or by credit card to
//	  driver with 5% surcharge.

//	  if ((getRadio(theForm.contact_type) == "booking" && getRadio(theForm.payment_type) == "undecided"))
//	  {
//		alert("For bookings you must tell us your preferred payment method");
//		return (false);
//	  }

	  if (
		   getRadio(theForm.contact_type) == "booking" &&
		   (
			  (theForm.pickup_location.selectedIndex>=1 && theForm.pickup_location.selectedIndex<=4) ||
			  (theForm.destination_location.selectedIndex>=1 && theForm.destination_location.selectedIndex<=4)
		   ) &&
	   theForm.flight_number.value == ""
		)
	  {
		alert("If possible, for bookings to or from an airport we would like you to tell us your flight number. If you do not know the number, please type 'unknown'");
		theForm.flight_number.focus();
		return (false);
	  }

	//	The next bit checks if the date of travel is within the next 24 hours. If so an alert box is shown, warning 
	//	the user that they should phone to confirm either booking or quotation.
	
	// alert("date_of_travel = " + theForm.date_of_travel.value + "\rcurrentDate = " + currentDate);
	// alert("theForm.date_of_travel.value = " + theForm.date_of_travel.value + "\rday = " + theForm.date_of_travel.value.substring(0,2) + "\rmonth = " + theForm.date_of_travel.value.substring(3,6) + "\ryear = " + theForm.date_of_travel.value.substring(7,11));
	var currentDate2 = currentDate;
	currentDate2.setHours(0);
	currentDate2.setMinutes(0);
	currentDate2.setSeconds(0);
	var travelDate = theForm.date_of_travel.value.substring(3,6) + " " + theForm.date_of_travel.value.substring(0,2) + ", " + theForm.date_of_travel.value.substring(7,11);
	// alert("travelDate = " + travelDate + "\rparse = " + Date.parse(travelDate) + "\rcurrentDate2 = " + currentDate2 + "\rparse = " + Date.parse(currentDate2));
	if ((Date.parse(travelDate)-Date.parse(currentDate2)) <= 24*60*60*1000)
	{
		if (getRadio(theForm.contact_type) == "booking") 
		{
			alert("Because your journey is in the next 24 hours we strongly advise you to telephone us on 01638 742424 to confirm that your booking has been received.");
		}
		else
		{
			alert("Because your journey is in the next 24 hours we strongly advise you to telephone us on 01638 742424 to confirm that we have received your request for a quotation.");
		};
		
	}

  if (debug) alert("Here's a summary of all the input data:\rrefnumber = " + theForm.refnumber.value + "\rcontact_type = " + getRadio(theForm.contact_type) + "\rcustomer_name = " + theForm.customer_name.value + "\remail = " + theForm.email.value + "\rtelephoneNo = " + theForm.telephoneNo.value + "\rmobileNo = " + theForm.mobileNo.value + "\rpickup_location = " + theForm.pickup_location.value + "\rpickup_address = " + theForm.pickup_address.value + "\rpickup_postcode0 = " + theForm.pickup_postcode0.value + "\rdestination_location = " + theForm.destination_location.value + "\rdestination_address = " + theForm.destination_address.value + "\rdestination_postcode0 = " + theForm.destination_postcode0.value + "\rdate_of_travel = " + theForm.date_of_travel.value + "\rpickup_hour = " + theForm.pickup_hour.value + "\rpickup_minute = " + theForm.pickup_minute.value + "\rdestination_hour = " + theForm.destination_hour.value + "\rdestination_minute = " + theForm.destination_minute.value + "\rflight No = " + theForm.flight_number.value + "\rpassengers = " + theForm.passengers.value + "\rsuitcases = " + theForm.suitcases.value + "\rhand_luggage = " + theForm.hand_luggage.value + "\rski_bags = " + theForm.ski_bags.value + "\rpushchairs = " + theForm.pushchairs.value + "\rvehicle_type = " + getRadio(theForm.vehicle_type) + "\rpayment_type = " + getRadio(theForm.payment_type)  );

  return (optionsForNextPage(theForm))
};


// Now handle optionsForNextPage

function optionsForNextPage(form) {

	// Prepare parameters to pass to next page;
	// No longer use secure website - just Paypal - so all pages come from ...
	
	form.success.value = 'http://www.airports-anytime.co.uk/'

	// decide where to get next page from ...
	// if a booking and user wants to book online - go to secure site
	//if (getRadio(form.contact_type) == 'booking' && getRadio(form.payment_type) == "Credit card online") {
	//	form.success.value = 'http://www.airports-anytime.co.uk/' // was 'https://www.secure-website.com/airports/'
	//} else {
	// not booking so no need to be secure, use local file
	//	form.success.value = 'http://www.airports-anytime.co.uk/'
	//};

	// if debugging then load local versions of the pages
	if (debug) {
		form.success.value = './';
		//if (getRadio(form.contact_type) == 'booking' && getRadio(form.payment_type) == "Credit card online") {form.success.value = './secure_website/'}
	}

	// based on entries in the form, decide what response page to show

	// If this is a booking and the user has opted to pay online, then show the online payment window
	if ((getRadio(form.contact_type) == "booking" && getRadio(form.payment_type) == "Credit card online"))
	{
		form.success.value = form.success.value + 'GB_online_payment.html';
	}
	else
	// If this is a request for quote, then load the relevant success page
	if (getRadio(form.contact_type) == "quotation")
	{
		form.success.value = form.success.value + 'thanks_rfq.html';
	}
	else
	{
		form.success.value = form.success.value + 'thanks_booking.html';
	};

	// add parameters to the URL using ? and & to pass to the next page (i.e., reply or online booking form)

	form.success.value = form.success.value + '?' +
		escape('customer_name') + '=' + getText(form.customer_name) + '&' +
		escape('Reference Number') + '=' + getText(form.refnumber) + '&' +
		escape('contact_type') + '=' + getRadio(form.contact_type) + '&' +
		escape('pickup_location') + '=' + getOption(form.pickup_location) + '&' +
		escape('destination_location') + '=' + getOption(form.destination_location) + '&' +
		escape('date_of_travel') + '=' + getText(form.date_of_travel) + '&' +
		escape('payment_type') + '=' + getRadio(form.payment_type) + '&' ;

	// if debug then just show the success page now, otherwide return true so that the form is posted,
	// and the success page is loaded in the usual way.
	if (debug) {
		location.href = form.success.value;
		return false
	}
	else {
	return true
	}
}




// Get TodaysDate's date
var TodaysDate=new Date();

// Limit the lowest year shown to this year
cal1.minYearChoice = parseInt(TodaysDate.getFullYear());
cal1.maxYearChoice = parseInt(TodaysDate.getFullYear())+2;

// Turn on use of date ranges here
cal1.useDateRange=true;


/* make cal1 pickable from today to a year from now
   Note: JS stupidly sets month numbers to start from 0 (i.e. Jan=0 Feb=1)
   my routines expect a normal month number with Jan=1 - hence the plus 1 on the months below

   the get functions below are standard Javascript
*/
cal1.setMinDate(TodaysDate.getFullYear(),TodaysDate.getMonth()+1, TodaysDate.getDate());
/*
   The +1 sets the max date for beginning or end to 10 years from now
*/
cal1.setMaxDate(parseInt(TodaysDate.getFullYear())+1,parseInt(TodaysDate.getMonth())+1, TodaysDate.getDate());