//<%-- Hide the children headers and bodies for a particular room --%>
function HideChildrenForRoom(xiRoomNumber, xiRoomCountDropDown, xiChildCountDropDown, xiChildSections)
{
  var lRoomCount = 1;
  var lShowHeader = false;
  if (xiRoomCountDropDown != "" && GetElementInDocument(xiRoomCountDropDown))
  {
    lRoomCount = GetElementInDocument(xiRoomCountDropDown).value;
  }
  if (GetElementInDocument(xiChildCountDropDown))
  {
    var lCountChildren = GetElementInDocument(xiChildCountDropDown).value;
    for(var i = 0; i < xiChildSections.length; i++)
    {
      var lChildSections = GetSpansByName(xiChildSections[i]);
      
      for (var j = 0; j < lChildSections.length; j++)
      {
        lChildSections[j].style.display = (i < lCountChildren) && (xiRoomNumber < lRoomCount) ? "" : "none";
        if ((i < lCountChildren) && (xiRoomNumber < lRoomCount))
        {
          lShowHeader = true;
        }
      }
    }
  }
//<%-- Hide the 'Ages' headers only for the first room, but show for any,
//     to ensure the header is displayed if 1 or more rooms have children. --%>
  if (xiRoomNumber < lRoomCount && (lShowHeader || xiRoomNumber == 0))
  {
    var lChildAgesHeaders = GetSpansByName("ChildAgeHeader");
    for (var j = 0; j < lChildAgesHeaders.length; j++)
    {
      lChildAgesHeaders[j].style.display = lShowHeader ? "" : "none";
    }
  }
}

//<%-- Hide the room sections --%>
function HideRoomSections(xiRoomCountDropDown, xiRoomSections, 
                          xiSingleRoomLabel, xiSingleRoomType, xiRoomTypeHeader,
                          xiRoomTypesVisible)
{
  if(xiRoomCountDropDown == "") return;
  
  var lRoomsDropDown = GetElementInDocument(xiRoomCountDropDown);
  
  if (lRoomsDropDown)
  {
    var lRooms = lRoomsDropDown.value;
    
    for(var i=0; i<xiRoomSections.length; i++)    
    {
      var lRoomSections = GetSpansByName(xiRoomSections[i]);
      for (var j = 0; j < lRoomSections.length; j++)
      {      
        lRoomSections[j].style.display = i < lRooms ? "" : "none";  
      }
    }
  }
  
  /* <%-- TODO: Create a deployment flag to toggle this (see related comment in FillRoomTypesForRoom --%> */
  var lHideSingleRoomType = false;
  
  if(GetElementInDocument(xiSingleRoomLabel))
  {
    GetElementInDocument(xiSingleRoomLabel).style.display = lRooms == 1 ? "none" : "";
  }
  
  if(GetElementInDocument(xiSingleRoomType))
  {
    GetElementInDocument(xiSingleRoomType).style.display = (lRooms == 1 && lHideSingleRoomType) ? "none" : "";
  }
  
  if(GetElementInDocument(xiRoomTypeHeader))
  {
    GetElementInDocument(xiRoomTypeHeader).style.display = (lRooms == 1 && lHideSingleRoomType) || !xiRoomTypesVisible ? "none" : "";
  }
}

//<%-- Hide the room sections --%>
function HidePaxSections(xiAdultCountDropDown, xiChildCountDropDown, xiPaxSections)
{
  if (xiAdultCountDropDown == "" || xiChildCountDropDown == "") return;
  
  var lPax = parseInt(GetElementInDocument(xiAdultCountDropDown).value) + 
    parseInt(GetElementInDocument(xiChildCountDropDown).value) - 1;
    
  for(var i=0; i<xiPaxSections.length; i++)
  {
    var lPaxSections = GetSpansByName(xiPaxSections[i]);
    for (var j = 0; j < lPaxSections.length; j++)
    {      
      lPaxSections[j].style.display = i < lPax ? "" : "none";
    }
  }    
}

//<%-- Fill in appropriate room types in a dropdown --%>
function FillRoomTypesForRoom(xiRoomCountDropDown, xiRoomTypeDropDown, xiRoomTypeLabel, xiAdultsDropDown, xiChildrenDropDown, xiDefaultToBlank, xiBlankText)
{
  if(xiRoomCountDropDown == "" || xiRoomTypeDropDown == "") return;
  var lRooms = GetElementInDocument(xiRoomCountDropDown).value;
  var lRoomTypeDropDown = GetElementInDocument(xiRoomTypeDropDown);
  
  var lAdults    = GetElementInDocument(xiAdultsDropDown).value;
  var lChildren  = GetElementInDocument(xiChildrenDropDown).value;
  var lRoomTypes = mRoomTypesForPax[lAdults + ";" + lChildren];
  var lOptions   = lRoomTypeDropDown.options;
  
  var lPrevValue = lRoomTypeDropDown.selectedIndex == -1 ? -1 : lRoomTypeDropDown.options[lRoomTypeDropDown.selectedIndex].value;
  lOptions.length = 0; //<%-- Clear dropdown --%>
  
  /*<%-- TODO: Create a deployment flag to enable the following lines only if single-room searches
            should always be fuzzy. NB HotelSearch.cs has related function that must match this. --%>
  if (lRooms == 1)
  {
    lRoomTypes = [0];
  }
  */
  
  var lShowRoomType = lRoomTypes && lRoomTypes.length > 0 && (lRoomTypes.length > 1 || lRoomTypes[0] != 0);
    
  if(!lShowRoomType)
  {
    lRoomTypeDropDown.style.display = "none";
    GetElementInDocument(xiRoomTypeLabel).style.display = "none";
  }
  else if(lRoomTypes.length == 1)
  {
    var lRoomTypeName = mRoomTypeNames[lRoomTypes[0]];
    
    lRoomTypeDropDown.style.display = "none";
    GetElementInDocument(xiRoomTypeLabel).style.display = "";
    GetElementInDocument(xiRoomTypeLabel).innerHTML = GetRoomTypeName(lRoomTypes[0], lAdults, lChildren);
    lOptions[0] = new Option("", lRoomTypes[i], false, false);
    lOptions.selectedIndex = 0;
  }
  else
  {
    if (xiDefaultToBlank)
    {
      lOptions[0] = new Option(xiBlankText, -1, false, false);
    }
    
    var lSelectedIndex = -1;
    
    for(var i=0; i<lRoomTypes.length; i++)
    {
      lOptions[lOptions.length] = new Option(GetRoomTypeName(lRoomTypes[i], lAdults, lChildren), lRoomTypes[i], false, false);
      
      if(lRoomTypes[i] == lPrevValue)
      {
        lSelectedIndex = lOptions.length - 1;
      }
    }
    GetElementInDocument(xiRoomTypeLabel).style.display = "none";
    lRoomTypeDropDown.style.display = "";
    
    if(lSelectedIndex > -1)
    {
      lRoomTypeDropDown.selectedIndex = lSelectedIndex;
    }
  }
  
  return lShowRoomType;
}

//<%-- Hack to work around the fact that getElementsByName only looks at ID and not NAME in IE --%>
function GetSpansByName(xiName)
{
  if (!IsIe())
  {
    return document.getElementsByName(xiName);
  }
  else
  {
    var lArray = new Array();
    var lElements = document.getElementsByTagName("span");
    for (var i = 0; i < lElements.length; i++)
    {
      if (lElements[i].name == xiName)
      {
        lArray.push(lElements[i]);
      }
    }
    
    return lArray;
  }
}

function GetTRsByName(xiName)
{
  if (!IsIe())
  {
    return document.getElementsByName(xiName);
  }
  else
  {
    var lArray = new Array();
    var lElements = document.getElementsByTagName("tr");
    for (var i = 0; i < lElements.length; i++)
    {
      if (lElements[i].name == xiName)
      {
        lArray.push(lElements[i]);
      }
    }
    
    return lArray;
  }
}

//<%-- Refresh all the room / room type / children display sections --%>
function RefreshRoomDisplay()
{
  var lRoomTypesVisible = FillRoomTypes();
  HideRooms(lRoomTypesVisible);
  HideChildren();
}

function ShowNightCountTextBox(xiCountNightsDropDown)
{
  var lCountNightsOther = GetElementInDocument("CountNightsOther");
  var lNightCountDropDown = GetElementInDocument(xiCountNightsDropDown);
  if (lCountNightsOther != null && lNightCountDropDown != null)
  {
    lCountNightsOther.style.display = lNightCountDropDown.value == "-1" ? "" : "none";
  }
}

var gStayLengthLastIndex = 0;
var gAdultCountLastIndex = 0;
var gChildCountLastIndex = 0;

function ShowStayLengthPopUp()
{
  ShowDropDownPopUp(CountNightsDropDown, gStayLengthLastIndex, "Please enter the number of nights:");
}

function ShowAdultCountPopUp()
{
  ShowDropDownPopUp(AdultCountDropDown, gAdultCountLastIndex, "Please enter the number of adults:");
}

function ShowChildCountPopUp()
{
  ShowDropDownPopUp(ChildCountDropDown, gChildCountLastIndex, "Please enter the number of children:");
}

function ShowDropDownPopUp(xiControl, xiLastValue, xiPrompt)
{
  var lDropDown = GetElementInDocument(xiControl);
  if (lDropDown.value == "-1")
  {
    var lValue = prompt(xiPrompt, "");
    if (!isNaN(parseInt(lValue)) && lValue != null && lValue > 0 && parseInt(lValue) == lValue)
    {
      var lIsNewItem = true;
      for (var i = 0; i < lDropDown.options.length; i++)
      {
        if (lValue == lDropDown.options[i].value)
        {
          lIsNewItem = false;
        }
      }
      if (lIsNewItem)
      {
        var lOther = lDropDown.options[lDropDown.options.length - 1];
        lDropDown.options.length = lDropDown.options.length + 1;
        lDropDown.options[lDropDown.options.length - 1] = new Option(lOther.text, lOther.value);
        lDropDown.options[lDropDown.options.length - 2] = new Option(lValue, lValue);
      }
      for (var i = 0; i < lDropDown.options.length; i++)
      {
        if (lValue == lDropDown.options[i].value)
        {
          lDropDown.selectedIndex = i;
        }
      }
    }
    else
    {
      lDropDown.selectedIndex = xiLastValue;
    }
  } 
  xiLastValue = lDropDown.selectedIndex;
}

//<%-- Reads the value from xiControl, and sets the visibility of xiControlToHide based on it --%>
function ShowSection(xiControl, xiValueToHide, xiControlToHide)
{
  var lDropDown = GetElementInDocument(xiControl);
  if (lDropDown.value == xiValueToHide)
  {
    xiControlToHide.style.display == "none";
  }
  else
  {
    xiControlToHide.style.display == "";
  }
}
    

//<%-- Hide the whole room section --%>
function DisplayRoomEntrySection(xiShowRooms, xiRoomEntrySection, xiPaxEntrySection)
{
  var lRoomSections = GetSpansByName(xiRoomEntrySection);
  for (var j = 0; j < lRoomSections.length; j++)
  {      
    lRoomSections[j].style.display = xiShowRooms ? "" : "none";
  }
  
  var lPaxSections = GetSpansByName(xiPaxEntrySection);
  for (var j = 0; j < lPaxSections.length; j++)
  {      
    lPaxSections[j].style.display = xiShowRooms ? "none" : "";
  }  
}  
  
