var Message1 = "Please select a Type of Tariff"
var Message2 = "Please select an Outward Route"
var Message3 = "Please select a Return Journey or check the One Way Journey box"
var Message4 = "Outward and Return routes must be different, please choose another."
var Message5 = "Please select a valid outward date"
var Message6 = "Please select a valid return date or check the OPEN DATED TICKET box"
var Message7 = "Return date cannot be before outward date"
var Message8	= "Please select a valid outward time."
var Message9	= "Please select a valid return time"
var Message10	= "Return time cannot be before outward time"
var Message11	= "Return time must be after outward time"
var Message12	= "Either normal or promotional fare may be selected, not both"
var Message13	= "Invalid passenger quantity specified"
var Message14	= "At least one passenger must be specified"
var Message15	= "You may only book 9 tickets in total, please modify your selections and try again"
var Message16	= "Vehicle details selected without a valid vehicle type"
var Message17	= "Trailer details selected without a valid vehicle type"
var Message18	= "Please select vehicle reg"
var Message19	= "Please select a vehicle length"
var Message20	= "Please select a vehicle height"
var Message21	= "Trailer details selected without a valid trailer type"
var Message22	= "Please select a Trailer length"
var Message23	= "Please select a Trailer height"
var Message24	= "Return vehicle details selected without a valid vehicle type"
var Message25	= "Return trailer details selected without a valid vehicle type"
var Message26	= "Please select return vehicle reg"
var Message27	= "Please select a return vehicle length"
var Message28	= "Please select a return vehicle height"
var Message29	= "Trailer details selected without a valid trailer type"
var Message30	= "Please select a return Trailer length"
var Message31	= "Please select a return Trailer height"

function padout(date)
{

	if (date.length==4)
	{
	
		date = "0" + date

	}

	return date;

}

function validateform()
{
	var f=document.CriteriaForm;

	if (f.DirectionFlag[1].checked) {
		f.OneWayFlag.value="on";
	} else {
		f.OneWayFlag.value="off";	
	}
	

	//if (!(f.TariffType[f.TariffType.selectedIndex].value)) {
		//Please select a Type of Tariff
	//	alert(Message1);
	//	f.TariffType.focus();
	//	return false;
	//}
	
	// Check that a valid Outward Route is selected
	if(f.OutRouteCode[f.OutRouteCode.selectedIndex].value=="none")
	{
		//Please select an Outward Route
		alert(Message2);
		f.OutRouteCode.focus();
	  return false;
  }
	else
	{
	  temp = f.OutRouteCode[f.OutRouteCode.selectedIndex].value.split('\t');
	  f.OutDepPoint.value=temp[0].substr(0,temp[0].indexOf("|"));
 	  f.OutArrPoint.value=temp[0].substr(temp[0].indexOf("|")+1);
	};

	// Check that a valid Return Route is selected or one way flag is checked
	if((f.RetRouteCodeSelect[f.RetRouteCodeSelect.selectedIndex].value=="none") && (!(f.OneWayFlag.value=="on")))
	{
		//Please select a Return Journey or check the One Way Journey box
		alert(Message3);
		f.RetRouteCodeSelect.focus();
		return false;
	}
	else
	{
	  temp = f.RetRouteCodeSelect[f.RetRouteCodeSelect.selectedIndex].value.split('\t');   
	  f.RetDepPoint.value=temp[0].substr(0,temp[0].indexOf("|"));
 	  f.RetArrPoint.value=temp[0].substr(temp[0].indexOf("|")+1);
	};

	// Check that Out and Return Route are different
	if(f.OutRouteCode[f.OutRouteCode.selectedIndex].value == f.RetRouteCodeSelect[f.RetRouteCodeSelect.selectedIndex].value)
	{
		//Outward and Return routes must be different, please choose another.
		alert(Message4);
		f.RetRouteCodeSelect.focus();
		return false;
	};

	// Check that a valid Outward Date is entered
	if((f.outday[f.outday.selectedIndex].value=="none")||(f.outmy[f.outmy.selectedIndex].value=="none"))
	{
		//Please select a valid outward date
		alert(Message5);
		f.outday.focus();
		return false;
	};

//alert(f.returndaySelect[f.returndaySelect.selectedIndex].value);
//alert(f.returnmySelect[f.returnmySelect.selectedIndex].value);
//alert(f.OneWayFlag.value);
//alert(f.OpenDatedFlag.checked);


	// Check that a valid Return Date is entered
  if(((f.returndaySelect[f.returndaySelect.selectedIndex].value=="none")||(f.returnmySelect[f.returnmySelect.selectedIndex].value=="none"))&&((!(f.OneWayFlag.value=="on")) && (!(f.OpenDatedFlag.checked))))
	{
		//Please select a valid return date or check the OPEN DATED TICKET box
		alert(Message6);
		f.returndaySelect.focus();
		return false;
	}
	else
	{
		f.returnday.value = f.returndaySelect[f.returndaySelect.selectedIndex].value;
		f.returnmy.value = f.returnmySelect[f.returnmySelect.selectedIndex].value;
	};

	// Check that the Return Date is after the Outward Date
	outdayval = f.outday[f.outday.selectedIndex].value;
	outdayval = parseInt(outdayval*10/10);	// *10/10 is required to make parseInt work properly
	outmyval = f.outmy[f.outmy.selectedIndex].value;
	outmyval = padout(outmyval)
	
	outdepdateval = outdayval + "/" + outmyval;
	
	f.OutDepDate.value=outdepdateval;

	if ((!(f.OneWayFlag.value=="on")) && (!(f.OpenDatedFlag.checked)))
	{	
		returndayval = f.returndaySelect[f.returndaySelect.selectedIndex].value;
		returndayval = parseInt(returndayval*10/10);
		returnmyval = f.returnmySelect[f.returnmySelect.selectedIndex].value;
		
		returnmyval = padout(returnmyval)
		
		retdepdateval = returndayval + "/" + returnmyval;

		f.RetDepDate.value=retdepdateval;
	
	  // Check that Return YEAR is same as or after the Outward YEAR
		if (parseInt((returnmyval.substr(3,2)*10/10)) < parseInt((outmyval.substr(3,2)*10/10)))
		{
			//Return date cannot be before outward date
			alert(Message7);
			f.returndaySelect.focus();
			return false;
		};
		// Check that Return MONTH is same as or after the Outward MONTH if Out and Return YEAR are same
		if (parseInt((returnmyval.substr(3,2)*10/10)) == parseInt((outmyval.substr(3,2)*10/10)))
		{
			if (parseInt((returnmyval.substr(0,2)*10/10)) < parseInt((outmyval.substr(0,2)*10/10)))
			{
				//Return date cannot be before outward date
				alert(Message7);
				f.returndaySelect.focus();
				return false;
			};
		};
		// Check that Return DAY is same as or after the Outward DAY if Out and Return YEAR and MONTH are same
		if ((parseInt((returnmyval.substr(3,2)*10/10)) == parseInt((outmyval.substr(3,2)*10/10))) && 
			 (parseInt((returnmyval.substr(0,2)*10/10)) == parseInt((outmyval.substr(0,2)*10/10))))
		{
			if (returndayval < outdayval)
			{
				//Return date cannot be before outward date
				alert(Message7);
				f.returndaySelect.focus();
				return false;
			};
		};
	};

	//Default time
	//f.OutDepTime.value = "10:10"
	//f.RetDepTime.value = "00:00"
	
	//Check that a valid Outward Time is entered
	if((f.outhrs[f.outhrs.selectedIndex].value=="none")||(f.outmins[f.outmins.selectedIndex].value=="none"))
	{
		//Please select a valid outward time.
		alert(Message8);
		f.outhrs.focus();
		return false;
	};

  f.OutDepTime.value=(f.outhrs[f.outhrs.selectedIndex].value + ":" + f.outmins[f.outmins.selectedIndex].value)

	//Check that a valid Return Time is entered
	if(((f.returnhrsSelect[f.returnhrsSelect.selectedIndex].value=="none") || (f.returnminsSelect[f.returnminsSelect.selectedIndex].value=="none"))&&((!(f.OneWayFlag.value=="on"))&&(!(f.OpenDatedFlag.checked))))
	{
		//Please select a valid return time
		alert(Message9);
		f.returnhrsSelect.focus();
		return false;
	}
	else
	{	
		f.returnhrs.value = f.returnhrsSelect[f.returnhrsSelect.selectedIndex].value;
		f.returnmins.value = f.returnminsSelect[f.returnminsSelect.selectedIndex].value;
	};

	f.RetDepTime.value=(f.returnhrs.value + ":" + f.returnmins.value);

	// Check that Return TIME is same as or after the Outward TIME if Out and Return YEAR, MONTH and DATE are same

	if((!(f.OneWayFlag.value=="on"))&&(!(f.OpenDatedFlag.checked)))
	{
	   if ((parseInt(returnmyval.substr(3,2)*10/10) == parseInt(outmyval.substr(3,2)*10/10)) && 
	       (parseInt(returnmyval.substr(0,2)*10/10) == parseInt(outmyval.substr(0,2)*10/10)) &&
	       (returndayval == outdayval))
	   {
			outhrs = f.outhrs.value;
			if (outhrs == "24") {outhrs = "00";}
			outhrs = parseInt(outhrs*10/10)
			outmins = f.returnminsSelect[f.outmins.selectedIndex].value;
			outmins = parseInt(outmins*10/10);
			returnhrs = f.returnhrsSelect[f.returnhrsSelect.selectedIndex].value;
			if (returnhrs == "24") {returnhrs = "00";}
			returnhrs = parseInt(returnhrs*10/10);
			returnmins = f.returnminsSelect[f.returnminsSelect.selectedIndex].value;
			returnmins = parseInt(returnmins*10/10);
			
			if (returnhrs < outhrs)
			{
				//Return time cannot be before outward time
			  alert(Message10);
		    f.returnhrsSelect.focus();
			  return false;
			};

			if (returnhrs == outhrs)
			{
				if (returnmins <= outmins)
				{
					//Return time must be after outward time
					//alert(Message11);
					//f.returnminsSelect.focus();
					//return false;
				}
			};
	   };
  };
  
	// One Way Nulls
	if (!(f.OneWayFlag.value=="on"))
	{
	//f.RetRouteCode.value = "";
		f.returnday.value = "";
		f.returnmy.value = "";
		f.returnhrs.value = "";
		f.returnmins.value = "";
	};

	// OpenDated Nulls
	if(f.OpenDatedFlag.checked)
	{
		f.returnday.value = "";
		f.returnmy.value = "";
		f.returnhrs.value = "";
		f.returnmins.value = "";
	};
	
	// Check that a valid Tariff is selected
	if(f.TariffType[f.TariffType.selectedIndex].value){
	 	tariffSplit = f.TariffType[f.TariffType.selectedIndex].value.split('\t');
	 	//alert('here'+tariffSplit[0]+'/'+tariffSplit[1]);
		f.Band.value = tariffSplit[0];
		f.Tariff.value = tariffSplit[1];
	}	else {
		f.Band.value = "";
		f.Tariff.value = "";
	};
	
	// Check if alternate Product/Band selected
	if(f.AlternateBand.value)
	{
		if(f.TariffType[f.TariffType.selectedIndex].value)
		{
			//Either normal or promotional fare may be selected, not both
		  alert(Message12);
		  f.TariffType.focus();
		  return false;
		}
		else
		{
	    f.Band.value = f.AlternateBand.value;
		};
	};

	// Check if alternate MarketSector/Tariff selected
	if(f.AlternateTariff.value)
	{
		if(f.TariffType[f.TariffType.selectedIndex].value)
		{
			//Either normal or promotional fare may be selected, not both
		  alert(Message12);
		  f.TariffType.focus();
		  return false;
		}
		else
		{
	  	f.Tariff.value = f.AlternateTariff.value;
		};
	};

	if (!(f.OneWayFlag.value=="on"))
  {
		if (f.RetTotalPaxQty.value == 0)
		{	
			//No return passengers specified
			alert(Message14);
			f.RetTotalPaxQty.focus();
			return false;
		};
	};

	// Vehicle Type NOT selected
	
	//if (!(f.VehType[f.VehType.selectedIndex].value))
	//{

		// Vehicle details selected without a valid vehicle type
		//	if (f.VehReg.value) 
		//	{
		//Vehicle details selected without a valid vehicle type
		//		alert(Message16);
		//		f.VehType.focus();
		//		return false;		
		//	}

		// Trailer details selected without a valid vehicle type
		//if ((f.TrlType.value) || 
		//    (f.TrlLength[f.TrlLength.selectedIndex].value != "0") || 
		//    (f.TrlHeight[f.TrlHeight.selectedIndex].value != "0"))
		//{
		//Trailer details selected without a valid vehicle type
		//	alert(Message17);
		//	f.VehType.focus();
		//	return false;		
		//}			
	//};


		// Vehicle Reg

		//if ((f.VehType[f.VehType.selectedIndex].value) && (!(f.VehReg.value)))
		//{
			//Please select vehicle reg
		//	alert(Message18);
		//	f.VehReg.focus();
		//	return false;
		//};
	
		// Vehicle Length
		
if(f.VehType && typeof(f.VehType) != "undefined" && f.VehType != "undefined")
{

		if (f.VehType[f.VehType.selectedIndex].value != ' ') {
			if (f.VehLengthHeight[f.VehLengthHeight.selectedIndex].value == "0")
			{
			//Please select a vehicle length
				alert(Message19);
				f.VehLengthHeight.focus();
				return false;
			} else {			
				VehicleLengthHeightSplit = f.VehLengthHeight[f.VehLengthHeight.selectedIndex].value.split('\t');				
				f.VehLength.value = VehicleLengthHeightSplit[0];
				f.VehHeight.value = VehicleLengthHeightSplit[1];	
			}
		};
	
		// Vehicle Height

		//if ((f.VehType[f.VehLength.selectedIndex].value) && (f.VehHeight[f.VehHeight.selectedIndex].value == "0"))
		//{
		//Please select a vehicle height
		//	alert(Message20);
		//	f.VehHeight.focus();
		//	return false;
		//};

		// Trailer Type
	
		//if (!(f.TrlType[f.TrlType.selectedIndex].value))
		//{
			// Trailer details selected without a valid trailer type
		//	if ((f.TrlLength[f.TrlLength.selectedIndex].value != "0") || 
		//	    (f.TrlHeight[f.TrlHeight.selectedIndex].value != "0"))
		//	{
		//Trailer details selected without a valid trailer type
		//		alert(Message21);
		//		f.TrlType.focus();
		//		return false;		
		//	}
		//};
	
		// Trailer Length

		if (!(f.TrlType[f.TrlType.selectedIndex].value==" ")) {
			if (f.TrlLengthHeight[f.TrlLengthHeight.selectedIndex].value == "0")
			{
			//Please select a Trailer length
				alert(Message22);
				f.TrlLengthHeight.focus();
				return false;
			} else {			
				TrlLengthHeightSplit = f.TrlLengthHeight[f.TrlLengthHeight.selectedIndex].value.split('\t');				
				f.TrlLength.value = TrlLengthHeightSplit[0];
				f.TrlHeight.value = TrlLengthHeightSplit[1];				
			}
		};

		// Trailer Height

		//if ((f.TrlType[f.TrlType.selectedIndex].value) && //(f.TrlHeight[f.TrlHeight.selectedIndex].value == "0"))
		//{
		//Please select a Trailer height
		//	alert(Message23);
		//	f.TrlHeight.focus();
		//	return false;
		//};
		
	//if ((!(f.OneWayFlag.value=="on")) && (!(f.copyveh.checked))) {		
	if ((!(f.OneWayFlag.value=="on"))) {		
		// Vehicle Length ret

		if (f.VehTypeRet[f.VehTypeRet.selectedIndex].value != ' ') {
			if (f.VehLengthHeightRet[f.VehLengthHeightRet.selectedIndex].value == "0")
			{
			//Please select a vehicle length
				alert(Message19);
				f.VehLengthHeightRet.focus();
				return false;
			} else {			
				VehicleLengthHeightRetSplit = f.VehLengthHeightRet[f.VehLengthHeightRet.selectedIndex].value.split('\t');				
				f.VehLengthRet.value = VehicleLengthHeightRetSplit[0];
				f.VehHeightRet.value = VehicleLengthHeightRetSplit[1];				
			}
		};

		// Trailer Length Ret

		if (!(f.TrlTypeRet[f.TrlTypeRet.selectedIndex].value==" ")) {
			if (f.TrlLengthHeightRet[f.TrlLengthHeightRet.selectedIndex].value == "0")
			{
			//Please select a Trailer length
				alert(Message22);
				f.TrlLengthHeightRet.focus();
				return false;
			} else {
				TrlLengthHeightRetSplit = f.TrlLengthHeightRet[f.TrlLengthHeightRet.selectedIndex].value.split('\t');				
				f.TrlLengthRet.value = TrlLengthHeightRetSplit[0];
				f.TrlHeightRet.value = TrlLengthHeightRetSplit[1];
			}
		};		  
	}
	
	//if ((!(f.OneWayFlag.value=="on")) && (f.copyveh.checked)) {	
	if ((!(f.OneWayFlag.value=="on"))) {	
		VehicleLengthHeightSplit = f.VehLengthHeight[f.VehLengthHeight.selectedIndex].value.split('\t');				

		f.VehLengthRet.value = VehicleLengthHeightSplit[0];
		f.VehHeightRet.value = VehicleLengthHeightSplit[1];
	}				
				
} // Check if vehicle defined on page

	//set operating company based on route
	for (x=0;x<RouteOpCoArray[0].length;x++)
	{
		if (RouteOpCoArray[0][x] == (f.OutDepPoint.value + f.OutArrPoint.value))
		{
			f.OperatingCompany.value = RouteOpCoArray[1][x];
			break;
		}	
	}	

	//
	if (!(document.getElementById("VehType") == null))
	{

		handleBackButton();

		//populate selected values in case user returns to page
		document.BackCheck.travellingfromstored.value = document.CriteriaForm.TravelGroup.value;
		document.BackCheck.depfromstored.value = document.CriteriaForm.OutRouteCode.value;
		document.BackCheck.retfromstored.value = document.CriteriaForm.RetRouteCodeSelect.value;
		document.BackCheck.deppaxcountstored.value = document.CriteriaForm.OutTotalPaxQty.value;
		document.BackCheck.retpaxcountstored.value = document.CriteriaForm.RetTotalPaxQty.value;
		document.BackCheck.depvehstored.value = document.CriteriaForm.VehType.value;
		document.BackCheck.depvehlenstored.value = document.CriteriaForm.VehLengthHeight.value;
		document.BackCheck.deptrlstored.value = document.CriteriaForm.TrlType.value;
		document.BackCheck.deptrllengthstored.value = document.CriteriaForm.TrlLengthHeight.value;
		document.BackCheck.retvehstored.value = document.CriteriaForm.VehTypeRet.value;
		document.BackCheck.retvehlengthstored.value = document.CriteriaForm.VehLengthHeightRet.value;
		document.BackCheck.rettrlstored.value = document.CriteriaForm.TrlTypeRet.value;
		document.BackCheck.rettrllengthstored.value = document.CriteriaForm.TrlLengthHeightRet.value;
		document.BackCheck.bookoffersstored.value = document.CriteriaForm.TariffType.value;
		
		document.BackCheck.retmonthstored.value = document.CriteriaForm.returnmySelect.value;
		document.BackCheck.retdaystored.value = document.CriteriaForm.returndaySelect.value;
		document.BackCheck.outmonthstored.value = document.CriteriaForm.outmy.value;
		document.BackCheck.outdaystored.value = document.CriteriaForm.outday.value;
	}
	
	return(true)


};

function isValidDate(varMonth,varDay,varYear)
{
		 	var month;
			
			month = parseInt(varMonth);

			if ((month==4 || month==6 || month==9 || month==11) && parseInt(varDay) == 31)
			{
				//alert("Month "+arrmonthChar[month]+" has only 30 days!");
				return(false);
			}
		
			if (month == 2)
			{ 
				
				var year = parseInt("20"+varYear);	  
				year = parseInt(year);

				var day = parseInt(varDay);
				
				var yearfloat = (year / 4);
				var yearint = parseInt(yearfloat);
				var yearCheck = (yearint * 4)
				if(year == yearCheck)
				{
					Check4 = true;
				}
				else
				{
					Check4 = false;
				}

				yearfloat = (year / 100);
				yearint = parseInt(yearfloat);
				yearCheck = (yearint * 100)
				if(year == yearCheck)
				{
					Check100 = true;
				}
				else
				{
					Check100 = false;
				}
	
				yearfloat = (year / 400);
				yearint = parseInt(yearfloat);
				yearCheck = (yearint * 400)
				if(year == yearCheck)
				{
					Check400 = true;
				}
				else
				{
					Check400 = false;
				}
	
				var CheckLeapYear = ((Check4 == true) && (Check100 != true) || (Check400 == true))
			
				if(CheckLeapYear == true)
				{
					if(day > 29)
					{
						//alert("February in "+ year +" has only 29 days!");						
						return(false);
					}
				}
					
				if(CheckLeapYear == false)
				{
					if(day > 28)
					{
						//alert("February in "+ year +" has only 28 days!");						
						return(false);
					}
				}	
			 }
			 		 
			 return(true);
}
