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);
	
	//selectDeparture.options[departureIndex].className = "dropdown2";
	
	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");
  selectDestination.options[0].className = "dropdown1";

  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].className = "dropdown1";
	  //selectDestination.options[selectIndex].class = "titledrop";
	  
	}else{
	
	  selectDestination.options[selectIndex] = new Option(groupName.firstChild.data, groupCode.firstChild.data);
	  selectDestination.options[selectIndex].className = "dropdown1";
	  
	}
	
	
	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);
		  if (destinationsNode.childNodes.length < 2){
		    selectDestination.options[selectIndex].className = "dropdown1";
		  }else{
			selectDestination.options[selectIndex].className = "dropdown2"; 
		  }
		  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){
	
  //alert(document.forms["searchForm"]["departureList"].options.selectedIndex.text);

  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;

  var departure_text = departureCode[departureCode.selectedIndex].text;
  
  document.getElementById("remember_des").value = departure_text;
  
  if(departure_text == "All Countries" || departure_text == "All South" || departure_text == "All Europe"){
	document.getElementById("sentdest").value = departure_text;  
  }else{
	 
	 if(departure_text == "Bahamas - Nassau"){
		departure_text = "Nassau"; 
	 }
	 if(departure_text == "Barbados - Bridgetown"){
		departure_text = "Bridgetown"; 
	 }
	 if(departure_text == "Belize - Belize City"){
		departure_text = "Belize City"; 
	 }
	 if(departure_text == "Cayman Islands - Grand Cayman"){
		departure_text = "Grand Cayman"; 
	 }
	 if(departure_text == "Colombia - Cartagena"){
		departure_text = "Cartagena"; 
	 }
	 if(departure_text == "El Salvador - San Salvador"){
		departure_text = "San Salvador"; 
	 }
	 if(departure_text == "Greece - Athens"){
		departure_text = "Athens"; 
	 }
	 if(departure_text == "Guadeloupe - Pointe a Pitre"){
		departure_text = "Pointe a Pitre"; 
	 }
	 if(departure_text == "Puerto Rico - San Juan"){
		departure_text = "San Juan"; 
	 }
	 if(departure_text == "Saint Kitts Nevis - St Kitts"){
		departure_text = "St Kitts"; 
	 }
	 if(departure_text == "Saint Lucia - St Lucia"){
		departure_text = "St Lucia"; 
	 }
	 if(departure_text == "Spain - Madrid"){
		departure_text = "Madrid"; 
	 }
	 if(departure_text == "Turks And Caicos - Providenciales"){
		departure_text = "Providenciales"; 
	 }
	 if(departure_text == "Venezuela - Margarita Island"){
		departure_text = "Margarita Island"; 
	 }	  
	 departure_text = departure_text.replace(/ALL /i, "");
	 departure_text = departure_text.replace(/     - /i, "");
	 departure_text = departure_text.replace(/- /i, "");
	 
	 document.getElementById("sentdest").value = departure_text;
  }
  
  

  var allHotels;
  
  //alert(selectedCode);

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

    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];
					
					//alert(destHot.firstChild.data);
					
					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 Nights", 4);
 selectDuration.options[1] = new Option("5 to 10 Nights", 7);
 selectDuration.options[2] = new Option("11 to 16 Nights", 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 Nights", 4);
	durationListCounter++;
	
  }
  if (fiveten > 0){
	
	selectDuration.options[durationListCounter] = new Option("5 to 10 Nights", 7);
	durationListCounter++;
	
  }
  if (elevensixteen > 0){
	
	selectDuration.options[durationListCounter] = new Option("11 to 16 Nights", 14);
	durationListCounter++;
	
  }
  
  var departureCode = document.forms["searchForm"]["destinationList"];
    
  var selectedCode = departureCode[departureCode.selectedIndex].value;
  
  var departure = document.forms["searchForm"]["gateway_dep"].value; 
  
  if(selectDuration.options[0].value == 4){
	  
    if(selectedCode == 71 || selectedCode == 25){
	 
	  selectDuration.options[0].selected = true;
	   
	}else{
	
	  selectDuration.options[1].selected = true;
	
	}
	  
  }else{
	 
	 selectDuration.options[0].selected = true;
	 
  }
  
  /*if (departure == "YYZ" || departure == "YUL"){ 
  
    if (selectedCode == 2 || selectedCode == 24 || selectedCode == 47 || selectedCode == 15 || selectedCode == 10 || selectedCode == 8 || selectedCode == "1,2,17,44,7,77,5,24,69,9"){
  
      selectDuration.options[1].selected = true;
     
    }
  }*/
  if (xmldata = document.getElementById("sentalias").value == "sun"){
	
	//alert(selectedCode);
	if(selectedCode == 15 || selectedCode == 8){
	
		selectDuration.options[0].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;
		
	var hotelArray = hotelCodes.split(',');
			
    hotelCount = hotelArray.length;
	
	xmlNode = xmldoc.documentElement;
	
	hotelsNode = xmlNode.firstChild;
	
	var listIndex = 0;
	
	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]){
			  
			  var specials_check = document.getElementById("sentalias").value;
			  
			  if(specials_check=="san"){
				var hotel_name = hotelName.firstChild.data;
				var alias_grab = hotel_name.indexOf('SANDALS');
				
				if(alias_grab == 0){
				 
				 listIndex++;
				 selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";
					
				}
			
			  }
			  else if(specials_check=="bea"){
				var hotel_name = hotelName.firstChild.data;
				var alias_grab = hotel_name.indexOf('BEACHES');
				
				if(alias_grab == 0){
				 
				 listIndex++;
				   selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";	
					
				}
			
			  }
			  else if(specials_check=="bzs"){
				var hotel_name = hotelName.firstChild.data;
				var alias_grab = hotel_name.indexOf('BREEZES');
				
				if(alias_grab == 0){
				 
				 listIndex++;
				   selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";	
					
				}
				
				var alias_grab2 = hotel_name.indexOf('HEDONISM');
				
				if(alias_grab2 == 0){
				 
				 listIndex++;
				   selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";	
					
				}
				
				var alias_grab3 = hotel_name.indexOf('STARFISH TRELAWNY');
				
				if(alias_grab3 == 0){
				 
				 listIndex++;
				   selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";	
					
				}
				
				var alias_grab4 = hotel_name.indexOf('GRAND LIDO');
				
				if(alias_grab4 == 0){
				 
				 listIndex++;
				   selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";	
					
				}
				
				var alias_grab5 = hotel_name.indexOf('ROOMS OCHO RIOS');
				
				if(alias_grab5 == 0){
				 
				 listIndex++;
				   selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				 //selectHotel.options[listIndex].className = "dropdown2";	
					
				}
			
			  }
			  else{
			 
			  listIndex = hotelIndex + 1;
			  
			    selectHotel.options[listIndex] = new Option(hotelName.firstChild.data, hotelCode.firstChild.data);	
				//electHotel.options[listIndex].className = "dropdown2";
			  }
			  
			}
			
		}						   
												   
	}	
	
}
function setDepMem(){
  var departure_cityCode = document.forms["searchForm"]["departureList"];
  var dep_city_text = departure_cityCode[departure_cityCode.selectedIndex].text;
  document.getElementById("remember_dep").value = dep_city_text;	
}
function greyOut(){	 
 var hotelChosen = document.forms["searchForm"]["hotelList"]; 
 var selectedHotel = hotelChosen[hotelChosen.selectedIndex].value;
 selectRating = document.getElementById("rating");
 selectPrice = document.getElementById("price");
 if(selectedHotel == "ALL"){
   document.forms['searchForm'].rating.options.length = 4
   selectRating.options[0] = new Option("All Ratings");
   selectRating.options[1] = new Option("3+", 3);
   selectRating.options[2] = new Option("4+", 4);
   selectRating.options[3] = new Option("5+", 5);
   selectRating.options.disabled=false;
   document.forms['searchForm'].rating.options.length = 4
   selectPrice.options[0] = new Option("Get Me a Deal!");
   selectPrice.options[1] = new Option("Less than $1000", 1);
   selectPrice.options[2] = new Option("Less than $1500", 2);
   selectPrice.options[3] = new Option("Less than $2000", 3);
   selectPrice.options.disabled=false;
 }else{ 
 
 document.forms['searchForm'].rating.options.length = 1;
 selectRating.options[0] = new Option("All Ratings", 1);
 selectRating.options.disabled=true;
 
 document.forms['searchForm'].price.options.length = 1;
 selectPrice.options[0] = new Option("Get Me a Deal!");
 selectPrice.options.disabled=true;
 }
}
