// menu control variables & functions

var visibleChildId = '';
var currentChildId = '';

function findTop(e) 
{
  var top = e.offsetTop;
  
  while(e.offsetParent) {
    e = e.offsetParent;
    top += e.offsetTop;
  }
  
  return(top);
}

function countLines(e)
{
  var i;
  var lines = 0;
  
  if (e) {
    // count the text child nodes to compute required height
    for(lines=0, i=0; i < e.childNodes.length; i++) {
      if (e.childNodes[i].nodeName == 'A') {
        // anchor element count as one line
        lines++;
      }
      else if (e.childNodes[i].nodeName == 'DIV') {
        // recursively count lines in div child elements
        lines += countLines(e.childNodes[i]);
      }
    }
  }
  
  return(lines);
}

function showMenu(id) 
{
  var e;
  var p;
  var top;
  var height;
  var windowBottom;

  // if there is another menus showing, hide it first
  if (visibleChildId) {
    e = document.getElementById(visibleChildId);
    e.style.height = '0px';
    e.style.visibility = 'hidden';
    visibleChildId = '';
    
    // set the link color of the "top" link to un-hover
    for(p=e.parentNode; p.nodeName != 'DIV'; p=p.parentNode) { }
    for(p=p.firstChild; p.nodeName != 'A'; p=p.nextSibling) { }
    p.style.backgroundColor = '#efefef';
  }
  
  // bail out if we are asked to show the current (fixed) child menu
  if (currentChildId == id) {
    return;
  }
  
  // if we have a child menu with the right id make it visible
  e = document.getElementById(id);
  if (e) {
    // figure out how to position the popped menu 
    top = findTop(e.parentNode) + 8;
    height = (countLines(e) * 20);

    // browser inconsistency
    if (typeof window.pageYOffset != 'undefined') {
      windowBottom = window.innerHeight + window.pageYOffset;
    }
    else if (typeof document.documentElement != 'undefined') {
      windowBottom = document.documentElement.clientHeight + document.documentElement.scrollTop;
    }
    else {
      windowBottom = document.body.clientHeight + document.body.scrollTop;
    }

    // figure out how much of the 
    if ((height+top) > windowBottom) {
      top = windowBottom - height;
    }

    e.style.height = height + 'px';
    e.style.position = 'absolute';
    e.style.left = '150px';
    e.style.top = top + 'px';
    e.style.zIndex = 20;
    e.style.visibility = 'visible';
        
    // locate the "top" link for this submenu and mark it hovered...
    for(p=e.parentNode; p.nodeName != 'DIV'; p=p.parentNode) { }
    for(p=p.firstChild; p.nodeName != 'A'; p=p.nextSibling) { }
    p.style.backgroundColor = '#dcdcdc';
    
    // remember which submenu we just opened up
    visibleChildId = id;
  }
}

function initMenu(leafId) 
{
  var e = document.getElementById(leafId);
  var p;

  if (e) {  
    e.className = 'menu breadcrumb';
   
    // find the first parent DIV and open it
    e=e.parentNode;   
    visibleChildId = '';
    currentChildId = e.id;

    e.style.height = (countLines(e) * 20)+ 'px';
    e.style.position = 'relative';
    e.style.zIndex = 10;
    e.style.visibility = 'visible';
 
    // make any children visible
    e = document.getElementById(leafId);
    for(e=document.getElementById(leafId).nextSibling; e && (e.nodeName != 'DIV'); e=e.nextSibling) {}
    if (e) {
      e.style.visibility = 'visible';
      e.style.position = 'relative';
      e.style.zIndex = 10;
      currentChildId = e.id;
    }
  }
}

function selectHorse(us_id, feif_id) {
  document.getElementById('US_id').value = us_id;
  document.getElementById('FEIF_id').value = feif_id;
  hideIbox();
}

function findHorses() {
  var params;
     
  params = 'Submit=Submit';
  params = params + '&Name1=' + document.getElementById('Name1').value;
  params = params + '&FarmName=' + document.getElementById('FarmName').value;
     
  showIndicator();
  http.open('post', '/sbook_search.php', true);
  
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=iso-8859-1");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close");
     
  http.onreadystatechange = function() {
    if(http.readyState == 4){
      hideIndicator();
      var response = http.responseText;
      setIBoxContent(response);
    }
  }
     
  http.send(params);
}
