// DateTimeControl Client Script Library
// Alfa-XP, Michael Isipciuc, 2007.
//
FM.UI.DateTimeControl= function(element) {
		FM.UI.DateTimeControl.initializeBase(this, [element]);
		this._formatD= element.getAttribute('dateformat');	
		this._formatT= element.getAttribute('timeformat');	
		this.hasTime= (this._formatT != null);
		this._format= (this.hasTime) ? (this._formatD + " " + this._formatT) : this._formatD;
		
		this._inputD= this._ui.rows[0].cells[0].firstChild.firstChild;
		this._btnD= this._ui.rows[0].cells[1].firstChild;
		this._prevD= this._prevT= "";
		
		if (this.hasTime) {
			this._inputT= this._ui.rows[0].cells[2].firstChild.firstChild;
			this._btnT= this._ui.rows[0].cells[3].firstChild;
		} else {
			this._inputT= null;
		}
		this._focusElement= this._inputD;
		
		this.Date= null;
}

FM.UI.DateTimeControl.prototype= {
	initialize : function() {
    FM.UI.DateTimeControl.callBaseMethod(this, "initialize");
    //
	},
	
	attachEvents : function() {
		if (this._eventsAttached) return; else this._eventsAttached= true;
		
		$addHandlers(this._btnD, {
			'click': this.btn_click,
			'mouseover': this.btn_on,
			'mouseout' : this.btn_off
		}, this);
		$addHandlers(this._inputD, {
			'click': this._input_click,
			'focus': this._input_onfocus,
			'beforedeactivate': this.ParseDate
			/*''change': this.ParseDate*/
		}, this);
		
		if (this.hasTime) {
			$addHandlers(this._btnT, {
				'click': this.btnT_click,
				'mouseover': this.btnT_on,
				'mouseout' : this.btnT_off
			}, this);
			$addHandlers(this._inputT, {
				'focus': this._inputT_onfocus,
				'beforedeactivate': this.ParseTime
			}, this);
		}	
	},
	
	detachEvents : function() {
		if (!this._eventsAttached) return; else this._eventsAttached= false;
		
		$clearHandlers(this._btnD);
		$clearHandlers(this._inputD);
		if (this.hasTime) {
			$clearHandlers(this._btnT);
			$clearHandlers(this._inputT);
		}
	},
	
	initPopup : function() {
		//
	},
	
    
	dispose : function() {
		if (!this._inputD) return;
		
		if (FM.UI.PopupCalendar._current) { FM.UI.PopupCalendar._current.Hide(); FM.UI.PopupCalendar._current= null; }
		if (FM.UI.TimeTable._current) { FM.UI.TimeTable._current.Hide(); FM.UI.TimeTable._current= null; }
		
    FM.UI.DateTimeControl.callBaseMethod(this, "dispose");
    
    this._inputD= null; this._inputT= null;
  },
  
  _input_click: function(e) {
		if (this.getValue() == "") this.showCalendar();
	},
  _input_onfocus: function(e) {
		this._prevD= this._inputD.value;
	},
	_inputT_onfocus: function(e) {
		this._prevT= this._inputT.value;
	},
	_input_onchange: function(e) {
		this.Parse();
	},
	
  RegisterOnChangeHandler : function(h, reg) {
		if (reg) {
			$addHandler(this._inputD, 'change', h);
			if (this._inputT) $addHandler(this._inputT, 'change', h);
		} else {
			// this._inputD and this._inputT 
			// will be unregistered in the dispose method
		}
  },
  
  setValue : function(s) { 
		this._control.value= s;
		this.Date= DT_parseUtcValue(s);
		this._inputD.value= (s.length > 0) ? this.Date.localeFormat(this._formatD) : "";
		if (this._inputT) this._inputT.value= (s.length > 0) ? this.Date.localeFormat(this._formatT) : "";
	},
	setDate : function(oDate) { 
		this.Date= oDate;
		this._control.value= (this.Date != null) ? DT_formatUtcDate(this.Date) : "";
		this._inputD.value= (this.Date != null) ? this.Date.localeFormat(this._formatD) : "";
		if (this._inputT) this._inputT.value= (this.Date != null) ? this.Date.localeFormat(this._formatT) : "";
	},
		
  getValue : function() { 
		return this._control.value;
	},
	getDate : function() { 
		if (this.Date == null) this.Date= DT_parseUtcValue(this._control.value);
		return this.Date;
	},
	getDisplayValue : function() { 
		var d= Trim(this._inputD.value);
		if (d.length == 0) return d;
		var t= (this._inputT) ? Trim(this._inputT.value) : "";
		if (t.length > 0) return (d + " " + t);
		return d;
	},

	setValueFromCalendar : function(y, m, d) { 
		var oDate= this.Date;
		if (!oDate) oDate= new Date();
		oDate.setFullYear(y, m, d);
		this.setDate(oDate);
		this._inputD.focus();
		this.NotifyObservers();
	},
	
	setValueFromTimeTable : function(h, m) { 
		var oDate= this.Date;
		if (!oDate) oDate= new Date();
		oDate.setHours(h, m, 0, 0);
		this.setDate(oDate);
		this._inputT.focus();
		this.NotifyObservers();
	},
	
	ParseDate : function(e) { 
		var d= this.getDisplayValue();
		if (d.length == 0) { this.Date= null; this._control.value= ""; return; }
		var date= DT_parseDisplayValue(d, this._format);
		if (date == null) {
			window.Form__MustValidDate= "You must enter a valid date.";
			alert(window.Form__MustValidDate + "\n\n" + this._formatD);
			this._inputD.value= this._prevD;
			if (e) { e.preventDefault(); e.stopPropagation(); }
			else this._inputD.focus();
		} else {
			this.Date= date;
			this._control.value= DT_formatUtcDate(this.Date);
		}
	},
	ParseTime : function(e) { 
		var d= this.getDisplayValue();
		if (d.length == 0) return;
		var date= DT_parseDisplayValue(d, this._format);
		if (date == null) {
			window.Form__MustValidTime= "You must enter a valid time.";
			alert(window.Form__MustValidTime + "\n\n" + this._formatT);
			this._inputT.value= this._prevT;
			if (e) { e.preventDefault(); e.stopPropagation(); }
			else this._inputT.focus();
		} else {
			this.Date= date;
			this._control.value= DT_formatUtcDate(this.Date);
		}
	},


  btn_click : function(e) {
		this._input_onfocus(e);
		this.showCalendar();
  },
  btnT_click : function(e) {
		this._inputT_onfocus(e);
		this.showTimeTable();
  },
  
  showCalendar : function() {
		var o= FM.UI.PopupCalendar.getCurrent();
		if (o.Window.Visibled) { o.Hide(); return; }
		if (!this.Date) {
			this.Date= DT_parseDisplayValue(this.getDisplayValue(), this._format);
		}
		
		o.Window.SetOffsetParent(this._inputD);
		o.SetCurrentDate(this.Date, this._formatD);
		if (this.returnValueDlg == null) this.returnValueDlg= Function.createDelegate(this, this.setValueFromCalendar);
		o.OnSelect= this.returnValueDlg;
		o.Show();
  },
  
  showTimeTable : function() {
		var o= FM.UI.TimeTable.getCurrent(this._formatT);
		if (o.Window.Visibled) { o.Hide(); return; }
		if (!this.Date) {
			this.Date= DT_parseDisplayValue(this.getDisplayValue(), this._format);
		}
		
		o.Window.SetOffsetParent(this._inputT);
		o.SetCurrentTime(this.Date, this._formatT);
		if (this.returnTTDlg == null) this.returnTTDlg= Function.createDelegate(this, this.setValueFromTimeTable);
		o.OnSelect= this.returnTTDlg;
		o.Show();
  },
  
	btn_on : function() 	{ this._btnD.src= fmCommonPath + "Images/btn2_on_cal.gif"; },
	btn_off : function() { this._btnD.src= fmCommonPath + "Images/btn2_off_cal.gif"; },
	btnT_on : function() 	{ this._btnT.src= fmCommonPath + "Images/btn_on_tm.gif"; },
	btnT_off : function() { this._btnT.src= fmCommonPath + "Images/btn_off_tm.gif"; },
	
	redraw : function() { 
		this._inputD.disabled= !this.Enabled;
		if (this._inputT) this._inputT.disabled= !this.Enabled;
		
		this._btnD.src= fmCommonPath + "Images/btn2_"+(this.Enabled ? "off" : "dis")+"_cal.gif"
		if (this._btnT) this._btnT.src= fmCommonPath + "Images/btn_"+(this.Enabled ? "off" : "dis")+"_tm.gif";
	}
}

FM.UI.DateTimeControl.inheritsFrom(FM.UI.BaseFormControl);
FM.UI.DateTimeControl.registerClass('FM.UI.DateTimeControl', FM.UI.BaseFormControl);


Sys.Application.notifyScriptLoaded();
