/** 
* Basic HTML classes
*
* @import Web/UI/Control.js
*/

/** Base for controls that based on HTML tags */
with(Web_UI_HtmlControl = NewClass()) 
{
	inherits(Web_UI_Control);

	// Constructs a new HTML tag object and applies mouse events
	prototype.__construct = function () {
		parent.__construct.call(this);
		this.events.onClick = new Web_UI_Event('onclick', Web_UI_MouseEventArgs);
		this.events.onDblClick = new Web_UI_Event('ondblclick', Web_UI_MouseEventArgs);
		this.events.onMouseDown = new Web_UI_Event('onmousedown', Web_UI_MouseEventArgs);
		this.events.onMouseMove = new Web_UI_Event('onmousemove', Web_UI_MouseEventArgs);
		this.events.onMouseOver = new Web_UI_Event('onmouseover', Web_UI_MouseEventArgs);
		this.events.onMouseOut = new Web_UI_Event('onmouseout', Web_UI_MouseEventArgs);
		this.events.onMouseUp = new Web_UI_Event('onmouseup', Web_UI_MouseEventArgs);
		this.events.onContextMenu = new Web_UI_Event('oncontextmenu', Web_UI_MouseEventArgs);
	}

	// Changes the control so it has error CSS
	prototype.setError = function() {
		if (!this.tag) return;
		if (this.errorClass && !isString(this.tag.__className)) {
			this.tag.__className = this.tag.className;
			if (!isString(this.tag.__className)) this.tag.__className = '';
			this.tag.className += ' ' + this.errorClass;
			this.tag.className = this.tag.className.trim();
		}
		if (this.errorStyle && this.tag.getAttribute && !isString(this.tag.__style)) {
			if (document.all) this.tag.__style = this.tag.style.cssText;
			else this.tag.__style = this.tag.getAttribute('style');
			if (!this.tag.__style) this.tag.__style = '';
			if (document.all) this.tag.style.cssText =this.tag.__style + this.errorStyle;
			else this.tag.setAttribute('style', this.tag.__style + this.errorStyle);
		}
	}

	// Restores the CSS for the control
	prototype.clearError = function() {
		if (!this.tag) return;
		if (isString(this.tag.__className)) this.tag.className = this.tag.__className;
		if (isString(this.tag.__style)) {
			if (document.all) this.tag.style.cssText = this.tag.__style;
			else this.tag.setAttribute('style', this.tag.__style);
		}
		this.tag.__className = null;
		this.tag.__style = null;
	}
}

/** Base for all controls derived from HTML form controls */
with(Web_UI_HtmlFormControl = NewClass()) 
{
	inherits(Web_UI_HtmlControl);

	// Adds	key events to the form fields
	prototype.__construct = function () {
		parent.__construct.call(this);
		this.events.onKeyDown = new Web_UI_Event('onkeydown', Web_UI_KeyEventArgs);
		this.events.onKeyPress = new Web_UI_Event('onkeypress', Web_UI_KeyEventArgs);
		this.events.onKeyUp = new Web_UI_Event('onkeyup', Web_UI_KeyEventArgs);
		this.events.onChange = new Web_UI_Event('onchange').add(this.change.bind(this));
		this.events.onSelect = new Web_UI_Event('onselect');
	}
	
	// Bind the change event
	prototype.load = function (e) {
		if (parent.load) parent.load.call(this, e);
		if (this.tag && this.submitOnChange) this.events.onChange.add(this.submitForm.bind(this));
	}	
	
	// Submit the form from this control
	prototype.submitForm = function (e) {
		if (!this.tag || !this.tag.form) return;
		__doPostBack(this.tag.form.id, this.clientId, null, this.causesValidation);
	}	
	
	// Gets the value of this form control
	prototype.getValue = function(value) {
		if (!this.tag) return null;
		return this.tag.value;
	}	
	
	// Sets the value of this form control
	prototype.setValue = function(value) {
		if (!this.tag) return;
		this.tag.value = value;
		if (this.tag.onchange) this.tag.onchange();
	}
	
	// Gets the value of this form control
	prototype.change = function() {
		if (parent.change) parent.change.call(this);
		this.changed = true;
	}	
}
