var MOOMailValidation = new Class({
	initialize : function(field, container, tableFather, tableSon, log, button,
			controller, action) {
		this.field = $(field);
		// this.trs = $$("." + tr_class);
		this.div = $(container)
		this.button = $(button);
		this.log = $(log);
		this.controller = controller;
		this.action = action;
		this.button_active = true;
		this.tableFather = $(tableFather);
		this.tableSon = $(tableSon);
		// this.field.readonly = false;
		this.field.removeAttribute('readonly');
		/**
		 * Starts: Fx.Slide in the elements so we can easely apply efects to
		 * them like in mootools 1.2
		 */
		/*
		 * $each(this.trs, function(tr) { tr.FxSlide(); });
		 */
		this.tableFather.FxSlide();
		this.div.FxSlide();
		this.button.FxSlide();
		this.log.FxSlide();
		/**
		 * End
		 */
		/*
		 * $each(this.trs, function(tr) { tr.hide(); });
		 */
		this.div.hide();
		this.addHandlers();
	},
	addHandlers : function() {
		if (this.handlers === undefined) {
			this.handlers = {
				click :this.handlerClick.bind(this)
			};
		}
		this.button.addEvent('click', this.handlers.click);
	},
	handlerClick : function(ev) {
		if (this.button_active) {
			var value = this.field.getValue().trim();
			this.field.value = value;
			if (!value.blank() && value.isEmail()) {
				new Ajax(Constants.pathDomain + "/" + this.controller + "/"
						+ this.action, {
					method :'get',
					onSuccess :this.evaluateResponse.bind(this),
					data :{
						email :value
					}
				}).request();
			} else {
				this.log.setText("XXX!.");
				this.log.highlight("#f00", "#E6E6E6");
			}
		}
	},
	evaluateResponse : function(response) {
		var data = Json.evaluate(response);
		if (!data.error) {
			this.tableFather.adopt($$("#" + this.tableSon.id + " tr"));
			this.button_active = false;
			this.button.hide();
			this.log.hide();
			this.field.readOnly = true;

		} else {
			this.log.setText(data.msj);
			this.log.highlight("#f00", "#E6E6E6");
		}

	}

});
