﻿getQueryString = function(name)
{
    var result = "";
    if (typeof(window.location.search) != "undefined")
    {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0; i<vars.length; i++) 
        {
            var pair = vars[i].split("=");
            if (pair[0].toLowerCase() == name.toLowerCase()) 
            {
                result = pair[1];
                break;
            }
        }
    }
    return result;
}

findElementByClass = function(tag, className, withinElement)
{
    var result = null;
    var tags = null;
    if ((typeof(withinElement) != "undefined") && (withinElement != null))
    {
        tags = withinElement.getElementsByTagName(tag);
    }
    else
    {
        tags = document.getElementsByTagName(tag);
    }
    className = className.toLowerCase();
    for (var i=0; i<tags.length; i++)
    {
        var classes = tags[i].className.toLowerCase().split(' ');
        for (var c=0; c<classes.length; c++)
        {
            if (classes[c] == className)
            {
                result = tags[i];
                break;
            }
        }
    }
    return result;
}

isDecendant = function(decendant, ancestor)
{
    return ((decendant.parentNode == ancestor)  || (decendant.parentNode != document) && isDecendant(decendant.parentNode, ancestor));
}

disableOrEnableAllFormElementIn = function(wrapper, disable)
{
    if ((typeof(document.forms) != "undefined") && (typeof(document.forms.length) != "undefined") && (document.forms.length == 1))
    {
        var theForm = document.forms[0];
        var wrapperElement = document.getElementById(wrapper);
        if ((wrapperElement != null) && (typeof(theForm.elements) != "undefined") && (typeof(theForm.elements.length) != "undefined") && (theForm.elements.length > 0))
        {
            for(i = 0; i < theForm.elements.length; i++)
            {
                var theElement = theForm.elements[i];
                if (isDecendant(theElement, wrapperElement) && (typeof(theElement.disabled) != "undefined"))
                {
                    theElement.disabled = disable;
                }
            }
        }
   }
}

dimMenu = function()
{
    var nav = findElementByClass("DIV", "SecondaryNav");
    if (nav != null)
    {
        nav.className += " DimMenu";
        var anchors = nav.getElementsByTagName("A");
        var spans = nav.getElementsByTagName("SPAN");
        var listitems = nav.getElementsByTagName("LI");
        for (var a=0; a<anchors.length; a++)
        {
            var element = anchors[a];
            if (element.className.length == 0)
            {
                element.className = "DimMenu";
            }
            else
            {
                element.className += " DimMenu";
            }
        }
        for (var s=0; s<spans.length; s++)
        {
            var element = spans[s];
            if (element.className.length == 0)
            {
                element.className = "DimMenu";
            }
            else
            {
                element.className += " DimMenu";
            }
        }
        for (var l=0; l<listitems.length; l++)
        {
            var element = listitems[l];
            if (element.className.length == 0)
            {
                element.className = "DimMenu";
            }
            else
            {
                element.className += " DimMenu";
            }
        }
    }

    nav = findElementByClass("DIV", "PrimaryNav");
    if (nav != null)
    {
        nav.className += " DimMenu";
    }
}

undimMenu = function()
{
    var nav = findElementByClass("DIV", "SecondaryNav");
    if (nav != null)
    {
        nav.className = "SecondaryNav";
        var anchors = nav.getElementsByTagName("A");
        var spans = nav.getElementsByTagName("SPAN");
        var listitems = nav.getElementsByTagName("LI");
        for (var a=0; a<anchors.length; a++)
        {
            var element = anchors[a];
            var start = element.className.indexOf(" DimMenu");
            if (start > -1)
            {
                element.className = element.className.substring(0, start);
            }
            else
            {
                element.className = "";
            }
        }
        for (var s=0; s<spans.length; s++)
        {
            var element = spans[s];
            var start = element.className.indexOf(" DimMenu");
            if (start > -1)
            {
                element.className = element.className.substring(0, start);
            }
            else
            {
                element.className = "";
            }
        }
        for (var l=0; l<listitems.length; l++)
        {
            var element = listitems[l];
            var start = element.className.indexOf(" DimMenu");
            if (start > -1)
            {
                element.className = element.className.substring(0, start);
            }
            else
            {
                element.className = "";
            }
        }
    }

    nav = findElementByClass("DIV", "PrimaryNav");
    if (nav != null)
    {
        nav.className = "PrimaryNav";
    }
}

hookUpNavEvents = function()
{
    var primaryNav = findElementByClass("DIV", "PrimaryNav");
    if (primaryNav != null)
    {
        var menuItems = primaryNav.getElementsByTagName("LI");
        for (var l=0; l<menuItems.length; l++)
        {
            // We don't want to do anything with the selected submenu (if it has one) since
            // it will already be showing in the SecondaryNav and we won't show
            // its corresponding dropdown.
            var menuItem = menuItems[l]; 
            if (menuItem.parentNode.className.toLowerCase() == "aspnet-menu") // just top-level menu items (not submenu items)
            {
                if ((menuItem.className.toLowerCase().indexOf("aspnet-menu-selected") == -1) &&
                    (menuItem.className.toLowerCase().indexOf("aspnet-menu-childselected") == -1))
                {
                    if ((typeof(menuItem.addEventListener) != "undefined") && (menuItem.addEventListener != null)) 
                    {
                        menuItem.addEventListener('mouseover', dimMenu , false);
                        menuItem.addEventListener('mouseout', undimMenu , false);
                    }
                    else 
                    {
                        menuItem.attachEvent('onmouseover', dimMenu);
                        menuItem.attachEvent('onmouseout', undimMenu);
                    }
                }
            }
        }
    }
}

adjustNavWidths = function()
{
    var primaryNav = findElementByClass("DIV", "PrimaryNav");
    var primaryLists = null;
    var primaryTopList = null;
    if (primaryNav != null)
    {
        primaryLists = primaryNav.getElementsByTagName("UL");
        for (var l=0; l<primaryLists.length; l++)
        {
            if (primaryLists[l].className.toLowerCase() == "aspnet-menu")
            {
                primaryTopList = primaryLists[l];
                break;
            }
        }
        
        // Adjust the widths and positions of the dropdown menus. These are the menus that appear when you
        // hover your mouse over a primary menu item that has a submenu (dropdown menu). We want these
        // dropdown menus to appear, if possible, with their left edge coincident with the left edge of the
        // menu item that you are hovering over. However, we want (even more) for the right edge of the dropdown
        // menu to go no further to the right then the right edge of the dashboard. Also, we need to set the
        // overall width of the dropdown to equal (or slightly exceed) the sum of the widths of its
        // items (which line up horizontally).
        
        for (var d=0; d<primaryLists.length; d++)
        {
            if (primaryLists[d].className.toLowerCase() != "aspnet-menu") // skip the topmost UL so we only consider the submenus
            {
                var dropdown = primaryLists[d];
                var items = dropdown.getElementsByTagName("LI");
                var dropdownRequiredWidth = 0;
                for (var j=0; j<items.length; j++)
                {
                    dropdownRequiredWidth += items[j].offsetWidth;
                }
                dropdownRequiredWidth += 1; // fudge
                dropdown.style.width = dropdownRequiredWidth + "px";
                
                if ((typeof(dropdown.offsetParent) != "undefined") && (dropdown.offsetParent != null) &&
                    (typeof(dropdown.offsetParent.offsetLeft) != "undefined") && (dropdown.offsetParent.offsetLeft != null) &&
                    (typeof(dropdown.offsetParent.offsetParent) != "undefined") && (dropdown.offsetParent.offsetParent != null) &&
                    (typeof(primaryNav.clientWidth) != "undefined") && (primaryNav.clientWidth != null) &&
                    ((dropdown.offsetParent.offsetLeft + dropdownRequiredWidth) > primaryNav.clientWidth))
                {
                    dropdown.style.left = (primaryNav.clientWidth - (dropdown.offsetParent.offsetLeft + dropdownRequiredWidth)) + "px"; 
                }
            }
        }        
        
        // If the secondary nav is showing then align its left edge with the corresponding dropdown (from the
        // primary nav menu item that it corresponds to).

        var secondaryNav = findElementByClass("DIV", "SecondaryNav");
        if (secondaryNav != null)
        {
            secondaryLists = secondaryNav.getElementsByTagName("UL");
            if (secondaryLists.length == 1) // this should be either 1 or 0
            {
                secondaryList = secondaryLists[0];
                var correspondingPrimaryMenuItem = findElementByClass("LI", "aspnet-menu-selected", primaryNav);
                if ((correspondingPrimaryMenuItem != null) && (correspondingPrimaryMenuItem.parentNode.parentNode.parentNode.className != "PrimaryNav"))
                {
                    correspondingPrimaryMenuItem = findElementByClass("LI", "aspnet-menu-childselected", primaryNav);
                }
                if ((correspondingPrimaryMenuItem != null) && (correspondingPrimaryMenuItem.parentNode.parentNode.parentNode.className == "PrimaryNav"))
                {
                    var hoverMenus = correspondingPrimaryMenuItem.getElementsByTagName("UL");
                    if (hoverMenus.length == 1) // this should be exactly 1 but let's check to be sure
                    {
                        var hoverMenu = hoverMenus[0];
                        if (hoverMenu.style.left != "")
                        {
                            secondaryList.style.left = (primaryNav.clientWidth - hoverMenu.clientWidth + 2) + "px";
                        }
                        else
                        {
                            secondaryList.style.left = (hoverMenu.parentNode.offsetLeft + 1) + "px";
                        }
                        hoverMenu.style.display = "none";
                    }
                }
            }
        } 
    }
}

initWebTrends = function()
{
    if (document.cookie.indexOf(gFpc+"=") == -1)
    {
        var host = "";
        if (typeof(window.location.hostname) != "undefined")
        {
            host = window.location.hostname.toLowerCase();
        }
        else if (typeof(window.location.host) != "undefined")
        {
            host = window.location.host.toLowerCase();
        }
        if ((host.indexOf("microsoft.com") == 0) || (host.indexOf("www.microsoft.com") == 0))
        {
            document.write("<SCR"+"IPT TYPE='text/javascript' SRC='"+"http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+gDomain+"/"+gDcsId+"/wtid.js"+"'><\/SCR"+"IPT>");
        }
    }
}

initMsAnalytics = function()
{
    if ((typeof(msAnalytics) == "undefined") || (msAnalytics == null) ||
        (typeof(msAnalytics.ProfileId) == "undefined") ||
        (typeof(msAnalytics.TrackPage) == "undefined"))
    {
        window.setTimeout("initMsAnalytics()", 5000);
    }
    else
    {
        msAnalytics.ProfileId = 'D442';
        msAnalytics.TrackPage();
    }
}

atlas = function (uri) 
{
    var timestamp = new Date();
    var qs = "?qstr=random=" + Math.ceil(Math.random() * 99999999) + timestamp.getUTCFullYear() + timestamp.getUTCMonth() + timestamp.getUTCDate() + timestamp.getUTCHours() + timestamp.getUTCMinutes() + timestamp.getUTCSeconds() + timestamp.getUTCMilliseconds();
    var uriPlus = uri + qs;

    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = uriPlus;

    document.getElementsByTagName('head')[0].appendChild(script);
}

/*
if ((typeof(window.addEventListener) != 'undefined') && (window.addEventListener != null)) 
{
    window.addEventListener('onload', initWebTrends, false);
//    window.addEventListener('onload', initMsAnalytics, false);
}
else 
{
    window.attachEvent('onload', initWebTrends);
//    window.attachEvent('onload', initMsAnalytics);
}
*/
