var iTableNo = 0;
var iMenuNo = 0;
var strHand = "hand";

function ResetMenuHL(tblMenuTop) {
  var iSubMenu;
  for (iSubMenu = 0; iSubMenu < tblMenuTop.childNodes.length; iSubMenu++) {
    if (((tblMenuTop.childNodes[iSubMenu].tagName + "").toUpperCase() == "TR") &&
        ((tblMenuTop.childNodes[iSubMenu].className + "").substring((tblMenuTop.childNodes[iSubMenu].className + "").length - 1) == "H")) {
      tblMenuTop.childNodes[iSubMenu].className = (tblMenuTop.childNodes[iSubMenu].className + "").substring(0, (tblMenuTop.childNodes[iSubMenu].className + "").length - 1);
    }
    ResetMenuHL(tblMenuTop.childNodes[iSubMenu]);
  }
}

function ExpandMenu(trMenu, trTable) {
  ResetMenuHL(document.getElementById("tblMenu1"));
  if (trTable.style.display == "") {
    trTable.style.display = "none";
  }
  else {
    if (trTable.parentNode.childNodes != null) {
      for (iChild = 0; iChild < trTable.parentNode.childNodes.length; iChild++) {
        if ((trTable.parentNode.childNodes[iChild] != trTable) &&
            (trTable.parentNode.childNodes[iChild] != trMenu) &&
            (trTable.parentNode.childNodes[iChild].id != "")) {
          if ((trTable.parentNode.childNodes[iChild].id + "").substring(0, 5) == "trTbl") {
            trTable.parentNode.childNodes[iChild].style.display = "none";
          }
        }
      }
    }
    if ((trTable.childNodes[0].childNodes[0].innerHTML + "").substring(0, 15).toUpperCase().indexOf("<TR") >= 0) {
      trTable.style.display = "";
    }
  }
  if ((trMenu.className + "").substring((trMenu.className + "").length - 1) != "H") {
    trMenu.className += "H";
  }
}

function SetMenuBG(trMenuBG, iLevel, strType, iSubMenu) {
  trMenuBG.style.backgroundImage = "url('/images/menu_background" + ((iSubMenu == 0)?"":"") + strGBNumber + strType + (iLevel + 1) + ".jpg')";
  //alert(trMenuBG.style.backgroundImage);
}

function ShowMsg(strMsg) {
  window.status = strMsg;
}

function LoadPage(strURL, strTarget) {
  window.location.href=strURL;
//  aRef.target = strTarget;
//  aRef.href = strURL;
//  aRef.click();
}

function GetMenus(nodeParent, iLevel) {
  var nodeCurrent = nodeParent;
  var strResult = "";
  var iSubMenu;
  iTableNo++;
  strResult += "<table id=tblMenu" + iTableNo + " width='150' border=0 cellpadding=0 cellspacing=0";
  var bMainChildren = false;
  for (iChild = 0; iChild < nodeCurrent.childNodes.length; iChild++) {
    if ((nodeCurrent.childNodes[iChild].attributes != null) &&
        (nodeCurrent.childNodes[iChild].attributes.getNamedItem("TITLE") != null) &&
        (nodeCurrent.childNodes[iChild].attributes.getNamedItem("TITLE").value != "")) {
      bMainChildren = true;
      break;
    }
  }
  if (bMainChildren == true) {
    iMenuNo++;
  }
  strResult += ">\n";
  var iSubMenuUsed = 0;
  for (iSubMenu = 0; iSubMenu < nodeCurrent.childNodes.length; iSubMenu++) {
    if (nodeCurrent.childNodes[iSubMenu].tagName == "") {
      continue;
    }
    if ((nodeCurrent.childNodes[iSubMenu].attributes == null) ||
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TITLE") == null) ||
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TITLE").value == "")) {
      continue;
    }
    iMenuNo++;
    strMenuType = "";
    strClick = "";
    var bChildren = false;
    for (iChild = 0; iChild < nodeCurrent.childNodes[iSubMenu].childNodes.length; iChild++) {
      if ((nodeCurrent.childNodes[iSubMenu].childNodes[iChild].attributes != null) &&
          (nodeCurrent.childNodes[iSubMenu].childNodes[iChild].attributes.getNamedItem("TITLE") != null) &&
          (nodeCurrent.childNodes[iSubMenu].childNodes[iChild].attributes.getNamedItem("TITLE").value != "")) {
        bChildren = true;
        break;
      }
    }
    if (bChildren == false) {
      strResult += "<tr class=MenuItem";
    }
    else {
      strResult += "<tr class=MenuItemP";
      strMenuType = "P";
    }
    strClick = "ExpandMenu(this, document.getElementById(\"trTbl" + iTableNo + "\"));";
    strResult += " style=\"background-image: URL('/images/menu_background" + ((iSubMenuUsed == 0)?"":"") + strGBNumber + strMenuType + (iLevel + 1) + ".jpg')\"";
    if (iLevel > 0) {
      //strResult += " style='display:none'";
    }
    strResult += " id=trMenu" + iMenuNo;
    strResult += " onmouseover=\"JavaScript:";
    if ((nodeCurrent.childNodes[iSubMenu].attributes != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("MSG") != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("MSG").value != "")) {
      strResult += " ShowMsg(\'" + nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("MSG").value + "\');";
    }
    strResult += " SetMenuBG(this, " + iLevel + ", \'" + strMenuType + "HL\', " + iSubMenuUsed + ")\"";
    strResult += " onmouseout='JavaScript:";
    if ((nodeCurrent.childNodes[iSubMenu].attributes != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("MSG") != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("MSG").value != "")) {
      strResult += " ShowMsg(\"\");";
    }
    strResult += " SetMenuBG(this, " + iLevel + ",  \"" + strMenuType + "\", " + iSubMenuUsed + ")'";
    if ((nodeCurrent.childNodes[iSubMenu].attributes != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("URL") != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("URL").value != "")) {
      strClick += "LoadPage(\"" + nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("URL").value + "\",\"";
      if ((nodeCurrent.childNodes[iSubMenu].attributes != null) &&
          (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TARGET") != null) &&
          (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TARGET").value != "")) {
        strClick += nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TARGET").value;
      }
      else {
        strClick += "main";
      }
      strClick += "\")";
    }
    if (strClick != "") {
      strResult += " onclick='JavaScript:" + strClick + "'";
    }
    strResult += ">\n";
    strResult += "<td height=15px width='100%' style='padding-left:" + (5 + (10 * iLevel)) + "px;cursor:" + strHand + ";'>";
    if ((nodeCurrent.childNodes[iSubMenu].attributes != null) &&
        (nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TITLE") != null)) {
      strResult += nodeCurrent.childNodes[iSubMenu].attributes.getNamedItem("TITLE").value;
    }
    strResult += "</td></tr>\n<tr style='";
    if (bChildren == true) {
      strResult += "";
    }
    strResult += "display:none' id=trTbl" + iTableNo + "><td height=15px>";
    strResult += GetMenus(nodeCurrent.childNodes[iSubMenu], iLevel + 1);

    strResult += "</td>";
    strResult += "</tr>\n";
    iSubMenuUsed++;
  }
  if (bMainChildren == true) {
    if (iLevel == 0) {
      strResult += "<tr class=MenuBottom><td></td></tr>\n";
    }
  }
  strResult += "</table>\n";

  return strResult;
}
function ShowMenu(strMenuXML) {
  if (navigator.appName.indexOf("Explorer")>-1) {
    xmlMenu = new ActiveXObject("Microsoft.XMLDOM");
  }
  else {
    xmlMenu = document.implementation.createDocument("text/xml","",null);
    strHand = "pointer";
  }
  xmlMenu.async = false;
  try {
    xmlMenu.load(strMenuXML);
  }
  catch (e) {
    alert(e + "");
  }
  try {
    strMenuHTML = GetMenus(xmlMenu.documentElement, 0);
  }
  catch (e) {
    alert(e + ":" + xmlMenu + "");
  }
  
  try {
    document.getElementById("TP").innerHTML = strMenuHTML;
  }
  catch (e) {
    alert(e);
  }
}
