// A reference to the currently-displayed popup window
var mdivPopup = null;

/*******************************************************************************
 * Displays a popup window with the specified HTML contents.  The anchorElement
 * parameter specifies the element to which the popup is positioned.
 ******************************************************************************/
function descPopup(anchorElement, innerHTML) {
   try {

      // Remove any existing popups
      if (mdivPopup != null)
         forceHidePopup();

      // Create the popup
      mdivPopup = document.createElement('div');
      mdivPopup.style.position = 'absolute';
      mdivPopup.style.zIndex = 999999
//      mdivPopup.style.backgroundColor = 'infobackground';
      mdivPopup.style.backgroundColor = '#FFFFCC';
      mdivPopup.style.color = 'black';
      mdivPopup.style.borderStyle = 'solid';
      mdivPopup.style.borderWidth = '1px';
      mdivPopup.style.marginWidth = '4px';
      mdivPopup.style.borderColor = '#586C7E';
      mdivPopup.style.fontSize = '10px';
      mdivPopup.style.fontFamily = 'Verdana, Arial, sans-serif';
      mdivPopup.style.width = '230px';
      mdivPopup.onmouseout = hidePopup;

      // Insert the content of the popup
      mdivPopup.innerHTML += "&nbsp;" + innerHTML + "&nbsp;";

      // Position the popup
      mdivPopup.style.top = getAbsoluteTop(anchorElement) + anchorElement.offsetHeight;
      mdivPopup.style.left = getAbsoluteLeft(anchorElement) - 230;

      // Show the popup
      document.body.appendChild(mdivPopup);
   }
   catch (x) {}
}


/*******************************************************************************
 * Hides a popup window that was shown via the showPopup function.  If the mouse
 * cursor is over the popup window, it is not hidden.
 ******************************************************************************/
function hidePopup() {
   try {
      if (mdivPopup != null) {
         // if (!isUnderCursor(mdivPopup))
            forceHidePopup();
      }
   }
   catch (x) {}
}


/*******************************************************************************
 * Hides a popup window that was shown via the showPopup function, regardless of
 * whether the cursor is still over the popup window.
 ******************************************************************************/
function forceHidePopup() {
   try {
      if (mdivPopup != null) {
         document.body.removeChild(mdivPopup);
         mdivPopup = null;
      }
   }
   catch (x) {}
}


/*******************************************************************************
 * Provides a safe way to obtain an element reference.
 ******************************************************************************/
function safeGetElement(elementID) {
   try {
      return document.getElementById(elementID);
   }
   catch (x) {
      return null;
   }
}


/*******************************************************************************
 * Toggles the display property of the specified HTML element
 ******************************************************************************/
function toggleDisplay(element) {
   try {
      element.style.display = element.style.display == 'none' ? '' : 'none';
   }
   catch (x) {}
}


/*******************************************************************************
 * Toggles the visibility property of the specified HTML element
 ******************************************************************************/
function toggleVisibility(element) {
   try {
      element.style.visibility = element.style.display == 'visible' ? 'hidden' : 'visible';
   }
   catch (x) {}
}


/******************************************************************************
 * Returns true if the mouse cursor is currently over the specified element;
 * otherwise, returns false.
 *****************************************************************************/
function isUnderCursor(element) {
   try {
      var intTop = getAbsoluteTop(element) - document.body.scrollTop;
      var intLeft = getAbsoluteLeft(element) - document.body.scrollLeft;
      var intBottom = intTop + element.offsetHeight + 1;
      var intRight = intLeft + element.offsetWidth + 1;

      if (((event.clientX >= intLeft) && (event.clientX <= intRight))
      && ((event.clientY >= intTop) && (event.clientY <= intBottom)))
         return true;
      else
         return false;
   }
   catch (x) {}
}


/******************************************************************************
 * Returns the absolute position of the left edge of the specified element.
 *****************************************************************************/
function getAbsoluteLeft(element) {
   try {
      var intLeft = 0;
      var objParent = element;

      do {
         intLeft += objParent.offsetLeft;
         objParent = objParent.offsetParent;
      }
      while (objParent != null);

      return intLeft;
   }
   catch (x) {}
}


/******************************************************************************
 * Returns the absolute position of the top edge of the specified element.
 *****************************************************************************/
function getAbsoluteTop(element) {
   try {
      var intTop = 0;
      var objParent = element;

      do {
         intTop += objParent.offsetTop;
         objParent = objParent.offsetParent;
      }
      while (objParent != null);

      return intTop;
   }
   catch (x) {}
}


/******************************************************************************
 * Hides all windowed elements that are beneath the specified element.
 *****************************************************************************/
function hideOverlappingWindowedElements(element) {
   hideOverlappingElements(element, "select");
   hideOverlappingElements(element, "applet");
   hideOverlappingElements(element, "iframe");
   hideOverlappingElements(element, "object");
}


/******************************************************************************
 * Makes all windowed elements visible.
 *****************************************************************************/
function showWindowedElements() {
   showElements("select");
   showElements("applet");
   showElements("iframe");
   showElements("object");
}


/******************************************************************************
 * Hides all elements with the specified tagName that that overlap the
 * specified element.
 *****************************************************************************/
function hideOverlappingElements(element, tagName) {
   try {
      var intElementTop = getAbsoluteTop(element) - document.body.scrollTop;
      var intElementLeft = getAbsoluteLeft(element) - document.body.scrollLeft;
      var intElementBottom = intElementTop + element.offsetHeight;
      var intElementRight = intElementLeft + element.offsetWidth;

      var arrElements = document.getElementsByTagName(tagName);
      for (var i = 0; i < arrElements.length; i++) {
         var intSelectTop = getAbsoluteTop(arrElements[i]) - document.body.scrollTop;
         var intSelectLeft = getAbsoluteLeft(arrElements[i]) - document.body.scrollLeft;
         var intSelectBottom = intSelectTop + arrElements[i].offsetHeight;
         var intSelectRight = intSelectLeft + arrElements[i].offsetWidth;

         if (((intElementTop <= intSelectBottom) && (intElementBottom >= intSelectTop))
         && ((intElementLeft <= intSelectRight) && (intElementRight >= intSelectLeft)))
            arrElements[i].style.visibility = "hidden";
      }
   }
   catch (x) {}
}


/******************************************************************************
 * Makes all elements with the specified tagName visible.
 *****************************************************************************/
function showElements(tagName) {
   try {
      var arrElements = document.getElementsByTagName(tagName);
      for (var i = 0; i < arrElements.length; i++) {
         arrElements[i].style.visibility = "visible";
      }
   }
   catch (x) {}
}

