var travelWindow;
// Variables with names starting with om are used in setting custom evars in order to send details of travel searches to Omniture.
// Use the om variables rather than setting the evars directly to make it easier to change which evars we use if necessary - simply need to alter the sendTravelSearchEvars() function.
var omTravelType = "";
var omDepOrLocation = "";
var omDestination = "";
var omDaysBookToTravel = "";
var omTravelDuration = "";
var omNumRooms = "";
var omNumACS = "";
var omCarType = "";
var omDateToday = new Date();

function sendTravelSearchEvars(){
	var s=s_gi(s_account);
	s.linkTrackVars = 'eVar2,eVar3,eVar4,eVar5,eVar6,eVar8,eVar9,eVar10'; 
	s.linkTrackEvents = 'None';
	s.eVar2 = omTravelType;
	s.eVar3 = omDepOrLocation;
	s.eVar4 = omDestination;
	s.eVar5 = omDaysBookToTravel;
	s.eVar6 = omTravelDuration;
	s.eVar8 = omNumRooms;
	s.eVar9 = omNumACS;
	s.eVar10 = omCarType;
	s.tl(this,'o','Search');
	s.eVar2 = s.eVar3 = s.eVar4 = s.eVar5 = s.eVar6 = s.eVar8 = s.eVar9 = s.eVar10 = '';
}
function hide(id) {
	var element = document.getElementById(id).style;					    	
	element.display = 'none';
}
function show(id) {
	var element = document.getElementById(id).style;					    	
	element.display = 'block';
}
function openTravelProvider() {	
	// As window content loads slowly for travel, close any existing travel window first.  Otherwise will see previous results for several seconds.
	if(travelWindow){
		travelWindow.close();
		travelWindow = null;
	}
	travelWindow = window.open('','travel','width=800,height=425,location=yes,menubar,scrollbars,status,resizable,toolbar');
	travelWindow.focus();
}
function validateFlight(flightForm, single) {	
	var from = flightForm.FrAirport.value;
	if (from.length == 0) {
		alert('Please enter a departure airport');
		return false;
	}
	if (from.length < 3) {
		alert('Your departure must be at least 3 characters in length');
		return false;
	}
	var to = flightForm.ToAirport.value;
	if (to.length < 3) {
		alert('Your destination must be at least 3 characters in length');
		return false;
	}
	var fromDate = getDateInt(flightForm.FromDate.value);
	var toDate = getDateInt(flightForm.ToDate.value);
	
	if (single !=null && single == true) {
		if (fromDate == -1) {
			alert("Please enter your date using the dd/mm/yyyy format.");
			return false;
		}
	}
	else {
		if (fromDate == -1 || toDate == -1) {
			alert("Please enter both dates using the dd/mm/yyyy format.");
			return false;
		}
	}
	
	if (!(validatePeople(flightForm))) {		
		return false;
	}
	if (single !=null && single != true) {
		if (!(isDateBeforeDate(flightForm.FromDate,flightForm.ToDate))) {
			alert("Your return flight must be after your outbound one.");
			return false;
		}
	}
	if (isDateInPast(flightForm.FromDate) || (single !=null && single != true && isDateInPast(flightForm.ToDate))) {
		alert("Your chosen dates must be in the future");
		return false;
	}		
	omDepOrLocation = flightForm.FrAirport.value;
	omDestination = flightForm.ToAirport.value;
	omDaysBookToTravel = Math.floor((fromDate - omDateToday)/86400000)+1;
	if(flightForm.TripType[0].checked){
		omTravelDuration = (toDate - fromDate)/86400000;
	}else{
		omTravelDuration = 0;
	}
	omNumRooms = "";
	omNumACS = getACS(flightForm);
	omCarType = "";
	sendTravelSearchEvars();
	openTravelProvider();
	return true;	
}
function validateHotel(hotelForm) {
	var cityName = hotelForm.CityName.value;	
	if (cityName.length < 3) {
		alert('Your destination must be at least 3 characters in length');
		return false;
	}
	var inDate = getDateInt(hotelForm.InDate.value);
	var outDate = getDateInt(hotelForm.OutDate.value);
	
	if (inDate == -1 || outDate == -1) {
		alert("Please enter both dates using the dd/mm/yyyy format.");
		return false;
	}
	if (!(isDateBeforeDate(hotelForm.InDate,hotelForm.OutDate))) {
		alert("Your last night must be after your first night.");
		return false;
	}
	if (isDateInPast(hotelForm.InDate) || isDateInPast(hotelForm.OutDate)) {
		alert("Your chosen dates must be in the future");
		return false;
	}
	if (!(validatePeople(hotelForm))) {		
		return false;
	}
	omDepOrLocation = hotelForm.CityName.value;
	omDestination = "";
	omDaysBookToTravel = Math.floor((inDate - omDateToday)/86400000)+1;
	omTravelDuration = (outDate - inDate)/86400000;
	omNumRooms = hotelForm.NumRoom.value;
	omNumACS = getACS(hotelForm);
	omCarType = "";
	sendTravelSearchEvars();
	openTravelProvider();
	return true;	
}
function validateCar(carForm) {
	var pickUpLoc = carForm.PickUpLoc.value;
	if (pickUpLoc.length < 3) {
		alert('Your pick-up location must be at least 3 characters in length');
		return false;
	}
	var fromDate = getDateInt(carForm.FromDate.value);
	var toDate = getDateInt(carForm.ToDate.value);
	
	if (fromDate == -1 || toDate == -1) {
		alert("Please enter both dates using the dd/mm/yyyy format.");
		return false;
	}
	if (isDateInPast(carForm.FromDate) || isDateInPast(carForm.ToDate)) {
		alert("Your chosen dates must be in the future");
		return false;
	}
	// For other options (eg Hotel), it's enough to set omTravelType in the appropriate tbeShowState function,
	// but the Car and Activities page doesn't run those functions, so set omTravelType here in the validation as well to cater for that page.
	omTravelType="Car"; 
	omDepOrLocation = carForm.PickUpLoc.value;
	omDestination = "";
	omDaysBookToTravel = Math.floor((fromDate - omDateToday)/86400000)+1;
	omTravelDuration = (toDate - fromDate)/86400000;
	omNumRooms = "";
	omNumACS = "";
	omCarType = carForm.Class.value;
	sendTravelSearchEvars();
	openTravelProvider();
	return true;	
}
function validatePackage(packageForm) {

	if (document.getElementById('o2').checked){	
		packageForm.rfrr.value = '30004-437.SE.A4';
	}
	if (document.getElementById('o4').checked){	
		packageForm.rfrr.value = '30004-437.SE.A5';
	}
	if (document.getElementById('o6').checked){	
		packageForm.rfrr.value = '30004-437.SE.A6';
	}

	var from = packageForm.FrAirport.value;
	if (from.length == 0) {
		alert('Please enter a departure airport');
		return false;
	}
	if (from.length < 3) {
		alert('Your departure must be at least 3 characters in length');
		return false;
	}
	var to = packageForm.DestName.value;
	if (to.length < 3) {
		alert('Your destination must be at least 3 characters in length');
		return false;
	}
	var fromDate = getDateInt(packageForm.FromDate.value);
	var toDate = getDateInt(packageForm.ToDate.value);
	
	if (fromDate == -1 || toDate == -1) {
		alert("Please enter both dates using the dd/mm/yyyy format.");
		return false;
	}
	if (!(validatePeople(packageForm))) {		
		return false;
	}
	if (!(isDateBeforeDate(packageForm.FromDate,packageForm.ToDate))) {
		alert("Your return flight must be after your outbound one.");
		return false;
	}
	if (isDateInPast(packageForm.FromDate) || isDateInPast(packageForm.ToDate)) {
		alert("Your chosen dates must be in the future");
		return false;
	}
	omDepOrLocation = from;
	omDestination = to;
	omDaysBookToTravel = Math.floor((fromDate - omDateToday)/86400000)+1;
	omTravelDuration = (toDate - fromDate)/86400000;
	if(omTravelType == "Flight+Car"){
		omNumRooms = "";
	}else{
		omNumRooms = packageForm.NumRoom.value;
	}
	omNumACS = getACS(packageForm);	
	omCarType = "";
	sendTravelSearchEvars();
	openTravelProvider();
	return true;
}
function getDateInt(inDate) {
	if(inDate.length==0 || inDate == 'dd/mm/yyyy') {
		return -1;
	}
	else {
		index1 = inDate.indexOf("/");
		index2 = inDate.lastIndexOf("/");
		if (index1 >= 0 && index2 >= 0)	{
			var year = inDate.substring(index2 + 1, inDate.length);
			var month;
			if (index1 == index2) {
				today = new Date();
				year = today.getYear();
				if (year < 2000)
					year = 1900 + year;
				month = inDate.substring(index1 + 1, inDate.length) - 1;
			}

			else
				month = inDate.substring(index1 + 1, index2) - 1;

			if (year.length == 2)
				year = "20" + year;

			var tempDate = new Date(year, month, inDate.substring(0, index1));

			if (isNaN(tempDate.getTime()))
				return 0;
			else
				return tempDate.getTime();
		}
		else
		{
			return 0;
		}

	}
}
function validatePeople(tForm) {
	if (tForm.NumAdult.value == 0 && tForm.NumChild.value == 0 && tForm.NumSenior.value == 0) {
		alert("At least one person must travel.");
		return false;
	}	
	if ((parseInt(tForm.NumAdult.value) + parseInt(tForm.NumChild.value) + parseInt(tForm.NumSenior.value)) > 6) {
		alert("For groups of 7 people or more, please call 0845 610 0320 to check availability and place a booking");
		return false;
	}
	return true;
}
function getACS(aForm){
	var acs = aForm.NumAdult.value + "-" + aForm.NumChild.value + "-" + aForm.NumSenior.value;
	return acs;
}
function isDateBeforeDate(dateOut,dateReturn) {
	var dateOutInt = getDateInt(dateOut.value);
	var dateReturnInt = getDateInt(dateReturn.value);
	
	var dateOutDate = new Date(dateOutInt);
	var dateReturnDate = new Date(dateReturnInt);
	
	if (dateOutInt > dateReturnInt) {
		return false;
	}
	return true;	
}
function isDateInPast(dDate) {
	var dDateInt = getDateInt(dDate.value);
	var now = new Date().getTime();
	
	if (now > dDateInt) {
		return true;
	}
	return false;
}

function tickAirClass() {
	if (document.flight.AirClass.checked) {
		show('airClassDiv');
	}
	else {
		document.flight.Class.value = 3;
		document.flight.Airln.value = "";
		hide('airClassDiv');
	}
}

//Travel Booking Engine
function tbeSetState(bookingOption) {

	if ( bookingOption=="o1") tbeShowState1(); //flights
	if ( bookingOption=="o2") tbeShowState2(); //flightAndHotel
	if ( bookingOption=="o3") tbeShowState3(); //Hotel
	if ( bookingOption=="o4") tbeShowState4(); //flightAndHotelAndCar
	if ( bookingOption=="o5") tbeShowState5(); //Car
	if ( bookingOption=="o6") tbeShowState6(); //flightAndCar
	
}
//flights
function tbeShowState1() {

	hide('hotelDIV');
	hide('packageDIV');
	hide('carDIV');
	show('flightDIV');
	hide('numRoomsTR');
	hide('numCarsTR');
	show('numPeopleTR');
	show('numInfantsTR');
	
	omTravelType="Flight";
}
//flightAndHotel
function tbeShowState2() {

	hide('hotelDIV');
	show('packageDIV');
	document.package.PackageType.value=2;
	show('roomDIV');
	hide('carDIV');
	hide('flightDIV');
	show('numRoomsTR');
	hide('numCarsTR');
	show('numPeopleTR');
	hide('numInfantsTR');
	show("more_options");
//	document.getElementById("tbeBdr15").style.borderTop = "1px solid #439539";	
	
	omTravelType="Flight+Hotel";
}
//Hotel
function tbeShowState3() {

	show('hotelDIV');
	hide('packageDIV');
	hide('carDIV');
	hide('flightDIV');
	show('numRoomsTR');
	hide('numCarsTR');
	hide('numPeopleTR');
	hide('numInfantsTR');
	
	omTravelType="Hotel";
}
//flightAndHotelAndCar
function tbeShowState4() {

	hide('hotelDIV');
	show('packageDIV');
	document.package.PackageType.value=1;
	show('roomDIV');
	hide('carDIV');
	hide('flightDIV');
	show('numRoomsTR');
	show('numCarsTR');
	show('numPeopleTR');
	hide('numInfantsTR');
	show("more_options");
//	document.getElementById("tbeBdr15").style.borderTop = "1px solid #439539";

	omTravelType="Flight+Hotel+Car";
}
//Car
function tbeShowState5() {

	hide('hotelDIV');
	hide('packageDIV');
	show('carDIV');
	hide('flightDIV');
	hide('numRoomsTR');
	show('numCarsTR');
	hide('numPeopleTR');
	hide('numInfantsTR');
	
	omTravelType="Car";
}
//flightAndCar
function tbeShowState6() {

	hide('hotelDIV');
	show('packageDIV');
	document.package.PackageType.value=5;
	hide('roomDIV');
	setColWidth('tdAdlt','70px');
	setColWidth('tdChld','75px');
	hide('carDIV');
	hide('flightDIV');
	hide('numRoomsTR');
	show('numCarsTR');
	show('numPeopleTR');
	hide('numInfantsTR');
	hide("more_options");
//	document.getElementById("tbeBdr15").style.borderTop = "1px solid #dae790";
	
	omTravelType="Flight+Car";
}

function setColWidth(id, width) {

	document.getElementById(id).style.width = width;

}

function divShow(destinationName,destinationCount) {
// This function hi-lites the selected destination on the left hand side of sub-holiday pages
// and, on the right hand side, shows the selected div and hides all the others

// Right Hand Side  
// Build a list of divisions contained in div colRight and hide them all 
	var mySection = document.getElementById("colRight"); 
	var myDivs = mySection.getElementsByTagName("div");
	var numDivs = myDivs.length;
	var divName = destinationName+"_Block";

	var currentDiv = document.getElementById(divName);
	
// Hide all div on RHS	
	for (var x = 0; x < numDivs; x++) {
		myDivs[x].style.display = 'none';
	}
// Display the selected div 
	currentDiv.style.display = 'block';

// Display all the child divs of the selected div 
	myDivs = currentDiv.getElementsByTagName("div");
	numDivs = myDivs.length;
		
	for (var x = 0; x < numDivs; x++) {
		myDivs[x].style.display = 'block';
	}


// Left Hand Side 
// Because much of the code is to do with styles, it is split into an IE6 section and other browsers section

	var ieVersion = 0;
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null) {
      ieVersion = parseFloat( RegExp.$1 );
    }
  }
	if (ieVersion == 6) {
// IE6 version
// Reset all the LHS divs
		var priceName = "";
		var priceDiv = "";
		for (var x = 1; x < 11; x++) {
		 	divName = "Destination_Tag_"+x;
		 	currentDiv = document.getElementById(divName);
		 	priceName = "Price_"+x;
		 	priceDiv = document.getElementById(priceName);
	
	
		 	
	 		if (currentDiv != null) {
		 	
			 	currentDiv.style.borderRight = "solid 4px #d4b740";
			 	currentDiv.style.borderLeft = "solid 2px #d4b740";
			 	currentDiv.style.backgroundColor = "#e8e3b8";
			 	priceDiv.style.display = "block";
			 	
			 	if (x == 1) {
			 		currentDiv.style.height ="39px";
			 		currentDiv.style.borderTop = "solid 2px #d4b740";
			 		currentDiv.style.borderBottom = "solid 1px #ffffff";
			 	} 
			 	else if (x == 10) {
			 		currentDiv.style.height ="39px";
			 		currentDiv.style.borderTop = "none";
			 		currentDiv.style.borderBottom = "solid 2px #d4b740";
			 	}
			 	else {
			 		currentDiv.style.height ="39px";
			 		currentDiv.style.borderTop = "none";
			 		currentDiv.style.borderBottom = "solid 1px #ffffff";
			 	}
			}
		}
	// Hightlight the selection on the left hand menu		
		var divSelName = "Destination_Tag_"+destinationCount;
		var selectionDiv = document.getElementById(divSelName);
		selectionDiv.style.backgroundColor = "#ffffff";
		selectionDiv.style.height = "39px"
		selectionDiv.style.borderTop = "solid #deb93a 4px";
		selectionDiv.style.borderLeft = "solid #deb93a 4px";
		selectionDiv.style.borderBottom = "solid #deb93a 4px";
	selectionDiv.style.borderRight = "none";
	}
// All other browsers
	else {
		// Reset all the LHS divs
		var priceName = "";
		var priceDiv = "";
		for (var x = 1; x < 11; x++) {
		 	divName = "Destination_Tag_"+x;
		 	currentDiv = document.getElementById(divName);
		 	priceName = "Price_"+x;
		 	priceDiv = document.getElementById(priceName);
	
	
		 	
	 		if (currentDiv != null) {
		 	
			 	currentDiv.style.borderRight = "solid 4px #d4b740";
			 	currentDiv.style.borderLeft = "solid 2px #d4b740";
			 	currentDiv.style.backgroundColor = "#e8e3b8";
			 	priceDiv.style.display = "block";
			 	
			 	if (x == 1) {
			 		currentDiv.style.height ="36px";
			 		currentDiv.style.borderTop = "solid 2px #d4b740";
			 		currentDiv.style.borderBottom = "solid 1px #ffffff";
			 	} 
			 	else if (x == 10) {
			 		currentDiv.style.height ="37px";
			 		currentDiv.style.borderTop = "none";
			 		currentDiv.style.borderBottom = "solid 2px #d4b740";
			 	}
			 	else {
			 		currentDiv.style.height ="38px";
			 		currentDiv.style.borderTop = "none";
			 		currentDiv.style.borderBottom = "solid 1px #ffffff";
			 	}
			}
		}
	// Hightlight the selection on the left hand menu		
		var divSelName = "Destination_Tag_"+destinationCount;
		var selectionDiv = document.getElementById(divSelName);
		selectionDiv.style.backgroundColor = "#ffffff";
		selectionDiv.style.height = "31px"
		selectionDiv.style.borderTop = "solid #deb93a 4px";
		selectionDiv.style.borderLeft = "solid #deb93a 4px";
		selectionDiv.style.borderBottom = "solid #deb93a 4px";
		selectionDiv.style.borderRight = "none";
	}


// Hide the Price of the selected ID
	priceName = "Price_"+destinationCount;
	priceDiv = document.getElementById(priceName);
	priceDiv.style.display = "none";

// remove white bottom border above the current selection
	var aboveCount = destinationCount - 1;
	var divAboveName  = "Destination_Tag_"+aboveCount;
	if (aboveCount > 0) {
		aboveDiv = document.getElementById(divAboveName);
		aboveDiv.style.borderBottom ="1px solid #e8e3b8";	
	}

}
function clearOtherCity(){
	document.getElementById("act_locn_name").value="";
}
function clearLocnRadios(){
	if(document.getElementById("act_locn_name").value!=""){
		clearRadios();
	}
}
function validateActivity(){
	var msg = "";
	var locn = document.getElementById("act_locn_name").value;
	if(!isRadiosChecked() && (locn == " (e.g. London or LGW)" || locn == "" || isOnlySpaces(locn))){
		msg+= "Please select or enter a location to search for.";
	}
	var start = document.getElementById("act_start").value;
	var end = document.getElementById("act_end").value;
	if(start == " dd/mm/yy" || start == "" || isOnlySpaces(start) || end == " dd/mm/yy" || end == "" || isOnlySpaces(end)){
		if(msg!=""){
			msg+="\n\n";
		}
		msg+= "Please enter the dates for your search";
	}
	if(msg!=""){
		alert(msg);
		return false;
	}
	openTravelProvider();
	return true;
}
function clearRadios(){
	var theRadios = document.activity.LocationID;
	for(i=0;i<theRadios.length;i++){
		theRadios[i].checked = false;
	}
}		
function isRadiosChecked(){
	var theRadios = document.activity.LocationID;
	for(i=0;i<theRadios.length;i++){
		if(theRadios[i].checked == true){
			return true;
		}
	}
	return false;
}
function isOnlySpaces(inputString){
	for (var i=0;i<inputString.length;i++){
		if (inputString.charAt(i) != " ") return false;
	}
	return true;
}

// Alternate Travel clickNDisplay
function clickNDisplay(destinationName,destinationCount) {
// This function hi-lites the selected destination on the left hand side of sub-holiday pages
// and, on the right hand side, shows the selected div and hides all the others

// LHS Hand Side  
// Build a list of divisions contained in div colRight and hide them all 
	var mySection = document.getElementById("leftCol"); 
	var myDivs = mySection.getElementsByTagName("div");
	var numDivs = myDivs.length;
	var divName = destinationName+"_Block";

	var currentDiv = document.getElementById(divName);

// Hide all div on RHS	
	for (var x = 0; x < numDivs; x++) {
		myDivs[x].style.display = 'none';
	}
// Display the selected div 
	currentDiv.style.display = 'block';

// Display all the child divs of the selected div 
	myDivs = currentDiv.getElementsByTagName("div");
	numDivs = myDivs.length;
		
	for (var x = 0; x < numDivs; x++) {
		myDivs[x].style.display = 'block';
	}


// Left Hand Side 

		var priceName = "";
		var priceDiv = "";
		for (var x = 1; x < 11; x++) {
		 	divName = "Dest_Tag_"+x;
		 	spanDestName = "destSpan_"+x;
		 	spanPriceName = "priceSpan_"+x;

		 	currentDiv = document.getElementById(divName);
		 	currentSpanDestName = document.getElementById(spanDestName);
		 	currentSpanPriceName = document.getElementById(spanPriceName);
		 	
	 		if (currentDiv != null) {
			 	currentDiv.style.backgroundImage = "url(/wcsstore/Greenbee/images/greenbee/travel_click_tab_light.gif)";
			 	currentSpanDestName.style.color = "#464646";
			 	currentSpanPriceName.style.color = "#cc3300";
			}
		}
	// Hightlight the selection on the left hand menu		
		var divSelName = "Dest_Tag_"+destinationCount;
		var selectionDiv = document.getElementById(divSelName);
 		selectionDiv.style.backgroundImage = "url(/wcsstore/Greenbee/images/greenbee/travel_click_tab_dark.gif)";

		var destSpan = "destSpan_"+destinationCount;
		var selectedDestSpan = document.getElementById(destSpan);
		selectedDestSpan.style.color = "#fff";
		
		var priceSpan = "priceSpan_"+destinationCount;
		var selectedPriceSpan = document.getElementById(priceSpan);
		selectedPriceSpan.style.color = "#fff";
		
	}





