﻿function highLightMenuItem(){
    
    var requestedPage = document.URL;
    // get all anchors on the page
    var anchors = document.getElementsByTagName('a');
    var selectedItem = null;
    var shortLink = null;
    // get the last piece of the requested page, example: home.aspx
    requestedPage = requestedPage.substring(requestedPage.lastIndexOf('/')+1).toUpperCase();
    
    if(requestedPage == "")
    {
        // if the requestedpage is empty set the home element on selected
        // loop through all anchors
        for(var i = 0 ; i < anchors.length ; i++){
            shortLink = anchors[i].getAttribute('href').toUpperCase()
            shortLink = shortLink.substring(shortLink.lastIndexOf('/')+1);
            
            // get the first item in the menu and exit the loop by calling break;
            if(anchors[i].parentNode.parentNode.parentNode.id.toUpperCase() == "MENU")
            {
                selectedItem = anchors[i]
                // set the class of that anchor
                selectedItem.className = 'active';
                break;
            }
        }
    }
    else
    {
        // loop through all anchors
        for(var i = 0 ; i < anchors.length ; i++){
        
            // get the last piece of the anchor link
            
            if(anchors[i].getAttribute('href') != null)
            {
                shortLink = anchors[i].getAttribute('href').toUpperCase()
                shortLink = shortLink.substring(shortLink.lastIndexOf('/')+1);
            }
            // compare it with the requested one
            if(shortLink == requestedPage)
            {
                // if those match
                selectedItem = anchors[i]
                
                // set the class of that anchor
                selectedItem.className = 'active';
            }
        }
    }
}