var countriesAjaxUrl = LANG_ROOT_PATH + 'AJAX/ContinentCountries.aspx';
var xmlCountries = GetXmlRequestObject();
var countriesDropDown = null;
var getElement = function (id) { return (document.all ? document.all[id] : document.getElementById(id)); }

// 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;
}

var Continents = {};
function AddCountry(countryName, countryCode, continent){
	var countries = Continents[continent];
	if(!countries)
		countries = Continents[continent] = [];
	
	countries.push({name:countryName, code:countryCode});
}

// make a request to the server
function fetchCountries(elemId, continent){
	if(xmlCountries && xmlCountries.readyState != 0)
		xmlCountries.abort();

	countriesDropDown = getElement(elemId);
	
	if(countriesDropDown){
		while(countriesDropDown.options.length > 0)
			countriesDropDown.remove(0);

		if(continent != null && continent.length < 2){
			countriesDropDown.options[0] = new Option(TXT_ANY_COUNTRY, '');
			setDisabled(countriesDropDown, true);
			updateList(countriesDropDown);
			return;
		}
	}

	var countries = Continents[continent];
	if(countriesDropDown && countries && countries.length > 0){
		renderCountryDropDown(countriesDropDown, countries);
	}
	else if(countriesDropDown && xmlCountries){
		countriesDropDown.options[0] = new Option(TXT_LOADING, '');
		updateList(countriesDropDown);
		setDisabled(countriesDropDown, true);

		var pg = countriesAjaxUrl;
		pg += '?continent=' + continent;
		pg += '&lang=' + LANG_CODE;
		
		xmlCountries.open('GET', pg, true);
		xmlCountries.onreadystatechange = function(){
			try{
				if(xmlCountries.readyState == 4){
					while(countriesDropDown.options.length > 0)
						countriesDropDown.remove(0);

					if(xmlCountries.responseText)
						eval(xmlCountries.responseText);
					
					countries = Continents[continent];
					renderCountryDropDown(countriesDropDown, countries);
				}
			}
			catch(ex){}
		};
		xmlCountries.send(null);
	}
}

function renderCountryDropDown(countriesDropDown, countries) {
	if(countries && countriesDropDown){
		countriesDropDown.options[0] = new Option(TXT_ANY_COUNTRY, '');
		for(var i = 0; i < countries.length; i++){
			var newOpt = new Option(countries[i].name, countries[i].code);
			countriesDropDown.options[countriesDropDown.options.length] = newOpt;
		}
	}
	
	updateList(countriesDropDown);
	if(countriesDropDown){ setDisabled(countriesDropDown, false); }
}

function abortFetchCountries(){
	if(xmlCountries && xmlCountries.readyState != 0){
		xmlCountries.abort();
		xmlCountries = null;
	}
}

function setDisabled(ctl, val){
	if(ctl){
		if(ctl.setDisabled){ ctl.setDisabled(val)}
		else if(ctl.disabled != null){ ctl.disabled = val; }
	}
}

function updateList(ctl){
	if(ctl && ctl.updateList){ ctl.updateList(); }
}

