
if (!Gx.Form)
	Gx.Form = {};

Gx.Form.Validator = new Class({
	Implements: [Options, Events],
	initialize: function(form, options) {
		this.setOptions(options);
		this.already = [];
		this.set(form);
	},
	/*
	 * Set
	 */
	set: function(form) {
		if (!form)
			return this;
		
		//
		this.form = $(form);
		this.elements = this.form.getElements('[name], .submit');
	
		// 
		if (!this.already.contains(form)) {
			//
			this.already.push(form);
			
			// Add Event "enter" to widgets
			this.elements.each(function(element) {
				switch(true) {
					// Button submit
					//case element.hasClass('submit'):
					//	element.addEvent('click', this.keydown.bindWithEvent(this));
					//	break;
					
					// Entry
					case element.hasClass('entry'):
						element.addEvent('enter', this.keydown.bindWithEvent(this));
						break;
				}
			}, this);
		}
	
		return this;
	},
	keydown: function(event) {
		this.submit();
	},
	/*
	 * Reset
	 */
	reset: function() {
		this.elements.each(function(element) {
			if (element.setValue)
				element.setValue();
		});
	},
	/*
	 * Before calling this function it is important to initialize the widgets
	 */
	submit: function(action, method) {
		action = action || this.form.getProperty('action');
		method = method || this.form.getProperty('method');
	
		var data = {};
		this.elements.each(function(element) {
			if ($type(element.getValue) == 'function')
				data[element.get('name')] = element.getValue();
		});

		new Gx.Request(action + ':' + method, {
			// Error message
			onError: (function(code, message, method) {
				this.fireEvent('onError', [code, message, method]);
			}).bind(this),
			
			// Valide message
			onComplete: (function(status, result, text) {
				this.fireEvent('onValidate', [status, result, text]);
			}).bind(this)
		}).send(data);
	}
});

/*
var Validator = new Class({
	Implements: Events,
	initialize: function(form, options) {
		this.form = form || null;
	},
	to: function(form) {
		 // BUG: If IE warning use [name!=""] and html comment <!-- -->
		
		this.form = form || this.form;
		this.elements = this.form.getElements('[name]');
		this.data = {};

		this.elements.each(function(element) {
			if ((name = element.getProperty('name')) == '')
				return;
			
			var prop = (element.get('type')) ? element.get('type') || element.type : element.get('class');
			switch(prop) {
				default:
					this.data[name] = element.value;
					break;
			}
		}, this);
		return this;
	},
	validate: function(method) {
		if (!method || this.run)
			return;
			
		new Request.Custom(this.form.getProperty('action') + ':' + method, {
			// Error message
			onError: (function(code, message, method) {
				// Sigle error display with toolip
				this.elements.each(function(element) {
					if (this.run || element.getProperty('name') != message[0].name)
						return;

					this.error(element, message[0].message);
				}, this);
				
				this.run = false;
			}).bind(this),
			
			// Valide message
			onComplete: (function(response) {
				this.fireEvent('onValidate', response);
			}).bind(this)
		}).send(this.data);
	},
	error: function(element, message) {
		this.run = true;
		// BUG: if possible detect has focus before focus()
		if (element.focus)
			element.focus();
			
		if (!element.tooltip)
			element.tooltip = new Tips.Base(element);
		
		element.tooltip.setValue(message).setShow();
	}
});
*/