var SimpleMOOForm = new Class({

	initialize : function(options) {
		this.options = Object.extend({
			form :null,
			log :null,
			button :null,
			resetFields :null,
			onComplete :null,
			spinner :null
		}, options || {});

		/** Start: AjaxForm attributes * */
		this.form = $(this.options.form);
		this.log = $(this.options.log);
		this.button = $(this.options.button);
		this.resetFields = this.options.resetFields || [];
		this.onComplete = this.options.onComplete || null;
		this.spinner = $(this.options.spinner) || null;
		/** End: AjaxForm attributes * */

		/** Start: fixed Fx for log */
		if (this.log) {
			this.log.FxSlide();
			this.log.hide();
		}
		/** End: fixed Fx for log */
		if (this.button) {
			this.button.disabled = false;
		}
		this.reset();
		if (this.form) {
			this.form.addEvent('submit', this.submitForm.bind(this));
		}
	},
	submitForm : function(e) {
		new Event(e).stop();
		// alert(this.spinner);
	if (this.spinner) {
		this.spinner.addClass('ajax-small-loading');
	}
	if (this.log) {
		this.log.stop();
		this.log.hide();
	}
	var self = this;
	if (this.button) {
		this.button.disabled = true;
	}
	this.form.send({
		onSuccess :self.handleOnComplete.bind(self),
		onFailure :self.onAjaxFailure.bind(self)
	});
	return false;
},
reset : function() {
	this.resetFields.each( function(field) {
		$(field).value = "";
	});
},
handleOnComplete : function(response) {
	if (this.button) {
		this.button.disabled = false;
	}
	var r = Json.evaluate(response);
	if (this.spinner) {
		this.spinner.removeClass('ajax-small-loading');
	}
	if (r.error) {
		this.onAjaxFailure(response);
	} else {
		this.reset();
		if (this.button) {
			this.button.disabled = false;
		}
		if (this.log && r.msj) {
			this.log.empty();
			this.log.setHTML(r.msj);
			this.log.removeClass('error');
			this.log.addClass('succes');
			this.log.toggle();
			this.log.highlight("#0F0", "#E6E6E6");
			try {
				this.hideLog = this.log.toggle.delay(3500, this.log);
			} catch (e) {
			}
		}
		if (this.onComplete) {

			this.onComplete(response);
		}
	}
},
onAjaxFailure : function(response) {
	this.button.disabled = false;
	if (this.spinner) {
		this.spinner.removeClass('ajax-small-loading');
	}
	var r = Json.evaluate(response);
	if (this.log && r.msj) {
		this.log.empty();
		this.log.setHTML(r.msj);
		this.log.removeClass('succes');
		this.log.addClass('error')
		this.log.toggle();
		this.log.highlight("#f00", "#E6E6E6");
		this.log.toggle.delay(3500, this.log);
	}
}
});