Fix datepicker text input handling

Change-Id: Ia2a34baaab2a9f695097f2c11484ad74ba8cd4e8
diff --git a/dev/js/src/datepicker.js b/dev/js/src/datepicker.js
index 0fd47dd..8ae5267 100644
--- a/dev/js/src/datepicker.js
+++ b/dev/js/src/datepicker.js
@@ -7,6 +7,9 @@
 define(['util'], function () {
   "use strict";
 
+  KorAP._validDateMatchRE   = new RegExp("^[lg]?eq$");
+  KorAP._validDateRE        = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
+
   /*
    * Localizations
    */
@@ -45,18 +48,18 @@
      */
     select : function (year, month, day) {
       if (arguments.length >= 1) {
-	this._selected = {'year' : year};
-	this._showYear = year;
-	if (arguments.length >= 2) {
-	  this._selected['month'] = month;
-	  this._showMonth = month;
-	  if (arguments.length >= 3) {
-	    this._selected['day'] = day;
-	    this._showDay = day;
-	  };
-	};
+        this._selected = {'year' : year};
+        this._showYear = year;
+        if (arguments.length >= 2) {
+          this._selected['month'] = month;
+          this._showMonth = month;
+          if (arguments.length >= 3) {
+            this._selected['day'] = day;
+            this._showDay = day;
+          };
+        };
 
-	return this;
+        return this;
       };
 
       return this._selected;
@@ -74,11 +77,11 @@
 
     store : function () {
       if (this._click !== undefined)
-	this._click(this._selected);
+        this._click(this._selected);
       else 
-	console.dir(this._selected);
+        console.dir(this._selected);
     },
-
+    
     /**
      * Set the action for clicking as a callback.
      * The callback will retrieve a an object with
@@ -103,18 +106,18 @@
       e.setAttribute('tabindex', 0);
       e.style.outline = 0;
       e.classList.add('datepicker');
-
+      
       var today = new Date();
 
       // Show year
       this._showYear = (year !== undefined) ? year :
-	(this._selected['year'] ? this._selected['year'] :
-	 today.getYear() + 1900);
+        (this._selected['year'] ? this._selected['year'] :
+         today.getYear() + 1900);
 
       // Show month
       this._showMonth = month ? month :
-	(this._selected['month'] ? this._selected['month'] :
-	 (today.getMonth() + 1));
+        (this._selected['month'] ? this._selected['month'] :
+         (today.getMonth() + 1));
 
       // Append all helpers
       this._element.appendChild(this._monthHelper());
@@ -134,25 +137,29 @@
       input.setAttribute('tabindex', 0);
 
       input.addEventListener(
-	'keyup',
-	function (e) {
-	  if (e.keyCode == 13) {
-	    if (this.fromString(input.value))
-	      this.store();
-
-	    e.halt();
-            return false;
-	  }
-	  else {
-	    if (this.fromString(input.value)) {
-	      this._updateYear();
-	      this._updateMonth();
-	      this._updateDay();
-	    };
-	  };
-	}.bind(this)
+        'keyup',
+        function (e) {
+          if (this.fromString(input.value)) {
+            this._updateYear();
+            this._updateMonth();
+            this._updateDay();
+          };
+        }.bind(this)
       );
 
+      input.addEventListener(
+        'keypress',
+        function (e) {
+          if (e.keyCode == 13) {
+            if (this.fromString(input.value))
+              this.store();
+
+            e.halt();
+            return false;
+          }
+        }.bind(this)
+      )
+
       return input;
     },
 
@@ -181,15 +188,15 @@
       var v = '';
       var s = this._selected;
       if (s['year']) {
-	v += s['year'];
-	if (s['month']) {
-	  v += '-';
-	  v += s['month'] < 10 ? '0' + s['month'] : s['month'];
-	  if (s['day']) {
-	    v += '-';
-	    v += s['day'] < 10 ? '0' + s['day'] : s['day'];
-	  };
-	};
+        v += s['year'];
+        if (s['month']) {
+          v += '-';
+          v += s['month'] < 10 ? '0' + s['month'] : s['month'];
+          if (s['day']) {
+            v += '-';
+            v += s['day'] < 10 ? '0' + s['day'] : s['day'];
+          };
+        };
       };
       return v;
     },
@@ -199,11 +206,11 @@
      */
     incrYear : function () {
       if (this._showYear < 9999) {
-	this._showYear++;
-	this._updateYear();
-	this._updateMonth();
-	this._updateDay();
-	return this;
+        this._showYear++;
+        this._updateYear();
+        this._updateMonth();
+        this._updateDay();
+        return this;
       };
       return;
     },
@@ -214,11 +221,11 @@
      */
     decrYear : function () {
       if (this._showYear > 0) {
-	this._showYear--;
-	this._updateYear();
-	this._updateMonth();
-	this._updateDay();
-	return this;
+        this._showYear--;
+        this._updateYear();
+        this._updateMonth();
+        this._updateDay();
+        return this;
       };
       return;
     },
@@ -230,12 +237,12 @@
     incrMonth : function () {
       this._showMonth++;
       if (this._showMonth > 12) {
-	this._showMonth = 1;
-	this.incrYear();
+        this._showMonth = 1;
+        this.incrYear();
       }
       else {
-	this._updateMonth();
-	this._updateDay();
+        this._updateMonth();
+        this._updateDay();
       };
       return this;
     },
@@ -247,12 +254,12 @@
     decrMonth : function () {
       this._showMonth--;
       if (this._showMonth < 1) {
-	this._showMonth = 12;
-	this.decrYear();
+        this._showMonth = 12;
+        this.decrYear();
       }
       else {
-	this._updateMonth();
-	this._updateDay();
+        this._updateMonth();
+        this._updateDay();
       };
 
       return this;
@@ -266,19 +273,19 @@
 
       // Decrement year
       year.appendChild(d.createElement('span'))
-	.onclick = this.decrYear.bind(this);
+        .onclick = this.decrYear.bind(this);
 
       this._yElement = year.appendChild(d.createElement('span'));
       this._yElement.appendChild(document.createTextNode(this._showYear));
 
       this._yElement.onclick = function () {
-	this.set(this._showYear);
+        this.set(this._showYear);
       }.bind(this);
       this._selectYear();
 
       // Increment year
       year.appendChild(d.createElement('span'))
-	.onclick = this.incrYear.bind(this);
+        .onclick = this.incrYear.bind(this);
 
       return year;
     },
@@ -293,9 +300,9 @@
     // Check if the viewed year is current
     _selectYear : function () {
       if (this._showYear === this.select()['year'])
-	this._yElement.classList.add('selected');
+        this._yElement.classList.add('selected');
       else
-	this._yElement.classList.remove('selected');
+        this._yElement.classList.remove('selected');
     },
 
 
@@ -306,21 +313,21 @@
 
       // Decrement month
       month.appendChild(d.createElement('span'))
-	.onclick = this.decrMonth.bind(this);
+        .onclick = this.decrMonth.bind(this);
       
       this._mElement = month.appendChild(d.createElement('span'));
       this._mElement.appendChild(
-	document.createTextNode(loc.MONTH[this._showMonth-1])
+        document.createTextNode(loc.MONTH[this._showMonth-1])
       );
       this._mElement.onclick = function () {
-	this.set(this._showYear, this._showMonth);
+        this.set(this._showYear, this._showMonth);
       }.bind(this);
 
       this._selectMonth();
       
       // Increment month
       month.appendChild(d.createElement('span'))
-	.onclick = this.incrMonth.bind(this);
+        .onclick = this.incrMonth.bind(this);
 
       return month;
     },
@@ -328,7 +335,7 @@
     // Update the month helper view.
     _updateMonth : function () {
       if (this._showMonth === undefined || this._showMonth > 12)
-	this._showMonth = 1;
+        this._showMonth = 1;
 
       this._mElement.firstChild.data = loc.MONTH[this._showMonth-1];
       this._selectMonth();
@@ -338,10 +345,10 @@
     // Check if the viewed month is current
     _selectMonth : function () {
       if (this._showYear === this.select()['year'] &&
-	  this._showMonth === this.select()['month'])
-	this._mElement.classList.add('selected');
+          this._showMonth === this.select()['month'])
+        this._mElement.classList.add('selected');
       else
-	this._mElement.classList.remove('selected');
+        this._mElement.classList.remove('selected');
     },
 
 
@@ -351,10 +358,10 @@
 
       // Localized day view
       var tr = table.appendChild(d.createElement('thead'))
-	.appendChild(d.createElement('tr'));
+          .appendChild(d.createElement('tr'));
       for (var i = 0; i < 7; i++) {
-	tr.appendChild(d.createElement('th'))
-	  .appendChild(d.createTextNode(loc.WDAY[i]));
+        tr.appendChild(d.createElement('th'))
+          .appendChild(d.createTextNode(loc.WDAY[i]));
       };
 
       this._dBElement = this._dayBody();
@@ -365,22 +372,22 @@
 
     _dayBody : function () {
       var showDate = new Date(
-	this._showYear,
-	this._showMonth - 1,
-	1,
-	0,
-	0,
-	0,
-	0
+        this._showYear,
+        this._showMonth - 1,
+        1,
+        0,
+        0,
+        0,
+        0
       );
       var date = new Date(
-	this._showYear,
-	this._showMonth - 1,
-	1,
-	0,
-	0,
-	0,
-	0
+        this._showYear,
+        this._showMonth - 1,
+        1,
+        0,
+        0,
+        0,
+        0
       );
       var today = new Date();
       var that = this;
@@ -388,11 +395,11 @@
       // What happens, in case someone clicks
       // on a date
       var click = function () {
-	that.set(
-	  that._showYear,
-	  that._showMonth,
-	  parseInt(this.firstChild.data)
-	);
+        that.set(
+          that._showYear,
+          that._showMonth,
+          parseInt(this.firstChild.data)
+        );
       };
 
       // Skip back to the previous monday (may be in the last month)
@@ -405,46 +412,46 @@
       // Iterate over all days of the table
       while (1) {
 
-	// Loop through the week
-	var tr = tb.appendChild(d.createElement('tr'));
-	for (var i = 0; i < 7; i++) {
-	  var td = tr.appendChild(d.createElement('td'));
+        // Loop through the week
+        var tr = tb.appendChild(d.createElement('tr'));
+        for (var i = 0; i < 7; i++) {
+          var td = tr.appendChild(d.createElement('td'));
+          
+          // Not part of the current month
+          if (date.getMonth() !== showDate.getMonth()) {
+            td.classList.add('out');
+          }
+          else {
+            td.onclick = click;
+          };
+    
+          // This is the current day
+          if (date.getDate()     === today.getDate() &&
+              date.getMonth()    === today.getMonth() &&
+              date.getFullYear() === today.getFullYear()) {
+            td.classList.add('today');
+          };
 
-	  // Not part of the current month
-	  if (date.getMonth() !== showDate.getMonth()) {
-	    td.classList.add('out');
-	  }
-	  else {
-	    td.onclick = click;
-	  };
-	  
-	  // This is the current day
-	  if (date.getDate()     === today.getDate() &&
-	      date.getMonth()    === today.getMonth() &&
-	      date.getFullYear() === today.getFullYear()) {
-	    td.classList.add('today');
-	  };
+          // This is the day selected
+          if (s && s['day']) {
+            if (date.getDate()     === s['day'] &&
+                date.getMonth()    === s['month']-1 &&
+                date.getFullYear() === s['year']) {
+              td.classList.add('selected');
+            };
+          };
 
-	  // This is the day selected
-	  if (s && s['day']) {
-	    if (date.getDate()     === s['day'] &&
-		date.getMonth()    === s['month']-1 &&
-		date.getFullYear() === s['year']) {
-	      td.classList.add('selected');
-	    };
-	  };
+          // Add the current day to the table
+          td.appendChild(
+            d.createTextNode(date.getDate())
+          );
+    
+          // Next day
+          date.setDate(date.getDate() + 1);
+        };
 
-	  // Add the current day to the table
-	  td.appendChild(
-	    d.createTextNode(date.getDate())
-	  );
-	  
-	  // Next day
-	  date.setDate(date.getDate() + 1);
-	};
-
-	if (date.getMonth() !== showDate.getMonth())
-	  break;
+        if (date.getMonth() !== showDate.getMonth())
+          break;
       };
       return tb;
     },
@@ -453,18 +460,18 @@
     _updateDay : function () {
       var newBody = this._dayBody();
       this._dBElement.parentNode.replaceChild(
-	newBody,
-	this._dBElement
+        newBody,
+        this._dBElement
       );
       this._dBElement = newBody;
     },
 
     fromString : function (v) {
       if (v === undefined)
-	return false;
+        return false;
 
       if (!KorAP._validDateRE.test(v))
-	return false;
+        return false;
 
       var d = v.split('-', 3);
       d[0] = parseInt(d[0]);
diff --git a/dev/js/src/vc.js b/dev/js/src/vc.js
index 22b2c84..d6a26f2 100644
--- a/dev/js/src/vc.js
+++ b/dev/js/src/vc.js
@@ -58,8 +58,6 @@
 
   // ???
   KorAP._validStringMatchRE = new RegExp("^(?:eq|ne|contains(?:not)?|excludes)$");
-  KorAP._validDateMatchRE   = new RegExp("^[lg]?eq$");
-  KorAP._validDateRE        = new RegExp("^(?:\\d{4})(?:-\\d\\d(?:-\\d\\d)?)?$");
   KorAP._overrideStyles     = false;
 
   var loc = KorAP.Locale;