//////////////////////////////////////////////////////////////////////////////
//  jaxtextarea
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// 	Jax.Widgets.TextArea constructor.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
//
//	Usage:
//		Jax.Widgets.TextArea( name, parent );
//
//	TODO (chriswa):
//	[1] Default regular expression to look for invalid sequences.
//	[2]	Keypress filtering.
//	[3] Limit.
//////////////////////////////////////////////////////////////////////////////
Jax.Widgets.TextArea = function() {
	var m_keypress = function(){};
    var m_this = this;

	// Establish class and initialize instance.
	Jax.oop.declare(Jax.Widgets.TextArea, Jax.Widgets.Input);
	this.setInstance();
    
	// Set methods.
	this.setMethod(initialize);
	this.setMethod(isEmpty);    
	this.setMethod(isFull);
	this.setMethod(keypressHandler);
	this.setMethod(validate);
	this.setMethod(getValue);
	this.setMethod(setValue);
	
	// Initialize instance.
	if (arguments.length) {
		this.name = arguments[0];
		this.parent = arguments[1];
		this.items = this.getItems(this.name, this.parent);
		this.initialize(arguments);
	}

	//////////////////////////////////////////////////////////////////////////
	// Class methods definitions
	//////////////////////////////////////////////////////////////////////////

	//////////////////////////////////////////////////////////////////////////
	// Initialize
	//////////////////////////////////////////////////////////////////////////
	function initialize(args) {
    
        // Set up element.
		this.element = this.items.itemAt(0);
		
		if (this.element == null) {
			throw new Exception(this, "Missing TextArea widget");
        }

		// Clear image.
		this.clearError();

        // Set keypress handler.
        this.element.onkeypress = new Delegate(this, onKeyPress);
	}

	//////////////////////////////////////////////////////////////////////////
	//	isEmpty
	//////////////////////////////////////////////////////////////////////////
	function isEmpty() {
		return this.element.value.toString().trim() === "";
	}

	//////////////////////////////////////////////////////////////////////////
	//	isError
	//////////////////////////////////////////////////////////////////////////
	function isError() {
        return this.error;
	}

	//////////////////////////////////////////////////////////////////////////
	//	isFull
	//////////////////////////////////////////////////////////////////////////
	function isFull() {
		return false;
	}

	//////////////////////////////////////////////////////////////////////////
	// onKeyPress()
	//////////////////////////////////////////////////////////////////////////
	function onKeyPress(eventArgs) {
		var evnt = new Jax.KeyEvent(eventArgs);
        m_keypress(m_this);
	}

	//////////////////////////////////////////////////////////////////////////
	// keypressHandler
	//////////////////////////////////////////////////////////////////////////
	function keypressHandler(handler) {
        checkHandler(handler);
        m_keypress = handler;
	}
    
	//////////////////////////////////////////////////////////////////////////
	//	Validate : Validate options.
	//////////////////////////////////////////////////////////////////////////
	function validate() {
		var retval = false;

		if (this.isEmpty()) {
			retval = true;
        }
		else {
			// Validate text field, and test for invalid character combinations.
			if (this.element.value.indexOf("&#") == -1) {
				this.clearError();
				retval = true;
			}
			else {
				this.dispatchError(new Exception(this, Jax.Widgets.TextArea.INVALID_CHARACTER));
			}
		}			

		return retval;
	}

	//////////////////////////////////////////////////////////////////////////
	// Value
	//////////////////////////////////////////////////////////////////////////
	function getValue() {
		return this.element.value.toString().trim();
    }
    
    function setValue() {
		if (arguments.length) {
			this.element.value = arguments[0];
        }
	}
    
	//////////////////////////////////////////////////////////////////////////
	// Helpers.
	//////////////////////////////////////////////////////////////////////////
    function checkHandler(handler) {
		if (!Jax.js.isFunction(handler)) {
			throw new Error("handler is not a function type");
        }
    }
};

//////////////////////////////////////////////////////////////////////////////
//	Jax.Widgets.TextArea enumerators.
//////////////////////////////////////////////////////////////////////////////
Jax.Widgets.TextArea.INVALID_CHARACTER	=  100;