/*
 * Copyright (c)  2006-2007 Maskat Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
maskat.lang.Class.declare("maskat.widget.rialto.TextWrapper")
	.extend("maskat.widget.rialto.RialtoWidgetWrapper", {

    createWidget: function(parent) {
    	var text = new rialto.widget.Text(
    		this.name,
    		this.top,
    		this.left,
    		this.width,
    		this.datatype,
    		null, /* parent */
    		this);

		var self = this;
		text.onblur = function() { self.notifyEvent("onblur"); };
		text.onfocus = function() { self.notifyEvent("onfocus"); };

		this.widget = text;
		return text;
    },

	getControlElement: function(){
		return this.widget.getHtmlExt().firstChild;
	},

	getValue: function() {
		switch (this.widget.datatype) {
		case "DT":
		case "DA":
		case "D":
			return this.getDateTimeValue();
		default:
			return this.widget.getValue();
		}
	},
	
	getDateTimeValue: function(){
		var value = this.widget.getValue();
		  if (value.length!=11){
		     return "";
		  }
		  if (this.datatype=="D"){
		     return this.month() + "/" + this.day() + "/" + this.year();
		  }
		  if (this.datatype=="DA"){
		     return this.year() + "-" + this.month() + "-" + this.day();
		  }
		  if (this.datatype=="DT"){
		     var datePart =  this.year() + "-" + this.month() + "-" + this.day();
		     var now = new Date();
		     var hours = now.getHours();
		     var strHours = hours<10? "0"+hours:""+hours;
		     var minutes = now.getMinutes();
		     var strMinutes = minutes<10? "0"+minutes:""+minutes;
		     var seconds = now.getSeconds();
		     var strSeconds = seconds<10? "0"+seconds:""+seconds;
		     return datePart + "T" + strHours + ":" + strMinutes + ":" + strSeconds;
		  }
		  return "";
	},
	
    year :function(){
	  var value = this.widget.getValue();
	  if (value.length != 11){
	    return "";
	  }
	  return value.substring(0,4);
	},
	
	month : function(){
	  var value = this.widget.getValue();
	  if (value.length != 11){
	    return "";
	  }
	  return value.substring(5,7);
	},
	
	day : function(){
	  var value = this.widget.getValue();
	  if (value.length != 11){
	    return "";
	  }
	  return value.substring(8,10);
	},

	setValue: function(value) {
		this.widget.setValue(value);
	},

	clear: function() {
		this.widget.setValue("");
	},

    postCreateWidget: function() {
		this.addKeyEventListener(this.widget.divExt);
    },

	setFocus: function() {
		this.widget.divExt.childNodes[0].focus();
	}
});
