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 clearListGeneric(listElem, sLoading) {
	if(listElem!=0){
		while(listElem.options.length > 0) {
			listElem.options[0] = null;
		}
		while(listElem.hasChildNodes()) {
			listElem.removeChild(listElem.firstChild);
		}
		listElem.options[0] = new Option(sLoading,'');
	}
}


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 disableTourForm(lLevel)
{
    if (lLevel == 1)
    {
        // Clear and disable Types
	    oTourTypesDDL = document.getElementById('tourtypeTours');
	    oTourTypesDDL.disabled = 'disabled';
	    clearListGeneric(oTourTypesDDL, '- Loading Types - ');
	    oTourTypesDDL.options[0] = new Option("- Select a country -","");
	    oTourTypesDDL.options[0].selected = 'selected';
	}
	
	if (lLevel == 1 || lLevel == 2)
	{
	    // Clear and disable Options
        oTourTypesDDL = document.getElementById('touroptionTours');		
	    oTourTypesDDL.disabled = 'disabled';
	    clearListGeneric(oTourTypesDDL, '- Loading Types - ');
	    oTourTypesDDL.options[0] = new Option("- Select a type -","");
	    oTourTypesDDL.options[0].selected = 'selected';
	}
	
	if (lLevel == 1 || lLevel == 2 || lLevel == 3)
	{
	    // Disable Methods
        oTransportMethod = document.getElementById('tourtransportmethodTours');
        oTransportMethod.disabled = 'disabled';
	}
	
	if (lLevel == 1 || lLevel == 2 || lLevel == 3 || lLevel == 4)
	{
	    oTransportMethod.options[0].selected = 'selected';
	    // Disable Airports
        oAirportFrom = document.getElementById('airportfromTours');
        oAirportFrom.disabled = 'disabled';
    }
	
	if (lLevel == 1 || lLevel == 2 || lLevel == 3 || lLevel == 4 || lLevel == 5)
	{
	    oAirportFrom.options[0].selected = 'selected';
	    // Disable Leave Dates
        oLeaveDate = document.getElementById('startdateTours');
        oLeaveDate.disabled = 'disabled';
	    oLeaveDate.options[0].selected = 'selected';
    }
}

function getTourTypes(intCountryCode, sFormID, parent){
    if (parent == 0)
    {
	    disableTourForm(1)
	    oTourTypesDDL = document.getElementById('touroptionTours');
		oTourTypesDDL.disabled = 'disabled';
	    oTourTypesDDL = document.getElementById('tourtypeTours');
	}
	else
	{
	    oTourTypesDDL = document.getElementById('touroptionTours');
	    disableTourForm(2)
	}
	
	if(intCountryCode!= '')
	{
		oTourTypesDDL.disabled = 'disabled';
		clearListGeneric(oTourTypesDDL, '- Loading Types - ');
		
		var sResponse = readFile('/includes/search/LookupTourOpXML.asp?searchtype=tourtype&location=' + intCountryCode + '&parent=' + parent);
		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);
		}
		
		oTourTypesDDL.options[0] = new Option("- Select a type -","");		
		var oTypes = XMLDocTemp.getElementsByTagName('Type');
		if(oTypes.length > 0) {
			for(x=0;x<oTypes.length;x++) {
				oTourTypesDDL.options[oTourTypesDDL.options.length] = new Option(oTypes[x].attributes.getNamedItem("title").nodeValue,oTypes[x].attributes.getNamedItem("code").nodeValue);
			}
		}
		oTourTypesDDL.disabled = '';
		
        if (parent != 0)
        {
		    oTourTypesDDL.options[0] = new Option("All Options","");
		    oTourTypesDDL.options[0].selected = 'selected';
		    oTransportMethod = document.getElementById('tourtransportmethodTours');
		    oTransportMethod.disabled = '';
		}
	}
}	

function changeTransportMethod(sMethod, sFormID){
    var oAirportFrom = document.getElementById('airportfromTours');
	var oLeaveDate = document.getElementById('startdateTours');
	oLeaveDate.disabled = '';
    if (sMethod == '')
    {
	    oLeaveDate.disabled = 'disabled';
    }
    if (sMethod == 'Flight')
    {
        oAirportFrom.disabled = '';
		oAirportFrom.options[0] = new Option("- Select an airport -","");
		oAirportFrom.options[0].selected = 'selected';
    }
    else
    {
		oAirportFrom.options[0] = new Option("- Select a method -","");
        oAirportFrom.disabled = 'disabled';	
		oAirportFrom.options[0].selected = 'selected';
    }
    
    // Populate available dates
    getTourDates(sMethod, sFormID);
}

function getTourNames(intCountryCode, sFormID){
	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 -","");		
	}	
	getTourDates(0, intCountryCode, sFormID)
}
function getTourDates(sMethod, sFormID){
	oCountry = document.getElementById('countryTours');
	if (oCountry != null) iCountry = oCountry.options[oCountry.selectedIndex].value;
	oType = document.getElementById('tourtypeTours');
	if (oType != null) iType = oType.options[oType.selectedIndex].value;
	oOption = document.getElementById('touroptionTours');
	if (oOption != null) iOption = oOption.options[oOption.selectedIndex].value;
	var sDivID = 'tdTourDates';
	//var sURL = '/includes/search/TourDates.asp?TourID=' + iTourID + '&FormID=' + sFormID + '&CountryID=' + intCountryCode;
	
	if (iOption == '') iCatID = iType; else iCatID = iOption;
	
	var sURL = '/includes/search/TourDates.asp?CountryID='  + iCountry + '&FormID=' + sFormID + '&CategoryID=' + iCatID + '&Method=' + sMethod;
	var oTourInfo = new TourAJAX(sDivID, sURL);
	oTourInfo.update(sDivID);
}


