var targetUrl = LANG_ROOT_PATH + 'AJAX/CountryAirports.aspx';
var xmlApts = GetXmlRequestObject();
var targetDropDown = null;

// gets a reference to the xml request object
function GetXmlRequestObject(){
	var obj = null;
	if(window.XMLHttpRequest){
		obj = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		try{ obj = new ActiveXObject('Msxml2.XMLHTTP'); }
		catch(e){
			try{ obj = new ActiveXObject('Microsoft.XMLHTTP'); }
			catch(oc){ obj = null; }
		}
	}
	return obj;
}

function AddCountryAirport(name, iata)
{
	if(targetDropDown)
	{
		var newOpt = new Option(name, iata);
		targetDropDown.options[targetDropDown.options.length] = newOpt;
		targetDropDown.disabled = false;
	}
}

// make a request to the server
function FetchAirports(elemId, country){
	if(xmlApts && xmlApts.readyState != 0)
		xmlApts.abort();

	targetDropDown = GetItemById(elemId);

	if(targetDropDown && xmlApts)
	{
		while(targetDropDown.options.length > 0)
			targetDropDown.remove(0);

		targetDropDown.options[0] = new Option(LOADING_TEXT, '');
		targetDropDown.disabled = true;

		var pg = targetUrl;
		pg += '?country=' + encodeURIComponent(country);
		
		xmlApts.open('GET', pg, true);
		xmlApts.onreadystatechange = function()
		{
			try
			{
				if(xmlApts.readyState == 4)
				{
					while(targetDropDown.options.length > 0)
						targetDropDown.remove(0);

					try{ targetDropDown.options[0] = new Option(TXT_SELECT_AIRPORT, ''); } catch(ex){}
					
					if(xmlApts.responseText)
						eval(xmlApts.responseText);
					
					if(targetDropDown)
						targetDropDown.disabled = false;
				}
			}
			catch(ex){}
		};
		xmlApts.send(null);
	}
}

function GetItemById(id){
	return (document.all ? document.all[id] : document.getElementById(id));
}
