var mouseX, mouseY, winH, winW, divW, xOffSet, yOffSet;
var IE = document.all?true:false;
var divW = 0;
var hoverDelay=1000;
var isOver = false;
var isShowing = false;
var helpPrefix = "_help_";
var nowShowing = "";
if (!document.all)
{
   //document.captureEvents(Event.MOUSEMOVE)
   //document.addEventListener("mousemove", getMouseXY);
}
document.onmousemove = getMouseXY;
function getMouseXY(e) {
    if(document.all){
        mouseX = event.clientX;// + document.body.scrollLeft;
        mouseY = event.clientY;// + document.body.scrollTop;
    } else {
        mouseX = e.pageX;
        mouseY = e.pageY;
    }
    if (isShowing)
        showHelpDiv();
}

window.onresize = function() {
    getWindowSize();
    getScrollXY();
}
/**
 * wrapper function for more hard coded links to reference help popup
 **/
function showDiv(strName){
    hideHelpDiv(); // Hide previous if there was one
    nowShowing = strName;
    isOver = true;
    setTimeout("showHelpDiv()", hoverDelay);
}
/**
 * wrapper function for more hard coded links to reference help popup
 **/
function showDiv(strName, delay){
    hideHelpDiv(); // Hide previous if there was one
    nowShowing = strName;
    isOver = true;
    setTimeout("showHelpDiv()", delay);
}
/**
 * wrapper function for more hard coded links to reference help popup
 **/
function hideDiv(strName){
    hideHelpDiv();
}

function showHelpDiv(){
    if( isOver && nowShowing != "" && nowShowing != helpPrefix ) {
        
        getScrollXY();     // Pos of the Scroll Offsets
        getWindowSize();   // Height and Width of Window
    
        // Vertical Positions
        // If we display the DIV to the top the drop downs will show
        // through on IE so we need to make sure we pop down        
        var divY = (mouseY+10);
    
        // IE handles offsets differently
        if (document.all)
            divY = (divY + yOffSet);
        var shownDiv = document.getElementById(nowShowing);
        shownDiv.style.top = "-1000px";
        shownDiv.style.display="block";
    
        // Horizontal Positions
        // Since we can really only reliably determine how much of the
        // window on the left is showing, we will start by placing the
        // right hand side of the box at the mouse position. Then we  
        // can shift the box to the right if we are off screen.       
        var divX = mouseX - divW;
    
        // IE handles offsets differently
        if(document.all)
            divX = (divX + xOffSet);
    
        var winWidth = 0;
        if( typeof( window.innerWidth ) == 'number' ) {
            //Non-IE
            winWidth = window.innerWidth - 20;
        } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
            //IE 6+ in 'standards compliant mode'
            winWidth = document.documentElement.clientWidth - 20;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
            //IE 4 compatible
            winWidth = document.body.clientWidth - 20;
        }

        // We are off screen 
        if(divX + shownDiv.clientWidth > winWidth) {
            while(divX + shownDiv.clientWidth > winWidth) {
                divX--;
            }
        }
    
        shownDiv.style.left = divX+"px";
        shownDiv.style.top = divY+"px";
        isShowing = true; 
    }
}

function hideHelpDiv() {
    isOver = false;
    isShowing = false;

    if (nowShowing != "" && nowShowing != helpPrefix)
       document.getElementById(nowShowing).style.display="none";
}

function getWindowSize() {
    winW = 0;
    winH = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        // Non-IE
        winW = window.innerWidth;
        winH = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //  6+ in 'standards compliant mode'
        winW = document.documentElement.clientWidth;
        winH = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        // IE 4 compatible
        winW = document.body.clientWidth;
        winH = document.body.clientHeight;
    }
}

function getScrollXY() {
    yOffSet = 0;
    xOffSet = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape compliant
        yOffSet = window.pageYOffset;
        xOffSet = window.pageXOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM compliant
        yOffSet = document.body.scrollTop;
        xOffSet = document.body.scrollLeft;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        yOffSet = document.documentElement.scrollTop;
        xOffSet = document.documentElement.scrollLeft;
    }
    return [ xOffSet, yOffSet ];
}
    
function addTableHeaderHandlers(tableId) {
    var table = document.getElementById(tableId);
    var col = table.getElementsByTagName("th");
    for (i = 0; i < col.length; i++) {
        col[i].onmouseover = function () {
            if (document.getElementById(helpPrefix + this.innerHTML)!=null){
                hideHelpDiv(); // Hide previous if there was one
                isOver = true;
                nowShowing = helpPrefix + this.innerHTML;
                if(nowShowing != "" && nowShowing != helpPrefix) {
                    this.style.cursor='help';
                    setTimeout("showHelpDiv()", hoverDelay);
                } else {
                    this.style.cursor='pointer';
                }
            }
        };
        col[i].onmouseout = function () {
            if (document.getElementById(helpPrefix + this.innerHTML)!=null){
                hideHelpDiv();
            }    
        };
    }
}

function addTableHeaderHandlersByName(tableId, divNames) {
    var table = document.getElementById(tableId);
    var col = table.getElementsByTagName("th");
    for (i = 0; i < col.length; i++) {
        col[i].id = divNames[i];
        col[i].onmouseover = function () {
            hideHelpDiv(); // Hide previous if there was one
            isOver = true;
            nowShowing = helpPrefix + this.id;
            if( nowShowing != "" && nowShowing != helpPrefix ) {
                this.style.cursor='help';
                setTimeout("showHelpDiv()", hoverDelay);
            } else {
                this.style.cursor='pointer';
            }
        };
        col[i].onmouseout = function () {
            hideHelpDiv();
        };
    }
}
