var RollOnColor = '#FFE9AD';  //Default mouseover color set
var RollOffColor = '#FEC013';  //Default background color set
var direction = 0;
var menu;
var timerId = 0;
var timerDelId = 0;

function onload() {

//setHTMLTitle();

  var errorfound = 0;
  // Loading XML/XSL and transform into elements
  // Error capturing test on XML/XSL load
  try{
    var source = new ActiveXObject("Msxml2.DOMDocument.3.0");
    source.async = false;
  }
  catch (e){
    document.location.href = '/library/message/downloadxml.asp';
    errorfound = 1;
  }
  try{
    source.load(xmldata.src);
  }
  catch (e){
    alert ('ERROR: Unable to load XML File at ' + xmldata.src);
    errorfound = 1;
  }
  try{
    if (source.documentElement.selectSingleNode("menu").getAttribute("oncolor").text != null){
      RollOnColor = source.documentElement.selectSingleNode("menu").getAttribute("oncolor");
    }
    if (source.documentElement.selectSingleNode("menu").getAttribute("offcolor").text != null){
      RollOffColor = source.documentElement.selectSingleNode("menu").getAttribute("offcolor");
    }
  }
  catch (e){
    //No error, fail gracefully
  }
  if (errorfound == 0){
    // Load style sheet.
    var stylesheet = new ActiveXObject("Msxml2.DOMDocument.3.0");
    stylesheet.async = false;
    stylesheet.load(xsldata.src);
    // Fill result div tag with the transformation
    document.all.result.innerHTML = source.transformNode(stylesheet);
  }
  //setStyle(); /*Set the styles for the NAV, tried this but it didn't work*/
}

function setHTMLTitle()
{
var tagname= new Array (document.getElementsByTagName("H1"));
for (i=0;i<=tagname.length; i++){
//document.getElementsByTagName("H1")[count].style.color=clrnum;
//alert(document.getElementsByTagName("H1")[i].innerHTML)
//alert(document.getElementsByTagName("H2")[i].innerHTML)
document.title = document.getElementsByTagName("H1")[i].innerHTML;
break;
}
}

function deletePopup(el){
  var oDiv = result.all.tags("DIV");
  var iDiv;
  showItems("SELECT");
  showItems("IFRAME"); 
  if (oDiv != null){
    iDiv = oDiv.length;
    for (var i=0; i<iDiv; i++){
      if(oDiv[i].className == 'floatingMenu'){
        oDiv[i].style.display = 'none';
        oDiv[i].style.visibility = 'hidden';
      }
    }
  } 
  if (el){
    while (el.parentElement && el.parentElement.id != 'result'){
      el = el.parentElement;
      if(el.className == 'floatingMenu'){
        el.style.display='';
        el.style.visibility='visible';
        hideItems("SELECT", el);
        hideItems("IFRAME", el);
      }
    }
  }
}

function doMenuz(el, popup) {
  timerId = 0;
  if (menu){
    deletePopup(el);
  }
  menu = popup;
  menu.style.posTop = el.offsetTop;
  menu.style.posLeft = 171;
  menu.style.display = '';
  menu.style.visibility='visible';
  menu.style.zIndex = 15;
  menu.onchange = adjustLocation(menu);
  hideItems("SELECT", menu);
  hideItems("IFRAME", menu);
}

function adjustLocation(menu){
  /*Constants*/
  var docBody = window.document.body;
  var docTop = docBody.scrollTop;
  var screenHeight = docBody.clientHeight;
  var MenuHeight = menu.offsetHeight;
  var MenuBottom = element_top(menu) + MenuHeight; 
  var difference = screenHeight - MenuHeight;
  var screenWidth = docBody.clientWidth;
  var MenuWidth = menu.offsetWidth;
  var cursorX = menu.screenX;
  if (MenuBottom - docBody.scrollTop >= screenHeight)/*below bottom*/{
    menu.style.posTop = (menu.style.posTop-(MenuHeight)+18);/*shift menu to go up*/
  }
  if ((element_top(menu) - docTop) < 0)/*above top*/{
    menu.style.posTop = 0;
    menu.style.top = (difference / 2) - (element_top(menu)) + docBody.scrollTop;
  }
  if (offsetLeft(menu) + MenuWidth > screenWidth || direction == 1)/*off edge*/{
    menu.style.posLeft = - MenuWidth + 5;
    direction = 1;
  }
  if (getTotalWidth(menu) < screenWidth && direction == 1)/*not off edge*/{
    direction = 0;
  }
}

function offsetLeft(el){
  x = el.offsetLeft
  for (e = el.offsetParent; e; e = e.offsetParent)/*Walking back up the structure to find the parent*/
  x += e.offsetLeft;
  return x
}

function getTotalWidth(el){
  x = el.offsetWidth
  for (e = el.offsetParent; e; e = e.offsetParent)/*Walking back up the structure to find the parent*/
  if (e.nodeName.toUpperCase() == 'TABLE' || e.nodeName.toUpperCase() == 'DIV'){
    x += e.offsetWidth;
  }
  return x
}

function removePopup(){
  deletePopup('');
}

function popup(el){
  var iSubMenuID
  timerId = window.setTimeout('doMenuz(' + el.id + ', ' + el.submenu + ')', 0);
}

function cancelPopup(){
  timerId = 0;
}

function delayRemove(){
  deletePopup();
}

function cancelDelay(){
}

function rollon(a){
  a.style.backgroundColor=RollOnColor;
  a.style.border = RollOnColor + ' solid 1px';
}

function rolloff(a){
  a.style.backgroundColor=RollOffColor;
  a.style.border = RollOffColor + ' solid 1px';
}

function hideItems(elmID, popup){
  for (i = 0; i < document.all.tags(elmID).length; i++){
    obj = document.all.tags(elmID)[i];
    if (! obj || ! obj.offsetParent) continue;
    // Find the element's offsetTop and offsetLeft relative to the BODY tag.
    objLeft   = obj.offsetLeft;
    objTop    = obj.offsetTop;
    objParent = obj.offsetParent;
    while (objParent.tagName.toUpperCase() != "BODY"){
      objLeft  += objParent.offsetLeft;
      objTop   += objParent.offsetTop;
      objParent = objParent.offsetParent;
    }
    //if (popup.style.posLeft > (objLeft + obj.offsetWidth) || objLeft > (popup.style.posLeft + result.offsetWidth))
    if (popup.style.posLeft > (objLeft + obj.offsetWidth) || objLeft > getTotalWidth(popup)){
    }
    else if (element_top(popup) > objTop + obj.offsetHeight){
    }
    else
    obj.style.visibility = "hidden";
  }
}

function showItems(elmID){
  for (i = 0; i < document.all.tags(elmID).length; i++){
    obj = document.all.tags(elmID)[i];
    if (! obj || ! obj.offsetParent)
    continue;
    obj.style.visibility = "";
  }
}

function element_top(el){
  var et = 0
  while (el){
    et += el.offsetTop
    el = el.offsetParent
  }
  return et
}

function setStyle(){
  //alert('RollOffColor=' + RollOffColor + ' in setStyle, global should be #f1f1f1');
  var sStyle = '<style>'+
    '.leftnav { background-color: ' + RollOffColor + '; }'+
    '.menuBar {background-color: ' + RollOffColor + '; border: ' + RollOffColor + ' solid 1px; width: 182px;}'+
    '.menuBarFont {font-weight: normal; font-size: 8pt; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;}'+
    '.menu {background-color: #ff0000; border:0 outset #f0f0f0;}'+
    '.menuTD {background-color: ' + RollOffColor + ';}'+
    '.subMenu {background-color: ' + RollOffColor + '; border: ' + RollOffColor + ' solid 1px;}'+
    '.floatingMenu {background-color: #cccccc;}'+
    '.middleColLine {width:139; background-color:#cccccc;}'+
    '</style>';
  return sStyle;
}
