/*
** Help routines and constants that are required by the AWPX framework
*/

//---------------------------------------------------------------
// Globals
//---------------------------------------------------------------

var awpxHelp_activeHelp;
var awpxHelp_changedOnClick = new Array();
var awpxHelp_lastActiveHelp;


/*
    Add the class 'helpAreaActive' to all elements that had the class 'helpArea'.
    This can be used in a modal help system to highlight the active help areas;
    after which any click in the active help areas will display the corresponding
    help in the help iframe.
    This function acts as a toggle active/inactive.

    Params (both optional):
        helpId: the ID of the page to display in the awpxHelp_helpFrame when activated
        helpText: the text to display in the awpxHelp_helpText when activated

*/
function awpxHelp_activateHelpArea(helpId , helpText)
{
    awpxHelp_activeHelp = ! awpxHelp_activeHelp;
    var eltList = awpxGetElementsByClass(document, "helpArea" , false)
    var elt;
    for(var i=0; i < eltList.length; i++)
    {
        if (awpxHelp_activeHelp)
        {
            awpxAddClassName(eltList[i], "helpAreaActive");
            if (! awpxHelp_changedOnClick[eltList[i]])
                awpxHelp_changedOnClick[eltList[i]] = eltList[i].onclick;
            eltList[i].onclick = null;
            eltList[i].onclick = awpxHelp_clickHelpAreaActive;
        } else {
            awpxRemoveClassName(eltList[i], "helpAreaActive");
            eltList[i].onclick = null;
            eltList[i].onclick = awpxHelp_changedOnClick[eltList[i]];
            awpxHelp_lastActiveHelp = null;
        }
    }
    if (awpxHelp_activeHelp)
    {
        awpxHelp_displayHelpText(helpText);
        awpxHelp_displayHelpUrl(helpId);
        awpxHelp_showMessageArea(true);
    }
}

/*
    onClick handler that displays the help.
    If an active helpArea is clicked again, the help mode is exited.
*/
function awpxHelp_clickHelpAreaActive()
{
    if (awpxHelp_lastActiveHelp == this)
    {
        awpxHelp_activateHelpArea();
        return false;
    }
    awpxHelp_lastActiveHelp = this;
    var helpId = awpxHelp_findHelpId(this);
    awpxHelp_displayHelpUrl(helpId);
    return false;
}

/*
    Find the helpId to display. The helpId is indicated by a class helpId_XXXX.
    If not found on this element, we go up to the parents until we find one.
*/
function awpxHelp_findHelpId(elt)
{
    if (! elt)
        return '';

    if (elt.className)
    {
        var classNameList = elt.className.split(' ');
        for(var i = 0 ; i < classNameList.length ; i++)
        {
            var clsName = classNameList[i].trim();
            if(!clsName)
                continue;
            if(clsName.indexOf("helpId_") == 0)
            {
                helpId = clsName.substring(7);
                return helpId;
            }
        }
    }
    // none was found, get the parent's helpId
    return awpxHelp_findHelpId(elt.parentNode);

}

/*
    Display the help text in the awpxHelp_displayHelp element.
    If either helpText is NULL or there is no element by the name
    "awpxHelp_helpText", do not display anything.
*/
function awpxHelp_displayHelpText(helpText , assumeMessageAreaAlreadyVisible)
{
    if (helpText && document.getElementById("awpxHelp_helpText"))
    {
        if (! assumeMessageAreaAlreadyVisible)
            awpxHelp_showMessageArea(true);
        document.getElementById("awpxHelp_helpText").innerText = helpText;
    }
    return false;
}


/*
    Display a help page in the help iframe (awpxHelp_displayUrl)
    If either the helpId is NULL or there is no iFrame by the name
    "awpxHelp_helpFrame", do not display anything.
*/
function awpxHelp_displayHelpUrl(helpId)
{
    if (helpId && document.getElementById("awpxHelp_helpFrame"))
    {
        awpxHelp_showMessageArea(true);
        var baseUrl = document.getElementById("awpxHelp_helpFrameUrl").href;
        document.getElementById("awpxHelp_helpFrame").src = baseUrl + "?id=" + helpId;
        var search = window.location.search;
        if (search.indexOf('HelpAuthor=true') != -1)
        {
            awpxHelp_displayHelpText("Showing help file for ID = " + helpId , true);
        }

    }
    return false;
}


/*
    Hide the message area by calling awpxHelp_showMessageArea(false) and
    exit the help modal mode.
*/
function awpxHelp_dismissHelp()
{
    if (awpxHelp_activeHelp)
    {
        awpxHelp_activateHelpArea();
    } else {
    }
    var baseUrl = document.getElementById("awpxHelp_helpFrameUrl").href;
    document.getElementById("awpxHelp_helpFrame").src = baseUrl + "?close=1";
    awpxHelp_showMessageArea(false);
    return false;
}

function awpxHelp_isMessageAreaShown()
{
    return (document.getElementById("awpxHelp_visibleArea").style.display != "none");
}

/*
    Make the help area (awpxHelp_visibleArea) visible
*/
function awpxHelp_showMessageArea(setTo)
{
    if (setTo)
    {
        document.getElementById("awpxHelp_visibleArea").style.display = "block";
    } else {
        document.getElementById("awpxHelp_visibleArea").style.display = "none";
    }
    if (window.awpxHelp_showMessageAreaCallBack)
        awpxHelp_showMessageAreaCallBack(setTo);
}

/*
    Process the key clicks and invoke the help if necessary
*/
function awpxHelp_captureKeyEvent(event)
{
    var evt = awpxGetEvent(event);
    if (evt && (evt.ctrlKey || evt.altKey))
    {
        var sourceElement = awpxGetEventSource(evt);
        if (! sourceElement) {
           return false;
        }

        var helpId = awpxHelp_findHelpId(sourceElement);
        awpxHelp_displayHelpUrl(helpId);

        evt.returnValue = false;
        awpxCancelEvent(evt);
        return false;
    }
    return true;
}
