Bw.Widgets.Datefield =
{
	superclassName: "Bw.Widgets.Editable",
	selfclassName: "Bw.Widgets.Datefield",
	
	create: function (val)
	{
		var obj = document.createElement ("div");
		
		if (val) obj.setAttribute ("value", val);
		
		Bw.Core.bootstrap (obj, "Bw.Widgets.Datefield");
		
		return obj;
	},
	
	initialize: function ()
	{
		Bw.Widgets.Widget.initialize.call (this);

		this.draw();

		this.calendar = null;

		var v = this.getAttribute ("value");
		if (v && v != '') this.setValue (v);

		var self = this;
		this.button.onclick = function () { self.toggleCalendar(); return true; };
		
		var self = this;
		
		this.onupdate = this.getAttribute ("onupdate");
		this.field.onupdate = function () { self.changed(); }; 
		
		this.onfocus = this.getAttribute ("onfocus");
		this.field.onfocus = function () { self.focused(); }; 
		
		return false;
	}, 
	
	clear: function ()
	{
		this.field.clear();
	},
	
	changed: function ()
	{
		if (this.onupdate) Bw.Util.call (this, this.onupdate);
	},
	
	focused: function ()
	{
		if (this.onfocus) Bw.Util.call (this, this.onfocus);
	},
	
	validate: function ()
	{
		var v = this.field.getValue();
		if (v == "") return;
		
		var d = Bw.Date.Helpers.read (v);
		if (isNaN(d))
		{
			alert ("La date est invalide");
		}
	},
	
	draw: function ()
	{
		this.field = Bw.Widgets.Textfield.create ();
		
		var self = this;
		this.field.field.onchange = function () { self.validate() };
		
		//this.field.title = Bw.Date.DEFAULT_DATE_FORMAT;
		//this.field.field.readOnly = true;

		this.button = Bw.Widgets.StockIcon.create ("calendar");
		
		this.appendChild (this.field);
		this.appendChild (this.button);
		
		var b = document.createElement ("div");
		b.style.clear = "both";
		this.appendChild (b);
	},

	toggleCalendar: function ()
	{
		if (this.disabled) return;
		
		var f = this.field;
		var c = this.calendar;
		if (c == null)
		{
			c = Bw.Widgets.Calendar.create();
			with (c.style)
			{
				position = "absolute";
				zIndex = "10000";
			}
			
			var self = this;
			c.onaction = function() { self.calendarAction(); };
			
			document.body.appendChild (c);
			
			with (c.style)
			{
				top = (Bw.Util.getElementTopPosition (f) + this.field.offsetHeight - 1) + "px";
				left = Bw.Util.getElementLeftPosition (f) + "px";
			}
			c.hide();
			
			this.calendar = c;
		}
		
		if (c.visible())
		{
			c.hide();
		}
		else
		{
			c.show();
	
			if (f.getValue() == "") return;
			
			var s = null;
			try	{
				s = Bw.Date.Helpers.read (f.getValue());
			}
			catch (e) { }
			
			if (s && s != null && !isNaN (s.valueOf()))
			{
				c.setValue (s);
			}
		}
		
		this.field.field.focus();
	},

	calendarAction: function ()
	{
		this.field.setValue (Bw.Date.Helpers.write (this.calendar.getValue()));
		this.toggleCalendar();
		this.changed();
	},
	
	getValue: function ()
	{
		return Bw.Date.Helpers.read (this.field.getValue());
	},

	setValue: function (val)
	{
		if (!val) val = "";
		 
		if (typeof val == "string")
		{
			if (val.length > 0)
			{
				val = Bw.Date.Helpers.read (val);
			}
		}
		
		this.field.setValue ( (val == "" ) ? "" : Bw.Date.Helpers.write (val));
	},

	disable: function()
	{
		this.disabled = true;
		this.field.disable();
		if (this.calendar) this.calendar.hide();
	},
	
	enable: function ()
	{
	    this.disabled = false;
		this.field.enable();
	}
};


