<!--
//*******************************************
//* Validation and general DHTML functions  *
//* CarHire.js								*
//* June 2004								*
//* By Adrian T.							*
//*******************************************

strDefaultAirport = "";

//*******************************************
//FUNCTION: updateMonth
//*******************************************
//DESC:Ensures the pick-up date is valid and sets the 
//			drop-off date to be 3 days after pick-up.
//JOB:	#379
//*******************************************

// 
function validateDates(strFor){
	if (strFor == "from")
	{
		var from_date_DD = parseInt(dGetById("from_date_DD").value, 10);
		var from_date_MM = parseInt(dGetById("from_date_MM").value, 10) - 1;
		var from_date_YYYY = parseInt(dGetById("from_date_YYYY").value);
	
		if (!(isNaN(from_date_DD) || isNaN(from_date_MM) || isNaN(from_date_YYYY)))
		{
			var dteCheckIn = new Date(from_date_YYYY, from_date_MM, from_date_DD);
	
			// Reset date to the last day of the month if it's not valid.
			if (from_date_DD != dteCheckIn.getDate())
			{
				from_date_DD = from_date_DD - dteCheckIn.getDate();
				dGetById("from_date_DD").value = from_date_DD;
			}
	
			// Set checkout date.
			var dteCheckOut = new Date(from_date_YYYY, from_date_MM, from_date_DD + 7);
			var to_date_DD = dteCheckOut.getDate(); if (to_date_DD < 10) to_date_DD = "0" + to_date_DD;
			var to_date_MM = dteCheckOut.getMonth() + 1; if (to_date_MM < 10) to_date_MM = "0" + to_date_MM;
			
			dGetById("to_date_DD").value = to_date_DD;
			dGetById("to_date_MM").value = to_date_MM;
			dGetById("to_date_YYYY").value = dteCheckOut.getFullYear();
		}
	} else {
		var to_date_DD = parseInt(dGetById("to_date_DD").value, 10);
		var to_date_MM = parseInt(dGetById("to_date_MM").value, 10) - 1;
		var to_date_YYYY = parseInt(dGetById("to_date_YYYY").value);

		if (!(isNaN(to_date_DD) || isNaN(to_date_MM) || isNaN(to_date_YYYY)))
		{
			var dteCheckOut = new Date(to_date_YYYY, to_date_MM, to_date_DD);

			// Reset date to the last day of the month if it's not valid.
			if (to_date_DD != dteCheckOut.getDate())
			{
				to_date_DD = to_date_DD - dteCheckOut.getDate();
				dGetById("to_date_DD").value = to_date_DD;
			}		
		}	
	}
}

//*******************************************************
// FUNCTION:	populate
//*******************************************************
// DESC:		Adds option value and text to a selectbox.
//				This function is called from CarHire/CarHireGofa.asp
// PARAMS:		strTxt - Option text 
//				strOpt - Option value
//				strThisElement = Select box to change
// RETURNS:		nothing
//*******************************************************
function populateCar(strTxt,strOpt,strThisElement)	{
	strMyElement = dGetById(strThisElement);
	strMyElement.options[intOptionCount] = new Option(strOpt,strTxt);
	
	if (strTxt == strDefaultAirport){
		strMyElement.options[intOptionCount].selected = true;
	}
	intOptionCount++;
}

//*******************************************************
// FUNCTION:	clearSelect
//*******************************************************
// DESC:		Clears the contents of a select box
// PARAMS:		tmpID - id of select to work with
//				tmpDefault - The option text for the cleared select box
// RETURNS:		nothing
//*******************************************************
function clearSelect(tmpID,tmpDefault)	{
	strMyElement = dGetById(tmpID)
	strMyElement.options.length = 0;
	strMyElement.options[0] = new Option(tmpDefault,"");
}
//*******************************************************

//*******************************************************
// FUNCTION:	populateSelect
//*******************************************************
// DESC:		Clears/Populates a select element
// PARAMS:		objID - The calling object
//				strValue - The selected value from the calling object
// RETURNS:		nothing
//*******************************************************

function populateSelect(objFormElName)
{
	//eval("strValue = document.Step1." + objFormElName + ".options[document.Step1." + objFormElName + ".selectedIndex].value")
	eval("strValue = dGetById(\"" + objFormElName + "\").options[dGetById(\"" + objFormElName + "\").selectedIndex].value");

	var fieldName = "country1";
	if (!dGetById(fieldName)) {
		fieldName = "country"; }

	if (objFormElName == fieldName)	{
		clearSelect("area","--- select area ---")
		clearSelect("location","--- select location ---")

		objFormElName = "area"
	}	else if (objFormElName == "area")	{
		clearSelect("location","--- select area ---")
		objFormElName = "location"
	}
	
	intOptionCount=0;
	
	SelectVisibilityCar(objFormElName + "Select","hidden")
	SelectVisibilityCar(objFormElName + "Message","visible")
	
	var myIFrame = dGetById("Gofa");
	carJax('/travel/travelsupermarket/CarHireFinderGofa.aspx?action=' + objFormElName + '&value=' + strValue);
}
function populateSelectCar(objFormElName)
{
	//eval("strValue = document.Step1." + objFormElName + ".options[document.Step1." + objFormElName + ".selectedIndex].value")
	eval("strValue = dGetById(\"" + objFormElName + "\").options[dGetById(\"" + objFormElName + "\").selectedIndex].value");
	
	var fieldName = "country1";
	if (!dGetById(fieldName)) {
		fieldName = "country"; }
	
	if (objFormElName == fieldName)	{
		clearSelect("area","--- select area ---")
		clearSelect("location","--- select location ---")

		objFormElName = "area"
	}	else if (objFormElName == "area")	{
		clearSelect("location","--- select area ---")
		objFormElName = "location"
	}
	
	intOptionCount=0;
	
	SelectVisibilityCar(objFormElName + "Select","hidden")
	SelectVisibilityCar(objFormElName + "Message","visible")
	
	var myIFrame = dGetById("Gofa");
	carJax('/travel/travelsupermarket/CarHireFinderGofa.aspx?action=' + objFormElName + '&value=' + strValue);
}
//*******************************************************

//*******************************************************
// FUNCTION:	SelectVisibility
//*******************************************************
// DESC:		Sets visibility style attribute to a div
// PARAMS:		objDIV - The calling object
//				strStatus - Style attribute value to set to
// RETURNS:		nothing
//*******************************************************
function SelectVisibilityCar(objDIV,strStatus)	{
	objSelPopStatus = dGetById(objDIV);
	if (objSelPopStatus) {
		objSelPopStatus.style.visibility = strStatus;
	}
}
//*******************************************************


function carJax(uri) {
	var scriptElement = document.createElement('SCRIPT');
	scriptElement.src=uri;
	document.body.appendChild(scriptElement);
}

//*******************************************************
// FUNCTION:	checkDayOfMonthYear
//*******************************************************
// DESC:		Checks day is valid for month and year
// PARAMS:		month
//				year
// RETURNS:		no of days in month year
//*******************************************************
function checkDayOfMonthYear(intMonth, intYear)	{
	var strDate, intTmpMonth;
	intTmpMonth = Math.round(intMonth) + 1;
	strDate = intTmpMonth + '/1/' + intYear;
	strDate = strDate.toString();
	theDate = new Date (Date.parse(strDate)-1000*60*60*24);
	return(theDate.getDate());
	
}
//*******************************************************


//*******************************************************
// FUNCTION: checkField
//*******************************************************
// DESC:		checks if a selection has been made from
//				a select box or a text field is not empty
// PARAMS:		strField is the field to validate
//				strFieldName is the friendly name of the
//				field to display to the user
// RETURNS:		true or false
//*******************************************************
function checkField(strFieldName,strField)
{
try {
	var strValue = dGetById(strField).value;
	//alert(strValue);
	if ((strValue == '') || strValue == null )
	{
		if (dGetById(strField).type == 'text')
			{
			// it's a text field...
			showErrorMessage("Please enter a " + strFieldName);
			//alert("Please enter a " + strFieldName);
			}
		else
			{
			// it's a select box...
			showErrorMessage("Please select a " + strFieldName + " from the list.");
			//alert("Please select a " + strFieldName + " from the list.");
			}
		dGetById(strField).focus();
		return false;
	}
	else
	{
		return true;
	}
} catch (fieldError) {
	//alert(String(strField) + '...' + fieldError);
	return true;
}
}
//*******************************************************

//*******************************************************
// FUNCTION: setHiddenAge
//*******************************************************
// DESC:		checks if a selection has been made from
//				a select box or a text field is not empty
// PARAMS:		strField is the field to validate
//				strFieldName is the friendly name of the
//				field to display to the user
// RETURNS:		true or false
//*******************************************************
function setHiddenAge(intAge)
{
	if((intAge != '') || (intAge != null)){
		var AgeElement = dGetById("age");
		if (AgeElement) {
			AgeElement.value = "";
			AgeElement = intAge;
		}
	}
}
//*******************************************************


//*******************************************************
// FUNCTION: checkAge
//*******************************************************
// DESC:		checks to make sure driver is over 21 on 
//				the collection date, if not alerts user
// PARAMS:		None
// RETURNS:		true
//				false
//*******************************************************
function checkAge()
{
	// driver date of birth elements...
	strDriverdob_DD = dGetById('driverdob_date_DD').value;
	strDriverdob_MM = dGetById('driverdob_date_MM').value;
	strDriverdob_YYYY = dGetById('driverdob_date_YYYY').value;

	// Check DOB year is in YYYY format
	if((strDriverdob_YYYY.length < 4)){
		showErrorMessage("Please enter your date of birth year in YYYY format");
		//alert("Please enter your date of birth year in YYYY format");
		dGetById('driverdob_date_YYYY').select()
		dGetById('driverdob_date_YYYY').focus()
		return false;
	}
	
		// Check DOB year is in YYYY format
	if(isNaN(strDriverdob_YYYY)){
		showErrorMessage("Please only use numbers for your date of birth year");
		//alert("Please only use numbers for your date of birth year");
		dGetById('driverdob_date_YYYY').select()
		dGetById('driverdob_date_YYYY').focus()
		return false;
	}
	
	// Check DOB day is valid for month and year (i.e. not 31 Feb)
	intDaysInMonth = checkDayOfMonthYear(strDriverdob_MM,strDriverdob_YYYY);
	//alert("DOB day: " + strReturndate_DD + "\nDays in Month/Year: " + intDaysInMonth);
	if (Math.round(strDriverdob_DD) > intDaysInMonth){
		showErrorMessage("There are only " + intDaysInMonth + " days for the date of birth month and year you selected");
		dGetById("driverdob_date_DD").focus();
		return false;
	}
	
	// collection date elements...
	strCollectiondate_DD = dGetById('from_date_DD').value;
	strCollectiondate_MM = dGetById('from_date_MM').value;
	strCollectiondate_YYYY = dGetById('from_date_YYYY').value;
	strCollectiondate_time = dGetById('from_date_time').value;
	
	// compare dob with collection date...
    yearAge = strCollectiondate_YYYY - strDriverdob_YYYY;

    if (strCollectiondate_MM >= strDriverdob_MM){
        var monthAge = strCollectiondate_MM - strDriverdob_MM;
    }
    else {
        yearAge--;
        var monthAge = 12 + strCollectiondate_MM - strDriverdob_MM;
    }

    if (strCollectiondate_DD >= strDriverdob_DD){
        var dateAge = strCollectiondate_DD - strDriverdob_DD;
    }
    else {
        monthAge--;
        var dateAge = 31 + strCollectiondate_DD - strDriverdob_DD;

        if (monthAge < 0) {
            monthAge = 11;
            yearAge--; 
        }
    }


	// is driver 76 or over?
	if (yearAge >= 76)
	{
		showErrorMessage("Sorry, the age range for hiring a car is 21 to 75 years of age");
		//alert("Sorry, the age range for hiring a car is 21 to 75 years of age");
		return false;
	}

	// is the driver 21 or over?..
	if (yearAge >= 21)
		{
		// yes, pass back "true"...
		setHiddenAge(yearAge);
		return true;
		}
	else
		{
		// no, alert...
		showErrorMessage("Sorry the age range for hiring a car is 21 to 75 years of age");
		//alert("Sorry, the age range for hiring a car is 21 to 75 years of age");
		return false;
		}
}
//*******************************************************


//*******************************************************
// FUNCTION: checkDates
//*******************************************************
// DESC:		validates collection and return dates and
//				alerts user if validation fails
// PARAMS:		None
// RETURNS:		true
//				false
//*******************************************************
function checkDates()
{
	// collection date elements, time is HH:MM so split at ":" and put in an array ...
	strCollectiondate_DD = dGetById('from_date_DD').value;
	strCollectiondate_MM = dGetById('from_date_MM').value;
	strCollectiondate_YYYY = dGetById('from_date_YYYY').value;
	arrCollectiondate_time = (dGetById('from_date_time').value).split(":");

	// return date elements, see above for description...
	strReturndate_DD = dGetById('to_date_DD').value;
	strReturndate_MM = dGetById('to_date_MM').value;
	strReturndate_YYYY = dGetById('to_date_YYYY').value;
	arrReturndate_time = (dGetById('to_date_time').value).split(":");
	
	
	
	// create date objects for collection and return...
	// for the time, use elements of arrCollectiondate_time and arrReturndate_time arrays [0] is HH, [1] is MM. The final 00 is seconds (not captured from form)...
	var objCollectiondate = new Date(strCollectiondate_YYYY, strCollectiondate_MM-1, strCollectiondate_DD, arrCollectiondate_time[0],arrCollectiondate_time[1],00);
	var objReturndate = new Date(strReturndate_YYYY, strReturndate_MM-1, strReturndate_DD, arrReturndate_time[0], arrReturndate_time[1],00);	
	var dtToday=new Date();
	var iTodayDay=dtToday.getDate();
	var iTodayMonth=dtToday.getMonth();
	var iTodayYear=dtToday.getFullYear();
	var dtTimelessToday=new Date(iTodayYear, iTodayMonth, iTodayDay);
	var dtTimelessCollect=new Date(strCollectiondate_YYYY, strCollectiondate_MM-1, strCollectiondate_DD);

	// Check pickup day is valid for month and year (i.e. not 31 Feb)
	intDaysInMonth = checkDayOfMonthYear(strCollectiondate_MM,strCollectiondate_YYYY);
	//alert("Pickup day: " + strCollectiondate_DD + "\nDays in Month/Year: " + intDaysInMonth);
	if (Math.round(strCollectiondate_DD) > intDaysInMonth){
		showErrorMessage("There are only " + intDaysInMonth + " days in the pickup month and year you selected");
		dGetById("from_date_DD").focus();
		return false;
	}
	
	// Check drop off day is valid for month and year (i.e. not 31 Feb)
	intDaysInMonth = checkDayOfMonthYear(strReturndate_MM,strReturndate_YYYY);
	//alert("Dropoff day: " + strReturndate_DD + "\nDays in Month/Year: " + intDaysInMonth);
	if (Math.round(strReturndate_DD) > intDaysInMonth){
		showErrorMessage("There are only " + intDaysInMonth + " days in the drop off month and year you selected");
		dGetById("to_date_DD").focus();
		return false;
	}
	
	// Check collection date is in the future
	// alert("Col: " + objCollectiondate + "\n" + "Ret: " + objReturndate);
	if (dtTimelessCollect < dtTimelessToday){
		//check that the return date is not before the collection date
		//alert("Your collection time and date must be in the future");
		showErrorMessage("Your pickup date and time must be in the future");
		return false;
	}
	
	// If same day the collection should be at least 5 hours from 
	// the current hour rounded up
	if(dtTimelessToday.valueOf() == dtTimelessCollect.valueOf()){
	
		// check the collection time is 5 hours from the current time
		// use 6 because the current time will be rounded up to the
		// nearest hour e.g. 10:15 will be treated as 11:00
		if(objCollectiondate.getHours() < (dtToday.getHours() + 6)){
			//alert("The collection time must be at least 5 hours from the current time");
			showErrorMessage("The pickup time must be at least 5 hours from the current time")
			dGetById('from_date_time').selectedIndex = 0;
			dGetById('from_date_time').focus();
			return false;
		}
	}

	// Check the return date is not before the collection date
	//alert("Col: " + objCollectiondate + "\n" + "Ret: " + objReturndate);
	if (objReturndate <= objCollectiondate){
		//check that the return date is not before the collection date
		//alert("Your return time and date cannot be before or the same as your collection time and date");
		showErrorMessage("Your drop off date and time cannot be before or the same as your pickup date and time");
		return false;
	}

	// Everything OK	
	return true;
}
//*******************************************************


//*******************************************************
// FUNCTION: showErrorMessage
//*******************************************************
// DESC:		Shows the message in the errorDiv
// PARAMS:		strErrorMsg
// RETURNS:		nothing
//*******************************************************
function showErrorMessage(strErrorMsg)
{
	if(strErrorMsg.length > 0){
		var strError = "Please correct the following:<ul style='margin-bottom:10px'><li>" + strErrorMsg;
		var errDiv = dGetById("errorDiv");
		if (errDiv) {
			errDiv.innerHTML=strError;
			errDiv.style.display = "inline";
		}
		strError = strError + "</li></ul><br>"
		document.location = "#top";
	}
}
//*******************************************************


//*******************************************************
// FUNCTION: clearErrorMessage
//*******************************************************
// DESC:		Clears and hides (collapses) the message in the errorDiv
// PARAMS:		none
// RETURNS:		nothing
//*******************************************************
function clearErrorMessage()
{
	var errDiv = dGetById("errorDiv");
	if (errDiv) {
		errDiv.innerHTML='';
		errDiv.style.display = "none";
	}
}
//*******************************************************

//*******************************************************
// FUNCTION: validateEmail
//*******************************************************
// DESC:		Validates the user's email address
// PARAMS:		objEmailField - the form field id
// RETURNS:		true = email is valid, false = email not valid
//*******************************************************
function validateEmail(objEmailName, objEmailDomain)
{
	var reEmailName = /^([\w\'\"-])+((([a-zA-Z\-0-9])|\.)+)$/
	var reEmailDomain = /^([a-zA-Z0-9_-])+((([a-zA-Z\-0-9]{1,})+\.)+([a-zA-Z]{2,})+)$/
	
	if (objEmailName) {
	
	    var strEmailName = objEmailName.value;
	    var strEmailDomain = objEmailDomain.value;
	    if ((strEmailName.length > 0) || (strEmailDomain.length > 0))
	    {		
		   blnExpEmailName = reEmailName.test(strEmailName);
		   blnExpEmailDomain = reEmailDomain.test(strEmailDomain);
		   if (blnExpEmailName == false || blnExpEmailDomain == false)
		   {
		    	showErrorMessage("Please enter a valid email address.");
			    return false;
		   }
	    }
	    return true;
	}
}

function carHire_submitStepOne()
{
try {
	var fieldName = "country1";
	if (!dGetById(fieldName)) {
		fieldName = "country"; }

	var result = false;
	clearErrorMessage();
	// field id's to check, both select and text...
	arrFieldId = new Array(fieldName,"area","location","from_date_DD","from_date_MM","from_date_YYYY","from_date_time","to_date_DD","to_date_MM","to_date_YYYY","to_date_time","driverdob_date_DD","driverdob_date_MM","driverdob_date_YYYY","cartypeid");
	// coresponding friendly name to appear in the alert, "destination country", "collection time" etc...
	arrFieldName = new Array("destination country","destination area","destination location","collection day","collection month","collection year","collection time","return day","return month","return year","return time","driver DOB day","driver DOB month","driver DOB year","car category");
	// loop through all the fields and run the checkField function...
	for (var i = 0; i < arrFieldId.length; i++)
	{
		result = checkField(arrFieldName[i],arrFieldId[i]);
		if (!result)
		{
		// check failed, don't submit...
		return false;
		}	else	{
			try {
				if (arrFieldId[i] == fieldName || arrFieldId[i] == "area" || arrFieldId[i] == "location")	{
					eval("tmpFEPopulate = document.Step1." + arrFieldId[i] + ".options[document.Step1." + arrFieldId[i] + ".selectedIndex].text")
					eval("document.Step1.FE" + arrFieldId[i] + ".value = tmpFEPopulate")
				}
			} catch (catchy) {
			}
		}
	}

	result = checkDates();
	// check collection and return dates...
	if (!result)
	{
	return false;
	}
	
	// check driver age...
	result = checkAge();
	if (!result)
	{
	return false;
	}
	
	// check email (if populated)...
	if (dGetById('emailName')) {
	      result = validateEmail(dGetById('emailName'), dGetById('emailDomain'));
	}
	
	if (!result)
	{
	return false;
	}	
	
	if (parent) {
		if (parent.window) {
			if (parent.window['formLoad_dyn']) {
				eval(parent.window['formLoad_dyn']);
			}
		}
	}

	return true;
} catch (ex) {
	alert(ex);
	return false;
}
}
//*******************************************************
i = 0;
//-->