﻿// Select Control
// Dependencies: BMTWeb.js

// ------------------------------
// Public Functions
//
// Object Functions:
// PopupMenu(zMenuID, oDiv)                                                     - Create a Popup Menu Object, oDiv is a DIV to create the object in, defaults to first FORM on the page
// PopupMenu.AddMenuItem(zItemID, zText, zImagePath, oClickFunction, bHidden)   - Add an item to the popup menu, bHidden optional
// PopupMenu.AddSubMenuItem(zItemID, zText, zMenuID, bHidden)                   - Add a link to a sub menu, bHidden optional
// PopupMenu.AddDivider(zItemID)                                                - Add a dividing line
// PopupMenu.HideMenuItem(zItemID)                                              - Hide a specific menu Item
// PopupMenu.ShowMenuItem(zItemID)                                              - Show a specific menu Item
//
// Static Functions:
// popupMenuLoad()                          - Add the body events
// popupMenuExists(zMenuID)                 - Check if popup menu exists
// popupMenuShow(zMenuID, lX, lY, bReload)  - Show a popup menu, bReload optional, if set to true forces the reload of menu items
// popupMenuHide()                          - Hide all open menus
// ------------------------------

// ------------------
// Public Functions
// ------------------

var omPopupMenu = new Object();         // Popup Menu
var omPopupMenuShowing = new Array();   // Popup Menus showing
var omPopupMenuTimer = null;

function popupMenuLoad()
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Add the body events

	document.body.onmouseup = function () { popupMenuHide() };
	//document.body.onfocus = function () { popupMenuHide() };
}

function popupMenuExists(zMenuID)
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Check if popup menu exists
    if (omPopupMenu[zMenuID])
        return true;
    else
        return false;
}

function popupMenuShow(zMenuID, oMenu, lWidth)
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Show a menu
    //          bReload optional, if set to true forces the reload of menu items
    
    clearInterval(omPopupMenuTimer);
   
    popupMenuHide(); // Hide any open menus
    
	var lpX = bmtWebGetOffsetLeft(oMenu);
	var lpY = bmtWebGetOffsetTop(oMenu) + 20;

    // Show a popup Menu
    omPopupMenu[zMenuID].Show(lpX, lpY, lWidth);
   
}

function popupMenuHide()
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Hide all open popup menus
    var zpMenuID;

    for (var lpLoop = 0; lpLoop < omPopupMenuShowing.length; lpLoop++)
    {
        zpMenuID = omPopupMenuShowing[lpLoop];

        omPopupMenu[zpMenuID].Hide();
    } 
}

// Popup Menu Object
function PopupMenu(zMenuID, oDiv)
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Create a Popup Menu Object

    this.MenuID = zMenuID;
    if (oDiv)
        this.Div = oDiv;                // defined to position iframes then add to div
    else
        this.Div = document.forms[0];   // else add to first first form on page
    this.SubMenus = new Object();
    this.SubMenuID = "";                // currently displayed sub menu
    this.MenuItemsIndex = new Object(); // Index of each menu item
    this.MenuItems = new Array();       // Menu Items / SubMenuItems / Dividers
    this.MenuType = {Item: 1, SubMenu: 2, Divider: 3};
    this.Iframe = null;
    this.NoImages = false;              // Set to true to not add image column

    omPopupMenu[this.MenuID] = this;    // Add popup menu to list of popup menus
}

PopupMenu.prototype = 
{
    AddMenuItem: function (zItemID, zText, zImagePath, oClickFunction, bHidden)
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Add an item to the popup menu, bHidden optional
	    var opMenuItem = {MenuType: this.MenuType.Item, 
	                        ItemID: zItemID,
	                        Text: zText,
	                        ImagePath: zImagePath,
	                        ClickFunction: oClickFunction,
	                        Hidden: bHidden};
        
        this.MenuItemsIndex[zItemID] = this.MenuItems.length;
        this.MenuItems[this.MenuItems.length] = opMenuItem;
	},

    AddSubMenuItem: function (zItemID, zText, zMenuID, oClickFunction, bHidden)
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Add a link to a sub menu, bHidden optional

	    // bHidden optional
	    // Opens a Sub Menu Item

	    var opMenuItem = {MenuType: this.MenuType.SubMenu,
	                        ItemID: zItemID,
	                        Text: zText,
	                        MenuID: zMenuID,
							ClickFunction: oClickFunction,
	                        Hidden: bHidden};

        this.MenuItemsIndex[zItemID] = this.MenuItems.length;
        this.MenuItems[this.MenuItems.length] = opMenuItem;
	},

	AddDivider: function (zItemID)
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Add a gap
	    var opMenuItem = {MenuType: this.MenuType.Divider, ItemID: zItemID};
	    
	    this.MenuItemsIndex[zItemID] = this.MenuItems.length;
        this.MenuItems[this.MenuItems.length] = opMenuItem;	
	},

	
	HideMenuItem: function (zItemID)
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Hide a specific menu Item	
	    this.MenuItems[this.MenuItemsIndex[zItemID]].Hidden = true;
	    
	    if (this.Iframe != null)
	    {
	        this.Iframe.contentWindow.document.getElementById(zItemID).style.display = "none";
	        //this.ResizeIframe();
        }
	},

	ShowMenuItem: function (zItemID)
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Show a specific menu Item	
	    this.MenuItems[this.MenuItemsIndex[zItemID]].Hidden = false;
	    
	    if (this.Iframe != null)
	    {
	        this.Iframe.contentWindow.document.getElementById(zItemID).style.display = "block";	        
            //this.ResizeIframe();
        }
	},

// ------------------
// Private Functions
// ------------------

    Show: function(lX, lY, lWidth, zParentMenuID, bReload)
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Show the menu, zParentMenuID optional,
        //                         bReload optional, if set to true forces the reload of menu items

        if (this.Iframe != null && bReload != true)
        {
            var opIframe = this.Iframe;
        }
        else
        {
            if (bReload == true && this.Iframe != null)
                this.Div.removeChild(this.Iframe);

			if (bmGlobalFirefoxBrowser)
			{
				opIframe = document.createElement("div");
				opIframe.style.display = "none";
				opIframe.style.position = "absolute";
				opIframe.id = "PopupMenuIframe_" + this.MenuID;
				opIframe.style.overflow = "none";
				opIframe.setAttribute("IsPopupMenu", "true");
			}
			else
			{
				// Create Popup Menu
				opIframe = document.createElement("iframe");
				opIframe.style.display = "none";
				opIframe.marginWidth = 0;
				opIframe.marginHeight = 0;
				opIframe.frameBorder = "0";
				opIframe.style.position = "absolute";
				opIframe.id = "PopupMenuIframe_" + this.MenuID;
				opIframe.scrolling = "no";
			}
		
			// If a width greater than zero is specified, set the menu to be this width
			if (lWidth > 0)
				opIframe.style.width = lWidth + "px";
			
			if (zParentMenuID)
				opIframe.style.zIndex = 6000;
			else
				opIframe.style.zIndex = 5000;

            // Add To Page (insert as first item in div)
            // This must be done before content can be added to the IFRAME
            this.Div.insertBefore(opIframe, this.Div.firstChild);

            // Initial content of iframe must be done via document.write
		    var zpContent = "<head>" +
					            "<link href='" + bmtWebResolveURL("~/Controls/Popup/Style.css") + "' type='text/css' rel='stylesheet'>" +
					        "</head>" +
					        "<body ";
					        
            if (!document.getElementById("debug"))
                zpContent += "ondrag='event.returnValue = false;' onselectstart='event.returnValue = false;' oncontextmenu='event.returnValue = false;'";
                
            zpContent += "></body>";
            
            if (bmGlobalFirefoxBrowser)
            {
				var opDocument = window.document;
				var opBody = opIframe;
            }
            else
            {
				opIframe.contentWindow.document.write(zpContent);
		    
				var opDocument = opIframe.contentWindow.document;
				var opBody = opIframe.contentWindow.document.body;
			}
            
            opBody.style.margin = "0px;";

            var opTableOuter = opDocument.createElement("table");
            opTableOuter.cellPadding = "2";
            opTableOuter.cellSpacing = "0";
            opTableOuter.onmouseout = function () { popupMenuMouseOutMenu(); };
            opTableOuter.onmouseover = function () { popupMenuMouseOverMenu(); };
            
            if (lWidth > 0)
				opTableOuter.style.width = "100%";
			
			if (zParentMenuID)	// If this is a sub-menu, shade it a darker grey colour
				opTableOuter.className = "popupSubMenuTable";
			else
				opTableOuter.className = "popupMenuTable";

            var opTBodyOuter = opDocument.createElement("tbody");
            var opTR = opDocument.createElement("tr");
			
			var opTD = opDocument.createElement("td");
			
			if (this.NoImages == false)
			{
				opTD.style.backgroundImage = "url('" + bmtWebResolveURL("~/Controls/Popup/ImageCellBackground.gif") + "')";
				opTD.style.backgroundRepeat = "repeat-y";
			}
			
            var opTable = opDocument.createElement("table");
            opTable.cellPadding = "1";
            opTable.cellSpacing = "0";
            opTable.style.width = "100%";
            
            var opTBody = opDocument.createElement("tbody");
    
            var opMenuItem;
            for (var lpLoop = 0; lpLoop < this.MenuItems.length; lpLoop++)
            {
                opMenuItem = this.MenuItems[lpLoop];
                
                switch (opMenuItem.MenuType)
                {
                    case this.MenuType.Item:
                        opTBody.appendChild(this.CreateItem(opDocument, opMenuItem));
                        break;

                    case this.MenuType.SubMenu:
                        opTBody.appendChild(this.CreateSubMenu(opDocument, opMenuItem));
                        break;

                    case this.MenuType.Divider:
                        opTBody.appendChild(this.CreateGap(opDocument, opMenuItem));
                        break;
                }           
            }

            opTable.appendChild(opTBody);
            
            opTD.appendChild(opTable);
            opTR.appendChild(opTD);
            opTBodyOuter.appendChild(opTR);
            opTableOuter.appendChild(opTBodyOuter);            
            opBody.appendChild(opTableOuter);

            // Add Popup Menu to object using MenuID
            this.Iframe = opIframe;
        }

        var lpScrollHeight = 0;
        var lpScrollWidth = 0;

        // Check whether scrolling visible BEFORE making date picker visible
        if (document.body.scrollHeight > document.body.clientHeight)
            lpScrollHeight = 17;
    	
        if (document.body.scrollWidth > document.body.clientWidth)
            lpScrollWidth = 17;

        opIframe.style.left =  lX + "px";
        opIframe.style.top = lY + "px";
        opIframe.style.display = "block";
                
        this.ResizeIframe();
        
        // Adjust right position if off screen, taking into account if scroll bars are showing
        if((parseInt(opIframe.offsetTop) + parseInt(opIframe.offsetHeight)) > (document.body.clientHeight + document.body.scrollTop + lpScrollHeight))
        {		
            lpPosition = parseInt(document.body.offsetHeight) - opIframe.offsetHeight + document.body.scrollTop - lpScrollHeight;

            if (lpPosition < 0)
                lpPosition = 0;

	        opIframe.style.top = lpPosition + "px";
        }

        // Adjust left position if off screen, taking into account if scroll bars are showing						
        if((parseInt(opIframe.offsetLeft) + parseInt(opIframe.offsetWidth)) > (document.body.clientWidth + document.body.scrollLeft))
        {
            
            if (zParentMenuID == "" || !zParentMenuID)
                opIframe.style.left = parseInt(document.body.offsetWidth) - opIframe.offsetWidth + document.body.scrollLeft - lpScrollWidth;
            else
            {
                lpPosition = bmtWebGetOffsetLeft(omPopupMenu[zParentMenuID].Iframe) -  parseInt(opIframe.style.width) + 1;

                if (lpPosition < 0)
                    lpPosition = 0;

                opIframe.style.left = lpPosition + "px";
            }
        }
         
        opIframe.focus();
        omPopupMenuShowing[omPopupMenuShowing.length] = this.MenuID;
    },

	AddSubMenu: function (oPopupMenu)
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Add a sub menu to SubMenus object
	    SubMenus[oPopupMenu.MenuID] = oPopupMenu;
	},

	Hide: function()
	{
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Hide the iframe	
	    if (this.Iframe != null)
	        this.Iframe.style.display = "none";
	},


    ResizeIframe: function()
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Resize Iframe
    
        var opIframe = this.Iframe;

		if (bmGlobalFirefoxBrowser)
		{
			opIframe.style.width = (opIframe.firstChild.clientWidth + 2) + "px";
			opIframe.style.height = (opIframe.firstChild.clientHeight + 2) + "px";
		}
		else
		{
			opIframe.style.width = (opIframe.contentWindow.document.body.firstChild.clientWidth + 2) + "px";
			opIframe.style.height = (opIframe.contentWindow.document.body.firstChild.clientHeight + 2) + "px";
		}
    },

    CreateItem: function(oDocument, oMenuItem)
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Create Item in DOM structure
    
        var opTable = oDocument.createElement("table");
        opTable.cellPadding = "0";
        opTable.cellSpacing = "0";
        opTable.style.width = "100%";

        var opTBody = oDocument.createElement("tbody");
        var opTR = oDocument.createElement("tr");
        var opTD = oDocument.createElement("td");

        if (this.NoImages == false)
        {
            opTR.appendChild(this.CreateImage(oDocument, oMenuItem));
        }

        opTD.style.paddingTop = "3px";
        opTD.style.paddingBottom = "3px";
        opTD.style.paddingRight = "3px";
        opTD.nowrap = "nowrap";
        
        opTD.appendChild(oDocument.createTextNode(oMenuItem.Text));
        opTD.className = "popupMenuTdNormal";

        opTR.appendChild(opTD);
        opTBody.appendChild(opTR);
        opTable.appendChild(opTBody);
        
        return this.CreateAnchorCell(opTable, oDocument, oMenuItem);
    },

    CreateSubMenu: function(oDocument, oMenuItem)
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Create Sub Menu in DOM structure
            
        var opTable = oDocument.createElement("table");
        opTable.cellPadding = "0";
        opTable.cellSpacing = "0";
        opTable.style.width = "100%";

        var opTBody = oDocument.createElement("tbody");
        var opTR = oDocument.createElement("tr");
        var opTD = oDocument.createElement("td");

        if (this.NoImages == false)
        {
            opTR.appendChild(this.CreateImage(oDocument, oMenuItem));
        }

		opTD.className = "popupMenuTdNormal";
        opTD.style.paddingTop = "3px";
        opTD.style.paddingBottom = "3px";
        opTD.style.paddingRight = "3px";
        opTD.nowrap = "nowrap";

        opTD.appendChild(oDocument.createTextNode(oMenuItem.Text));
        opTR.appendChild(opTD);

        opTD = oDocument.createElement("td");
        opTD.className = "popupMenuTdNormal";
        opTD.style.paddingRight = "3px";
        opTD.style.paddingLeft = "3px";
        
        opTD.style.width = "4px";

        var opImage = oDocument.createElement("img");
        opImage.src = bmtWebResolveURL("~/Controls/Popup/RightArrow.gif");
        opImage.border = "0";
        opImage.style.width = "4px";
        opImage.style.height = "7px";

        opTD.appendChild(opImage);
        
        opTR.appendChild(opTD);
        opTBody.appendChild(opTR);
        opTable.appendChild(opTBody);
        
        return this.CreateAnchorCell(opTable, oDocument, oMenuItem);        
    },

    CreateGap: function(oDocument, oMenuItem)
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Create Gap in DOM structure

        var opTable = oDocument.createElement("table");
        opTable.cellPadding = "0";
        opTable.cellSpacing = "0";
        opTable.style.width = "100%";

        var opTBody = oDocument.createElement("tbody");
        var opTR = oDocument.createElement("tr");
        var opTD = oDocument.createElement("td");

        if (this.NoImages == false)
        {
            opTR.appendChild(this.CreateImage(oDocument, oMenuItem));
        }

        opTD.style.backgroundRepeat = "repeat-x";
        opTD.style.backgroundImage = "url(" + bmtWebResolveURL("~/Controls/Popup/Gap.gif") + ")";

        var opImage = oDocument.createElement("img");
        opImage.style.height = "1px";
        opImage.style.width = "1px";
        opImage.src = bmtWebResolveURL("~/Controls/Popup/Gap.gif");

        opTD.appendChild(opImage);

        opTR.appendChild(opTD);
        opTBody.appendChild(opTR);
        opTable.appendChild(opTBody);
        
        return this.CreateAnchorCell(opTable, oDocument, oMenuItem);  
    },
    
    CreateImage: function (oDocument, oMenuItem)
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Create Image cell in structure
            
        var opTD = oDocument.createElement("td");
        opTD.style.width = "27px";
        opTD.style.paddingLeft = "2px";

        if (oMenuItem.MenuType != this.MenuType.Divider)
        {
            var opImage = oDocument.createElement("img");
            opImage.style.height = "16px";
            opImage.style.width = "16px";

            if (oMenuItem.ImagePath == "" || !oMenuItem.ImagePath)
                opImage.src = bmtWebResolveURL("~/Controls/Popup/BlankImage.gif");
            else
                opImage.src = oMenuItem.ImagePath;

            opTD.appendChild(opImage);
        }       

        return opTD;    
    },
    
    CreateAnchorCell: function (oTable, oDocument, oMenuItem)
    {
        // Author:	Jon Peters
        // Date:	22/06/2007
        // Purpose:	Create Anchor
    
        var opTR = oDocument.createElement("tr");
        opTR.id = oMenuItem.ItemID;
        
        if (oMenuItem.Hidden == true)
            opTR.style.display = "none";

        var opAnchor = oDocument.createElement("a");       
        var opTD = oDocument.createElement("td");
        
        opTD.style.width = "100%";

        if (oMenuItem.MenuType != this.MenuType.Divider)
        {
            opAnchor.href = "#";
            
            if (oMenuItem.MenuType == this.MenuType.Item)
            {
                opAnchor.onclick = function () { oMenuItem.ClickFunction.call(); popupMenuHide(); };
                
                var zpMenuID = this.MenuID;
                
                opAnchor.onmouseover = function () { popupMenuSubMenuHide(zpMenuID); popupMenuMouseOver(this); };
                opAnchor.onfocus = function () { popupMenuSubMenuHide(zpMenuID); popupMenuMouseOver(this); };
            }
            else
            {
                var zpMenuID = this.MenuID;
                
                opAnchor.onmouseover = function () { popupMenuSubMenuHide(zpMenuID); popupMenuMouseOver(this); popupMenuSubMenuShow(zpMenuID, this, oMenuItem.MenuID); };
                opAnchor.onfocus = function () { popupMenuMouseOver(this); };
                //opAnchor.onclick = function () { popupMenuSubMenuHide(zpMenuID); popupMenuSubMenuShow(zpMenuID, this, oMenuItem.MenuID); };
                opAnchor.onclick = function () { oMenuItem.ClickFunction.call(); popupMenuHide(); };
            }
            
            opAnchor.onmouseout = function () { popupMenuMouseOut(this); };
            
            opAnchor.onblur = function () { popupMenuMouseOut(this); };
            opAnchor.className = "popupMenuAnchor";   

            opAnchor.appendChild(oTable);
            opTD.appendChild(opAnchor);
            opTR.appendChild(opTD);
        }
        else
        {
            opTD.appendChild(oTable);
            opTR.appendChild(opTD);
        }

        return opTR;
    }
}

function popupMenuMouseOver(oAnchor)
{      
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Mouse Hover Highlight menu item
    oAnchor.className = "popupMenuAnchorHover"; 
    oAnchor.firstChild.firstChild.firstChild.firstChild.className = "popupMenuTdHover";
}

function popupMenuMouseOut(oAnchor)
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Mouse Out Remove Highlight
    oAnchor.className = "popupMenuAnchor";
    oAnchor.firstChild.firstChild.firstChild.firstChild.className = "popupMenuTdNormal";
}

function popupMenuSubMenuHide(zMenuID)
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Hide a Sub Menu

    var opPopupMenu = omPopupMenu[zMenuID];
    
    if (opPopupMenu.SubMenuID != "")
    {
        opPopupMenu.Iframe.focus();
        omPopupMenu[opPopupMenu.SubMenuID].Hide();
        opPopupMenu.SubMenuID = "";
    }
}

function popupMenuSubMenuShow(zMenuID, oAnchor, zSubMenuID)
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Show a Sub Menu
    
    var opPopupMenu = omPopupMenu[zMenuID];    
    opPopupMenu.SubMenuID = zSubMenuID;
    var opPopupMenu = omPopupMenu[zMenuID];
    var opIframe = opPopupMenu.Iframe;
    
    var lpX = (parseInt(opIframe.style.left) + 4 + oAnchor.parentNode.clientWidth + 4) - 17;
    var lpY = (parseInt(opIframe.style.top) + popupMenuGetOffsetTop(oAnchor) - 3) + 2;
    omPopupMenu[zSubMenuID].Show(lpX, lpY, 0, zMenuID);
}

function popupFolder()
{
    // Author:	Jon Peters
    // Date:	22/06/2007
    // Purpose:	Retrieve the relative folder containing the popup menu control
    return bmtWebResolveURL("~/Controls/Popup/");
}

function popupMenuGetOffsetTop(oElement)
{
	var zpCSSText = document.styleSheets[0].cssText;
	var opParent = oElement.offsetParent;
	var lpOffset = oElement.offsetTop;
	
	try
	{
		while (1!=2)
		{
			if (opParent.nodeName == "DIV")
			{
				if (opParent.style.overflow == "auto" || opParent.style.overflowY == "auto" || opParent.getAttribute("IsPopupMenu") == "true")
				{
					return lpOffset;
				}
			}
		
			lpOffset += opParent.offsetTop;
			
			if (opParent.style.borderTopWidth)
				lpOffset += parseInt(opParent.style.borderTopWidth);
				
			opParent = opParent.offsetParent;
		}
	}
	catch(opCatch)
	{
		return lpOffset;
	}
}

function popupMenuMouseOutMenu()
{
	omPopupMenuTimer = setTimeout("popupMenuHide()", 750);
}

function popupMenuMouseOverMenu()
{
	clearInterval(omPopupMenuTimer);
}