﻿// JScript File

var theMenu;
var menuEnabled;
var theTarget;

var enabledOptions = new Array();

function SetEnabledOptions(index, options)
{
  enabledOptions[index] = options;
}
       
//Turn of menu click
function EnableMenu()
{
  menuEnabled = true;
}

//Turn on menu click
function DisableMenu()
{
  menuEnabled= false;
}

function SetMenu(menu)
{
  theMenu = menu;
}

function SetTarget(targetId)
{
  theTarget = document.getElementById(targetId);
}

 function HideMenu()
{
    if(theMenu != null)
    {
        theMenu.Hide();
    }
}
         
function RowClick(index, e)
{
    if(theMenu != null)
    {   
        theMenu.Hide();
        
        if(theTarget != null)
        {
          theTarget.value = '-1';
        }
        
        if(menuEnabled)
        {
            UpdateMenu(index);
            theMenu.Show(e);
            
            if(theTarget != null)
            {
              theTarget.value = index;
            }
        }
    }
    
    e.cancelBubble = true;
    e.returnValue = true;
}

//Hide/enable items based on the row index
function UpdateMenu(index)
{
    //Get the allowed options
    if(enabledOptions[index] != null)
    {
        for(menuItemIndex in theMenu.AllItems)
        {
            EnableOptions(enabledOptions[index].split(','), theMenu.AllItems[menuItemIndex]);
        }
    }
}

function EnableOptions(enabledOptionsArray, menuItem)
{
    menuItem.Hide();
    
    for(optionIndex in enabledOptionsArray)
    {
        if(enabledOptionsArray[optionIndex] == menuItem.Value)
        {
           menuItem.Show();
        }
    }
}