////////////////////////////////////////////////////////////////////////////////////////////
// Form client-side script
////////////////////////////////////////////////////////////////////////////////////////////

addNamespace("Web");
addNamespace("Web.UI");

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.Validators = new Array();

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.Form = Base.extend(
{
	constructor: function(form)
	{
		if (!form)
		{
			alert("The form was not found on the page!  Aborting...");
			return;
		}
		
		this.form = $(form);
		this.enabled = true;
		this.validate = true;
		this.onsubmit = null;
		
		if (this.form.onsubmit != null)
		{
			var _onsubmit = this.form.onsubmit;
			var _this = this;
			
			this.form.onsubmit = function()
			{
				_this.handleSubmit();
				_onsubmit.bindAsEventListener(_this.control)();
			};
		}
		else
			this.form.onsubmit = this.handleSubmit.bind(this);
	},
	
	handleSubmit: function()
	{
		if (!this.enabled)
			return false;
		
		if (this.validate)
		{
			if (!this.handleValidate())
				return false;
		}
		
		if (this.onsubmit)
			return this.onsubmit();
		
		return true;
	},
	
	handleValidate: function()
	{
		var message = "";
		var failure = false;
		
		for (var i = 0; i < Web.UI.Validators.length; i++)
		{
			var val = Web.UI.Validators[i];
			
			if (val.enabled && !val.validate())
			{
				failure = true;
				message += val.validationMessage + "\n";
			}
		}
		
		if (failure)
		{
			alert(message);
			return false;
		}
		
		return true;
	},
	
	preventPostbackOnEnter: function()
	{
		this.form.onkeydown = this.disablePostbackHandler.bindAsEventListener(this);
	},
	
	disablePostbackHandler: function(e)
	{
		Core.Utility.setEvent(e);
		var keyCode = e.which ? e.which : (e.keyCode ? e.keyCode : (e.charCode ? e.charCode : 0));
		
		if (keyCode == 13) // enter
			return false;
	},
	
	enable: function(enable)
	{
		this.enabled = enable ? true : false;
		this.updateFormElements();
	},
	
	disable: function()
	{
		this.enable(false);
	},
	
	updateFormElements: function()
	{
		fields = $$("body input");
		for(i = 0; i < fields.length; i++)
			fields[i].disabled = !this.enabled;
		
		fields = $$("body select");
		for(i = 0; i < fields.length; i++)
			fields[i].disabled = !this.enabled;
		
		fields = $$("body textarea");
		for(i = 0; i < fields.length; i++)
			fields[i].disabled = !this.enabled;
	}
});

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.ValidationSettings = Base.extend(
{
	constructor: function()
	{
	}
},
{
	focusOnError: true,
	selectOnError: true,
	colorOnError: true,
	errorColor: "InfoText",
	errorBackgroundColor: "InfoBackground",
	
	update: function()
	{
		for (var i = 0; i < Web.UI.Validators.length; i++)
		{
			var val = Web.UI.Validators[i];
			val.focusOnError = Web.UI.ValidationSettings.focusOnError;
			val.selectOnError = Web.UI.ValidationSettings.selectOnError;
			val.colorOnError = Web.UI.ValidationSettings.colorOnError;
			val.errorColor = Web.UI.ValidationSettings.errorColor;
			val.errorBackgroundColor = Web.UI.ValidationSettings.errorBackgroundColor;
		}
	}
});

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.Validator = Base.extend(
{
	constructor: function()
	{
		this.enabled = true;
		this.validationMessage = "";
		
		Web.UI.Validators.push(this);
	},
	
	validate: function()
	{
		return true;
	},
	
	validationError: function(message)
	{
		this.validationMessage = message;
		return false;
	}
});

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.ControlValidator = Web.UI.Validator.extend(
{
	constructor: function(control)
	{
		this.base();
		this.control = $(control);
		this.type = this.control.tagName.toLowerCase();
		this.focusOnError = Web.UI.ValidationSettings.focusOnError;
		this.selectOnError = Web.UI.ValidationSettings.selectOnError;
		this.colorOnError = Web.UI.ValidationSettings.colorOnError;
		this.errorColor = Web.UI.ValidationSettings.errorColor;
		this.errorBackgroundColor = Web.UI.ValidationSettings.errorBackgroundColor;
		this.oldColor = this.control.style.color;
		this.oldBackgroundColor = this.control.style.backgroundColor;
		this.validatingAfterChange = false;
		
		if (this.type == "input" || this.type == "textarea")
		{
			var _onchange = this.control.onchange;
			var _this = this;
			
			this.control.onchange = function()
			{
				_this.validatingAfterChange = true;
				_this.validate();
				_this.validatingAfterChange = false;
				
				if (_onchange != null)
					_onchange.bindAsEventListener(_this.control)();
			};
		}
	},
	
	validationError: function(message)
	{
		if (this.validatingAfterChange)
			return false;
		
		try
		{
			if (this.focusOnError)
				this.control.focus();
			
			if (this.selectOnError)
				this.control.select();
		}
		catch (dummy)
		{ }
		
		if (this.colorOnError)
		{
			this.control.style.color = this.errorColor;
			this.control.style.backgroundColor = this.errorBackgroundColor;
		}
		
		return this.base(message);
	}
});

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.RequiredFieldValidator = Web.UI.ControlValidator.extend(
{
	constructor: function(control)
	{
		this.base(control);
	},
	
	validate: function()
	{
		if (this.type == "input" || this.type == "textarea")
		{
			if (this.control.value != null && this.control.value.length == 0)
				return this.validationError();
		}
		else if (this.type == "select")
		{
			if (this.control.options[this.control.selectedIndex].value != "")
				return this.validationError();
		}
		
		if (this.colorOnError)
		{
			this.control.style.color = this.oldColor;
			this.control.style.backgroundColor = this.oldBackgroundColor;
		}
		
		return true;
	},
	
	validationError: function()
	{
		return this.base(this.control.title ? this.control.title : "A required field is missing.");
	}
});

////////////////////////////////////////////////////////////////////////////////////////////

Web.UI.RequiredFieldLengthValidator = Web.UI.ControlValidator.extend(
{
	constructor: function(control, min, max)
	{
		this.base(control);
		this.minimumLength = 0;
		this.maximumLength = 999999999;
		
		if (min != undefined) this.minimumLength = min;
		if (max != undefined) this.maximumLength = max;
	},
	
	validate: function()
	{
		if (this.type == "input" || this.type == "textarea")
		{
			if (this.control.value != null && 
				(this.control.value.length < this.minimumLength || this.control.value.length > this.maximumLength))
				return this.validationError();
		}
		
		if (this.colorOnError)
		{
			this.control.style.color = this.oldColor;
			this.control.style.backgroundColor = this.oldBackgroundColor;
		}
		
		return true;
	},
	
	validationError: function()
	{
		return this.base(this.control.title ? this.control.title : "A required field is not the correct length.");
	}
});

window.Web_UI_Form_Loaded = true;



