/**
* Validation controls
*
* @import Web/UI/HtmlControl.js
*/

/** Required field validator */
with (Web_UI_FormControl_ErrorContainer = NewClass()) 
{
	inherits(Web_UI_HtmlControl);

	// If shown on server, show now
	prototype.load = function()
	{
		if (parent.load) parent.load.call(this);
		if (this.formId) this.form = Page.findControl(this.formId);
		if (this.form) this.form.errorHandlers.add(this);
	}

	// Process the failed validators from
	prototype.process = function()
	{
		if (!this.forId) {
			if (this.form.errors.length == 0) this.hide();
			else this.show();
		} else {
			if (this.form.controlErrors[this.forId] && this.form.controlErrors[this.forId].length > 0) this.show();
			else this.hide();
		}
	}
	
	// If shown on server, show now
	prototype.render = function()
	{
		if (parent.render) parent.render.call(this);
		if (this.serverShow) this.show();
	}
	
	// Show the error; includes optional error message
	prototype.show = function()
	{
		if (!this.tag) return false;
		this.tag.style.display = '';
    	this.tag.style.visibility = 'visible';
    	var control = null;
    	if (this.forId) control = Page.findControl(this.forId);
        if (control) {
        	if (control.events.onChange) control.events.onChange.add(this.targetChange.bind(this));
        	if (control.events.onKeyPress) control.events.onKeyPress.add(this.targetChange.bind(this));
        }    	
    	Page.events.onResize.invoke();
    	return true;
	};

	// Hide the error
	prototype.hide = function()
	{
		if (!this.tag) return false;
		this.tag.style.display = 'none';
    	this.tag.style.visibility = 'hidden';	
    	var control = null;
    	if (this.forId) control = Page.findControl(this.forId);
        if (control) {
        	if (control.events.onChange) control.events.onChange.remove(this.targetChange.bind(this));
        	if (control.events.onKeyPress) control.events.onKeyPress.remove(this.targetChange.bind(this));
        }    	
    	Page.events.onResize.invoke();	
	};
	
	// Hide if the target control has changed
	prototype.targetChange = function() { this.hide(); return true; }
}

/** Required field validator */
with (Web_UI_FormControl_ErrorAlert = NewClass()) 
{
	inherits(Web_UI_Control);

	// If shown on server, show now
	prototype.load = function()
	{
		if (parent.load) parent.load.call(this);
		if (this.formId) this.form = Page.findControl(this.formId);
		if (this.form) this.form.errorHandlers.add(this);
	}

	// If shown on server, show now
	prototype.render = function()
	{
		if (parent.render) parent.render.call(this);
		if (!this.serverShow) return;
		if (this.errorText) alert(this.errorText);
		else if (this.errorMessage) alert(this.errorMessage);
	}

	// Process the failed validators from
	prototype.process = function()
	{
		if (this.form.errors.length == 0) return;
		if (this.errorMessage) alert(this.errorMessage);
		else alert(this.form.errors[0]);
	}
}

/** Required field validator */
with (Web_UI_FormControl_ErrorLabel = NewClass()) 
{
	inherits(Web_UI_FormControl_ErrorContainer);

	// Process the failed validators from
	prototype.process = function()
	{
		if (this.forId) {
			if (this.form.controlErrors[this.forId] && this.form.controlErrors[this.forId].length > 0) {
				this.show(this.form.controlErrors[this.forId][0]);
			} else this.hide();
		} else {
			if (this.form.errors.length > 0) this.show(this.form.errors[0]);
			else this.hide();
		}
	}
	
	// Show the error; includes optional error message
	prototype.show = function(message)
	{
		if (!this.tag) return false;
		if (message) this.tag.innerHTML = message.toHtml();
		if (parent.show) parent.show.call(this);
    	return true;
	};
}
