var xmlDoc;
var depAirport;
function loadPackageXML(){
	// code for IE
	if (window.ActiveXObject){
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load("/includes/packageholidays_destinations.xml");
		getPackageDestinations();
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument) {
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load("/includes/packageholidays_destinations.xml");
		xmlDoc.onload=getPackageDestinations;
	}else{
		alert('Your browser cannot handle this script');
	}
}

//Clear List
function clearListPackage(listElem) {
	if(listElem!=0){
		while(listElem.options.length > 0) {
			listElem.options[0] = null;
		}
		while(listElem.hasChildNodes()) {
			listElem.removeChild(listElem.firstChild);
		}
		listElem.options[0] = new Option('- Loading destination -','');
	}
}


function getPackageDestinations(depairport) {
	
	
	sDestinationDDL = document.getElementById('packageLocationToPackages')
	sDestinationDDL.disabled = 'disabled';
	
	clearListPackage(sDestinationDDL);
	if(depairport!=undefined && depairport.length == 3) {
		depAirport = depairport;
	}
	
	if(typeof xmlDoc=='object') {
		Departure = xmlDoc.getElementsByTagName('Departure');
		for(x=0;x<Departure.length;x++) {
			if(Departure[x].attributes.getNamedItem("AirportCode").nodeValue == depAirport) {
				Destination = Departure[x].getElementsByTagName('Destination');

				for(y=0;y<Destination.length;y++) {
					sAirportCode = Destination[y].attributes.getNamedItem("AirportCode").nodeValue
					sAirportName = Destination[y].attributes.getNamedItem("AirportName").nodeValue
					sCountryName = Destination[y].attributes.getNamedItem("CountryName").nodeValue
					
					if(sPreCountry!=sCountryName){
						var CountryName = document.createElement('optgroup');
						CountryName.label = sCountryName;
						sDestinationDDL.appendChild(CountryName);
					}
					var sPreCountry = sCountryName
					sDestinationDDL.options[sDestinationDDL.options.length] = new Option(sAirportName, sAirportCode);
					
				}
			}
		}
		sDestinationDDL.options[0].innerHTML = '- Select your destination -';
		sDestinationDDL.disabled = '';
	}else{
		loadPackageXML();
	}
}

function getTourNames(intCountryCode){
	sDestinationDDL = document.getElementById('packageLocationTours')
	
	if(intCountryCode!= ''){
		sDestinationDDL.disabled = 'disabled';
		clearListPackage(sDestinationDDL);
		
		var sResponse = readFile('/includes/search/LookupTourOpXML.asp?searchtype=tour&location=' + intCountryCode);
		if(window.DOMParser) {
			var parser=new DOMParser();
			var XMLDocTemp=parser.parseFromString(sResponse,"text/xml");
		}else{
			var XMLDocTemp=new ActiveXObject("Microsoft.XMLDOM");
			XMLDocTemp.async='false';
			XMLDocTemp.loadXML(sResponse);
		}
	
		
		sDestinationDDL.options[0] = new Option("- All tours -","");		
		var oTours = XMLDocTemp.getElementsByTagName('Location');
		if(oTours.length > 0) {
			for(x=0;x<oTours.length;x++) {
				sDestinationDDL.options[sDestinationDDL.options.length] = new Option(oTours[x].attributes.getNamedItem("title").nodeValue,oTours[x].attributes.getNamedItem("code").nodeValue);
			}
		}
		sDestinationDDL.disabled = '';
	}else{
		sDestinationDDL.disabled = 'disabled';
		clearListPackage(sDestinationDDL);
		sDestinationDDL.options[0] = new Option("- Select a country -","");		
	}	
}


