﻿
//*************************************
//          ODAKeyCode
//*************************************

function ODAKeyCodeVersion()
{
    return "1.0";
}

var ODAKeyCode = Class.create();
ODAKeyCode.prototype = {
    initialize: function(event) {
        this.keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
        this._event = event;
        this._shiftKey = this._event.shiftKey;
        this._controlKey = this._event.ctrlKey;
        this._altKey = this._event.altKey;

        //if(Browser.browser.toLowerCase() == "explorer"){
        this.createStaticPropsIE();
        this.getCharFromKeyCodeIE();
        //        }
        //        else if(Browser.browser.toLowerCase() == "firefox"){
        //            this.createStaticPropsFF();
        //            this.getCharFromKeyCodeFF();
        //        }
    },

    createStaticPropsIE: function() {
        this.BACKSPACE = 8;
        this.TAB = 9;
        this.ENTER = 13;
        this.SHIFT = 16;
        this.CTRL = 17;
        this.ALT = 18;
        this.BREAK = 19;
        this.CAPS_LOCK = 20;
        this.ESCAPE = 27;
        this.SPACE = 32;
        this.PAGE_UP = 33;
        this.PAGE_DOWN = 34;
        this.END = 35;
        this.HOME = 36;
        this.LEFT_ARROW = 37;
        this.UP_ARROW = 38;
        this.RIGHT_ARROW = 39;
        this.DOWN_ARROW = 40;
        this.INSERT = 45;
        this.DELETE = 46;
        this.LEFT_WINDOW = 91;
        this.RIGHT_WINDOW = 92;
        this.SELECT = 93;
        this.F1 = 112;
        this.F2 = 113;
        this.F3 = 114;
        this.F4 = 115;
        this.F5 = 116;
        this.F6 = 117;
        this.F7 = 118;
        this.F8 = 119;
        this.F9 = 120;
        this.F10 = 121;
        this.F11 = 122;
        this.F12 = 123;
        this.NUM_LOCK = 144;
        this.SCROLL_LOCK = 145;
    },

    createStaticPropsFF: function() {
        this.BACKSPACE = 8;
        this.TAB = 9;
        this.ENTER = 13;
        this.SHIFT = 16;
        this.CTRL = 17;
        this.ALT = 18;
        this.BREAK = 19;
        this.CAPS_LOCK = 20;
        this.ESCAPE = 27;
        this.SPACE = 32;
        this.PAGE_UP = 33;
        this.PAGE_DOWN = 34;
        this.END = 35;
        this.HOME = 36;
        this.LEFT_ARROW = 37;
        this.UP_ARROW = 38;
        this.RIGHT_ARROW = 39;
        this.DOWN_ARROW = 40;
        this.INSERT = 45;
        this.DELETE = 46;
        this.LEFT_WINDOW = 91;
        this.RIGHT_WINDOW = 92;
        this.SELECT = 93;
        this.F1 = 112;
        this.F2 = 113;
        this.F3 = 114;
        this.F4 = 115;
        this.F5 = 116;
        this.F6 = 117;
        this.F7 = 118;
        this.F8 = 119;
        this.F9 = 120;
        this.F10 = 121;
        this.F11 = 122;
        this.F12 = 123;
    },

    getCharFromKeyCodeIE: function() {
        this.IsSpecialChar = false;
        this.IsNumber = false;
        this.IsAlpha = false;
        this.IsSpecialKey = false;

        switch (this.keyCode) {
            case 8:
                // backspace 8 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.BACKSPACE;
                break;
            case 9:
                // tab 9 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.TAB;
                break;
            case 13:
                // enter 13 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.ENTER;
                break;
            case 16:
                // shift 16 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.SHIFT;
                break;
            case 17:
                // ctrl 17 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.CTRL;
                break;
            case 18:
                // alt 18 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.ALT;
                break;
            case 19:
                // pause/break 19 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.BREAK;
                break;
            case 20:
                // caps lock 20 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.CAPS_LOCK;
                break;
            case 27:
                // escape 27 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.ESCAPE;
                break;
            case 32:
                // space 32
                this.IsSpecialChar = true;
                this.keyCodeValue = " ";
                break;
            case 33:
                // page up 33 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.PAGE_UP;
                break;
            case 34:
                // page down 34 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.PAGE_DOWN;
                break;
            case 35:
                // end 35 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.END;
                break;
            case 36:
                // home 36 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.HOME;
                break;
            case 37:
                // left arrow 37 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.LEFT_ARROW;
                break;
            case 38:
                // up arrow 38 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.UP_ARROW;
                break;
            case 39:
                // right arrow 39 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.RIGHT_ARROW;
                break;
            case 40:
                // down arrow 40 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.DOWN_ARROW;
                break;
            case 45:
                // insert 45 
                this.IsSpecialKey = false;
                this.keyCodeValue = "-";
                break;
            case 46:
                // delete 46 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.DELETE;
                break;
            case 48:
                // 0 48 
                if (this._shiftKey) {
                    this.keyCodeValue = ")";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(0);
                }
                break;
            case 49:
                // 1 49 
                if (this._shiftKey) {
                    this.keyCodeValue = "!";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(1);
                }
                break;
            case 50:
                // 2 50 
                if (this._shiftKey) {
                    this.keyCodeValue = "@"
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(2);
                }
                break;
            case 51:
                // 3 51 
                if (this._shiftKey) {
                    this.keyCodeValue = "#";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(3);
                }
                break;
            case 52:
                // 4 52 
                if (this._shiftKey) {
                    this.keyCodeValue = "$";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(4);
                }
                break;
            case 53:
                // 5 53 
                if (this._shiftKey) {
                    this.keyCodeValue = "%";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(5);
                }
                break;
            case 54:
                // 6 54 
                if (this._shiftKey) {
                    this.keyCodeValue = "^";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(6);
                }
                break;
            case 55:
                // 7 55 
                if (this._shiftKey) {
                    this.keyCodeValue = "&";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(7);
                }
                break;
            case 56:
                // 8 56 
                if (this._shiftKey) {
                    this.keyCodeValue = "*";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(8);
                }
                break;
            case 57:
                // 9 57 
                if (this._shiftKey) {
                    this.keyCodeValue = "(";
                }
                else {
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(9);
                }
                break;
            case 58:
                this.IsAlpha = true;
                this.keyCodeValue = ":";
                break;
            case 59:
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? ":" : ";";
                break;
            case 60:
                this.IsAlpha = true;
                this.keyCodeValue = "<";
                break;
            case 61:
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "+" : "=";
                break;
            case 62:
                this.IsAlpha = true;
                this.keyCodeValue = ">";
                break;
            case 63:
                this.IsAlpha = true;
                this.keyCodeValue = "?";
                break;
            case 64:
                this.keyCodeValue = "@";
                break;
            case 65:
                // a 65 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "A" : "a";
                break;
            case 66:
                // b 66 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "B" : "b";
                break;
            case 67:
                // c 67 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "C" : "c";
                break;
            case 68:
                // d 68 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "D" : "d";
                break;
            case 69:
                // e 69 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "E" : "e";
                break;
            case 70:
                // f 70 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "F" : "f";
                break;
            case 71:
                // g 71 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "G" : "g";
                break;
            case 72:
                // h 72 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "H" : "h";
                break;
            case 73:
                // i 73 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "I" : "i";
                break;
            case 74:
                // j 74 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "J" : "j";
                break;
            case 75:
                // k 75 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "K" : "k";
                break;
            case 76:
                // l 76 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "L" : "l";
                break;
            case 77:
                // m 77 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "M" : "m";
                break;
            case 78:
                // n 78 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "N" : "n";
                break;
            case 79:
                // o 79 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "O" : "o";
                break;
            case 80:
                // p 80 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "P" : "p";
                break;
            case 81:
                // q 81 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "Q" : "q";
                break;
            case 82:
                // r 82 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "R" : "r";
                break;
            case 83:
                // s 83 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "S" : "s";
                break;
            case 84:
                // t 84 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "T" : "t";
                break;
            case 85:
                // u 85 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "U" : "u";
                break;
            case 86:
                // v 86 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "V" : "v";
                break;
            case 87:
                // w 87 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "W" : "w";
                break;
            case 88:
                // x 88 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "X" : "x";
                break;
            case 89:
                // y 89 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "Y" : "y";
                break;
            case 90:
                // z 90 
                this.IsAlpha = true;
                this.keyCodeValue = this._shiftKey ? "Z" : "z";
                break;
            case 91:
                // left window key 91 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.LEFT_WINDOW;
                break;
            case 92:
                // right window key 92 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.RIGHT_WINDOW;
                break;
            case 93:
                // select key 93 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.SELECT;
                break;
            case 96:
                // numpad 0 96 

                this.IsNumber = true;
                this.keyCodeValue = parseInt(0);
                break;
            case 97:
                // numpad 1 97 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(1);
                break;
            case 98:
                // numpad 2 98 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(2);
                break;
            case 99:
                // numpad 3 99 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(3);
                break;
            case 100:
                // numpad 4 100 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(4);
                break;
            case 101:
                // numpad 5 101 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(5);
                break;
            case 102:
                // numpad 6 102 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(6);
                break;
            case 103:
                // numpad 7 103 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(7);
                break;
            case 104:
                // numpad 8 104 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(8);
                break;
            case 105:
                // numpad 9 105 
                this.IsNumber = true;
                this.keyCodeValue = parseInt(9);
                break;
            case 106:
                // multiply 106 
                this.keyCodeValue = "*";
                break;
            case 107:
                // add 107 
                this.keyCodeValue = "+";
                break;
            case 109:
                // subtract 109 
                this.keyCodeValue = this._shiftKey ? "_" : "-";
                break;
            case 110:
                // decimal point 110 
                this.keyCodeValue = this._shiftKey ? ">" : ".";
                break;
            case 111:
                // divide 111 
                this.keyCodeValue = this._shiftKey ? "?" : "/";
                break;
            case 112:
                // f1 112
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F1;
                break;
            case 113:
                // f2 113 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F2;
                break;
            case 114:
                // f3 114 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F3;
                break;
            case 115:
                // f4 115 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F4;
                break;
            case 116:
                // f5 116 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F5;
                break;
            case 117:
                // f6 117 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F6;
                break;
            case 118:
                // f7 118 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F7;
                break;
            case 119:
                // f8 119 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F8;
                break;
            case 120:
                // f9 120 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F9;
                break;
            case 121:
                // f10 121 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F10;
                break;
            case 122:
                // f11 122 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F11;
                break;
            case 123:
                // f12 123 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.F12;
                break;
            case 144:
                // num lock 144 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.NUM_LOCK;
                break;
            case 145:
                // scroll lock 145 
                this.IsSpecialKey = true;
                this.keyCodeValue = this.SCROLL_LOCK;
                break;
            case 186:
                // semi-colon 186 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? ":" : ";";
                break;
            case 187:
                // equal sign 187 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "+" : "=";
                break;
            case 188:
                // comma 188 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "<" : ",";
                break;
            case 189:
                // dash 189 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "_" : "-";
                break;
            case 190:
                // period 190 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? ">" : ".";
                break;
            case 191:
                // forward slash 191 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "?" : "/";
                break;
            case 192:
                // grave accent 192 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "~" : "`";
                break;
            case 219:
                // open bracket 219 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "{" : "[";
                break;
            case 220:
                // back slash 220 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "|" : "\\";
                break;
            case 221:
                // close braket 221 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "}" : "]";
                break;
            case 222:
                // single quote 222 
                this.IsSpecialChar = true;
                this.keyCodeValue = this._shiftKey ? "\"" : "'";
                break;
        }
    },

    getCharFromKeyCodeFF: function() {
        this.IsSpecialChar = false;
        this.IsNumber = false;
        this.IsAlpha = false;
        this.IsSpecialKey = false;

        if (this._event.which == 0) {
            this.IsSpecialChar = true;
        }
        else if (this._event.which >= 8 && this._event.which <= 46) {
            switch (this.keyCode) {
                case 8:
                    // backspace 8 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.BACKSPACE;
                    break;
                case 9:
                    // tab 9 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.TAB;
                    break;
                case 13:
                    // enter 13 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.ENTER;
                    break;
                case 16:
                    // shift 16 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.SHIFT;
                    break;
                case 17:
                    // ctrl 17 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.CTRL;
                    break;
                case 18:
                    // alt 18 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.ALT;
                    break;
                case 19:
                    // pause/break 19 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.BREAK;
                    break;
                case 20:
                    // caps lock 20 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.CAPS_LOCK;
                    break;
                case 27:
                    // escape 27 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.ESCAPE;
                    break;
                case 32:
                    // space 32
                    this.IsSpecialChar = true;
                    this.keyCodeValue = this.SPACE;
                    break;
                case 33:
                    // page up 33 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.PAGE_UP;
                    break;
                case 34:
                    // page down 34 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.PAGE_DOWN;
                    break;
                case 35:
                    // end 35 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.END;
                    break;
                case 36:
                    // home 36 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.HOME;
                    break;
                case 37:
                    // left arrow 37 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.LEFT_ARROW;
                    break;
                case 38:
                    // up arrow 38 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.UP_ARROW;
                    break;
                case 39:
                    // right arrow 39 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.RIGHT_ARROW;
                    break;
                case 40:
                    // down arrow 40 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.DOWN_ARROW;
                    break;
                case 45:
                    // insert 45 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.INSERT;
                    break;
                case 46:
                    // delete 46 
                    this.IsSpecialKey = true;
                    this.keyCodeValue = this.DELETE;
                    break;
            }
            return;
        }
        else if (this.keyCode == 16 && this._event.shiftKey) {
            this.IsSpecialChar = true;
            this.keyCode = this.SHIFT;
        }
        else if (this.keyCode == 110 || this.keyCode == 190) {
            this.IsSpecialChar = true;
            this.keyCodeValue = ".";

            return;
        }
        else if (this.keyCode >= 65 && this.keyCode <= 90 && this._event.shiftKey) {
            this.IsAlpha = true;
        }
        else if (this.keyCode >= 97 && this.keyCode <= 122 && !this._event.shiftKey && this._event.isChar) {
            this.IsAlpha = true;
        }
        else if (this.keyCode >= 96 && this.keyCode <= 105) {
            switch (this.keyCode) {
                case 96:
                    // numpad 0 96 

                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(0);
                    break;
                case 97:
                    // numpad 1 97 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(1);
                    break;
                case 98:
                    // numpad 2 98 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(2);
                    break;
                case 99:
                    // numpad 3 99 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(3);
                    break;
                case 100:
                    // numpad 4 100 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(4);
                    break;
                case 101:
                    // numpad 5 101 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(5);
                    break;
                case 102:
                    // numpad 6 102 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(6);
                    break;
                case 103:
                    // numpad 7 103 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(7);
                    break;
                case 104:
                    // numpad 8 104 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(8);
                    break;
                case 105:
                    // numpad 9 105 
                    this.IsNumber = true;
                    this.keyCodeValue = parseInt(9);
                    break;
            }
            return;
        }
        else if (this.keyCode > 127 && this._event.shiftKey) {
            this.IsSpecialChar = true;
            this.keyCodeValue = String.fromCharCode(parseInt(this.keyCode) - 96);
            return;
        }
        else if (this.keyCode >= 48 && this.keyCode <= 57) {
            this.IsNumber = true;
        }
        else {
            this.IsSpecialChar = true;
        }

        this.keyCodeValue = String.fromCharCode(this.keyCode);
    }


};