function displayDeparture(xmldoc){
	
  var departureCity = document.getElementById("current_location").value;
	
  var xmlNode, gatewaysNode, gatewayNode, departureNode, displayText, nodeIndex, selectDeparture;
  
  selectDeparture = document.getElementById("departureList");
  
  xmlNode = xmldoc.documentElement;

  gatewaysNode = xmlNode.firstChild;

  for(nodeIndex = 0; nodeIndex < gatewaysNode.childNodes.length; nodeIndex++){

    gatewayNode = gatewaysNode.childNodes[nodeIndex];

    departureNode = gatewayNode.firstChild.nextSibling;

	departureCode = gatewayNode.firstChild;
	
	//alert(departureCode.data);
	
	if (gatewayNode.firstChild.value != ""){
		
	departureIndex = nodeIndex + 1;  

    selectDeparture.options[departureIndex] = new Option(departureNode.firstChild.data, departureCode.firstChild.data);
	
	setmatch(document.searchForm.departureList.options, departureCity);
	
	}
	
	/*  
	  if (document.searchForm.departureList.options[departureIndex].value == departureCity){
		  
        document.searchForm.departureList.options[departureIndex].selected = true;
	 
	 }*/     

  }

}

function displayDestination(xmldoc){

  selectDestination = document.getElementById("destinationList");

  selectDestination.options[0] = new Option("Select a Destination");

  document.forms['searchForm'].destinationList.options.length = 1;
  
  var departureForm = document.forms["searchForm"]["departureList"];
  
  var selectedDeparture, nodeIndex, xmlNode, gatewaysNode, gatewayNode, groupsNode, codeNode, nameNode, destinationsNode, destinationsGroupsNode, destinationCount, destinationGroupsCount, groupCount, destinationsNode; 
  
  selectedDeparture = departureForm[departureForm.selectedIndex].value;
  
  nodeIndex = parseInt(selectedDeparture);
  
  nodeIndex = nodeIndex - 1;
  
  // This section filters down the XML node into the working sections

  xmlNode = xmldoc.documentElement;
  
  gatewaysNode = xmlNode.firstChild;

  gatewayNode = gatewaysNode.firstChild;
    
  destinationsGroupsNode = gatewayNode.lastChild; // this is the last tree node before the actual data can be pulled for departures.
  
  //alert("blah" + destinationsGroupsNode.childNodes.length);
  
  var destinationGroupsCount = 0;
  
  var selectIndex = 0;
  
  for(destinationGroupsCount = 0; destinationGroupsCount < destinationsGroupsNode.childNodes.length; destinationGroupsCount++){

    groupNode = destinationsGroupsNode.childNodes[destinationGroupsCount];
	
	groupCode = groupNode.childNodes[0];
	
	groupName = groupNode.childNodes[1];
	
	groupCount = groupNode.childNodes.length;
	
	//alert ("this is the group count" + groupCount);
	
	//alert(groupName.firstChild.data);
	// insert style code here!
	
	var str=groupName.firstChild.data;
    if(str.search(/All/) < 0){

	  groupNameValue = "All " + groupName.firstChild.data;
	  selectDestination.options[selectIndex] = new Option(groupNameValue, groupCode.firstChild.data);
	  //selectDestination.options[selectIndex].class = "titledrop";
	  
	}else{
	
	  selectDestination.options[selectIndex] = new Option(groupName.firstChild.data, groupCode.firstChild.data);
	  
	}
	
	
	if (selectIndex == 0){
	
	  document.forms['searchForm']['dest_dep'].value = groupCode.firstChild.data;
		
	}
	
	//addCountry = nameNode.firstChild.data + country;

    //selectDestination.options[selectIndex] = new Option(addCountry, codeNode.firstChild.data);
	
	selectIndex++;
	
	if (groupCount == 3){
	
		destinationsNode = groupNode.childNodes[2];
		
		destinationCount = 0;
	
		if (destinationsNode.childNodes.length < 2){
		
			selectIndex--;
		
		}
		
		//alert(destinationsNode.childNodes.length);
				
		for (destinationCount = 0; destinationCount < destinationsNode.childNodes.length; destinationCount++){
			
			//alert(destinationsNode.childNodes.length);
		
		  destinationNode = destinationsNode.childNodes[destinationCount];
		  
		  destCode = destinationNode.childNodes[0]; 
		  
		  destName = destinationNode.childNodes[1];
		  
		  destDur = destinationNode.childNodes[2];
		  
		  destHot = destinationNode.childNodes[3];
		  
		  if (destName.firstChild.data == "Grand cayman"){
			
			destName.firstChild.data = "Grand Cayman";
			  
		  }
		  
		  if (destinationsNode.childNodes.length < 2){
			
			 var loc_name = destName.firstChild.data;
			  
		  }else{ 
		  
		  var loc_name = " - " + destName.firstChild.data;

		  }
		  
		 if (destinationsNode.childNodes.length < 2){
			 
			if(destName.firstChild.data != groupName.firstChild.data){
				
			  if(destName.firstChild.data == "Margarita"){
				
				destName.firstChild.data = "Margarita Island";
				  
			  }	
				
			  if(destName.firstChild.data != "Antigua"){
				  
				//var destNameEmd  
		
	      	    destName.firstChild.data = groupName.firstChild.data + " - " + destName.firstChild.data;
			  
			  }
			
			}
	
	      }
		  
		  selectDestination.options[selectIndex] = new Option(loc_name, destCode.firstChild.data);
		  
		  selectIndex++;
			
		}
	
	//alert(destinationsNode.childNodes.length);
	
	}
	/*
	  for(destinationCount = 0; destinationCount < destinationsNode.childNodes.length; destinationCount++){
			
	  }
	*/
  }

}


function displayAirportCode(xmldoc){

  var xmlNode, gatewaysNode, gatewayNode, departureNode, displayText, nodeIndex, selectDeparture;

  var indexId = document.forms["searchForm"]["departureList"];

  airportCode = indexId[indexId.selectedIndex].value;
  
  document.getElementById("gateway_dep").value = airportCode;
  
  

}


function getHotels(xmldoc){

  var destinationsIndex;

  selectHotel = document.getElementById("hotelList");
  
  //selectHotel.options[0] = new Option("All Hotels", "ALL");
  
  var departureCode = document.forms["searchForm"]["destinationList"];
    
  var selectedCode = departureCode[departureCode.selectedIndex].value;
  
  var departure = document.forms["searchForm"]["gateway_dep"].value;
  
  //alert(departure);
 
  var allHotels;
  
  //alert(selectedCode);

  document.getElementById("dest_dep").value = selectedCode;
  
  if(selectedCode.length <= 3){ 

    xmlNode = xmldoc.documentElement;
  
    gatewaysNode = xmlNode.firstChild;
	
      gatewayNode = gatewaysNode.firstChild;
	  
	  gatewayCode = gatewayNode.firstChild;
	  
	  //alert(gatewayCode.firstChild.data);
		
		nodeIndex = gatewaysNode.childNodes.length;
		
		//alert(gatewayCode.firstChild.data);
		
		//alert(gatewaysNode.childNodes.length);
		
		destinationGroupsNode = gatewayNode.childNodes[2];
		
		//alert(destinationGroupsNode.childNodes.length);
		
		//alert(destinationGroupsNode.childNodes[1].length);
		
		for (groupsIndex = 0; groupsIndex < destinationGroupsNode.childNodes.length; groupsIndex++){
			
			destinationsNode = destinationGroupsNode.childNodes[groupsIndex];
			
			nodeCount = destinationsNode.childNodes.length;
			
			//alert(nodeCount);
			
			if (nodeCount > 2){
				
				//alert(nodeCount);
			
		      destinationNode = destinationsNode.childNodes[2];
	
		      //alert(destinationNode.childNodes.length);	
				
  			  for (destinationIndex = 0; destinationIndex < destinationNode.childNodes.length; destinationIndex++){
			   
			    destinationInfo = destinationNode.childNodes[destinationIndex];
				
				destCode = destinationInfo.firstChild;
				
				//alert(destCode.firstChild.data);
				
				if (destCode.firstChild.data == selectedCode){ 
				
				    //alert(destCode.firstChild.data);
				
				    destDur = destinationInfo.childNodes[2];
					
					setDuration(destDur.firstChild.data);
					
					//alert(destDur.firstChild.data);
					
					destHot = destinationInfo.childNodes[3];
					
					document.getElementById("hotels").value = destHot.firstChild.data;
					
					getHotelName();			
					
				} 
				   
			  }
			 
			}
				
		}

  }
  if(selectedCode.length > 2){
	  
	 // alert("test");
	
	resetDuration();
	
  }
  
}

function resetDuration(){
 selectDuration = document.getElementById("duration");
 document.forms['searchForm'].duration.options.length = 3
 selectDuration.options[0] = new Option("3 or 4 Days", 4);
 selectDuration.options[1] = new Option("5 to 10 Days", 7);
 selectDuration.options[2] = new Option("11 to 16 Days", 14);
 selectDuration.options[1].selected = true;


}

function resetHotel(){

  document.forms['searchForm'].hotelList.options.length = 1;
  document.forms['searchForm'].hotels.value = "";

}

function fillnothing(){

  document.forms['searchForm'].hotels.value = "Random";

}
function resetGateway(){

  document.forms['searchForm'].gateway_dep.value = "";
  
}

function resetDestination(){

  document.forms['searchForm'].dest_dep.value = "";
  
}

function resetHotelText(){

  document.forms['searchForm'].hotel_no.value = "";
  
}


function setHotelNo(){
 
  var hotelSelect = document.forms["searchForm"]["hotelList"];

  var hotelCode = hotelSelect[hotelSelect.selectedIndex].value;
  
  document.getElementById("hotel_no").value = hotelCode;

}

function setDuration(duration){
	
  document.forms['searchForm'].duration.options.length = 1

  selectDuration = document.getElementById("duration");

  var durationArray = duration.split(',');
  
  var threefour = 0;
  var fiveten = 0;
  var elevensixteen = 0;
					
  durationCount = durationArray.length;
  
  for(durationIndex = 0; durationIndex < durationCount; durationIndex++){

    if (durationArray[durationIndex] < 5){
	
	  threefour++;
		
	}
	
	if (durationArray[durationIndex] > 4 && durationArray[durationIndex] < 11){
	
	  fiveten++;
		
	}
	
    if (durationArray[durationIndex] > 10){
	
	  elevensixteen++;
		
	}
  }
  
  var durationListCounter = 0;
  
  if (threefour > 0){
	
	selectDuration.options[durationListCounter] = new Option("3 or 4 Days", 4);
	durationListCounter++;
	
  }
  if (fiveten > 0){
	
	selectDuration.options[durationListCounter] = new Option("5 to 10 Days", 7);
	durationListCounter++;
	
  }
  if (elevensixteen > 0){
	
	selectDuration.options[durationListCounter] = new Option("11 to 16 Days", 14);
	durationListCounter++;
	
  }
  
  var departureCode = document.forms["searchForm"]["destinationList"];
    
  var selectedCode = departureCode[departureCode.selectedIndex].value;
  
  var departure = document.forms["searchForm"]["gateway_dep"].value; 
  
  if (departure == "YYZ"){ 
  
    if (selectedCode == 2 || selectedCode == 24 || selectedCode == 47 || selectedCode == 15 || selectedCode == "1,2,17,44,7,77,5,24,69,9"){
  
      selectDuration.options[1].selected = true;
     
    }
  }

}

function setmatch(A,wotstring){

  var L=A.length,who,val;
  
  for(var i=0; i<L; i++){
  
    who=A[i];
	
	if(who.text===wotstring){
	
	  who.parentNode.selectedIndex=i;
	  return who;
		
	}
  
  }
	
}

function getHotelsNames(xmldoc){

	hotelCodes = document.getElementById("hotels").value;
	
	//alert(hotelCodes);
	
	var hotelArray = hotelCodes.split(',');
	
	//alert(hotelArray[0]);
				
    hotelCount = hotelArray.length;
	
	xmlNode = xmldoc.documentElement;
	
	hotelsNode = xmlNode.firstChild;
	
	//selectHotel.options[0] = new Option("All Hotels", "");
	
	for(hotelIndex = 0; hotelIndex < hotelCount; hotelIndex++){
		
		for(hotelLength = 0; hotelLength < hotelsNode.childNodes.length; hotelLength++){  
		
		  hotelNode = hotelsNode.childNodes[hotelLength];
		
		  hotelCode = hotelNode.firstChild;
		
		  hotelName = hotelNode.lastChild;
		  
			if(hotelCode.firstChild.data == hotelArray[hotelIndex]){
			
			  //alert(hotelCode.firstChild.data + hotelName.firstChild.data);
			  
			  listIndex = hotelIndex + 1;
			  
			  selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);
			  
			  //listIndex++;
			}
			
		}
		
		/*
		
			*/									   
												   
	}	
	
}
