//*************************************************************************
//*** tn_searchpanel_main_mtlite.js ***************************************
//*************************************************************************

// GLOBAL VARIABLES DEFINED IN ROUTES DATA FEED
// var Startbit (int, bit value of the first month with routes)
// var endbit (int, bit value of the last month with routes)
// var startMonth (int 0-11, todays month)
// var startYear (int 0-11, todays year)
// var FirstMonth (int 0-11, month that Startbit occurs in)
// var FirstYear (int, year that Startbit occurs in )
// var departures (array, departure codes that have routes)
// var destinations (array, destination codes that have routes)
// var validMonths (non-empty 2D array, bit mask values of form validMonths[desintation][departure])

// GLOBAL LOOKUP VARIABLES
var destinationLookup = new Array();
var departureLookup = new Array();
var combinedLookup = new Array();
var destinationLookup1 = new Array();
var departureLookup1 = new Array();
var combinedLookup1 = new Array();
var errorLookup = new Array();

// GLOBAL SORT VARIABLES
var alphaDestinationArray = new Array();
var valueDestinationArray = new Array();
var alphaDepartureArray	 = new Array();
var valueDepartureArray	= new Array();
	var alphaDepArray = new Array();
	var valueDepArray = new Array();
	var alphaDestArray = new Array();
	var valueDestArray = new Array();	
	var ukairport;

// GLOBAL DATE VARIABLES
var todaysDate;
var calendarDate = new Date();
var calendarDateRet = new Date();
var maxDate = new Date();
var daysofweekshort = new Array("Su","Mo","Tu","We","Th","Fr","Sa");
var daysofweeklong = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var monthsofyear = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var validOnDate = true;
var fromCalendar = false;
var calendarVisible = false;
var pauseCalendar = true;
var pauseCalendar2 = true;
var pauseCalendar3 = true;

//GLOBAL VALIDATION VARIABLES
var paxerror = false;

//GLOBAL ROUTES VARIABLES TO BE USED
var validMonthsUsed;
var endBitUsed;
var FirstMonthUsed;
var FirstYearUsed;
var departuresUsed;
var destinationsUsed;

//**********Startup functions, called once**********
function start() {
loadlookups();
loadlookups1();
if(typeof departuresi != "undefined" && typeof departureso != "undefined")
{
	sortRoutes1();
}

sortRoutes2();

changeroutesfeed();
initializeDate();
}

function changeroutesfeed()
{
//alert("gets to changeroutesfeed");

	if(eval(document.search.Way[1].checked))
	{
		validMonthsUsed = validMonths;
		//alert(validMonthsUsed);
		//alert(validMonthsUsed[3][4]);
		
		endBitUsed 	 = endBit;
		FirstMonthUsed	 = FirstMonth;
		FirstYearUsed	 = FirstYear;
		departuresUsed   = departures;
		destinationsUsed = destinations;
		
		destinationsUsed = destinationsUsed.slice(1);
		validMonthsUsed = validMonthsUsed.slice(1);	
		//departuresUsed = departuresUsed.slice(1);
		maxDate.setDate(1);
		maxDate.setMonth(startMonth + endBitUsed + 1);
		maxDate.setTime(addDays(maxDate.getTime(), -1));
		

		document.getElementById("OutDepAir").options.length = 0;
		document.getElementById("OutArrAir").options.length = 0;

		document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option("<- Please choose ->", "0");
		document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option("<- Please choose ->", "0");

		populateDropDowns2();		
	}
	else
	{
		//needalert
		//alert("one way routes");
		document.getElementById("OutDepAir").options.length = 0;
		document.getElementById("OutArrAir").options.length = 0;

		document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option("<- Please choose ->", "0");
		document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option("<- Please choose ->", "0");

		populateDropDowns1();		

				
	}
	
	

}

function loadlookups()
{
	var destinationAirportArray = document.getElementById("destinationAirport").value.replace(regExp, "").split("|");
	var departureAirportArray = document.getElementById("departureAirport").value.replace(regExp, "").split("|");
	
	var destlength = destinationAirportArray.length;

	for (var i=0;i<destinationAirportArray.length;i++)
	{
		destinationLookup[i] = destinationAirportArray[i].split(",");
	}


	for (var i=0;i<departureAirportArray.length;i++)
	{
		departureLookup[i] = departureAirportArray[i].split(",");
	}
	
	for (var i=0;i<destinationAirportArray.length;i++)
	{
		combinedLookup[i] = destinationAirportArray[i].split(",");
	}
	
	for (var i=0;i<departureAirportArray.length;i++)
	{
		combinedLookup[destlength + i] = departureAirportArray[i].split(",");
	}
	
	//alert(combinedLookup)
			
}

function loadlookups1()
{
	var destinationAirportArray1 = document.getElementById("destinationAirport1").value.replace(regExp, "").split("|");
	var departureAirportArray1 = document.getElementById("departureAirport1").value.replace(regExp, "").split("|");
	
	var destlength = destinationAirportArray1.length;

	for (var i=0;i<destinationAirportArray1.length;i++)
	{
		destinationLookup1[i] = destinationAirportArray1[i].split(",");
	}


	for (var i=0;i<departureAirportArray1.length;i++)
	{
		departureLookup1[i] = departureAirportArray1[i].split(",");
	}
	
	
	for (var i=0;i<destinationAirportArray1.length;i++)
	{
		combinedLookup1[i] = destinationAirportArray1[i].split(",");
	}
	
	for (var i=0;i<departureAirportArray1.length;i++)
	{
		combinedLookup1[destlength + i] = departureAirportArray1[i].split(",");
	}
	
	//alert(combinedLookup1)
			
}

function sortRoutes1()
{
	var description;
	//var loadToDropdown;
	//var nullArray = new Array();
	//alphaDepartureArray = nullArray;
	//alphaDestinationArray = nullArray;
	//valueDepartureArray = nullArray;
	//valueDestinationArray = nullArray;

	departuresUsed = departuresi;
	
	for(i=0;i<departuresUsed.length;i++)
	{
		description = lookupcode(departureLookup1,departuresUsed[i]);
		if (description != departuresUsed[i]) { alphaDepartureArray.push(description); }
	}
	
	
	departuresUsed = departureso;
	
	for(i=0;i<departuresUsed.length;i++)
	{
		description = lookupcode(departureLookup1,departuresUsed[i]);
		if (description != departuresUsed[i]) { alphaDepartureArray.push(description); }
	}

	alphaDepartureArray.sort();
	
	for(i=0;i<alphaDepartureArray.length;i++)
	{
		valueDepartureArray.push(lookupvalue(combinedLookup1,alphaDepartureArray[i]));
	}	


	destinationsUsed = destinationsi; 
		
	for(i=0;i<destinationsUsed.length;i++)
	{
		description = lookupcode(destinationLookup1,destinationsUsed[i]);
		if (description != destinationsUsed[i]) { alphaDestinationArray.push(description); }
	}
	
	destinationsUsed = destinationso; 
	
	for(i=0;i<destinationsUsed.length;i++)
	{
		description = lookupcode(destinationLookup1,destinationsUsed[i]);
		if (description != destinationsUsed[i]) { alphaDestinationArray.push(description); }
	}	

	alphaDestinationArray.sort();

	for(i=0;i<alphaDestinationArray.length;i++)
	{
		valueDestinationArray.push(lookupvalue(combinedLookup1,alphaDestinationArray[i]));
	}
	
	//alert(alphaDestinationArray + valueDestinationArray);

}

function populateDropDowns1()
{
//alert("IN POPULATE DROPDOWNS alphaDepDestArray = " + alphaDepDestArray);
	for(i=0;i<alphaDestinationArray.length;i++)
	{
		document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option(alphaDestinationArray[i],valueDestinationArray[i]);
	}

	for(i=0;i<alphaDepartureArray.length;i++)
	{
		document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option(alphaDepartureArray[i],valueDepartureArray[i]);		
	}

}

function sortRoutes2()
{

	var description;

	for(i=0;i<departures.length;i++)
	{
		description = lookupcode(departureLookup,departures[i]);
		if (description != departures[i]) { alphaDepArray.push(description); }
	}
	
	alphaDepArray.sort();

	for(i=0;i<alphaDepArray.length;i++)
	{
		valueDepArray.push(lookupvalue(departureLookup,alphaDepArray[i]));
	}	


	for(i=0;i<destinations.length;i++)
	{
		description = lookupcode(destinationLookup,destinations[i]);
		if (description != destinations[i]) { alphaDestArray.push(description); }
	}
	
	alphaDestArray.sort();

	for(i=0;i<alphaDestArray.length;i++)
	{
		valueDestArray.push(lookupvalue(destinationLookup,alphaDestArray[i]));
	}
		
}

function populateDropDowns2()
{

	for(i=0;i<alphaDepArray.length;i++)
	{
		document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option(alphaDepArray[i],valueDepArray[i]);
	}
	
	for(i=0;i<alphaDestArray.length;i++)
	{
		document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option(alphaDestArray[i],valueDestArray[i]);
	}	

}

function initializeDate()
{
	if (dateLayout)
	{
		//alert(dateLayout);
		
		switch(dateLayout)
		{
			case "DAYSAHEAD":	cmsDateFormat = 1; break;
			case "SPECIFICDATE": 	cmsDateFormat = 2; break;
			case "FIRSTROUTE":	cmsDateFormat = 3; break;
			default: cmsDateFormat = 1; break;
		}
	}
	else
	{
		cmsDateFormat = 1;
	}

	// days ahead unless before current date
	if (cmsDateFormat == 1)
	{
	
		if ((daysAhead.toString != "NaN") && (daysAhead.toString().length >= 1))
		{			
			calendarDate.setTime(addDays(todaysDate.getTime(), Number(daysAhead)));
		}
		else
		{
			calendarDate.setTime(todaysDate.getTime());
		}
		
		if (calendarDate.getTime() < todaysDate.getTime())
		{
			calendarDate.setTime(todaysDate.getTime());			
		}		
	}
	else
	{
		// specific date unless before current date
		if (cmsDateFormat == 2)
		{
		//alert("specificDate = " + specificDate);
		//alert("specificDate length = " + specificDate.length);
			if (specificDate.length == 11)
			{	
				var calendarStartArray = specificDate.split("-");
			
				calendarStartArray[0] = Number(calendarStartArray[0]);
				calendarStartArray[3] = Number(calendarStartArray[3]);
				
				switch (calendarStartArray[1])
				{
					case "Jan": calendarStartArray[1] = 0; break;
					case "Feb": calendarStartArray[1] = 1; break;
					case "Mar": calendarStartArray[1] = 2; break;
					case "Apr": calendarStartArray[1] = 3; break;
					case "May": calendarStartArray[1] = 4; break;
					case "Jun": calendarStartArray[1] = 5; break;
					case "Jul": calendarStartArray[1] = 6; break;
					case "Aug": calendarStartArray[1] = 7; break;
					case "Sep": calendarStartArray[1] = 8; break;
					case "Oct": calendarStartArray[1] = 9; break;
					case "Nov": calendarStartArray[1] = 10; break;
					case "Dec": calendarStartArray[1] = 11; break;
				}	
			}

			if (specificDate.length == 10)
			{
				calendarStartArray = specificDate.split("/");

				calendarStartArray[0] = Number(calendarStartArray[0]);
				calendarStartArray[1] = Number(calendarStartArray[1]) - 1;
				calendarStartArray[2] = Number(calendarStartArray[2]);
				//alert(calendarStartArray);
				
			}
			
			if ((specificDate.length != 10) && (specificDate.length != 11))
			{
				calendarStartArray[0] = today.getDate();
				calendarStartArray[1] = today.getMonth();
				calendarStartArray[2] = today.getFullYear();
			}
			
			calendarDate.setFullYear(Number(calendarStartArray[2]));
			calendarDate.setDate(Number(calendarStartArray[0]));
			calendarDate.setMonth(Number(calendarStartArray[1]));
			
			if (calendarDate.getTime() < todaysDate.getTime())
			{
				calendarDate.setTime(todaysDate.getTime());			
			}
		}
		else 
		{
			// first route unless before current date
			if (cmsDateFormat == 3)
			{
				calendarDate.setFullYear(FirstYearUsed);
				calendarDate.setDate(1);
				calendarDate.setMonth(FirstMonthUsed);
				
				if (calendarDate.getTime() < todaysDate.getTime())
				{
					calendarDate.setTime(addDays(todaysDate.getTime(),1));			
				}				
			}
			else
			{
				// default to today				
				calendarDate.setTime(todaysDate.getTime());
			}
		}
	}
	//alert("writing date " + calendarDate);
	writeDate(calendarDate.getDate(), calendarDate.getMonth(), calendarDate.getFullYear());
}

//**********End of startup function**********


//**********Array lookup functions**********

function lookupcode(array,value)
{
	for(var i=0;i<array.length;i++)	if(array[i][1]==value) return array[i][0];
	return value;
}

function lookupvalue(array,code)
{
	for(var i=0;i<array.length;i++) if(array[i][0]==code) return array[i][1];
	return code;
}

function lookupcodeNoArray(value)
{
	for(var i=0;i<destinationLookup.length;i++)
	{
		if(destinationLookup[i][1]==value)
		{
		var value = destinationLookup[i][0];
		
		break;
		}
	}
	return value;
}

function lookupvalueposition(array,code)
{
	for(var i=0;i<array.length;i++) 
	{
		if (array[i] == code)
		{
			position = i; 
			return position;
		}
	}
}

function isInLookup(array, code)
{
	for(var i=0;i<array.length;i++)
	{
		if (array[i][1] == code)
		{
			return true;
		}
	}

	return false;
}

//**********End of array lookup functions**********



//**********Change event functions**********

function changeDestination()
{

	if(eval(document.search.Way[1].checked))
	{	
		var value = document.search.OutDepAir.value;
		var selectedDestination = document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value;
		//alert(document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
		if (value == 0)
		{
			document.getElementById("OutArrAir").options.length = 0;
			document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option("<- Please choose ->", "0");
			populateDropDowns2();
		}
		else
		{
			var filtereddestinationArray = new Array();
			var filtereddepartureArray = new Array();

			var j = 0;

			document.getElementById("OutArrAir").options.length = 0;
			document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option("<- Please choose ->", "0");

			var departureposition = lookupvalueposition(departuresUsed, value);
			var destinationposition = lookupvalueposition(destinationsUsed, value);

			//alert("destination = " + selectedDestination);
			//alert("departure = " + value);
			//alert("lookup = " + destinationLookup);
			//alert(destinationposition);

				for(i=0;i<departures.length;i++)
				{
					if(value == departures[i])
					{
						ukairport = 1;					
						//needalert
						//alert(value + " is a uk airport ukairport = " + ukairport);
						break;
					}
				}

				for(i=0;i<destinations.length;i++)
				{
					if(value == destinations[i])
					{
						ukairport = 0;					
						//needalert
						//alert(value + " NOT uk airport ukairport = " + ukairport);
						break;
					}			
				}

			if(ukairport == 1)
			{
				for(i=0;i<destinationsUsed.length;i++)
				{		
					var bit = validMonthsUsed[i][departureposition];

					if ((bit != 0) && (isInLookup(destinationLookup, destinationsUsed[i])))
					{		
						filtereddestinationArray[j] = destinationsUsed[i];
						j++;
					}
				}
				//needalert
				//alert(filtereddestinationArray);			
				lookupandPopulateDest(filtereddestinationArray);

			}
			else
			{
				for(i=0;i<departuresUsed.length;i++)
				{		
					var bit = validMonthsUsed[destinationposition][i];
					//alert("bit = " + validMonthsUsed[destinationposition][i] + " destinationposition = " + destinationposition + " i = " + i)

					if ((bit != 0) && (isInLookup(combinedLookup, departuresUsed[i])))
					{		
						filtereddepartureArray[j] = departuresUsed[i];
						j++;
					}
				}
				//needalert
				//alert(filtereddepartureArray);
				lookupandPopulateDest(filtereddepartureArray);

			}

		}

		setSelectedDropDown(selectedDestination, "OutArrAir");
	}
	else
	{
		var value = document.search.OutDepAir.value;
		var selectedDestination = document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value;
		//alert(document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
		if (value == 0)
		{
			document.getElementById("OutArrAir").options.length = 0;
			document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option("<- Please choose ->", "0");
			populateDropDowns1();
		}
		else
		{
			var filtereddestinationArray = new Array();
			var filtereddepartureArray = new Array();

			var j = 0;
			var iORo;

			document.getElementById("OutArrAir").options.length = 0;
			document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option("<- Please choose ->", "0");

			departuresUsed = departureso;

			for(i=0;i<departuresUsed.length;i++)
			{		
				if(value == departuresUsed[i])
				{
					iORo = "o";
					break;
				}
			}

			//alert("iORo = " + iORo)


			departuresUsed = departuresi;

			for(i=0;i<departuresUsed.length;i++)
			{		
				if(value == departuresUsed[i])
				{
					iORo = "i";
					break;
				}
			}

			//alert("iORo = " + iORo)

			if(iORo == "o")
			{
				validMonthsUsed = validMonthso;
				//alert("valid months outbound = " + validMonthsUsed);

				endBitUsed 	 = endBito;
				FirstMonthUsed	 = FirstMontho;
				FirstYearUsed	 = FirstYearo;
				departuresUsed   = departureso;
				destinationsUsed = destinationso;

				destinationsUsed = destinationsUsed.slice(1);
				validMonthsUsed = validMonthsUsed.slice(1);	

				maxDate.setDate(1);
				maxDate.setMonth(startMonth + endBitUsed + 1);
				maxDate.setTime(addDays(maxDate.getTime(), -1));		
			}
			else
			{
				validMonthsUsed = validMonthsi;
				//alert("valid months inbound = " + validMonthsUsed);

				endBitUsed 	 = endBiti;
				FirstMonthUsed	 = FirstMonthi;
				FirstYearUsed	 = FirstYeari;
				departuresUsed   = departuresi;
				destinationsUsed = destinationsi;

				destinationsUsed = destinationsUsed.slice(1);
				validMonthsUsed = validMonthsUsed.slice(1);	

				maxDate.setDate(1);
				maxDate.setMonth(startMonth + endBitUsed + 1);
				maxDate.setTime(addDays(maxDate.getTime(), -1));		
			}

			var departureposition = lookupvalueposition(departuresUsed, value);
			var destinationposition = lookupvalueposition(destinationsUsed, value);
			//needalert
			//alert("departureposition = " + departureposition)


				for(i=0;i<destinationsUsed.length;i++)
				{		
					var bit = validMonthsUsed[i][departureposition];
					//alert("bit = " + bit + " for "+ destinationsUsed[i])

					if ((bit != 0) && (isInLookup(combinedLookup1, destinationsUsed[i])))
					{		
						filtereddestinationArray[j] = destinationsUsed[i];
						j++;
					}
				}
				//needalert
				//alert(filtereddestinationArray);			
				lookupandPopulateDest1(filtereddestinationArray);
				ukairport = 1;

	

		}

		setSelectedDropDown(selectedDestination, "OutArrAir");

	}

}

function lookupandPopulateDest(array)
{
var alphaDestinationArrayfiltered = new Array();
var valueDestinationArrayfiltered = new Array();

	var description;
	
	for(i=0;i<array.length;i++)
	{
		description = lookupcode(combinedLookup,array[i]);
		if (description != destinationsUsed[i]) { alphaDestinationArrayfiltered.push(description); }
	}

	alphaDestinationArrayfiltered.sort();

	for(i=0;i<array.length;i++)
	{
		valueDestinationArrayfiltered.push(lookupvalue(combinedLookup, alphaDestinationArrayfiltered[i]));
	}
	
	for(i=0;i<array.length;i++)
	{
		document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option(alphaDestinationArrayfiltered[i],valueDestinationArrayfiltered[i]);		
	}
}

function lookupandPopulateDest1(array)
{
var alphaDestinationArrayfiltered = new Array();
var valueDestinationArrayfiltered = new Array();

	var description;
	
	for(i=0;i<array.length;i++)
	{
		description = lookupcode(combinedLookup1,array[i]);
		if (description != destinationsUsed[i]) { alphaDestinationArrayfiltered.push(description); }
	}

	alphaDestinationArrayfiltered.sort();

	for(i=0;i<array.length;i++)
	{
		valueDestinationArrayfiltered.push(lookupvalue(combinedLookup1, alphaDestinationArrayfiltered[i]));
	}
	
	for(i=0;i<array.length;i++)
	{
		document.getElementById("OutArrAir").options[document.getElementById("OutArrAir").options.length] = new Option(alphaDestinationArrayfiltered[i],valueDestinationArrayfiltered[i]);		
	}
}


function lookupandPopulateDep1(array)
{
var alphaDepartureArrayfiltered = new Array();
var valueDepartureArrayfiltered = new Array();

	var description;
	
	for(i=0;i<array.length;i++)
	{
		description = lookupcode(combinedLookup1,array[i]);
		if (description != destinationsUsed[i]) { alphaDepartureArrayfiltered.push(description); }
	}

	alphaDepartureArrayfiltered.sort();

	for(i=0;i<array.length;i++)
	{
		valueDepartureArrayfiltered.push(lookupvalue(combinedLookup1, alphaDepartureArrayfiltered[i]));
	}
	
	for(i=0;i<array.length;i++)
	{
		document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option(alphaDepartureArrayfiltered[i],valueDepartureArrayfiltered[i]);		
	}
}


function changeDeparture()
{

	if(eval(document.search.Way[1].checked))
	{	
		var value = document.search.OutArrAir.value;
		var selectedDestination = document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value;
		//alert(document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
		if (value == 0)
		{
			document.getElementById("OutDepAir").options.length = 0;
			document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option("<- Please choose ->", "0");
			populateDropDowns2();
		}
		else
		{
			var filtereddestinationArray = new Array();
			var filtereddepartureArray = new Array();

			var j = 0;

			document.getElementById("OutDepAir").options.length = 0;
			document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option("<- Please choose ->", "0");

			var departureposition = lookupvalueposition(departuresUsed, value);
			var destinationposition = lookupvalueposition(destinationsUsed, value);

			//alert("destination = " + selectedDestination);
			//alert("departure = " + value);
			//alert("lookup = " + destinationLookup);
			//alert(destinationposition);

				for(i=0;i<departures.length;i++)
				{
					if(value == departures[i])
					{
						ukairport = 1;					
						//needalert
						//alert(value + " is a uk airport ukairport = " + ukairport);
						break;
					}
				}

				for(i=0;i<destinations.length;i++)
				{
					if(value == destinations[i])
					{
						ukairport = 0;					
						//needalert
						//alert(value + " NOT uk airport ukairport = " + ukairport);
						break;
					}			
				}

			if(ukairport == 1)
			{
				for(i=0;i<destinationsUsed.length;i++)
				{		
					var bit = validMonthsUsed[i][departureposition];

					if ((bit != 0) && (isInLookup(destinationLookup, destinationsUsed[i])))
					{		
						filtereddestinationArray[j] = destinationsUsed[i];
						j++;
					}
				}
				//needalert
				//alert(filtereddestinationArray);			
				lookupandPopulateDep(filtereddestinationArray);

			}
			else
			{
				for(i=0;i<departuresUsed.length;i++)
				{		
					var bit = validMonthsUsed[destinationposition][i];
					//alert("bit = " + validMonthsUsed[destinationposition][i] + " destinationposition = " + destinationposition + " i = " + i)

					if ((bit != 0) && (isInLookup(combinedLookup, departuresUsed[i])))
					{		
						filtereddepartureArray[j] = departuresUsed[i];
						j++;
					}
				}
				//needalert
				//alert(filtereddepartureArray);
				lookupandPopulateDep(filtereddepartureArray);

			}

		}

		setSelectedDropDown(selectedDestination, "OutDepAir");
	}
	else
	{
		var value = document.search.OutArrAir.value;
		var selectedDestination = document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value;
		//alert(document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
		if (value == 0)
		{
			document.getElementById("OutDepAir").options.length = 0;
			document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option("<- Please choose ->", "0");
			populateDropDowns1();
		}
		else
		{
			var filtereddestinationArray = new Array();
			var filtereddepartureArray = new Array();

			var j = 0;
			var iORo;

			document.getElementById("OutDepAir").options.length = 0;
			document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option("<- Please choose ->", "0");

			departuresUsed = departureso;

			for(i=0;i<departuresUsed.length;i++)
			{		
				if(value == departuresUsed[i])
				{
					iORo = "o";
					break;
				}
			}

			//alert("iORo = " + iORo)


			departuresUsed = departuresi;

			for(i=0;i<departuresUsed.length;i++)
			{		
				if(value == departuresUsed[i])
				{
					iORo = "i";
					break;
				}
			}

			//alert("iORo = " + iORo)

			if(iORo == "o")
			{
				validMonthsUsed = validMonthso;
				//alert("valid months outbound = " + validMonthsUsed);

				endBitUsed 	 = endBito;
				FirstMonthUsed	 = FirstMontho;
				FirstYearUsed	 = FirstYearo;
				departuresUsed   = departureso;
				destinationsUsed = destinationso;

				destinationsUsed = destinationsUsed.slice(1);
				validMonthsUsed = validMonthsUsed.slice(1);	

				maxDate.setDate(1);
				maxDate.setMonth(startMonth + endBitUsed + 1);
				maxDate.setTime(addDays(maxDate.getTime(), -1));		
			}
			else
			{
				validMonthsUsed = validMonthsi;
				//alert("valid months inbound = " + validMonthsUsed);

				endBitUsed 	 = endBiti;
				FirstMonthUsed	 = FirstMonthi;
				FirstYearUsed	 = FirstYeari;
				departuresUsed   = departuresi;
				destinationsUsed = destinationsi;

				destinationsUsed = destinationsUsed.slice(1);
				validMonthsUsed = validMonthsUsed.slice(1);	

				maxDate.setDate(1);
				maxDate.setMonth(startMonth + endBitUsed + 1);
				maxDate.setTime(addDays(maxDate.getTime(), -1));		
			}

			var departureposition = lookupvalueposition(departuresUsed, value);
			var destinationposition = lookupvalueposition(destinationsUsed, value);
			//needalert
			//alert("departureposition = " + departureposition)
			//alert(validMonthsUsed)


				for(i=0;i<destinationsUsed.length;i++)
				{		
					var bit = validMonthsUsed[i][departureposition];
					//alert("bit = " + bit + " for "+ destinationsUsed[i])

					if ((bit != 0) && (isInLookup(combinedLookup1, destinationsUsed[i])))
					{		
						filtereddepartureArray[j] = destinationsUsed[i];
						j++;
					}
				}
				//needalert
				//alert(filtereddepartureArray);			
				lookupandPopulateDep1(filtereddepartureArray);
				ukairport = 1;

	

		}

		setSelectedDropDown(selectedDestination, "OutDepAir");

	}

}

function setSelectedDropDown(code, dropDown)
{
	for (var i = 0; i < document.getElementById(dropDown).length; i++)
	{
		if (document.getElementById(dropDown)[i].value == code)
		{
			document.getElementById(dropDown).selectedIndex = i;
			break;
		}
	}
}

function lookupandPopulateDep(array)
{
var alphaDestinationArrayfiltered = new Array();
var valueDestinationArrayfiltered = new Array();

	var description;
	
	for(i=0;i<array.length;i++)
	{
		description = lookupcode(departureLookup,array[i]);
		if (description != destinationsUsed[i]) { alphaDestinationArrayfiltered.push(description); }
	}

	alphaDestinationArrayfiltered.sort();

	for(i=0;i<array.length;i++)
	{
		valueDestinationArrayfiltered.push(lookupvalue(departureLookup, alphaDestinationArrayfiltered[i]));
	}
	
	for(i=0;i<array.length;i++)
	{
		document.getElementById("OutDepAir").options[document.getElementById("OutDepAir").options.length] = new Option(alphaDestinationArrayfiltered[i],valueDestinationArrayfiltered[i]);		
	}
}

function clearErrorMessage(elementID, elementClass)
{
	eval('document.getElementById("' + elementID + '").className = "' + elementClass + '"');
	eval('document.getElementById("' + elementID + 'Error").className = "u-errorhide"');
}

//**********End of change event functions**********



//**********Date functions**********

function addDays(milliseconds, days)
{
	return milliseconds + (days * 24 * 60 * 60 * 1000);
}

function writeDate(day, month, year)
{
	// Fill date field with calendar date	
	//alert("setting date to" + day + "-" + month + "-" + year);
	var dayString;
	var monthString;
	calendarDate.setFullYear(year);
	calendarDate.setMonth(month);
	calendarDate.setDate(day);
	

	// Pad out single digits
	document.search.outDepDateText.value = padDate(calendarDate);
	document.search.outRetDateText.value = padDate(calendarDate);
}

function padDate(dateObject)
{
	if (dateObject.getDate() < 10)
	{
		dayString = "0" + dateObject.getDate();
	}
	else 
	{
		dayString = "" + dateObject.getDate();
	}

	if (dateObject.getMonth() + 1 < 10) 
	{
		monthString = "0" + (dateObject.getMonth() + 1);
	}
	else
	{
		monthString = "" + (dateObject.getMonth() + 1);
	}	
	
	var dateString = dayString + "/" + monthString + "/" + dateObject.getFullYear();	
	return dateString;
}	
	
function readDate(dateString)
{
	var returnDate = new Date();
	var regexp = /-|\//;
	var dateStringArray = dateString.split(regexp);
	var day = Number(dateStringArray[0]);
	var month = Number(dateStringArray[1]);
	var year = Number(dateStringArray[2]);
	// if date input is not a digit or of zero length, return false
	if ((month.toString() == "NaN") || (month.toString().length == 0) || (year.toString() == "NaN") || (year.toString().length == 0) || (day.toString() == "NaN") || (day.toString().length == 0)) { return 0; }
	
	// if month is outside valid range, return false
	if ((month <= 0) || (month > 12)) { return 0; }
	
	// if day is outside valid range, return false
	if ((day <= 0) || (day >= 32)) { return 0; }
	
	// if year is not 2 or 4 digits long, return false
	if ((dateStringArray[2].length != 2) && (dateStringArray[2].length != 4)) { return 0; }

	// if date is 2 digits long and equals 20, return false
	if ((dateStringArray[2].length == 2) && (Number(dateStringArray[2]) == 20)) { return 0; }

	// if year is less than current year, return false
	if (((dateStringArray[2].length == 4) && (year < yearNow)) || ((dateStringArray[2].length == 2) && ((year + 2000) < yearNow))) { return 1; }
	
	// if year is 2 digits long, add 2000
	if (dateStringArray[2].length == 2) { year = year + 2000; }
	
	returnDate.setFullYear(year);
	returnDate.setDate(day);
	returnDate.setMonth(month - 1);
	
	// if date is less than current date or greater than max date, return false
	if (returnDate.getTime() < todaysDate.getTime()) { return 1; }
	if (returnDate.getFullYear() > maxDate.getFullYear()) { return 2; }
	if ((returnDate.getFullYear() == maxDate.getFullYear()) && (returnDate.getMonth() > maxDate.getMonth())) { return 2; }
	

	// check that number of days is valid for month
	switch (month) {
		case 1: if (day > 31) { return 0; }
		case 2: if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
			{
				if (day > 29) {	return 0; } 
				else
				{
					if (day > 28) {	return 0; }
				}
			} break;
		case 3: if (day > 31) { return 0; } break;
		case 4: if (day > 30) { return 0; } break;
		case 5: if (day > 31) { return 0; } break;
		case 6: if (day > 30) { return 0; } break;
		case 7: if (day > 31) { return 0; } break;
		case 8: if (day > 31) { return 0; } break;
		case 9: if (day > 30) { return 0; } break;
		case 10: if (day > 31) { return 0; } break
		case 11: if (day > 30) { return 0; } break;
		case 12: if (day > 31) { return 0; } break;
	}
	return returnDate;
}

function verifyDate(dateString)
{
	// if route selected and valid date, set calendar date
	if (isRouteSelected()) 
	{
		var show = readDate(dateString)
		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
			}
			else
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);					
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
			else
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}
		}
		
		if (typeof(show) == "object")
		{	
			//alert(show)
			writeDate(show.getDate(),show.getMonth(),show.getFullYear());					
			clearErrorMessage("outDepDateText", "srchdate");
			isRouteValid(show);
			//alert(calendarDate)
			
		}
		else
		{
			switch (show)
			{
			 case 0: 	document.getElementById("outDepDateTextError").innerHTML = dateinvalid;
					document.getElementById("outDepDateTextError").className = "u-error";
					document.getElementById("outDepDateText").className = "srchdate u-errorcol";			 
			 		break;
			 case 1: 	document.getElementById("outDepDateTextError").innerHTML = datesmall;
			 		moveToFirstRouteNoMessage(selectedDeparture, selectedDestination);
			 		break;
			 case 2: 	document.getElementById("outDepDateTextError").innerHTML = datebig;
			 		moveToFirstRouteNoMessage(selectedDeparture, selectedDestination);
			 		break;
			
			
			}
				
		}
		
	}
	// else incomplete route, display error message
	else
	{
		notCompleteRoute();
	}
}

function isRouteSelected()
{
	//alert(document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
	// if dep or dest no set to please choose, return true
	if ((Number(document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value) == 0) || (Number(document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value) == 0)) { return false; }
	return true;
}

function isRouteValid(show)
{
	//Check bit mask to see if current route is valid
	
	var day = calendarDate.getDate();
	var month = calendarDate.getMonth();
	var year = calendarDate.getFullYear();
	
	var monthYears = (year - startYear) * 12;
	var currentBit = month + startBit - startMonth + monthYears;

		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
			}
			else
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);					
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
			else
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}
		}

	//alert(selectedDestination + " " + selectedDeparture + " " + ukairport)

		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{	
				if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
				{
					calendarDate = show;	
				}
				else
				{
					moveToFirstRoute(selectedDestination, selectedDeparture);
				}
			}
			else
			{
				if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
				{
					calendarDate = show;	
				}
				else
				{
					moveToFirstRoute(selectedDeparture, selectedDestination);
				}			
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
				{
					calendarDate = show;	
				}
				else
				{
					moveToFirstRoute(selectedDeparture, selectedDestination);
				}
			}
			else
			{
				if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
				{
					calendarDate = show;	
				}
				else
				{
					moveToFirstRoute(selectedDestination, selectedDeparture);
				}			
			}
		}
}

function moveToFirstRoute(selectedDeparture, selectedDestination)
{
	var monthYearArray = findFirstRoute(selectedDeparture, selectedDestination);
	
	writeDate(1, monthYearArray[0], monthYearArray[1]);

	document.getElementById("outDepDateTextError").innerHTML = datemoved;
	document.getElementById("outDepDateTextError").className = "u-change";		
	//alert("date moved to first available for this route");
	document.getElementById("outDepDateText").focus();		
}

function moveToFirstRouteNoMessage(selectedDeparture, selectedDestination)
{
	var monthYearArray = findFirstRoute(selectedDeparture, selectedDestination);
	
	writeDate(1, monthYearArray[0], monthYearArray[1]);	
	document.getElementById("outDepDateTextError").className = "u-change";
	document.getElementById("outDepDateText").focus();		
}

function moveToFirstRouteCalendar(selectedDeparture, selectedDestination)
{
	var monthYearArray = findFirstRoute(selectedDeparture, selectedDestination);
	
	if ((calendarDate.getMonth() + (calendarDate.getFullYear() * 12)) < (monthYearArray[0] + (monthYearArray[1] * 12)))
	{
		writeDate(1, monthYearArray[0], monthYearArray[1]);

		document.getElementById("outDepDateTextError").innerHTML = datemoved;
		document.getElementById("outDepDateTextError").className = "u-change";		
		//alert("date moved to first available for this route");
		document.getElementById("outDepDateText").focus();	
		return monthYearArray;
	}

	return new Array(calendarDate.getMonth(), calendarDate.getFullYear());
}

function findFirstRoute(selectedDeparture, selectedDestination)
{
	var monthYearArray = new Array(calendarDate.getMonth(), calendarDate.getFullYear());

if(ukairport == 1)
{
	if(layout == "LAYOUT1")
	{
		for (var i = startBit; i <= endBitUsed; i++)
		{
			var month = (i + startMonth) % 12;
			var year = Math.floor((i + startMonth) / 12) + startYear;

			if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,i)))
			{
				monthYearArray[0] = month;
				monthYearArray[1] = year;

				if ((monthYearArray[0] != month) || (monthYearArray[1] != year))
				{

					validOnDate = false;
				}
				else
				{
					validOnDate = true;
				}
				return monthYearArray;
			}
		}
	}
	else
	{
		for (var i = startBit; i <= endBitUsed; i++)
		{
			var month = (i + startMonth) % 12;
			var year = Math.floor((i + startMonth) / 12) + startYear;
			//alert("validMonthsUsed[" + selectedDeparture + "]" + "[" +selectedDestination + "]");
			if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,i)))
			{
				monthYearArray[0] = month;
				monthYearArray[1] = year;

				if ((monthYearArray[0] != month) || (monthYearArray[1] != year))
				{

					validOnDate = false;
				}
				else
				{
					validOnDate = true;
				}
				//alert(monthYearArray);
				return monthYearArray;
			}
		}	
	}
}
else
{
	if(layout == "LAYOUT1")
	{	
		for (var i = startBit; i <= endBitUsed; i++)
		{
			var month = (i + startMonth) % 12;
			var year = Math.floor((i + startMonth) / 12) + startYear;
			//alert("validMonthsUsed[" + selectedDeparture + "]" + "[" +selectedDestination + "]");
			if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,i)))
			{
				monthYearArray[0] = month;
				monthYearArray[1] = year;

				if ((monthYearArray[0] != month) || (monthYearArray[1] != year))
				{

					validOnDate = false;
				}
				else
				{
					validOnDate = true;
				}
				//alert(monthYearArray);
				return monthYearArray;
			}
		}
	}
	else
	{
		for (var i = startBit; i <= endBitUsed; i++)
		{
			var month = (i + startMonth) % 12;
			var year = Math.floor((i + startMonth) / 12) + startYear;

			if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,i)))
			{
				monthYearArray[0] = month;
				monthYearArray[1] = year;

				if ((monthYearArray[0] != month) || (monthYearArray[1] != year))
				{

					validOnDate = false;
				}
				else
				{
					validOnDate = true;
				}
				return monthYearArray;
			}
		}	
	}
}	
	validOnDate = false;
	return monthYearArray;
}

function findLastRoute(selectedDeparture, selectedDestination, endBitUsed)
{
	var bitMask = validMonthsUsed[selectedDestination][selectedDeparture];

if(ukairport == 1)
{
	if(layout == "LAYOUT1")
	{
		for (var i = endBitUsed; i >= startBit; i--)
		{
			if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,i)))
			{
				//alert("last bit = " + i);
				return i;
			}	
		}
	}
	else
	{
		for (var i = endBitUsed; i >= startBit; i--)
		{
			if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,i)))
			{
				//alert("last bit = " + i);
				return i;
			}	
		}	
	}
}
else
{
	if(layout == "LAYOUT1")
	{
		for (var i = endBitUsed; i >= startBit; i--)
		{
			if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,i)))
			{
				//alert("last bit = " + i);
				return i;
			}	
		}
	}
	else
	{
		for (var i = endBitUsed; i >= startBit; i--)
		{
			if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,i)))
			{
				//alert("last bit = " + i);
				return i;
			}	
		}	
	}
}
	return endBitUsed;
}

function notCompleteRoute() {

	var OutArrAirValue = Number(document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
	var OutDepAirValue = Number(document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);

	if ((OutArrAirValue == 0) && (OutDepAirValue != 0))
	{
		// dep set, dest not set
		document.getElementById("OutArrAir").className = "srchdest u-errorcol";
		document.getElementById("OutArrAirError").innerHTML = destbeforedate;
		document.getElementById("OutArrAirError").className = "u-error";		
		document.getElementById("OutArrAir").focus();		
		
	}
	if ((OutArrAirValue != 0) && (OutDepAirValue == 0))
	{
		// dep not set, dest set
		document.getElementById("OutDepAir").className = "srchdest u-errorcol";
		document.getElementById("OutDepAirError").innerHTML = depbeforedate;	
		document.getElementById("OutDepAirError").className = "u-error";		
		document.getElementById("OutDepAir").focus();		
		
	}	
	
	if ((layout == "LAYOUT1") && (OutArrAirValue == 0) && (OutDepAirValue == 0))
	{
		// both not set, layout 1
		document.getElementById("OutDepAir").className = "srchdest u-errorcol";
		document.getElementById("OutDepAirError").innerHTML = depbeforedate;
		document.getElementById("OutDepAirError").className = "u-error";		
		document.getElementById("OutArrAir").className = "srchdest u-errorcol";
		document.getElementById("OutArrAirError").innerHTML = destbeforedate;
		document.getElementById("OutArrAirError").className = "u-error";		
		document.getElementById("OutDepAir").focus();		
	}
	else 
	{
		if ((layout == "LAYOUT2") && (OutArrAirValue == 0) && (OutDepAirValue == 0))
		{
		// both not set, layout 2
		document.getElementById("OutArrAir").className = "srchdest u-errorcol";
		document.getElementById("OutArrAirError").innerHTML = destbeforedate;
		document.getElementById("OutArrAirError").className = "u-error";
		document.getElementById("OutDepAir").className = "srchdest u-errorcol";
		document.getElementById("OutDepAirError").innerHTML = depbeforedate;
		document.getElementById("OutDepAirError").className = "u-error";		
		document.getElementById("OutArrAir").focus();
		}
	}
}

function changeCalendarDropDown(monthyear)
{
	var monthyeararray = monthyear.split(",");
	showCalendar(monthyeararray[0], monthyeararray[1]);
}

function updateCalendar()
{
	if((document.getElementById("OutDepAir").value != "0") && (document.getElementById("OutArrAir").value != "0"))
	{
		if(calendarVisible)
		{
			//alert("showing calendar for " + calendarDate.getMonth() + "/" + calendarDate.getFullYear());
			fromCalendar = false;
			showCalendar(calendarDate.getMonth(), calendarDate.getFullYear());
		}
	}
}


// BEGIN FLOATING LAYER CODE - JS
isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

function ddInit(e)
{
	topDog=isIE?"BODY":"HTML"; //set top-most element
	whichDog=isIE?document.all.theLayer:document.getElementById("theLayer"); //set layer getting method
	hotDog=isIE?event.srcElement:e.target; // set event getting method
	calendarTable=hotDog;


	// while event is not on calendar and not on top-element
	if(hotDog.id)
	{
		while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog)
		{
			// get parent event
			hotDog=isIE?hotDog.parentElement:hotDog.parentNode;
		}
	}
	
	// if parent is calendar
	if(hotDog.id=="titleBar")
	{
		// set offsets
		offsetx=isIE?event.clientX:e.clientX;
		offsety=isIE?event.clientY:e.clientY;
		nowX=parseInt(whichDog.style.left);
		nowY=parseInt(whichDog.style.top);
		ddEnabled=true;
		
		// move calendar
		document.onmousemove=dd;
	}
	else
	{
		if(calendarVisible)
		{
			var i = 0;
			while ((calendarTable.tagName!="HTML" && calendarTable.tagName!="BODY")&&calendarTable.id!="theLayer")
			{			
				// get parent event
				//alert(calendarTable.id);
				//alert(calendarTable.tagName);

				if(isIE)
				{
					if (calendarTable.parentElement)
					{
						calendarTable = calendarTable.parentElement;
					}
				}
				else
				{
					if (calendarTable.parentNode)
					{
						calendarTable = calendarTable.parentNode;
					}
				}
			}
			
			// if clicked anywhere else, hide calendar
			//alert(calendarTable.id);
			if (calendarTable.id!="theLayer")
			{
				hideMe();
			}
		}	
	}
}

function dd(e)
{
	if (pauseCalendar)
	{
		if (pauseCalendar2)
		{
			if (pauseCalendar3)
			{
				if(!ddEnabled) return;
				whichDog.style.left=(isIE?nowX+event.clientX-offsetx:nowX+e.clientX-offsetx)+"px";	
				whichDog.style.top=(isIE?nowY+event.clientY-offsety:nowY+e.clientY-offsety)+"px";	
				
				if(isIE)
				{
					document.getElementById("calendarShim").style.left = whichDog.style.left;
					document.getElementById("calendarShim").style.top = whichDog.style.top;
				}
				pauseCalendar = false;
				pauseCalendar2 = false;
				pauseCalendar3 = false;
			
			}
			else
			{
				pauseCalendar3 = true;
			}
		}
		else
		{
			pauseCalendar2 = true;
		}
	}
	else
	{
		pauseCalendar = true;
	}
	return false;
}

function ddN4(whatDog)
{
	if(!isN4) return;
	N4=eval(whatDog);
	N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
	N4.onmousedown=function(e) { N4.captureEvents(Event.MOUSEMOVE); N4x=e.x; N4y=e.y; }
	N4.onmousemove=function(e) { if(isHot) { N4.moveBy(e.x-N4x,e.y-N4y); return false; } }
	N4.onmouseup=function() { N4.releaseEvents(Event.MOUSEMOVE); }
}

function hideMe()
{
	if(isIE||isNN) whichDog.style.visibility="hidden";
	else if(isN4) document.theLayer.visibility="hide";
	calendarVisible = false;
	if(isIE)
	{
		document.getElementById("calendarShim").style.display = "none";
	}
}

function showMe()
{
	if (isIE)
	{
		document.getElementById("calendarShim").style.width = whichDog.style.width;
		document.getElementById("calendarShim").style.height = whichDog.style.height;
		document.getElementById("calendarShim").style.top = whichDog.style.top;
		document.getElementById("calendarShim").style.left = whichDog.style.left;	
		document.getElementById("calendarShim").style.zIndex = whichDog.style.zIndex - 1;
		document.getElementById("calendarShim").style.display = "block";
	}
	
	if (isIE||isNN) whichDog.style.visibility="visible";
	else if (isN4) document.theLayer.visibility="show";
	calendarVisible = true;
}

document.onmousedown=ddInit;
document.onmouseup=Function("ddEnabled=false");
// END FLOATING LAYER CODE - JS

//**********End of date functions**********

//***********validate dropdowns on submit*********

		
		
function checkSubmit()
{	
	var valid = true;
	
	if (isRouteSelected()) 
	{
		var show = readDate(document.getElementById("outDepDateText").value);
		if (typeof(show) == "object")
		{	
			var monthYears = (show.getFullYear() - startYear) * 12;
			var currentBit = show.getMonth() + startBit - startMonth + monthYears;
			var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
			var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
		
			//if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
			//{		
			//}
			//else
			//{
			//	document.getElementById("outDepDateTextError").innerHTML = dateunavailable;
			//	document.getElementById("outDepDateTextError").className = "u-error";
			//	document.getElementById("outDepDateText").className = "u-errorcol";	
			//	valid = false;
			//}
		}
		else
		{
			switch (show)
			{
			 case 0: document.getElementById("outDepDateTextError").innerHTML = dateinvalid; break;
			 case 1: document.getElementById("outDepDateTextError").innerHTML = datesmall; break;
			 case 2: document.getElementById("outDepDateTextError").innerHTML = datebig; break;
			
			
			}
			document.getElementById("outDepDateTextError").className = "u-error";
			document.getElementById("outDepDateText").className = "u-errorcol";	
			valid = false;
		}		
	}
	// else incomplete route, display error message
	else
	{
		if (document.search.OutDepAir.value.length == 1)
		{
			document.getElementById("OutDepAirError").innerHTML = deperror;
			document.getElementById("OutDepAirError").className = "u-error";
			document.getElementById("OutDepAir").className = "srchdest u-errorcol";
			valid = false;
		}

		if (document.search.OutArrAir.value.length == 1)
		{
			document.getElementById("OutArrAirError").innerHTML = desterror;
			document.getElementById("OutArrAirError").className = "u-error";
			document.getElementById("OutArrAir").className = "srchdest u-errorcol";
			valid = false;
		}
	}

	if(eval(document.search.Way[1].checked))
	{
		if((calendarDateRet < calendarDate) ||  (document.getElementById("outDepDateText").value == document.getElementById("outRetDateText").value))
		{
			//alert("gets here");
			//alert("calendarDateRet = " + calendarDateRet + " calendarDate " + calendarDate );
			document.search.outRetDateText.value = padDate(calendarDate);
			document.getElementById("outRetDateTextError").innerHTML = dateinvalidret;
			document.getElementById("outRetDateTextError").style.display = ""; 		
			document.getElementById("outRetDateTextError").className = "u-error";
			document.getElementById("outRetDateText").className = "srchdate u-errorcol";		
			valid = false;		
		}
	}

		
  //if(Number(document.search.duration.value)<=0)
  // {
  //	 document.getElementById("DurationError").className = "u-error";
 //	 document.getElementById("duration").className = "srchduration u-errorcol";
  // valid=false;
  // }
  //else
  // {
//		document.getElementById("DurationError").className = "u-errorhide";
//		document.getElementById("duration").className = "srchduration";
  // }
  
  
   
	adpax = Number(document.search.AdultPax.value);
	chpax = Number(document.search.ChildPax.value);
	inpax = Number(document.search.InfantPax.value);
	totalpax = adpax + chpax + inpax;
	
	if (totalpax > maxPax)
	{
		paxerror = true;
		document.getElementById("PaxError").innerHTML = paxtotal;
		document.getElementById("PaxError").className = "u-error";
				
		document.getElementById("ChildPax").className = "u-errorcol";
		document.getElementById("InfantPax").className = "u-errorcol";
		document.getElementById("AdultPax").className = "u-errorcol";
		valid = false;
	
	}
	else
	{
		document.getElementById("PaxError").className = "u-errorhide";
		document.getElementById("ChildPax").className = "";
		document.getElementById("InfantPax").className = "";
		document.getElementById("AdultPax").className = "";
	}
	
	return valid;

}
//***********end of validate dropdowns on submit*********

//***********validate onchange*********

function checkPax()
{
	if (paxerror)	
	{
		adpax = Number(document.search.AdultPax.value);
		chpax = Number(document.search.ChildPax.value);
		inpax = Number(document.search.InfantPax.value);
		totalpax = adpax + chpax + inpax;

		if (totalpax > maxPax)
		{
			document.getElementById("PaxError").innerHTML = paxtotal;
			document.getElementById("PaxError").className = "u-error";		

			document.getElementById("ChildPax").className = " u-errorcol";	
			document.getElementById("InfantPax").className = "u-errorcol";				
			document.getElementById("AdultPax").className = "u-errorcol";

		}
		else
		{
			document.getElementById("PaxError").className = "u-errorhide";
			document.getElementById("ChildPax").className = "";
			document.getElementById("InfantPax").className = "";
			document.getElementById("AdultPax").className = "";
		}

		return false;		
	}
}

//function checkDuration()
//{
//if(Number(document.search.duration.value)<=0)
//	{
//	document.getElementById("DurationError").className = "u-error";
//	document.getElementById("duration").className = "srchduration u-errorcol";
//	valid=false;
//	}
//else
//	{
//	document.getElementById("DurationError").className = "u-errorhide";
//	document.getElementById("duration").className = "srchduration";
//	}
//}



//***********end of validate onchange*********



//***********DEBUG*********
function prepareSubmit()
{
	//Complete departure and return dates, airport codes
	
	if(document.search.OutArrAir[document.search.OutArrAir.selectedIndex].value=='ALL')
	{
		document.search.OutArrAir[document.search.OutArrAir.selectedIndex].value = '';
	}
	
	document.search.RetDepAir.value = document.search.OutArrAir.value;	
	document.search.RetArrAir.value = document.search.OutDepAir.value;
	document.getElementById("OutDepDate").value = padDate(calendarDate);
	document.getElementById("RetDepDate").value = padDate(calendarDateRet);	
	document.getElementById("duration").value = days_between(document.getElementById("outDepDateText").value,document.getElementById("outRetDateText").value);
	document.getElementById("Company").value = "JB";
	
	if(eval(document.search.Way[0].checked))
	{
	document.search.RetDepAir.value = "";
	document.search.RetArrAir.value = "";
	document.getElementById("RetDepDate").value = "";
	document.getElementById("duration").value = 0;
	document.getElementById("Company").value = "JU";	
	}	

	//displaySearchParameters();
	pleasewait();
	setCookie();
}

function displaySearchParameters()
{
	var searchparams = "SEARCH PARAMETERS\n\n";

		searchparams += "Form action = " + document.search.action + "\n";

	for (var i=0; i < document.search.length; i++)
	{
		searchparams += document.search[i].name + " = " + document.search[i].value + "\n";
	}

	alert(searchparams);	
}

//***********end of DEBUG*********


//***********Cookie functions*********

function setCookie()
{
	// set date and master
	var cookieId = "holiday";
	var masterValue;
	var masterIndex;

	if (layout == "LAYOUT1")
	{
		masterValue = document.search.OutDepAir[document.getElementById("OutDepAir").selectedIndex].value;
		masterIndex = document.search.OutDepAir.selectedIndex;
		slaveValue = document.search.OutArrAir[document.getElementById("OutArrAir").selectedIndex].value;
		slaveIndex = document.search.OutArrAir.selectedIndex;		
	}
	else
	{
		masterValue = document.search.OutArrAir[document.getElementById("OutArrAir").selectedIndex].value;
		masterIndex = document.search.OutArrAir.selectedIndex;
		slaveValue = document.search.OutDepAir[document.getElementById("OutDepAir").selectedIndex].value;
		slaveIndex = document.search.OutDepAir.selectedIndex;		
	}
	
 	if (eval(document.search.Way[0].checked)) 
	{
 		var wayIndex = 0;
 	}
	else
	{
		var wayIndex = 1;	
	}	

	var cookieData = cookieId + ',';
	cookieData += masterValue + ',';
	cookieData += masterIndex + ','
	cookieData += slaveValue + ',';
	cookieData += slaveIndex + ','
	cookieData += document.getElementById("OutDepDate").value + ',';
	cookieData += document.getElementById("RetDepDate").value + ',';
	cookieData += document.getElementById("AdultPax").value + ',';
	cookieData += document.getElementById("ChildPax").value + ',';
	cookieData += document.getElementById("InfantPax").value + ',';
	cookieData += wayIndex;

	var expiryDate = new Date();
	expiryDate.setDate(todaysDate.getDate()+1825);	

	document.cookie = (cookieName + "=") + cookieData + ";expires=" + expiryDate.toGMTString();	

	//alert('Set cookie: ' + cookieData);
}

function getCookie(cookieName)
{
	resetsearch();
	// get master
	var cookieData = '';
	var curcookie = document.cookie;
	if( curcookie.length > 0 )
	{
		foundAt = curcookie.indexOf( cookieName + "=" );

		if( foundAt == -1 || cookieName == "" )
		{
			//alert(curcookie);
			return false;    // the cookie was not set
		}
		else
		{
			foundAt += cookieName.length + 1;
			endAt    = curcookie.indexOf(";", foundAt);

			if( endAt == -1 )
			{
				endAt = curcookie.length;
			}

			var cookieData = unescape(curcookie.substring(foundAt, endAt));
			//alert('Get cookie: ' + cookieData);

			var cookieArray = cookieData.split(',');
			var cookieId = cookieArray[0];
			var setMasterValue = cookieArray[1];
			var setMasterIndex = cookieArray[2];
			var setSlaveValue = cookieArray[3];
			var setSlaveIndex = cookieArray[4];
			var setDateValue = cookieArray[5];
			var setDateRetValue = cookieArray[6];
			var setAdultPaxIndex = cookieArray[7];
			var setChildPaxIndex = cookieArray[8];
			var setInfantPaxIndex = cookieArray[9];			
			var setWayIndex = cookieArray[10];			
			
			//document.getElementById("Way").selectedIndex = setWayIndex;
			eval("document.search.Way[" + setWayIndex + "].checked = true");
			
			if(setWayIndex == 0)
			{
				changeroutesfeed();
				hideRetCal();
			}
			else
			{
				changeroutesfeed();
				showRetCal();
			}
			
			if (layout == "LAYOUT1")
			{
				if (setMasterValue.length == 0)
				{
					document.getElementById("OutDepAir").value = "ALL";
					changeDestination();
				}
				else
				{				
					document.getElementById("OutDepAir").value = setMasterValue;
					document.getElementById("OutDepAir").selectedIndex = setMasterIndex;
					
					changeDestination();
					
					document.getElementById("OutArrAir").value = setSlaveValue;
					document.getElementById("OutArrAir").selectedIndex = setSlaveIndex;							
				}		
			}
			else
			{
				if (setMasterValue.length == 0)
				{
					document.getElementById("OutArrAir").value = "ALL";
					changeDeparture();
				}
				else	
				{
					document.getElementById("OutArrAir").value = setMasterValue;
					document.getElementById("OutArrAir").selectedIndex = setMasterIndex;
				
					changeDeparture();
				
					document.getElementById("OutDepAir").value = setSlaveValue;
					document.getElementById("OutDepAir").selectedIndex = setSlaveIndex;					
				}			
			}
			
			//document.getElementById("duration").selectedIndex = setDurationIndex;
			document.getElementById("AdultPax").value = setAdultPaxIndex;
			document.getElementById("ChildPax").value = setChildPaxIndex;
			document.getElementById("InfantPax").value = setInfantPaxIndex;
	
			//alert('Get cookie: ' + cookieData);
			
			var cookiedate = new Date();
			
			cookiedate.setFullYear(Number(setDateValue.substring(6,10)));
			cookiedate.setDate(Number(setDateValue.substring(0,2)));
			cookiedate.setMonth(Number(setDateValue.substring(3,5)) - 1);
			cookiedate.setHours(0);
			cookiedate.setMinutes(0);

			if (todaysDate > cookiedate)
			{				
				initializeDate();
								
				if (isRouteSelected()) 
				{				
					verifyDate(document.getElementById("outDepDateText").value);
				}
				
				setDateRetValue = document.getElementById("outRetDateText").value;
				
				if(setWayIndex == 1)
				{
					var setDateRetValuesplit = setDateRetValue.split('/');
					calendarDateRet.setFullYear(Number(setDateRetValuesplit[2]));
					calendarDateRet.setDate(Number(setDateRetValuesplit[0]));
					calendarDateRet.setMonth(Number(setDateRetValuesplit[1]-1));
				}				
			}
			else
			{
				dateLayout = "SPECIFICDATE";
				specificDate = setDateValue;
				initializeDate();
								
				if (isRouteSelected()) 
				{				
					verifyDate(document.getElementById("outDepDateText").value);
				}
				
				document.getElementById("outRetDateText").value = setDateRetValue;

				if(setWayIndex == 1)
				{
					var setDateRetValuesplit = setDateRetValue.split('/');
					calendarDateRet.setFullYear(Number(setDateRetValuesplit[2]));
					calendarDateRet.setDate(Number(setDateRetValuesplit[0]));
					calendarDateRet.setMonth(Number(setDateRetValuesplit[1]-1));
				}				
			}			
			
		}
	}

}


function checkJump()
{
	var jump = document.getElementById("srchtype").value;
	
	if (jump.indexOf("http") == -1)
	{
		return true;
	}
	else
	{
		window.open(jump);
		return false;
	}
	
}

function jump(jumpValue)
{
	if (jumpValue.indexOf("http") == -1)
	{
		window.top.location = ("AniteNextPage.asp?p=" + jumpValue);
	}
	else
	{
		window.open(jumpValue);

	}
}


function writeDate2(day, month, year)
{
	// Fill date field with calendar date	
	//alert("setting date to" + day + "-" + month + "-" + year);
	var dayString;
	var monthString;
	//Number(day);
	//Number(month);
	//Number(year);
	calendarDateRet.setFullYear(year);
	calendarDateRet.setMonth(month);
	calendarDateRet.setDate(day);	

	// Pad out single digits
	if(calendarDateRet < calendarDate)
	{
		document.search.outRetDateText.value = padDate(calendarDateRet);
		document.getElementById("outRetDateTextError").innerHTML = dateinvalidret;
		document.getElementById("outRetDateTextError").style.display = ""; 		
		document.getElementById("outRetDateTextError").className = "u-error";
		document.getElementById("outRetDateText").className = "srchdate u-errorcol";		
		
	}
	else
	{
		document.search.outRetDateText.value = padDate(calendarDateRet);
		document.getElementById("outRetDateTextError").style.display = "none"; 
		document.getElementById("outRetDateText").className = "srchdate";
	}
	
}


function hideRetCal()
{
document.getElementById("retcal").style.display = "none";
}

function showRetCal()
{
document.getElementById("retcal").style.display = "";
document.search.outRetDateText.value = padDate(calendarDate);	
}

function days_between(date1, date2) {

var date1split = date1.split('/');
var date2split = date2.split('/');

var calendarDatetmp1 = new Date();
var calendarDateRettmp1 = new Date();

	calendarDatetmp1.setFullYear(Number(date1split[2]));
	calendarDatetmp1.setDate(Number(date1split[0]));
	calendarDatetmp1.setMonth(Number(date1split[1])-1);

	calendarDateRettmp1.setFullYear(Number(date2split[2]));
	calendarDateRettmp1.setDate(Number(date2split[0]));
	calendarDateRettmp1.setMonth(Number(date2split[1])-1);

//alert("calendarDatetmp1 = " + calendarDatetmp1 + "calendarDateRettmp1 = " + calendarDateRettmp1 )

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24;

    // Convert both dates to milliseconds
    var date1_ms = calendarDatetmp1.getTime();
    var date2_ms = calendarDateRettmp1.getTime();

    // Calculate the difference in milliseconds;
    var difference_ms = Math.abs(date1_ms - date2_ms);
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY);

}







function showCalendar(month, year)
{
//alert("gets to showcal3 - ukairport = " + ukairport);
	// if route selected, show calendar
	if (isRouteSelected())
	{
		// clear existing calendar layer info
		document.getElementById("calendarContent").innerHTML = "";
		// display the calendar using a temporary date, split up important html tags
		var validRoute;
		
		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
			}
			else
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{		
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
			else
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}
		}
		
		//alert("selectedDeparture = " + selectedDeparture +" selectedDestination = "+ selectedDestination);

		var firstMonthWithRoute = findFirstRoute(selectedDeparture, selectedDestination);
		var monthYearArray = new Array();
		
		if (fromCalendar)
		{	
			monthYearArray[0] = month;
			monthYearArray[1] = year;
		}
		else
		{
			monthYearArray = moveToFirstRouteCalendar(selectedDeparture, selectedDestination);
		}
		
			fromCalendar = true;

		var calendartempdate = new Date(monthYearArray[1], monthYearArray[0], 1);
		var month = calendartempdate.getMonth();
		var year = calendartempdate.getFullYear();
		var monthYears = (year - startYear) * 12;
		var currentBit = month + startBit - startMonth + monthYears;
		var firstRouteBit = firstMonthWithRoute[0] + startBit - startMonth + ((firstMonthWithRoute[1] - startYear) * 12);
		var lastRouteBit = findLastRoute(selectedDeparture, selectedDestination, endBitUsed);	
		//alert("ROute Bits = " + firstRouteBit + " " + lastRouteBit)
		

if(ukairport == 1)
{
		//needalert
		//alert("route bit = " + validMonthsUsed[selectedDestination][selectedDeparture] + ", current bit = " + Math.pow(2,currentBit));

	if(layout == "LAYOUT1")
	{
		if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}
	}
	else
	{
		if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}	
	}
}
else
{
		//needalert
		//alert("route bit = " + validMonthsUsed[selectedDeparture][selectedDestination] + ", current bit = " + Math.pow(2,currentBit));

	if(layout == "LAYOUT1")
	{
		if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}
	}
	else
	{
		if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}	
	}
}
		
		var calendarbuffer = new String("<table id=\"calendarTable\" cellpadding=\"0\" cellspacing=\"0\"> \n" +
		"<tr> \n");	

		if ((firstMonthWithRoute[1] == year) && (firstMonthWithRoute[0] == month))
		{
			calendarbuffer +=
			"<td id=\"calendarMonth\">" + 
			"&nbsp;"
			//alert("firstMonthWithRoute[1] == year line 2067")
		}
		else
		{
			//alert("ELSE firstMonthWithRoute[1] == year line 2067")
			var calendarmonthback = new Date(calendartempdate.getTime());
			calendarmonthback.setMonth(calendarmonthback.getMonth() - 1);			
			calendarbuffer +=
			"<td id=\"calendarMonth\" style=\"cursor:pointer;\" onClick=\"javascript:window.showCalendar("+ calendarmonthback.getMonth() + "," + calendarmonthback.getFullYear() + ");\">" +
			"<a href=\"javascript:window.showCalendar("+ calendarmonthback.getMonth() + "," + calendarmonthback.getFullYear() + ");\"><<</a>"
		}

		calendarbuffer +=
		"</td> \n";

		var calendarmonthyear = new Date();
		calendarmonthyear.setFullYear(firstMonthWithRoute[1]);
		calendarmonthyear.setDate(1);
		calendarmonthyear.setMonth(firstMonthWithRoute[0])

		calendarbuffer +=
		"<td colspan=\"5\"> \n" +
		"<select id=\"calendarMonthYear\" onChange=\"javascript:window.changeCalendarDropDown(this.value);\">";
			
		//alert("start bit = " + startBit);
		//alert("first route bit = " + firstRouteBit);
		//alert("current bit = " + currentBit);
		//alert("last route bit = " + lastRouteBit);
			
		for (var i = firstRouteBit; i <= lastRouteBit; i ++)
		{
			calendarbuffer += "<option value=\"" + calendarmonthyear.getMonth() + "," + calendarmonthyear.getFullYear() + "\"";
			
			if ((month == calendarmonthyear.getMonth()) && (year == calendarmonthyear.getFullYear()))
			{			
				calendarbuffer +=
				" selected=\"selected\"";			
			}
			
			calendarbuffer +=
			">" + monthsofyear[calendarmonthyear.getMonth()] + " " + calendarmonthyear.getFullYear() + "</option>\n";		
		
			calendarmonthyear.setMonth(calendarmonthyear.getMonth() + 1);		
		}
		
		calendarbuffer +=
		"</select>" +
		"</td> \n";

		if (currentBit >= lastRouteBit)
		{
			calendarbuffer +=
			"<td id=\"calendarMonth\">" + 
			"&nbsp;"
		}
		else
		{
			var calendarmonthforward = new Date(calendartempdate.getTime());
			calendarmonthforward.setMonth(calendarmonthforward.getMonth() + 1);
			calendarbuffer +=
			"<td id=\"calendarMonth\" style=\"cursor:pointer;\" onClick=\"javascript:window.showCalendar(" + calendarmonthforward.getMonth() + "," + calendarmonthforward.getFullYear() + ");\">" +
			"<a href=\"javascript:window.showCalendar(" + calendarmonthforward.getMonth() + "," + calendarmonthforward.getFullYear() + ");\">>></a>"
		}

		calendarbuffer +=
		"</td> \n"+
		"</tr> \n" +	
		"<tr> \n" +
		"<td class=\"calendarSundayHeader\">" + daysofweekshort[0] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[1] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[2] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[3] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[4] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[5] + "</td> \n" +
		"<td class=\"calendarSaturdayHeader\">" + daysofweekshort[6] + "</td> \n" +
		"</tr> \n";

		var calendarRows = 0;

		while (month == calendartempdate.getMonth())
		{
			calendarRows++;
			
			calendarbuffer +=
			"<tr class=\"calendarDateRow\"> \n";

			for (var i=0; i <=6; i++)
			{
				if (calendartempdate.getDay() == i && month == calendartempdate.getMonth())
				{
					if ((todaysDate.getTime() <= addDays(calendartempdate.getTime(), (bookStart * -1))) && validRoute)
					{
						if (i == 0)
						{
							calendarbuffer +=
							"<td class=\"calendarSunday\" onMouseOver=\"javascript:this.className='calendarSundayHover';\" onMouseOut=\"javascript:this.className='calendarSunday';\" style=\"cursor:pointer;\" onClick=\"javascript:window.writeDate(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + "); hideMe();\"><a href=\"javascript:window.writeDate(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + ");hideMe();\">" + calendartempdate.getDate() + "</a></td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else if (i == 6)
						{
							calendarbuffer +=
							"<td class=\"calendarSaturday\" onMouseOver=\"javascript:this.className='calendarSaturdayHover';\" onMouseOut=\"javascript:this.className='calendarSaturday';\" style=\"cursor:pointer;\" onClick=\"javascript:window.writeDate(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + "); hideMe();\"><a href=\"javascript:window.writeDate(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + ");hideMe();\">" + calendartempdate.getDate() + "</a></td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else
						{
							calendarbuffer +=
							"<td class=\"calendarWeekday\" onMouseOver=\"javascript:this.className='calendarWeekdayHover';\" onMouseOut=\"javascript:this.className='calendarWeekday';\" style=\"cursor:pointer;\" onClick=\"javascript:window.writeDate(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + "); hideMe();\"><a href=\"javascript:window.writeDate(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + ");hideMe();\">" + calendartempdate.getDate() + "</a></td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
					}
					else {
						if ((todaysDate.getFullYear() == calendartempdate.getFullYear()) && (todaysDate.getMonth() == calendartempdate.getMonth()) && (todaysDate.getDate() == calendartempdate.getDate()) && (todaysDate.getDay() == i))
						{
							var today = "id=\"today\"";
						}
						else
						{
							var today = "";
						}

						if (i == 0)
						{
							calendarbuffer +=
							"<td " + today + " class=\"calendarSundayStrike\">" + calendartempdate.getDate() + "</td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else if (i == 6)
						{
							calendarbuffer +=
							"<td " + today + " class=\"calendarSaturdayStrike\">" + calendartempdate.getDate() + "</td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else
						{
							calendarbuffer +=
							"<td " + today + " class=\"calendarWeekdayStrike\">" + calendartempdate.getDate() + "</td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
					}
				}
				else if (i == 0)
				{
					calendarbuffer +=
					"<td class=\"calendarSundayNoBook\">&nbsp;</td> \n";
				}
				else if (i == 6)
				{
					calendarbuffer +=
					"<td class=\"calendarSaturdayNoBook\">&nbsp;</td> \n";
				}
				else 
				{
					calendarbuffer +=
					"<td class=\"calendarWeekdayNoBook\">&nbsp;</td> \n";
				}
			}
			calendarbuffer +=
			"</tr> \n";
		}

		calendarbuffer +=
		"</table>";

		document.getElementById("calendarContent").innerHTML = calendarbuffer;	
		
		if (isIE)
		{
			var calendarHeight = calendarOffset + 22 * calendarRows;
			document.getElementById("theLayer").style.height = calendarHeight + "px";
		}
		showMe();
	}
	// else incomplete route, display error message
	else
	{
		notCompleteRoute();
		//alert("please select a valid route");
	}
}

function changeCalendarDropDown2(monthyear)
{
	var monthyeararray = monthyear.split(",");
	showCalendar1(monthyeararray[0], monthyeararray[1]);
}

















function showCalendar1(month, year)
{
//alert("gets to showcal3 - ukairport = " + ukairport);
	// if route selected, show calendar
	if (isRouteSelected())
	{
		// clear existing calendar layer info
		document.getElementById("calendarContent").innerHTML = "";
		// display the calendar using a temporary date, split up important html tags
		var validRoute;
		
		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
			}
			else
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);					
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
			else
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}
		}
		
		//alert("selectedDeparture = " + selectedDeparture +" selectedDestination = "+ selectedDestination);

		var firstMonthWithRoute = findFirstRoute(selectedDeparture, selectedDestination);
		var monthYearArray = new Array();
		
		if (fromCalendar)
		{	
			monthYearArray[0] = month;
			monthYearArray[1] = year;
		}
		else
		{
			monthYearArray = moveToFirstRouteCalendar(selectedDeparture, selectedDestination);
		}
		
			fromCalendar = true;

		var calendartempdate = new Date(monthYearArray[1], monthYearArray[0], 1);
		var month = calendartempdate.getMonth();
		var year = calendartempdate.getFullYear();
		var monthYears = (year - startYear) * 12;
		var currentBit = month + startBit - startMonth + monthYears;
		var firstRouteBit = firstMonthWithRoute[0] + startBit - startMonth + ((firstMonthWithRoute[1] - startYear) * 12);
		var lastRouteBit = findLastRoute(selectedDeparture, selectedDestination, endBitUsed);	
		//alert("ROute Bits = " + firstRouteBit + " " + lastRouteBit)
		//alert("route bit = " + validMonthsUsed[selectedDestination][selectedDeparture] + ", current bit = " + Math.pow(2,currentBit));

if(ukairport == 1)
{
	if(layout == "LAYOUT1")
	{
		if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}
	}
	else
	{
		if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}	
	}
}
else
{
	if(layout == "LAYOUT1")
	{
		if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}
	}
	else
	{
		if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
		{	
			//alert("If validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit))");
			validRoute = true;	
		}
		else
		{
			//alert("");		
			validRoute = false;
		}	
	}
}
		
		var calendarbuffer = new String("<table id=\"calendarTable\" cellpadding=\"0\" cellspacing=\"0\"> \n" +
		"<tr> \n");	

		if ((firstMonthWithRoute[1] == year) && (firstMonthWithRoute[0] == month))
		{
			calendarbuffer +=
			"<td id=\"calendarMonth\">" + 
			"&nbsp;"
			//alert("firstMonthWithRoute[1] == year line 2067")
		}
		else
		{
			//alert("ELSE firstMonthWithRoute[1] == year line 2067")
			var calendarmonthback = new Date(calendartempdate.getTime());
			calendarmonthback.setMonth(calendarmonthback.getMonth() - 1);			
			calendarbuffer +=
			"<td id=\"calendarMonth\" style=\"cursor:pointer;\" onClick=\"javascript:window.showCalendar1("+ calendarmonthback.getMonth() + "," + calendarmonthback.getFullYear() + ");\">" +
			"<a href=\"javascript:window.showCalendar1("+ calendarmonthback.getMonth() + "," + calendarmonthback.getFullYear() + ");\"><<</a>"
		}

		calendarbuffer +=
		"</td> \n";

		var calendarmonthyear = new Date();
		calendarmonthyear.setFullYear(firstMonthWithRoute[1]);
		calendarmonthyear.setDate(1);
		calendarmonthyear.setMonth(firstMonthWithRoute[0])

		calendarbuffer +=
		"<td colspan=\"5\"> \n" +
		"<select id=\"calendarMonthYear\" onChange=\"javascript:window.changeCalendarDropDown2(this.value);\">";
			
		//alert("start bit = " + startBit);
		//alert("first route bit = " + firstRouteBit);
		//alert("current bit = " + currentBit);
		//alert("last route bit = " + lastRouteBit);
			
		for (var i = firstRouteBit; i <= lastRouteBit; i ++)
		{
			calendarbuffer += "<option value=\"" + calendarmonthyear.getMonth() + "," + calendarmonthyear.getFullYear() + "\"";
			
			if ((month == calendarmonthyear.getMonth()) && (year == calendarmonthyear.getFullYear()))
			{			
				calendarbuffer +=
				" selected=\"selected\"";			
			}
			
			calendarbuffer +=
			">" + monthsofyear[calendarmonthyear.getMonth()] + " " + calendarmonthyear.getFullYear() + "</option>\n";		
		
			calendarmonthyear.setMonth(calendarmonthyear.getMonth() + 1);		
		}
		
		calendarbuffer +=
		"</select>" +
		"</td> \n";

		if (currentBit >= lastRouteBit)
		{
			calendarbuffer +=
			"<td id=\"calendarMonth\">" + 
			"&nbsp;"
		}
		else
		{
			var calendarmonthforward = new Date(calendartempdate.getTime());
			calendarmonthforward.setMonth(calendarmonthforward.getMonth() + 1);
			calendarbuffer +=
			"<td id=\"calendarMonth\" style=\"cursor:pointer;\" onClick=\"javascript:window.showCalendar1(" + calendarmonthforward.getMonth() + "," + calendarmonthforward.getFullYear() + ");\">" +
			"<a href=\"javascript:window.showCalendar1(" + calendarmonthforward.getMonth() + "," + calendarmonthforward.getFullYear() + ");\">>></a>"
		}

		calendarbuffer +=
		"</td> \n"+
		"</tr> \n" +	
		"<tr> \n" +
		"<td class=\"calendarSundayHeader\">" + daysofweekshort[0] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[1] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[2] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[3] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[4] + "</td> \n" +
		"<td class=\"calendarWeekdayHeader\">" + daysofweekshort[5] + "</td> \n" +
		"<td class=\"calendarSaturdayHeader\">" + daysofweekshort[6] + "</td> \n" +
		"</tr> \n";

		var calendarRows = 0;

		while (month == calendartempdate.getMonth())
		{
			calendarRows++;
			
			calendarbuffer +=
			"<tr class=\"calendarDateRow\"> \n";

			for (var i=0; i <=6; i++)
			{
				if (calendartempdate.getDay() == i && month == calendartempdate.getMonth())
				{
					if ((todaysDate.getTime() <= addDays(calendartempdate.getTime(), (bookStart * -1))) && validRoute)
					{
						if (i == 0)
						{
							calendarbuffer +=
							"<td class=\"calendarSunday\" onMouseOver=\"javascript:this.className='calendarSundayHover';\" onMouseOut=\"javascript:this.className='calendarSunday';\" style=\"cursor:pointer;\" onClick=\"javascript:window.writeDate2(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + "); hideMe();\"><a href=\"javascript:window.writeDate2(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + ");hideMe();\">" + calendartempdate.getDate() + "</a></td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else if (i == 6)
						{
							calendarbuffer +=
							"<td class=\"calendarSaturday\" onMouseOver=\"javascript:this.className='calendarSaturdayHover';\" onMouseOut=\"javascript:this.className='calendarSaturday';\" style=\"cursor:pointer;\" onClick=\"javascript:window.writeDate2(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + "); hideMe();\"><a href=\"javascript:window.writeDate2(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + ");hideMe();\">" + calendartempdate.getDate() + "</a></td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else
						{
							calendarbuffer +=
							"<td class=\"calendarWeekday\" onMouseOver=\"javascript:this.className='calendarWeekdayHover';\" onMouseOut=\"javascript:this.className='calendarWeekday';\" style=\"cursor:pointer;\" onClick=\"javascript:window.writeDate2(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + "); hideMe();\"><a href=\"javascript:window.writeDate2(" + calendartempdate.getDate() + "," + calendartempdate.getMonth() + "," + calendartempdate.getFullYear() + ");hideMe();\">" + calendartempdate.getDate() + "</a></td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
					}
					else {
						if ((todaysDate.getFullYear() == calendartempdate.getFullYear()) && (todaysDate.getMonth() == calendartempdate.getMonth()) && (todaysDate.getDate() == calendartempdate.getDate()) && (todaysDate.getDay() == i))
						{
							var today = "id=\"today\"";
						}
						else
						{
							var today = "";
						}

						if (i == 0)
						{
							calendarbuffer +=
							"<td " + today + " class=\"calendarSundayStrike\">" + calendartempdate.getDate() + "</td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else if (i == 6)
						{
							calendarbuffer +=
							"<td " + today + " class=\"calendarSaturdayStrike\">" + calendartempdate.getDate() + "</td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
						else
						{
							calendarbuffer +=
							"<td " + today + " class=\"calendarWeekdayStrike\">" + calendartempdate.getDate() + "</td> \n";
							calendartempdate.setDate(calendartempdate.getDate() + 1);
						}
					}
				}
				else if (i == 0)
				{
					calendarbuffer +=
					"<td class=\"calendarSundayNoBook\">&nbsp;</td> \n";
				}
				else if (i == 6)
				{
					calendarbuffer +=
					"<td class=\"calendarSaturdayNoBook\">&nbsp;</td> \n";
				}
				else 
				{
					calendarbuffer +=
					"<td class=\"calendarWeekdayNoBook\">&nbsp;</td> \n";
				}
			}
			calendarbuffer +=
			"</tr> \n";
		}

		calendarbuffer +=
		"</table>";

		document.getElementById("calendarContent").innerHTML = calendarbuffer;	
		
		if (isIE)
		{
			var calendarHeight = calendarOffset + 22 * calendarRows;
			document.getElementById("theLayer").style.height = calendarHeight + "px";
		}
		showMe();
	}
	// else incomplete route, display error message
	else
	{
		notCompleteRoute();
		//alert("please select a valid route");
	}
}


function verifyDate2(dateString)
{
	// if route selected and valid date, set calendar date
	if (isRouteSelected()) 
	{
		var show = readDate(dateString)

		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
			}
			else
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);					
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
			else
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}
		}

		if (typeof(show) == "object")
		{	
			writeDate2(show.getDate(),show.getMonth(),show.getFullYear());
			clearErrorMessage("outRetDateText", "srchdate");			
			isRouteValid2(show);
		}
		else
		{
			switch (show)
			{
			 case 0: 	document.getElementById("outRetDateTextError").innerHTML = dateinvalid;
					document.getElementById("outRetDateTextError").className = "u-error";
					document.getElementById("outRetDateText").className = "srchdate u-errorcol";			 
			 		break;
			 case 1: 	document.getElementById("outRetDateTextError").innerHTML = datesmall;
			 		//alert("case 1");
			 		moveToFirstRouteNoMessage2(selectedDeparture, selectedDestination);
			 		break;
			 case 2: 	document.getElementById("outRetDateTextError").innerHTML = datebig;
			 		moveToFirstRouteNoMessage2(selectedDeparture, selectedDestination);
			 		break;
			
			
			}
				
		}
		
	}
	// else incomplete route, display error message
	else
	{
		notCompleteRoute();
	}
}


function isRouteValid2(show)
{
	//Check bit mask to see if current route is valid
	//alert("isRouteValid2 (" + show);
	var day = calendarDateRet.getDate();
	var month = calendarDateRet.getMonth();
	var year = calendarDateRet.getFullYear();
	
	var monthYears = (year - startYear) * 12;
	var currentBit = month + startBit - startMonth + monthYears;

		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);
			}
			else
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				var selectedDeparture = lookupvalueposition(destinationsUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(departuresUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);		
			}
			else
			{
				var selectedDeparture = lookupvalueposition(departuresUsed, document.getElementById("OutDepAir")[document.getElementById("OutDepAir").selectedIndex].value);
				var selectedDestination = lookupvalueposition(destinationsUsed, document.getElementById("OutArrAir")[document.getElementById("OutArrAir").selectedIndex].value);			
			}		
		}

	//alert(selectedDestination + " " + selectedDeparture + " " + ukairport)

		if(ukairport == 1)
		{
			if(layout == "LAYOUT1")
			{		
				if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
				{
					calendarDateRet = show;	
				}
				else
				{
					moveToFirstRoute2(selectedDestination, selectedDeparture);
				}
			}
			else
			{
				if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
				{
					calendarDateRet = show;	
				}
				else
				{
					moveToFirstRoute2(selectedDeparture, selectedDestination);
				}			
			}
		}
		else
		{
			if(layout == "LAYOUT1")
			{
				if (validMonthsUsed[selectedDeparture][selectedDestination] & (Math.pow(2,currentBit)))
				{
					calendarDateRet = show;	
				}
				else
				{
					//alert("gets to move route selectedDeparture = " + selectedDeparture)
					moveToFirstRoute2(selectedDeparture, selectedDestination);
				}
			}
			else
			{
				if (validMonthsUsed[selectedDestination][selectedDeparture] & (Math.pow(2,currentBit)))
				{
					calendarDateRet = show;	
				}
				else
				{
					moveToFirstRoute2(selectedDestination, selectedDeparture);
				}			
			}
		}
}

function moveToFirstRoute2(selectedDeparture, selectedDestination)
{
	var monthYearArray = findFirstRoute(selectedDeparture, selectedDestination);
	
	writeDate2(1, monthYearArray[0], monthYearArray[1]);

	document.getElementById("outRetDateTextError").innerHTML = datemoved;
	document.getElementById("outRetDateTextError").className = "u-change";		
	//alert("date moved to first available for this route");
	document.getElementById("outRetDateTextError").style.display = "";
	document.getElementById("outRetDateText").focus();		
}

function moveToFirstRouteNoMessage2(selectedDeparture, selectedDestination)
{
	var monthYearArray = findFirstRoute(selectedDeparture, selectedDestination);
	
	writeDate2(1, monthYearArray[0], monthYearArray[1]);	
	document.getElementById("outRetDateTextError").className = "u-change";
	document.getElementById("outRetDateText").focus();		
}


