﻿function initDatePicker(elm)
{
	if (window._datePickerFunctions == null) {
		window._datePickerFunctions = 
		{
			_cboDay: [],
			_cboMonth: [],
			_fldYear: [],

			getValue: function()
			{
				with (this)
				{
					var date = new Date(_fldYear.getValue(), _cboMonth.getValue() - 1, _cboDay.getValue());		
					if (date.getFullYear() != castInt(_fldYear.getValue()) 
						|| date.getMonth() != (castInt(_cboMonth.getValue()) - 1) 
						|| date.getDate() != castInt(_cboDay.getValue()))			
						date = null;		
					return date;
				}	
			},

			setValue: function(date)
			{
				with (this)
				{
					_cboDay.setValue(date != null ? date.getDate() : "");
					_cboMonth.setValue(date != null ? date.getMonth() + 1 : "");
					_fldYear.setValue(date != null ? date.getFullYear() : "");
				}	
			},

			getYearText: function() {return this._fldYear.value;},

			isEmpty: function()
			{		
				with (this)
				{
					return !_cboDay.getValue() && !_cboMonth.getValue() 
							&& ((_yearEntryFreeForm && _fldYear.isEmpty()) || (!_yearEntryFreeForm && !_fldYear.getValue()));			
				}	
			},

			isValid: function()			
			{
				if (this._yearEntryFreeForm && !this._fldYear.isValid())
					return false;
				else	
					return this.getValue() != null;
			},

			getEnabled: function()		{return this._cboDay.getEnabled();},
			setEnabled: function (val)		
			{
				this._cboDay.setEnabled(val);
				this._cboMonth.setEnabled(val);
				this._fldYear.setEnabled(val);		
			},

			getReadOnly: function ()		{return this._cboDay.getReadOnly();},
			setReadOnly: function (val) 
			{
				this._cboDay.setReadOnly(val);
				this._cboMonth.setReadOnly(val);
				this._fldYear.setReadOnly(val);		
			},

			setFocus: function()	{this._cboDay.setFocus();},

			_onChildCtlChanged: function() 
			{
				if (this._parent._onChangeHandler)
					window.postAction("try {" + this._parent.id + "._onChangeHandler(); } catch(e) {displayErr('DatePicker_OnChange', e);}");		
			},

			setOnChangeHandler: function(handler) {this._onChangeHandler = handler;},
			
			cleanUp: function()
			{
				if (elm._cboDay != null)	{ elm._cboDay._parent = null;   elm._cboDay._onChangeHandler = null; }
				if (elm._cboMonth != null)	{ elm._cboMonth._parent = null; elm._cboMonth._onChangeHandler = null; }
				if (elm._fldYear != null)	{ elm._fldYear._parent = null;  elm._fldYear._onChangeHandler = null; }
			}
			
		};
	}
	
	// save a copy of any pre-initialized handlers
	var initOnChangeHandler = elm._onChangeHandler;

	// add interfaces
	elm = extendElement(elm);
	elm = Object.extend(elm, window._datePickerFunctions);

	// initialize
	elm._cboDay = document.getElementById(elm.id + "_cboDay");		elm._cboDay._parent = elm;		
	elm._cboMonth = document.getElementById(elm.id + "_cboMonth");	elm._cboMonth._parent = elm;	
	elm._fldYear = document.getElementById(elm.id + "_cboYear");		
	if (!elm._fldYear) {
		elm._fldYear = document.getElementById(elm.id + "_numYear");		
		elm._yearEntryFreeForm = true;
	}
	elm._fldYear._parent = elm;

	// auto event wire up
	elm._onChangeHandler = (initOnChangeHandler ? initOnChangeHandler : getEventHandler(elm.id, "OnChange"));
	elm._cboDay._onChangeHandler = window._datePickerFunctions._onChildCtlChanged;
	elm._cboMonth._onChangeHandler = window._datePickerFunctions._onChildCtlChanged;
	elm._fldYear._onChangeHandler = window._datePickerFunctions._onChildCtlChanged;

	return elm;	
}
