var oDateRegex = new RegExp("(?=\\d)^(?:(?!(?:10\\D(?:0?[5-9]|1[0-4])\\D(?:1582))|(?:0?9\\D(?:0?[3-9]|1[0-3])\\D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!/31)(?!-31)(?!\\.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\\d\\d)(?:[02468][048]|[13579][26])(?!\\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\\x20BC))))))|(?:0?2(?=.(?:(?:\\d\\D)|(?:[01]\\d)|(?:2[0-8])))))([-.\\/])(0?[1-9]|[12]\\d|3[01])\\2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?\\d)\\x20BC)|(?:\\d{4}(?!\\x20BC)))\\d{4}(?:\\x20BC)?)(?:$|(?=\\x20\\d)\\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\\d){0,2}(?:\\x20[aApP][mM]))|(?:[01]\\d|2[0-3])(?::[0-5]\\d){1,2})?$");

function SetSearch(type)
{
	var trHotels = document.getElementById('trHotels');
	var divVacations = document.getElementById('divVacations');

	if(divFlight) divFlight.style.display = 'none';
	if(divHotel) divHotel.style.display = 'none';
	if(trHotels) trHotels.style.display = 'none';
	if(divCar) divCar.style.display = 'none';
	if(divCruise) divCruise.style.display = 'none';
	if(divFlightCarHotel) divFlightCarHotel.style.display = 'none';
	if(divVacations) divVacations.style.display = 'none';

	if(type==0)
	{   
		if(divFlight) divFlight.style.display = '';
	}
	else if(type==1)
	{
		if(divCar) divCar.style.display = '';
	}
	else if(type==2)
	{
		if(divHotel) divHotel.style.display = '';
//		if(trHotels) trHotels.style.display = '';
	}
	else if(type==3)
	{
		if(divCruise) divCruise.style.display = '';
	}
	else if(type==4||type==5)
	{
		if(divFlightCarHotel) divFlightCarHotel.style.display = '';
	}
	else if(type==6)
	{
		if(divVacations) divVacations.style.display = '';
	}
	else
	{
		if(divFlight) divFlight.style.display = '';
	}
}

function SetHotelSearch(type)
{
	var tblHotelSearch = document.getElementById('tblHotelSearch');
	var lnkHotelAdvanced = document.getElementById('lnkHotelAdvanced');
	var lnkHotelBasic = document.getElementById('lnkHotelBasic');

	if(tblHotelSearch) tblHotelSearch.style.display = 'none';
	if(lnkHotelAdvanced) lnkHotelAdvanced.style.display = 'none';
	if(lnkHotelBasic) lnkHotelBasic.style.display = 'none';

	if(type==1)
	{
		if(lnkHotelAdvanced) lnkHotelAdvanced.style.display = '';
	}
	else
	{
		if(tblHotelSearch) tblHotelSearch.style.display = '';
		if(lnkHotelBasic) lnkHotelBasic.style.display = '';
	}
}

function ValidateFlightSearch(strSBName)
{
	var txtDepartCity = document.getElementById(strSBName+"_txtDepartCity");
	var txtDepartDate = document.getElementById(strSBName+"_txtDepartDate");
	var txtDestinationCity = document.getElementById(strSBName+"_txtDestinationCity");
	var txtReturnDate = document.getElementById(strSBName+"_txtReturnDate");

	var imgDepartCityError = document.getElementById(strSBName+"_imgDepartCityError");
	var imgDepartDateError = document.getElementById(strSBName+"_imgDepartDateError");
	var imgDestCityError = document.getElementById(strSBName+"_imgDestCityError");
	var imgReturnDateError = document.getElementById(strSBName+"_imgReturnDateError");

	// If the javascript is busted, validate and let the server-side code handle it
	if(!txtDepartCity || !txtDestinationCity || !txtDepartDate || !txtReturnDate) return true;

	if(txtDepartCity.value == "")
	{
		if(imgDepartCityError) imgDepartCityError.style.display = "";
		else alert('A departure city is required.');

		txtDepartCity.focus();
		return false;
	}

	if(txtDestinationCity.value == "")
	{
		if(imgDestCityError) imgDestCityError.style.display = "";
		else alert('A destination city is required.');

		txtDestinationCity.focus();
		return false;
	}

	if(!oDateRegex.test(txtDepartDate.value))
	{
		if(imgDepartDateError) imgDepartDateError.style.display = "";
		else alert('Invalid departure date.');

		txtDepartDate.focus();
		return false;
	}

	if(!oDateRegex.test(txtReturnDate.value))
	{
		if(imgReturnDateError) imgReturnDateError.style.display = "";
		else alert('Invalid return date.');

		txtReturnDate.focus();
		return false;
	}

	return true;
}

function ValidateCarSearch(strSBName)
{
	var dPickup;
	var dDropoff;
	var txtPickUpCity = document.getElementById(strSBName+"_txtPickUpCity");
	var txtPickUpDate = document.getElementById(strSBName+"_txtPickUpDate");
	var txtDropOffDate = document.getElementById(strSBName+"_txtDropOffDate");
	var ddlPickUpTime = document.getElementById(strSBName+"_ddlPickUpTime");
	var ddlDropOffTime = document.getElementById(strSBName+"_ddlDropOffTime");

	var imgPickUpCityError = document.getElementById(strSBName+"_imgCityError");
	var imgPickUpDateError = document.getElementById(strSBName+"_imgPickUpDateError");
	var imgPickUpTimeError = document.getElementById(strSBName+"_imgPickUpTimeError");
	var imgDropOffDateError = document.getElementById(strSBName+"_imgDropOffDateError");
	var imgDropOffTimeError = document.getElementById(strSBName+"_imgDropOffTimeError");
	var imgDropOffError = document.getElementById(strSBName+"_imgDropOffError");

	if(!txtPickUpCity || !txtPickUpDate || !txtDropOffDate || !ddlPickUpTime || !ddlDropOffTime) return true;

	if(imgPickUpCityError) imgPickUpCityError.style.display = "none";
	if(imgPickUpDateError) imgPickUpDateError.style.display = "none";
	if(imgPickUpTimeError) imgPickUpTimeError.style.display = "none";
	if(imgDropOffDateError) imgDropOffDateError.style.display = "none";
	if(imgDropOffTimeError) imgDropOffTimeError.style.display = "none";
	if(imgDropOffError) imgDropOffError.style.display = "none";

	if(txtPickUpCity.value == "")
	{
		if(imgPickUpCityError) imgPickUpCityError.style.display = "";
		else alert('A pick-up city is required.');

		txtPickUpCity.focus();
		return false;
	}

	if(!oDateRegex.test(txtPickUpDate.value))
	{
		if(imgPickUpDateError) imgPickUpDateError.style.display = "";
		else alert('Invalid pick-up date.');

		txtPickUpDate.focus();
		return false;
	}
	else
	{
		var strHour = ddlPickUpTime.options[ddlPickUpTime.selectedIndex].value;
		var iHour = new Number(strHour.substring(0, strHour.length-1));

		var d = new Date();
		var dPickup = new Date(txtPickUpDate.value);
		dPickup.setHours(iHour);

		if(dPickup < d)
		{
			if(imgPickUpTimeError) imgPickUpTimeError.style.display = "";
			else alert('Pick-up time must be later than the current local time.');

			ddlPickUpTime.focus();
			return false;
		}
	}

	if(!oDateRegex.test(txtDropOffDate.value))
	{
		if(imgDropOffDateError) imgDropOffDateError.style.display = "";
		else alert('Invalid drop-off date.');

		txtDropOffDate.focus();
		return false;
	}
	else
	{
		var strHour = ddlDropOffTime.options[ddlDropOffTime.selectedIndex].value;
		var iHour = new Number(strHour.substring(0, strHour.length-1));

		var d = new Date();
		var dDropoff = new Date(txtDropOffDate.value);
		dDropoff.setHours(iHour);

		if(dDropoff < d)
		{
			if(imgDropOffTimeError) imgDropOffTimeError.style.display = "";
			else alert('Drop-off time must be later than the current local time.');

			ddlDropOffTime.focus();
			return false;
		}

		if(dDropoff < dPickup)
		{
			if(imgDropOffError) imgDropOffError.style.display = "";
			else alert('Drop-off date must be later than the pick-up date.');

			ddlDropOffTime.focus();
			return false;
		}
	}

	return true;
}

function ValidateHotelSearch(strSBName)
{
	var txtCity = document.getElementById(strSBName+"_txtCity");
	var txtInDate = document.getElementById(strSBName+"_txtCheckInDate");
	var txtOutDate = document.getElementById(strSBName+"_txtCheckOutDate");

	var imgCityError = document.getElementById(strSBName+"_imgCityError");
	var imgInDateFmtError = document.getElementById(strSBName+"_imgInDateFormatError");
	var imgOutDateFmtError = document.getElementById(strSBName+"_imgOutDateFormatError");
	var imgOutError = document.getElementById(strSBName+"_imgHotelOutError");

	if(!txtCity || !txtInDate || !txtOutDate) return true;

	if(imgCityError) imgCityError.style.display = "none";
	if(imgInDateFmtError) imgInDateFmtError.style.display = "none";
	if(imgOutDateFmtError) imgOutDateFmtError.style.display = "none";
	if(imgOutError) imgOutError.style.display = "none";

	if(txtCity.value == "")
	{
		if(imgCityError) imgCityError.style.display = "";
		else alert('A city is required.');

		txtCity.focus();
		return false;
	}

	if(!oDateRegex.test(txtInDate.value))
	{
		if(imgInDateFmtError) imgInDateFmtError.style.display = "";
		else alert('Invalid check-in date.');

		txtInDate.focus();
		return false;
	}
	else
	{
		var d = new Date(txtInDate.value);
		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate());

		if(d < today)
		{
			if(imgInDateFmtError) imgInDateFmtError.style.display = "";
			else alert('Invalid check-in date.');

			txtInDate.focus();
			return false;
		}
	}

	if(!oDateRegex.test(txtOutDate.value))
	{
		if(imgOutDateFmtError) imgOutDateFmtError.style.display = "";
		else alert('Invalid check-out date.');

		txtOutDate.focus();
		return false;
	}	
	else
	{
		var d = new Date(txtOutDate.value);
		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate());

		if(d < today)
		{
			if(imgOutError) imgOutError.style.display = "";
			else alert('Invalid check-out date.');

			txtOutDate.focus();
			return false;
		}
	}

	if(new Date(txtOutDate.value) <= new Date(txtInDate.value))
	{
		if(imgOutError) imgOutError.style.display = "";
		else alert('Check-out date must be after check-in date.');

		txtOutDate.focus();
		return false;
	}

	return true;
}

function ReformatDate(strDate)
{
	var d = new Date(strDate);
	var tmp = window.gfPop.gsSplit;
	window.gfPop.gsSplit = "-";

	var str = window.gfPop.fFormatDate(d.getFullYear(), d.getMonth()+1, d.getDate());
	window.gfPop.gsSplit = tmp;

	return str;
}

function DoFlightSearch(strSBName)
{
	var txtDepartCity = document.getElementById(strSBName+"_txtDepartCity");
	var txtDepartDate = document.getElementById(strSBName+"_txtDepartDate");
	var txtDestinationCity = document.getElementById(strSBName+"_txtDestinationCity");
	var txtReturnDate = document.getElementById(strSBName+"_txtReturnDate");
	var ddlAdults = document.getElementById(strSBName+"_ddlAdults");
	var btnSearch = document.getElementById(strSBName+"_btnSearch");

	if(!btnSearch || !txtDepartCity || !txtDepartDate || !txtDestinationCity || !txtReturnDate || !ddlAdults) return;

	var qs = "";

	qs += "dptc="+escape(txtDepartCity.value);
	qs += "&dptd="+ReformatDate(txtDepartDate.value);
	qs += "&dstc="+escape(txtDestinationCity.value);
	qs += "&rd="+ReformatDate(txtReturnDate.value);
	qs += "&ad="+ddlAdults.options[ddlAdults.selectedIndex].value;

	btnSearch.href = "FlightResults.aspx?"+qs;
}

function DoCruiseSearch(strSBName)
{
	var oCLD = document.getElementById(strSBName+"_ddlCruiseLine");
	var oCSM = document.getElementById(strSBName+"_ddlSailMonth");
	var oCSY = document.getElementById(strSBName+"_ddlSailYear");
	var oPD = document.getElementById(strSBName+"_ddlPort");
	var oDTD = document.getElementById(strSBName+"_ddlDestination");
	var oDD = document.getElementById(strSBName+"_ddlDuration");
	var oAnchor = document.getElementById(strSBName+"_btnSearch");
	if(!oAnchor) return;

	var qs = "";

	if(oCLD) qs += "&VendorID[]="+oCLD.options[oCLD.selectedIndex].value;
	if(oCSM) qs += "&cruise_month="+oCSM.options[oCSM.selectedIndex].value;
	if(oCSY) qs += "&cruise_year="+oCSY.options[oCSY.selectedIndex].value;
	if(oPD) qs += "&portid="+oPD.options[oPD.selectedIndex].value;
	if(oDTD) qs += "&DestinationID="+oDTD.options[oDTD.selectedIndex].value;
	if(oDD) qs += "&cruise_duration="+oDD.options[oDD.selectedIndex].value;

	oAnchor.href = "https://booking.ytbtravel.com/travel/cruise/search.rvlx?PIN="+oAnchor.getAttribute("YTBId")+qs;
}

function ValidateFCHSearch(strSBName)
{
	var oDepartCity = document.getElementById(strSBName+"_txtDepartureCity");
	var oDepartDate = document.getElementById(strSBName+"_txtDepartDate");
	var oDestinationCity = document.getElementById(strSBName+"_txtDestinationCity");
	var oReturnDate = document.getElementById(strSBName+"_txtReturnDate");

	var oDepartCityError = document.getElementById(strSBName+"_imgDepartCityError");
	var oDestinationCityError = document.getElementById(strSBName+"_imgDestinationCityError");
	var oDepartDateError = document.getElementById(strSBName+"_imgDepartDateError");
	var oReturnDateError = document.getElementById(strSBName+"_imgReturnDateError");

	if(oDepartCityError) oDepartCityError.style.display = "none";
	if(oDestinationCityError) oDestinationCityError.style.display = "none";
	if(oDepartDateError) oDepartDateError.style.display = "none";
	if(oReturnDateError) oReturnDateError.style.display = "none";

	if(oDepartCity.value == "")
	{
		if(oDepartCityError) oDepartCityError.style.display = "";
		else alert('A departure city is required.');

		oDepartCity.focus();
		return false;
	}

	if(oDestinationCity.value == "")
	{
		if(oDestinationCityError) oDestinationCityError.style.display = "";
		else alert('A destination city is required.');

		oDestinationCity.focus();
		return false;
	}

	if(!oDateRegex.test(oDepartDate.value))
	{
		if(oDepartDateError) oDepartDateError.style.display = "";
		else alert('Invalid departure date.');

		oDepartDate.focus();
		return false;
	}

	if(!oDateRegex.test(oReturnDate.value))
	{
		if(oReturnDateError) oReturnDateError.style.display = "";
		else alert('Invalid return date.');

		oReturnDate.focus();
		return false;
	}

	return true;
}