﻿//<script>

/*
* Sets the focus on the first element that should "reasonably" receive it
*/
function Fev_FocusOnFirstFocusableFormElement() {
    for (i = 0; i < document.forms.length; i++) {
        if (Fev_FocusOnDefaultElement(document.forms[i])) return;
        //if (Fev_FocusOnFirstFocusableElement(document.forms[i])) return;
    }
}

/*
* Sets the focus on the first element that is able to receive it
*/
function Fev_FocusOnFirstFocusableElement(objForm) {
    if (objForm && (objForm != null)) {
        for (i = 0; i < objForm.length; i++) {
            var objElement = objForm.elements[i];
            if (Fev_IsFocusableElement(objElement)) {
                objElement.focus();
                return true;
            }
        }
    }
    return false;
}

/*
* Sets the focus on the first element that should "reasonably" receive it
*/
function Fev_FocusOnDefaultElement(objForm) {
    if (objForm && (objForm != null)) {
        for (i = 0; i < objForm.length; i++) {
            var objElement = objForm.elements[i];
            if (Fev_IsFocusableElement(objElement)) {
                var strType = Fev_GetElementType(objElement);
                //we know (strType != null) because it was checked within Fev_IsFocusableElement().
                if (strType.toLowerCase().indexOf("select") == 0) {
                    //NOTE: SELECT tags are ignored (they interfere with mousewheel scrolling when they have focus)
                }
                else {
                    // if object and all it's parents are visible...
                    if (Fev_IsElementVisible(objElement)) {
                        objElement.focus();
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

/*
* returns true if the element can receive focus
*/
function Fev_IsFocusableElement(objElement) {
    if (objElement &&
        (objElement != null) &&
        Fev_IsElementEnabled(objElement) &&
        Fev_IsElementVisible(objElement) &&
        Fev_IsElementEditable(objElement)) {
        var strType = Fev_GetElementType(objElement);
        if (strType != null) {
            if ((strType == "text") || (strType == "textarea") || (strType.toString().charAt(0) == "s")) {
                return true;
            }
        }
    }
    return false;
}

/*
* returns true if the element is enabled
*/
function Fev_IsElementEnabled(objElement) {
    if (objElement && (objElement != null)) {
        if (!(objElement.disabled == false)) {
            return false;
        }
        return true;
    }
    return false;
}

/*
* returns true if the element's content is editable by the user
*/
function Fev_IsElementEditable(objElement) {
    if (objElement && (objElement != null)) {
        if (objElement.readOnly) {
            return false;
        }
        if ((!objElement.isContentEditable) && (typeof (objElement.isContentEditable) != 'undefined')) {
            return false;
        }
        return true;
    }
    return false;
}

/*
* returns true if the element is visible to the user
*/
function Fev_IsElementVisible(objElement) {
    if (objElement && (objElement != null)) {
        if (objElement.style && (objElement.style != null)) {
            if (objElement.style.display && (objElement.style.display.toLowerCase() == 'none')) {
                return false;
            }
            if (objElement.style.visibility && (objElement.style.visibility.toLowerCase() == 'hidden')) {
                return false;
            }
            /*
            if (objElement.style.visibility && (objElement.style.visibility.toLowerCase() == 'inherit'))
            {
            var objParentElement = Fev_GetParentElement(objElement);
            if (objParentElement && (objParentElement != null) && (!Fev_IsElementVisible(objParentElement)))
            {
            return false;
            }
            }
            */
        }

        var objParentElement = Fev_GetParentElement(objElement);
        if (objParentElement && (objParentElement != null)) {
            return Fev_IsElementVisible(objParentElement);
        }
        else {
            return true;
        }
    }
    return false;
}

/*
* returns true if the element responds directly to Enter key presses
* return true for:
*     Textarea, Select/Dropdown, Input Buttons (Submit/Button/Image/Reset),
*     A tags
* return false for everything else, including:
*     Input type=[Radio/Checkbox/Text/Password/File]
*     IMG tags
*/
function Fev_IsElementUsesEnterKey(objElement) {
    if (objElement && (objElement != null)) {
        var strType = Fev_GetElementType(objElement);
        if (strType != null) strType = strType.toLowerCase();
        switch (strType) {
            case "textarea":
            case "select":
            case "submit":
            case "button":
            case "image":
            case "reset":
                return true;
                break;
            case "radio":
            case "checkbox":
            case "text":
            case "password":
            case "file":
                return false;
                break;
            default:
                break;
        }

        var strTagName = Fev_GetElementTagName(objElement);
        if (strTagName != null) strTagName = strTagName.toLowerCase();
        switch (strTagName) {
            case "textarea":
            case "select":
            case "a":
                return true;
                break;
            case "img":
            case "input":
            default:
                break;
        }
    }
    return false;
}

function Fev_GetParentElement(objElement) {
    if (objElement && (objElement != null)) {
        if (objElement.parentNode && (objElement.parentNode != null)) {
            return objElement.parentNode;
        }
        if (objElement.parentElement && (objElement.parentElement != null)) {
            return objElement.parentElement;
        }
    }
    return null;
}

function Fev_GetElementType(objElement) {
    if (objElement && (objElement != null)) {
        if (objElement.type) {
            return objElement.type;
        }
    }
    return null;
}

function Fev_GetElementTagName(objElement) {
    if (objElement && (objElement != null)) {
        if (objElement.tagName) {
            return objElement.tagName;
        }
    }
    return null;
}

function Fev_GetEventSourceElement(objEvent) {
    if (objEvent && (objEvent != null)) {
        // if IE...
        if (objEvent.srcElement) {
            return objEvent.srcElement;
        }
        // if Firefox...
        else if (objEvent.target) {
            return objEvent.target;
        }
    }
    return null;
}

function Fev_IsEnterKeyPressed(bIgnoreTextAreaEvents) {
    if (window.event) {
        var e = window.event;
        var bIsEnterKeyPress = ((e.keyCode == 13) && (e.type == 'keypress'));
        if (bIsEnterKeyPress) {
            if (bIgnoreTextAreaEvents && (bIgnoreTextAreaEvents == true)) {
                var strType = Fev_GetElementType(Fev_GetEventSourceElement(e));
                if (strType != null) strType = strType.toLowerCase();
                if (strType == "textarea") {
                    return false;
                }
            }
            return true;
        }
    }
    return false;
}

function Fev_IsFormSubmitKeyPress(event) {
    // for IE...
    if (window.event) {
        var e = window.event;
        var bIsEnterKeyPress = ((e.keyCode == 13) && (e.type == 'keypress'));
        if (bIsEnterKeyPress) {
            var eventSrc = Fev_GetEventSourceElement(e);
            if (!Fev_IsElementUsesEnterKey(eventSrc)) {
                return true;
            }
        }
    }
    // for Netscape/Firefox
    else if (event.which) {
        var bIsEnterKeyPress = (event.which == 13);
        if (bIsEnterKeyPress) {
            var eventSrc = Fev_GetEventSourceElement(event);
            if (!Fev_IsElementUsesEnterKey(eventSrc)) {
                return true;
            }
        }
    }

    return false;
}

/**************************************************************************************
*  Function    : getHRefName()                                                       *
*  Description : We need to get the name of button used in the <a href tag.  For     *
*                example, if the href tag = __doPostBack('Menu1$Button') then we     *
*        need to return Menu1$Button.                        *
*  Parameters  : anElement: anElement whose HRef value is retrieved and          *
*        and parsed.                                 *
**************************************************************************************/
function getHRefName(anElement) {
    var anHRef = anElement.href;

    var startpos = anHRef.indexOf("doPostBack('");
    if (startpos >= 0) {
        startpos = startpos + "doPostBack('".length;
    } else {
        if (navigator.appName == "Netscape") {
            startpos = anHRef.indexOf('DoPostBackWithOptions(new WebForm_PostBackOptions("');
            startpos = startpos + 'DoPostBackWithOptions(new WebForm_PostBackOptions("'.length;
        }
        else { // IE
            startpos = anHRef.indexOf('DoPostBackWithOptions(new%20WebForm_PostBackOptions("');
            startpos = startpos + 'DoPostBackWithOptions(new%20WebForm_PostBackOptions("'.length;
        }
    }

    var endpos = anHRef.indexOf("',");
    if (endpos < 0) endpos = anHRef.indexOf('",');


    anHRef = anHRef.substring(startpos, endpos);

    return anHRef;
}

/**************************************************************************************
*  Function    : clickLinkButtonText()                                               *
*  Description : onclick event handler for HTML table/row shell surrounding button   *
*                    text link.  Locates the anchor in the center table cell and     *
*                    invokes Fev_ClickButton (passing it the anchor's button id) to  *
*                    simulate a physical clicking of the button text link.           *
*  Parameters  : pButtonTableOrRowNode, html table/row shell receiving the onclick   *
*                    event, and which surrounds the button text link to be clicked   *
*                event, browser-generated onclick event object                       *
*  Assumptions : Only "button" and "menu item" HTML table/row shells will call this  *
*                    function.                                                       *
*  ISD Feature : "Button/Menu Item Image Edges Clickable"                            *
*  Authors     : Samson Wong                                                         *
**************************************************************************************/
function clickLinkButtonText(pButtonTableOrRowNode, event) {
    // make sure to cancel bubbling of clicks.
    if (!event) return;
    event.cancelBubble = true;
    if (event.stopPropagation) event.stopPropagation();

    // also check to make sure the target was not the inner area.   
    // target is used by Firefox, srcElement is used by IE
    if (event.target && event.target.toString().toLowerCase().indexOf("dopostback") > -1) return;
    if (event.srcElement && event.srcElement.toString().toLowerCase().indexOf("dopostback") > -1) return;

    var lAnchorNodeArray = pButtonTableOrRowNode.getElementsByTagName("a");

    // if "button", "horizontal menu item", "vertical menu item middle row" clicked...
    if ((lAnchorNodeArray != null) && (lAnchorNodeArray.length == 1)) {
        Fev_ClickButton(lAnchorNodeArray.item(0).id, event);
    }
    else // if "vertical menu item upper/lower row" clicked...
    {
        var lParentTableNode = pButtonTableOrRowNode.parentNode;
        var lChildrenNodeArray = lParentTableNode.getElementsByTagName("tr");
        // alert("clickLinkButtonText(lChildrenNodeArray.length=" + lChildrenNodeArray.length + ")");

        if (lChildrenNodeArray != null) {
            var lClickedRowFound = false;
            var lCurrentRowItemNumber = 1; // ignore vertical menu edge top row

            // locate the clicked row (this will either be one row above or below
            //     the row containing the vertical menu item to be clicked); terminate search
            //     before vertical menu edge bottom row
            while ((lClickedRowFound == false) && (lCurrentRowItemNumber < lChildrenNodeArray.length - 1)) {
                if (lChildrenNodeArray.item(lCurrentRowItemNumber) != pButtonTableOrRowNode) {
                    lCurrentRowItemNumber++;
                }
                else {
                    // alert("clickLinkButtonText(lCurrentRowItemNumber=" + lCurrentRowItemNumber + ")");
                    lClickedRowFound = true;
                }
            }

            if (lClickedRowFound == true) {
                // if row above first vertical menu item was clicked...
                if (lCurrentRowItemNumber == 1) {
                    // vertical menu item to be clicked must be below clicked row
                    lAnchorNodeArray = lChildrenNodeArray.item(lCurrentRowItemNumber + 1).getElementsByTagName("a");
                    // if row above vertical menu item was clicked...
                    if ((lAnchorNodeArray != null) && (lAnchorNodeArray.length == 1)) {
 
                        Fev_ClickButton(lAnchorNodeArray.item(0).id, event);
                    }
                }
                // if row below last vertical menu item was clicked...
                else if (lCurrentRowItemNumber == (lChildrenNodeArray.length - 2)) {
                    // vertical menu item to be clicked must be above clicked row
                    lAnchorNodeArray = lChildrenNodeArray.item(lCurrentRowItemNumber - 1).getElementsByTagName("a");
                    ((lAnchorNodeArray != null) && (lAnchorNodeArray.length == 1))
                    {
                        Fev_ClickButton(lAnchorNodeArray.item(0).id, event);
                    }
                }
                // if row of any other vertical menu item was clicked...
                else {
                    lAnchorNodeArray = lChildrenNodeArray.item(lCurrentRowItemNumber + 1).getElementsByTagName("a");
                    // if row above vertical menu item was clicked...
                    if ((lAnchorNodeArray != null) && (lAnchorNodeArray.length == 1)) {
                        Fev_ClickButton(lAnchorNodeArray.item(0).id, event);
                    }
                    // if row below vertical menu item was clicked...
                    else {
                        lAnchorNodeArray = lChildrenNodeArray.item(lCurrentRowItemNumber - 1).getElementsByTagName("a");
                        ((lAnchorNodeArray != null) && (lAnchorNodeArray.length == 1))
                        {
                            Fev_ClickButton(lAnchorNodeArray.item(0).id, event);
                        }
                    }
                }
            }
        }
    }
}


function Fev_ClickButton(buttonId, event) {
    // make sure to cancel bubbling of clicks.
    if (!event) return;
    event.cancelBubble = true;
    if (event.stopPropagation) event.stopPropagation();

    var buttonIdWithUnderscores = buttonId;

    var button = document.getElementById(buttonId);

    // If button is null, then try replacing $ with _ and look again.
    if (button == null) {
        while (buttonIdWithUnderscores.indexOf("$") != -1) {
            buttonIdWithUnderscores = buttonIdWithUnderscores.replace("$", "_");
        }

        button = document.getElementById(buttonIdWithUnderscores);
    }

    // Still nothing?  Try appending _Button
    if (button == null) {
        button = document.getElementById(buttonIdWithUnderscores + '_Button');
    }

    // Still nothing?  Try appending __Button
    if (button == null) {
        button = document.getElementById(buttonIdWithUnderscores + '__Button');
    }

    if (button) {
        // var anHRef = getHRefName(button);
        // if (anHRef)
        // {

        var nav = navigator.appName;
        if (nav.toUpperCase().indexOf(('Microsoft').toUpperCase()) >= 0) {
            button.click();
        }
        else {
            var anHRef;
            // retrieve the entire href, stripping out (if any) the preceding "javascript:" string
            if (button.href.toLowerCase().indexOf("javascript:") >= 0) {
                anHRef = button.href.substring("javascript:".length, button.href.length);
            }
            else {
                anHRef = button.href;
            }
            // convert all HTML-encoded quotes into true quotes
            anHRef = anHRef.replace("&quot;", '"');

            // call the javascript built-in function to execute the href string (in effect, this is analogous
            //     to IE's button.click(), but without having to do the complicated parsing of the href string
            //     to decide between regular "doPostBack"s and "doPostBackWithOptions"s)
            eval(anHRef);

            /*
            if (DoesButtonUsePostbackWithOptions(button)) {
                WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(buttonId, '', true, '', '', false, true));
            }
            else
            {
                __doPostBack(anHRef, '');
            }                
            */
        }

        return true;
        // }
    }
    return false;
}

// returns true if the href uses PostBackWithOptions.
function DoesButtonUsePostbackWithOptions(anElement) {
    var anHRef = anElement.href;
    var startpos = anHRef.indexOf('PostBackWithOptions');
    if (startpos >= 0) {
        return true;
    }
    return false;
}