// QSearch form validation
function doFormQS()
{
	// get today's date
	var tDate = new Date();
	
	// build 2 dates from dropdowns
	var pDate = new Date(document.details.slFromYear.value,document.details.slFromMonth.value - 1,document.details.slFromDay.value);
	var dDate = new Date(document.details.slToYear.value,document.details.slToMonth.value - 1,document.details.slToDay.value);
	
	var tMonth = tDate.getMonth();
	var tDay = tDate.getDate();

	var pMonth = pDate.getMonth();
	var pDay = pDate.getDate();
	
	// check for a country
	if(document.details.area.value == "sel" || document.details.area.value == "none")
	{
		alert("Please select a specific destination from the country dropdown list.");
		document.details.area.focus();
		return false;

	// check for a pick up location
	} else if(document.details.tlocation.value == "") {
		alert("Please select a pick up location.");
		document.details.tlocation.focus();
		return false;

	// check for a drop off location
	} else if(document.details.tdropoff.value == "") {
		alert("Please select a drop off location.");
		document.details.tdropoff.focus();
		return false;

	// check if pickup is today
	} else if (pMonth == tMonth && pDay == tDay) {
		alert("We are unable to make online bookings for same day pick up, but we may be able to help you.\nPlease call our call centre on 1300 554 432 or 02 9939 4433.");
		return false;

	// check pickup date hasn't already passed
	} else if (pDate < tDate){
		alert("The pickup date you have selected has already passed. Please reselect.");
		document.details.slFromMonth.focus();
		return false;

	// compare the 2 dates
	} else if(dDate < pDate) {
		alert("The dropoff date you have selected is before your pick up date. Please reselect.");
		document.details.slFromMonth.focus();
		return false;

	// check there is a driver age
	} else if(document.details.fiDriverAge.value == "") {
		alert("You have not entered your age.");
		document.details.fiDriverAge.focus();
		return false;

	// check driver age is valid
	} else if(document.details.fiDriverAge.value < 21 || document.details.fiDriverAge.value > 70) {
		alert("If you are under 21 or over 70, conditions may apply to your rental.\nPlease call our call centre on 1300 554 432 or 02 9939 4433.");
		document.details.fiDriverAge.focus();
		return false;

	}
}

// get a variable from the querystring
function getQueryVariable(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++)
	{
		var pair = vars[i].split("=");
		if (pair[0] == variable)
		{
			return pair[1];
		}
	} 
}

// deeplink into carlist.w
function carlistLink(area,loc) {
	// get area & location
	var area = area;
	var location = loc;

	if(getQueryVariable("aff"))
	{
		var affcode = getQueryVariable("aff");
	} else {
		var affcode = "";
	}
	
	// get pick up dates
	var fDate = document.details.slFromDay.value;
	var fMonth = document.details.slFromMonth.value;
	if(fMonth < 10)
	{
		fMonth = "0" + fMonth;
	}
	var fYear = document.details.slFromYear.value;
	var fTime = document.details.slFromTime.value;
	fTimeNew = new String(fTime);
	fTimeNew.replace(":","%3A");
	
	// get drop off dates
	var tDate = document.details.slToDay.value;
	var tMonth = document.details.slToMonth.value;
	if(tMonth < 10)
	{
		tMonth = "0" + tMonth;
	}
	var tYear = document.details.slToYear.value;
	var tTime = document.details.slToTime.value;
	tTimeNew = new String(tTime);
	tTimeNew.replace(":","%3A");

	var baseURL = "/cgi-bin/liveweb.sh/carlist.w?UckUcZllaaIpkkac=&POlfigXkjkjkWbdq=&SblzdbFStskKccak=&ctryref=AUS&lang=AEN";
	
	var action = baseURL + "&aff=" + affcode + 
						"&selFromTime=" + fTimeNew + 
						"&slFromDay=" + fDate + 
						"&slFromMonth=" + fMonth + 
						"&slFromYear=" + fYear + 
						"&selToTime=" + tTimeNew + 
						"&slToDay=" + tDate +
						"&slToMonth=" + tMonth + 
						"&slToYear=" + tYear + 
						"&area=" + area + 
						"&tlocation=" + location + 
						"&tdropoff=" + location + 
						"&fidriverage=26";

	window.location = action;
}

// chnDur and addDur to get rid of js errors
function chnDur() { }
function addDur() { }

