﻿function controls_getKey(e) {
    if (!e) e = window.event;
    var key = e.which;
    if (!key) key = e.keyCode;
    return key;
}
function controls_isEnterKey(e) {
    return controls_getKey(e) == 13;
}
function controls_enterCancel(event) {
    return !controls_isEnterKey(event);
}
function controls_enterKey(e, id) {
    if (document.getElementById(id)) {
        if (controls_isEnterKey(e)) {
            var object = document.getElementById(id);
            if (object.tagName == 'A') {
                var href = object.href.replace(window.location, "");
                if (href.length > 1) {
                    if (!object.onclick || object.onclick()) {
                        try {
                            eval(unescape(object.href.replace(/^javascript:/i, "")));
                        }
                        catch (err) {
                            object.click();
                        }
                    }
                }
                else {
                    object.onclick();
                }
                return false;
            }
            else if (object.tagName == 'INPUT' || object.tagName == 'BUTTON') {
                object.click();
                return false;
            }
        }
    }
    return true;
}
function controls_captchacon_init(cid, helperId, helperId2) {
    controls_captcha_insertById(cid, helperId, helperId2);
}
function controls_captcha_insertById(containerId, helperId, helperId2) {
    var container = $('#' + containerId);
    var helper = $('#' + helperId);

    var helper2;
    if (helperId2) {
        helper2 = $('#' + helperId2);
    }

    controls_captcha_insert(container, helper, helper2);
}
function controls_captcha_insert(container, helper, helper2) {
    var ctrl = container;
    var cid = ctrl.id || 'captcha';
    ctrl.attr('id', cid);
    
    // Locate the captcha token.
    var captcha = $(ctrl).find('*:contains("[CAPTCHA]"):last');

    // Create captcha ui and attach validation.
    if (captcha) {
        //var val = $('#' + helperId).val();
        var val = $(helper).val();

        $(captcha).html('<span class="captchacon"><img alt="Loading..." src="/captcha.aspx?captchaText='
            + val
            + '"/><p>Please enter the security code as shown in the image.<br/><input type="text" id="' + cid + '_input" value="" onkeypress="return controls_enterCancel(event);"/></p><p class="error" style="display:none;">Please enter the correct security code.</p></span>');

        // Locate the submit button that will trigger captcha validation.
        var submit = $(ctrl).find('input[type="submit"]');

        // terrible hack goes here.
        var onclick = submit.attr('onclick');
        if (onclick) {
            onclick = onclick.replace('this', '$("#" + "' + cid + '").find("input[type=\'submit\']").get(0)');
            onclick = onclick.replace('return ', ';');
        }

        // Remove click listeners.
        $(submit).removeAttr('onclick');

        // Attach validation click event handler to submit button.
        $(submit).click(function (e) {
            var valid = $('#' + cid + '_input').val() == val;

            $(ctrl).find('.error').css('display', (valid) ? 'none' : 'inline');

            if (valid) {
                $(helper2).attr('value', val);
                if (onclick && onclick != '') {
                    eval(onclick);
                }
            }
            else if (e) {
                e.preventDefault();
            }

            return valid;
        });
    }
}

function controls_forum_init(cid, helperId, helperId2) {
    // Hack to get forum to show up correctly.  D:
    $('#' + cid).css('width', 'auto');
    var forum = $('#' + cid + '_content_html_wrapper');
    var txt = $(forum).find('textarea');
    $(txt).css({ 'width': '99%' });
    $(forum).find('td').css({ 'padding': '0px' });
    $(forum).css({ 'visibility' : 'visible', 'height' : 'auto' });

    // Add captcha functionality if specified.
    if (txt && helperId && helperId != '') {
        // Add the captcha token.
        $(txt).closest('tr').parent().append('<tr><td>[CAPTCHA]</td></tr>');
        // Locate the captcha container.
        var container = $(txt).closest(':has(input[type="submit"])');
        // Run the captcha container init script.
        controls_captcha_insert($(container), $('#' + helperId), (helperId2) ? $('#' + helperId2) : null);
    }
}

function controls_modal_init(cid) {
    var $modal = $("#" + cid);

    // Attach "close" functionality to any assigned buttons.
    $('a.closeModal[href="#' + cid + '"]').click(function (e) {
        controls_modal_close($modal, e);
    });

    // Attach "open" functionality to any buttons.
    $('a.openModal[href="#' + cid + '"]').click(function (e) {
        e.preventDefault();
        controls_modal_open($modal);
    });
}
function controls_modal_openById(id) {
    var modal = $('#' + id);
    if (modal.length > 0) {
        controls_modal_open(modal);
    }
}
function controls_modal_open(modal) {
    //$('#overlay').show();
    $('#overlay').css({ display: 'block', opacity: .50 });
    $('div.modal').each(function () {
        if ($(this) != modal && $(this).css('display') == 'block') {
            $(this).css('z-index', '49');
        }
    });
    var height = modal.show().height();
    var winHeight = $(window).height();
    var scrollTop = $(window).scrollTop();
    var top = (winHeight - height) / 3;
    if (top < 0) { top = 0; }
    top = top + scrollTop;
    modal.css({ 'top': top, 'z-index': 51 });
}
function controls_modal_closeById(id) {
    var modal = $('#' + id);
    if (modal.length > 0) {
        controls_modal_close(modal);
    }
}
function controls_modal_close(modal, e) {
    if (e) {
        e.preventDefault();
    }
    modal.fadeOut(function () {
        var areModalsOpen = false;
        $('div.modal').each(function () {
            if ($(this).css('display') == 'block') {
                $(this).css('z-index', '100');
                areModalsOpen = true;
            }
        });
        if (!areModalsOpen) {
            $('#overlay').fadeOut();
        }
    });
}
