﻿
// dropdown hooks
function selectedValue(sel) {
    if (!sel) return null;
    if (!sel.options) return null;

    var l = sel.options.length;

    for (var i = 0; i < l; i++) {
        if (sel.options[i].selected)
            return sel.options[i].value;
    }
}

// globals
function hookHandlerByID(elementToHook, eventName, eventHandler) {
    var eth = document.getElementById(elementToHook);
    if (!eth) return false;

    return hookHandler(eth, eventName, eventHandler, direct);
}
function hookHandler(elementToHook, eventName, eventHandler) {
    if (!elementToHook) return false;
    var onEventName = 'on' + eventName;
    if (eventName == 'submit') {
        elementToHook[onEventName] = eventHandler;
        return true;
    }

    try {
        if (elementToHook.addEventListener) // others
        {
            elementToHook.addEventListener(eventName, function (event) { return eventHandler(event); }, false);
        }
        else if (elementToHook.attachEvent) // IE8-
        {
            elementToHook.attachEvent(onEventName, function (event) { return eventHandler(event); });
        }
        else {
            elementToHook[onEventName] = eventHandler;
        }
        return true;
    }
    catch (ex) { }

    return false;
}

// text counters
function hookCounter(controlID, counterID, maxtext) {
    try {
        var ctl = document.getElementById(controlID);
        var params = {
                proofID: controlID,
                resultID: counterID,
                max: maxtext
            }
        var cb = Function.createCallback(checkCounter, params);

        hookHandler(ctl, 'keyup', cb);
        hookHandler(ctl, 'keydown', cb);

        checkCounter(ctl, params);
    }
    catch (ex) { }
}

function checkCounter(e, p) {
    if (!p || !p.proofID || !p.resultID) return;

    try {
        var msg = document.getElementById(p.proofID);
        var res = document.getElementById(p.resultID);

        if (p.max > 0) {
            if (msg.value.length > p.max) {
                msg.value = msg.value.substring(0, p.max);
            }

            res.value = p.max - msg.value.length;
        }
        else {
            res.value = msg.value.length;
        }
    }
    catch (ex) { }
}

// email scrambling
function writeEmail(sth1, sth2, sth3, tabindex) {
    document.write(unescape("%3Ca href='mail"));
    document.write("to:" + sth3);
    document.write(sth2 + sth1);
    if (tabindex) {
        document.write("' tabindex='" + tabindex);
    }
    document.write(unescape("'%3E") + sth3);
    document.write(sth2 + sth1 + unescape("%3C/a%3E"));
}

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
