﻿// ############################################################################
// Collection of JavaScript functions that are required throughout ASI 
// software.  These functions are different from those found in ASIUtility.js 
// in that they are only useful if used inside Asi.Webroot.
// ############################################################################

//
// A browser-independent function for determining if the user has the CTRL key
// pressed.
//
function IsCtrl(e)
{
    if (window.event)
        return window.event.ctrlKey;
    else
        return (e.modifiers & Event.CONTROL_MASK == Event.CONTROL_MASK);
}
//
// A browser-independent function for determining if the user has the SHIFT key
// pressed.
//
function IsShift(e)
{
    if (window.event)
        return window.event.shiftKey;
    else
        return (e.modifiers & Event.SHIFT_MASK == Event.SHIFT_MASK);
}
//
// Use this function when you want to pass back to the server the state
// of the user's CTRL key.  On the server-side Method you can access whether
// the CTRL key was pressed using the DisplayPageBase.IsCtrl property.
// If this function name changes you must change DisplayPageBase.CLIENTSCRIPT_SetIsCtrl
//
function SetIsCtrl(e)
{ 
    if ($get('__CTRLKEY') != null)
        $get('__CTRLKEY').value = IsCtrl(e); 
        
    // TODO: should i create the hidden input element if it's not already on the Form?
}
//
// Use this function when you want to pass back to the server the state
// of the user's SHIFT key.  On the server-side Method you can access whether
// the SHIFT key was pressed using the DisplayPageBase.IsShift property.
// If this function name changes you must change DisplayPageBase.CLIENTSCRIPT_SetIsShift
//
function SetIsShift(e)
{ 
    if ($get('__SHIFTKEY') != null)
        $get('__SHIFTKEY').value = IsShift(e); 
        
    // TODO: should i create the hidden input element if it's not already on the Form?
}
//
// Perform the specified event on the specifed control.  Also sets the
// requested argument into the __EVENTARGUMENT hidden input element.
// If eventName is not specified 'click' will be used by default.
//
function InitiateControlEventWithArgument(controlClientID, argument, eventName)
{
    if (eventName == null)
        eventName = 'click';

    if (argument != null && argument != '') 
        $get('__EVENTARGUMENT').value = argument;
        
    var ajaxifiedControl = $get(controlClientID);
    eval('ajaxifiedControl.' + eventName + '()');
    //ajaxifiedControl.fireEvent('on' + eventName);
}
//
//
//
function InitiatePostBack(controlUniqueId, argument)
{
    __doPostBack(controlUniqueId, argument);
}
//
// To support the bug with ajax tabcontainer not persisting current
// active tab across postbacks.
//
var ignoreTabChange = false;
// TODO: Would like to do the same thing on save, in case the validation failure is in another tab.
function TabContainer_OnActiveTabChanged(sender, args)
{
    sender.get_clientStateField().value = sender.saveClientState();
    if (typeof (Page_ClientValidate) == 'function')
    {
        if (!ignoreTabChange && !Page_ClientValidate())
        {
            ShowFailedTab(sender);
        }
    }
}
function ShowProblemTab()
{
    if (typeof (Page_ClientValidate) == 'function' && !Page_ClientValidate())
    {
        // Find all ajax tabviews on the page.
        var tabContainers = getElementsByClassName('ajax__tab_xp');
        for (var i = 0; i < tabContainers.length; ++i)
        {
            ShowFailedTab($find(tabContainers[i].id));
        }
    }
}
function ShowFailedTab(tabContainer)
{
    // Check to see if there is a validator on the current tab that failed. If so, disallow the tab change.
    for (i = 0; i < Page_Validators.length; ++i)
    {
        var val = Page_Validators[i];
        if (val.evaluationfunction(val) == false)
        {
            // Find the tab containing the invalid control.
            var tabs = tabContainer.get_tabs();

            var control = val.parentNode;
            while (control != null)
            {
                for (var i = 0; i < tabs.length; ++i)
                {
                    if (control == tabs[i]._element)
                    {
                        ignoreTabChange = true;
                        tabContainer.set_activeTab(tabs[i]);
                        ignoreTabChange = false;
                        return;
                    }
                }
                control = control.parentNode;
            }
        }
    }
}
function getElementsByClassName(classname, node)
{
    if (!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for (var i = 0, j = els.length; i < j; i++)
        if (re.test(els[i].className)) a.push(els[i]);
    return a;
}
