var lists = new Array();
var d = document;

function validateForm() {
	
	var errorMsg = '';
	
	if (d.getElementById('airportfromgroup').value == '') {
		errorMsg += 'You must select a departure point.\n';
	}

	if (d.getElementById('airportfromgroup').value == '') {
		errorMsg += 'You must select a destination location.\n';
	}

	if (errorMsg == '') {
		return true;
	} else {
		alert('There have been errors on your form submission:\n\n' + errorMsg);
		return false;
	}

}

function init() {


	// Departures
	lists['london'] = new Array();
	lists['north'] = new Array();
	lists['midlands'] = new Array();
	lists['scotland'] = new Array();
	lists['west'] = new Array();
	lists['ireland'] = new Array();

	lists['london'][0] = new Array('Gatwick','Luton','Stansted','Heathrow');
	lists['london'][1] = new Array('LGW','LTN','STN','LHR');

	lists['north'][0] = new Array('Blackpool','Doncaster','Humberside','Leeds/Bradford','Liverpool','Manchester','Newcastle','Teeside');
	lists['north'][1] = new Array('BLK','DSA','HUY','LBA','LPL','MAN','NCL','MME');

	lists['midlands'][0] = new Array('Birmingham','Bournemouth','East-Midlands','Manchester','Norwich');
	lists['midlands'][1] = new Array('BHX','BOH','EMA','MAN','NWI');

	lists['scotland'][0] = new Array('Glasgow','Edinburgh','Aberdeen','Prestwick');
	lists['scotland'][1] = new Array('GLA','EDI','ABZ','PIK');

	lists['west'][0] = new Array('Cardiff','Bristol','Exeter','Bournemouth','Southampton');
	lists['west'][1] = new Array('CWL','BRS','EXT','BOH','SOU');

	lists['ireland'][0] = new Array('Belfast','Belfast City','City of Derry','Cork','Dublin','Knock','Shannon');
	lists['ireland'][1] = new Array('BFS','BHD','LDY','ORK','DUB','NOC','SNN');

	// Destinations
	lists['balearics'] = new Array();
	lists['canaries'] = new Array();
	lists['mainlandspain'] = new Array();
	lists['greece'] = new Array();
	lists['turkey'] = new Array();
	lists['portugal'] = new Array();
	lists['usa'] = new Array();
	lists['caribbean'] = new Array();
	lists['european'] = new Array();
	lists['australia'] = new Array();
	lists['africa'] = new Array();

	lists['canaries'][0] = new Array('Tenerife','La Palma','Lanzarote','Gran Canaria','Fuerteventura');
	lists['canaries'][1] = new Array('TFS','SPC','ACE','LPA','FUE');

	lists['mainlandspain'][0] = new Array('Costa Del Sol','Costa Blanca','Costa Dorada','Barcelona','Costa Brava','Almeria','San Javier','Valencia');
	lists['mainlandspain'][1] = new Array('AGP','ALC','REU','BCN','GRO','LEI','MJV','VLC');

	lists['turkey'][0] = new Array('Bodrum','Dalaman','Antalya','Izmir','Istanbul');
	lists['turkey'][1] = new Array('BJV','DLM','AYT','ADB','IST');

	lists['greece'][0] = new Array('Larnaca (Cyprus)','Paphos (Cyprus)','Kerkyra, Corfu (Greece)','Kos (Greece)','Zakynthos/Zante (Greece)','Rhodos (Greece)','Skiathos (Greece)','Mykonos (Greece)','Heraklion, Crete (Greece)','Kefalonia (Greece)','Khania (Greece)','Athens','Santorini, Thira (Greece)','Kalamata (Greece)','Kavala (Greece)','Mitilini (Greece)','Preveza/Lefkas (Greece)','Saloniki (Greece)','Samos (Greece)','Volos (Greece)');
	lists['greece'][1] = new Array('LCA','PFO','CFU','KGS','ZTH','RHO','JSI','JMK','HER','EFL','CHQ','ATH','JTR','KLX','KVA','MJT','PVK','SKG','SMI','VOL');

	lists['africa'][0] = new Array('Tunisia (Monastir)','Tunisia (Tunis)','Sfax (Tunisia)','Luxor (Egypt)','Sharm El Sheikh','Hurghada (Egypt)','Gambia','Mombassa (Kenya)','Morocco (Agadir)','Goa (India)','Sri Lanka','Nairobi (Kenya)');
	lists['africa'][1] = new Array('MIR','TUN','SFA','LXR','SSH','HRG','BJL','MBA','AGA','GOI','CMB','NBO');

	lists['usa'][0] = new Array('Florida','Orlando','Miami','Los Angeles','Las Vegas','Honolulu','Vancouver (Canada)','Calgary (Canada)','Toronto (Canada)','Ottawa (Canada)','Halifax (Canada)','Montreal (Canada)','Edmonton (Canada)','San Francisco');
	lists['usa'][1] = new Array('SFB','MCO','MIA','LAX','LAS','HNL','YVR','YYC','YYZ','YOW','YHZ','YUL','YEG','SFO');

	lists['caribbean'][0] = new Array('Nassau (Bahamas)','Freeport, Grand Bahama','Barbados','Montego Bay','Antigua','St Kitts','Tobago','Grenada','St Lucia','Puerto Plata (Dominican Republic)','La Romana','Charlotte Amalie, St. Thomas','La Habana (Havana)','Ciego de Avila','Varadero (Cuba)','Holguin (Cuba)','Acapulco','Cancun','Puerto Vallarta (Mexico)','Margerita (Venezuela)','Natal','Recife','Salvador');
	lists['caribbean'][1] = new Array('NAS','FPO','BGI','MBJ','ANU','SKB','TAB','GND','UVF','POP','LRM','STT','HAV','AVI','VRA','HOG','ACA','CUN','PVR','PMV','NAT','REC','SSA');

	lists['balearics'][0] = new Array('Ibiza','Majorca','Menorca');
	lists['balearics'][1] = new Array('IBZ','PMI','MAH');

	lists['portugal'][0] = new Array('Algarve','Oporto (Portugal)','Lisbon','Madeira');
	lists['portugal'][1] = new Array('FAO','OPO','LIS','FNC');

	lists['european'][0] = new Array('Rome','Milan','Amalfi Coast (Naples)','Pisa','Rimini','Turin','Venice','Verona, Italy','Catania (Sicily)','Cagliari (Sardinia)','Bologna','Bergamo','Malta','Nice','Toulouse (France)','Lyon (France)','Grenoble','Geneva (Switzerland)','Salzburg (Austria)','Innsbruck (Austria)','Bourgas (Bulgaria)','Varna (Bulgaria)','Pula','Praha (Prague)','Kittila','Rovaniemi','Frankfurt','Leipzig','Krakow','Ljubljana (Slovenia)','Zurich','Alghero, Sardinia','Ajaccio, Corsica','Brindisi','Bastia, Corsica','Calvi, Corsica (France)','Figari, Corsica','Gibraltar','Dubrovnik (Croatia)');
	lists['european'][1] = new Array('FCO','MXP','NAP','PSA','RMI','TRN','VCE','VRN','CTA','CAG','BLQ','BGY','MLA','NCE','TLS','LYS','GNB','GVA','SZG','INN','BOJ','VAR','PUY','PRG','KTT','RVN','FRA','LEJ','KRK','LJU','ZRH','AHO','AJA','BDS','BIA','CLY','FSC','GIB','DBV');

	lists['australia'][0] = new Array('Bangkok','Phuket','Maldives','Mauritius','Gasmata Island');
	lists['australia'][1] = new Array('BKK','HKT','MLE','MRU','GMI');

}

function emptyList( box ) {
	var size = box.options.length;
	for (i = size; i >= 1; i--) {
		box.options[i] = null;
	}
}

function fillList( box, arr, selecteditem ) {
	for ( i = 0; i < arr[0].length; i++ ) {
		option = new Option( arr[0][i], arr[1][i] );
		box.options[box.length] = option;

		if (arr[1][i] == selecteditem) {
			box.options[box.length - 1].selected = true;
		}
	}
}

function updatedepartures( box, selecteditem ) {
	if (box.value == "") {
		d.getElementById("airportfrom").disabled = true;
		d.getElementById("airportfrom").value = "";
	} else {
		list = lists[box.options[box.selectedIndex].value];
		emptyList(d.getElementById("airportfrom"));
		fillList(d.getElementById("airportfrom"), list, selecteditem);
		d.getElementById("airportfrom").disabled = false;
	}
}

function updatedestinations( box, selecteditem ) {
	if (box.value == "") {
		d.getElementById("airportto").disabled = true;
		d.getElementById("airportto").value = "";
	} else {
		list = lists[box.options[box.selectedIndex].value];
		emptyList(d.getElementById("airportto") );
		fillList(d.getElementById("airportto"), list, selecteditem );
		d.getElementById("airportto").disabled = false;
	}
}

window.onload = function() {
	init();
	d.getElementById('airportfromgroup').onchange = function() { updatedepartures(d.getElementById("airportfromgroup")) };
 	d.getElementById('airporttogroup').onchange = function() { updatedestinations(d.getElementById("airporttogroup")) };
	d.getElementById('frm_late-deals').onsubmit = function() { return validateForm(); }
	updatedepartures(d.getElementById("airportfromgroup"));
	updatedestinations(d.getElementById("airporttogroup"));
}
