var FormCheck = new Class({
	/*
	formcheck.js v.1.0 for mootools v1.1 - 05 / 2007
	by Floor SA (http://www.floor.ch) - MIT-style license
	last modified by Jerome Vial 07.05.12
	
	class : required, phone, digit, minchar (minimum 2 characteres), email
	works with : input (text, radio, checkbox), textarea, select
	*/
	
	options: {
		form_id: 'mainform',		//the target form id
		error_class: 'error_f',		//highlight error class	
		highlight : 1,				//0 : highlight none, 1 : one, 2 : all
		scroll_to_error : true		//Smooth scroll the page to first error
	},

	initialize: function(options){
		
		
	
		//Initialize some vars
		this.setOptions(options);
		this.errorArray = [];
		this.radioArray = [];
		
		if(!$(this.options.form_id)){
			$$('form').each(function(el){
					if(el.getAttribute('name') == this.options.form_id){
						el.setProperty('id', this.options.form_id);
					}
				}.bind(this));
		}
		
		//Add an event on submit action
		$(this.options.form_id).addEvent('submit', function(e) {
			// Do our validation:
			if (this.options.highlight > 0 && this.errorArray.length != 0) {
				this.errorArray.each(function(el) {
					el.removeClass(this.options.error_class);
				}.bind(this));
			}
			this.errorArray = [];
			this.validate();
			if (this.errorArray.length != 0) {
				switch(this.options.highlight) {
					case 1 :	this.errorArray[0].addClass(this.options.error_class);
								break;
					case 2 :	this.errorArray.each(function (el) {
									el.addClass(this.options.error_class);
								}.bind(this));
								break;
				}
				
				this.focusOn.pass(this.errorArray[0]).delay(500);
				if (this.options.scroll_to_error) new Fx.Scroll(window).scrollTo(0,this.errorArray[0].getCoordinates().top - 30);
				new Event(e).stop();
			}
		}.bind(this));
		//Make an array with radios
		$$("input").each(function(el){
			if (el.type == "radio") { this.radioArray.push(el) }
		}.bind(this));
	},
	
	focusOn: function(error) {
		error.focus();
	},
	validate: function(){
		$$('.required').each(function(el){
			switch(true) {
				case el.getTag() == "input" && el.hasClass('email') : 
					this.validateEmail(el);
					break;
				case el.getTag() == "input" && el.hasClass('phone') : 
					this.validatePhone(el);
					break;
				case el.getTag() == "input" && el.hasClass('minchar') : 
					this.validateMinChar(el);
					break;
				case el.getTag() == "input" && el.hasClass('digit') : 
					this.validateDigit(el);
					break;
				case (el.getTag() == "input" && el.type == "text") : 
					this.validateText(el);
					break;
				case el.getTag() == "input" && !el.type: 
					this.validateText(el);
					break;
				case el.getTag() == "textarea" :
					this.validateText(el);
					break;
				case el.getTag() == "select" :
					this.validateSelect(el);
					break;
				case el.getTag() == "input" && el.type == "radio" : 
					this.validateRadio(el);
					break;
				case el.getTag() == "input" && el.type == "checkbox" : 
					this.validateCheckbox(el);
					break;
			}
		}.bind(this));
	},
	validateMinChar: function(el){
		if(el.value.length < 2){
			this.errorArray.push(el);
		}
	},
	validateDigit: function(el){
		if (el.value.test('^[0-9]+$') == false) {
			this.errorArray.push(el);
		}
	},
	validateEmail: function(el){
		if (el.value.test('^.+@.+\\.[a-z]+$') == false) {
			this.errorArray.push(el);
		}
	},
	validatePhone: function(el){
		if (el.value.test('^([0-9]|[\(\)\+\.\ \-])+$') == false) {
			this.errorArray.push(el);
		}
	},
	validateText: function(el){
		if(el.value.length < 1 || el.value == ''){
			this.errorArray.push(el);
		}
	},
	validateSelect: function(el){
		if(el.value == el.options[0].value) {
			this.errorArray.push(el);
		}
	},
	validateRadio: function(el){
		var validBox = false;
		this.radioArray.each(function(el2){
			if (el2.name == el.name  && el2.checked == true ) { validBox = true; }
		});
		//We check if there is a false for every required
		if (validBox == false){
			this.errorArray.push(el.getParent().getParent().getParent());
		}
	},
	validateCheckbox: function(el){
		if(el.checked == false){
			this.errorArray.push(el);
		}
	}
});

FormCheck.implement(new Options);


// window.addEvent('domready', function() {
//	var formCheck = new FormCheck({form_id : 'mainform', error_class : 'error_f', highlight : 2, scroll_to_error : false});
// });