/****************** COMMON/BASE FUNCTIONS ******************/

/****************** BROWSER DETECTION ******************/
var isIE = navigator.userAgent.indexOf("MSIE") > -1;
//var isSafari = !isIE && navigator.userAgent.indexOf("Safari") > -1;
var safari2 = (navigator.userAgent.indexOf("Safari") != -1) && (navigator.userAgent.substr(navigator.userAgent.indexOf("Safari/")+7,1) < 5) ? true : false;
var safari3 = (navigator.userAgent.indexOf("Safari") != -1) && (navigator.userAgent.substr(navigator.userAgent.indexOf("Safari/")+7,1) >= 5) ? true : false;
var isSafari = safari2;
var isMozilla = !isIE && !safari2 && !safari3 && navigator.userAgent.indexOf("Gecko") > -1;
var Mac = (navigator.userAgent.indexOf("mac") != -1) || (navigator.userAgent.indexOf("Mac") != -1);
var NS6 = (navigator.userAgent.indexOf('Netscape') != -1 && navigator.userAgent.indexOf('Gecko') != -1);
if(isIE || isMozilla || isSafari) var dom = 1;

/****************** DEBUGGING ******************/
var debuggingON = false;
var debugWindow = null;
if (debuggingON) {
  debugWindow = window.open("", "debugWindow", 'status=1,scrollbars=1,resizable=1,width=1024,height=600');
  if (debugWindow.location.href.indexOf("/plateau/jsdebugger.jsp") == -1) {
    debugWindow.open("/plateau/jsdebugger.jsp", "debugWindow");
  }
}
function debug(msg) {
  debugWindow.debug(window.name + "(" + detailIndex + ") " + msg);
}
/****************** DEBUGGING ******************/
var safariLatency = 10;
function getIFrame(id) {
  var w = null;
  if (isSafari) {
    w = getElement(id);
  } else {
    w = window.frames[id];
  }
  return w;
}

var maxWaiting = 50;
function loadMacIFrame(bufferid, jspSource) {
  var w = getIFrame(bufferid);
  if(w != null && w.document != null && w.document.location != null && w.document.location.href != null){
     if (!w.document.location.href || w.document.location.href.indexOf(jspSource) == -1) {
       w.document.location.href = jspSource;
     }
     maxWaiting = 50;
  }else if (w != null && w.src != null){
       if (!w.src || w.src.indexOf(jspSource) == -1) {
       w.src = jspSource;
     }
     maxWaiting = 50;
  }else{
     if(maxWaiting > 0){
        setTimeout("loadMacIFrame(\"" + bufferid + "\",\"" + jspSource + "\");", safariLatency);
        maxWaiting --;
     }else{
        return;
     }
  }
}

function getElement(id) {
  return document.getElementById ? document.getElementById(id): document.all ? document.all[id] : null;
}

function getLayerStatesParameterValue() {
  var value = "";
  for (var id in layerStates) {
    if (layerStates[id] == 1) {
      value += id + ";";
    }
  }
  return value;
}

function constructUrlFromForm(baseUrl, form) {
  var url = baseUrl;
  var hasParameters = (url.indexOf("?") != -1);
  if (form) {
    for (var i=0; i<form.elements.length; i++) {
      var element = form.elements[i];
      if (hasParameters) {
        url += "&" + element.name + "=" + escape(element.value);
      } else {
        hasParameters = true;
        url += "?" + element.name + "=" + escape(element.value);
      }
    }
  }
  return url;
}

/****************** LAYER SUPPORT FUNCTIONS ******************/

function toggleStaticLayer(id) {
  var openedID = id+"openedDivID";
  var closedID = id+"closedDivID";
  var openedDiv = getElement(openedID);
  var closedDiv = getElement(closedID);
  if (openedDiv.style.display == "none") {
    // we're currently CLOSED, so OPEN
    openedDiv.style.display = "inline";
    closedDiv.style.display = "none";
    layerStates[id] = 1;
  } else {
    // we're currently OPENED, so CLOSE
    closedDiv.style.display = "inline";
    openedDiv.style.display = "none";
    layerStates[id] = 0;
  }
  notifyParentOfHeightChange();
  return false;
}

function toggleDynamicLayer(id, jspSource) {
  var openedID = id+"openedDivID";
  var closedID = id+"closedDivID";
  var openedDiv = getElement(openedID);
  var closedDiv = getElement(closedID);
  if (openedDiv.style.display == "none") {
    // we're currently CLOSED, so OPEN
    var div = getElement(id+"DivID");
    div.innerHTML = "";
    openedDiv.style.display = "inline";
    closedDiv.style.display = "none";
    startProxyLoading(id, jspSource);
    layerStates[id] = 1;
  } else {
    // we're currently OPENED, so CLOSE
    closedDiv.style.display = "inline";
    openedDiv.style.display = "none";
    layerStates[id] = 0;
    notifyParentOfHeightChange();
  }
  return false;
}

function toggleEmbeddedLayer(id, jspSource) {
  var bufferid = id+"Buffer";
  var buffer = getIFrame(bufferid);
  if (buffer) {
    var openedID = id+"openedDivID";
    var closedID = id+"closedDivID";
    var openedDiv = getElement(openedID);
    var closedDiv = getElement(closedID);
    if (openedDiv.style.display == "none") {
      // we're currently CLOSED, so OPEN
      openedDiv.style.display = "inline";
      closedDiv.style.display = "none";
      if (isSafari) {
        setTimeout("loadMacIFrame(\"" + bufferid + "\",\"" + jspSource + "\");", safariLatency);
      } else {
        if (!buffer.document.location.href || buffer.document.location.href.indexOf(jspSource) == -1) {
          buffer.document.location.href = jspSource;
        } else {
          notifyParentOfHeightChange();
        }
      }
      layerStates[id] = 1;
    } else {
      // we're currently OPENED, so CLOSE
      closedDiv.style.display = "inline";
      openedDiv.style.display = "none";
      layerStates[id] = 0;
      notifyParentOfHeightChange();
    }
  }
  return false;
}

function toggleStaticTableRowLayer(id, columns, classTranslationMapping) {
  toggleTableCellClasses(id, columns, classTranslationMapping);
  toggleTableRowLayer(id);
  return toggleStaticLayer(id);
}

function toggleDynamicTableRowLayer(id, columns, jspSource, classTranslationMapping) {
  toggleTableCellClasses(id, columns, classTranslationMapping);
  toggleTableRowLayer(id);

  var openedID = id+"openedDivID";
  var closedID = id+"closedDivID";
  var openedDiv = getElement(openedID);
  var closedDiv = getElement(closedID);
  if (openedDiv.style.display == "none") {
    // we're currently CLOSED, so OPEN
    var div = getElement(id+"DivID");
    div.innerHTML = "";
    openedDiv.style.display = "inline";
    closedDiv.style.display = "none";
    startProxyLoading(id, jspSource);
    layerStates[id] = 1;
  } else {
    // we're currently OPENED, so CLOSE
    closedDiv.style.display = "inline";
    openedDiv.style.display = "none";
    layerStates[id] = 0;
    notifyParentOfHeightChange();
  }
  return false;
}

function toggleEmbeddedTableRowLayer(id, columns, jspSource, classTranslationMapping) {
  toggleTableCellClasses(id, columns, classTranslationMapping);
  toggleTableRowLayer(id);
  return toggleEmbeddedLayer(id, jspSource);
}

function toggleTableCellClasses(id, columns, classTranslationMapping) {
  // figure out the class translation mapping
  var ctmArray = new String(classTranslationMapping).split(";");
  var ctMap = new Object();
  for (var i=0; i<ctmArray.length; i++) {
    var pair = ctmArray[i].split("=");
    ctMap[pair[0]] = pair[1];
    ctMap[pair[1]] = pair[0]; // for reversibility
  }

  // set all styles in the row to special style or revert to original using translation map
  for (var i=0; i<columns; i++) {
    var cellID = id + "row_" + i;
    var e = getElement(cellID);
    e.className = ctMap[e.className];
  }
}

function toggleTableRowLayer(id) {
  var dataRowID = id + "row_data";
  var rowElems = document.getElementsByName(dataRowID);
  var dataRow;
  if (rowElems.length >0){
    for(var i=0; i<rowElems.length; i++){
      dataRow = rowElems[i];
      if (dataRow.style.display == "none") {
        dataRow.style.display = "";
      } else {
        dataRow.style.display = "none";
      }
    }
  } else{
    dataRow = getElement(dataRowID);
    if (dataRow.style.display == "none") {
      dataRow.style.display = "";
    } else {
      dataRow.style.display = "none";
    }
  }
}


// the loadingLayers object is used to track which [dynamic] layers are in the *process* of loading
// loadingLayers[id] == 1 if loading, 0 or undefined if done loading or not loaded respectively
var loadingLayers = new Object();
function startProxyLoading(id, src) {
  var proxy = window.frames[id+"Proxy"];
  if (proxy) {
    loadingLayers[id] = 1;
    proxy.location.href = src;
    setTimeout("waitForProxyLoad('" + id + "')", 100);
  }
}

function waitForProxyLoad(id) {
  if (loadingLayers[id] == 1) {
    var div = getElement(id+"DivID");
  }
}

function notifyProxyLoadCompletion(id) {
  // proxy layer has loaded - copy html
  loadingLayers[id] = 0;
  var proxy = window.frames[id+"Proxy"];
  if (proxy) {
    var proxyDiv = proxy.document.getElementById(id);
    if (proxyDiv) {
      var divID = id+"DivID";
      var dest = getElement(divID);
      if (dest && typeof dest.innerHTML != "undefined") dest.innerHTML = proxyDiv.innerHTML;
      notifyParentOfHeightChange();
    }
  }
}

//  set the iframe on the current window to the size of the div inside the iframe with the same id
// this function sets both width and height.
function setIframeHeight(id) {
  var iframeName = id + "Buffer";
  var iframeWin = window.frames[iframeName];
  if (iframeWin) {
    var sourceDiv = iframeWin.document.getElementById ? iframeWin.document.getElementById(id): iframeWin.document.all ? iframeWin.document.all[id] : null;
    if (sourceDiv) {
      var iframeEl = getElement(iframeName);
      if (iframeEl) {
        var extra = 0;
        if (isSafari && sourceDiv.id == "detailWindow") extra = 20;
        var height = sourceDiv.offsetHeight + sourceDiv.offsetTop;
        var width = getIframeContentWidth(iframeEl, sourceDiv) ;
        if (debuggingON) debug("setting iframe height to " + height + " (sourceDiv.offsetHeight == " + sourceDiv.offsetHeight + ")");
        if (debuggingON) debug("setting iframe width to " + width + " (sourceDiv.offsetWidth == " + sourceDiv.offsetWidth + ")");
        if (height < 0) {
          if (debuggingON) debug("delaying setIframeHeight('" + id + "') until height calculation returns >= 0");
          setTimeout("setIframeHeight('" + id + "')", 100);
        } else {
          iframeEl.style.height = (height + extra) + "px";
          iframeEl.style.width = "100%";
          iframeEl.parentNode.style.width = "100%";
          if(width > iframeEl.offsetWidth)
            iframeEl.style.width = (width + extra) + "px";
        }
      } else if (debuggingON) debug("Could not find frame element " + iframeName);
    }
  }
  reconcileFrameWidths(embeddedWindowsArray);
}
function getIframeContentWidth(_iFrame, sourceDiv){
  var newWidth;
  if(isIE){
    newWidth=  sourceDiv.offsetWidth;
  }else {
    newWidth = getWidestTableWidth(_iFrame.contentDocument?_iFrame.contentDocument:_iFrame.document);
  }
  return newWidth;
}

function getWidestTableWidth(_document) {
  var widestWidth = 0;
  var _elems = _document.getElementsByTagName("TABLE");
    for (var i = 0; i < _elems.length; i++) {
      if (_elems[i].offsetWidth > widestWidth) {
        widestWidth = _elems[i].offsetWidth;
      }
    }
  return widestWidth;
}

// called by the parent of an iframe thru the iframe's onload event, OR possibly by the iframe contents as window.parent.notifyHeightChange(id)
// will resize the iframe from the iframe's body div's content height, then notify any parent detail windows of the height change
function notifyHeightChange(id) {
  if (isSafari) {
    // underlying data may not yet be ready
    setTimeout("setIframeHeight('" + id + "'); notifyParentOfHeightChange();", 50);
  } else {
    setIframeHeight(id);
    notifyParentOfHeightChange();
  }
}

var embeddedWindowsArray = new Array(0); //used with reconcileFrameWidths()

// place in body onload of page containing multiple embedded windows to ensure that all iframes on the page are the same width
    function reconcileFrameWidths(_embeddedWindowsArray){
      var widestFrameWidth = 0;
      if(_embeddedWindowsArray){
        for(var i = 0; i < _embeddedWindowsArray.length ; i++){
          var _elem = document.getElementById(_embeddedWindowsArray[i]);
// todo Don comparison below should compare to the value it will set
          if(_elem.offsetWidth > widestFrameWidth){
            if(isIE){
              widestFrameWidth = _elem.offsetWidth;
            } else {
              widestFrameWidth  = getWidestTableWidth(_elem.contentDocument?_elem.contentDocument:_elem.document);
            }
          }
        }
        for(var i = 0; i < _embeddedWindowsArray.length ; i++){
          var _elem = document.getElementById(_embeddedWindowsArray[i]);
          if(_elem.parentNode){
            var elementToResize = _elem.parentNode;
            elementToResize.style.width = widestFrameWidth;
          } else alert('cannot find parentNode in reconcileFrameWidths()');
        }
      }
    }

// if a layer (static/dynamic/embedded) changes in some way so as to affect its height (e.g. layer toggled, loaded, page changed),
// it should call this method.
// it essentially means that the CURRENT WINDOW has a layer that has changed height, and any containing iframes need
// to be resized accordingly.
// there are two possible containing iframe types: embedded window layer iframe, detail iframe
function notifyParentOfHeightChange() {
  // detail support
  if (isDetailWindow) setParentDetailWindowToDetailContentHeight();
  // layer support
  if (_currentLayerID != null && window.parent && window.parent.setIframeHeight) {
    if (isSafari) {
      setTimeout("window.parent.setIframeHeight('" + _currentLayerID + "');", safariLatency);
    } else {
      window.parent.setIframeHeight(_currentLayerID);
    }
  }
  if (window.parent && window.parent != window && window.parent.notifyParentOfHeightChange) {
    window.parent.notifyParentOfHeightChange();
  }
}

/****************** DETAIL WINDOW FUNCTIONS ******************/

function showBodyLayer() {
  if (debuggingON) debug("showBodyLayer()");
  var bodyDiv = getElement("bodyDiv" + detailIndex);
  if (bodyDiv) {
      bodyDiv.style.position = "static";
      bodyDiv.style.clip = "rect(auto)";
      document.body.style.marginTop = "";
  } else return false;
  return true;
}
function hideBodyLayer() {
  if (debuggingON) debug("hideBodyLayer()");
  var bodyDiv = getElement("bodyDiv" + detailIndex);
  if (bodyDiv) {
      bodyDiv.style.position = "absolute";
      bodyDiv.style.clip = "rect(0px 0px 0px 0px)";
      document.body.scrollTop = 0;
      document.body.style.marginTop = "0px";
    return true;
  }
  return false;
}
function showDetailLayer() {
  if (debuggingON) debug("showDetailLayer()");
  var detailRow = getElement("detailRow" + detailIndex);
  if (detailRow) {
    detailRow.style.display = "";
    return true;
  }
  return false;
}
function hideDetailLayer() {
  if (debuggingON) debug("hideDetailLayer()");
  var detailRow = getElement("detailRow" + detailIndex);
  if (detailRow) {
    detailRow.style.display = "none";
  } else return false;
  return true;
}
var counter = 0;
function setDetailLayerSrc(src, newDetailIndex) {
  if (debuggingON) debug("setDetailLayerSrc('" + src + "','" + newDetailIndex + "'), detailIndex == " + detailIndex);
  if (src && src != "") {
    if (src.indexOf("?") == -1) src += "?"; else src += "&";
    // note: if we have to pass detailIndex around, here's the code:   src += "detailIndex=" + newDetailIndex + "&uniqueUrlCounter=" + (counter++);
    // add a unique parameter to prevent browser caching of data in the iframe
    src += "uniqueUrlCounter=" + (counter++);
    if (debuggingON) debug("...new SRC == " + src);
  } else
    src = staticWebFileContextPath + "/images/blank.html";

  var bufferid = "detailWindowBuffer" + detailIndex;
  if (debuggingON) debug("...bufferid == " + bufferid);
  if (isSafari) {
    setTimeout("loadMacIFrame(\"" + bufferid + "\",\"" + src + "\");", safariLatency);
  } else {
    var win = getIFrame(bufferid);
    if (win) {
      win.location.href = src;
    } else return false;
  }
  return true;
}

function simulateClick(elId) {      
    var evt;
    var el = document.getElementById(elId);
    if (document.createEvent){
          evt = document.createEvent("MouseEvents");
          if (evt.initMouseEvent){
                evt.initMouseEvent("click", true, true, window,
                            0, 0, 0, 0, 0, false, false, false, false, 0, null);
          } else {
                evt = false;
          }
    }
    (evt)? el.dispatchEvent(evt):(el.click && el.click());
}

function focusHeading(elID){
heading = document.getElementById(elID);
if(isMozilla){
    this.location = heading.href;                 
}else{
    simulateClick(elID);
}
heading.focus();
}

function showDetailWindow(src) {
  if (debuggingON) debug("showDetailWindow('" + src + "')");
  if (hideBodyLayer()) {
    if (showDetailLayer()) {
      if (src) {
        var newDetailIndex = (!detailIndex || detailIndex == "") ? "0" : (detailIndex + "1");
        if (!setDetailLayerSrc(src, newDetailIndex)) {
          alert("buffer window not found");
        }
      }
    } else {
      alert("detail layer not found");
    }
  } else {
    alert("body layer not found");
  }
  return false;
}

function clearDetailWindow() {
  if (debuggingON) debug("clearDetailWindow()");
  if (!isIE) return true;  // incompatible
  var bufferid = "detailWindowBuffer" + detailIndex;
  var iframeEl = getElement(bufferid);
  if (iframeEl) {
    iframeEl.style.height = "0px";
    var win = getIFrame(bufferid);
    if (win) {
      win.location.href = staticWebFileContextPath + "/images/blank.html";
    } else return false;
  } else return false;
  return true;
}

function closeDetailWindow() {
  if (debuggingON) debug("closeDetailWindow()");
  if (clearDetailWindow()) {
    // reset the state of detail wnidow to hidden and this window's margin override
      if (showBodyLayer()) {
          if (hideDetailLayer()) {
              setParentDetailWindowToDetailContentHeight();
      } else alert("detailRow not found");
    } else alert("Could not find body layer to show");
  } else alert("Could not clear detail window - window not found");
  //notifyHeightChange(); //add if we need to have detail windows inside of IFrames
  return false;
}

function notifyParentOfDetailHeightChange(obj) {
  if (debuggingON) debug("notifyParentOfDetailHeightChange() called");
  setParentDetailWindowToDetailContentHeight(obj);
}
//This function also sets the ParentDetailWindow's width to the DetailContentWidth
function setParentDetailWindowToDetailContentHeight(obj) {
    if (debuggingON) debug("setParentDetailWindowToDetailContentHeight called: isDetailWindow == " + isDetailWindow);
    if (isDetailWindow) {
        // get height of the body div
        var height = 0;
        var sourceDivName = "bodyDiv" + detailIndex;
        var sourceDiv = getElement(sourceDivName);
        if (sourceDiv) {
            // only do this if the underlying sourceDiv is not hidden (safari is absolute, ie/moz offsetHeight == -1)
            if (sourceDiv.offsetHeight > 0 && sourceDiv.style.position != "absolute") {
                extra = 0;
                if (isSafari) extra = 100;
                height = sourceDiv.offsetHeight + sourceDiv.offsetTop + extra;
                width = getIframeContentWidth(window, sourceDiv) ;
                if (debuggingON) debug("...setParentDetailWindowToDetailContentHeight() calc'd width as " + width + " -height as " + height + " from sourceDiv " + sourceDiv.id);

                // inform parent
                if (window.parent && window.parent.setDetailWindowSize) {
                    if (isSafari) {
                        setTimeout("window.parent.setDetailWindowSize("+ width + ","  + height + ");", safariLatency);
                    } else {
                        window.parent.setDetailWindowSize(width, height, obj);
                        if (debuggingON) debug("...parent call to setDetailWindowSize() done");
                    }
                }
            }
        } else {
            alert("Your detail window lacks a body content div named " + sourceDivName);
        }
    }
}

function setDetailWindowSize(width, height, options) {
  if (debuggingON) debug("setDetailWindowSize("+ width + ","   + height + ") called: isDetailWindow == " + isDetailWindow);
  // apply height to the detail window iframe
  var iframeName = "detailWindowBuffer" + detailIndex;
  var iframeEl = getElement(iframeName);
  if (iframeEl) {
    if (debuggingON) debug("...setting iframe height to " + height);
    iframeEl.style.height = height + "px";
    if (debuggingON) debug("...setting iframe width to " + width);
    if(options){
        //apply percentage width
        if(options.width)
        iframeEl.style.width = options.width;
    }else{
        //apply pixel width
        iframeEl.style.width = width + "px";
    }
  } else alert("iframeEl was null for " + iframeName);

  // bubble up ancestor tree
  if (window.parent && window.parent != window && window.parent.setDetailWindowSize) {
    if (debuggingON) debug("...calling parent.setDetailWindowSize()...");
    window.parent.setDetailWindowSize(width, height);
  }
  if (debuggingON) debug("...setDetailWindowSize("+width + "," + height + ") done");
}

/****************** MENU SUPPORT ******************/
var howmanymenus = 0;
var menunames;
var stopshow = 0;
var noway = 0;
var timerID = null
var timerID2 = null
var timerID3 = null
var whatsopen = "none";
var menutop = 0;
var menuleft = 0;
var cellpad=2;
var buttonheight = 21;
var toggleswitch = 1;
var menusInitialized = false;

if(Mac || isIE || NS6 || isMozilla || isSafari)noway = 0;
else noway = 1;

function initMenus(menucount) {
  howmanymenus = menucount;
  menunames = new Array(howmanymenus);
}

function startup() {
  if(isSafari){
//  fix to Trackers 10098 & 10099 - this function is needed as a workaround for a Safari issue
//  where, while opening/loading a window,
//  you are not able to interact with that window's elements within that same event or function.
//  You can act on that window in a setTimeout call as a workaround.
    setTimeout("startupImpl();", safariLatency);
  } else {
    startupImpl();
  }
}

function startupImpl() {
  if (noway) return;
  menusInitialized = true;
  if (isIE && !Mac) {
    for (var i = 0; i <= howmanymenus; i++) {
      var menuTableStr = menunames[i] + 'Table';
    }
  }
  getpositions();
  eval("document.getElementById('menu" + selectedMenuIndex + "').style.visibility = 'visible';");
  whatsopen = "menu"+selectedMenuIndex;
}

function toggle(whatmenu, isMain) {
  if (noway) return;
  if(!menusInitialized) return;
  getpositions();
  if (isMain == 1) {
      if (timerID) clearTimeout(timerID);
      if (whatsopen == "none") {
          if (timerID) clearTimeout(timerID);
            timerID = setTimeout("replacePopup('" + whatmenu + "');", 500);
          jcsmenu = whatmenu.substr(4, 1);
          if(isSafari){top.window.window.scrollX = top.window.scrollX+100}
          isMain = 0;
      }
      if (whatsopen == whatmenu) isMain = 0;

      if (whatsopen != whatmenu && whatsopen != "none") {
         if (timerID) clearTimeout(timerID);
            timerID = setTimeout("replacePopup('" + whatmenu + "');", 500);
          jcsmenu1 = whatmenu.substr(4, 1);
          if(isSafari){top.window.scrollX = top.window.scrollX+100}
          isMain = 0;
      }
      if (timerID2) clearTimeout(timerID2);
      timerID2 = setTimeout("hidePopup('" + whatmenu + "');", 3000);
      return;
  }
  if (isMain == 0 && (whatsopen != "none" && isMain != 1)) hidePopup(whatmenu);
}

function toggleSelectedLinkText(whatmenu, newClass){
  var test  =  selectedLink.substring(0, selectedLink.length-4);
  if(whatmenu == test){
    document.getElementById(selectedLink).className = newClass;
  }
}

function hidePopup(whatmenu) {
if(!menusInitialized) return;
  if (noway) return;
  eval("document.getElementById('" + whatmenu + "').style.visibility = 'hidden';");
  eval("document.getElementById('menu" + selectedMenuIndex + "').style.visibility = 'visible';");
  whatsopen="menu"+selectedMenuIndex;
  return;
}
function replacePopup(whatmenu) {
if(!menusInitialized) return;
  if (noway) return;
  if(whatmenu == whatsopen) return;
  if (whatmenu != "none") {
      eval("document.getElementById('" + whatmenu + "').style.visibility = 'visible';");
  }
  eval("document.getElementById('" + whatsopen + "').style.visibility = 'hidden';");
  whatsopen=whatmenu;
  return;
}

function toggle2(whatmenu) {
  if (noway) return;
  if(!menusInitialized) return;
      if (toggleswitch == 0) {
          eval("document.getElementById('" + whatmenu + "').style.visibility = 'visible';");
          toggleswitch = 1;
          whatsopen = whatmenu;
          return;
      }
      if (toggleswitch == 1) {
          eval("document.getElementById('" + whatmenu + "').style.visibility = 'hidden';");
          toggleswitch = 0;
          whatsopen = "none";
          jcsmenuoff22 = whatmenu.substr(4, 1);
          if(jcsmenuoff22 != selectedMenuIndex) {
            document.getElementById("MenuGroup" + jcsmenuoff22).className ='NavCell';
          }
          else {
            document.getElementById("MenuGroup" + jcsmenuoff22).className ='NavCellOn';
          }
          return;
      }
}

function goSub(where, what, tgt) {
  stopshow = -1;
  clearTimeout(timerID);
  clearTimeout(timerID2);
  if (tgt == '') var wintgt = "self";
  else var wintgt = tgt;
  if (tgt == '') whereto = "location.href='" + where + "'";
  if (tgt == 'new') whereto = "window.open('" + where + "','" + tgt + "');";
  if (tgt == 'top' ) whereto = "window.top.location.href='" + where + "'";
  if (tgt != 'top' && tgt != 'new' && tgt != '') whereto = "parent." + tgt + ".location.href='" + where + "'";
  setTimeout("eval(whereto)", 550);
}

function getStylesheetRule(ruleIndex){
  var ssRule = "ssRule null";
  var retVal = "retVal null";
  if(isIE){
    ssRule = document.styleSheets[0].rules;
  }  else{
    ssRule =document.styleSheets[0].cssRules;
  }
  for (i=0; i<2; i++){
    if(ssRule[i].selectorText.toLowerCase() == ruleIndex.toLowerCase()){
      retVal =  ssRule[i];
      return retVal;
    }
  }
  return retVal;
}

function getpositions() {
  if (noway) return;
  if(dom){
    var logoOffset = document.getElementById('logoTable').offsetHeight;
    var ssRule = getStylesheetRule('body');
    var bodyTopMargin = ssRule.style.marginTop;
    bodyTopMargin = parseInt(bodyTopMargin.substring(0, (bodyTopMargin.length-2)));
    menutop = document.getElementById('navbarBottom').offsetHeight+1;
    menutop += bodyTopMargin + logoOffset;
      for (var i = 0; i <= howmanymenus; i++) {
          menuleft = document.getElementById('MenuGroup'+(i+1)).offsetHeight+15;
          eval("document.getElementById('" + menunames[i] + "').style.left=" + menuleft);
          if(isSafari) {
            eval("document.getElementById('" + menunames[i] + "').style.top=" + (menutop+9));
          }
          else {
            eval("document.getElementById('" + menunames[i] + "').style.top=" + menutop);
          }
          if (NS6) menuleft += cellpad;
      }
  }
}

function killMenu(whatwait, killtime) {
  if (noway) return;
  if (stopshow == -1)return;
  if (timerID) clearTimeout(timerID);
  if (timerID2) clearTimeout(timerID2);
  timerID = setTimeout("hidePopup('" + whatwait + "');", killtime);
}

function checkMenu() {
  if (noway) return;
  if (stopshow == -1) return;
  if (timerID) clearTimeout(timerID);
  if (timerID2) clearTimeout(timerID2);
}
/******************** FLYOUT SUPPORT ************************/
   var flyoutObj;              //the object that can fly out
   var flyoutShowing = false;  //indicates if the menu is showing or not
   var flyoutMax = 0;          //the maximum position for the menu (opened will always be at 0)
   var flyoutMin = -180;       //a default minimum position for the menu (docked left value)
   var flyoutLoc = -180;       //a default position for the menu
   var timer=0;                //the timer used for setTimeout calls
   var increment = 30;         //the number of pixels the menu moves at a time
   var menuSpeed = 20;        //the timeout vale between invremental movements

    if(isMozilla){
      increment = 80;
    }

   // call from body onload initializes the values and ensures that the menu gets lined up properly with the left side of the screen.
   // param menuElementID = the div that will be the menu
   function initFlyout(menuElementID, tabTextElementID, flyoutTop){

     flyoutObj = document.getElementById(menuElementID);
     flyoutObj.tabTextElementID = tabTextElementID;
     flyoutShowing = false;
     flyoutMax = 0;
     flyoutMin = -1*document.getElementById('leftCell').offsetWidth;
     flyoutLoc = -1*document.getElementById('leftCell').offsetWidth;
     document.getElementById('Flyout').style.left=flyoutLoc;
     document.getElementById('Flyout').style.top=flyoutTop;
     if(isIE) {
       var flyoutIFrameLeftCellStyle = document.getElementById('FlyoutIFrameLeftCell').style;
       flyoutIFrameLeftCellStyle.left=0;
       flyoutIFrameLeftCellStyle.top=0;
       flyoutIFrameLeftCellStyle.zIndex=-1;
       flyoutIFrameLeftCellStyle.height=1*document.getElementById('leftCell').offsetHeight;
       flyoutIFrameLeftCellStyle.width=1*document.getElementById('leftCell').offsetWidth;
       var tabCornerheight = 7;
       var flyoutIFrameFlyoutTabStyle = document.getElementById('FlyoutIFrameFlyoutTab').style;
       flyoutIFrameFlyoutTabStyle.left=0 + 1*document.getElementById('leftCell').offsetWidth;
       flyoutIFrameFlyoutTabStyle.top=tabCornerheight;
       flyoutIFrameFlyoutTabStyle.zIndex=-1;
       flyoutIFrameFlyoutTabStyle.height=1*document.getElementById('FlyoutTabCell').offsetHeight-tabCornerheight*2;
       flyoutIFrameFlyoutTabStyle.width=1*document.getElementById('rightCell').offsetWidth;
     }
     timer=0;
     document.getElementById('Flyout').style.visibility='visible';
   }

   // determines if the menu needs to be opened or closed and initiates the opening or closing
     function flyout(myElemID){
     var myElem = document.getElementById(myElemID);
       if(flyoutShowing == true){
         flyoutHide(myElemID, flyoutObj.tabTextElementID);
         flyoutShowing = false;
       } else {
         flyoutShow(myElemID, flyoutObj.tabTextElementID);
         flyoutShowing = true;
       }
     }

     //changes the text of the menu so that it says 'close' when showing and displays the menu
     function flyoutShow(myElemID, tabTextElementID){
       myElem = document.getElementById(myElemID);
       document.getElementById(tabTextElementID).innerHTML = closeHtml;
       moveOut(myElemID);
       if(isIE){
         document.getElementById(myElemID+"IFrameFlyoutTab").style.visibility='visible';
         document.getElementById(myElemID+"IFrameLeftCell").style.visibility='visible';
         moveOut(myElemID+"IFrameLeftCell");
         moveTabFrame(myElemID+"IFrameFlyoutTab");
       }
     }

     //changes the text of the menu so that it says 'open' when hidden and hides the menu
     function flyoutHide(myElemID, tabTextElementID){
       myElem = document.getElementById(myElemID);
       document.getElementById(tabTextElementID).innerHTML = openHtml;
       moveIn(myElemID);
       if(isIE){
         moveIn(myElemID+"IFrameLeftCell");
         moveTabFrame(myElemID+"IFrameFlyoutTab");
       }
     }

    //does the actual showing of the menu based upon initialization settings above
     function moveOut(elemToMoveID, tab){
       var elemToMove = document.getElementById(elemToMoveID);
       if(flyoutLoc < flyoutMax - increment){
         flyoutLoc += increment;
         elemToMove.style.left = flyoutLoc;
         timer = setTimeout("moveOut('" + elemToMoveID +"')", menuSpeed);
       } else {
         elemToMove.style.left = flyoutMax;
       }
     }

     //does the actual hiding of the menu based upon initialization settings above
     function moveIn(elemToMoveID, tab){
       var elemToMove = document.getElementById(elemToMoveID);
       var min = flyoutMin;
       if(flyoutLoc > flyoutMin + increment){
         flyoutLoc -= increment;
         elemToMove.style.left = flyoutLoc;
         timer = setTimeout("moveIn('" + elemToMoveID +"')", menuSpeed);
       } else {
         elemToMove.style.left = flyoutMin;
       }
     }

     function moveTabFrame(elemToMoveID, tab){
       var elemToMove = document.getElementById(elemToMoveID);
       elemToMove.style.left = document.getElementById('rightCell').offsetLeft;
     }

//need to make this work in safari
var ie = navigator.appName.indexOf('Microsoft')!=-1;
var ns = navigator.appName.indexOf('Netscape')!=-1 && navigator.userAgent.indexOf('Netscape')!=-1;
var safari = isSafari;
var moz = !ie && !ns && !safari2 && !safari3;
var firefox = moz &&  navigator.userAgent.indexOf('Firefox');

var enabletip=false;
var ContentInfo = "";
var curX = 0;
var curY = 0;
var offsetX=-100;
var offsetY=-100;
var offsetfromcursorX=3;
var offsetfromcursorY=7;
var freeze=0;
var tooltip, tooltipiframe, detailPage, divId, detailDivHeight;

// use this in tag onMouseover="tip('Content goes here', 'Style', maxWidth);" onMouseout="hidetip()"
function tip(THead1, styleClass, maxWidthOverride){
    enabletip=true;
    var tipDocument=document;
    if (tipDocument.getElementById("dhtmltooltip") == null) {
        var tipPage = parent;
        tipDocument = tipPage.document;
        while (tipDocument.getElementById("dhtmltooltip") == null && tipPage.parent!=null) {
            tipPage = tipPage.parent;
            tipDocument = tipPage.document;
        }    
    }    
    
    tooltip=tipDocument.getElementById("dhtmltooltip");
    tooltipiframe=tipDocument.getElementById("frm");
    ContentInfo = '<table id="tiptable">'+
                  '<tr><td class="'+styleClass+'">' + decodeURIComponent(THead1) + '</td></tr>' +
                  '</table>';
    tooltip.innerHTML=ContentInfo;
    tooltiptable=tipDocument.getElementById("tiptable");
    tooltiptable.style.whiteSpace="nowrap";
    if(typeof maxWidthOverride != "undefined"){
      if(tooltiptable.offsetWidth > maxWidthOverride){
        tooltiptable.style.whiteSpace="normal";
        tooltiptable.style.width=maxWidthOverride;
        tooltip.style.width=tooltiptable.offsetWidth+15;
      }
      else{
        tooltip.style.width=tooltiptable.offsetWidth+15;
      }
    }
    else{
      tooltip.style.width=tooltiptable.offsetWidth+15;
    }
    document.onmousemove=positiontip;
    return false;
}

function calculateOffsetX(){
  if(ie) offsetX = top.screenLeft;
  else   offsetX = top.screenX;
}

function calculateOffsetY(){
  if(ie) offsetY = top.screenTop;
  else{
    var toolbarHeight = top.outerHeight - top.innerHeight;
    offsetY = top.screenY + toolbarHeight;
  }
}

function calculateToolTipXLocation(curX){
  var displaceXforNN = -2;
  if(ie) return curX - offsetX + top.document.body.scrollLeft + offsetfromcursorX;
  else   return curX - offsetX + top.scrollX + displaceXforNN + offsetfromcursorX;
}

function calculateToolTipYLocation(curY){
  var displaceYforNN = 30;
  if(ie) return curY - offsetY  + top.document.body.scrollTop + offsetfromcursorY;
  else   return curY - offsetY  + top.scrollY  + displaceYforNN + offsetfromcursorY;
}

function calculateOffsets(){
  calculateOffsetX();
  calculateOffsetY();
}

function positiontip(e){
  calculateOffsets();
  if(enabletip && freeze==0){
    if(!ie){
      curX=e.screenX;
      curY=e.screenY;
    }
    else{
      curX=event.screenX;
      curY=event.screenY;
    }
    //Find out how close the mouse is to the edges of the window
    var winwidth = ie&&!window.opera? top.document.body.clientWidth : top.innerWidth-20;
    var winheight = ie&&!window.opera? top.document.body.clientHeight : top.innerHeight-20;
    var rightedge = winwidth - (curX - offsetX);
    var leftedge = curX - offsetY;
    var bottomedge = winheight - (curY - offsetY);
    var topedge = curY - offsetY;
    var locY = winheight - curY + top.scrollY + 50;
    var locX = curX + offsetfromcursorX;

    if(!safari){
      //if the horizontal distance isn't enough to accomodate the width of the context menu
      if(rightedge < tooltip.offsetWidth){
        if(leftedge > tooltip.offsetWidth){
          tooltip.style.left = calculateToolTipXLocation(curX) - (tooltip.offsetWidth + offsetfromcursorX*2);
        }
        else{
          tooltip.style.left = calculateToolTipXLocation(curX);
        }
      }
      else{
        tooltip.style.left = calculateToolTipXLocation(curX);
      }
      //if the vertical distance isn't enough to accomodate the height of the context menu
      if(bottomedge < tooltip.offsetHeight){
        if(topedge > tooltip.offsetHeight){
          tooltip.style.top = calculateToolTipYLocation(curY) - (tooltip.offsetHeight + offsetfromcursorY*2);
        }
        else{
          tooltip.style.top = calculateToolTipYLocation(curY);
        }
      }
      else{
        tooltip.style.top = calculateToolTipYLocation(curY);
      }
    }
    else{
    //if the horizontal distance isn't enough to accomodate the width of the context menu
      if((locX + tooltip.offsetWidth) > winwidth + top.scrollX){
        if(locX > tooltip.offsetWidth){
          tooltip.style.left = locX - (tooltip.offsetWidth+offsetfromcursorX*2);
        }
        else{
          tooltip.style.left = locX;
        }
      }
      else{
        tooltip.style.left = locX;
      }
      //if the vertical distance isn't enough to accomodate the height of the context menu
      if((locY + tooltip.offsetHeight) > winheight + top.scrollY){
        if(locY > tooltip.offsetHeight){
          tooltip.style.top = locY - (tooltip.offsetHeight+offsetfromcursorY*2);
        }
        else{
          tooltip.style.top = locY;
        }
      }
      else{
        tooltip.style.top = locY;
      }
    }
    tooltipiframe.style.top = tooltip.style.top;
    tooltipiframe.style.left = tooltip.style.left;
    tooltipiframe.style.width = tooltip.offsetWidth;
    tooltipiframe.style.height = tooltip.offsetHeight;
    tooltipiframe.style.zIndex = tooltip.style.zIndex;
    tooltipiframe.style.visibility="visible";
    tooltip.style.visibility="visible";
    freeze = 1;
  }
}

function hidetip(){
  enabletip=false;
  freeze = 0;
    if (tooltip != null) {
      tooltip.style.visibility="hidden";
      tooltipiframe.style.visibility="hidden";
      tooltip.innerHTML = "";
      tooltip.style.width="0px";
      tooltipiframe.style.width="0px";
    }
}

//  fix to Tracker 10086 - this function is needed as a workaround for a Safari issue
//  where, if you display a hidden IFrame as part of an event or function,
//  you are not able to interact with that IFrame's elements within that same event or function.
//  You can act on that IFrame in a setTimeout call as a workaround.
function submitHTMLForm( formName ){
  var submitCall = formName + ".submit()";
  if(isSafari){
    setTimeout( submitCall  , safariLatency);
  } else {
    eval(submitCall);
  }
}

function showRowElement(elem) {
    if (isSafari) {
      elem.style.display = 'table-row';
    } else {
      elem.style.display = 'inline';
    }
  }

/*
     The maintainHeaderOnScroll function will keep the header at the top of the table when scrolling.
     ex. on manager_dashboard.jsp
     This is done by duplicating the THEAD section of the table
       and leaving the duplicated DIV at the top of the DIV that contains the table.
     To use, set the id of your TABLE's THEAD to the value if theadID passed in and
       wrap your TABLE in a div whose ID is equal to scrollingDivID.
     theadID = ID of the THEAD to clone
     scrollingDivID = the id of the DIV wrapping the scrolling TABLE
     floatTableID = will be the ID of the floating DIV that is auto-created
     floatDivID = will be the ID of the TABLE that is auto-created in the floating DIV
    */
function maintainHeaderOnScroll(theadID, scrollingDivID, floatTableID, floatDivID) {
  var staticThead = document.getElementById(theadID);
  var scrollingDiv = document.getElementById(scrollingDivID);
  var floatTable = document.getElementById(floatTableID);
  var floatDiv = document.getElementById(floatDivID);
  var headerThere = floatDiv != null;
  if (!headerThere) { //don't create a new one if it already exists
    //create a floating DIV element and add it to scrolling DIV
    floatDiv = document.createElement('DIV');
    floatDiv.id = floatDivID;
    floatDiv.style.position = 'absolute';
    floatDiv.style.visibility = 'hidden';
    floatDiv.style.border = 0;
    scrollingDiv.appendChild(floatDiv);
    //create a TABLE element and add it to floating DIV element
    floatTable = document.createElement('TABLE');
    floatTable.id = floatTableID;
    floatDiv.appendChild(floatTable);
    floatTable.className = "DataTable";
    floatTable.style.border = 0;
    floatTable.style.borderCollapse = 'collapse';
    //copy the THEAD from the table and add it to the TABLE element
    var copy_thead = staticThead.cloneNode(true);
    floatTable.appendChild(copy_thead);
    floatDiv.style.position = 'absolute';
    floatDiv.style.visibility = 'visible';
    //copy TH widths from original THEAD element to floating DIVs TH elements so they match
    var lowerTHs = staticThead.getElementsByTagName('TH');
    var staticTHs = floatDiv.getElementsByTagName('TH');
    //account for horizontal scrolling
    if (!isIE) {
      floatDiv.style.width = staticThead.parentNode.offsetWidth;
    }
    for (var i = 0; i < lowerTHs.length; i++) {
      staticTHs[i].style.width = lowerTHs[i].offsetWidth - lowerTHs.length;
    }
  }
  //always keep the table header at the top of the table unless scrolled to top
  if (scrollingDiv.scrollTop <= 5) {
    floatDiv.style.visibility = 'hidden';
  } else {
    floatDiv.style.visibility = 'visible';
  }
  floatDiv.style.top = 0 + scrollingDiv.scrollTop;
  floatDiv.style.left = 1;
}

function deleteFloatDiv(scrollingDivID, floatDivID) {
  var scrollingDiv = document.getElementById(scrollingDivID);
  if (scrollingDiv != null) {
    var chNode;
    if(isIE){
      chNode = scrollingDiv.children(floatDivID)
    } else {
      var chNodes = scrollingDiv.childNodes;
      for(var i=0; i<chNodes.length; i++){
        if(floatDivID == chNodes[i].id){
        chNode = chNodes[i];
        }
      }
    }

    if (chNode != null) {
      scrollingDiv.removeChild(chNode);
    }
  }
}


function doNotSubmit(e) {
    var characterCode;
    if (e && e.which) {
        e = e;
        characterCode = e.which;
    } else {
        e = event
        characterCode = e.keyCode;
    }
    if (characterCode == 13 || characterCode == 3) {
        return false;
    } else {
        return true;
    }
}

function doDetailWindowOnResize() {
	var bodyDiv = getElement("bodyDiv" + detailIndex);
	var windowWidth = getWindowWidth();

	if (bodyDiv) {
		bodyDiv.style.width = windowWidth;
	}
	notifyParentOfDetailHeightChange();
}

function doDetailWindowOnLoad() {
	top.window.onresize = doDetailWindowOnResize;   //non-IE
	top.document.body.onresize = doDetailWindowOnResize;  //IE
	doDetailWindowOnResize();
}

function getWindowWidth() {
	var myWidth = 0;
	if (typeof (top.window.innerWidth) == "number") {
		    //Non-IE
		myWidth = top.window.innerWidth;
	} else {
		if (top.document.documentElement && top.document.documentElement.clientWidth) {
		    //IE 6+ in 'standards compliant mode'
			myWidth = top.document.documentElement.clientWidth;
		} else {
			if (top.document.body && top.document.body.clientWidth) {
		    //IE 4 compatible
				myWidth = top.document.body.clientWidth;
			}
		}
	}
	return myWidth;
}

function getWindowHeight() {
	var myHeight = 0;
	if (typeof (top.window.innerHeight) == "number") {
		    //Non-IE
		myHeight = top.window.innerHeight;
	} else {
		if (top.document.documentElement && top.document.documentElement.clientHeight) {
		    //IE 6+ in 'standards compliant mode'
			myHeight = top.document.documentElement.clientHeight;
		} else {
			if (top.document.body && top.document.body.clientHeight) {
		    //IE 4 compatible
				myHeight = top.document.body.clientHeight;
			}
		}
	}
	return myHeight;
}