﻿/* ********************************************************************
 **********************************************************************
 * 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 TMI_attach, TMI_close;
  function TMI_buildKeyboardInputs(inputElems) {
    var self = this;

    this.TMI_version = "1.24";
    this.TMI_target = this.TMI_visible = false;
    this.TMI_shift = this.TMI_capslock = this.TMI_alternate = this.TMI_dead = false;
    this.TMI_deadkeysOn = false;
    this.TMI_kt = "US Int'l";  // Default keyboard layout
    this.TMI_clearPasswords = false;  // Clear password fields on focus
    this.TMI_showVersion = false;
    this.TMI_clickless = false;
    this.TMI_clicklessDelay = 500;
    this.TMI_keyCenter = 3;

    this.TMI_isIE = /*@cc_on!@*/false;
    this.TMI_useIFrame = /MSIE 6/i.test(navigator.userAgent);
    this.TMI_isIElt8 = /*@if(@_jscript_version < 5.8)!@end@*/false;
    this.TMI_isMoz = (navigator.product == "Gecko");
    this.TMI_isWebKit = RegExp("KHTML").test(navigator.userAgent);
	this.TMI_date = new Date();


    /* ***** Create keyboards ************************************** */
    this.TMI_layout = {};
    this.TMI_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.TMI_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.TMI_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.TMI_layout.Numpad = [ // Number pad
      [["$"], ["\u00a3"], ["\u20ac"], ["\u00a5"], ["/"], ["^"], ["Bksp", "Bksp"]],
      [["."], ["7"], ["8"], ["9"], ["*"], ["<"], ["("], ["["]],
      [["="], ["4"], ["5"], ["6"], ["-"], [">"], [")"], ["]"]],
      [["0"], ["1"], ["2"], ["3"], ["+"], ["Enter", "Enter"]],
      [[" "]]
    ];
    this.TMI_layoutDDK.Numpad = true;


    /* ***** Define Dead Keys ************************************** */
    this.TMI_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.TMI_deadkey['"'] = this.TMI_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.TMI_deadkey['~'] = [ // Tilde
      ["a", "\u00e3"], ["o", "\u00f5"], ["n", "\u00f1"],
      ["A", "\u00c3"], ["O", "\u00d5"], ["N", "\u00d1"]
    ];
    this.TMI_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.TMI_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.TMI_deadkey['\u02d8'] = [ // Romanian and Turkish breve
      ["a", "\u0103"], ["g", "\u011f"],
      ["A", "\u0102"], ["G", "\u011e"]
    ];
    this.TMI_deadkey['`'] = [ // Grave
      ["a", "\u00e0"], ["e", "\u00e8"], ["i", "\u00ec"], ["o", "\u00f2"], ["u", "\u00f9"],
      ["A", "\u00c0"], ["E", "\u00c8"], ["I", "\u00cc"], ["O", "\u00d2"], ["U", "\u00d9"]
    ];
    this.TMI_deadkey["'"] = this.TMI_deadkey['\u00b4'] = this.TMI_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.TMI_deadkey['\u02dd'] = [ // Hungarian Double Acute Accent
      ["o", "\u0151"], ["u", "\u0171"],
      ["O", "\u0150"], ["U", "\u0170"]
    ];
    this.TMI_deadkey['\u0385'] = [ // Greek Dialytika + Tonos
      ["\u03b9", "\u0390"], ["\u03c5", "\u03b0"]
    ];
    this.TMI_deadkey['\u00b0'] = this.TMI_deadkey['\u00ba'] = [ // Ring
      ["a", "\u00e5"], ["u", "\u016f"],
      ["A", "\u00c5"], ["U", "\u016e"]
    ];
    this.TMI_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.TMI_deadkey['\u02D9'] = [ // Dot-above
      ["c", "\u010B"], ["e", "\u0117"], ["g", "\u0121"], ["z", "\u017C"],
      ["C", "\u010A"], ["E", "\u0116"], ["G", "\u0120"], ["Z", "\u017B"]
    ];
    this.TMI_deadkey['\u00B8'] = this.TMI_deadkey['\u201a'] = [ // Cedilla
      ["c", "\u00e7"], ["s", "\u015F"],
      ["C", "\u00c7"], ["S", "\u015E"]
    ];
    this.TMI_deadkey[','] = [ // Comma
      ["s", (this.TMI_isIElt8) ? "\u015F" : "\u0219"], ["t", (this.TMI_isIElt8) ? "\u0163" : "\u021B"],
      ["S", (this.TMI_isIElt8) ? "\u015E" : "\u0218"], ["T", (this.TMI_isIElt8) ? "\u0162" : "\u021A"]
    ];



    /* ****************************************************************
     * Attach the keyboard to an element
     *
     */
    this.TMI_attachKeyboard = TMI_attach = function(elem, styleName) {
      if (elem.TMI_attached) return false;
      var keybut = document.createElement('img');
		keybut.src = pickerIconName(styleName, "clock.gif");		
		var size = pickerIconSize(styleName);
		if (0 < size) keybut.height = keybut.width = size;
      keybut.alt = "Time picker";
      keybut.className = "keyboardInputInitiator";
      keybut.title = "Show time picker";
      keybut.elem = elem;
      keybut.keyStyle = pickerStyle(styleName);
      keybut.onclick = function() { self.TMI_show(this.elem, this.keyStyle); };
      elem.TMI_attached = true;
      elem.parentNode.insertBefore(keybut, elem);
      if (this.TMI_isIE) {
        elem.onclick = elem.onselect = elem.onkeyup = function(e) {
          if ((e || event).type != "keyup" || !this.readOnly)
            this.range = document.selection.createRange();
        };
      }
    };


    var TMI_ClickAway=function(){self.TMI_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("timeInput") > -1)
          this.TMI_attachKeyboard(ex, ex.className);
        else if (ex.attachEvent)
		  {	ex.attachEvent("onfocus", TMI_ClickAway);
		  } else if (ex.addEventListener)
		  {	ex.addEventListener("focus", TMI_ClickAway, true);
		  }
      }


    /* ***** Build the keyboard interface ************************** */
    this.TMI_keyboard = document.createElement('table');
    this.TMI_keyboard.className = "keyboardInputMaster keyboardInputMedium";
    this.TMI_keyboard.dir = "ltr";
    this.TMI_keyboard.cellSpacing = this.TMI_keyboard.border = "0";

    var thead = document.createElement('thead');
	
      var tr = document.createElement('tr');
        var th = document.createElement('th');
         
          tr.appendChild(th);

		  
        var td = document.createElement('td');
          var clearer = document.createElement('span');
              clearer.id = "timeInputClear";
              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.TMI_target.value = "";
                self.TMI_target.focus();
                return false;
              };
            td.appendChild(clearer);

          var closer = document.createElement('span');
              closer.id = "timeInputClose";
              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.TMI_close(); };
            td.appendChild(closer);

          tr.appendChild(td);
        thead.appendChild(tr);
    this.TMI_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.TMI_showVersion) {
            var div = document.createElement('div');
              var ver = document.createElement('var');
                  ver.appendChild(document.createTextNode("v" + this.TMI_version));
                div.appendChild(ver);
              td.appendChild(div);
          }
          tr.appendChild(td);
        tbody.appendChild(tr);
		
    this.TMI_keyboard.appendChild(tbody);

    if (this.TMI_useIFrame) {
      this.TMI_iframe = document.createElement('iframe');
      this.TMI_iframe.style.position = "absolute";
      this.TMI_iframe.style.border = "0px none";
      this.TMI_iframe.style.filter = "mask()";
      this.TMI_iframe.style.zIndex = "999999";
    }

this.TMI_startingMonth = function(year, month)
{
  var thisDay = new Date();
 
  if ((month >= 0) && (year > 0))
  {
    thisDay = new Date(year, month, 1);
  } else
  {
    //day = thisDay.getDate();
    thisDay.setDate(1);
  }
  return thisDay;
};
/*
this.TMI_startingMonth = function(dateVal)
{
  var thisDay = new Date();
	if(dateVal)
		thisDay = dateVal;
 
    day = thisDay.getDate();
    thisDay.setDate(1);
  }
  return thisDay;
};
*/
this.TMI_getButtonCode = function(container, dateVal, adjust, label)
{
	var newSeconds = dateVal.getSeconds() + 60 * dateVal.getMinutes() + 3600 * dateVal.getHours() + adjust;
	while(newSeconds<0)
		newSeconds+=(3600*24);
	newSeconds=newSeconds%(3600*24);
	var newHours=parseInt(newSeconds/3600);
	newSeconds=newSeconds%3600;
	var newMinutes=parseInt(newSeconds/60);
	newSeconds=newSeconds%60;
  //var bt=document.createElement('button');
  var bt=container;
  bt.onclick = function()
  {
	//refreshDatePicker(\"" + datePickerDivID + "\",\"" + dateFieldName + "\", " + newYear + ", " + newMonth + ");'>" + label + "</button>";
	//self.TMI_show(this.elem);
	self.TMI_date.setHours(newHours);
	self.TMI_date.setMinutes(newMinutes);
	self.TMI_date.setSeconds(newSeconds);
	self.TMI_target.value=""+newHours+":"+parseInt(newMinutes/10)+(newMinutes%10)+":"+parseInt(newSeconds/10)+(newSeconds%10);
	//alert(self.TMI_target.value);
	self.TMI_buildKeys();
  };
	bt.onmouseover = function() {
	this.className += " hover";
	};
	bt.onmouseout = function() {
	this.className = this.className.replace(/ ?(hover|pressed)/g, "");
	};
	bt.onmousedown = function() {
	this.className += " pressed";
	};
	bt.onmouseup = function() {
	this.className = this.className.replace(/ ?pressed/g, "");
	};
  bt.ondblclick = function()
  {
	//refreshDatePicker(\"" + datePickerDivID + "\",\"" + dateFieldName + "\", " + newYear + ", " + newMonth + ");'>" + label + "</button>";
	//self.TMI_show(this.elem);
  };
  bt.appendChild(document.createTextNode(label));
  //container.appendChild(bt);
};

    /* ****************************************************************
     * Build or rebuild the keyboard keys
     *
     */
    this.TMI_buildKeys = function() {
      this.TMI_shift = this.TMI_capslock = this.TMI_alternate = this.TMI_dead = false;
	  this.TMI_deadkeysOn=false;
      //this.TMI_deadkeysOn = (this.TMI_layoutDDK[this.TMI_kt]) ? false : this.TMI_keyboard.getElementsByTagName('label')[0].getElementsByTagName('input')[0].checked;

      var container = this.TMI_keyboard.tBodies[0].getElementsByTagName('div')[0];
      while (container.firstChild) 
         container.removeChild(container.firstChild);

      {
		   var thisDay=this.TMI_date;
		   
	      var targetDate=stringToTime(this.TMI_target.value);
	      if(targetDate==null)
		      targetDate=new Date();
   		
		   var dayArrayShort = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
		   var monthArrayLong = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
   				
		   var table = document.createElement('table');
		   var tbody = document.createElement('tbody');
		   table.className = "keyboardInputCenter";
		   table.cellSpacing = table.border = "1";
		   {
			   var tr = document.createElement('tr');
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,3600,'+');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,600,'+');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,60,'+');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,10,'+');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,1,'+');
				   tr.appendChild(td);
			   }
			   tbody.appendChild(tr);
		   }
		   {
			   var tr = document.createElement('tr');
			   {
				   var td = document.createElement('td');
				   //td.colSpan="2";
				   td.className="timeDisplay";
				   td.appendChild(document.createTextNode(thisDay.getHours()));
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   td.className="timeColon";
				   td.appendChild(document.createTextNode(':'));
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   //td.colSpan="2";
				   td.className="timeDisplay";
				   td.appendChild(document.createTextNode((thisDay.getMinutes()/10)|0));
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   //td.colSpan="2";
				   td.className="timeDisplay";
				   td.appendChild(document.createTextNode(thisDay.getMinutes()%10));
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   td.appendChild(document.createTextNode(':'));
				   td.className="timeColon";
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   //td.colSpan="2";
				   td.className="timeDisplay";
				   td.appendChild(document.createTextNode((thisDay.getSeconds()/10)|0));
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   //td.colSpan="2";
				   td.className="timeDisplay";
				   td.appendChild(document.createTextNode(thisDay.getSeconds()%10));
				   tr.appendChild(td);
			   }
			   tbody.appendChild(tr);
		   }
		   {
			   var tr = document.createElement('tr');
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,-3600,'-');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,-600,'-');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,-60,'-');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,-10,'-');
				   tr.appendChild(td);
			   }
			   {
				   var td = document.createElement('td');
				   this.TMI_getButtonCode(td,thisDay,-1,'-');
				   tr.appendChild(td);
			   }
			   tbody.appendChild(tr);
		   }
		   table.appendChild(tbody);
   		
		   container.appendChild(table);
	   }
  };

    this.TMI_buildKeys();
    TMI_disableSelection(this.TMI_keyboard);


    /* ****************************************************************
     * Controls modifier keys
     *
     */
    this.TMI_modify = function(type) {
      switch (type) {
        case "Alt":
        case "AltGr": this.TMI_alternate = !this.TMI_alternate; break;
        case "Caps": this.TMI_capslock = !this.TMI_capslock; break;
        case "Shift": this.TMI_shift = !this.TMI_shift; break;
      } var vchar = 0;
      if (!this.TMI_shift != !this.TMI_capslock) vchar += 1;

      var tables = this.TMI_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.TMI_layout[this.TMI_kt][x][y];

          if (tds[y].className.indexOf('hover') > -1) className.push("hover");

          switch (lkey[1]) {
            case "Alt":
            case "AltGr":
              if (this.TMI_alternate) className.push("dead");
              break;
            case "Shift":
              if (this.TMI_shift) className.push("dead");
              break;
            case "Caps":
              if (this.TMI_capslock) className.push("dead");
              break;
            case "Tab": case "Enter": case "Bksp": break;
            default:
              if (type) tds[y].firstChild.nodeValue = lkey[vchar + ((this.TMI_alternate && lkey.length == 4) ? 2 : 0)];
              if (this.TMI_deadkeysOn) {
                var char = tds[y].firstChild.nodeValue;
                if (this.TMI_dead) {
                  if (char == this.TMI_dead) className.push("dead");
                  for (var z = 0; z < this.TMI_deadkey[this.TMI_dead].length; z++) {
                    if (char == this.TMI_deadkey[this.TMI_dead][z][0]) {
                      className.push("target");
                      break;
                    }
                  }
                }
                for (key in this.TMI_deadkey)
                  if (key === char) { className.push("alive"); break; }
              }
          }

          if (y == tds.length - 1 && tds.length > this.TMI_keyCenter) className.push("last");
          if (lkey[0] == " ") className.push("space");
          tds[y].className = className.join(" ");
        }
      }
    };


    /* ****************************************************************
     * Insert text at the cursor
     *
     */
    this.TMI_insert = function(text) {
      this.TMI_target.focus();
      if (this.TMI_target.maxLength) this.TMI_target.maxlength = this.TMI_target.maxLength;
      if (typeof this.TMI_target.maxlength == "undefined" ||
          this.TMI_target.maxlength < 0 ||
          this.TMI_target.value.length < this.TMI_target.maxlength) {
        if (this.TMI_target.setSelectionRange) {
          if (this.TMI_target.readOnly && this.TMI_isWebKit) {
            var rng = [this.TMI_target.selStart || 0, this.TMI_target.selEnd || 0];
          } else var rng = [this.TMI_target.selectionStart, this.TMI_target.selectionEnd];
          this.TMI_target.value = this.TMI_target.value.substr(0, rng[0]) + text + this.TMI_target.value.substr(rng[1]);
          if (text == "\n" && window.opera) rng[0]++;
          this.TMI_target.setSelectionRange(rng[0] + text.length, rng[0] + text.length);
          if (this.TMI_target.readOnly && this.TMI_isWebKit) {
            var range = window.getSelection().getRangeAt(0);
            this.TMI_target.selStart = range.startOffset;
            this.TMI_target.selEnd = range.endOffset;
          }
        } else if (this.TMI_target.createTextRange) {
          try {
            this.TMI_target.range.select();
          } catch(e) { this.TMI_target.range = document.selection.createRange(); }
          this.TMI_target.range.text = text;
          this.TMI_target.range.collapse(true);
          this.TMI_target.range.select();
        } else this.TMI_target.value += text;
        if (this.TMI_shift) this.TMI_modify("Shift");
        if (this.TMI_alternate) this.TMI_modify("AltGr");
        this.TMI_target.focus();
      } else if (this.TMI_target.createTextRange && this.TMI_target.range)
        this.TMI_target.range.select();
    };


    /* ****************************************************************
     * Show the keyboard interface
     *
     */
    this.TMI_show = function(elem, style) {
      if (style != null) this.TMI_keyboard.className = style;
      if (this.TMI_target = elem) {
        if (this.TMI_visible != elem) {
          if (this.TMI_isIE) {
            if (!this.TMI_target.range) {
              this.TMI_target.range = this.TMI_target.createTextRange();
              this.TMI_target.range.moveStart('character', this.TMI_target.value.length);
            } this.TMI_target.range.select();
          }
          try { this.TMI_keyboard.parentNode.removeChild(this.TMI_keyboard); } catch (e) {}
          if (this.TMI_clearPasswords && this.TMI_target.type == "password") this.TMI_target.value = "";
		  
		  this.TMI_date=stringToTime(this.TMI_target.value);
		  if(this.TMI_date==null)
		  {
			this.TMI_date=new Date();
			this.TMI_target.value=""+this.TMI_date.getHours()+":"+parseInt(this.TMI_date.getMinutes()/10)+(this.TMI_date.getMinutes()%10)+":"+parseInt(this.TMI_date.getSeconds()/10)+(this.TMI_date.getSeconds()%10);
		  }
		  //if(this.TMI_target.type == "password")

          var elem = this.TMI_target;
          this.TMI_target.keyboardPosition = "absolute";
          do {
            if (TMI_getStyle(elem, "position") == "fixed") {
              this.TMI_target.keyboardPosition = "fixed";
              break;
            }
          } while (elem = elem.offsetParent);

          if (this.TMI_useIFrame) document.body.appendChild(this.TMI_iframe);
          document.body.appendChild(this.TMI_keyboard);
          this.TMI_keyboard.style.top = this.TMI_keyboard.style.right = this.TMI_keyboard.style.bottom = this.TMI_keyboard.style.left = "auto";
          this.TMI_keyboard.style.position = this.TMI_target.keyboardPosition;

          this.TMI_visible = this.TMI_target;
		  this.TMI_buildKeys();
          this.TMI_position();
          this.TMI_target.focus();
        } else this.TMI_close();
      }
    };


    /* ****************************************************************
     * Position the keyboard
     *
     */
    this.TMI_position = function() {
      if (self.TMI_visible) {
        var inputElemPos = TMI_findPos(self.TMI_target);
        self.TMI_keyboard.style.top = inputElemPos[1] - ((self.TMI_target.keyboardPosition == "fixed" && !self.TMI_isIE && !self.TMI_isMoz) ? TMI_scrollDist()[1] : 0) + self.TMI_target.offsetHeight + 3 + "px";
        self.TMI_keyboard.style.left = Math.min(TMI_innerDimensions()[0] - self.TMI_keyboard.offsetWidth - 15, inputElemPos[0]) + "px";
        if (self.TMI_useIFrame) {
          self.TMI_iframe.style.width = self.TMI_keyboard.offsetWidth + "px";
          self.TMI_iframe.style.height = self.TMI_keyboard.offsetHeight + "px";
          self.TMI_iframe.style.top = self.TMI_keyboard.style.top;
          self.TMI_iframe.style.left = self.TMI_keyboard.style.left;
        }
      }
    };


    if (window.addEventListener) {
      window.addEventListener('resize', this.TMI_position, false);
    } else if (window.attachEvent)
      window.attachEvent('onresize', this.TMI_position);


    /* ****************************************************************
     * Close the keyboard interface
     *
     */
    this.TMI_close = TMI_close = function(stopFocus) {
      if (this.TMI_visible) {
        try {
          this.TMI_keyboard.parentNode.removeChild(this.TMI_keyboard);
          if (this.TMI_useIFrame) this.TMI_iframe.parentNode.removeChild(this.TMI_iframe);
        } catch (e) {}
        if (!stopFocus) this.TMI_target.focus();
        this.TMI_target = this.TMI_visible = false;
      }
    };
  };

  function TMI_findPos(obj) {
    var curleft = curtop = 0;
    do {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    return [curleft, curtop];
  }

  function TMI_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 TMI_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 TMI_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 TMI_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; };
  }
   function stringToTime(intime)
   {
	   if(intime==null || intime.length==0)
		   return null;
	   var punct;
	   if(intime.indexOf(".")!=-1)
		   punct=".";
	   else if(intime.indexOf("-")!=-1)
		   punct="-";
	   else
		   punct=":";
	   var hour = "00";
	   var minute = "00";
	   var second = "00";
	   var times = intime.split(punct);
	   if(times.length >= 1)
		   hour = times[0];
	   if(times.length >= 2)
		   minute = times[1];
	   if(times.length >= 3)
		   second = times[2];
	   if(parseInt(hour, 10) > 23 || parseInt(minute, 10) > 59 || parseInt(second, 10) > 59)
	   {
		   return null;
	   }
	   if (hour.length == 1)
		   hour = "0" + hour;
	   if (minute.length == 1)
		   minute = "0" + minute;
	   if (second.length == 1)
		   second = "0" + second;
	   var varTime = new Date(Date.parse("1/1/1970 " + hour + ":" + minute + ":" + second));
	   var cmpTime1 = hour + ":" + minute + ":" + second;
	   hour = varTime.getHours();
	   minute = varTime.getMinutes();
	   second = varTime.getSeconds();
	   if (hour.toString().length == 1)
		   hour = "0" + hour;
	   if (minute.toString().length == 1)
		   minute = "0" + minute;
	   if (second.toString().length == 1)
		   second = "0" + second;
	   var cmpTime2 = hour + ":" + minute + ":" + second;
	   if (cmpTime1 != cmpTime2)
	   {
		   return null;
	   }
	   return varTime;
   }
