var formCheck = new Class({
				 
	Implements: [Options, Events],

	options: {
	},
	
	element:	null,
	submit:		null,
	
	
	initialize: function(form, submit, options){
		this.setOptions(options);
		this.element = $$(form)[0];
		this.submit = this.element.getElement(submit);
		
		this.addSubmitEvent();
		this.formUrl();
	},
	
	addSubmitEvent: function(){
		this.element.addEvent('submit', function(ev){
			event.stop();
		});
		this.submit.addEvent('click', function(){
			this.checkForm();
		}.bind(this));
	},
	
	getFormData:	function(){
		this.data = {};
		$each(this.element.getElements('input, textarea, select'), function(el){
			if (el.get('name')){
				this.data[el.get('name')] = el.get('type') == 'checkbox' ? el.get('checked') : el.get('value');
			}		
		}, this);
	},
	
	formUrl:	function(){
		this.url = new URI(this.element.get('action'));
		this.url.set('directory', this.url.get('directory') + 'check/');
		
		this.url = this.url.get('directory');
	},
	
	checkForm:	function(){
		this.getFormData();
		this.clearErrors();
		this.element.fade(0.5);
		new Request.JSON({
			'url' 		:		this.url,
			'method' 	:		this.element.get('method'),
			'data'		:		this.data,
			'onComplete':		function(json){
									if (json === true){
										this.fireEvent('complete');	
										this.element.fade(1);
									} else {
										this.errors(json);
									}
								}.bind(this)
		}).send();
	},
	
	clearErrors:	function(){
		$each(this.element.getElements('.error'), function(error){
			error.destroy();
		});
	},
	
	errors:	function(errors){
		$each(errors, function(error, name){
			var input = this.element.getElement('*[name='+name+']');
			
			input.set('tween', {duration: 'long'}).highlight('#009C7D');
			
			this.addError(input, error);
		}, this);
		this.element.fade(1);
	},
	
	addError:	function(input, message){
		var error = new Element('div', {
			'class'	:	'error',
			'text'	:	message
		}).injectBottom(input.getParent('dd'));
	}
});
