DDI.Web.TextBox = 
	function(el)
	{	
		if (arguments.length > 0)
		{
			this.init(el);
		}
	}

DDI.Web.TextBox.prototype.init = 
	function(el)
	{
		if (typeof el == 'string') 
		{
			this.el = YAHOO.util.Dom.get(el);
		}
		else
		{
			this.el = el;
		}
		YAHOO.util.Event.addListener(this.el, 'focus', DDI.Web.TextBox.OnFocus);
		YAHOO.util.Event.addListener(this.el, 'blur', DDI.Web.TextBox.OnBlur);
		DDI.Web.TextBox.Watermark(this.el);
	}

DDI.Web.TextBox.prototype.toString = 
	function()
	{
		return this.el.value;
	}

DDI.Web.TextBox.ClearSelection = 
	function(el)
	{
		if (typeof el == 'string') 
		{
			this.el = YAHOO.util.Dom.get(el);
		}
		else
		{
			this.el = el;
		}
		DDI.Web.Selection.clear(this.el);
	}

DDI.Web.TextBox.OnBlur = 
	function(e)
	{
		var el = YAHOO.util.Event.getTarget(e);
				
		if (el.value == '') 
		{
			// check for watermark if nothing entered
			DDI.Web.TextBox.Watermark(el)
			return true;
		}
		else
		{
			// get field's attributes
			
			var lowerCase = DDI.Web.GetAttribute('boolean', el, 'lowercase', false);
			var upperCase = DDI.Web.GetAttribute('boolean', el, 'uppercase', false);
			var spaceLess = DDI.Web.GetAttribute('boolean', el, 'spaceless', false);

			// format data entered as necessary
			
			var finalValue = el.value;
			
			if (lowerCase) finalValue =finalValue.toLowerCase();	
			if (upperCase) finalValue =finalValue.toUpperCase();	
			if (spaceLess)
			{
				var re = /\s/g;
				var finalValue = finalValue.replace(re, '');
			}
			
			el.value = finalValue;
			return true;
		}
		
	}

DDI.Web.TextBox.OnFocus = 
	function(e)
	{
		// remove watermark text, if present
		var el = YAHOO.util.Event.getTarget(e);
		var watermark = DDI.Web.GetAttribute('string', el, 'watermark', '');
		if (watermark.length > 0 && el.value == watermark) el.value = '';
		if (YAHOO.util.Dom.hasClass(el, 'watermark')) YAHOO.util.Dom.removeClass(el, 'watermark');

		// automatically highlight text when the field gains focus
		YAHOO.util.Event.getTarget(e).select();
	}

DDI.Web.TextBox.Watermark =
	function(el)
	{
		var watermark = DDI.Web.GetAttribute('string', el, 'watermark', '');
		if (watermark.length > 0 && el.value == '')
		{
			el.value = watermark;
			YAHOO.util.Dom.addClass(el, 'watermark');
		}
	}

DDI_Web_TextBox_ValidateMinLength =
	function(oSrc, args)
	{
		args.IsValid = true;
		var el = YAHOO.util.Dom.get(oSrc.id.replace(_LEN, ''));
		var minLength = DDI.Web.GetAttribute('int', el, 'minlength', 0);
		if (minLength > 0 && el.value.length > 0 && el.value.length < minLength) args.IsValid = false;
	}

