function search_elements(){
  var elements = new Array();
  elements[0] = "input";
  elements[1] = "select";
  return elements;
}

function recallsearch(){
  var i, num, cookie, children, child;
  var elements = search_elements();
  for (e in elements)
    {
    children = document.getElementById("search_container").getElementsByTagName(elements[e]);
    num = children.length;
    
    for (i = 0; i < num; i++)
      {
      child = children[i];
      cookie = readCookie("search_"+child.name);
      if (cookie != null)
        {
        if ((child.tagName == "INPUT") && (child.type == "checkbox"))
          {
          if (cookie == 1)
            {
            child.checked = true;
            }
          else
            {
            child.checked = false;
            }
          }
        else
          {
          child.value = cookie;        
          }
        }
      }
    }
}

function storesearch(){
  var elements = search_elements();
  var e, num, children, child, element, value;
  
  for (e in elements)
    {
    children = document.getElementById("search_container").getElementsByTagName(elements[e]);
    
    num = children.length;
    for (i = 0; i < num; i++)
      {
      child = children[i];
      if (child.name)
        {
        if ((child.tagName == "SELECT") || ((child.tagName == "INPUT") && (child.type == "text")))
          {
          createCookie("search_"+child.name, child.value, 0.5);
          }
        else if ((child.tagName == "INPUT") && (child.type == "checkbox"))
          {
          value = (child.checked == true) ? 1 : 0;
          createCookie("search_"+child.name, value, 0.5);
          }
        }
      }
    }
}

function apartment_list(){
  var i, obj, element, parent, child, num;
  
  // Store areas/neighborhoods
  var area;
  neighborhoods = new Object(); // Global variable
  
  element = document.getElementById("search_neighborhoods");
  num = element.getElementsByTagName("option").length;
  var e, textnode;
  for (i = 0; i < num; i++)
    {
    child = element.getElementsByTagName("option")[i];
    if (child.value === "")
      {
      area = child.firstChild.nodeValue;
      neighborhoods[area] = new Object();
      if (!firstarea)
        {
        var firstarea = area;
        }
      e = 0;
      }
    else if (child.value !== "NULL")
      {
      neighborhoods[area][e++] = child.value;
      }
    }
  
  // Remove nodes
  element = document.getElementById("search_areasneighborhoods");
  while (element.hasChildNodes())
    {
    element.removeChild(element.firstChild);
    }

  // Display area links list
  obj = document.createElement("ul");
  obj.setAttribute("id", "search_areas");
  document.getElementById("search_areasneighborhoods").appendChild(obj);
  
  var area;
  for (area in neighborhoods)
    {
    parent = document.getElementById("search_areas");
    
    obj = document.createElement("li");
    parent.appendChild(obj);
    
    obj = document.createElement("a");
    obj.setAttribute("href", neighborhoods_href(area));
    parent.lastChild.appendChild(obj);
  
    obj = document.createTextNode(area);
    parent.lastChild.firstChild.appendChild(obj);
    }
  
  // Display the default neighborhood
  displayneighborhoods(firstarea);
}

function str_to_var_name(str) {
  // Converts a string into a variable name 
  // (e.g., 'Lazy Dog' -> 'lazy_dog')
  
  // @param  string  str  string to convert
  
  // @return  string  variable name
  
  var var_name;
  
  var_name = str.toLowerCase();
  var_name = var_name.replace(/ /g, '_');
  
  return var_name;
}

function displayneighborhoods(area){
  var num, i, parent, element, obj, next, container, href,
      neighborhoods_id, child, children, areas_element;
  
  neighborhoods_id = 'search_neighborhoods_' + str_to_var_name(area);
  areas_element = document.getElementById("search_areas");
  
  // Modify links list
  var link_element, strong_element;
  var elements = areas_element.getElementsByTagName('li');
  num = elements.length;
  for (i = 0; i < num; i++)
    {
    element = elements[i].firstChild;
    textnode = element.firstChild;
    if (element.tagName === "STRONG")
      {
      obj = document.createElement("a");
      obj.setAttribute("href", neighborhoods_href(textnode.nodeValue));
      elements[i].appendChild(obj);
      
      link_element = elements[i].getElementsByTagName("a")[0];
      obj = document.createTextNode(textnode.nodeValue);
      link_element.appendChild(obj);
      
      elements[i].replaceChild(link_element, elements[i].firstChild);
      }
    else if ((element.tagName === "A") && (textnode.nodeValue === area))
      {
      obj = document.createElement("strong");
      elements[i].appendChild(obj);
  
      obj = document.createTextNode(textnode.nodeValue);
      strong_element = elements[i].getElementsByTagName("strong")[0];
      strong_element.appendChild(obj);
      
      elements[i].replaceChild(strong_element, elements[i].firstChild);
      }
    }
  
  // Hide all other neighborhoods
  children = document.getElementById('search_areasneighborhoods')
               .getElementsByTagName('div');
  num = children.length;
  
  for (i = 0; i < num; i++)
    {
    if (children[i].className == 'neighborhoods')
      {
      children[i].style.display = 'none';
      }
    }
  
  if (!document.getElementById(neighborhoods_id))
    {
    // Create box for neighborhoods
    obj = document.createElement("div");
    obj.className = 'neighborhoods';
    obj.setAttribute("id", neighborhoods_id);
    obj.style.display = 'none';
    document.getElementById("search_areasneighborhoods")
      .insertBefore(obj, areas_element);
    
    // Print neighborhoods
    var id;
    var neighborhoods_box = document.getElementById(neighborhoods_id);
    i = 0;
    for (neighborhood in neighborhoods[area])
      {
      id = "neighborhood_" + str_to_var_name(area) + i;
      
      obj = document.createElement("label");
      obj.setAttribute("for", id);
      neighborhoods_box.appendChild(obj);
      
      obj = document.createElement("input");
      obj.setAttribute("id", id);
      obj.setAttribute("type", "checkbox");
      obj.setAttribute("name", "neighborhoods[]");
      obj.setAttribute("value", neighborhoods[area][neighborhood]);
      neighborhoods_box.lastChild.appendChild(obj);
     
      obj = document.createTextNode(" "+neighborhoods[area][neighborhood]);
      neighborhoods_box.lastChild.appendChild(obj);
      
      i++;
      }
    
    // Fix for Internet Explorer LABELs (it won't select the corresponding checkbox)
    num = neighborhoods_box.getElementsByTagName('label').length;
    for (i = 0; i < num; i++)
      {
      element = neighborhoods_box.getElementsByTagName('label')[i];
      element.htmlFor = element.getElementsByTagName('input')[0].id;
      }
    
    // Highlight the labels when they are clicked
    obj = document.getElementById(neighborhoods_id).getElementsByTagName("label");
    num = obj.length;
    for (i = 0; i < num; i++)
      {
      obj[i].onclick = new Function("apartment_highlight_toggle(this);");
      }
    
    // Create "select all" / "deselect all" links container
    obj = document.createElement('div');
    obj.style.position = "absolute";
    obj.style.right = "0em";
    obj.style.bottom = "-1.3em";
    
    // Add the links container after the neighborhoods box
    document.getElementById(neighborhoods_id).appendChild(obj);
    
    // Populate the links container
    container = obj;
    
    obj = document.createElement('a');
    obj.setAttribute(
      "href", 
      "javascript:toggleneighborhoods(1, '" + neighborhoods_id + "')");
    container.appendChild(obj);
    
    obj = document.createTextNode('Select All');
    container.lastChild.appendChild(obj);
    
    obj = document.createTextNode(' / ');
    container.appendChild(obj);
    
    obj = document.createElement('a');
    obj.setAttribute(
      "href", 
      "javascript:toggleneighborhoods(0, '" + neighborhoods_id + "')");
    container.appendChild(obj);
    
    obj = document.createTextNode('Deselect All');
    container.lastChild.appendChild(obj);
    }
  
  document.getElementById(neighborhoods_id).style.display 
    = 'block';
}

function apartment_highlight_toggle(obj){
  if (!browser("ie5"))
    {
    if (obj.getElementsByTagName("input")[0].checked)
      {
      obj.style.backgroundColor = "#cedffe";
      }
    else
      {
      obj.style.backgroundColor = "transparent";
      }
    }
}

function toggleneighborhoods(status, containerid){
  var i, num, container, labels;
  
  // Toggle checkboxes
  togglechecks(status, containerid);
  
  // Highlight labels
  container = document.getElementById(containerid);
  labels = container.getElementsByTagName('label');
  num = labels.length;
  
  i = 0;
  while (i < num)
    {
    apartment_highlight_toggle(labels[i]);
    i++;
    }
}

function neighborhoods_href(area){
  return "javascript:displayneighborhoods('"+area+"');";
}
  
function search_changeinputs(formid){
  var i, href;
  var elements = document.getElementById(formid).getElementsByTagName('input');
  for (i = 0; i < elements.length; i++)
    {
    href = "javascript:";
    if (elements[i].type == "submit")
      {
      href += "createformelement('"+elements[i].id+"');";
      href += "document.getElementById('"+formid+"').submit();";
      i = replace_searchinput(elements, href, i);
      }
    else if (elements[i].type == "reset")
      {
      href += "document.getElementById('"+formid+"').reset();";
      i = replace_searchinput(elements, href, i);
      }
    }
}

function search_deselectchildren(parent){
  if (document.getElementById(parent))
    {
    var container = document.getElementById(parent);
    var inputs = container.getElementsByTagName('input');
    var num = inputs.length;
    var i;
    for (i = 0; i < num; i++)
      {
      inputs[i].checked = false;
      }
    }
}

function replace_searchinput(elements, href, i){
  if (element_to_imglink(elements[i], href))
    {
    // Element has been removed; loop increment must be decreased
    return --i;
    }
  else
    {
    return i;
    }
}

function reset_neighborhoods(){
  var i;
  var elements = document.getElementById('search_neighborhoods').getElementsByTagName('label');
  var num = elements.length;
  for (i = 0; i < num; i++)
    {
    elements[i].style.backgroundColor = "transparent";
    }
}

function details_unitlinks(){
  // Add links that expand/collapse unit details
  
  var element, obj, text;
  var num = document.getElementById('details_body').getElementsByTagName('td').length;
  
  // Loop through all TDs
  var i = 0;
  while (i < num)
    {
    element = document.getElementById('details_body').getElementsByTagName('td')[i];
    
    if (element.className == "details_unitname")
      {
      // This element will contain the expand/collapse link
      
      // Store and destroy the text
      text = element.firstChild.nodeValue;
      element.removeChild(element.firstChild);
      
      // Create the link
      obj = document.createElement('a');
      obj.setAttribute('href', 'javascript:details_unitdetailtoggle('+i+');');
      obj.setAttribute('title', 'Show the unit details');
      element.appendChild(obj);
      
      // Recall and create the text within the link
      obj = document.createTextNode(text);
      element.firstChild.appendChild(obj);
      }
    
    i++;
    }
}

function details_hideallunitdetails(){
  // Hide all unit details
  
  var row, obj, text;
  var num = document.getElementById('details_body').getElementsByTagName('tr').length;
  
  // Loop through all TRs
  var i = 0;
  while (i < num)
    {
    row = document.getElementById('details_body').getElementsByTagName('tr')[i];
    
    if (row.className == "details_floorplandesc")
      {
      // Hide this row
      row.style.display = "none";
      }
    
    i++;
    }
}

function details_unitdetailtoggle(td){
  // Show/hide details rows
  
  var i, contentarea, detailsrow, linktitle, obj, text;
  var container = document.getElementById('details_body');
  var element = container.getElementsByTagName('td')[td];
  var linkelement = element.getElementsByTagName('a')[0];
  
  if (element)
    {
    // Find the details row 
    var next = element.parentNode;
    do
      {
      next = next.nextSibling;
      }
    while (next.className != "details_floorplandesc")
    
    // Store details row
    detailsrow = next;
    
    contentarea = detailsrow.getElementsByTagName('div')[0];
    
    var linktext = linkelement.firstChild.nodeValue;
    
    // Show/hide the details row
    if (detailsrow.style.display == "none")
      {
      detailsrow.style.display = "";
      linktitle = "Hide the unit details";
      
      // Add 'hide' link if it doesn't exist
      var hidelinkid = 'details_hidelink'+td
      
      if (!document.getElementById(hidelinkid))
        {        
        // Create link
        obj = document.createElement('a');
        obj.setAttribute('id', hidelinkid);
        obj.setAttribute('href', 'javascript:details_unitdetailtoggle('+td+');');
        obj.setAttribute('title', linktitle);
        contentarea.appendChild(obj);
        
        var hidelink = contentarea.lastChild;
        
        // Style link
        hidelink.style.fontSize = "0.85em";
        hidelink.style.styleFloat = hidelink.style.cssFloat = "right";
        
        // Create link text
        obj = document.createTextNode('Hide');
        hidelink.appendChild(obj);
        }
      }
    else
      {
      detailsrow.style.display = "none";
      linktitle = "Show the unit details";
      }
    
    linkelement.title = linktitle;
    }
}

function largephotobox(){
  // Enlarged photos will be opened inside a central element
  
  if (!document.getElementById('largephotobox'))
    {
    // Couldn't find the 'central' element
    return false;
    }
  else
    {
    var current_uri, i, hash_start, href, parent;
    var container = document.getElementById('details_body');
    var links = container.getElementsByTagName('a');
    var num = links.length;
    
    for (i = 0; i < num; i++)
      {
      if (links[i].className == "largephotolink")
        {
        // Remove target as the default setting opens a new window
        links[i].removeAttribute("target");
        
        // Escape apostrophes
        href = links[i].href.replace(/'/g, "\'");
        
        // Set onclick to enlarge the image
        links[i].onclick = new Function("openlargephoto('"+href+"');");
        
        // Set href to jump to the enlarged image anchor
        links[i].href = add_hash_to_uri(
                          String(window.location), 'largephotobox');
        }
      }
    }
}

function openlargephoto(imghref){
  // Open enlarged photo inside the 'large photo box'
  
  var obj;
  var container = document.getElementById('largephotobox');
  
  // Remove all nodes inside container
  while (container.hasChildNodes())
    {
    container.removeChild(container.firstChild);
    }
  
  // Create the new image
  obj = document.createElement('img');
  obj.setAttribute('alt', "");
  obj.setAttribute('src', imghref);
  obj.setAttribute('width', "534");
  obj.setAttribute('height', "405");
  
  // Append the image
  container.appendChild(obj);
  
  // Set bottom margin
  container.lastChild.style.marginBottom = "1em";
}

function largephotopopup(){
  // Enlarged photos will be opened in a popup window
  
  var i, href, parent;
  var container = document.getElementById('details_body');
  var links = container.getElementsByTagName('a');
  var num = links.length;
  
  for (i = 0; i < num; i++)
    {
    if ((links[i].className == "largephotolink") && (links[i].target != ""))
      {
      links[i].target = "_self";
      
      // Escape apostrophes
      href = links[i].href.replace(/'/g, "\'");
      
      // Set href to open the popup window
      links[i].href = "javascript:imagepopup('"+href+"', 534, 405)";
      }
    }
}