/**
 * validate.js
 * JavaScript validation routine for the edit airline page.
 *
 * Modified version of Validate 2.1 by Samuel Birch, licensed
 * under the Open Source MIT License.
 *
 * @package		Airline_Payscales_JS
 * @version	 	$Id$
 * @copyright 	Copyright (c) 2008-2009, Will Fly For Food DBA Jet Web Designs Inc.
 * @author 		Webster Network <contact@webster-network.com>
 * @link 		http://www.webster-network.com/
 * @filesource
 */

var Validate = new Class(
{
	getOptions: function()
	{
		return {
			validateOnBlur:	  true,
			errorClass:		  'error',
			errorMsgClass:	  'errorMessage',
			onFail:			  Class.empty,
			onSuccess:		  false,
			showErrorsInline: true,
			label:			  'Please wait ...'
		};
	},
	
	initialize: function(form, options)
	{
		this.setOptions(this.getOptions(), options);
		
		this.form	  = $(form);
		this.elements = this.form.getElements('.required');
		this.list	  = [];
		
		this.elements.each(function(el, i)
		{
			if (this.options.validateOnBlur)
			{
				el.addEvent( 'blur', this.validate.bind(this, el) );
			}
		}.bind(this));
		
		this.form.addEvent('submit', function(e)
		{
			var event	 = new Event(e);
			var doSubmit = true;

			this.elements.each(function(el, i)
			{
				if ( !this.validate(el) )
				{
					event.stop();

					doSubmit = false

					this.list.include(el);
				}
				else
				{
					this.list.remove(el);
				}
			}.bind(this));
			
			if (doSubmit)
			{
				if (this.options.onSuccess)
				{
					event.stop();

					this.options.onSuccess(this.form);
				}
				else
				{
					this.form.getElement('input[type=submit]').setProperty('value', this.options.label);
				}
			}
			else
			{
				this.options.onFail( this.getList() );
			}
			
		}.bind(this));
		
	},
	
	getList: function()
	{
		var list = new Element('ul');

		this.list.each(function(el, i)
		{
			if (el.title != '')
			{
				var li = new Element('li').injectInside(list);

				new Element('label').setProperty('for', el.id).setText(el.title).injectInside(li);
			}
		});
		
		return list;
	},
	
	validate: function(el)
	{
		var valid = true;
		
		this.clearMsg(el);
		
		switch (el.type)
		{
			case 'select-one':
				if (el.value == '')
				{
					valid = false;

					this.setMsg(el, 'Please select a value');
				}
				else
				{
					valid = true;
				}

				break;
			case 'text':
			case 'textarea':
				var regEmail = /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/;
				var regNum   = /[-+]?[0-9]*\.?[0-9]+/;

				if ( (el.value != '') && ( !el.hasClass('number') ) && ( !el.hasClass('num') ) )
				{
					if ( el.hasClass('email') )
					{
						if ( el.value.toUpperCase().match(regEmail) )
						{
							valid = true;
						}
						else
						{
							valid = false;

							this.setMsg(el, 'Enter a valid email address');
						}
					}
				}
				else if ( el.hasClass('number') )
				{
					if ( ( el.value.match(regNum) ) || (el.value == '') )
					{
						valid = true;
					}
					else
					{
						valid = false;

						this.setMsg(el, 'Enter a valid number');
					}
				}
				else if ( el.hasClass('num') )
				{
					if ( ( el.value.match(regNum) ) || (el.value == '') || (el.value == 'COLA+') )
					{
						valid = true;
					}
					else
					{
						valid = false;
						
						this.setMsg(el);
					}
				}
				else
				{
					valid = false;

					this.setMsg(el);
				}
				
				break;
				
		}

		return valid;
	},
	
	setMsg: function(el, msg)
	{
		if (msg == undefined)
		{
			msg = el.title;
		}

		if (this.options.showErrorsInline)
		{
			if (el.error == undefined)
			{
				el.error = new Element('span').addClass(this.options.errorMsgClass).setText(msg).injectAfter(el);
			}
			else
			{
				el.error.setText(msg);
			}

			el.addClass(this.options.errorClass);
		}
	},
	
	clearMsg: function(el)
	{
		el.removeClass(this.options.errorClass);

		if (el.error != undefined)
		{
			el.error.remove();
			
			el.error = undefined;
		}
	}
	
});

Validate.implement(new Options);
Validate.implement(new Events);