  /*  Your are permitted to reuse this code as long as the following copyright
      notice is not removed:

      This HTML tip handling is copyright 1998 by insideDHTML.com, LLC. More information about this
      code can be found at Inside Dynamic HTML: HTTP://www.insideDHTML.com
  */


  // Support for all collection
  var allSupport = document.all!=null
  var getElementById = document.getElementById!=null

// Define some global variables
  var isCSS, isW3C, isIE4, isNN4, isIE6;
  
  var isCSS = (document.body && document.body.style) ? true : false;
  var isW3C = (isCSS && document.getElementById)     ? true : false;
  var isIE4 = (isCSS && document.all)                ? true : false;
  var isNN4 = (document.layers)                      ? true : false;
  var isIE6 = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
      
  var x_offset   = 5;
  var y_offset   = 10;
   
  var VB_CONTACTINFO = "<b>Support</b>: techsupport@knightbondpoint.com - (877) 423-7839<br>"+ 
                       "<b>Trading Desk</b>: trader@knightbondpoint.com - (800) 373-9478<br>"+ 
                       "<b>Sales</b>: sales@knightbondpoint.com - (800) 764-7609<br>"; 

  function setupEventObject(e) {
    // Map NS event object to IEs
    if (e==null) return // IE returns
    window.event = e
    window.event.fromElement = e.target
    window.event.toElement = e.target
    window.event.srcElement = e.target
    window.event.x = e.x
    window.event.y = e.y
    // Route the event to the original element
    // Necessary to make sure _tip is set.
    if (!getElementById)
      window.event.srcElement.handleEvent(e);
  }


  function checkName(src) {
    // Look for tooltip in IE
    while ((src!=null) && (src._tip==null))
      src = src.parentElement
    return src
  }

  function getElement(elName) {
    // Get an element from its ID
    if (allSupport) {
      return document.all[elName]
    } else if (getElementById) {
      return document.getElementById(elName)
    } else {
      return document.layers[elName]
    }
  }

  function writeContents(el, tip, tipWidth) {
    // Replace the contents of the tooltip
    if (allSupport || getElementById) {
      el.innerHTML = tip
      el.style.whiteSpace = 'nowrap'
    } else {
      // In NS, insert a table to work around
      // stylesheet rendering bug.
      // NS fails to apply style sheets when writing
      // contents into a positioned element.
      el.document.open()
      if (!tipWidth)
         tipWidth=100;
      el.document.write("<TABLE WIDTH=" + tipWidth + " BORDER=1 bordercolor=black><TR><TD WIDTH=100% BGCOLOR=#FFFFE1>")
      el.document.write(tip)
      el.document.write("</TD></TR></TABLE>")
      el.document.close()
    }
  }

  function setPosition(el) {
      var field = el;
      var gparent = el.parentElement;
      if (field.style)
         field = field.style;
   // Get the location of the parent element
    if (allSupport) {
      cursorY = window.event.clientY + document.body.scrollTop;
      cursorX = window.event.clientX + document.body.scrollLeft;
    } else {
      cursorY = window.event.pageY;
      cursorX = window.event.pageX;
    }
    x = cursorX;
    y = cursorY;
   // Get some window information so we can make sure the box doesn't extend off the edge of the screen
      var window_width = 0, window_height = 0, scroll_left = 0, scroll_top = 0;
      if (document.documentElement.clientWidth) {
         window_width  = document.documentElement.clientWidth;
         window_height = document.documentElement.clientHeight;
         scroll_left   = document.documentElement.scrollLeft;
         scroll_top    = document.documentElement.scrollTop;
      } else if (document.body.clientWidth || document.body.clientHeight) {
         window_width  = document.body.clientWidth;
         window_height = document.body.clientHeight;
         scroll_left   = document.body.scrollLeft;
         scroll_top    = document.body.scrollTop;
      } else {
         window_width  = window.innerWidth;
         window_height = window.innerHeight;
         scroll_left   = document.body.scrollLeft;
         scroll_top    = document.body.scrollTop;
      }
   // Do our best to try to keep the popup onscreen and away from the mouse
      if (window_width > 0 && window_height > 0) {
         var orig_field = getElement("tipBox");
         width  = parseInt(orig_field.offsetWidth);
         height = parseInt(orig_field.offsetHeight);
      // Adjust the element location?
         if (cursorX > window_width + scroll_left - width - 2)
            x = window_width + scroll_left - width - 2 - x_offset;     // subtract a couple of extra pixels to account for borders
         if (cursorY > window_height + scroll_top - height - 7)
            y = window_height + scroll_top - height - 7; // subtract a couple of extra pixels to account for borders
         if (cursorY > y)
            y = cursorY - height - 7 - y_offset;
      }
   // Make some minor corrections
      x += x_offset;
      y += y_offset;
      if (x < 0) x = 0;
      if (y < 0) y = 0;

    if (allSupport) {
      el.style.pixelTop = y;
      el.style.pixelLeft = x
    } else if (getElementById) {
      el.style.top = y;
      el.style.left = x;
    } else {
      el.top = y;
      el.left = x;
    }
  }
      
  function setVisibility(el, bDisplay) {
    // Hide or show to tip
    if (bDisplay)
      if (allSupport || getElementById)
        el.style.visibility = "visible" 
      else
        el.visibility = "show";
    else
      if (allSupport || getElementById)
        el.style.visibility = "hidden"
      else
        el.visibility = "hidden"
  }


  function displayContents(tip, tipWidth) {
    // Display the tooltip. 
    var el = getElement("tipBox")
    writeContents(el, tip, tipWidth)
    // delay to give position time to be better
    for (t = 0; t < 100; t++) {}
    setPosition(el)
    setVisibility(el, true)
  }

  function doMouseOver(e) {
    // Mouse moves over an element
    setupEventObject(e)
    var el, tip
    if ((el = checkName(window.event.srcElement))!=null)
      if (!el._display) {
        displayContents(el._tip, el._tipWidth)
        el._display = true
      }
  }

  function doMouseOut(e) {
    // Mouse leaves an element
    setupEventObject(e)
    el = checkName(window.event.srcElement)
    var el, tip
    if ((el = checkName(window.event.srcElement))!=null)
      if (el._display)
        if ((el.contains==null) || (!el.contains(window.event.toElement))) {
          setVisibility(getElement("tipBox"), false)
          el._display = false
        }
  }

  function initToolTips() {
    // Do Loading
    if ((window.document.captureEvents==null) && (!allSupport) && !(getElementById))
      return // Not IE4 or NS4
    if (window.document.captureEvents!=null && !getElementById)  // NS - capture events
      window.document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)
    if (!allSupport) {
       document.addEventListener("mouseover",doMouseOver,true);
       document.addEventListener("mouseout",doMouseOut,true);
    }
    window.document.onmouseover = doMouseOver;
    window.document.onmouseout = doMouseOut;
  }
