var catchSubmit = Class.create();


catchSubmit.prototype = {

	reqEls:		[],
	errors:		[],
	respond:	{},
		
		
	initialize: function(options) {

		// itt be lehet vezetni egyeni validalasokat
		//
		//this.options 	= options || {};

		if (document.forms) $A(document.forms).each(function(form) {

			// LABEL.req = @reqEls array
			//
			$(form).getElementsBySelector('label').each(function(label) {
				if (label.hasClassName('req')) this.reqEls.push(label);
			}.bind(this));

			// FROM.submit event
			//
			Event.observe(form, "submit", function(event) {	this.validate(form, event);	}.bind(this) )
			
		}.bind(this));

	},


	validate: function(target, event) {

		this.respond 	= $(target).serialize(true);
		this.errors		= [];

		
		this.reqEls.each(function(i) {

			i.removeClassName('error');
			field = $(i.htmlFor);
			
			if(!this.respond[field.name] || this.respond[field.name] == 0) {
				this.errors.push(i);
			}

		}.bind(this));

		
		if (this.errors.length) {
			this.setError(this.errors);
			Event.stop(event);
		}
		
	},
	

	setError: function(target) {
		
		target.each(function(t) {

			$(t).addClassName('error');
			
		}.bind(this));
	} 

	
}



