// This is the client side DHTML JScript code that drives the 
// menu interaction. Requires IE4+
//
// Menu selection takes the 'target' attribute from the XML menu 
// and finds a matching parent frame, a child iframe or open a 
// new named window.  If no target name is provided it navigates 
// the current page to the selected URL.  See Select() function.
//

var currentElement          = null;         //Needed to track current highlighted element
var menuArray               = new Array();  //Tracks what divs are showing so it knows what to hide when a new span is clicked
var onTextColor             = "#ffffff";    //Text color with mouseover
var offTextColor            = "#ffffff";    //Text color with NO mouseover
var onCellColor             = "#77aadd";    //Span color with mouseover
var offCellColor            = "#6495ED";    //Span color with nomouseover
var offsetX                 = 0;            //For top level submenu positioning
var offsetY                 = 55;            //For top level submenu positioning
var subOffsetX              = 1;            //For submenu positioning
var subOffsetY              = 0;            //For submenu positioning
var started                 = false;        //Used to track if we just started the menus
var debug                   = false;        //whether to debug menu events.

// Register onclick event so what if user clicks outside menu, the menus disappear.
//document.onclick = ClickFilter;
// Register events for document
addListener(document,'mousemove',MoveFilter,false);
addListener(document,'keydown',KeyFilter,false);

// Cross-browser implementation of element.addEventListener()
function addListener(element, type, expression, bubbling)
{
	bubbling = bubbling || false;
	
	if(window.addEventListener)	{ // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
		return true;
	} else return false;
}

function getCoords(element)
{
    var coords = { x: 0, y: 0, width: element.offsetWidth, height:element.offsetHeight};
    while (element) 
    {
        coords.x += element.offsetLeft;
        coords.y += element.offsetTop;
        element = element.offsetParent;
    }
    return coords;
}

function ClickFilter(e)
{
    var src_elem;
    if ( document.all )
    {
        src_elem = window.event.srcElement;
    }
    else
    {
        src_elem = e.target;
    }
	if ( src_elem.className == "clsMenuBarCell" )
		hideAllDivs();
}

function MoveFilter(e)
{
    var src_elem;
    if ( document.all )
    {
        src_elem = window.event.srcElement;
    }
    else
    {
        src_elem = e.target;
    }

	if ( src_elem.className != "clsMenuBarCell" &&
			src_elem.className != "clsMenuBar" &&
				src_elem.className != "clsMenuItem" &&
					src_elem.className != "clsMenu" 
		)
		hideAllDivs();
}

function KeyFilter(e)
{
    var event_obj;
    if ( document.all )
    {
        event_obj = window.event;
    }
    else
    {
        event_obj = e;
    }

    if (event_obj.keyCode == 27) hideAllDivs();
}

function startIt(menu,thisItem,level) {                     //menu = menu to display,thisItem=coordinates of item to use,level=current depth of menus
    Debug("startmenu="+menu + ", item=" + thisItem.id + ", level=" + level);
    hideAllDivs();
    started = true;                                     //Lets us know we're coming in for the 1st time
    stateChange(menu,thisItem,level);
}

function Debug(msg) {
    if (debug) {
        var row = DEBUG.insertRow();
        var cell = row.insertCell();
        cell.innerText = msg;
    }
}

function UnFlip(eItem)
{
    eItem.flipped = false;
    var eImg = eItem.children["arrow"];
    if (eImg) {
        eImg.src = "images/right_arrow.gif";
    }
    var eParent = eItem.parentNode;
    eParent.flipped = null;
}

function UnFlipItem(eParent)
{
    var eItem = eParent.flipped;
    if (eItem) {
        UnFlip(eItem);
    }
    eParent.flipped = null;
}

function Flip(eItem)
{
    var eImg = eItem.children["arrow"];
    if (eImg) {
        eImg.src = "images/left_arrow.gif";
        var eParent = eItem.parentNode;
        eParent.flipped = eItem;
        eItem.flipped = true;
    }
}

function stateChange(menu,thisItem,level) 
{                                 //menu = menu to display,thisItem=name of span item to use,level=current depth of menus
    Debug("menu="+menu + ", item=" + thisItem.id + ", level=" + level);
     
    if (currentElement) { 
        currentElement.style.background = offCellColor;
        currentElement.style.color = offTextColor;
        //if (currentElement.flipped) UnFlip(currentElement);
        currentElement = null;
    }
            
    //Turn on new item
    currentElement = thisItem;
    thisItem.style.background = onCellColor;
    thisItem.style.color = onTextColor;
        
    if (menu != "") 
    {
        eMenu = document.all(menu);
        eItem = thisItem;
        hideDiv(level);
        menuArray[menuArray.length] = menu; // add to list of open menus.
        
        client_rect = getCoords(eItem);
        
        var positionX = client_rect.x;
        var positionY = client_rect.y + client_rect.height - 2; // 2 pixel has been reduced for the menu adjustment
        
        // if this is a submenu, add width of submenu item.
        if (eItem.tagName == "SPAN") {
            positionX = client_rect.x + client_rect.width;
            positionY = client_rect.y - 2;
        } else {
            positionY = client_rect.y + client_rect.height - 2;
        }

        eMenu.style.left = positionX;
        eMenu.style.top = positionY;
        eMenu.style.display = "block";

        // Now that menu is visible check width (width is 0 initially)
        
        //If screen isn't wide enough to fit menu, then pop it up on the left side.
        if ((positionX + eMenu.offsetWidth - document.body.scrollLeft) >= document.body.clientWidth) {
            if (level == 0 || (eItem.parentNode.offsetLeft - eMenu.offsetWidth) < 0) {
                eMenu.style.left = document.body.clientWidth - eMenu.offsetWidth + document.body.scrollLeft;
            } else {
                eMenu.style.left = eItem.parentNode.offsetLeft - eMenu.offsetWidth;            
                Flip(eItem);
            }
        }
        //If the menu is too far to the left to display, bump it to the right some
        if (positionX <= document.body.scrollLeft) {
            eMenu.style.left = document.body.scrollLeft;
        }
        //If the menu is too far down, bump the menu up so the bottom equals the body clientHeight property
        if ((positionY + eMenu.offsetHeight) >= document.body.clientHeight) {
            if (started != true) 
                eMenu.style.top = document.body.clientHeight - eMenu.offsetHeight;
        }
        
    }
    started = false; //After 1st menu, turn of started variable
}

function Select(url, name)
{
    if (name != '') {
        if (parent != window && parent.frames[name] != null) {            
            parent.frames[name].url = url;
        } else if (document.all.item(name)) {
            document.all.item(name).src = url;
        } else {
            window.open(url, name);
        }
    } else {
        location.href = url;
    }
    event.returnValue = false;
}

function hideDiv(currentLevel) {
    Debug("hideDiv="+currentLevel);
    for (var i = currentLevel; i < menuArray.length; i++) {
        if (menuArray[i] == null) continue;
        Debug("hide="+menuArray[i]);
        var eMenu = document.all(menuArray[i]);
        if (eMenu) {
            UnFlipItem(eMenu);
            eMenu.style.display='none';
        }
    }
    menuArray.length = currentLevel;
}

function hideAllDivs() {
    Debug("hideAllDivs");    
    for (var i = 0; i < menuArray.length; i++) {
        var arrayString = new String(menuArray[i]);
        if (arrayString == "undefined") continue;
        var eMenu = document.all(menuArray[i]);
  		eMenu.style.display = "none";
		eMenu.style.left = 0;
		eMenu.style.top = 0;
		UnFlipItem(eMenu);
    }
    if (currentElement) { 
        currentElement.style.background = offCellColor;
        currentElement.style.color = offTextColor;
        currentElement = null;
        menuArray = new Array();    
        currentSpanElement = "";        
    }
}
 
  

function onContact( contactUrl )
{
     window.open( contactUrl );
}

function onPrivacy( product )
//  product 1 or 2 = eTimesheets
//  product 3 = employeemax
//  product 4 = other
{
	if ( product == "3" )
		infoWindow( "privacystatement.htm" );
	else if ( product == "4" )
		infoWindow( "privacystatement_other.htm" );
	else
		infoWindow( "privacystatement_et.htm" );
}

function onService( product )
//  product 1 or 2 = eTimesheets
//  product 3 = employeemax
//  product 4 = other
{
	if ( product == "3" )
		infoWindow( "serviceagreement_et.htm" );
	else if ( product == "4" )
		infoWindow( "serviceagreement_other.htm" );
	else
		infoWindow( "serviceagreement_et.htm" );
}

function onHelp()
{
	infoWindow( "help/default.htm" );
}

function GoToUrl( sURL)
{
	window.location.href = sURL;
}

function infoWindow( file )
{
	window.open( file,null, "height=500,width=700,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}


function onPopUpHelp1()
{
	window.open( "PopHelpCodes.htm" ,null, "height=600,width=690,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

function onPopUpHelp2()
{
	window.open( "PopHelpAttribute.htm" ,null, "height=240,width=740,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

function onPopUpHelp3()
{
	window.open( "PopHelpEmpSel.htm" ,null, "height=420,width=740,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

function onPopUpHelp4()
{
	window.open( "PopHelpPaycodeSel.htm" ,null, "height=320,width=700,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
}

function onPopUpHelp5()
{
//	window.open( "PopHelpCustomCodes.htm" ,null, "height=500,width=750,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
	window.open( "http://sites.movefirst.com/info/eTimesheets_CustomCodes.htm");
}

function onPopUpHelp6()
{
	window.open( "PopHelpAttributeGroups.htm" ,null, "height=600,width=840,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
} 

	function trim(inputString) {
	// Removes leading and trailing spaces from the passed string. Also removes
	// consecutive spaces and replaces it with one space. If something besides
	// a string is passed in (null, custom object, etc.) then return the input.
	if (typeof inputString != "string") { return inputString; }
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	while (ch == " ") { // Check for spaces at the beginning of the string
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ") { // Check for spaces at the end of the string
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
		retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}
	return retValue; // Return the trimmed string back to the user
	} // Ends the "trim" function


