    /////////////////////////////////////////////////////////////
    // Identify page as enabled for Media Center. This avoids a warning dialog to user.
    function IsMCEEnabled()
    {
        return true
    }

    /////////////////////////////////////////////////////////////////
    // Initialization
    function Initialize()
    {
      // Set the Homeportal default background color
      try {
         window.external.MediaCenter.BGColor = "RGB(19,32,102)";
      }
      catch(e) {
         // Ignore error.
      }
      // if current focus is on shared or custom viewport (i.e. when you hit Back after you go to full screen for
      // the media playback), don't try to do setfocus
      if (nCurFocus == "SVP" || nCurFocus == "CVP")
      {
          return;
      }
      SetFocus();
    }

    /////////////////////////////////////////////////////////////////
    // disable right click
    function Disable()
    {
        if (event.button == 2)
            try
            {
                 window.external.MediaCenter.Dialog("Right-click is disabled","",1,30,true);
            }
            catch(e)
            {
                alert("Right-click is disabled");
            }
    }

    /////////////////////////////////////////////////////////////////
    // user selected item, navigate to it based on DestinationArray
    function DoNavigation()
    {
        // make sure focus is not on a viewport
        if ((nCurFocus == "SVP") || (nCurFocus == "CVP"))
        {
            return  
        }

        try
        {    
            // if array element is not empty
            if (aFocusTableArray[nCurFocus-1][10] != "")
            {
                try
                {
                    // try to run as a function; if element string is a link instead of a function, create an error
                    eval(aFocusTableArray[nCurFocus-1][10]);
                    // If element is a link starting with "http" it will not cause error;
                    // so check if string starts with "http"; if so, treat as a link
                    try 
                    {
                        var sArraySubStr = (aFocusTableArray[nCurFocus-1][10]).substring(0, 4)
                        if (sArraySubStr == "http")
                        {
                            window.navigate(aFocusTableArray[nCurFocus-1][10]);
                        }
                    }
                    catch(e)
                    {
                        // ignore error
                    }
                }
                catch(e)
                {
                    // if previous throws error, treat as a link
                    window.navigate(aFocusTableArray[nCurFocus-1][10]);
                }
            }
        }
        catch(e)
        {
            try
            {
                window.external.MediaCenter.Dialog("Number: " + e.number + "\n\nDescription: " + e.description,"DoNavigation() Error",1,30,true); 
            }
            catch(e)
            {
                alert("DoNavigation() Error\n\nNumber: " + e.number + "\n\nDescription: " + e.description);
            }
        }
    }


    /////////////////////////////////////////////////////////////////
    // focus on item in list based on nCurFocus value and user input
    function SetFocus(nKey)
    {
        try
        {
            /////////////////////////////////////////////////////////////////////////////////
            // reset mouse variable to nullify mouseover event, which can fire after keypress    
            nMouseEvent = 0;

            //////////////////////////////////////////////////////////////////////////////////
            // hold onto current focus in case it gets broken along the way

            var nPrevFocus = nCurFocus;

            //////////////////////////////////////////////////////////////////////////////////
            // update nCurFocus values based on key selection
            // on first visit to page, nothing to do because nCurFocus is initialized

            //first, check if previous focus is on a viewport
            if (nPrevFocus == "CVP")
            {
                // check correct array for new focus item
                nCurFocus = aCustomViewportArray[nKey];
            }
            else if (nPrevFocus == "SVP")
            {
                // check correct array for new focus item
                nCurFocus = aSharedViewportArray[nKey];
            }
            // if previous focus is not on a viewport, find new focus item in regular array
            else
            {
                if (nKey >= 0)
                {
                    nCurFocus = aFocusTableArray[nCurFocus-1][nKey];
                }
            }

           //////////////////////////////////////////////////////////////////////////////////
            // user tried an illegal move, reset nCurFocus to known state
            if (nCurFocus == -1)
            {
                nCurFocus = nPrevFocus;
                return;
            }
            DrawFocus(nPrevFocus, nCurFocus);

        }
        catch(e)
        {
            try
            {
                window.external.MediaCenter.Dialog("Number: " + e.number + "\n\nDescription: " + e.description,"SetFocus() Error",1,30,true); 
            }
            catch(e)
            {
                alert("SetFocus() Error\n\nNumber: " + e.number + "\n\nDescription: " + e.description);
            }
        }
    }


    /////////////////////////////////////////////////////////////////
    // DrawFocus
    function DrawFocus(nPrevFocus, nCurFocus)
    {
        //alert ("Starting DrawFocus (" + nPrevFocus + ", " + nCurFocus + ")");
        try
        {
            // if previous item is a viewport
            if ((nPrevFocus == "SVP") || (nPrevFocus == "CVP"))
            {
            // remove focus from viewport
                if (nPrevFocus == "SVP")
                {
                    try
                    {
                       // add code to remove focus from viewport (not supported yet)
                    }
                    catch(e)
                    {
                        //Viewport is hidden; ignore error
                    }
                }
                if (nPrevFocus == "CVP")
                {
                    try
                    {
                       // add code to remove focus from viewport (not supported yet)
                    }
                    catch(e)
                    {
                        //Viewport is hidden; ignore error
                    }
                }
            }
            else
            {
                // remove focus from the previous item if different
                if (nPrevFocus != nCurFocus)
                {
                    var prevItem = eval('item' + nPrevFocus);
                    // check if previous item is "enforced" (to indicate active category)
                    try
                    {
                        if (prevItem.id == strEnforcedItem)
                        {
                            prevItem.className = strEnforcedItemNoFocusStyle;
                        }
                        else
                        {
                            prevItem.className = aFocusTableArray[nPrevFocus-1][5];
                        }
                        // Call OnFocusChange function
                        if (aFocusTableArray[nPrevFocus-1][11] != null)
                           aFocusTableArray[nPrevFocus-1][11] (prevItem, false);
                    }
                    catch(e)
                    {
                        prevItem.className=aFocusTableArray[nPrevFocus-1][5];
                    }
                }
            }

            // Play "click" sound
            playFocusSound();

            // set buttons in current array to non-dormant, and buttons in other
            // arrays to dormant
            setDormant(nCurFocus, nPrevFocus);

            // if new item is shared viewport
            if (nCurFocus == "SVP")
            {
                  // check if shared viewport is visible
                try
                {
                    if (window.external.MediaCenter.SharedViewPort.Visible == true)
                    {
                        window.external.MediaCenter.SharedViewPort.Focus(); // focus on shared viewport
                    }
                    else
                    {
                        // viewport is hidden, so reset focus to backup item in viewport array, by setting nKey to 4
                        SetFocus(4);
                    }
                }

                catch(e)
                {
                    SetFocus(4);
                }

                return;
            }
            //if new item is custom viewport
            else if (nCurFocus == "CVP")
              {
                  // check if custom viewport is visible
                try
                {
                    if (window.external.MediaCenter.CustomViewPort.visible == true)
                    {
                        window.external.MediaCenter.CustomViewPort.Focus(); // focus on custom viewport
                    }
                    else
                    {
                        // viewport is hidden, so reset focus to backup item in viewport array, by setting nKey to 4
                        SetFocus(4);
                    }
                }

                catch(e)
                {
                    SetFocus(4);
                }

                return;
              }

            // give focus to the new item
            var newItem = eval('item' + nCurFocus)

            // check if new item is "enforced" (to indicate active category)
            try
            {
                if (newItem.id == strEnforcedItem)
                {
                    // reset style
                    newItem.className = strEnforcedItemFocusStyle;
                }
                else
                {
                    // reset style
                    newItem.className=aFocusTableArray[nCurFocus-1][4];
                }
               // Call OnFocusChange function
               if (aFocusTableArray[nCurFocus-1][11] != null)
                  aFocusTableArray[nCurFocus-1][11] (newItem, true);
            }
            catch(e)
            {
                newItem.className=aFocusTableArray[nCurFocus-1][4];
            }

            // determine if the page has a scrolling menu
            try
            {

                if  (strScrollPage == "yes")
                {
                    scrollMenu(nCurFocus);
                }
            }
            catch (e)
            {
                //ignore error
            }

            // Custom (optional) function if needed; locate this function on the HTML page
            try
            {
                doOnFocus();
            }
            catch(e)
            {
                //ignore error
            }
        }
        catch(e)
        {
            try
            {
                window.external.MediaCenter.Dialog("Number: " + e.number + "\n\nDescription: " + e.description,"DrawFocus() Error",1,30,true);    
            }
            catch(e)
            {
                alert("DrawFocus() Error\n\nNumber: " + e.number + "\n\nDescription: " + e.description);
            }
        }
    }

    /////////////////////////////////////////////////////////////////
    // Scaling elements for page resize 
    function onScaleEvent(vScale)
    {
        try
        {
            body.style.zoom=vScale;
        }
        catch(e)
        {
            // ignore error
        }
    }

    /////////////////////////////////////////////////////////////////
    // determine which remote control key the user selected 
    // and take appropriate action
    function onRemoteEvent(keyChar)
    {
        try
        {
            switch (keyChar)
            {
            case 0x26:  // Up button selected
                SetFocus(0);
                return true;
                break;

            case 0x28:  // Down button selected
                SetFocus(1);
                return true;
                break;

            case 0x25:  // Left button selected
                SetFocus(2);
                return true;
                break;

            case 0x27:  // Right button selected
                SetFocus(3);
                return true;
                break;

            case 0x0D:    // Enter button selected, execute link to content/page
                DoNavigation();
                return true;
                break;

            case 0x08:  // Keyboard Backspace selected
                return false;
                break;
                
            case 0xA6:            // Browser Back button selected; Media Center will already perform a Back
                return false;      // navigation when this is pressed, but this case can be used to add additional
                break;             //functionality to Back button

            case 0x21:  // Page up (plus) selected; page-up scrolling menu
                        // (currently works for keyboard pg-up, but not for remote key)
                pageUp();
                return true;
                break;

            case 0x22:  // Page down (minus) selected; page-down scrolling menu
                        // (currently works for keyboard pg-down, but not for remote key)
                pageDown();
                return true;
                break;

            case 0x1B:  // Clear button selected
               try {
                  if (eval('item' + nCurFocus).tagName == "INPUT")
                     doOnClear();
               }
               catch (e) {
               }
               return true;
               break;

            default:
                return false;
                // ignore all other clicks
            }
        }
        catch(ex)
        {
            logEx("onRemoteEvent(" + keyChar + ")", ex);
        }
    }

    /////////////////////////////////////////////////////////////////
    // SetCounter
    function setCounter()
    {
        //if page contains a scrolling button menu, set counter values
        if (strScrollPage == "yes")
        {
            // Make the menu's item counter visible
            itemCounterSpan.style.display = "block";
            // Show total number of buttons in menu
            counterTotal.innerText = eval(nTotalBtns);
        }
    }

    /////////////////////////////////////////////////////////////////
    // Sounds
    function playFocusSound()
    {
        btnSound.src = strFocusSound;
    }

    // ----------------- Start code to scroll menu ------------------                         
    function scrollMenu(nCurFocus)
    {
        var nFocusBtnRow
        //test for number of columns
        try 
        {
            var nCols = nScrollMenuBtnCols
        }
        catch(e)
        {
            var nCols = 1
        }

            // if selected item button is not on the scrolling menu table, gray-out counter and end function
        if ((nCurFocus < nScrollMenuStartNo) || (nCurFocus > nScrollMenuEndNo))
        {
            itemCounterSpan.style.filter = "alpha(opacity=70)";
            return
        }
        // determine what position the current-focus button occupies in list table by
        // subtracting the number of buttons that occur on the page before the 
        // menu starts (nScrollMenuStartNo - 1) from the item number
        // of the current-focus button (nCurFocus)
        var nFocusBtnPosition = (nCurFocus - (nScrollMenuStartNo - 1));
            // determine what row in list table current-focus button occupies
        if (Math.floor((nFocusBtnPosition) / nCols) < ((nFocusBtnPosition) / nCols))
        {
            nFocusBtnRow = (Math.floor((nFocusBtnPosition) / nCols) + 1)
        }
        else 
        {
            nFocusBtnRow = ((nFocusBtnPosition) / nCols)
        }

        // reset counter
        resetCounter(nCurFocus);

        // set a variable for the current position (# of button spaces offset) of 
        // the top of the list table
        var nTableTopPosition = (listTable.offsetTop / -(nBtnHeight));

        // determine whether the current-focus button is out of view, above or below; scroll accordingly

        if (nFocusBtnRow > (nVisibleBtns + nTableTopPosition))
        {
            // focus btn is below view, so scroll down
            listTable.style.top = (listTable.offsetTop - nBtnHeight)    
        }
        if (nFocusBtnRow <= nTableTopPosition)
        {
            // focus btn is above view, so scroll up
            listTable.style.top = ((nFocusBtnRow - 1) * -(nBtnHeight))
        }
    }
    // ----------------- End code to scroll menu ------------------
    
    function parentTable(elem)
    {
       var leftMenu = document.all.leftMenu;
       var listTable = document.all.listTable;
       var homepageTable = null;
       var bottomMenu = null;
       try {
          bottomMenu = document.all.BottomMenu;
       }
       catch(e)
       { }
       try {
          homepageTable = document.all.HomepageButton;
       }
       catch(e)
       { }
       while (elem != leftMenu && elem != listTable && elem != bottomMenu && elem != homepageTable && elem.parentElement != null)
          elem = elem.parentElement;
       return elem;
    }

   function setClassOfAll(parTable, index)
   {
      var i;
      for (i = 0; i < aFocusTableArray.length; i++) {
         try {
            var elem = eval ('item' + (i+1));
            if (parentTable(elem) == parTable)
               elem.className = aFocusTableArray[i][index];
         }
         catch(e)
         {
         }
      }
   }

    // ----------------- Start code to set non-selected menu to dormant ------------------
    function setDormant(nCurFocus, prevItemNo)
    // NOTE: this function does not work for every item; it is made to catch errors and ignore them
    {
        try
        {
            // if strDormantPage variable on HTML page is not set to "yes" then end function
            if (strDormantPage != 'yes')
            {
                return;
            }
        }
        catch(e)
        {
        // ignore error
        }

        try
        {
        // find previous button
            var oPrevBtn = eval('item' + prevItemNo);
            // find outer tables for previous button
            var oPrevParentTable = parentTable(oPrevBtn); // oPrevBtn.parentElement.parentElement.parentElement.parentElement;
        }
        catch(e)
        {
            //ignore error
            // can't set previous item to dormant
        }

        try
        {
            // find current button
            var oCurrentBtn = eval('item' + nCurFocus);
            // find outer tables for current button
            var oCurParentTable = parentTable(oCurrentBtn); // oCurrentBtn.parentElement.parentElement.parentElement.parentElement;
        }
        catch(e)
        {
            //ignore error
            // can't set current item to dormant
        }

        // if previous and current buttons are in the same table, do nothing
        try
        {
            if (oPrevParentTable == oCurParentTable)
            {
                return;
            }
        }
        catch(e)
        {
            //ignore error
        }

        try
        {
            // set previous-table items to dormant
            setClassOfAll(oPrevParentTable, 6);
            // reset type style in each cell
            oPrevParentTable.className=aFocusTableArray[(prevItemNo-1)][7];
            //if enforced item is in previous table, make it dormant too
            try
            {
                // if(eval(strEnforcedItem).parentElement.parentElement.parentElement.parentElement == oPrevParentTable)
                if(parentTable(eval(strEnforcedItem)) == oPrevParentTable)
                {
                    eval(strEnforcedItem).className = strEnforcedItemDormantStyle
                }
            }
            catch(e)
            {
            //ignore error
            }
        }
        catch(e)
        {
        //ignore error
        }

        // set current button's menu to active (non-dormant)
        try
        {
            setClassOfAll(oCurParentTable, 5);
            // reset type style in all of parent table
            oCurParentTable.className=aFocusTableArray[(nCurFocus-1)][8];
            //if enforced item is in current table, make it active too
            try
            {
                // if(eval(strEnforcedItem).parentElement.parentElement.parentElement.parentElement == oCurParentTable)
                if(parentTable(eval(strEnforcedItem)) == oCurParentTable)
                {
                    eval(strEnforcedItem).className = strEnforcedItemNoFocusStyle
                }
            }
            catch(e)
            {
            //ignore error
            }
        }
        catch(e)
        {
            //ignore error
        }
    }
    // ----------------- End code to set non-selected menu to dormant ------------------


    // ----------------- Start code to reset counter number values (for scrolling menus) ------------------
    function resetCounter(nCurFocus)
    {
        var nFocusBtnPosition = (nCurFocus - (nScrollMenuStartNo - 1));
        // un-gray counter
        itemCounterSpan.style.filter = ""; // was alpha(opacity=100) but this is ugly as it ruins font antialiasing
        // display position of currently selected btn in counter
        counterNum.innerText = eval(nFocusBtnPosition);
        // gray out up arrow if focus in on top button
        if (nCurFocus == nScrollMenuStartNo)
        {
            arrowUp.disabled = true;
            arrowUp.style.filter = "alpha(opacity=20)";
        }
        else
        {
            arrowUp.disabled = false;
            arrowUp.style.filter = "alpha(opacity=70)";
        }
        // gray out down arrow if focus in on bottom button
        if (nCurFocus == nScrollMenuEndNo)
        {
            arrowDown.disabled = true;
            arrowDown.style.filter = "alpha(opacity=20)"
        }
        else
        {
            arrowDown.disabled = false;
            arrowDown.style.filter = "alpha(opacity=70)"
        }
    }
    // ----------------- End code to reset counter number values  ------------------


    // --------------- Start code for Paging up/down (for scrolling menus) ---------------------------
    try 
    {
        var nPageDistance = ((nVisibleBtns * nBtnHeight) - nBtnHeight)        // Maximum distance to scroll for one "page" increment
    }
    catch(e)
    {
        nPageDistance = 0;
    }
    
    function pageDown()
    {
        // test to see if focus is on a table that should scroll
        // NOTE: this test assumes that the ID for your scrolling table is "ListTable"
        try
        {
            // If ID for scrollong table is not "listTable" then return.
            if (parentTable(eval("item" + nCurFocus)) != listTable)
            // if (eval("item" + nCurFocus).parentElement.parentElement.parentElement.parentElement.id != "listTable")
            {
                return
            };
        }
        catch(e)
        {
            return
        }
        // determine if listTable has cellspacing; will need to subtract extra spacing from bottom of table
        var nTablePadding = eval(listTable.getAttribute("cellspacing"));
        // variables
        nPageDistance = ((nVisibleBtns * nBtnHeight) - nBtnHeight);                        // Maximum distance to scroll for one "page" increment

        // if there is just one big selectable row showing at a time in the scrollable menu, treat paging down the same as
        // scrolling down one item at a time (call setFocus function, passing the down-arrow key code).
        if (nPageDistance < nBtnHeight)
        {
            SetFocus(1);
            return
        }

        var nMaxPageDistance = ((listTable.offsetHeight - nTablePadding) - nPageDistance); // Maximum total paging distance
        var nCurrentScrollAmnt = -listTable.offsetTop;                                     // amount already scrolled
        //test for number of columns
        try
        {
            var nCols = nScrollMenuBtnCols
        }
        catch(e)
        {
            var nCols = 1
        }

        // if selected item button is not on the scrolling menu table, end function
        if ((nCurFocus < nScrollMenuStartNo) || (nCurFocus > nScrollMenuEndNo))
        {
            return
        }

        var nFocusBtnPosition = (nCurFocus - (nScrollMenuStartNo - 1));

        // determine what row in list table current-focus button occupies
        if (Math.floor((nFocusBtnPosition) / nCols) < ((nFocusBtnPosition) / nCols))
        {
            nFocusBtnRow = (Math.floor(nFocusBtnPosition / nCols) + 1)
        }
        else
        {
            nFocusBtnRow = ((nFocusBtnPosition) / nCols)
        }
        // determine last row in menu
        var nLastRow = Math.floor((nScrollMenuEndNo - (nScrollMenuStartNo - 1)) / nCols)

        if (Math.floor((nScrollMenuEndNo - (nScrollMenuStartNo - 1)) / nCols) < ((nScrollMenuEndNo - (nScrollMenuStartNo - 1)) / nCols))
        {
            nLastRow = ((Math.floor((nScrollMenuEndNo - (nScrollMenuStartNo - 1)) / nCols)) + 1)
        }
        else
        {
            nLastRow = ((nScrollMenuEndNo - (nScrollMenuStartNo - 1)) / nCols)
        }
        // find which column current focus btn is in (multiply number of previous row by # of columns, and subtract from nFocusBtnPosition)    
        var nFocusCol = (nFocusBtnPosition - ((nFocusBtnRow - 1) * nCols));
        // bottom visible row
        var nBottomVisRowNo = ((nCurrentScrollAmnt / nBtnHeight) + nVisibleBtns);
        if (nBottomVisRowNo >= nLastRow)
        {
            nBottomVisRowNo = nLastRow;
        }

        // determine which button is in focus col of bottom visible row
        var nNewFocusPosition = (((nBottomVisRowNo -1) * nCols) + nFocusCol);
        var nNewFocusBtnNo = (nNewFocusPosition + (nScrollMenuStartNo - 1));
        if (nNewFocusBtnNo > nScrollMenuEndNo)
        {
            nNewFocusBtnNo = nScrollMenuEndNo;
        }

        // if menu has already scrolled to bottom, set focus to last button and end function
        if (listTable.offsetHeight <= ((nVisibleBtns * nBtnHeight) + nCurrentScrollAmnt + nTablePadding))
        {

            DrawFocus(nCurFocus, nNewFocusBtnNo);
            nCurFocus = nNewFocusBtnNo;
            //reset counter
            resetCounter (nCurFocus);
            return;
        }

        // If current-focus btn is not in bottom visible row, assign focus there, do resets, and end function
        if (nCurFocus != nNewFocusBtnNo)
        {
            DrawFocus(nCurFocus, nNewFocusBtnNo);
            nCurFocus = nNewFocusBtnNo;
            //reset counter
            resetCounter (nCurFocus);
            return;
        }

        // move page down
        listTable.style.top = -(nCurrentScrollAmnt + nPageDistance);

        // update variables
        nCurrentScrollAmnt = -listTable.offsetTop;
        nBottomVisRowNo = ((nCurrentScrollAmnt / nBtnHeight) + nVisibleBtns);
        if (nBottomVisRowNo >= nLastRow)
        {
            nBottomVisRowNo = nLastRow;
        }

        // update which button is in bottom visible row, focus col -- based on new position
        nNewFocusPosition = (((nBottomVisRowNo -1) * nCols) + nFocusCol);
        nNewFocusBtnNo = (nNewFocusPosition + (nScrollMenuStartNo - 1));
        if (nNewFocusBtnNo > nScrollMenuEndNo)
        {
            nNewFocusBtnNo = nScrollMenuEndNo;
        }

        //reset focus to bottom visible btn
        DrawFocus(nCurFocus, nNewFocusBtnNo);
        nCurFocus = nNewFocusBtnNo;
        //reset counter
        resetCounter (nCurFocus);
    }


    //----------------------------Page Up ---------------------------------------------
    function pageUp()
    {
        // test to see if focus is on a table that should scroll
        // NOTE: this test assumes that the ID for your scrolling table is "ListTable"
        try
        {
            // If ID for scrollong table is not "listTable" then return
            if (parentTable(eval("item" + nCurFocus)) != listTable)
                return;
        }
        catch(e)
        {
            return;
        }

        // In case any erroneous mouseover event calls useMouse function, set nMouseEvnet value to 0
        nMouseEvent = 0;
        // determine if listTable has cellspacing; will need to subtract extra spacing from bottom of table
        var nTablePadding = eval(listTable.getAttribute("cellspacing"));
        // variables
        nPageDistance = ((nVisibleBtns * nBtnHeight) - nBtnHeight);                         // Maximum distance to scroll for one "page" increment

        // if there is just one big selectable row showing at a time in the scrollable menu, treat paging up the same as
        // scrolling up one row at a time (to do this, call setFocus function, passing the up-arrow key code).
        if (nPageDistance < nBtnHeight)
        {
            SetFocus(0);
            return;
        }
        var nMaxPageDistance = ((listTable.offsetHeight - nTablePadding) - nPageDistance);  // Maximum total paging distance
        var nCurrentScrollAmnt = -listTable.offsetTop;                                      // amount already scrolled

        //test for number of columns
        try 
        {
            var nCols = nScrollMenuBtnCols;
        }
        catch(e)
        {
            var nCols = 1;
        }

        // if selected item button is not on the scrolling menu table, end function
        if ((nCurFocus < nScrollMenuStartNo) || (nCurFocus > nScrollMenuEndNo)) {
            return;
        }

        var nFocusBtnPosition = (nCurFocus - (nScrollMenuStartNo - 1));

        // determine what row in list table current-focus button occupies
        if (Math.floor((nFocusBtnPosition) / nCols) < ((nFocusBtnPosition) / nCols))
        {
            nFocusBtnRow = (Math.floor(nFocusBtnPosition / nCols) + 1);
        }
        else
        {
            nFocusBtnRow = ((nFocusBtnPosition) / nCols);
        }

        // find which column current focus btn is in (multiply number of previous row by # of columns, and subtract from nFocusBtnPosition)    
        var nFocusCol = (nFocusBtnPosition - ((nFocusBtnRow - 1) * nCols));

        // top visible row
        var nTopVisRowNo = ((nCurrentScrollAmnt / nBtnHeight) + 1);

        // determine which button is in target col of top visible row
        var nNewFocusPosition = (((nTopVisRowNo -1) * nCols) + nFocusCol);

        var nNewFocusBtnNo = (nNewFocusPosition + (nScrollMenuStartNo - 1));

        // if menu is already at top, set focus to top btn, reset counter, and end function
        if (listTable.offsetTop >= 0)
        {
            DrawFocus(nCurFocus, nNewFocusBtnNo);
            nCurFocus = nNewFocusBtnNo;
            resetCounter (nCurFocus);
            return;
        }

        // If focus is not on top visible button in column, assign focus there and end function    
        if (nCurFocus != nNewFocusBtnNo)
        {
            DrawFocus(nCurFocus, nNewFocusBtnNo);
            nCurFocus = nNewFocusBtnNo;
            // reset counter
            resetCounter (nCurFocus);
            return;
        }

        // move page up
        if ((-nCurrentScrollAmnt + nPageDistance) < 0) 
        {
            listTable.style.top = (-nCurrentScrollAmnt + nPageDistance);
        }
        else 
        {
            listTable.style.top = 0;
        }

        // update variables
        nCurrentScrollAmnt = -listTable.offsetTop;
        nTopVisRowNo = ((nCurrentScrollAmnt / nBtnHeight) + 1);
        nNewFocusPosition = (((nTopVisRowNo -1) * nCols) + nFocusCol);
        nNewFocusBtnNo = (nNewFocusPosition + (nScrollMenuStartNo - 1));

        //reset focus to top visible btn
        DrawFocus(nCurFocus, nNewFocusBtnNo);
        nCurFocus = nNewFocusBtnNo;

        //reset counter
        resetCounter (nCurFocus);
    }
    // --------------------------------- End code for Paging up/down ----------------------------------------------------

    //----------------------------- Start code for switching to mouse ------------------------------------
    function useMouse(newItem)
    {
        if (nMouseEvent == 0)
        {
            nMouseEvent = 1;
            return;
        }
        var nNewItemNo = (newItem.children(0).id).substring(4);
        var nPrevFocus = nCurFocus;
        nCurFocus = nNewItemNo;
        DrawFocus(nPrevFocus, nCurFocus);
    }

    // use if item is not in table
    function useMouse2(newItem)
    {
        if (nMouseEvent == 0)
        {
            nMouseEvent = 1;
            return;
        }
        var nNewItemNo = newItem.id.substring(4);
        var nPrevFocus = nCurFocus;
        nCurFocus = nNewItemNo;
        DrawFocus(nPrevFocus, nCurFocus);
    }
    //----------------------------- End of code for switching to mouse ------------------------------------

    // Temporary "highlight" effect when button is clicked  state, or clicked state
    function btnHiLite (srcBtn, style)
    {
        try
        {
            if ((strEnforcedItem == srcBtn.id) && (window.event.type == 'mouseup'))
            {
                style = strEnforcedItemFocusStyle;
            }
        }
        catch(e)
        {
        }
        srcBtn.className = style;
    }
    
    function OnGridFocusChange(srcItem, newState)
    {
        // Called when there is a change to the focus state of an icon grid
        if (srcItem == null)
           return;
        if (newState == true) {
           srcItem.parentElement.style.filter="progid:DXImageTransform.Microsoft.Glow(color=yellow,strength=6)";
           srcItem.parentElement.style.paddingTop=0;
           srcItem.parentElement.style.paddingLeft=0;
           //srcItem.parentElement.parentElement.parentElement.parentElement.className = "gridItemCellFocus";
        }
        else {
           srcItem.parentElement.style.filter="";
           srcItem.parentElement.style.paddingTop=6;
           srcItem.parentElement.style.paddingLeft=6;
           //srcItem.parentElement.parentElement.parentElement.parentElement.className = "gridItemCell";
        }
    }

    // Set button for enforced state (add code as necessary)
    function btnEnforced(btn)
    {
    }

    //----------------------------- Start code for Spinner control ------------------------------------
    // args are: (string to indicate whether to move up or down, number to identify which spinner to set)
    function countSpinner(sPlusMinusStr, nSpinnerNumber)
    {
        // check for disabled button
        if (eval("item" + nCurFocus).disabled == true)
        {
            return;
        }

        // determine which array to use
        var sCurrentArray = "aSpinnerArray" + nSpinnerNumber
        // determine which element is currently shown
        var nTempElementCount = eval("aSpinnerArray" + nSpinnerNumber).count
        // if user clicked Plus, move element count forward by one
        if (sPlusMinusStr == "plus")
        {
            nTempElementCount = nTempElementCount + 1
        }
        // if user clicked Minus, move element count back by one
        if (sPlusMinusStr == "minus")
        {
            nTempElementCount = nTempElementCount - 1
        }

        // Call function to set spinner text
        setSpinner(sCurrentArray, nSpinnerNumber, nTempElementCount)

        // disable plus/minus buttons as needed
        if (nTempElementCount >= eval(sCurrentArray).length -1)
        {
            // disable second button in span (should be plus button)
            eval("item" + nCurFocus).parentElement.children[1].disabled = true
        }
        else
        {
            // enable second button in span (should be plus button)
            eval("item" + nCurFocus).parentElement.children[1].disabled = false
        }

        if (nTempElementCount <= 0)
        {
            // disable first button in span (should be minus button)
            eval("item" + nCurFocus).parentElement.children[0].disabled = true
        }
        else
        {
            // enable first button in span (should be minus button)
            eval("item" + nCurFocus).parentElement.children[0].disabled = false
        }

        //reset element count    
        eval(sCurrentArray).count = nTempElementCount;

    }

    /////////////////////////////////////////////////////////////////
    // setSpinner
    function setSpinner(sCurrentArray, nSpinnerNumber, nTempElementCount)
    {
        //determine which spinner box to update
        var oCurBox = eval("spinnerBox" + nSpinnerNumber)
        // update box text with contents of array element
        oCurBox.innerHTML = (eval(sCurrentArray)[nTempElementCount]);
    }
    
   function browseWithoutStack(sUrl)
   {
      // Browse to a page without adding to the 'Back' button stack
      document.location.replace(sUrl);
   }

   function toggleCheckBox(btn, checkImage)
   {
	   if (btn.checked == "no") {
		   btn.checked = "yes";
		   btn.style.backgroundImage = checkImage;
	   } 
	   else {
		   btn.checked = "no";
		   btn.style.backgroundImage = "none";
	   }
   }

   function doLogout ()
   {
      // Ask the user to confirm the logout.
      var strMsg = "Are you sure you want to exit Homeportal?";
      var iRet = 3; // 2 = Yes, 3 = No
      try {
         iRet = window.external.MediaCenter().Dialog(strMsg, "Confirmation", 12, 30, true);
         var strRet = iRet.toString();
      }
      catch(e) {
         var bRet = confirm (strMsg);
         if (bRet == true)
            iRet = 2;
         var strRet = iRet.toString();
      }
      if (iRet == 2) {
         window.navigate ("Logout.aspx");
      }
   }

   function doAlert (strCaption, strMsg)
   {
	   try {
		   window.external.MediaCenter().Dialog(strCaption, strMsg, 1,30,true);
	   }
	   catch(e) {
		   alert(strCaption + "\n\n" + strMsg);
	   }
   }


