﻿/*                                          
»»»»»»»»»»»»»»»»»»»»»»»»  ««««««««««««««««« About «
»   Form Validation_v1.3.0                        «
»   Bruno Torrinha @ http://www.torrinha.com/     «
»   2011 Jun 23                                   «
»                                                 «
»»»»»»»»»»»»»»»»»»»»»»»»  «««««««««««« Change Log «
»   2011 Jun 23                                   «
»   - updated for Mootools 1.3.2;                 «
»                                                 «
»   2010 Mar 26                                   «
»   - add load lang.js for language support;      «
»                                                 «
»   2008 Nov 15                                   «
»   - First version                               «
»                                                 «
»»»»»»»»»»»»»»»»»»»»»»»»  «««««««««««««« Requires «
»   Mootools 1.3                                  «
»                                                 «
»»»»»»»»»»»»»»»»»»»»»»»»  ««««««««««««««« License «
»   DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE   «
»   http://en.wikipedia.org/wiki/WTFPL            «
»                                                 «
»»»»»»»»»»»»»»»»»»»»»»»»  «««««««««««««««««««««««««
*/

var formz = new Class({

	Implements: [Events, Options],

	options: {
		//onFailure: function(){},
		//onSuccess: function(){},
		morphOptions: 	{duration:250, link:'chain'}, 					//	Morph options (speed, transition, etc...)
		style: {
			label: {def:'.lblDef', hov:'.lblHov', err:'.lblErr'},
			input: {def:'.inpDef', hov:'.inpHov', err:'.inpErr'}
		}
	},

	initialize: function(elForm, options) {
		this.setOptions(options);
		this.frm = elForm;
		this.err = false;	//  Has no error
		this.inpEls = [];

		this.frm.getElements('input, textarea').each(function(el){
			if ( el.getPrevious('label') ){
				if (el.hasClass('_req')) this.attach(el);
				el.bErr = false;
				el.inpLabel = el.getPrevious('label').set('morph', this.options.morphOptions);
				el.set('morph', this.options.morphOptions);
				el.addEvents({
					'focus': this._onFocus.bind(this, el),
					'blur': this._onBlur.bind(this, el)
				}, this, el);
				this.inpEls.push(el);
			}
		}, this);

		this.frm.addEvents({
			'reset': this.frmReset.bind(this), 
			'submit': this.frmSubmit.bindWithEvent(this, false)
		});
	},

	attach: function(el){
		el.required = true;
	},

	detach: function(el){
		el.required = false;
	},

	_onFocus: function(el){
		var s =	this.options.style;
		if (! el.bErr) el.morph(s.input.hov).inpLabel.morph(s.label.hov);
	},

	_onBlur: function(el){
		var s =	this.options.style;
		if (! el.bErr) el.morph(s.input.def).inpLabel.morph(s.label.def);
	},

	validate: function(el){
		var x = true;				//	valid or not
		var str = el.value.trim();
		if(str == ''){
			x = false;
		} else {
			if(el.hasClass('email')) {
				(str.contains('@') && str.contains('.') && str.length > 3) ? x=true : x=false;
			} else if(el.hasClass('number')) {
				($type(str.toInt()) == 'number') ? x=true : x=false;
			} else {
				($type(str) == 'string' && str.length > 3) ? x=true : x=false;
			}
		}
		return(x);
	},

	frmReset: function(){
		var s =	this.options.style;
		this.inpEls.each(function(el){
			el.set('value', '').morph(s.input.def).bErr = false;
			el.inpLabel.morph(s.label.def);
		}, this);
	},

	frmSubmit: function(e){
		if (typeOf(e) == 'event') e.stop();

		this.err = false;
		var s = this.options.style;
		this.inpEls.each(function(el){
			if (el.required) {
				if (this.validate(el)) {
					el.morph(s.input.def).bErr = false;
					el.inpLabel.morph(s.label.def);
					if ( !this.err ){ this.err = false; }
				} else {
					el.morph(s.input.err).bErr = true;
					el.inpLabel.morph(s.label.err);
					this.err = true;
				}
			}
		}, this);
		this.fireEvent( (this.err) ? 'failure' : 'success' );
	}
});
