/**
 * Copyright (C)SERENA Software,Inc. 1998-2005. All Rights Reserved.
 * 
 * This computer software is Licensed Material belonging to Serena Software.
 * It is considered a trade secret and not to be used or divulged by parties
 * who have not received written authorization from Serena Software.
 */

/**
 * JavaScript Keyboard utility functions
 **/
var _keyUtil = "";

_KeyboardUtil.prototype.constructor = _KeyboardUtil;

_KeyboardUtil.prototype.KEY_ENTER = "13";
_KeyboardUtil.prototype.KEY_ESC = "27";

_KeyboardUtil.prototype.ACTION_CLOSE_WINDOW = "CLOSE_WINDOW";
_KeyboardUtil.prototype.ACTION_REFRESH_WINDOW = "REFRESH_WINDOW";

_KeyboardUtil.prototype.handlers = new Array();

_KeyboardUtil.prototype.isIE = navigator.appName == "Microsoft Internet Explorer";


_KeyboardUtil.prototype.init = function() {
    _keyUtil = this;
}

function _KeyboardUtil() {
    this.init();
}

/**
 * Internal key-press handler
 */
_KeyboardUtil.prototype.keyIsDown = function( firedEvent) {
    // Firefox passes in the event
    if ( firedEvent) {
        event = firedEvent;
    }

    var action = this.handlers[event.keyCode.toString()];

//  alert( "event: " + event + "\nkeyCode: " + event.keyCode + "\naction: " + action);

    // edge-cases
    if ( !action) {
        // no registered event on key
        return;
    }

    // handle the event
    try {
        if ( action == _keyUtil.ACTION_CLOSE_WINDOW) {
            window.close();
        }
        else if ( action == _keyUtil.ACTION_REFRESH_WINDOW) {
            document.location.href = document.location.href;
        }
        else {
            // call function
            action.call();
        }
        event.returnValue = false;
        
    }
    catch ( error) {
        throw new Error( "KeyboardUtil - There was an error executing keyboard event (keycode: " +
                         event.keyCode +
                         " | action: " +
                         action +
                         " | message: " +
                         error.message +
                         ")");
    }
}

/**
 * Registers a close of the window on ESC-key press
 */
_KeyboardUtil.prototype.closeWindowOnKeyEsc = function() {
    this.handlers[this.KEY_ESC] = this.ACTION_CLOSE_WINDOW;
}

/**
 * Registers a window refresh on ESC-key press
 */
_KeyboardUtil.prototype.refreshWindowOnKeyEsc = function() {
    this.handlers[this.KEY_ESC] = this.ACTION_REFRESH_WINDOW;
}

/**
 * Registers a close of the window on ENTER-key press
 */
_KeyboardUtil.prototype.closeWindowOnKeyEnter = function() {
    this.handlers[this.KEY_ENTER] = this.ACTION_CLOSE_WINDOW;
}

/**
 * Registers a user defined function to execute on a specific key
 */
_KeyboardUtil.prototype.registerKeyFunction = function( key, functionToCall) {
    this.handlers[key] = functionToCall;
}

document.onkeydown  = function( event) {
    _keyUtil.keyIsDown( event);
}

var KeyboardUtil = new _KeyboardUtil();
