// ------------------------------------------------------------------
// Geography objects
// ------------------------------------------------------------------
function departureAirportObj(asCode, asName) {
	this.Code = asCode;
	this.Name = asName;
}

function destinationAirportObj(asDepartCode, asArriveId, asName) {
	this.DepartCode = asDepartCode;
	this.Id = asArriveId;
	this.Name = asName;
}

function carrierObj(asDepartCode, asArriveId, asName) {
	this.Code = asDepartCode;
	this.ArriveId = asArriveId;
	this.Name = asName;
}

var goSelectedCountry;

// ------------------------------------------------------------------
// Geography build functions
// ------------------------------------------------------------------
function first(country) {
	if (country == 'FR') {
		goSelectedCountry = France;
	} else if(country == 'IT') {
		goSelectedCountry = Italy;
	} else if(country == 'PT') {
		goSelectedCountry = Portugal;
	} else if(country == 'ES') {
		goSelectedCountry = Spain;
	} else if(country == 'HR') {
		goSelectedCountry = Croatia;
	} else if(country == 'NL' ) {
		goSelectedCountry = Holland;
	}
	drawDepAirports();
	clearFlightChoice();
}

function clearFlightChoice() {
	var $destinationAirport = $('#destAirport');
	$destinationAirport.empty();
	$("<option value=''>-- Select a destination airport --</option>").appendTo($destinationAirport);

	$('#airlinesDisplay').html('');
}

function drawDepAirports() {
	var $depAirport = $('#depAirport');
	$depAirport.empty();
	$("<option value=''>-- Select a departure airport --</option>").appendTo($depAirport);

	for (var i=0; i < goSelectedCountry.depArray.length; i++) {
		$("<option value='" + goSelectedCountry.depArray[i].Code + "'>" + goSelectedCountry.depArray[i].Name + "</option>").appendTo($depAirport);
	}
}

function getAirlines() {
	var $destinationAirport = $('#destAirport');
	var $departureAirport = $('#depAirport');
	$('#airlinesDisplay').html('');

	if ($departureAirport.val() == "") {
		alert('Please select a departure point');
		return false;
	}

	if ($destinationAirport.val() == "") {
		alert('Please select your destination');
		return false;
	}

	for (var i=0; i < goSelectedCountry.airlinesArray.length; i++) {
		if ((goSelectedCountry.airlinesArray[i].Code==$departureAirport.val())&& 
			(goSelectedCountry.airlinesArray[i].ArriveId == $destinationAirport.val())) {

			$('<dd>'+goSelectedCountry.airlinesArray[i].Name+'</dd>').appendTo('#airlinesDisplay');
		}
	}

	if ($('#airlinesDisplay dd').length == 0) {
		$('<dt>Sorry, no airlines found</dt>').prependTo('#airlinesDisplay');
	} else {
		$('<dt>Airlines flying to destination</dt>').prependTo('#airlinesDisplay');
	}
}

// ------------------------------------------------------------------
// Attach 'onChange' events
// ------------------------------------------------------------------
$(function() {
	$('#depAirport').change(function() {
		var $destinationAirport = $('#destAirport');
		var lsDepartureAirport = $('#depAirport').val();

		$destinationAirport.empty();
		$("<option value=''>-- Select a destination airport --</option>").appendTo($destinationAirport);

		for (var i=0; i<goSelectedCountry.destArray.length; i++) {
			if (goSelectedCountry.destArray[i].DepartCode == lsDepartureAirport) {
				$("<option value='"+goSelectedCountry.destArray[i].Id+"'>"+goSelectedCountry.destArray[i].Name+"</option>").appendTo($destinationAirport);
			}
		}
	});
});