﻿// Functions based on ASP.NET's WebForm_AutoFocus scripts.
// Set focus to the first available text field
// Only difference from ASP.NET 2.0 default script functionality is that instead of calling
// TK_IsFocusableTag, a new function named TK_IsFocusableObject is called. This function allows focus only
// to text fields.

function TK_FindFirstFocusableChild(control) {
    if (!control || !(control.tagName)) {
        return null;
    }
    var tagName = control.tagName.toLowerCase();
    if (tagName == "undefined") {
        return null;
    }
    var children = control.childNodes;
    if (children) {
        for (var i = 0; i < children.length; i++) {
            try {
                if (TK_CanFocus(children[i])) {
                    return children[i];
                }
                else {
                    var focused = TK_FindFirstFocusableChild(children[i]);
                    if (TK_CanFocus(focused)) {
                        return focused;
                    }
                }
            } catch (e) {
            }
        }
    }
    return null;
}

function TK_AutoFocus(focusId) {
    var targetControl;
    if (__nonMSDOMBrowser) {
        targetControl = document.getElementById(focusId);
    }
    else {
        targetControl = document.all[focusId];
    }
    var focused = targetControl;
    if (targetControl && (!TK_CanFocus(targetControl))) {
        focused = TK_FindFirstFocusableChild(targetControl);
    }
    if (focused) {
        try {
            focused.focus();
            if (__nonMSDOMBrowser) {
                focused.scrollIntoView(false);
            }
            if (window.__smartNav) {
                window.__smartNav.ae = focused.id;
            }
        }
        catch (e) {
        }
    }
}

function TK_CanFocus(element) {
    if (!element || !(element.tagName)) return false;
    var tagName = element.tagName.toLowerCase();
    return (!(element.disabled) &&
            (!(element.type) || element.type.toLowerCase() != "hidden") &&
            TK_IsFocusableObject(element) &&
            TK_IsInVisibleContainer(element)
            );
}

function TK_IsFocusableObject(element) {
    var tagName = element.tagName.toLowerCase();
    return ((tagName == "input" && element.type == "text") ||
            tagName == "textarea" ||
            tagName == "select" ||
            tagName == "button");
}

function TK_IsInVisibleContainer(ctrl) {
    var current = ctrl;
    while ((typeof (current) != "undefined") && (current != null)) {
        if (current.disabled ||
            (typeof (current.style) != "undefined" &&
            ((typeof (current.style.display) != "undefined" &&
                current.style.display == "none") ||
                (typeof (current.style.visibility) != "undefined" &&
                current.style.visibility == "hidden")))) {
            return false;
        }
        if (typeof (current.parentNode) != "undefined" &&
                current.parentNode != null &&
                current.parentNode != current &&
                current.parentNode.tagName.toLowerCase() != "body") {
            current = current.parentNode;
        }
        else {
            return true;
        }
    }
    return true;
}

<!--
// Email obfuscator script 2.1 by Tim Williams, University of Arizona
// Random encryption key feature by Andrew Moulden, Site Engineering Ltd
// This code is freeware provided these four comment lines remain intact
// A wizard to generate this code is at http://www.jottings.com/obfuscator/
{

    function decodeText(encoded, keyE) {
        shift = encoded.length;
        link = "";
        for (i = 0; i < encoded.length; i++) {
            if (keyE.indexOf(encoded.charAt(i)) == -1) {
                ltr = encoded.charAt(i);
                link += (ltr);
            }
            else {
                ltr = (keyE.indexOf(encoded.charAt(i)) - shift + keyE.length) % keyE.length;
                link += (keyE.charAt(ltr));
            }
        }
        return link;
    }   

    function outputText(encoded, keyE) {
        shift = encoded.length;
        link = "";
        for (i = 0; i < encoded.length; i++) {
            if (keyE.indexOf(encoded.charAt(i)) == -1) {
                ltr = encoded.charAt(i);
                link = (ltr) + link;
            }
            else {
                ltr = (keyE.indexOf(encoded.charAt(i)) - shift + keyE.length) % keyE.length;
                link = (keyE.charAt(ltr)) + link;
            }
        }
        document.write("<a href='mailto:" + link + "'>" + link + "</a>");
    }

    function output(encoded, keyE, img) {
        shift = encoded.length;
        link = "";
        for (i = 0; i < encoded.length; i++) {
            if (keyE.indexOf(encoded.charAt(i)) == -1) {
                ltr = encoded.charAt(i);
                link += (ltr);
            }
            else {
                ltr = (keyE.indexOf(encoded.charAt(i)) - shift + keyE.length) % keyE.length;
                link += (keyE.charAt(ltr));
            }
        }
        document.write("<a href='mailto:" + link + "'>" + img + "</a>");
    }
}
//-->
