﻿/* ********************************************************************
 **********************************************************************
 * HTML Virtual Keyboard Interface Script - v1.24
 *   Copyright (c) 2009 - GreyWyvern
 *
 *  - Licenced for free distribution under the BSDL
 *          http://www.opensource.org/licenses/bsd-license.php
 *
 * Add a script-driven keyboard interface to text fields, password
 * fields and textareas.
 *
 * See http://www.greywyvern.com/code/javascript/keyboard for examples
 * and usage instructions.
 *
 *This Version Modified for snap surveys, www.snapsurveys.com, starting from Version 1.24
 * Version 1.24 - May 12, 2009
 *   - Belarusian, Serbian Cyrillic and Serbian Latin keyboard layouts added (Evgeniy Titov)
 *   - Fix for maxlength attribute on textareas
 *
 *   See full changelog at:
 *     http://www.greywyvern.com/code/javascript/keyboard.changelog.txt
 *
 * Keyboard Credits
 *   - Belarusian, Serbian Cyrillic and Serbian Latin keyboard layouts by Evgeniy Titov
 *   - Bulgarian Phonetic keyboard layout by Samuil Gospodinov
 *   - Swedish keyboard layout by Håkan Sandberg
 *   - Romanian keyboard layout by Aurel
 *   - Farsi (Persian) keyboard layout by Kaveh Bakhtiyari (www.bakhtiyari.com)
 *   - Burmese keyboard layout by Cetanapa
 *   - Slovenian keyboard layout by Miran Zeljko
 *   - Hungarian keyboard layout by Antal Sall 'Hiromacu'
 *   - Arabic keyboard layout by Srinivas Reddy
 *   - Italian and Spanish (Spain) keyboard layouts by dictionarist.com
 *   - Lithuanian and Russian keyboard layouts by Ramunas
 *   - German keyboard layout by QuHno
 *   - French keyboard layout by Hidden Evil
 *   - Polish Programmers layout by moose
 *   - Turkish keyboard layouts by offcu
 *   - Dutch and US Int'l keyboard layouts by jerone
 *   - Portuguese keyboard layout by clisboa
 *
 */
var VNM_attach, VNM_close;
  function VNM_buildKeyboardInputs(inputElems) {
    var self = this;

    this.VNM_version = "1.24";
    this.VNM_target = this.VNM_visible = false;
    this.VNM_shift = this.VNM_capslock = this.VNM_alternate = this.VNM_dead = false;
    this.VNM_kt = "Numpad";  // Default keyboard layout
    this.VNM_clearPasswords = false;  // Clear password fields on focus
    this.VNM_showVersion = false;
    this.VNM_clickless = false;
    this.VNM_clicklessDelay = 500;
    this.VNM_keyCenter = 3;

    this.VNM_isIE = /*@cc_on!@*/false;
    this.VNM_useIFrame = /MSIE 6/i.test(navigator.userAgent);
    this.VNM_isIElt8 = /*@if(@_jscript_version < 5.8)!@end@*/false;
    this.VNM_isMoz = (navigator.product == "Gecko");
    this.VNM_isWebKit = RegExp("KHTML").test(navigator.userAgent);


    /* ***** Create keyboards ************************************** */
    this.VNM_layout = {};
    this.VNM_layoutDDK = {};

    // - Lay out each keyboard in rows of sub-arrays.  Each sub-array
    //   represents one key.
    //
    // - Each sub-array consists of four slots described as follows:
    //     example: ["a", "A", "\u00e1", "\u00c1"]
    //
    //          a) Normal character
    //          A) Character + Shift or Caps
    //     \u00e1) Character + Alt or AltGr
    //     \u00c1) Character + Shift or Caps + Alt or AltGr
    //
    //   You may include sub-arrays which are fewer than four slots.
    //   In these cases, the missing slots will be blanked when the
    //   corresponding modifier key (Shift or AltGr) is pressed.
    //
    // - If the second slot of a sub-array matches one of the following
    //   strings:
    //       "Tab", "Caps", "Shift", "Enter", "Bksp", "Alt" OR "AltGr"
    //   then the function of the key will be the following,
    //   respectively:
    //     - Insert a tab
    //     - Toggle Caps Lock (technically a Shift Lock)
    //     - Next entered character will be the shifted character
    //     - Insert a newline (textarea), or close the keyboard
    //     - Delete the previous character
    //     - Next entered character will be the alternate character
    //
    //   The first slot of this sub-array will be the text to display
    //   on the corresponding key.  This allows for easy localisation
    //   of key names.
    //
    // - Layout dead keys (diacritic + letter) should be added as
    //   arrays of two item arrays with hash keys equal to the
    //   diacritic.  See the "this.VNM_deadkey" object below the layout
    //   definitions.  In  each two item child array, the second item
    //   is what the diacritic would change the first item to.
    //
    // - To disable dead keys for a layout, simply assign true to the
    //   this.VNM_layoutDDK (DDK = disable dead keys) object of the
    //   same name as the layout.  See the Numpad layout below for an
    //   example.
    //
    // - Note that any characters beyond the normal ASCII set should be
    //   entered in escaped Unicode format.  (eg \u00a3 = Pound symbol)
    //   You can find Unicode values for characters here:
    //     http://unicode.org/charts/
    //
    // - To remove a keyboard, just delete it, or comment it out of the
    //   source code


    this.VNM_layout.Numpad = [ // Number pad
    [["+"], ["-"], ["del", "Bksp"]],
    [["7"], ["8"], ["9"]],
    [["4"], ["5"], ["6"]],
    [["1"], ["2"], ["3"]],
    [["0"],["."]]
      //[["$"], ["\u00a3"], ["\u20ac"], ["\u00a5"], ["/"], ["^"], ["Bksp", "Bksp"]],
      //[["."], ["7"], ["8"], ["9"], ["*"], ["<"], ["("], ["["]],
      //[["="], ["4"], ["5"], ["6"], ["-"], [">"], [")"], ["]"]],
      //[["0"], ["1"], ["2"], ["3"], ["+"], ["Enter", "Enter"]],
      //[[" "]]
    ];
    this.VNM_layoutDDK.Numpad = true;

    /* ***** Define Dead Keys ************************************** */
    this.VNM_deadkey = {};

    // - Lay out each dead key set in one row of sub-arrays.  The rows
    //   below are wrapped so uppercase letters are below their
    //   lowercase equivalents.
    //
    // - The first letter in each sub-array is the letter pressed after
    //   the diacritic.  The second letter is the letter this key-combo
    //   will generate.
    //
    // - Note that if you have created a new keyboard layout and want
    //   it included in the distributed script, PLEASE TELL ME if you
    //   have added additional dead keys to the ones below.

    this.VNM_deadkey['"'] = this.VNM_deadkey['\u00a8'] = [ // Umlaut / Diaeresis / Greek Dialytika
      ["a", "\u00e4"], ["e", "\u00eb"], ["i", "\u00ef"], ["o", "\u00f6"], ["u", "\u00fc"], ["y", "\u00ff"], ["\u03b9", "\u03ca"], ["\u03c5", "\u03cb"],
      ["A", "\u00c4"], ["E", "\u00cb"], ["I", "\u00cf"], ["O", "\u00d6"], ["U", "\u00dc"], ["Y", "\u0178"], ["\u0399", "\u03aa"], ["\u03a5", "\u03ab"]
    ];
    this.VNM_deadkey['~'] = [ // Tilde
      ["a", "\u00e3"], ["o", "\u00f5"], ["n", "\u00f1"],
      ["A", "\u00c3"], ["O", "\u00d5"], ["N", "\u00d1"]
    ];
    this.VNM_deadkey['^'] = [ // Circumflex
      ["a", "\u00e2"], ["e", "\u00ea"], ["i", "\u00ee"], ["o", "\u00f4"], ["u", "\u00fb"], ["w", "\u0175"], ["y", "\u0177"],
      ["A", "\u00c2"], ["E", "\u00ca"], ["I", "\u00ce"], ["O", "\u00d4"], ["U", "\u00db"], ["W", "\u0174"], ["Y", "\u0176"]
    ];
    this.VNM_deadkey['\u02c7'] = [ // Baltic caron
      ["c", "\u010D"], ["s", "\u0161"], ["z", "\u017E"], ["r", "\u0159"], ["d", "\u010f"], ["t", "\u0165"], ["n", "\u0148"], ["l", "\u013e"], ["e", "\u011b"],
      ["C", "\u010C"], ["S", "\u0160"], ["Z", "\u017D"], ["R", "\u0158"], ["D", "\u010e"], ["T", "\u0164"], ["N", "\u0147"], ["L", "\u013d"], ["E", "\u011a"]
    ];
    this.VNM_deadkey['\u02d8'] = [ // Romanian and Turkish breve
      ["a", "\u0103"], ["g", "\u011f"],
      ["A", "\u0102"], ["G", "\u011e"]
    ];
    this.VNM_deadkey['`'] = [ // Grave
      ["a", "\u00e0"], ["e", "\u00e8"], ["i", "\u00ec"], ["o", "\u00f2"], ["u", "\u00f9"],
      ["A", "\u00c0"], ["E", "\u00c8"], ["I", "\u00cc"], ["O", "\u00d2"], ["U", "\u00d9"]
    ];
    this.VNM_deadkey["'"] = this.VNM_deadkey['\u00b4'] = this.VNM_deadkey['\u0384'] = [ // Acute / Greek Tonos
      ["a", "\u00e1"], ["e", "\u00e9"], ["i", "\u00ed"], ["o", "\u00f3"], ["u", "\u00fa"], ["y", "\u00fd"], ["\u03b1", "\u03ac"], ["\u03b5", "\u03ad"], ["\u03b7", "\u03ae"], ["\u03b9", "\u03af"], ["\u03bf", "\u03cc"], ["\u03c5", "\u03cd"], ["\u03c9", "\u03ce"],
      ["A", "\u00c1"], ["E", "\u00c9"], ["I", "\u00cd"], ["O", "\u00d3"], ["U", "\u00da"], ["Y", "\u00dd"], ["\u0391", "\u0386"], ["\u0395", "\u0388"], ["\u0397", "\u0389"], ["\u0399", "\u038a"], ["\u039f", "\u038c"], ["\u03a5", "\u038e"], ["\u03a9", "\u038f"]
    ];
    this.VNM_deadkey['\u02dd'] = [ // Hungarian Double Acute Accent
      ["o", "\u0151"], ["u", "\u0171"],
      ["O", "\u0150"], ["U", "\u0170"]
    ];
    this.VNM_deadkey['\u0385'] = [ // Greek Dialytika + Tonos
      ["\u03b9", "\u0390"], ["\u03c5", "\u03b0"]
    ];
    this.VNM_deadkey['\u00b0'] = this.VNM_deadkey['\u00ba'] = [ // Ring
      ["a", "\u00e5"], ["u", "\u016f"],
      ["A", "\u00c5"], ["U", "\u016e"]
    ];
    this.VNM_deadkey['\u02DB'] = [ // Ogonek
      ["a", "\u0106"], ["e", "\u0119"], ["i", "\u012f"], ["o", "\u01eb"], ["u", "\u0173"], ["y", "\u0177"],
      ["A", "\u0105"], ["E", "\u0118"], ["I", "\u012e"], ["O", "\u01ea"], ["U", "\u0172"], ["Y", "\u0176"]
    ];
    this.VNM_deadkey['\u02D9'] = [ // Dot-above
      ["c", "\u010B"], ["e", "\u0117"], ["g", "\u0121"], ["z", "\u017C"],
      ["C", "\u010A"], ["E", "\u0116"], ["G", "\u0120"], ["Z", "\u017B"]
    ];
    this.VNM_deadkey['\u00B8'] = this.VNM_deadkey['\u201a'] = [ // Cedilla
      ["c", "\u00e7"], ["s", "\u015F"],
      ["C", "\u00c7"], ["S", "\u015E"]
    ];
    this.VNM_deadkey[','] = [ // Comma
      ["s", (this.VNM_isIElt8) ? "\u015F" : "\u0219"], ["t", (this.VNM_isIElt8) ? "\u0163" : "\u021B"],
      ["S", (this.VNM_isIElt8) ? "\u015E" : "\u0218"], ["T", (this.VNM_isIElt8) ? "\u0162" : "\u021A"]
    ];



    /* ****************************************************************
     * Attach the keyboard to an element
     *
     */
    this.VNM_attachKeyboard = VNM_attach = function(elem, styleName) {
      if (elem.VNM_attached) return false;
      var keybut = document.createElement('img');
		keybut.src = pickerIconName(styleName, "numbers.gif");		
		var size = pickerIconSize(styleName);
		if (0 < size) keybut.height = keybut.width = size;
      keybut.alt = "Number pad";
      keybut.className = "keyboardInputInitiator";
      keybut.title = "Show number pad";
      keybut.elem = elem;
      keybut.keyStyle = pickerStyle(styleName);
      keybut.onclick = function() { self.VNM_show(this.elem, this.keyStyle); };
      elem.VNM_attached = true;
      elem.parentNode.insertBefore(keybut, elem);
      if (this.VNM_isIE) {
        elem.onclick = elem.onselect = elem.onkeyup = function(e) {
          if ((e || event).type != "keyup" || !this.readOnly)
            this.range = document.selection.createRange();
        };
      }
    };


    var VNM_ClickAway=function(){self.VNM_close(true);};
    /* ***** Find tagged input & textarea elements ***************** */
    for (var x = 0, elem; elem = inputElems[x++];)
      for (var y = 0, ex; ex = elem[y++];)
      { 
         if ((ex.nodeName == "TEXTAREA" || ex.type == "text" || ex.type == "password") && (ex.className.indexOf("keyboardInput_unused") > -1 || ex.className.indexOf("numberInput") > -1 || ex.className.indexOf("timeInput_unused") > -1))
           this.VNM_attachKeyboard(ex, ex.className);
        else if (ex.attachEvent)
		  {	ex.attachEvent("onfocus", VNM_ClickAway);
		  } else if (ex.addEventListener)
		  {	ex.addEventListener("focus", VNM_ClickAway, true);
		  }
      }


    /* ***** Build the keyboard interface ************************** */
    this.VNM_keyboard = document.createElement('table');
    this.VNM_keyboard.className = "keyboardInputMaster keyboardInputMedium";
    this.VNM_keyboard.dir = "ltr";
    this.VNM_keyboard.cellSpacing = this.VNM_keyboard.border = "0";

    var thead = document.createElement('thead');
      var tr = document.createElement('tr');
        var th = document.createElement('th');
          /*var kblist = document.createElement('select');
            for (ktype in this.VNM_layout) {
              if (typeof this.VNM_layout[ktype] == "object") {
                var opt = document.createElement('option');
                    opt.value = ktype;
                    opt.appendChild(document.createTextNode(ktype));
                  kblist.appendChild(opt);
              }
            }
            if (kblist.options.length) {
                kblist.value = this.VNM_kt;
                kblist.onchange = function() {
                  self.VNM_kt = this.value;
                  self.VNM_buildKeys();
                  self.VNM_position();
                };
              th.appendChild(kblist);
            }*/

            /*var label = document.createElement('label');
              var checkbox = document.createElement('input');
                  checkbox.type = "checkbox";
                  checkbox.title = "Dead keys: " + ((this.VNM_deadkeysOn) ? "On" : "Off");
                  checkbox.defaultChecked = this.VNM_deadkeysOn;
                  checkbox.onclick = function() {
                    self.VNM_deadkeysOn = this.checked;
                    this.title = "Dead keys: " + ((this.checked) ? "On" : "Off");
                    self.VNM_modify("");
                    return true;
                  };
                label.appendChild(this.VNM_deadkeysElem = checkbox);
                  checkbox.checked = this.VNM_deadkeysOn;
            th.appendChild(label);*/
          tr.appendChild(th);

        var td = document.createElement('td');
          var clearer = document.createElement('span');
              clearer.id = "numPadInputClear";
              clearer.appendChild(document.createTextNode("Clear"));
              clearer.title = "Clear this input";
              clearer.onmousedown = function() { this.className = "pressed"; };
              clearer.onmouseup = function() { this.className = ""; };
              clearer.onclick = function() {
                self.VNM_target.value = "";
                self.VNM_target.focus();
                return false;
              };
            td.appendChild(clearer);

          var closer = document.createElement('span');
              closer.id = "knumPadInputClose";
              closer.appendChild(document.createTextNode('Close'));
              closer.title = "Close this window";
              closer.onmousedown = function() { this.className = "pressed"; };
              closer.onmouseup = function() { this.className = ""; };
              closer.onclick = function() { self.VNM_close(); };
            td.appendChild(closer);

          tr.appendChild(td);
        thead.appendChild(tr);
    this.VNM_keyboard.appendChild(thead);

    var tbody = document.createElement('tbody');
      var tr = document.createElement('tr');
        var td = document.createElement('td');
            td.colSpan = "2";
          var div = document.createElement('div');
              div.className = "keyboardInputLayout";
            td.appendChild(div);
          if (this.VNM_showVersion) {
            var div = document.createElement('div');
              var ver = document.createElement('var');
                  ver.appendChild(document.createTextNode("v" + this.VNM_version));
                div.appendChild(ver);
              td.appendChild(div);
          }
          tr.appendChild(td);
        tbody.appendChild(tr);
    this.VNM_keyboard.appendChild(tbody);

    if (this.VNM_useIFrame) {
      this.VNM_iframe = document.createElement('iframe');
      this.VNM_iframe.style.position = "absolute";
      this.VNM_iframe.style.border = "0px none";
      this.VNM_iframe.style.filter = "mask()";
      this.VNM_iframe.style.zIndex = "999999";
    }


    /* ****************************************************************
     * Build or rebuild the keyboard keys
     *
     */
    this.VNM_buildKeys = function() {
      this.VNM_shift = this.VNM_capslock = this.VNM_alternate = this.VNM_dead = false;
      //this.VNM_deadkeysOn = (this.VNM_layoutDDK[this.VNM_kt]) ? false : this.VNM_keyboard.getElementsByTagName('label')[0].getElementsByTagName('input')[0].checked;

      var container = this.VNM_keyboard.tBodies[0].getElementsByTagName('div')[0];
      while (container.firstChild) container.removeChild(container.firstChild);

      for (var x = 0, hasDeadKey = false, lyt; lyt = this.VNM_layout[this.VNM_kt][x++];) {
        var table = document.createElement('table');
            table.cellSpacing = table.border = "0";
        if (lyt.length <= this.VNM_keyCenter) table.className = "keyboardInputCenter";
          var tbody = document.createElement('tbody');
            var tr = document.createElement('tr');
            for (var y = 0, lkey; lkey = lyt[y++];) {
              var td = document.createElement('td');
                  td.appendChild(document.createTextNode(lkey[0]));

                var className = [];
                //if (this.VNM_deadkeysOn)
                //  for (key in this.VNM_deadkey)
                //    if (key === lkey[0]) { className.push("alive"); break; }
                if (lyt.length > this.VNM_keyCenter && y == lyt.length) className.push("last");
                if (lkey[0] == " ") className.push("space");
                if (parseInt(lkey[0],10)>0) className.push("numDisplay");
                else if (lkey[0] == ".")  className.push("numDisplay");
                else if (lkey[0] == "0")  className.push("numDisplay zeroDisplay");
                td.className = className.join(" ");

                  td.VNM_clickless = 0;
                  if (!td.click) {
                    td.click = function() {
                      var evt = this.ownerDocument.createEvent('MouseEvents');
                      evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
                      this.dispatchEvent(evt);
                    };
                  }
                  td.onmouseover = function() {
                    if (self.VNM_clickless) {
                      var _self = this;
                      clearTimeout(this.VNM_clickless);
                      this.VNM_clickless = setTimeout(function() { _self.click(); }, self.VNM_clicklessDelay);
                    }
                    if (this.firstChild.nodeValue != "\xa0") this.className += " hover";
                  };
                  td.onmouseout = function() {
                    if (self.VNM_clickless) clearTimeout(this.VNM_clickless);
                    this.className = this.className.replace(/ ?(hover|pressed)/g, "");
                  };
                  td.onmousedown = function() {
                    if (self.VNM_clickless) clearTimeout(this.VNM_clickless);
                    if (this.firstChild.nodeValue != "\xa0") this.className += " pressed";
                  };
                  td.onmouseup = function() {
                    if (self.VNM_clickless) clearTimeout(this.VNM_clickless);
                    this.className = this.className.replace(/ ?pressed/g, "");
                  };
                  td.ondblclick = function() { return false; };

                switch (lkey[1]) {
                  case "Caps":
                  case "Shift":
                  case "Alt":
                  case "AltGr":
                    td.onclick = (function(type) { return function() { self.VNM_modify(type); return false; }; })(lkey[1]);
                    break;
                  case "0":
                    td.onclick = function() { self.VNM_insert("0"); return false; };
                    break;
                  case "":
                    //td.onclick = function() { self.VNM_insert("0"); return false; };
                    break;
                 case "Tab":
                    td.onclick = function() { self.VNM_insert("\t"); return false; };
                    break;
                  case "Bksp":
                    td.onclick = function() {
                      self.VNM_target.focus();
                      if (self.VNM_target.setSelectionRange) {
                        if (self.VNM_target.readOnly && self.VNM_isWebKit) {
                          var rng = [self.VNM_target.selStart || 0, self.VNM_target.selEnd || 0];
                        } else var rng = [self.VNM_target.selectionStart, self.VNM_target.selectionEnd];
                        if (rng[0] < rng[1]) rng[0]++;
                        self.VNM_target.value = self.VNM_target.value.substr(0, rng[0] - 1) + self.VNM_target.value.substr(rng[1]);
                        self.VNM_target.setSelectionRange(rng[0] - 1, rng[0] - 1);
                        if (self.VNM_target.readOnly && self.VNM_isWebKit) {
                          var range = window.getSelection().getRangeAt(0);
                          self.VNM_target.selStart = range.startOffset;
                          self.VNM_target.selEnd = range.endOffset;
                        }
                      } else if (self.VNM_target.createTextRange) {
                        try {
                          self.VNM_target.range.select();
                        } catch(e) { self.VNM_target.range = document.selection.createRange(); }
                        if (!self.VNM_target.range.text.length) self.VNM_target.range.moveStart('character', -1);
                        self.VNM_target.range.text = "";
                      } else self.VNM_target.value = self.VNM_target.value.substr(0, self.VNM_target.value.length - 1);
                      if (self.VNM_shift) self.VNM_modify("Shift");
                      if (self.VNM_alternate) self.VNM_modify("AltGr");
                      self.VNM_target.focus();
                      return true;
                    };
                    break;
                  case "Enter":
                    td.onclick = function() {
                      if (self.VNM_target.nodeName != "TEXTAREA") {
                        self.VNM_close();
                        this.className = this.className.replace(/ ?(hover|pressed)/g, "");
                      } else self.VNM_insert("\n");
                      return true;
                    };
                    break;
                  default:
                    td.onclick = function() {
                      /*if (self.VNM_deadkeysOn && self.VNM_dead) {
                        if (self.VNM_dead != this.firstChild.nodeValue) {
                          for (key in self.VNM_deadkey) {
                            if (key == self.VNM_dead) {
                              if (this.firstChild.nodeValue != " ") {
                                for (var z = 0, rezzed = false, dk; dk = self.VNM_deadkey[key][z++];) {
                                  if (dk[0] == this.firstChild.nodeValue) {
                                    self.VNM_insert(dk[1]);
                                    rezzed = true;
                                    break;
                                  }
                                }
                              } else {
                                self.VNM_insert(self.VNM_dead);
                                rezzed = true;
                              } break;
                            }
                          }
                        } else rezzed = true;
                      } self.VNM_dead = false;*/

                      if (/*!rezzed &&*/ this.firstChild.nodeValue != "\xa0") {
                        /*if (self.VNM_deadkeysOn) {
                          for (key in self.VNM_deadkey) {
                            if (key == this.firstChild.nodeValue) {
                              self.VNM_dead = key;
                              this.className += " dead";
                              if (self.VNM_shift) self.VNM_modify("Shift");
                              if (self.VNM_alternate) self.VNM_modify("AltGr");
                              break;
                            }
                          }
                          if (!self.VNM_dead) self.VNM_insert(this.firstChild.nodeValue);
                        } else*/
                           self.VNM_insert(this.firstChild.nodeValue);
                      }

                      self.VNM_modify("");
                      return false;
                    };

                }
                tr.appendChild(td);
              tbody.appendChild(tr);
            table.appendChild(tbody);

            for (var z = 0; z < 4; z++)
              if (this.VNM_deadkey[lkey[z] = lkey[z] || "\xa0"]) hasDeadKey = true;
        }
        container.appendChild(table);
      }
      //this.VNM_deadkeysElem.style.display = (!this.VNM_layoutDDK[this.VNM_kt] && hasDeadKey) ? "inline" : "none";
    };

    this.VNM_buildKeys();
    VNM_disableSelection(this.VNM_keyboard);


    /* ****************************************************************
     * Controls modifier keys
     *
     */
    this.VNM_modify = function(type) {
      switch (type) {
        case "Alt":
        case "AltGr": this.VNM_alternate = !this.VNM_alternate; break;
        case "Caps": this.VNM_capslock = !this.VNM_capslock; break;
        case "Shift": this.VNM_shift = !this.VNM_shift; break;
      } var vchar = 0;
      if (!this.VNM_shift != !this.VNM_capslock) vchar += 1;

      var tables = this.VNM_keyboard.getElementsByTagName('table');
      for (var x = 0; x < tables.length; x++) {
        var tds = tables[x].getElementsByTagName('td');
        for (var y = 0; y < tds.length; y++) {
          var className = [];
          var lkey = this.VNM_layout[this.VNM_kt][x][y];

          if (tds[y].className.indexOf('hover') > -1) className.push("hover");

          switch (lkey[1]) {
            /*case "Alt":
            case "AltGr":
              if (this.VNM_alternate) className.push("dead");
              break;
            case "Shift":
              if (this.VNM_shift) className.push("dead");
              break;
            case "Caps":
              if (this.VNM_capslock) className.push("dead");
              break;*/
            case "Tab": case "Enter": case "Bksp": break;
            default:
              if (type) tds[y].firstChild.nodeValue = lkey[vchar + ((this.VNM_alternate && lkey.length == 4) ? 2 : 0)];
              /*if (this.VNM_deadkeysOn) {
                var char = tds[y].firstChild.nodeValue;
                if (this.VNM_dead) {
                  if (char == this.VNM_dead) className.push("dead");
                  for (var z = 0; z < this.VNM_deadkey[this.VNM_dead].length; z++) {
                    if (char == this.VNM_deadkey[this.VNM_dead][z][0]) {
                      className.push("target");
                      break;
                    }
                  }
                }
                for (key in this.VNM_deadkey)
                  if (key === char) { className.push("alive"); break; }
              }*/
          }

          if (y == tds.length - 1 && tds.length > this.VNM_keyCenter) className.push("last");
          if (lkey[0] == " ") className.push("space");
          if (parseInt(lkey[0],10)>0) className.push("numDisplay");
          else if (lkey[0] == ".")  className.push("numDisplay");
          else if (lkey[0] == "0")  className.push("numDisplay zeroDisplay");
          
          tds[y].className = className.join(" ");
        }
      }
    };


    /* ****************************************************************
     * Insert text at the cursor
     *
     */
    this.VNM_insert = function(text) {
      this.VNM_target.focus();
      if (this.VNM_target.maxLength) this.VNM_target.maxlength = this.VNM_target.maxLength;
      if (typeof this.VNM_target.maxlength == "undefined" ||
          this.VNM_target.maxlength < 0 ||
          this.VNM_target.value.length < this.VNM_target.maxlength) {
        if (this.VNM_target.setSelectionRange) {
          if (this.VNM_target.readOnly && this.VNM_isWebKit) {
            var rng = [this.VNM_target.selStart || 0, this.VNM_target.selEnd || 0];
          } else var rng = [this.VNM_target.selectionStart, this.VNM_target.selectionEnd];
          this.VNM_target.value = this.VNM_target.value.substr(0, rng[0]) + text + this.VNM_target.value.substr(rng[1]);
          if (text == "\n" && window.opera) rng[0]++;
          this.VNM_target.setSelectionRange(rng[0] + text.length, rng[0] + text.length);
          if (this.VNM_target.readOnly && this.VNM_isWebKit) {
            var range = window.getSelection().getRangeAt(0);
            this.VNM_target.selStart = range.startOffset;
            this.VNM_target.selEnd = range.endOffset;
          }
        } else if (this.VNM_target.createTextRange) {
          try {
            this.VNM_target.range.select();
          } catch(e) { this.VNM_target.range = document.selection.createRange(); }
          this.VNM_target.range.text = text;
          this.VNM_target.range.collapse(true);
          this.VNM_target.range.select();
        } else this.VNM_target.value += text;
        if (this.VNM_shift) this.VNM_modify("Shift");
        if (this.VNM_alternate) this.VNM_modify("AltGr");
        this.VNM_target.focus();
      } else if (this.VNM_target.createTextRange && this.VNM_target.range)
        this.VNM_target.range.select();
    };


    /* ****************************************************************
     * Show the keyboard interface
     *
     */
    this.VNM_show = function(elem, style) {
      if (style != null) this.VNM_keyboard.className = style;
      if (this.VNM_target = elem) {
        if (this.VNM_visible != elem) {
          if (this.VNM_isIE) {
            if (!this.VNM_target.range) {
              this.VNM_target.range = this.VNM_target.createTextRange();
              this.VNM_target.range.moveStart('character', this.VNM_target.value.length);
            } this.VNM_target.range.select();
          }
          try { this.VNM_keyboard.parentNode.removeChild(this.VNM_keyboard); } catch (e) {}
          if (this.VNM_clearPasswords && this.VNM_target.type == "password") this.VNM_target.value = "";

          var elem = this.VNM_target;
          this.VNM_target.keyboardPosition = "absolute";
          do {
            if (VNM_getStyle(elem, "position") == "fixed") {
              this.VNM_target.keyboardPosition = "fixed";
              break;
            }
          } while (elem = elem.offsetParent);

          if (this.VNM_useIFrame) document.body.appendChild(this.VNM_iframe);
          document.body.appendChild(this.VNM_keyboard);
          this.VNM_keyboard.style.top = this.VNM_keyboard.style.right = this.VNM_keyboard.style.bottom = this.VNM_keyboard.style.left = "auto";
          this.VNM_keyboard.style.position = this.VNM_target.keyboardPosition;

          this.VNM_visible = this.VNM_target;
          this.VNM_position();
          this.VNM_target.focus();
        } else this.VNM_close();
      }
    };


    /* ****************************************************************
     * Position the keyboard
     *
     */
    this.VNM_position = function() {
      if (self.VNM_visible) {
        var inputElemPos = VNM_findPos(self.VNM_target);
        self.VNM_keyboard.style.top = inputElemPos[1] - ((self.VNM_target.keyboardPosition == "fixed" && !self.VNM_isIE && !self.VNM_isMoz) ? VNM_scrollDist()[1] : 0) + self.VNM_target.offsetHeight + 3 + "px";
        self.VNM_keyboard.style.left = Math.min(VNM_innerDimensions()[0] - self.VNM_keyboard.offsetWidth - 15, inputElemPos[0]) + "px";
        if (self.VNM_useIFrame) {
          self.VNM_iframe.style.width = self.VNM_keyboard.offsetWidth + "px";
          self.VNM_iframe.style.height = self.VNM_keyboard.offsetHeight + "px";
          self.VNM_iframe.style.top = self.VNM_keyboard.style.top;
          self.VNM_iframe.style.left = self.VNM_keyboard.style.left;
        }
      }
    };


    if (window.addEventListener) {
      window.addEventListener('resize', this.VNM_position, false);
    } else if (window.attachEvent)
      window.attachEvent('onresize', this.VNM_position);


    /* ****************************************************************
     * Close the keyboard interface
     *
     */
    this.VNM_close = VNM_close = function(stopFocus) {
      if (this.VNM_visible) {
        try {
          this.VNM_keyboard.parentNode.removeChild(this.VNM_keyboard);
          if (this.VNM_useIFrame) this.VNM_iframe.parentNode.removeChild(this.VNM_iframe);
        } catch (e) {}
        if (!stopFocus) this.VNM_target.focus();
        this.VNM_target = this.VNM_visible = false;
      }
    };
  };

  function VNM_findPos(obj) {
    var curleft = curtop = 0;
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft, curtop];
  }

  function VNM_innerDimensions() {
    if (self.innerHeight) {
      return [self.innerWidth, self.innerHeight];
    } else if (document.documentElement && document.documentElement.clientHeight) {
      return [document.documentElement.clientWidth, document.documentElement.clientHeight];
    } else if (document.body)
      return [document.body.clientWidth, document.body.clientHeight];
    return [0, 0];
  }

  function VNM_scrollDist() {
    var html = document.getElementsByTagName('html')[0];
    if (html.scrollTop && document.documentElement.scrollTop) {
      return [html.scrollLeft, html.scrollTop];
    } else if (html.scrollTop || document.documentElement.scrollTop)
      return [html.scrollLeft + document.documentElement.scrollLeft, html.scrollTop + document.documentElement.scrollTop];
    return [0, 0];
  }

  function VNM_getStyle(obj, styleProp) {
    if (obj.currentStyle) {
      var y = obj.currentStyle[styleProp];
    } else if (window.getComputedStyle)
      var y = window.getComputedStyle(obj, null)[styleProp];
    return y;
  }

  function VNM_disableSelection(elem) {
    elem.onselectstart = function() { return false; };
    elem.unselectable = "on";
    elem.style.MozUserSelect = "none";
    elem.style.cursor = "default";
    if (window.opera) elem.onmousedown = function() { return false; };
  }

