

_region = new Array;
_region[0] = new Array('CE', new Array(
new Array('MAIN', 'All Main Airports'),
new Array('BHX', 'Birmingham'),
new Array('CVT', 'Coventry'),
new Array('EMA', 'East Midlands'),
new Array('LPL', 'Liverpool'), 
new Array('LTN', 'London Luton'),
new Array('STN', 'London Stansted'),
new Array('MAN', 'Manchester'), 
new Array('NWI', 'Norwich')));

_region[1] = new Array('NE', new Array(
new Array('MAIN', 'All Main Airports'),
new Array('NCL', 'Newcastle'),
new Array('MME', 'Teeside'),
new Array('BLK', 'Blackpool'),
new Array('LBA', 'Leeds Bradford'), 
new Array('LPL', 'Liverpool'),
new Array('MAN', 'Manchester'),
new Array('DSA', 'Doncaster Sheffield'), 
new Array('HUY', 'Humberside')));

_region[2] = new Array('NI', new Array(
new Array('MAIN', 'All Main Airports'),
new Array('IOM', 'Isle of Man'),
new Array('LDY', 'Derry'),
new Array('BFS', 'Belfast'),
new Array('BHD', 'Belfast City'))); 

_region[3] = new Array('SCO', new Array(
new Array('MAIN', 'All Main Airports'),
new Array('LSI', 'Sumburgh (Shetlands)'),
new Array('KOI', 'Kirkwall (Orkney)'),
new Array('SYY', 'Stornoway'),
new Array('WIC', 'Wick'), 
new Array('BEB', 'Benbecula'),
new Array('INV', 'Inverness'),
new Array('BRR', 'Barra'),
new Array('ABZ', 'Aberdeen'),
new Array('DND', 'Dundee'),
new Array('TRE', 'Tiree'),
new Array('GLA', 'Glasgow'),
new Array('EDI', 'Edinburgh'),
new Array('ILY', 'Islay'),
new Array('CAL', 'Campbelttown'),
new Array('PIK', 'Glasgow Prestwick'),
new Array('NCL', 'Newcastle')));

_region[4] = new Array('SW', new Array(
new Array('MAIN', 'All Main Airports'),
new Array('BRS', 'Bristol'),
new Array('CWL', 'Cardiff'),
new Array('TSO', 'Tresco'),
new Array('ISC', 'St Marys'), 
new Array('SOU', 'Southampton'),
new Array('EXE', 'Exeter'),
new Array('NQY', 'Newquay'),
new Array('PLH', 'Plymouth'),
new Array('LEQ', 'Lands End'),
new Array('PZE', 'Penzance'),
new Array('GCI', 'Guernsey'),
new Array('ACI', 'Alderney'),
new Array('BOH', 'Bournemouth'),
new Array('JER', 'Jersey')));

_region[5] = new Array('SE', new Array(
new Array('MAIN', 'All Main Airports'),
new Array('LON', 'All London Airports'),
new Array('BOH', 'Bournmouth'),
new Array('BSH', 'Brighton'),
new Array('LCY', 'London City'),
new Array('LGW', 'London Gatwick'), 
new Array('LHR', 'London Heathrow'),
new Array('LTN', 'London Luton'),
new Array('STN', 'London Stansted'),
new Array('LYX', 'Lydd'),
new Array('SOU', 'Southampton')));


function partOfGrouping(dpt)
{

	for (i=0; i <= _region.length - 1; i++) {

			for (j=0; j <= _region[i][1].length - 1; j++) {
	
				if (_region[i][1][j][0] == dpt)
				{
					return _region[i][0];
				}
			}


	}
}

function PopulateHiddenFields()
{

var dptMonth;
dptMonth = document.forms[0].elements['flight-finder-departure-month-control'][document.forms[0].elements['flight-finder-departure-month-control'].selectedIndex].value;
if (dptMonth.length == 1)
{
 	dptMonth = '0' + dptMonth;
}

var arrMonth;
arrMonth = document.forms[0].elements['flight-finder-return-month-control'][document.forms[0].elements['flight-finder-return-month-control'].selectedIndex].value ;
if (arrMonth.length == 1)
{
 	arrMonth = '0' + arrMonth;
}


var returnDate;
returnDate = document.forms[0].elements['flight-finder-return-year-control'][document.forms[0].elements['flight-finder-return-year-control'].selectedIndex].value  + arrMonth  + document.forms[0].elements['flight-finder-return-day-control'][document.forms[0].elements['flight-finder-return-day-control'].selectedIndex].value;


	//departure date
	document.forms[0].elements['date'].value = document.forms[0].elements['flight-finder-departure-year-control'][document.forms[0].elements['flight-finder-departure-year-control'].selectedIndex].value  +  dptMonth  + document.forms[0].elements['flight-finder-departure-day-control'][document.forms[0].elements['flight-finder-departure-day-control'].selectedIndex].value ;  
	
	//return date
	if (returnDate != '') 
	{
		document.forms[0].elements['retdate'].value = returnDate;
	}

	
	var region;
	//
	region = document.getElementById('region').value;

if (region == '') {
	region = partOfGrouping(document.forms[0].elements['dpt'][document.forms[0].elements['dpt'].selectedIndex].value);
}

	//deals with airport groups,  hardcoded currently
	if (region != '')
	{
		if (region == 'SCO') {
			document.forms[0].elements['arrAirport'].value = 'Glasgow';
			//document.forms[0].elements['dpt'].value = 'GLA';
			document.forms[0].elements['arrAirportGroup'].value = 'ABZ|Aberdeen,DND|Dundee,GLA|Glasgow,EDI|Edinburgh,PIK|Glasgow Prestwick,NCL|Newcastle';
		}
	
		if (region == 'NE') {
		
			document.forms[0].elements['arrAirport'].value = 'Manchester';
			//document.forms[0].elements['dpt'].value = 'MAN';
			document.forms[0].elements['arrAirportGroup'].value = 'MAN|Manchester,NCL|Newcastle,MME|Teeside,BLK|Blackpool,LBA|Leeds Bradford,LPL|Liverpool,DSA|Doncaster Sheffield,HUY|Humberside';
		}
	
		if (region == 'NI') {
			document.forms[0].elements['arrAirport'].value = 'Belfast';
		//	document.forms[0].elements['dpt'].value = 'BFS';
			document.forms[0].elements['arrAirportGroup'].value = 'BFS|Belfast,BHD|Belfast City,LDY|Derry';
		}
	
		if (region == 'CE') {
			document.forms[0].elements['arrAirport'].value = 'London Stansted';
		//	document.forms[0].elements['dpt'].value = 'STN';
			document.forms[0].elements['arrAirportGroup'].value = 'STN|Stansted,LTN|Luton,MAN|Manchester,EMA|East Midlands,CVT|Coventry,BHX|Birmingham,NWI|Norwich';
		}
	
		if (region == 'SW') {
			document.forms[0].elements['arrAirport'].value = 'Southamton';
		//	document.forms[0].elements['dpt'].value = 'SOU';
			document.forms[0].elements['arrAirportGroup'].value = 'SOU|Southampton,BRS|Bristol,CWL|Cardiff,EXE|Exeter,NQY|Newquay,PLH|Plymouth,LEQ|Lands End,PZE|Penzance,GCI|Guernsey,ACI|Alderney,JER|Jersey,BOH|Bournemouth';
		}
	
		if (region == 'SE') {
			document.forms[0].elements['arrAirport'].value = 'London Heathrow';
		//	document.forms[0].elements['dpt'].value = 'LHR';
			document.forms[0].elements['arrAirportGroup'].value = 'LHR|London Heathrow,STN|London Stansted,LGW|London Gatwick,LTN|London Luton,LCY|London City,SOU|Southampton,BOH|Bournemouth,BSH|Brighton,LYX|Lydd';
		}
	
	//	if (region == 'SE'){
		//	document.forms[0].elements['arrAirport'].value = 'London Heathrow';
		//	document.forms[0].elements['dpt'].value = 'LHR';
		//	document.forms[0].elements['arrAirportGroup'].value = 'LHR|London Heathrow,STN|London Stansted,LGW|London Gatwick,LTN|London Luton,LCY|London City';
	//	}
	}
	
	document.forms[0].elements['destCountry'].value = document.forms[0].elements['arvcountry'][document.forms[0].elements['arvcountry'].selectedIndex].text;
	document.forms[0].elements['destAirport'].value = document.forms[0].elements['arv'][document.forms[0].elements['arv'].selectedIndex].text;
	document.forms[0].elements['arrRegion'].value =  document.forms[0].elements['dptCounty'][document.forms[0].elements['dptCounty'].selectedIndex].text;
	document.forms[0].elements['arrAirport'].value =  document.forms[0].elements['dpt'][document.forms[0].elements['dpt'].selectedIndex].text;
	
	eraseCookie('dpt');
	eraseCookie('dptCountry');
	eraseCookie('arv');
	eraseCookie('arvCountry');
	eraseCookie('dptYear');
	eraseCookie('dptMonth');
	eraseCookie('dptDay');
	eraseCookie('rtnYear');
	eraseCookie('rtnMonth');
	eraseCookie('rtnDay');
	eraseCookie('directsonly');

	createCookie('dpt',document.forms[0].elements['dpt'][document.forms[0].elements['dpt'].selectedIndex].value,1);
	createCookie('dptCountry',document.forms[0].elements['dptCounty'][document.forms[0].elements['dptCounty'].selectedIndex].value,1);
	createCookie('arv',document.forms[0].elements['arv'][document.forms[0].elements['arv'].selectedIndex].value,1);
	createCookie('arvCountry',document.forms[0].elements['arvcountry'][document.forms[0].elements['arvcountry'].selectedIndex].value,1);
	createCookie('dptYear',document.forms[0].elements['flight-finder-departure-year-control'][document.forms[0].elements['flight-finder-departure-year-control'].selectedIndex].value,1);
	createCookie('dptMonth',document.forms[0].elements['flight-finder-departure-month-control'][document.forms[0].elements['flight-finder-departure-month-control'].selectedIndex].value,1);
	createCookie('dptDay',document.forms[0].elements['flight-finder-departure-day-control'][document.forms[0].elements['flight-finder-departure-day-control'].selectedIndex].value,1);
	createCookie('rtnYear',document.forms[0].elements['flight-finder-return-year-control'][document.forms[0].elements['flight-finder-return-year-control'].selectedIndex].value,1);
	createCookie('rtnMonth',document.forms[0].elements['flight-finder-return-month-control'][document.forms[0].elements['flight-finder-return-month-control'].selectedIndex].value,1);
	createCookie('rtnDay',document.forms[0].elements['flight-finder-return-day-control'][document.forms[0].elements['flight-finder-return-day-control'].selectedIndex].value,1);
	


	if (document.forms[0].elements['directsonly'].checked == true) 
	{
		createCookie('directsonly',1,1);
	}
	else
	{
		createCookie('directsonly',0,1);
	}


}

function populateDepartures(destinationAirports, countryCodeSelect)
{




	countryCode = countryCodeSelect[countryCodeSelect.selectedIndex].value;



	if (countryCode != '')
	{
		//frames['iframe'].location.href = '/jsincluder.htm?dpt=1&countrycode=' + countryCode;
		document.getElementById('iframe').src = '/jsincluder.htm?dpt=1&countrycode=' + countryCode;
		document.forms[0].elements['dptCounty'].disabled = true;
		document.forms[0].elements['dpt'].disabled = true;
	}



}

function SetSelectedValue(options, value)
{



if (value != '' && value != null) {

	for (var i=0; i< options.length; i++) {
			if (options[i].value == value ) {
				options[i].selected = true;
			}
	
		}
}
}


function populateDeparture(airportCode, region)
{


selectRegion = document.forms[0].elements['flight-finder-departure-country-control'];
selectDepartures = document.forms[0].elements['dpt'];


for (i=0; i <= selectRegion.options.length - 1; i++) {


 if (selectRegion[i].value == '84')
	{
		selectRegion[i].selected = 'selected';
	}

}

for (i=0; i <= selectDepartures.options.length - 1; i++) {


 if (selectDepartures[i].value == airportCode)
	{
		selectDepartures[i].selected = 'selected';
	}

}


return;

selectDepartures.options.length = 0;

for (i=0; i <= _region.length - 1; i++ ) {

	if (_region[i][0] == region) {
		for (j=0; j <= _region[i][1].length - 1; j++ ) {

			selectDepartures.options[j] = new Option(_region[i][1][j][1], _region[i][1][j][0]);
			

 
			if (_region[i][1][j][0] == airportCode) {
				
				selectDepartures[j].selected = 'selected' ;			
			}
		}
		
		
		
	}

}

}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}


function HandleBack()
{
	qs();

	erase = false;

//	if (qsParm['qs'] != null) 	{
	
//		if (qsParm['qs'].indexOf('back_e_1') != -1) {
	
			dpt = readCookie('dpt');
			dptCountry = readCookie('dptCountry');
			arv = readCookie('arv');
			arvCountry = readCookie('arvCountry');
			dptYear = readCookie('dptYear');
			dptMonth = readCookie('dptMonth');
			dptDay = readCookie('dptDay');
			rtnYear = readCookie('rtnYear');
			rtnMonth = readCookie('rtnMonth');
			rtnDay = readCookie('rtnDay');
			directsonly = readCookie('directsonly');
		
			if (rtnYear != '') {
				SetSelectedValue(document.forms[0].elements['flight-finder-return-year-control'].options, rtnYear);
				SetSelectedValue(document.forms[0].elements['flight-finder-return-month-control'].options, rtnMonth);
				SetSelectedValue(document.forms[0].elements['flight-finder-return-day-control'].options, rtnDay);
				}
			if (dptYear != '') {
				SetSelectedValue(document.forms[0].elements['flight-finder-departure-year-control'].options, dptYear);
				SetSelectedValue(document.forms[0].elements['flight-finder-departure-month-control'].options, dptMonth);
				SetSelectedValue(document.forms[0].elements['flight-finder-departure-day-control'].options, dptDay);
				}
			
			if (dptCountry != '') {
				SetSelectedValue(document.forms[0].elements['dptCounty'].options, dptCountry);
				populateDepartures(document.forms[0].elements['dpt'],document.forms[0].elements['dptCounty']);
				}
			if (arvCountry != '') {
				SetSelectedValue(document.forms[0].elements['arvcountry'].options, arvCountry);
				populateDestinations(document.forms[0].elements['arv'],document.forms[0].elements['arvcountry']);
				}
			if (directsonly == 0) {
				document.forms[0].elements['directsonly'].checked = false;
				} 
			else {
				document.forms[0].elements['directsonly'].checked = true;
				}
//			} 
//		else 
//			{
//				erase = true;
//			}  
//		}
//	else 
//	{ 
//		erase  = true; 
//	}

	if (erase == true)
	{
		eraseCookie('dpt');
		eraseCookie('dptCountry');
		eraseCookie('arv');
		eraseCookie('arvCountry');
		eraseCookie('dptYear');
		eraseCookie('dptMonth');
		eraseCookie('dptDay');
		eraseCookie('rtnYear');
		eraseCookie('rtnMonth');
		eraseCookie('rtnDay');
		eraseCookie('directsonly');	
	}

}


var qsParm = new Array();
function qs() {
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) {
   	var pos = parms[i].indexOf('=');
   	if (pos > 0) {
      var key = parms[i].substring(0,pos);
      var val = parms[i].substring(pos+1);
      qsParm[key] = val;
      }
   }
}