// The xml in which everything is stored
var mainXml;

// URL to ajax service
var geoDataURL = "/SAS_Templates/Ajax/GeoData.aspx" ;

// Temp store when clicking on a city in tree, since city list load async
var cityToShowInDropDown= "";

// Cache with cities retrieved from server. 
// key: country code, value <AirportList countryCode="SE"><airport><code>ARN</code><name>Arlanda</name></airport></AirportList>
var cityListsCache = Array();
var cityListsCacheSASForm = Array();
var PCcityListsCache = Array();

function GEO_init(inUse) {

	if 	(getXmlTextBox() != null)
	{
		getOriginalDataInput().value = getXmlTextBox().value;
		if (inUse)  {
			GEO_updateTree();
		} else {
			getGeoDataTreePanel().innerHTML = "N/A";
			getCountryDropDownList().disabled = true;
			getRemoveAllButton().disabled = true;
			getAddAllButton().disabled = true;
			//getXmlTextBox().disabled = true;
		}
	}
}

/////////////////////////////////////////////////////
//
// XML Tree Events
//
/////////////////////////////////////////////////////

function expandCountryClicked(){
	try {
		// Send the <LI> element to the expand function
		expand(event.srcElement.parentNode);
	} catch(e) {
		showMessage("An error occured, could not expand country (" + e.description + ")", 2000);
		throw(e);
	}	
}

function collapseCountryClicked(){
	
	try{
		
		// Send the <LI> element to the collapse function
		collapse(event.srcElement.parentNode);
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}

function countryNameClick() {
	try{
		
		cityToShowInDropDown = null;
		setCountryInDropDown(event.srcElement.parentNode.id);
		highlightCountry(event.srcElement.parentNode);
		highlightCity("");
		updateAddRemoveButtons();
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}


function cityNameClick() {
	
	try{
		var cityNode = window.event.srcElement.parentNode;
		var countryCode = window.event.srcElement.parentNode.parentNode.parentNode.id;
		
		highlightCity(cityNode, countryCode);
		
		if (countryCode != getCountryDropDownList().value) {
			// Is used from loadCityList which is used from setCountryInDropDown.
			cityToShowInDropDown = cityNode.id;
			setCountryInDropDown(countryCode);
		} else {
			setCityInDropDown(cityNode.id);
		}
		
		updateAddRemoveButtons();
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}	

///////////////////////////////////////////////////
//
// Edit Area Events
//
//////////////////////////////////////////////////

function regionCountryListChanged() {
	try{
		// Empty selected city
		cityToShowInDropDown = "";
		
		var countryCode = event.srcElement.value;
		loadCityList(countryCode, getCityDropDownList());
		// Update country list
		highlightCountry(countryCode);
		highlightCity("");
		
		
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}

function cityListChanged() {
	try{
		var cityCode = event.srcElement.value;
		
		highlightCity(cityCode, getCountryDropDownList().value);
		
		updateAddRemoveButtons();
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}



function addCityClicked() {
	try{
		var cityCode = getCityDropDownList().value;
		var countryCode = getCountryDropDownList().value;
		var citiesToAdd = Array();
		
		// All cities selected
		if (cityCode.length > 0) {
			citiesToAdd.push(cityCode);
		} else {
			var childs = getCityDropDownList().childNodes;
			for (var i = 1; i < childs.length; i++) {
				if (!hasCity(countryCode, childs[i].value)) {
					citiesToAdd.push(childs[i].value);
				}
			}
		}
		
		
		addCity(countryCode, citiesToAdd);
		
		// Will send xml to server to sort it and then reload it.
		sortMainXML();
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
}


// Removes from XML AND from HTML. Instead of going
// to server. No need for sorting. A bit more work in this code,
// but much faster on client + no reload of tree.
function removeClicked() {

	try{
		var cityCode = getCityDropDownList().value;
		var countryCode = getCountryDropDownList().value;
		var citiesToRemove = Array();

		var countries = mainXml.getElementsByTagName("country");
		var countryNode;
		// Find country
	
		for(var i = 0; i < countries.length; i++) {
			if (countries[i].getAttribute("id") == countryCode) {
				countryNode = countries[i];
				break;
			}
		}
	
		if (countryNode == null) {
			showMessage("An error occured", 5000);
			return;
		}
		
		if (countryNode.childNodes.length == 1) {
			// remove all
			cityCode = "";
		}
	
		// Find the country <LI id="SE"> 
		var htmlCountryNode;
		var childs = getGeoDataTreePanel().firstChild.childNodes;
		for (var i in childs) {
			if (childs[i].id == countryCode) {
				htmlCountryNode = childs[i];
				break;
			}
		}
		
		// All cities selected
		if (cityCode.length == 0) {
			mainXml.documentElement.removeChild(countryNode);
			// remove from list manually
			htmlCountryNode.parentNode.removeChild(htmlCountryNode);
			try{
				getGeoDataTreePanel().firstChild.lastChild.lastChild.className= "cityListLast";
			} catch(e){
				//Might be the last in list
			}
			
			// Update preferred countries
			for (var listCount = 1; listCount < 5; listCount++) {
				removeValueFromList(countryCode, getPreferredCountyDropDownList(listCount));
			}
			
			removeValueFromList(countryCode, getPreselectCountryOriginDropDownList());
			removeValueFromList(countryCode, getPreselectCountryDestinationDropDownList());
			removeValueFromList(countryCode, getPreferredCityCountyDropDownList());
			
		} else {
			var childs = countryNode.childNodes;
			for (var i = 0; i < childs.length ; i++) {
				if (childs[i].getAttribute("id") == cityCode) {
					countryNode.removeChild(childs[i]);
					
					// Remove from preferred cities
					if (getPreferredCityCountyDropDownList().value == countryCode) {
						for (var j = 1; j < 5; j++) {
							removeValueFromList(cityCode, getPreferredCityDropDownList(j));	
						}
					}
					
					break;
				}
			}
			
			var cities = htmlCountryNode.lastChild.childNodes;
			for (var i in cities) {
				if (cities[i].id == cityCode) {
					htmlCountryNode.lastChild.removeChild(cities[i]);
					break;
				}
			}
			// change image src on last
			htmlCountryNode.lastChild.lastChild.firstChild.src = "/images/ExplorerTree/L.gif";
		}
		
		updateAddRemoveButtons();
		getXmlTextBox().value = mainXml.xml;
		
		updatePreselectBoxes();
		
		
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
}

function removeValueFromList(value, ddl) {
	
	for (var i = 0; i < ddl.options.length; i++) {
		if (ddl.options[i].value == value) {
			if (ddl.options[i].selected) {
				ddl.selectedIndex = 0;
			}
			ddl.removeChild(ddl.options[i]);
		}
	}
	
	ddl.fireEvent("onchange");
}


function removeAllClicked() {
	if(confirm("Really delete all?")) {
		getXmlTextBox().value = '<?xml version="1.0"?><countries psCountryDest="" psCityDest="" psCountryOrg="" psCityOrg="" />';
		GEO_updateTree();
		//getPreselectCountryOriginDropDownList().selectedIndex = 0;
		//getPreselectCountryDestinationDropDownList().selectedIndex = 0;
		//updatePreselectBoxes();
		
	}

}

function addAllGeoDataClicked() {
	
	if (!confirm("Really add all geo data from database?")) {
		return;
	}
	
	showPleaseWait();
	var xml_doc = new ActiveXObject("MSXML2.DOMDOCUMENT.3.0");
	xml_doc.async = false;
	xml_doc.loadXML('<Request><action>AddAllGeoData</action><xmlDoc/></Request>');
	xml_doc.selectSingleNode("Request/xmlDoc").appendChild(xml_doc.createTextNode(mainXml.xml));
	
	var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	xmlHttp.open("POST", geoDataURL, true);
	xmlHttp.setRequestHeader("Cache-control","no-store");
	xmlHttp.send(xml_doc);
	
	xmlHttp.onreadystatechange = function() {
		var readyState = xmlHttp.readyState;
		if (readyState != 4) {
			return;
		}
		
		try{
			
			var xml_doc = xmlHttp.responseXML; 
			
			
			if (checkAndHandleErrorInformation(xml_doc)) {
				return;
			}
			
			getXmlTextBox().value = xml_doc.xml;
			GEO_updateTree();
			
			hidePleaseWait();
		}
		catch(e) {
			showMessage("An error occured (" + e.description + ")", 2000);
			throw(e);
		}
	
	}
}

function undoChangesClicked() {

	if (!confirm("Really undo all changes since last saved?")) {
		return;
	}
	
	 getXmlTextBox().value = getOriginalDataInput().value;
	 GEO_updateTree();

}

function setMainCityClicked() {
	try{
		var countryCode = getCountryDropDownList().value;
		var cityCode = getCityDropDownList().value;
		
		mainCities[countryCode] = cityCode;
		
		// Change main xml
		var countryNode = mainXml.selectSingleNode("/countries/country[@id='" + countryCode + "']");
		countryNode.setAttribute("mainCity", cityCode);
		getXmlTextBox().value = mainXml.xml;
		
		// Change tree
		var cities = highlightedCountryNode.lastChild.childNodes;
		for (var i = 0; i< cities.length; i++) {
			cities[i].lastChild.lastChild.style.display = cities[i].id == cityCode ? "inline" : "none";
		}
		
		updatePreselectBoxes();
		
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
	
}

function noMainCityClicked() {
	
	try{
		var countryCode = getCountryDropDownList().value;
		
		mainCities[countryCode] = null;
		
	
		// Change main xml
		var countryNode = mainXml.selectSingleNode("/countries/country[@id='" + countryCode + "']");
		countryNode.setAttribute("mainCity", "");
		getXmlTextBox().value = mainXml.xml;
		
		// Change tree
		var cities = highlightedCountryNode.lastChild.childNodes;
		for (var i = 0; i< cities.length; i++) {
			cities[i].lastChild.lastChild.style.display =  "none";
		}
		
		// Update the boxes
		updatePreselectBoxes();
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
	
}


function preselectCountryDestinationChanged() {
	try{
		var ddl = getPreselectCountryDestinationDropDownList();
		var countryCode = ddl.value;
		
		// countryCode may be empty string
		if (mainXml.documentElement.getAttribute("psCountryDest") != countryCode)  {
			mainXml.documentElement.setAttribute("psCountryDest", countryCode);	
			mainXml.documentElement.setAttribute("psCityDest", "");
			getXmlTextBox().value = mainXml.xml;
		}
		
		
		if (countryCode.length == 0) {
			emptyPreselectCities(getPreselectCityDestinationDropDownList());
			return;
		}
		
		var selectedCity = mainXml.documentElement.getAttribute("psCityDest");
		
		loadCityListFromTree(countryCode, getPreselectCityDestinationDropDownList(), selectedCity);
	
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
	
}

function preselectCountryOriginChanged() {
	
	
	try{
		var ddl = getPreselectCountryOriginDropDownList();
		var countryCode = ddl.value;
		
		// countryCode may be empty string
		if (mainXml.documentElement.getAttribute("psCountryOrg") != countryCode)  {
			mainXml.documentElement.setAttribute("psCountryOrg", countryCode);	
			mainXml.documentElement.setAttribute("psCityOrg", "");
			getXmlTextBox().value = mainXml.xml;
		}
		
		
		if (countryCode.length == 0) {
			emptyPreselectCities(getPreselectCityOriginDropDownList());
			return;
		}
		
		var selectedCity = mainXml.documentElement.getAttribute("psCityOrg");
		
		loadCityListFromTree(countryCode, getPreselectCityOriginDropDownList(), selectedCity);
	
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
	
}

function preselectCityDestinationChanged() {
	
	try{	
		
		mainXml.documentElement.setAttribute("psCityDest", getPreselectCityDestinationDropDownList().value);	
		getXmlTextBox().value = mainXml.xml;
		if (getPreselectCountryDestinationDropDownList().selectedIndex == 0) {
			getPreselectCityDestinationDropDownList().disabled = true;
		}
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
}

function preselectCityOriginChanged() {
	try{
		mainXml.documentElement.setAttribute("psCityOrg", getPreselectCityOriginDropDownList().value);	
		getXmlTextBox().value = mainXml.xml;
		
		if (getPreselectCountryOriginDropDownList().selectedIndex == 0) {
			getPreselectCityOriginDropDownList().disabled = true;
		}
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
}


function preferredCountryChanged() {
	
	try{
		// Check what number / ordinal
		var id = window.event.srcElement.id;
		var ord = id.charAt(id.length - 1);
		
		var countryCode = event.srcElement.value;
		if (countryCode.length == 0) {
			var countryNode = mainXml.selectSingleNode("/countries/country[@ord='" + ord + "']")
			if (countryNode != null) {
				countryNode.setAttribute("perf", "0");
				countryNode.setAttribute("ord", "0");
			}
		} else {
			
			// Remove any other that had that ord
			var oldCountryNode = mainXml.selectSingleNode("/countries/country[@ord='" + ord + "']")
			if (oldCountryNode != null) {
				oldCountryNode.setAttribute("perf", "0");
				oldCountryNode.setAttribute("ord", "0");
			}
			
			var countryNode = mainXml.selectSingleNode("/countries/country[@id='" + countryCode + "']")
			countryNode.setAttribute("perf", "1");
			countryNode.setAttribute("ord", ord);
			
			// Check that no other drop down has that country
			for (var prefCountryCount = 1; prefCountryCount < 5; prefCountryCount++) {
				
				var ddl = getPreferredCountyDropDownList(prefCountryCount);
				
				if (ddl != event.srcElement && ddl.value == countryCode) {
					ddl.selectedIndex = 0;
				}
			}
			
		}
		
		getXmlTextBox().value = mainXml.xml;
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}
	
}


function preferredCityCountryChanged() {
	 
	 var countryCode = event.srcElement.value;
	 
	 for (var i = 1; i < 5; i++) {
		if (event.srcElement.selectedIndex == 0)  {
			getPreferredCityDropDownList(i).selectedIndex = 0;
			getPreferredCityDropDownList(i).fireEvent("onchange");
		} else {
			var selectedCity = "";
			// Find the node with that ordinal
			var node = mainXml.selectSingleNode("/countries/country[@id='" + countryCode + "']/city[@ord='" + i + "']");
			if (node != null) {
				selectedCity = node.getAttribute("id");
			}
			loadCityListFromTree(event.srcElement.value, getPreferredCityDropDownList(i), selectedCity);
		}
	}

}

function preferredCityChanged() {

	if (getPreferredCityCountyDropDownList().selectedIndex == 0) {
		event.srcElement.selectedIndex = 0;
		event.srcElement.disabled= true;
		return;
	}
	
	var countryCode = getPreferredCityCountyDropDownList().value;
	var cityCode = event.srcElement.value;
	
	// find ordinal
	var id = window.event.srcElement.id;
	var ord = id.charAt(id.length - 1);
	
	if (cityCode.length > 0) {
		var xpath = "/countries/country[@id='" + countryCode + "']/city[@id='" + cityCode + "']";
		var cityNode = mainXml.selectSingleNode(xpath);
		if (cityNode != null) {
			
			// Remove any other that had that ord
			var oldCityNode = mainXml.selectSingleNode("/countries/country[@id='" + countryCode + "']/city[@ord='" + ord + "']");
			if (oldCityNode != null) {
				oldCityNode.setAttribute("perf", "0");
				oldCityNode.setAttribute("ord", "0");
			}
			
			cityNode.setAttribute("perf", "1");
			cityNode.setAttribute("ord", ord);
			
			// Check that no other drop down has that city
			for (var prefCityCount = 1; prefCityCount < 5; prefCityCount++) {
				var ddl = getPreferredCityDropDownList(prefCityCount);
				if (ddl != event.srcElement && ddl.value == cityCode) {
					ddl.selectedIndex = 0;
				}
			}
			
		}
	} else {
		var node = mainXml.selectSingleNode("/countries/country[@id='" + countryCode + "']/city[@ord='" + ord + "']");
		if (node != null) {
			node.setAttribute("perf", "0");
			node.setAttribute("ord", "0");
		}
		
	}
	
	getXmlTextBox().value = mainXml.xml;
	

}
			
///////////////////////////////////////////////////
//
// Edit Area Methods
//
//////////////////////////////////////////////////		

function addCity(countryCode, citiesToAdd) {
	// Add city and country to xml
	var countries = mainXml.getElementsByTagName("country");
	var countryNode;
	// Find country
	
	for(var i = 0; i < countries.length; i++) {
		if (countries[i].getAttribute("id") == countryCode) {
			countryNode = countries[i];
			break;
		}
	}
	
	if (countryNode == null) {
		countryNode = mainXml.documentElement.appendChild(mainXml.createElement("country"));
		countryNode.setAttribute("id", countryCode);
		countryNode.setAttribute("perf", "0");
		countryNode.setAttribute("ord", "0");
		countryNode.setAttribute("name", countryNames[countryCode]);
		if (mainCities[countryCode] != null) {
			countryNode.setAttribute("mainCity", mainCities[countryCode]);
		}
	}
	
	for (var i in citiesToAdd) {
		var cityCode = citiesToAdd[i];
		
		if (hasCity(countryCode, cityCode)) {
			continue;
		}
		
		var cityNode = countryNode.appendChild(mainXml.createElement("city"));
		cityNode.setAttribute("id", cityCode);
		cityNode.setAttribute("perf", "0");
		cityNode.setAttribute("ord", "0");
		cityNode.setAttribute("name", cityNames[cityCode]);	
	}
	
}

function hasCity(countryCode, cityCode) {	
	
	if(cityCode == null || cityCode.length == 0) {
		return hasCountry(countryCode);
	}
	return mainXml.selectSingleNode
				("/countries/country[@id='" + countryCode + "']/city[@id='" + cityCode + "']") 
				!= null;
}

function hasCountry(countryCode) {	
	
	if(countryCode == null || countryCode.length == 0) {
		return true;
	}
	
	return mainXml.selectSingleNode
				("/countries/country[@id='" + countryCode + "']") 
				!= null;
}

function updateAddRemoveButtons() {
	if (getCityDropDownList().childNodes.length > 0 && getCityDropDownList().selectedIndex == 0) {
		getAddCityButton().innerText = "Add cities"
		getRemoveButton().innerText = "Remove cities"
	} else {
		getAddCityButton().innerText = "Add city"
		getRemoveButton().innerText = "Remove city"
	}
	
	var countryCode = getCountryDropDownList().value;
	var cityCode = getCityDropDownList().value;
	if (countryCode == null || countryCode.length == 0) {
		getRemoveButton().disabled = true;
		getAddCityButton().disabled = true;
	}
	else if (hasCity(countryCode, cityCode)) {
		getRemoveButton().disabled = false;
		getAddCityButton().disabled = cityCode.length != 0;
	} else {
		getRemoveButton().disabled = true;
		getAddCityButton().disabled = false;
	}
	
	
	if ( ( cityCode.length > 0 && mainCities[countryCode] == cityCode) 
		 || cityCode.length == 0
		 || !hasCity(countryCode, cityCode)) {
		getSetMainCityButton().disabled = true;
	} else {
		getSetMainCityButton().disabled = false;
	}
	
	
	// No main city
	getNoMainCityButton().disabled = mainCities[countryCode] == null || !hasCity(countryCode, cityCode);
	
}

////////////////////////////SASFormCitySelector2.ascx/////////////////////////////////////////
/* 
 * Loads a list of cities into the drop down list.
 * Loads list from server if not present in cache.
 * 
 * Default is async call to server, use sync = true if a synchronized
 * call is desired.
*/
function loadCityListSASForm(countryCode, dropDownList,selectCityText,language, sync) {
	
	
	try{	
		var succedFunction = function() {
				emptyList(dropDownList);
				populateCityListSASForm(dropDownList, cityListsCacheSASForm[countryCode],selectCityText);
					
				dropDownList.disabled = false;
				
				if (dropDownList == getCityDropDownList()) {
					// Will not work if done directly, seems that list is not completely loaded
					setTimeout("setCityInDropDown('" + cityToShowInDropDown + "'); updateAddRemoveButtons()", 50);
				}
				
		}
		
		var failFunction = function() {
				emptyList(dropDownList);
				
		}
		
		// Check cache
		var cityXML = cityListsCacheSASForm[countryCode];
		if (cityXML != null) {
			succedFunction.call(this);
			return;
		}
		
		sendCityListRequestSASForm(countryCode, succedFunction, failFunction, sync);
		
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}
// Loads city list into the cache and calls the callbackFunction
function sendCityListRequestSASForm(countryCode, callbackFunction, failFunction,language, sync) {
	
	showPleaseWait();
	
	/*var xml_doc = new ActiveXObject("MSXML2.DOMDOCUMENT.3.0");
	xml_doc.async = false;
	xml_doc.loadXML('<Request><action>GetSASFormCitiesForCountry</action><countryCode>' 
					+ countryCode + '</countryCode><language>' + language +'</language></Request>');

	var myXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	myXmlHttp.open("POST", geoDataURL, !sync);
	myXmlHttp.setRequestHeader("Cache-control","no-store");
	myXmlHttp.send(xml_doc);*/
	var xml_doc;
	var myXmlHttp
	if (window.ActiveXObject)
	{
		xml_doc = new ActiveXObject("Microsoft.XMLDOM");
		xml_doc.async = false;
		xml_doc.loadXML('<Request><action>GetSASFormCitiesForCountry</action><countryCode>' 
					+ countryCode + '</countryCode><language>' + language +'</language></Request>');		
		myXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		myXmlHttp.open("POST", geoDataURL, !sync);
		myXmlHttp.setRequestHeader("Cache-control","no-store");
		myXmlHttp.send(xml_doc);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xml_doc = document.implementation.createDocument("","",null);
		var xmlText = "<Request><action>GetSASFormCitiesForCountry</action><countryCode>";
		xmlText = xmlText +  countryCode;
		xmlText = xmlText + "</countryCode><language>";
		xmlText = xmlText + language;
		xmlText = xmlText + "</language></Request>";
		var parser = new DOMParser();		
		var doc = parser.parseFromString(xmlText,"text/xml");	
			
		myXmlHttp = new XMLHttpRequest();
		myXmlHttp.open("POST", geoDataURL, true);
		myXmlHttp.setRequestHeader("Cache-control","no-store");
		myXmlHttp.send(doc);
	}
	
	//xmlHttp.onreadystatechange = callbackFunction;
	myXmlHttp.onreadystatechange = function callback() {
		var readyState = myXmlHttp.readyState;
		if (readyState != 4) {
			return;
		}
			
		try{
			var xml_doc = myXmlHttp.responseXML; 
			
			if (checkAndHandleErrorInformation(xml_doc)) {
				if (failFunction != null) {
					failFunction.call(this);
				}
				return;
			}
			
			cityListsCacheSASForm[countryCode] = xml_doc;
			var cities = xml_doc.getElementsByTagName("city");
			//alert(cities.length);	
			//This condition is to ensure that the response consists of cities for the selected country		
			if ((cities.length == 0) && (countryCode.length == 2))	{			
				sendCityListRequestSASForm(countryCode, callbackFunction, failFunction,language, sync);
			}
			// Call the function that was passed as reference
			callbackFunction.call(this);
			
			hidePleaseWait();
			
		} catch(e) {
			showMessage("An error occured (" + e.description + ")", 2000);
			throw(e);
		}
	};
	
	if(sync) 
		callback();
}

// Populates any select list from xml doc 
function populateCityListSASForm(list, xml_doc,selectCityText) {

	emptyList(list);

	/*var cities = xml_doc.getElementsByTagName("city");
	
	//var countryCode = xml_doc.documentElement.getAttribute("countryCode");
	
	var optionNode = list.appendChild(document.createElement("option"));
	
	if (cities.length == 0){
		optionNode.innerText = selectCityText;
		list.disabled = true;
	} else {
		optionNode.innerText = selectCityText;
	}
	
	for (var i = 0; i < cities.length; i++) {
		var cityCode = cities[i].getElementsByTagName("code")[0].firstChild.nodeValue;
		var cityName = cities[i].getElementsByTagName("name")[0].firstChild.nodeValue;	
		
		var optionNode = document.createElement("option");
		optionNode.value = cityCode;
		optionNode.innerText = cityName;
				
		list.appendChild(optionNode);
	}*/
	var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);
	
	var cities = xml_doc.getElementsByTagName("city");
		
	if(isIE)
	{
		var optionNode = list.appendChild(document.createElement("option"));
		if (cities.length == 0){
			optionNode.innerText = selectCityText;
			list.disabled = true;
		} 
		else {
			optionNode.innerText = selectCityText;
		}
	}
	else
	{
		//var optionNode = document.createTextNode("option");
		var optionNode = document.createElement("option");
		if (cities.length == 0){			
			optionNode.textContent = selectCityText;
			list.disabled = true;
		} 
		else {					
			optionNode.textContent = selectCityText;
		}
	}
	list.appendChild(optionNode);	
	for (var i = 0; i < cities.length; i++) {
		var cityCode = cities[i].getElementsByTagName("code")[0].firstChild.nodeValue;
		var cityName = cities[i].getElementsByTagName("name")[0].firstChild.nodeValue;	
				
		if(isIE)
		{
			var optionNode = document.createElement("option");
			optionNode.value = cityCode;
			optionNode.innerText  = cityName;
		}
		else
		{
			var optionNode = document.createElement("option");
			optionNode.value = cityCode;
			optionNode.textContent  = cityName;
		}
		list.appendChild(optionNode);		
	}
	
}
////////////////////////////SASFormCitySelector2.ascx/////////////////////////////////////////


/* 
 * Loads a list of cities into the drop down list.
 * Loads list from server if not present in cache.
 * 
 * Default is async call to server, use sync = true if a synchronized
 * call is desired.
*/
function loadCityList(countryCode, dropDownList, sync) {
	
	
	try{
	
		var succedFunction = function() {
				emptyList(dropDownList);
				populateCityList(dropDownList, cityListsCache[countryCode]);
					
				dropDownList.disabled = false;
				
				if (dropDownList == getCityDropDownList()) {
					// Will not work if done directly, seems that list is not completely loaded
					setTimeout("setCityInDropDown('" + cityToShowInDropDown + "'); updateAddRemoveButtons()", 50);
				}
				
		}
		
		var failFunction = function() {
				emptyList(dropDownList);
				
		}
		
		// Check cache
		var cityXML = cityListsCache[countryCode];
		if (cityXML != null) {
			succedFunction.call(this);
			return;
		}
		
		sendCityListRequest(countryCode, succedFunction, failFunction, sync);
		
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}

/* 
 * Loads a list of cities into the drop down list.
 * Loads list from server if not present in cache.
 * 
 * Default is async call to server, use sync = true if a synchronized
 * call is desired.
*/
function loadPCCityList(countryCode, dropDownList, sync) {
	
	
	try{
	
		var succedFunction = function() {
				emptyList(dropDownList);
				populateCityList(dropDownList, PCcityListsCache[countryCode]);
					
				dropDownList.disabled = false;
				
				if (dropDownList == getCityDropDownList()) {
					// Will not work if done directly, seems that list is not completely loaded
					setTimeout("setCityInDropDown('" + cityToShowInDropDown + "'); updateAddRemoveButtons()", 50);
				}		
		}
		
		var failFunction = function() {
				emptyList(dropDownList);
				
		}
		
		// Check cache
		var cityXML = PCcityListsCache[countryCode];
		if (cityXML != null) {
			succedFunction.call(this);
			return;
		}
		
		sendPCCityListRequest(countryCode, succedFunction, failFunction, sync);
		
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		throw(e);
	}	
}

// Loads city list into the cache and calls the callbackFunction
function sendCityListRequest(countryCode, callbackFunction, failFunction, sync) {
	
	showPleaseWait();
	
	var xml_doc = new ActiveXObject("MSXML2.DOMDOCUMENT.3.0");
	xml_doc.async = false;
	xml_doc.loadXML('<Request><action>GetCitiesForCountry</action><countryCode>' 
					+ countryCode + '</countryCode></Request>');

	var myXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	myXmlHttp.open("POST", geoDataURL, !sync);
	myXmlHttp.setRequestHeader("Cache-control","no-store");
	myXmlHttp.send(xml_doc);
	
	//xmlHttp.onreadystatechange = callbackFunction;
	myXmlHttp.onreadystatechange = function callback() {
		var readyState = myXmlHttp.readyState;
		if (readyState != 4) {
			return;
		}
			
		try{
			var xml_doc = myXmlHttp.responseXML; 
			
			if (checkAndHandleErrorInformation(xml_doc)) {
				if (failFunction != null) {
					failFunction.call(this);
				}
				return;
			}
			
			cityListsCache[countryCode] = xml_doc;
			
			// Call the function that was passed as reference
			callbackFunction.call(this);
			
			hidePleaseWait();
			
		} catch(e) {
			showMessage("An error occured (" + e.description + ")", 2000);
			throw(e);
		}
	};
	
	if(sync) 
		callback();
}

function sendPCCityListRequest(countryCode, callbackFunction, failFunction, sync) {
	
	showPleaseWait();
	
	var xml_doc = new ActiveXObject("MSXML2.DOMDOCUMENT.3.0");
	xml_doc.async = false;
	xml_doc.loadXML('<Request><action>GetPCCitiesForCountry</action><countryCode>' 
					+ countryCode + '</countryCode></Request>');

	var myXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	myXmlHttp.open("POST", geoDataURL, !sync);
	myXmlHttp.setRequestHeader("Cache-control","no-store");
	myXmlHttp.send(xml_doc);
	
	//xmlHttp.onreadystatechange = callbackFunction;
	myXmlHttp.onreadystatechange = function callback() {
		var readyState = myXmlHttp.readyState;
		if (readyState != 4) {
			return;
		}
			
		try{
			var xml_doc = myXmlHttp.responseXML; 
			
			if (checkAndHandleErrorInformation(xml_doc)) {
				if (failFunction != null) {
					failFunction.call(this);
				}
				return;
			}
			
			PCcityListsCache[countryCode] = xml_doc;
			
			// Call the function that was passed as reference
			callbackFunction.call(this);
			
			hidePleaseWait();
			
		} catch(e) {
			showMessage("An error occured (" + e.description + ")", 2000);
			throw(e);
		}
	};
	
	if(sync) 
		callback();

}

// Populates any select list from xml doc 
function populateCityList(list, xml_doc) {

	emptyList(list);

	var airports = xml_doc.getElementsByTagName("airport");
	
	var countryCode = xml_doc.documentElement.getAttribute("countryCode");
	
	var optionNode = list.appendChild(document.createElement("option"));
	
	if (airports.length == 0){
		optionNode.innerText = "(No cities found)";
		list.disabled = true;
	} else {
		optionNode.innerText = "- Select all cities -";
	}
	
	for (var i = 0; i < airports.length; i++) {
		var airportCode = airports[i].getElementsByTagName("code")[0].firstChild.nodeValue;
		var airportName = airports[i].getElementsByTagName("name")[0].firstChild.nodeValue;	
		
		var optionNode = document.createElement("option");
		optionNode.value = airportCode;
		optionNode.innerText = airportName;
		if(    airports[i].getAttribute("mainCity") != null
			&& airports[i].getAttribute("mainCity") == "true") {
			optionNode.innerText += " *";
		} 
		
		list.appendChild(optionNode);
	}
	
}

// Shakes the preselect boxes, so any error color appears
function updatePreselectBoxes() {
	getPreselectCountryDestinationDropDownList().fireEvent("onchange");
	getPreselectCountryOriginDropDownList().fireEvent("onchange");
}

function emptyPreselectCities(ddl) {
	
	emptyList(ddl, "(Select country)");
	ddl.fireEvent("onchange");
	
}

function emptyList(ddl, firstOptionText) {
	ddl.innerHTML = "";
	if (firstOptionText != null) {
		var firstOptionNode = ddl.appendChild(document.createElement("option"));
		firstOptionNode.innerText = firstOptionText;
	}
}


// Updates the combo boxes from the mainXML
function appendPreferredCountriesOption(countryNode) {
	
	// Append in drop down lists
	for (var prefCountryCount = 1; prefCountryCount < 5; prefCountryCount++) {
		
		var optionNode = appendCountryListOption(countryNode, getPreferredCountyDropDownList(prefCountryCount));
		
		if (	countryNode.getAttribute("perf") == "1" 
				&& Number(countryNode.getAttribute("ord")) == prefCountryCount) {
			optionNode.selected = true;
		}
	}
}


function appendCountryListOption(xmlCountryNode, ddl) {
	
	var countryCode = xmlCountryNode.getAttribute("id");
	var countryName = xmlCountryNode.getAttribute("name");
	
	// if first
	if (ddl.childNodes.length == 0) {
		var firstOptionNode = ddl.appendChild(document.createElement("option"));
		firstOptionNode.innerText = "-- Select a country --";
	}
	
	var optionNode = ddl.appendChild(document.createElement("option"));
	optionNode.value = countryCode;
	optionNode.innerText = countryName;
	
	return optionNode;
	
}

function loadCityListFromTree(countryCode, ddl, selectedCity) {
	
	emptyList(ddl);
	selectedCity = selectedCity == null ? "" : selectedCity;
	
	var cities = mainXml.selectNodes("/countries/country[@id='" + countryCode + "']/city");
	
	var optionNode = ddl.appendChild(document.createElement("option"));
	optionNode.innerText = "-- Select a city --";
	
	for (var i = 0; i < cities.length; i++) {
		
		var cityCode = cities[i].getAttribute("id");
		var optionNode = appendCityListOption(cities[i], ddl, mainCities[countryCode]);

		if (selectedCity == cityCode) {
			optionNode.selected = true;
		}
		
	}
	ddl.disabled = false;	
}

function appendCityListOption(xmlCityNode,ddl, mainCity) {
	mainCity = mainCity == null ? "" : mainCity;
	
	var cityCode = xmlCityNode.getAttribute("id");
	var cityName = xmlCityNode.getAttribute("name");
	
	if (mainCity == cityCode) {
		cityName += " *";
	}
	
	var optionNode = ddl.appendChild(document.createElement("option"));
	optionNode.value = cityCode;
	optionNode.innerText = cityName;
	
	return optionNode;
}



/////////////////////////////////////////////////////
//
// Tree manipulation methods
//
/////////////////////////////////////////////////////

// countryCode = the country code, ex SV
function expandCountry(countryCode) {
	
	if (countryCode == null || countryCode.length == 0) {
		showMessage("An error occured, could not expand country", 2000);
		return;
	}
	
	// Get all <LI> items. Their ID is the country code
	var countryListItems = getGeoDataTreePanel().firstChild.childNodes;
	for (var i = 0; i < countryListItems.length; i++) {
		if (countryListItems[i].id == countryCode) {
			expand(countryListItems[i]);
			break;
		}
	}
	
}

// countryListItem = the <LI> item for that country
function expand(countryListItem){

	// Hide the first image (the +)
	countryListItem.firstChild.style.display = "none";
	// Show the second image (the -)
	countryListItem.firstChild.nextSibling.style.display = "inline";
	
	// Show the country
	countryListItem.lastChild.style.display = "block";
}

// countryListItem = the <LI> item for that country
function collapse(countryListItem){

	// Hide the first image (the +)
	countryListItem.firstChild.style.display = "inline";
	// Show the second image (the -)
	countryListItem.firstChild.nextSibling.style.display = "none";
	
	// Show the country
	countryListItem.lastChild.style.display = "none";
}


//The <LI> for that country that is highlighted. To avoid tree search
var highlightedCountryNode = null;
var highlightedCityNode = null;
// Highlights the country in the list.
// Returns true if it finds the country
function highlightCountry(countryNodeOrCode) {
	
	var panel = getGeoDataTreePanel();
	
	if (panel.firstChild == null) {
		// No countries filled in
		return false;
	}
	
	var countryNode;
	
	if (highlightedCountryNode != null) {
		highlightedCountryNode.childNodes[2].style.fontWeight = "normal";
		highlightedCountryNode = null;
	}
	
	// Check if this is not the <LI> but a country code
	if (typeof(countryNodeOrCode) != 'object' &&
		typeof(countryNodeOrCode)  == 'string') {
	
		countryNode  = findCountryNodeInTree(countryNodeOrCode);
	} else {
		countryNode  = countryNodeOrCode;
	}
	
	if (countryNode != null) {
		countryNode.childNodes[2].style.fontWeight = "bold";
		expand(countryNode);
		highlightedCountryNode = countryNode;
		
		if (window.event == null || !countryNode.contains(window.event.srcElement))  {
			if (countryNode.lastChild.childNodes.length < 24) {
				countryNode.lastChild.lastChild.scrollIntoView(false);
			}else {
				countryNode.lastChild.childNodes[23].scrollIntoView(false);		
			}
		}
		
		return true;
	}
	return false;
}

// Highlights the city in the list. Also highlights the country
// and expands the city list.
// Returns true if it finds the city
function highlightCity(cityNodeOrCode, countryCode) {

	
	var panel = getGeoDataTreePanel();
	if (panel.firstChild == null) {
		// No countries filled in
		return false;
	}
	
	// Deleselct previous
	if (highlightedCityNode != null) {
		highlightedCityNode.lastChild.style.fontWeight = "normal";
		highlightedCityNode = null;
	}
	
	if (cityNodeOrCode == null || cityNodeOrCode.length == 0) {
		return false;
	}
	
	var cityNode;
	
	
	// Check if this is not the <LI> but a country code
	if (typeof(cityNodeOrCode) != 'object' &&
		typeof(cityNodeOrCode)  == 'string') {
		
		// Search for the node
		var countryNode = findCountryNodeInTree(countryCode);
		if (countryNode != null) {
			var cityNodes = countryNode.lastChild.childNodes;
			for (var j = 0; j < cityNodes.length; j++) {
				if (cityNodes[j].id == cityNodeOrCode) {
					cityNode = cityNodes[j];
					break;
				}
			}
		}
	} else {
		cityNode  = cityNodeOrCode;
	}
	
	if (cityNode != null) {
		highlightedCityNode = cityNode;
		cityNode.lastChild.style.fontWeight = "bold";
		var countryNode = cityNode.parentNode.parentNode;
		highlightCountry(countryNode);
		return true;
	}
	
	return false;
}

function findCountryNodeInTree(countryCode) {

	var countryNodes = getGeoDataTreePanel().firstChild.childNodes;
	
	for (var i = 0; i < countryNodes.length; i++) {
		if(countryNodes[i].id == countryCode) {
			return countryNodes[i];
		} 
	}
	return null;
}

function loadMainXML(){
	mainXml = new ActiveXObject("MSXML2.DOMDOCUMENT.3.0");
	mainXml.async = false;
	mainXml.loadXML(getXmlTextBox().value);
}

function GEO_updateTree() {
	
	try{
		
		getGeoDataTreePanel().innerHTML = "";
		highlightedCountryNode = null;
		highlightedCityNode = null;
	
		loadMainXML();
		
		var xmlCountries = mainXml.documentElement.getElementsByTagName("country");
		
		var countryList = document.createElement("ul");
		countryList.className = "countryList";
		
		
		for (var i = 1; i < 5; i++) {
			emptyList(getPreferredCountyDropDownList(i), "-- Select a country --");
			getPreferredCityDropDownList(i).selectedIndex = 0;
			getPreferredCityDropDownList(i).disabled = true;
		}
		emptyList(getPreferredCityCountyDropDownList(), "-- Select a country --");
		
		emptyList(getPreselectCountryOriginDropDownList(), "-- Select a country --");
		emptyList(getPreselectCountryDestinationDropDownList(), "-- Select a country --");
		
		
		for (var countryCount = 0; countryCount < xmlCountries.length; countryCount++)
		{
			
			var countryNode = xmlCountries.item(countryCount);
			var countryName;
			
			var xmlCountryCode = countryNode.getAttribute("id");
			
			if(countryNames[xmlCountryCode] == null ) {
				countryName = xmlCountryCode;
			}
			else {
				countryName = countryNames[xmlCountryCode];
			}

			// container for the country
			country = document.createElement("li");
			country.id = xmlCountryCode;
			
			var imgCollapsed = document.createElement("img");
			imgCollapsed.src = "/images/ExplorerTree/Lplus.gif";
			imgCollapsed.align = "absmiddle";
			imgCollapsed.border = "0";
			imgCollapsed.onclick =  expandCountryClicked;
			imgCollapsed.alt = "Expand"
			
			var imgExpanded = document.createElement("img");
			imgExpanded.src = "/images/ExplorerTree/Lminus.gif";
			imgExpanded.align = "absmiddle";
			imgExpanded.border = "0";
			imgExpanded.style.display = "none";
			imgExpanded.onclick =  collapseCountryClicked;
			imgExpanded.alt = "Collapse"
			
			var countryLink = document.createElement("a");
			countryLink.innerText = countryName;
			countryLink.alt = countryName;
			countryLink.onclick = countryNameClick;
			countryLink.style.cursor = "hand";
			
			country.appendChild(imgCollapsed);
			country.appendChild(imgExpanded)
			country.appendChild(countryLink);
			
			var citiesUL = createCityList(countryNode);
			
			citiesUL.className = "cityList";
				
			if(countryCount == xmlCountries.length -1) {
				citiesUL.className += "Last";	
			}
			
			country.appendChild(citiesUL);
		
			countryList.appendChild(country);
			
			// Preselected origin
			var optionNode = appendCountryListOption(countryNode, getPreselectCountryOriginDropDownList());
			if (mainXml.documentElement.getAttribute("psCountryOrg") == xmlCountryCode) {
				optionNode.selected = true;
			}
			
			// Preselected destination
			var optionNode = appendCountryListOption(countryNode, getPreselectCountryDestinationDropDownList());
			if (mainXml.documentElement.getAttribute("psCountryDest") == xmlCountryCode) {
				optionNode.selected = true;
			}
			
			// Preferred country drop downs
			appendPreferredCountriesOption(countryNode);
			
			// Preferred city drop downs (the country choice list)
			appendCountryListOption(countryNode, getPreferredCityCountyDropDownList());
			
		}
		
		
		getGeoDataTreePanel().appendChild(countryList)
		
		// Exand nodes according to the select boxes
		var selectedCity = getCityDropDownList().value;
		var selectedCountry = getCountryDropDownList().value;
		
		if (selectedCity != null && selectedCity.length > 0) {
			if (!highlightCity(selectedCity, selectedCountry)) {
				highlightCountry(selectedCountry);
			}
		}
		else if (selectedCountry != null && selectedCountry.length > 0) {
			highlightCountry(selectedCountry);
		}
		
		updateAddRemoveButtons();
		// Update preselected
		updatePreselectBoxes();
		
		
	} catch(e) {
		getGeoDataTreePanel().innerText = "An error occured when updating tree";
		throw(e);
	}
}

// Creates the city UL in the tree
function createCityList(countryNode) {
	
	var citiesUL = document.createElement("UL");
			
	var cities = countryNode.getElementsByTagName("city");
	
	var xmlCountryCode = countryNode.getAttribute("id");
	
	var mainCity = "";
			
	if(countryNode.getAttribute("mainCity") != "")
	{
		mainCity = countryNode.getAttribute("mainCity");
	}
	
	// Set main city in cache. Otherwise, when loading cities in drop down, they will
	// get the default main city.
	mainCities[xmlCountryCode] = mainCity;
	
	for (var cityCount = 0; cityCount < cities.length; cityCount++) {
				
		var cityNode = cities.item(cityCount);
		
		var imgT = document.createElement("img");
		imgT.align = "absmiddle";
		
		if (cityCount >= cities.length - 1) {
		    imgT.src = "/images/ExplorerTree/L.gif";
		}
		else {
		    imgT.src = "/images/ExplorerTree/T.gif";
		}

		var cityName;
		var xmlCityCode = cityNode.getAttribute("id");
		
		
		if(cityNames[xmlCityCode] == null ) {
			cityName = xmlCityCode;
		}
		else {
			cityName = cityNames[xmlCityCode];
		}
				
		var cityLink = document.createElement("a");
		cityLink.innerText = cityName;
		cityLink.onclick = cityNameClick;
		cityLink.style.cursor = "hand";
			
		cityLink.alt = cityName;
		
		var mainCitySpan = cityLink.appendChild(document.createElement("span"));
		mainCitySpan.innerText = " *";
		mainCitySpan.style.display = xmlCityCode == mainCity ? "inline" : "none";
		
		
		var cityLI = citiesUL.appendChild(document.createElement("li"));
		cityLI.id = xmlCityCode;
		cityLI.appendChild(imgT);
		cityLI.appendChild(cityLink);
	}
	
	return citiesUL;
}


/////////////////////////////////////////////////////
//
// Misc utility methods
//
/////////////////////////////////////////////////////

function sortMainXML(callback) {
	
	showPleaseWait();
	try{
		//Empty city list
		
		xml_doc = new ActiveXObject("MSXML2.DOMDOCUMENT.3.0");
		xml_doc.async = false;
		xml_doc.loadXML('<Request><action>SortXML</action><xmlDoc/></Request>');
		xml_doc.selectSingleNode("Request/xmlDoc").appendChild(xml_doc.createTextNode(mainXml.xml));
		
		var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		
		xmlHttp.open("POST", geoDataURL, true);
		xmlHttp.setRequestHeader("Cache-control","no-store");
		xmlHttp.send(xml_doc);
		
		xmlHttp.onreadystatechange = function() {
	
			if (xmlHttp.readyState != 4) {
				return;
			}
			
			try{
				
				var xml_doc = xmlHttp.responseXML; 
				
				if (checkAndHandleErrorInformation(xml_doc)) {
					return;
				}
				
				getXmlTextBox().value = xml_doc.xml;
				GEO_updateTree();
				hidePleaseWait();
				if(callback) 
					callback.call(this)
			}
			catch(e) {
				showMessage("An error occured (" + e.description + ")", 2000);
				getGeoDataTreePanel().innerText = "An error occured when updating tree";
				throw(e);
			}

		}
		
	} catch(e) {
		showMessage("An error occured (" + e.description + ")", 2000);
		getGeoDataTreePanel().innerText = "An error occured when updating tree";
		throw(e);
	}
}


function showPleaseWait() {
	// will show hourglass cursor...
	getContentPanel().className = "waiting";
	//showMessage("Please wait...");
}

function hidePleaseWait() {
	getContentPanel().className = "";
	//hideMessage();
}

function showMessage(msg, hideTimeout) {
	var panel = getMessagePanel();
	if (panel != null) {
		panel.innerText = msg;
		panel.className = "ajaxMessageActive";
		if (hideTimeout != null) {
			setTimeout("hideMessage()", hideTimeout);
		}
	}
}

function hideMessage() {
	var panel = getMessagePanel();
	if (panel != null) {
		panel.innerText = "";
		panel.className = "ajaxMessagePassive";
	}
}


function setCountryInDropDown(countryCode) {
	
	loadCityList(countryCode, getCityDropDownList());
	
	var dropDown = getCountryDropDownList();
	for (var i = 0; i < dropDown.options.length; i++) {
		if(dropDown.options[i].value == countryCode){
			dropDown.selectedIndex = i;
			break;
		}
	}
}

function setCityInDropDown(cityCode) {
	
	var dropDown = getCityDropDownList();
	if (cityCode.length == 0) {
		dropDown.selectedIndex = 0;
		return false;
	}
	
	for (var i = 0; i < dropDown.options.length; i++) {
		if (dropDown.options[i].value == cityCode) {
			//dropDown.options[i].selected = true;
			dropDown.selectedIndex = i;
			return true;
		}
	}
	
	return false;
	
}

function checkAndHandleErrorInformation(xml_doc) {
	if (xml_doc == null) {
		return false;
	}
	
	if (xml_doc.documentElement == null) {
		return false;
	}
	
	if (xml_doc.documentElement.nodeName == "ErrorInformation") {
		showMessage(xml_doc.documentElement.firstChild.nodeValue, 5000);
		return true;
	}
}


function showTreeHTML()				{ return getHTMLInputField().value = getGeoDataTreePanel().innerHTML; }
function getGeoDataTreePanel()		{ return document.getElementById("GEO_treeView"); }
function getContentPanel()			{ return document.getElementById("GEO_content"); }

function getMessagePanel()			{ return document.getElementById("GEO_messagePanel");}
function getCityDropDownList()		{ return document.getElementById("GEO_cityAiportSelect");}
function getCountryDropDownList()	{ return document.getElementById("GEO_regionCountrySelect");}
function getAddCityButton()			{ return document.getElementById("GEO_addCityButton");}
function getRemoveButton()			{ return document.getElementById("GEO_removeButton");}
function getRemoveAllButton()		{ return document.getElementById("GEO_removeAllButton");}
function getAddAllButton()			{ return document.getElementById("GEO_addAllButton");}
function getSetMainCityButton()		{ return document.getElementById("GEO_setMainCityButton");}
function getNoMainCityButton()		{ return document.getElementById("GEO_noMainCityButton");}
function getPreselectCountryDestinationDropDownList()	{ return document.getElementById("GEO_preselectCountryDestination");}
function getPreselectCityDestinationDropDownList()		{ return document.getElementById("GEO_preselectCityDestination");}
function getPreselectCountryOriginDropDownList()		{ return document.getElementById("GEO_preselectCountryOrigin");}
function getPreselectCityOriginDropDownList()			{ return document.getElementById("GEO_preselectCityOrigin");}
function getPreferredCountyDropDownList(number)			{ return document.getElementById("GEO_preferredCountry_" + number);}
function getPreferredCityCountyDropDownList()			{ return document.getElementById("GEO_preferredCityCountryList");}
function getPreferredCityDropDownList(number)			{ return document.getElementById("GEO_preferredCity_" + number);}
function getOriginalDataInput(number)					{ return document.getElementById("GEO_originalData");}
