/*
----------------------------------------------------------
    This file is included in all pages
    2ergo Menu JS File
    FileName:    menu.js
    Version:     1.00               05/05/2004
    Copyright 2004 2ergo - www.2ergo.com
    Author: Ian Winstanley
----------------------------------------------------------
*/

document.onresize = captureMousePosition;
// Global variables
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

var windowSize = new Array();
function captureMousePosition(e) {
    if (document.all) {
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
    //window.status = "xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax;
    windowSize [0] = xMousePosMax;
    windowSize [1] = yMousePosMax;
}


// Writes the menu container divs to the page at page load time based on the values within the cSA (clientSubArray)
// This function saves over 130Kb in page size
function writeClientDivs(cSA) {
    var initialWriteString = '';
    var writeString = '';
    var previousKeyFirstLetter = '';
    var j = 0;
    var trY = ''

    for (i in cSA) {
        originalKey = i;
        currentKey = i.toLowerCase();
        currentKeyFirstLetter = currentKey.substring(0, 1);

        if  (currentKeyFirstLetter != previousKeyFirstLetter || j == 0) {
            if (j != 0) {
                writeString += '</div>';
            }
            initialWriteString += '<span id="href_' + currentKeyFirstLetter + '" class="subBtn" onclick="menuList(\'menu_' + currentKeyFirstLetter + '\',\'visible\', this.id, -15, 50, 1); return true"><a href="javascript:void(0)" class="subBtn" onmouseover="showStatus(\'Display a list of customers beginning with ' + currentKeyFirstLetter + '\')" onmouseout="showStatus()">' + currentKeyFirstLetter + '<img style="position:absolute; left:35px; margin-top:4px" src="/images/menu_arrow.gif"></a></span>';
            writeString += '<div id="menu_' + currentKeyFirstLetter + '" class="subMenu" onclick="javascript:menuList(\'menu_' + currentKeyFirstLetter + '\',\'hidden\')">';
        }

        writeString += '<a class="subBtn" href="javascript:chooseclient(\'' + originalKey + '\',\'' + cSA[originalKey] + '\',1)" onmouseover="showStatus(\'Query records for ' + originalKey + '\'); return true" onmouseout="showStatus()">' + originalKey + '</a>';
        j++;
        previousKeyFirstLetter = currentKeyFirstLetter;
    }
    writeString += '</div>';

    document.write(initialWriteString);
    document.write(writeString);
}


var lastMenuItem = '';
// Displays and hides menus based on the array and id sent in the parameters
function menuList(menu, state, hrefID, topOffset, leftOffset, child) {
    if (menu.match(/menu_/)) {
        if (clientArraySet == true) {
            for (var i in clientArray) {
                if (clientArray[i] != menu && clientArray[i] != '') {
                    getElementById(clientArray[i]).style.visibility = 'hidden';
                }
            }
        }
    } else if (getElementById(lastMenuItem)) {
        getElementById(lastMenuItem).style.visibility = 'hidden';   
    }
    lastMenuItem = menu;
        
    if (!child) {
        for (var i in menus) {
            if (menus[i] != menu) { 
                if (getElementById(menus[i])) {
                    getElementById(menus[i]).style.visibility = 'hidden';
                }
            }
        }
    }
    if (state) {
        if (getElementById(menu)) {
            getElementById(menu).style.visibility = state;
        }
    }

    if (hrefID) {
        topOffset = topOffset < 0 ? topOffset : 120;
        leftOffset = leftOffset ? leftOffset : 0;
        topOffset = topOffset != 58 ? document.getElementById(hrefID).offsetTop + topOffset : topOffset;
        if (NS || NS6 || NS60 || Firefox) {
            extraTop = 3;
        } else {
            extraTop = 0;
        }
        getElementById(menu).style.top = document.getElementById(hrefID).offsetHeight + topOffset + extraTop + 'px';
        var windowAvailHeight = windowSize[1];
        if ((getElementById(menu).offsetTop + document.getElementById(menu).offsetHeight + 75 > windowAvailHeight) && menu != 'menu4' 
            && navigator.userAgent.toLowerCase().search(/msie 7\.0/) == -1) {
            if (getElementById(menu).offsetTop > 190) {
                getElementById(menu).style.top = windowAvailHeight-350 + 'px';
            }
            getElementById(menu).style.height = windowAvailHeight - getElementById(menu).offsetTop - 90 + 'px';
            getElementById(menu).style.overflow = 'auto';
        }
        getElementById(menu).style.left = document.getElementById(hrefID).offsetLeft + leftOffset + 'px';
    }
}