(function() {

// Determine which window is active.
window.SC_isActive = false;
var handleFocusEvent = function(e) {
    window.SC_isActive = true;
};
var handleBlurEvent = function(e) {
    window.SC_isActive = false;
};
window.addEventListener("focus", handleFocusEvent)
window.addEventListener("blur", handleBlurEvent)

// Handle keyboard events.
var handleKeyboardEvent = function(e) {
    var inputIsActive = false;
    var activeElement = document.activeElement;
    if (activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA")) {
        inputIsActive = true;
    }
    
    if (e.keyCode === 191 && !inputIsActive) {        // '/'
        top.chatpane.focus();
        top.chatpane.chatform.graf.focus();
        top.chatpane.chatform.graf.value = "/";
    }
    else if (e.keyCode === 190 && !inputIsActive) {   // '.'
        top.chatpane.focus();
        top.chatpane.chatform.graf.focus();
    }
    else if (e.keyCode === 68 && e.ctrlKey) {         // 'd'
        top.mainpane.focus();
    }
};

window.addEventListener("keydown", handleKeyboardEvent)

})();

(function() {

// Determine which window is active.
window.SC_isActive = false;
var handleFocusEvent = function(e) {
    window.SC_isActive = true;
};
var handleBlurEvent = function(e) {
    window.SC_isActive = false;
};
window.addEventListener("focus", handleFocusEvent)
window.addEventListener("blur", handleBlurEvent)

// Handle keyboard events.
var handleKeyboardEvent = function(e) {
    var inputIsActive = false;
    var activeElement = document.activeElement;
    if (activeElement && (activeElement.tagName === "INPUT" || activeElement.tagName === "TEXTAREA")) {
        inputIsActive = true;
    }
    
    var handledEvent = false;
    if (e.keyCode === 191 && !inputIsActive) {        // '/'
        top.chatpane.focus();
        top.chatpane.chatform.graf.focus();
        top.chatpane.chatform.graf.value = "/";
        handledEvent = true;
    }
    else if (e.keyCode === 190 && !inputIsActive) {   // '.'
        top.chatpane.focus();
        top.chatpane.chatform.graf.focus();
        handledEvent = true;
    }
    else if (e.keyCode === 68 && e.ctrlKey) {         // '^d'
        top.mainpane.focus();
        handledEvent = true;
    }
    
    if (handledEvent) {
        e.preventDefault();
        e.stopPropagation();
    }
};

window.addEventListener("keydown", handleKeyboardEvent)

})();

Fix an issue with chat field focus commands.
Fix an issue with chat field focus commands.

Occasionally, hitting '/' when the chat field was not focused would
cause two slashes to appear in the chat field. Similarly, hitting '.'
when the field was not focused could cause a period to appear at the end
of the chat field.

To fix this issue we prevent the event from performing its default
action. In addition, we stop propagation of the event (just to be safe).
