/*
	Validation Library
	mpalmer 2.7.2007
	
	Note: Currently this just does required field validation for textboxes and FCKEditor boxes
	
	Instructions:
	
	Include this library and prototype in the page
	<script src="/js/prototype.js" type="text/javascript"></script>
    <script src="/js/global.js" type="text/javascript"></script>
    
    Create a Validation object and pass the form id (probably best to do this at the end of the doc)
	<script language="javascript" type="text/javascript">
		new Validation('theForm');
	</script>
	
	Add CSS class "required" to all inputs you wish to make required fields. These inputs must also have the
	name attribute specified.
	
	Include an errors div for validation messages to display in
	<div id="errors"></div>
*/
var Validation = Class.create();

Validation.prototype._Form;
Validation.prototype._RequiredFields;
Validation.prototype._FCKRequired;

Validation.prototype = {
	initialize: function(formName) {
		this._Form = $(formName);
		this._RequiredFields = document.getElementsByClassName('required');
		this._FCKRequired = document.getElementsByClassName('fckrequired');
		Event.observe(this._Form,'submit',this.Validate.bind(this),false);
	},
	
	Validate: function(event) {
		var isValid = true;
		$('errors').innerHTML = '';
		for (var i=0; i<this._RequiredFields.length; i++)
		{
			var name = this._RequiredFields[i].getAttribute('name');
			if (!this.Require(name))
				isValid = false;
		}
		
		for (var i=0; i<this._FCKRequired.length; i++)
		{
			var name = this._FCKRequired[i].getAttribute('name');
			if (!this.RequireFCK(name))
				isValid = false;
		}
		
		if (!isValid)
			Event.stop(event);
	},
	
	Require: function(fieldName) {
		if ($(fieldName).value != '')
	   	{
	   		return true;
	   	}
	   	else
	   	{
			$('errors').innerHTML += '<li>' + fieldName + ' is a required field.</li>';
	   		return false;
	   	}
	},
	
	RequireFCK: function(fck) {
		var oEditor = FCKeditorAPI.GetInstance(fck);
		if (oEditor.GetXHTML() != '')
			return true;
		else
		{
			$('errors').innerHTML += '<li>' + fck + ' is a required field.</li>';
	   		return false;
		}
	}
};