var fValidator = new Class({
	options: {
		msgContainerTag: "div",
		msgClass: "fValidator-msg",

		styleNeutral: {"background-color": "#fff", "border": "1px outset #000", "margin-bottom": "5px"},
		styleInvalid: {"background-color": "#fff", "border": "1px outset #000", "margin-bottom": "5px"},
		styleValid: {"background-color": "#fff", "border": "1px outset #000", "margin-bottom": "5px"},
		class050: {"width": 50},
		class100: {"width": 100},
		class150: {"width": 150},
		class200: {"width": 200},
		class250: {"width": 250},
		class300: {"width": 300},
		class400: {"width": 400},

		required: {type: "required", re: /[^.*]/, msg: "Campo obrigatório."},
		alpha: {type: "alpha", re: /^[a-z ._-]+$/i, msg: "Este campo só aceita letras."},
		alphanum: {type: "alphanum", re: /^[a-z0-9 ._-]+$/i, msg: "Este campo só aceita caracteres alfanumericos."},
		senha: {type: "senha", re: /^[a-z0-9]{5,10}$/, msg: "Entre 5 a 10 caracteres."},
		integer: {type: "integer", re: /^[-+]?\d+$/, msg: "Apenas números inteiros."},
		real: {type: "real", re: /^[-+]?\d*\.?\d+$/, msg: "Apenas números."},
		date: {type: "date", re: /^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$/, msg: "Digite uma data válida (mm/dd/yyyy)."},
		email: {type: "email", re: /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i, msg: "Digite um e-mail válido."},
		phone: {type: "phone", re: /^[\d\s ().-]+$/, msg: "Digite um telefone válido."},
		url: {type: "url", re: /^(http|https|ftp)\:\/\/[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i, msg: "Digite uma URL válida."},
		confirm: {type: "confirm", msg: "A confirmação de senha não confere com o campo senha."},
		cep: {type: "cep", re: /^[0-9]{5}-[0-9]{3}$/, msg : "CEP inválido"},
		onValid: $empty,
		onInvalid: $empty
	},

	initialize: function(form, options) {
		this.form = $(form);
		this.setOptions(options);

		this.fields = this.form.getElements("*[class^=fValidate]");
		this.validations = [];

		this.fields.each(function(element) {
			if(!this._isChildType(element)) element.setStyles(this.options.styleNeutral);
			element.cbErr = 0;
			var classes = element.getProperty("class").split(' ');
			classes.each(function(klass) {
				if(klass.match(/^fValidate(\[.+\])$/)) {
					var aFilters = eval(klass.match(/^fValidate(\[.+\])$/)[1]);
					for(var i = 0; i < aFilters.length; i++) {
						if(this.options[aFilters[i]]) this.register(element, this.options[aFilters[i]]);
						if(aFilters[i].charAt(0) == '=') this.register(element, $extend(this.options.confirm, {idField: aFilters[i].substr(1)}));
						if(aFilters[i].match(/^class[0-9]{3}$/)) element.setStyles(this.options[aFilters[i]]);
					}
				}
			}.bind(this));
		}.bind(this));

		this.form.addEvents({
			"reset": this._onReset.bind(this)
		});
	},

	register: function(field, options) {
		field = $(field);
		
		this.validations.push([field, options]);
		field.addEvent("blur", function() {
			this._validate(field, options);
		}.bind(this));
	},

	_isChildType: function(el) {
		var elType = el.type.toLowerCase();
		if((elType == "radio") || (elType == "checkbox")) return true;
		return false;
	},

	_validate: function(field, options) {
		switch(options.type) {
			case "confirm":
				if($(options.idField).get('value') == field.get('value')) this._msgRemove(field, options);
				else this._msgInject(field, options);
				break;
			default:
				if(options.re) {
					if(options.re.test(field.get('value'))) this._msgRemove(field, options);
					else this._msgInject(field, options);
				}
		}
	},
	_validateChild: function(child, options) {
		var nlButtonGroup = this.form[child.getProperty("name")];
		var cbCheckeds = 0;
		var isValid = true;
		if (nlButtonGroup.length) {
 		for(var i = 0; i < nlButtonGroup.length; i++) {
			if(nlButtonGroup[i].checked) {
				cbCheckeds++;
				if(!options.re.test(nlButtonGroup[i].get('value'))) {
					isValid = false;
					break;
				}
			}
		}
		} else if (this.form[child.getProperty("name")].checked) {
			cbCheckeds = 1;
		}
		if(cbCheckeds == 0 && options.type == "required") isValid = false;
		if(isValid) this._msgRemove(child, options);
		else this._msgInject(child, options);
	},
	_msgInject: function(owner, options) {
		if(!$(owner.getProperty("id") + options.type +"_msg")) {
			if (owner.cbErr <= 0 || options.type != 'required') {
			var msgContainer = new Element(this.options.msgContainerTag, {"id": owner.getProperty("id") + options.type +"_msg", "class": this.options.msgClass})
				.set('html',options.msg)
				.setStyles({
					opacity: 0,
					position: 'absolute',
					top: owner.getCoordinates().top,
					left: owner.getCoordinates().right + 5
				})
				.injectInside($(document.body))
				.fade(1);
			if(options.type != 'required' && $(owner.getProperty("id")+"required_msg")) $(owner.getProperty("id")+"required_msg").dispose();
			owner.cbErr++;
			this._chkStatus(owner, options);
			}
		}
	},

	_msgRemove: function(owner, options, isReset) {
		isReset = isReset || false;
		if($(owner.getProperty("id") + options.type +"_msg")) {
			var el = $(owner.getProperty("id") + options.type +"_msg");
			new Fx.Morph(el, {
				duration: 500,
				transition: Fx.Transitions.linear,
				onComplete: function() {if ($(owner.getProperty("id") + options.type +"_msg")) el.dispose()}
			}).start({ 'opacity': 0});
			if(!isReset) {
				owner.cbErr--;
				this._chkStatus(owner, options);
			}
		}
	},

	_chkStatus: function(field, options) {
		if(field.cbErr == 0) {
			new Fx.Morph(field,{transition: Fx.Transitions.linear}).start(this.options.styleValid);
			this.fireEvent("onValid", [field, options], 50);
		} else {
			new Fx.Morph(field,{transition: Fx.Transitions.linear}).start(this.options.styleInvalid);
			this.fireEvent("onInvalid", [field, options], 50);
		}
	},
	_onReset: function() {
		this.validations.each(function(array) {
			if(!this._isChildType(array[0])) array[0].setStyles(this.options.styleNeutral);
			array[0].cbErr = 0;
			this._msgRemove(array[0], array[1], true);
		}.bind(this));
	}
});
fValidator.implement(new Events); // Implements addEvent(type, fn), fireEvent(type, [args], delay) and removeEvent(type, fn)
fValidator.implement(new Options);// Implements setOptions(defaults, options)