var defaultSubmitControls = new Array();

// Assigns focus to a control.
//   - <controlName> is either a real control name or wildcard (as regular expression).
//   - <index> of control when such name exists more then 1 time.
function FocusControl( controlName, index )
{
    try
    {
        var c = GetControl( controlName, index );
        if ( c != null )
        {
            c.focus();
        }
    }
    catch(e){}
}

// Obtains control.
//   - <controlName> is either a real control name or wildcard (as regular expression).
//   - <index> of control when such name exists more then 1 time. when -1 passed, functions returns list of all controls matched
//   - <tagName> Optional. Tagname of control.
//   - <masterControl> is a control to start search from. Default is document.
function GetControl( controlName, index, tagName, masterControl )
{
    try
    {
        var exactControl = document.getElementById(controlName);
        if( exactControl != null)
        {
            return exactControl;
        }
        else
        {
            var currIndex = 0;
            var re = new RegExp( controlName, "i" );
            
            var controls = null;
            if ( typeof(tagName) == 'undefined' )
            {
                if (document.all)
                    // IE
                    controls = document.all;
                else
                    // Firefox
                    controls = document.getElementsByTagName('*');
            }
            else
            {
                if (masterControl == undefined)
                    controls = document.getElementsByTagName( tagName );
                else
                    controls = masterControl.getElementsByTagName( tagName );
            }
            
            var resultArray = new Array();
            
            // try to locate a control
            for ( var i = 0; i < controls.length; i++ )
            {
                if ( re.test( controls[ i ].id ) )
                {
                    if ( currIndex < index )
                        currIndex++;
                    else
                        if (index != -1)
                            return controls[ i ];
                        else
                            ArrayPush(resultArray, controls[ i ]);
                }
            }
            if (index == -1)
                return resultArray;
        }
    }
    catch( e )
    {
    }
    return null;
}

function OnKeyDown()
{
    switch ( window.event.keyCode )
    {
        case 13: /* Enter */
            var c = GetDefaultSubmitControl();
            if ( c != null )
            {
                try
                {
                    c.focus();
                }
                catch( e )
                {
                    return false;
                }
                return true;
            }
            break;
    }
}

// Function is compatible with IE 5.0
function ArrayPush( b, item )
{
  b.length = b.length+1;
  b[ b.length-1 ] = item;
}

// Function is compatible with IE 5.0
function ArrayPop( b )
{
  if ( b != null && b.length > 0 )
  {
    var item = b[b.length-1];
    b.length = b.length-1;
    return item;
  }
  return null;
}

function DefEnq( control )
{
    DefEnq( control );
}

function DefEnq( control, index, tagName )
{
    ArrayPush( defaultSubmitControls, new Array( control, index, tagName ) );
}

function DefDeq()
{
    var contolPop = ArrayPop( defaultSubmitControls );
    return contolPop;
}

function GetDefaultSubmitControl()
{
    if ( defaultSubmitControls.length > 0 )
    {
        var controlData = defaultSubmitControls[ defaultSubmitControls.length-1 ];
        var control = GetControl( controlData[0], controlData[1], controlData[2] );
        return control;
    }
    else
        return null;
}

function GetDefaultSubmitControlName()
{
    if ( defaultSubmitControls.length > 0 )
    {
        var controlData = defaultSubmitControls[ defaultSubmitControls.length-1 ];
        return controlData[0];
    }
    else
        return null;
}

function SetNoDefaultSubmitControl()
{
    defaultSubmitControls = new Array();
}

function SetSingleDefaultSubmitControl( control )
{
    SetNoDefaultSubmitControl();
    DefEnq( control );
}

// Confirms deleting of an item
function ConfirmDelete( target, confirmationMessage )
{
    if (typeof(confirmationMessage) == "undefined")
        confirmationMessage = "Do you want to delete this item?";

    if ( confirm( confirmationMessage ) )
        return true;
        
    return false;
}
