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

var debug = false; // replace with false when ready to run. When true email will not be sent! ;

// 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)
{
	// alert("Running the validator script")


	  if (theForm.email.value == "" && theForm.telephoneNo.value == "" && theForm.mobileNo.value == "")
	  {
		alert("Please give us some contact information so that\nwe 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);
	  }

	// Next check that if the pickup is not an airport then prompt the user to give the postcode
	// of the pickup location, or explicitly state 'unknown'
	  if (
		   getRadio(theForm.contact_type) == "booking" &&
		   (
			  (theForm.pickup_location.selectedIndex>4)
		   ) &&
	   theForm.pickup_postcode0.value == ""
		)
	  {
		alert("If possible please tell us the postcode of the pickup location. If you do not know this then please type 'unknown'");
		theForm.pickup_postcode0.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'.\n\nPlease 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'.\n\nPlease tell us that address.");
		theForm.destination_address.focus();
		return (false);
	  }


	// Next check that if pickup or destination is an airport then prompt the user to give the
	// flight number, or explicitly state 'unknown'
	  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);
	  }


	  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);
	  }


   // Check if the the pickup hour and the destination hour are Unspecified. If so warn user and go back. 
	  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);
	  }


	//	The next bit checks if the pickup time is in the future. If not an alert warns that there's a 
	// problem with the information given. 
	// If the pickup time is within the next 24 hours and alert box is shown warning 
	//	the user that they should phone to confirm receipt or contact info.
	
	//alert("currentDate = " + currentDate + "\ntheForm.date_of_travel.value = " + theForm.date_of_travel.value + "\nday = " + theForm.date_of_travel.value.substring(0,2) + "\nmonth = " + theForm.date_of_travel.value.substring(3,6) + "\nyear = " + theForm.date_of_travel.value.substring(7,11));


   // At this point either the pickup hour or detination hour are specified.
	if (theForm.pickup_hour.value == "Unspecified") // no pickup hour given so use destination hour
	{
		var travel_hour = theForm.destination_hour.value;
		if (theForm.destination_minute.value == "Unspecified") // and if no destination minute assume O'clock
		{var travel_minute = "00";}
		else
		{var travel_minute = theForm.destination_minute.value;} // otherwise use what's specified
	}
	else // The pickup hour is given so use that
	{
		var travel_hour = theForm.pickup_hour.value;
		if (theForm.pickup_minute.value == "Unspecified") // and if no pickup minute assume O'clock
		{var travel_minute = "00";}
		else
		{var travel_minute = theForm.pickup_minute.value;} // otherwise use what's specified
	};
	
	//alert("travel_hour = " + travel_hour + "\ntravel_minute = " + travel_minute);

	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) + " " + travel_hour + ":" + travel_minute;
	//alert("currentDate = " + currentDate + "\nparse = " + Date.parse(currentDate) + "\ntravelDate = " + travelDate + "\nparse = " + Date.parse(travelDate));
	var time_to_journey = (Date.parse(travelDate)-Date.parse(currentDate));
	// alert("Time until your journey = " + time_to_journey/1000.0 + " seconds")

	if (time_to_journey <= 0)
	{
		alert("I think there is an error in the information you have given me. Please check the travel date you have specified.");
		theForm.date_of_travel.focus();
		return (false);
	}


	if (time_to_journey <= 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.");
		};
		
	}



	//	  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 (debug) alert("Here's a summary of all the input data: \nrefnumber = " + theForm.refnumber.value + ". \ncontact_type = " + getRadio(theForm.contact_type) + ". \ncustomer_name = " + theForm.customer_name.value + ". \nemail = " + theForm.email.value + ". \ntelephoneNo = " + theForm.telephoneNo.value + ". \nmobileNo = " + theForm.mobileNo.value + ". \npickup_location = " + theForm.pickup_location.value + ". \npickup_address = " + theForm.pickup_address.value + ". \npickup_postcode0 = " + theForm.pickup_postcode0.value + ". \ndestination_location = " + theForm.destination_location.value + ". \ndestination_address = " + theForm.destination_address.value + ". \ndestination_postcode0 = " + theForm.destination_postcode0.value + ". \ndate_of_travel = " + theForm.date_of_travel.value + ". \npickup_hour = " + theForm.pickup_hour.value + ". \npickup_minute = " + theForm.pickup_minute.value + ". \ndestination_hour = " + theForm.destination_hour.value + ". \ndestination_minute = " + theForm.destination_minute.value + ". \nflight No = " + theForm.flight_number.value + ". \npassengers = " + theForm.passengers.value + ". \nsuitcases = " + theForm.suitcases.value + ". \nhand_luggage = " + theForm.hand_luggage.value + ". \nski_bags = " + theForm.ski_bags.value + ". \npushchairs = " + theForm.pushchairs.value + ". \npayment_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://wikitaxis.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://wikitaxis.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://wikitaxis.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 + '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 (with no email being sent), 
	// otherwise 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());