var mooCheck = new Class({
						 
	Implements: [Options],
		
	options:{
		check: "required",
		notifyContainer : "erreur"
	},
	
	initialize: function(form,options){
		this.form = form;
		this.setOptions(options);
		this.checkFiels = $$('.'+this.options.check);
		this.form.getElement('input[type=submit]').disabled = false;
		this.checkFiels.each(function(el,i) {
			this.check(el,i);
		},this);
		
		this.btnSubmit = this.form.getElement('input[type=submit]');
		
		//this.form.getElement('input[type=submit]').disabled = false;
		
		/* create Dotter instance */
		var dotter = new Dotter(this.form.getElement('input[type=submit]'),{
			delay: 200, //character delay
			message: 'Traitement',
			property: 'value'
		});
		
		/* element click */
		this.form.getElement('input[type=submit]').addEvent('click',function(e) {
			var action = function() {
				dotter.stop();
				btnSubmit.set('value','Submit');
			};
			dotter.start();
			action.delay(3000);
		});

		
	},
	
	check: function(el,i) {		
		// check si cet élément à un message d'erreur et si il est vide
		if(el.get('value')==' ') {
			//alert('el :'+el+' - '+el.get('value'));
			this.form.getElement('input[type=submit]').disabled = true;
		}
		
		el.addEvent('blur',function(e) {
			//stop!
			if(e) e.stop();
			if(el.get('value')=='') {
				this.showError(el);
				this.form.getElement('input[type=submit]').disabled = true;
			} else {
				this.showOk(el);	
			}
			
		}.bind(this));
		
	}, // check
	
	showError: function(el) {
		var span = el.getNext('span');
		if(span) {
			span.removeClass('confirm');
			span.addClass('erreur');
			span.set('html',el.get('title'));
			span.setStyles({
				visibility:'visible'
			});
		}
		el.addClass('formError');
		//this.form.getElement('input[type=submit]').disabled = true;
	}, // showError
	
	showOk: function(el) {
		var span = el.getNext('span');
		el.removeClass('formError');
		if(span) {
			span.removeClass('erreur');
			span.addClass('confirm');
			span.setStyles({
				visibility:'hidden'
			});
		}
		el.removeClass('formError');
		this.form.getElement('input[type=submit]').disabled = false;
	} // showOk
	
}) // Fin de la Class



var Dotter = new Class({

	/* implements */
	Implements: [Options,Events],

	/* options */
	options: {
		delay: 1000,
		dot: '.',
		message: 'Loading',
		numDots: 3,
		property: 'text',
		reset: false/*,
		onDot: $empty,
		onStart: $empty,
		onStop: $empty
		*/
	},

	/* initialization */
	initialize: function(container,options) {
		/* set options */
		this.setOptions(options);
		this.container = document.id(container);
		this.dots = 0;
		this.running = false;
	},

	/* adds dot */
	dot: function() {
		if(this.running) {
			var text = this.container.get(this.options.property);
			this.dots++;
			this.container.set(this.options.property,(this.dots % this.options.numDots != 0 ? text : this.options.message) + '' + this.options.dot);
		}
		return this;
	},

	/* loads or resets the dotter */
	load: function() {
		this.loaded = true;
		this.dots = 0;
		this.dotter = function(){ this.dot(); this.fireEvent('dot'); }.bind(this);
		this.periodical = this.dotter.periodical(this.options.delay);
		this.container.set(this.options.property,this.options.message + '' + this.options.dot);
		return this;
	},

	/* start the dotter */
	start: function() {
		if(!this.loaded || this.options.reset) this.load();
		this.running = true;
		this.fireEvent('start');
		return this;
	},

	/* stop the dotter */
	stop: function() {
		this.running = this.loaded = false;
		$clear(this.periodical);
		this.fireEvent('stop');
		return this;
	}
});
