var EditBoxField = Class.create(Component, {

	initialize: function($super, name, owner, editMode, type, currencySymbol, showEmpty){
		$super(name, owner, editMode);

		this.type = type;
		this.currencySymbol = currencySymbol.unescapeHTML();
		this.showEmpty = showEmpty;

		if (owner != null)
		{
			this.input = this.owner.name + ":" + this.name;
			this.text = this.owner.name + ":" + this.name + ":Text";
			this.container = this.owner.name + this.name;
		}
	},

	toggleDisplay: function(input, text, editMode){

		switch (editMode)
		{
			case true:
				text.hide();
				input.show();
				break;
			case false:
				input.hide();
				text.show();
				break;
		}

		return true;
	},

	clear: function($super)
	{
		$(this.input).setValue("");
		return $super();
	},

	trim: function(str){
		str = str.replace(/^\s+/, '');
		for (var i = str.length - 1; i >= 0; i--) {
			if (/\S/.test(str.charAt(i))) {
				str = str.substring(0, i + 1);
				break;
			}
		}

		return str;
	},

	setValueFromText: function()
	{
		var value = escape($(this.text).innerHTML);

		if ($(this.input).type == "checkbox")
			$(this.input).setValue(value == "No" ? null : "on");
		else
		{
			value = unescape(value.replace("%A0", "&nbsp;"));

			if (this.type == "currency")
				value = value.replace(this.currencySymbol, "");

			$(this.input).setValue(value != "&nbsp;" ? value : "");
		}
	},

	setTextFromValue: function()
	{
		if ($(this.input).type == "checkbox")
			$(this.text).innerHTML = ($(this.input).getValue() == null ? "No" : "Yes");
		else 
		{
			var value = this.trim($(this.input).getValue());
			value = (value == "" ? "&nbsp;" : (value == "null" ? null : value));

			if ((this.type == "currency") && (value != null) & (value != "&nbsp;"))
			{
				values = value.split(".");

				if (values[0] == "")
					values[0] = "0";
				else if (values.length == 1)
					values.push("00");

				if (values[1].length == 1)
					values[1] = values[1] + "0";
				else if (values[1].length > 2)
					values[1] = values[1].substring(0, 2);

				value = this.currencySymbol + values.join(".");
			}

			$(this.text).innerHTML = value;
		}
	},

	setEditMode: function($super, editMode, overwrite){
		try
		{
			$super(editMode);
			if ($(this.container))
			{
				this.toggleDisplay($(this.input), $(this.text), this.editMode);

				if (overwrite)
				{
					if (editMode == false)
						this.setTextFromValue();
					else this.setValueFromText();

					this.sendEvent("update", $(this.text).innerHTML);
				}

				if (editMode || this.showEmpty || (($(this.text).innerHTML != "&nbsp;") && (escape($(this.text).innerHTML) != "%A0") && ($(this.text).innerHTML != "")))
				{
					if (!$(this.container).visible());
						$(this.container).show();
				}
				else 
				{
					$(this.container).hide();
				}
			}
		}
		catch(e)
		{
			throw e;
		}

		return true;
	},

	getVisible: function(){
		if ($(this.container))
			return ($(this.container).visible());
		else return false;
	},

	setFocus: function(){
		if (this.editMode && $(this.input))
			$(this.input).focus();
	}
});


