// destination_seatseeker_v1-5
/*

1-2	all zero fares are allowed, have already been entered into navitaire as 'free seats'
1-3	added 99p by default code
1-4 	 mod for new tax element in xml feed - essentially 1-8 code removed and new flight object tax element added, removed saleFareToggle code,
	 use a comparason instead and go with the cheapest, now that AdultSaleFares return as null if not present (not 0.00)
1-5	For Skylights 8 - New URl code, new error trapping,
1-6 removed tax toggle stuff as taxes are now included in prices by default

*/




// globals...
var ninenineToggle = "true" ; // defaults all prices to £xx.99p
var returnCloseDate = 3; // closest date from departure brought back for returns 
var returnFarDate = 25; // furthest date from departure brought back for returns  

var xmlhttp;
var xmlDoc;

var oneWaySelector; //true=oneway, false=return
var allRoutes = new Array(); // master array - holds the route arrays of flight objects
var allRoutesIndex = new Array(); // matches route number to airport code and number of flights


function flightTypeSelector() {
	var d = new Date();
  var  cacheBuster = d.getTime();

	switch (directionAllowed) {
		case "both":
			txt = 	"<h4>Cheap seats to " + codeToAirport(page_arrivingAirport) + "</h4>";
			txt +=  "<h4>Choose one-way or return:</h4>";
			txt += "<table>";
			txt +=  "<tr><td><input onClick=\"newStart(\'/skylights/xml/FlightData_1WAY_" + page_arrivingAirport + "_outbound.xml?cb=" + cacheBuster + "\')\" name=\'typeSelector\' type=\'radio\' value=\'oneway\'\/>One-way Flights<\/td><\/tr>";
			txt +=  "<tr><td><input onClick=\"newStart(\'/skylights/xml/FlightData_RETURN_" + page_arrivingAirport + "_outbound.xml?cb=" + cacheBuster + "\')\" name=\'typeSelector\' type=\'radio\' value=\'return\'\/>Return Flights<\/td><\/tr>";
			txt +=  "<\/table>";
			break;

		case "oneway":
			txt = "<h4>Cheap seats to " + codeToAirport(page_arrivingAirport) + "</h4>";
			newStart("/skylights/xml/FlightData_1WAY_" + page_arrivingAirport + "_outbound.xml?cb=" + cacheBuster)
			break;

		case "return":
			txt = "<h4>Cheap returns to " + codeToAirport(page_arrivingAirport) + "</h4>";
			newStart("/skylights/xml/FlightData_RETURN_" + page_arrivingAirport + "_outbound.xml?cb=" + cacheBuster)
			break;
	}

	document.getElementById("typeSelector").innerHTML=txt;
	document.getElementById("typeSelector").style.display="block";
}


function newStart(url) {
	document.getElementById("loader").innerHTML="";
	document.getElementById("loader").style.display="none";
	document.getElementById("airportSelector").innerHTML="";
	document.getElementById("airportSelector").style.display="none";
	document.getElementById("outTable").innerHTML="";
	document.getElementById("outTable").style.display="none";
	document.getElementById("inTable").innerHTML="";
	document.getElementById("inTable").style.display="none";
	document.getElementById("paxTable").innerHTML="";
	document.getElementById("paxTable").style.display="none";
	loadXMLDoc(url)
}



function pageProcesses() {
	setOneWaySelector();
	buildArrays();
	buildSelector();
}


function setOneWaySelector() {
	(getCheckedValue(document.getElementById("inputForm").typeSelector) == "oneway") ? oneWaySelector=true : oneWaySelector=false;
}


function buildArrays() {
	route = xmlDoc.getElementsByTagName("route"); // gives an array of all the routes

	for (r=0; r<route.length; r++)
	{
		// master array - format is allroutes[route-index][flight-index]
		var stuffing = getFlights(r, route);
		allRoutes[r] = stuffing;
		// array for route number vs route airport code,  number of flights on route
		allRoutesIndex[r] = new Array(route[r].getAttribute("DepAiportCode"), stuffing.length)
	}
}


function getFlights(q, route) {
	var x=route[q].getElementsByTagName("flight");
	var y=route[q].getElementsByTagName("prices");
	var temp = new Array();
	
	for (i=0;i<x.length;i++)	{
		if (y[i].getAttribute("AdultSaleFare") != "") { // take any saleFare over a regFare (0.00 is a valid fare)
				temp[temp.length] = new FlightObj(x[i].getAttribute("Direction"),
								x[i].getAttribute("FlightDateTime"),
								x[i].getAttribute("M1"),
								x[i].getAttribute("M2"),
								y[i].getAttribute("AdultSaleFare"));
		}
		else if (y[i].getAttribute("AdultRegFare") != "") { // use reg fare (0.00 is a valid fare)
				temp[temp.length] = new FlightObj(x[i].getAttribute("Direction"),
								x[i].getAttribute("FlightDateTime"),
								x[i].getAttribute("M1"),
								x[i].getAttribute("M2"),
								y[i].getAttribute("AdultRegFare"));
		}
		else { // do nothing
		}
	}
	return temp;
}


function buildSelector() {
	var airport =  new Array();
	airport ["ABZ"] = "Aberdeen";
	airport ["BFS"] = "Belfast";
	airport ["BHX"] = "Birmingham";
	airport ["BOH"] = "Bournemouth";
	airport ["BRS"] = "Bristol";
	airport ["CWL"] = "Cardiff";
	airport ["DSA"] = "Doncaster Robin Hood";
	airport ["MME"] = "Durham Tees-Valley";
	airport ["EDI"] = "Edinburgh";
	airport ["EXT"] = "Exeter";
	airport ["GLA"] = "Glasgow";
	airport ["HUY"] = "Humberside";
	airport ["LBA"] = "Leeds/Bradford";
	airport ["LPL"] = "Liverpool";
	airport ["LGW"] = "London Gatwick";
	airport ["LTN"] = "London Luton";
	airport ["STN"] = "London Stansted";
	airport ["MAN"] = "Manchester";
	airport ["NCL"] = "Newcastle";
	airport ["NWI"] = "Norwich"
	airport ["EMA"] = "Nottingham East Midlands";

	if(allRoutesIndex.length != 0) {
		txt = "<h4>Leaving from:</h4>";
		txt += "<table><tr><td><select id=\'outAirportSelector\' name=\'outAirportSelector\' onChange=\'setPage(this.value);\'>";
		txt += "<option value=\'\'>--Departing Airport--<\/option>";

		for(q=0; q<allRoutesIndex.length; q++) {
			var depAirport = allRoutesIndex[q][0];
			txt += "<option value=\'" + depAirport +"\'>" + airport[depAirport] + "<\/option>";
		}

		txt += "<\/select><\/tr><\/table>";
		document.getElementById("airportSelector").innerHTML=txt;
		document.getElementById("airportSelector").style.display="block";
	}
	else {
		  atxt="<img src=\"..\/images\/seatseeker\/loading_error.gif\" height=\"35px\" alt=\"Sorry there is a problem with the data feed, please try again later!\" width=\"200px\"\/>";
		  document.getElementById("loader").innerHTML=atxt;
			document.getElementById("loader").style.display="block";
			document.getElementById("airportSelector").style.display="none";
	}

}


function setPage(depAirport) {
document.getElementById("inTable").innerHTML="";
document.getElementById("paxTable").innerHTML="";
if (depAirport != '') {
	buildOutTable(depAirport);
	}
}


function buildOutTable(depAirport) {
	var preferredAirport = Array("LGW","MAN","STN","BHX","NCL","GLA","EMA","LTN","BRS","LPL","NWI","LBA","EXT","EDI","BFS","ABZ","HUY","BOH","MME","CWL","DSA");
	var foundIt = false;
	var buildWith;
	var allRoutesIndexNum;
	var temp = new Array();

	// determine airport to show
	if (depAirport==null) {
		// find find if preferred airport exists
		loopI:
		for(i=0; i<preferredAirport.length; i++) {
			if (foundIt == false)
			{
				for(q=0; q<allRoutesIndex.length; q++) {
					if (preferredAirport[i]==allRoutesIndex[q][0]) {
						foundIt=true;
						buildWith=allRoutesIndex[q][0];
						break loopI;
					}
				}
			}
		}
	}
	else {
	buildWith = depAirport;
	}
	// build Temp array

	for(t=0; t<(allRoutes[getAllRoutesIndexFromAirportCode(buildWith)].length); t++)	{
		temp.push(allRoutes[getAllRoutesIndexFromAirportCode(buildWith)][t]);
		//alert(allRoutes[getAllRoutesIndexFromAirportCode(buildWith)][t].flightNumber)
	}

	// clean and sort array depending on parameters passed...sorted by price, return 10 results

	if(!oneWaySelector) {	// return flights
		temp.sort(sortBydirection);
		for (i=0;i<temp.length;i++) {
			if (temp[i].direction.toLowerCase() != "out") 	{
				temp = temp.slice(0,i);
				break;
			}
		}
		temp.sort(sortByadultFare);
		//temp = temp.slice(removeZeroPrice(temp));
		temp = temp.slice(0,10);
	}
	else { 	// one way flights
		temp.sort(sortByadultFare);
		//temp = temp.slice(removeZeroPrice(temp));
		temp = temp.slice(0,10);
	}
	
	
	if (temp.length != 0) {
	
		// build and output content
		txt = "<h4>Choose an outbound flight:</h4>";
		txt += "<table>";
		txt +="<tr><td><\/td><td><span class=\"headerText\">Departs<\/span><\/td><td width=\"30px\"><span class=\"headerText\">From<\/span><\/td><\/tr>";

		for (i=0;i<temp.length;i++) {

			if (i%2) { //odd
				txt += "<tr>";
			}
			else {
				txt += "<tr class=\"stripe\">"
			}

			if (oneWaySelector) {
				txt +=  "<td><input onClick=\'buildPaxTable()\' name=\'m1\' type=\'radio\' value=\'" + temp[i].m1LinkCode + "\'\/><\/td>";
			}
			else {
				txt +=  "<td><input onClick=\'buildInTable(\"" + temp[i].m1LinkCode + "\")\' name=\'m1\' type=\'radio\' value=\'" + temp[i].m1LinkCode + "\'\/><\/td>";
			}

			// get data for row
			var dateTxt = "<td>" + temp[i].flightDateTime.substring(8,10) + " " + dateToMonth(temp[i].flightDateTime.substring(5,7)) + " " + "\'" + temp[i].flightDateTime.substring(2,4) + "<\/td>";


			var priceTxt = "<td><b>&pound;";
			var fare = temp[i].adultFare;
			
			// check 99p invoked	
			if (ninenineToggle == "true") {
				priceTxt +=  ninenineFare(fare)
			}
			else {
				priceTxt += fare
			}

			priceTxt += "<\/b><\/td>";

			txt += dateTxt + priceTxt ;
			txt += "<\/tr>" +"\n";
		}
	}
	else {
		// opps no content
		txt = "<h4>Choose an outbound flight:</h4>";
		txt += "<table>";
		txt +="<tr><td>Sorry, we cannot find any results for this airport. Please select another UK airport, or use the more detailed search at the top of the page.<\/td><\/tr>";
	}
	
	txt += "<\/table>";
	document.getElementById("outTable").innerHTML=txt;
	document.getElementById("outTable").style.display="block";
}


function ninenineFare (fare) {
	var fareSplit = new Array;
	fareSplit = (fare.toString()).split(".");
	//alert("a " + fareSplit[0] + " b " + fareSplit[1]);
	var nineninefare = fareSplit[0] + ".99";
	return nineninefare;
}


function buildInTable(linkCode) {

	var temp = new Array();
 	var startDate = plusDaysToDate(linkCode, returnCloseDate);
 	var endDate = plusDaysToDate(linkCode, returnFarDate);
 	var buildWith = linkCode.substring(16,19);

	document.getElementById("paxTable").innerHTML="";

	// test for results
	if (allRoutes[getAllRoutesIndexFromAirportCode(buildWith)].length == 1) { // must only be outbound to get this far!
		txt = 	"<h4>Choose an inbound flight:</h4>";
		txt += 	"<table>";
		txt += 	"<tr><td>Sorry, we are unable to find an matching return flight. Please select another UK airport, or use the more detailed search at the top of the page.<\/tr>";
		txt += 	"<\/table>";
	}
	else {
		// build Temp array
		for(t=0; t<(allRoutes[getAllRoutesIndexFromAirportCode(buildWith)].length); t++)	{
			if (allRoutes[getAllRoutesIndexFromAirportCode(buildWith)][t].m2LinkCode != null)	{
				if (allRoutes[getAllRoutesIndexFromAirportCode(buildWith)][t].m2LinkCode.substring(0,8) >= startDate && allRoutes[getAllRoutesIndexFromAirportCode(buildWith)][t].m2LinkCode.substring(0,8) <= endDate) {
					temp.push(allRoutes[getAllRoutesIndexFromAirportCode(buildWith)][t]);
				}
			}
		}

		if (temp.length == 0) {
			txt = 	"<h4>Choose an inbound flight:</h4>";
			txt += 	"<table>";
			txt += 	"<tr><td>Sorry we are unable to find an matching return for your outbound flight. Please try our main seach, as it has a wider selection of dates for this aiport.<\/td><\/tr>";
			txt += 	"<\/table>";
		}
		else {

			// clean and sort array depending on parameters passed...sorted by date&time, all results

			// remove all 0 entries
			//temp = temp.slice(removeZeroPrice(temp));

			// build content
			txt = 	"<h4>Choose an inbound flight:<\/h4>";
			txt += 	"<table>";
			txt +=   "<tr ><td><\/td><td><span class=\"headerText\">Departs<\/span><\/td><td width=\"30px\"><span class=\"headerText\">From<\/span><\/td><\/tr>";

			for (i=0;i<temp.length;i++) {

				if (i%2) { //odd
					txt += "<tr>";
				}
				else {
					txt += "<tr class=\"stripe\">"
				}

					txt +=  "<td>" + "<input onClick=\'buildPaxTable()\' name=\'m2\' type=\'radio\' value=\'" + temp[i].m2LinkCode + "\'\/><\/td>";

					// get data for row
					var dateTxt = "<td>" + temp[i].flightDateTime.substring(8,10) + " " + dateToMonth(temp[i].flightDateTime.substring(5,7)) + " " + "\'" + temp[i].flightDateTime.substring(2,4) + "<\/td>";
					var priceTxt = "<td><b>&pound;";
					var fare = temp[i].adultFare;
					// check if 99p invoked
					if (ninenineToggle == "true") {
						priceTxt +=  ninenineFare(fare)
					}
					else {
						priceTxt += fare
					}

					txt += dateTxt + priceTxt ;
					txt += "<\/tr>" +"\n";
				}
				txt += "<\/table>";
			}
			// output content
			document.getElementById("inTable").innerHTML=txt;
			document.getElementById("inTable").style.display="block";
		}
}


function getAllRoutesIndexFromAirportCode(airportCode) {
	var allRoutesIndexNum;
	for (i=0; i<allRoutesIndex.length; i++)
	{
		if (allRoutesIndex[i][0]==airportCode)
		{
			allRoutesIndexNum=i;
			break;
		}
	}
	return allRoutesIndexNum
}


function removeZeroPrice (temp) {
		temp.sort(sortByadultFare);
		if (temp[0].adultFare==0) {
			var cutOff=0;
			for(i=1; i<temp.length; i++) {
				if ( (temp[i-1].adultFare==0) && (temp[i].adultFare!=0) ) {
					cutOff=i;
				}
			}
		return cutOff;
		}
}


function buildPaxTable() {
	var txt;
	txt = "<h4>How many travelling?<\/h4>";
	txt += "<table><tr><td colspan=\'2\'>Adults (18+ years)<\/td><td><select id=\'pax_adult\' name=\'deals_ADULT\'><option value=\'1\'>1<\/option><option value=\'2\' selected=\'selected\'>2<\/option><option value=\'3\'>3<\/option><option value=\'4\'>4<\/option><option value=\'5\'>5<\/option><option value=\'6\'>6<\/option><option value=\'7\'>7<\/option><option value=\'8\'>8<\/option><option value=\'9\'>9<\/option><\/select><\/td><\/tr>";
	txt += "<tr><td colspan=\'2\'>Children (2-17 years)<\/td><td><select id=\'pax_child\' name=\'deals_CHILD\'><option value=\'0\' selected=\'selected\'>0<\/option><option value=\'1\'>1<\/option><option value=\'2\' >2<\/option><option value=\'3\'>3<\/option><option value=\'4\'>4<\/option><option value=\'5\'>5<\/option><option value=\'6\'>6<\/option><option value=\'7\'>7<\/option><option value=\'8\'>8<\/option><option value=\'9\'>9<\/option><\/select><\/td><\/tr>";
	txt += "<tr><td colspan=\'2\'>Infants (0-23 months)<\/td><td><select id=\'pax_infant\' name=\'deals_INFANT\'><option value=\'0\' selected=\'selected\'>0<\/option><option value=\'1\'>1<\/option><option value=\'2\'>2<\/option><option value=\'3\'>3<\/option><option value=\'4\'>4<\/option><option value=\'5\'>5<\/option><option value=\'6\'>6<\/option><option value=\'7\'>7<\/option><option value=\'8\'>8<\/option><option value=\'9\'>9<\/option><\/select><\/td><\/tr>";
	txt += "<tr><td colspan=\'3\'><button type=\'button\' onClick=\'process(this.form)\' alt=\'Seek Seek\' title=\'Seat Seek'\><\/button><\/td><\/tr>";
	txt += "<tr><td colspan=\'3\'><br \/>Price is per adult and includes all taxes and changes.<\/td><\/tr>";
	txt += "<\/table>";

	document.getElementById("paxTable").innerHTML=txt;
	document.getElementById("paxTable").style.display="block";

}

function process(form) {
	var deals_totalPax;
	var deals_adultPax = document.getElementById("pax_adult");
	var deals_childPax = document.getElementById("pax_child");
	var deals_infantPax = document.getElementById("pax_infant");
	var deals_url;

	var deals_root="https://book.flythomascook.com/skylights/cgi-bin/skylights.cgi?";
	
	var deals_ADULT = "ADULT=" + deals_adultPax.options[deals_adultPax.selectedIndex].value;
	var deals_ADULTpx = "&ADULTpx=ADTI";
	var deals_CHILD = "&CHILD=" + deals_childPax.options[deals_childPax.selectedIndex].value;
	var deals_CHILDpx = "&CHILDpx=CHDI";
	var deals_INFANT = "&INFANT=" + deals_infantPax.options[deals_infantPax.selectedIndex].value;
	var deals_language = "&language=EN";
	var deals_module = "&module=SB";
	var deals_page = "&page=SELECT";
	var deals_sid = "&sid=";
	var deals_event= "&event=select";	
	
	var fltm1 = getCheckedValue(document.getElementById("inputForm").m1);	
	var deals_m1 = "&m1=" + fltm1;
	var deals_flight_1 = "&flight_1=" + fltm1.substring(0,50) + "|" + 
							 fltm1.substring(50,52) + "|" + 
							 fltm1.substring(52,54) + "||" + 
							 fltm1.substring(54,55) + "|" + 
							 (fltm1.substring(56,fltm1.length)).replace(/\+/g,"");  

	var deals_from1 = "&from1=" +  fltm1.substring(16,19);
	var deals_to1 = "&to1=" + fltm1.substring(19,22);
	var deals_departDay1 = "&departDay1=" + fltm1.substring(6,8);
	var deals_departMonth1 = "&departMonth1=" + fltm1.substring(0,6);	

	if (oneWaySelector != true) {
		var fltm2 = getCheckedValue(document.getElementById("inputForm").m2);	
		var deals_m2 = "&m2=" + fltm2;
		var deals_flight_2 = "&flight_2=" + fltm2.substring(0,50) + "|" + 
								 fltm2.substring(50,52) + "|" + 
								 fltm2.substring(52,54) + "||" + 
								 fltm2.substring(54,55) + "|" + 
							 		(fltm2.substring(56,fltm2.length)).replace(/\+/g,"");  	
								 
		var deals_from2 = "&from2=" +  fltm2.substring(16,19);
		var deals_to2 = "&to2=" + fltm2.substring(19,22);
		var deals_departDay2 = "&departDay1=" + fltm2.substring(6,8);
		var deals_departMonth2 = "&departMonth1=" + fltm2.substring(0,6);				
	}				

	var deals_travel;
	(oneWaySelector) ? deals_travel = "&travel=1" : deals_travel = "&travel=2" ;
	
	var deals_numberMarkets;
	(oneWaySelector) ? deals_numberMarkets = "&numberMarkets=1" : deals_numberMarkets = "&numberMarkets=2" ;

	
	
	// pax check
	if (parseInt(deals_adultPax.options[deals_adultPax.selectedIndex].value) + parseInt(deals_childPax.options[deals_childPax.selectedIndex].value) + parseInt(deals_infantPax.options[deals_infantPax.selectedIndex].value) > 9) {
		alert("You are allowed a maximum of 9 passengers \n per booking online. If your party is larger \n than this, please call our reservation center.");
		return false;
	}

	if (parseInt(deals_infantPax.options[deals_infantPax.selectedIndex].value) > parseInt(deals_adultPax.options[deals_adultPax.selectedIndex].value)) {
		alert("Sorry, you cannot select more infants \n than the number of adults travelling.");
		return false;
	}

	//build url for deep link
	if (oneWaySelector) {
		var deals_url = deals_root + deals_ADULT + deals_ADULTpx + deals_CHILD + deals_CHILDpx + deals_INFANT + deals_numberMarkets + deals_language + deals_module + deals_page + deals_m1 + deals_flight_1 + deals_sid + deals_event + deals_from1 + deals_to1 + deals_departDay1 + deals_departMonth1 + deals_travel;
	} 
	else {
		var deals_url = deals_root + deals_ADULT + deals_ADULTpx + deals_CHILD + deals_CHILDpx + deals_INFANT + deals_numberMarkets + deals_language + deals_module + deals_page + deals_m1 + deals_flight_1 + deals_m2 + deals_flight_2 + deals_sid + deals_event + deals_from1 + deals_to1 + deals_departDay1 + deals_departMonth1 + deals_from2 + deals_to2 + deals_departDay2 + deals_departMonth2 + deals_travel;

	}
	//build url for nedstat tracking
	
	//var nedstat_dir  = (oneWaySelector) ? ".oneway" : ".return" ;
	//var nedstat_url = "http://uk.sitestat.com/tcook/flythomascook/s?flytc.in-dest." +
	//							"from-" + deals_m1.substring(20,23) +
	//							".to-" + deals_m1.substring(23,26) +
	//							nedstat_dir +
	//							".date-" + deals_m1.substring(4,12) +
	//							"&ns_type=clickout&ns_url=" ;

	//window.location.href = nedstat_url + deals_url;
	window.location.href = deals_url;
}


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function plusDaysToDate(linkCode, plusDays) {
	var outYear, outMonth, outDay, inDate, inYear, inMonth, inDay, inDateStr;
	var plusDaysMS = (1000*60*60*24*plusDays);

	outYear = linkCode.substring(0,4);
	outMonth = linkCode.substring(4,6);
	outDay = linkCode.substring(6,8);

	outDate = new Date();
	inDate = new Date();

	outDate.setFullYear(outYear, outMonth-1, outDay);
	outMS = outDate.getTime();
	inDateMS = (outMS + plusDaysMS);

	inDate.setTime(inDateMS);

	inYear = String(inDate.getFullYear());
	inMonth= String(inDate.getMonth()+1);
	inDay= String(inDate.getDate());

	if (inMonth.length == 1) inMonth =  "0".concat (inMonth);
	if (inDay.length == 1) inDay = "0".concat (inDay);

	inDateStr = inYear.concat(inMonth, inDay);
	return inDateStr;
}



function codeToAirport (code) {
	var airport =  new Array();
	airport ["ABZ"] = "Aberdeen";
	airport ["BFS"] = "Belfast";
	airport ["BHX"] = "Birmingham";
	airport ["BOH"] = "Bournemouth";
	airport ["BRS"] = "Bristol";
	airport ["CWL"] = "Cardiff";
	airport ["DSA"] = "Doncaster Robin Hood";
	airport ["MME"] = "Durham Tees-Valley";
	airport ["EDI"] = "Edinburgh";
	airport ["EXT"] = "Exeter";
	airport ["GLA"] = "Glasgow";
	airport ["HUY"] = "Humberside";
	airport ["LBA"] = "Leeds/Bradford";
	airport ["LPL"] = "Liverpool";
	airport ["LGW"] = "London Gatwick";
	airport ["LTN"] = "London Luton";
	airport ["STN"] = "London Stansted";
	airport ["MAN"] = "Manchester";
	airport ["NCL"] = "Newcastle";
	airport ["NWI"] = "Norwich"
	airport ["EMA"] = "Nottingham East Midlands";
	airport["AGA"] = "Agadir";
	airport["ALC"] = "Alicante";
	airport["LEI"] = "Almeria";
	airport["AYT"] = "Antalya";
	airport["BJL"] = "Banjul";
	airport["BJV"] = "Bodrum";
	airport["BOJ"] = "Bourgas";
	airport["YYC"] = "Calgary";
	airport["CUN"] = "Cancun";
	airport["CTA"] = "Catania";
	airport["CMB"] = "Colombo";
	airport["CFU"] = "Corfu";
	airport["GOI"] = "Dabolim/Goa";
	airport["DLM"] = "Dalaman";
	airport["POP"] = "Dominican Republic/Puerto Plata";
	airport["VDB"] = "Fagernes";
	airport["FAO"] = "Faro";
	airport["SFB"] = "Florida/Sanford";
	airport["FUE"] = "Fuerteventura";
	airport["FNC"] = "Funchal/Madeira";
	airport["GVA"] = "Geneva";
	airport["GRO"] = "Gerona";
	airport["GNB"] = "Grenoble";
	airport["LPA"] = "Gran Canaria";
	airport["YHZ"] = "Halifax";
	airport["SKG"] = "Halkidiki";
	airport["HER"] = "Heraklion/Crete";
	airport["HRG"] = "Hurghada";
	airport["IBZ"] = "Ibiza";
	airport["INN"] = "Innsbruck";
	airport["ADB"] = "Izmir";
	airport["XRY"] = "Jerez";
	airport["KLX"] = "Kalamata";
	airport["KVA"] = "Kavala";
	airport["EFL"] = "Kefalonia";
	airport["KGS"] = "Kos";
	airport["TRV"] = "Trivandrum";
	airport["ACE"] = "Lanzarote";
	airport["LCA"] = "Larnaca/Cyprus";
	airport["LXS"] = "Lemnos";
	airport["LXR"] = "Luxor";
	airport["LYS"] = "Lyon";
	airport["PMI"] = "Majorca/Palma";
	airport["AGP"] = "Malaga";
	airport["MLE"] = "Male";
	airport["MLA"] = "Malta";
	airport["RMF"] = "Marsa Alam";
	airport["MAH"] = "Menorca";
	airport["MBA"] = "Mombasa";
	airport["MIR"] = "Monastir/Tunisia";
	airport["MBJ"] = "Montego Bay";
	airport["YUL"] = "Montreal";
	airport["NAP"] = "Naples";
	airport["OSD"] = "Ostersund";
	airport["YOW"] = "Ottawa";
	airport["PFO"] = "Paphos";
	airport["PDV"] = "Plovdiv";
	airport["PUJ"] = "Punta Cana";
	airport["PVK"] = "Preveza";
	airport["YQB"] = "Quebec";
	airport["REU"] = "Reus";
	airport["RHO"] = "Rhodes";
	airport["RVN"] = "Rovaniemi";
	airport["SSA"] = "Salvador Dois de Julho";
	airport["SZG"] = "Salzburg";
	airport["JTR"] = "Santorini";
	airport["SSH"] = "Sharm El Sheikh";
	airport["JSI"] = "Skiathos";
	airport["SOF"] = "Sofia";
	airport["SPU"] = "Split";
	airport["TFS"] = "Tenerife";
	airport["TLS"] = "Toulouse";
	airport["YYZ"] = "Toronto";
	airport["TRN"] = "Turin";
	airport["YVR"] = "Vancouver";
	airport["VAR"] = "Varna/Bulgaria";
	airport["VCE"] = "Venice";
	airport["VRN"] = "Verona";
	airport["ZTH"] = "Zante";
	return airport[code];
}


function dateToMonth (month) {
	var months =  new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul" , "Aug", "Sep", "Oct", "Nov", "Dec");
	return months[month-1];
}


function sortBydirection(a, b) { // to change order: return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    var x = a.direction;
    var y = b.direction;
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


function sortByadultFare(a, b) { // to change order: return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    var x = a.adultFare;
    var y = b.adultFare;
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


function sortByflightDateTime(a, b) { // to change order: return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    var x = a.flightDateTime;
    var y = b.flightDateTime;
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}


function FlightObj (direction, flightDateTime, m1LinkCode, m2LinkCode, adultFare) {
	this.direction=direction;
	this.flightDateTime=flightDateTime;
	this.m1LinkCode=m1LinkCode;
	this.m2LinkCode=m2LinkCode;
	this.adultFare=parseFloat(adultFare);
	//methods...
	this.showAllFlight=showAllFlight;
}

function showAllFlight(Array, location) {
	var txt=""
	for (i=0;i<Array.length;i++) {
		txt += Array[i].direction + " " +
			Array[i].flightDateTime + " " +
			Array[i].m1LinkCode + " " +
			Array[i].m2LinkCode + " " +
			Array[i].adultFare + " " +"<br/>" ;
	}
	document.getElementById(location).innerHTML=txt;
	document.getElementById(location).style.display="block";
}
