// QSearch form validation
function doFormQS()
{
	if(checkForm() && checkDate() && checkAge())
	{
		return true;
	} else {
		return false;
	}
}

function checkForm()
{
	// check a country has been selected
	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 a pick up location has been selected
	} else if(document.details.tlocation.value == "") {
		alert("Please select a pick-up location.");
		document.details.tlocation.focus();
		return false;

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

function checkDate()
{
	/* get selected values */	
	var pDate = document.details.slFromDay.value;
	var pMonth = document.details.slFromMonth.value;
	var pYear = document.details.slFromYear.value;
	
	var dDate = document.details.slToDay.value;
	var dMonth = document.details.slToMonth.value;
	var dYear = document.details.slToYear.value;
	
	/* check pick up date exists */
	if(((pMonth == "4" || pMonth == "6" || pMonth == "9" || pMonth == "11") && pDate == "31") || ((pMonth == "2") && (pDate == "30" || pDate == "31")))
	{
		alert("You have selected an invalid pick-up date. Please reselect.");
		document.details.slToMonth.focus();
		return false;
	}

	/* check drop off date exists */
	if(((dMonth == "4" || dMonth == "6" || dMonth == "9" || dMonth == "11") && dDate == "31") || ((dMonth == "2") && (dDate == "30" || dDate == "31")))
	{
		alert("You have selected an invalid drop-off date. Please reselect.");
		document.details.slToMonth.focus();
		return false;
	}
	
	/* build date objects */
	var pDateValid = new Date(pYear,pMonth - 1,pDate);
	var dDateValid = new Date(dYear,dMonth - 1,dDate);

	
	
	/* get today's date */
	/*var tDate = new Date();*/
	var today = new Date();    // returns current date with timestamp
	var today_floor= new Date(today.getFullYear(),today.getMonth(),today.getDate());  // return current date with time=00:00:00
	var firstallowpickupdayunix= today_floor.getTime()+2*24*3600*1000;  // returns the 00:00:00 time of the day after tomorrow

	var firstallowedpickupday= new Date(firstallowpickupdayunix);  // converts to date format



	/* get tomorrow date 
	tomorrowDate=tDate.getDate() + 1;*/
	
	/* is pick up today? */
	 if(pDateValid < today_floor) {
		alert("The pick-up date you have selected has already passed. Please reselect");
		document.details.slFromMonth.focus();
		return false;

	} 
	 else if(pDateValid < firstallowedpickupday )
	{
		alert("We are unable to make online bookings for same day or next day pick up, however we may be able to help you.\nPlease call our Reservations Centre on +971 4 3433 505 or email. reservations@holidayautos.ae \n\nBusiness Hours: Saturday through Thursday: 08.30 – 19.30 (7.30pm). \nFriday: closed, however for urgent bookings you may call +971 50 3766 993, between 10.00 – 17.00 (5pm). ");
		document.details.slFromMonth.focus();
		return false;

	}else if(dDateValid < pDateValid) {
		alert("The drop-off date you have selected is before your pick-up date. Please reselect");
		document.details.slFromMonth.focus();
		return false;
	}
	return true;
}

function checkAge()
{
	// check a driver age has been entered
	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) {
		alert("If you are under 21, conditions may apply to your rental.\nPlease call our call centre on +971 4 3433 505.");
		document.details.fiDriverAge.focus();
		return false;
	}
	return true;
}


// 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=MEA&lang=ME";
	
	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() {  }
