// JavaScript Document
$(document).ready(function() {
						   
	$.tabs("flights");
	
	$("#arrivalslist").mousedown(function() {
		PopulateDropdownBox("arrivalslist");
	});
	
	$("#departureslist").mousedown(function() {
		PopulateDropdownBox("departureslist");			
	});
	
});




function PopulateDropdownBox(type)
{
	
	$.ajax({
		   
		   type: "GET",
		   url: "/ajax/flights.asp?t=" + type,
		   dataType: "html",
		   async: false,
		   success: function(html) {
			   
			   var arrList = html.split("|").sort().reverse();
				arrList = fnUnique(arrList);
			   
			   $("#" + type).empty();
			   
			   $.each(arrList, function(i, val) {
					$("#" + type).prepend('<option value="' + val + '">' + val + '</option>');				 
				});
			   
			   $("#" + type).prepend('<option value="" selected="selected">Please select an airport</option>');
			   
			},
			error: function() { alert("Error"); }
	});

	$("#" + type).unbind("mousedown");
	
}



// Removes duplicates in the array 'a'
function fnUnique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!fnContains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

// Returns true if 's' is contained in the array 'a'
function fnContains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}