function genHeaderMenu(xmlfile, divid)
{
	try
    {
        if (navigator.userAgent.indexOf("MSIE") >= 0)
        {
            var mXml = new ActiveXObject("MSXML2.FreeThreadedDOMDocument");
            mXml.async = false;
            mXml.load(xmlfile);
            var current = getCurrentPage();
			var parent_link = getCurrentParentLink(current, mXml);
            var parent = getCurrentParent(current, mXml);
            var html = "<ul>";
            var submenuHtml = "";
            var menu = mXml.selectSingleNode("//ApplicationMenu/Menu[@lv='0']");
            if (menu != null)
            {
                for (var m=0; m<menu.childNodes.length; m++)
                {
                    var node = menu.childNodes[m];
                    var submenu = mXml.selectSingleNode("//ApplicationMenu/Menu[@id='" + node.getAttribute("link") + "']");
					html += "<li>";
					if (node.getAttribute("cp").indexOf("[") != 0)
					{
	                    html += "<a ";
	                    if (node.getAttribute("pageid") == parent)
	                        html += "class='here' ";
	                    if (submenu != null)
	                    {
	                        html += "onmouseover='showMenu(this)' ";
	                        html += "onmouseout='hideMenu()' ";
	                    }
	                    html += "link='" + node.getAttribute("link") + "' ";
	                    html += "href='" + node.getAttribute("pageid") + "'>";
	                    html += node.getAttribute("cp");
	                    html += "</a>";
					}
                    if (submenu != null)
                    {
                        submenuHtml += "<ul class='dropmenu' id='" + submenu.getAttribute("id") + "' style='position:absolute;background:#EEF0F4;padding:2px 5px 5px 5px;text-align:left;display:none;'>";
                        //html += (parent_link == node.getAttribute("link")) ? "<ul>" : "<ul style='display:none;'>";
						/*
						if (parent_link == node.getAttribute("link"))
							alert(parent_link +"=="+ node.getAttribute("link"));
						else
							alert(parent_link +"!="+ node.getAttribute("link"));
						*/	
						html += "<ul>";
                        for (var s=0; s<submenu.childNodes.length; s++)
                        {
                            var subnode = submenu.childNodes[s];
                            html += "<li";
							html += (parent_link.toLowerCase() == node.getAttribute("link").toLowerCase()) ? "  " : " style='display:none;' ";
                            submenuHtml += "<li style='float:none;width:50px;white-space:nowrap;'>";
                            if (s == 0)
                                html += " class='firstListLink'";
                            html += "><a ";
                            submenuHtml += "<a onmouseover='showMenu(this, true)' onmouseout='hideMenu()' style='background:none;padding:3px;'";
                            if (subnode.getAttribute("pageid") == current)
                                html += "class='here' ";
                            html += "href='" + subnode.getAttribute("pageid") + "'>";
                            submenuHtml += "href='" + subnode.getAttribute("pageid") + "'>";
                            html += subnode.getAttribute("cp");
                            submenuHtml += subnode.getAttribute("cp");
                            html += "</a>";
                            submenuHtml += "</a>";
                            html += "</li>";
                            submenuHtml += "</li>";
                        }
						submenuHtml += "</ul>";
                        html += "</ul>";
                    }
                    html += "</li>";
                }
                html += "</ul>";
                //html += submenuHtml;
	document.getElementById("submenu_div").outerHTML = submenuHtml;
                document.getElementById(divid).innerHTML = html;                
            }
        }
        else if (navigator.userAgent.indexOf('Firefox') >= 0)
        {
        }
        else
        {
            alert('Your browser is not currently supported.');
            return;
        }
    }
    catch(e)
    {
        alert(e.message);
    }
}

function getCurrentPage()
{
    if (document.getElementById("refermenu"))
        return document.getElementById("refermenu").getAttribute("link");
    else
        return window.location.href.substring(window.location.href.lastIndexOf('/') + 1).replace(/&[a-zA-Z0-9_=]*/g, '').replace(/#[a-zA-Z0-9-_=]*/g, '').replace(/\?[a-zA-Z0-9-_=]*/g, '');
}

function getCurrentParent(current, xmldoc)
{
    var lv = xmldoc.selectSingleNode("//ApplicationMenu/Menu[@id!='appmenu' and @lv='1']/Level[@pageid='" + current + "']");
    if (lv != null)
    {
        var link = lv.parentNode.getAttribute("id");
        return xmldoc.selectSingleNode("//ApplicationMenu/Menu[@id='appmenu']/Level[@link='" + link + "']").getAttribute("pageid");
    }
    else
    {
        return current;
    }
}

function getCurrentParentLink(current, xmldoc)
{
    var lv = xmldoc.selectSingleNode("//ApplicationMenu/Menu[@id!='appmenu' and @lv='1']/Level[@pageid='" + current + "']");
    if (lv != null)
    {
        var link = lv.parentNode.getAttribute("id");
        return link
    }
    else
    {
        return "";
    }
}

var closeTimer = null;
var menuItem = null;
function showMenu(menu, keep)
{
    if (keep)
        cancelCloseTimer();
    if ($(menu).attr("link"))
    {
	    // Cancel close timer
	    cancelCloseTimer();

	    // Close old sub menu
	    doHideMenu();

	    // Get new sub menu and show it
	    menuItem = $(menu);
	    var ul = $("ul#" + menuItem.attr("link"));
	    ul.css("display", "block");
	    var offset = [$('#schSiteNav').position().left,$('#schSiteNav').position().top];
		ul.css("top", menuItem.scrollTop() + menuItem.outerHeight() + offset[1]);
        ul.css('left', menuItem.position().left + offset[0]);
     }
}

function doHideMenu()
{
	if(menuItem)
	{
	    $("ul#" + menuItem.attr("link")).css('display', 'none');
	}
}

function hideMenu()
{
	closeTimer = window.setTimeout(doHideMenu, 300);
}

function cancelCloseTimer()
{
	if(closeTimer)
	{
		window.clearTimeout(closeTimer);
		closeTimer = null;
	}
}