var g_form, g_searchInput, g_activeInput;

document.documentElement.id = "js";

function addEvent(obj, type, func) {
    if (obj.addEventListener) {
        obj.addEventListener(type, func, 0);
    } else if (obj.attachEvent) {
        obj.attachEvent("on" + type, func);
    }
}

function commonKeydown(e) {
    if (!e) {
        e = window.event;
    }
    var key = e.keyCode;

    if (e.ctrlKey) {
        if (!g_activeInput) {
            if ((key == 13) && (document.forms.length == 1)) {
                g_form.submit();
            } else if (key == 38) {
                g_searchInput.focus();
            } else if (key == 37) {
                var p = document.getElementById('previous_page');
                if (p) {
                    location.href = p.href;
                }
            } else if (key == 39) {
                var n = document.getElementById('next_page');
                if (n) {
                    location.href = n.href;
                }
            }
        } else if ((key == 38) && (document.forms[0] == g_activeInput.form) && g_activeInput.select) {
            g_activeInput.select();
        }
    }
    else if ((key == 27) && (typeof(keydownEsc) == 'function')) {
        keydownEsc(e);
    }
}

function commonInit(focus) {
    g_form = document.forms[0];

    addEvent(document, "keydown", commonKeydown);
    if (g_form) {
        if (typeof(initTabs) == 'function') {
            initTabs(g_form);
        }

        for (var i = 0, l = document.forms.length; i < l; i++) {
            var f = document.forms[i];
            for (var j = 0, lj = f.length; j < lj; j++) {
                var inp = f.elements[j];

                if ((inp.tagName.toLowerCase() == 'textarea') || (inp.type && inp.type == 'text')) {
                    if (!g_searchInput) {
                        g_searchInput = inp;
                    }

                    addEvent(inp, "focus", function() {g_activeInput = this});
                    addEvent(inp, "blur",  function() {g_activeInput = null});
                }
            }
        }

        if (focus && g_searchInput) {
            g_searchInput.focus();
        }
    }

    if (typeof(addEmptyTitleAndBorderToAllImages) == 'function') {
        addEmptyTitleAndBorderToAllImages(document);
    }

    if (typeof(applyDecoration) == 'function') {
        applyDecoration(document);
    }
}