
FormValidator = new Class({
	
	initialize: function(form)
	{
		this.form = form;
		this.validatables = Array();
		this.getRules();
	},
	
	getRules: function()
	{	
		this.rules = window.formValidations;
		this.unvalidatedClass = this.rules.unvalidatedClass || '';
	},

	hasRule: function(element)
	{
		return(typeof(this.rules.fields[element]) != 'undefined');
	},

	getRule: function(element)
	{
		return(this.rules.fields[element]);
	},

	gatherElements:function()
	{
		return($(this.form).getElements('input'));
	},

	validate: function(validatables)
	{
		this.getRules(); 
		var returnValue = true;
		var validated = true;

		if(!validatables) { validatables = this.gatherElements(); }

		for(i=0; i<validatables.length; i++)
		{ 
			currentId = validatables[i].id;
			if(this.hasRule(currentId))
			{
				var val = new validateObject(this.getRule(currentId), validatables[i], this).validate();
				if(!val) validated = false;
			}			
		}
		return validated;
	}
});

validateObject = new Class({
	initialize: function(rule, element, validator)
	{
		this.rule = rule;
		this.element = $(element);
		this.mandatory = this.rule.mandatory || false;
		this.validator = validator;
		this.unvalidatedClass = (this.rule.mandatory) ? this.validator.unvalidatedClass : (this.rule.unvalidatedClass || this.validator.unvalidatedClass);
	},

	standardValidations: { // available regex validations.
				
				'date' : "^[0-9]{2}/[0-9]{2}/[0-9]{4}$",
				'email' : "^[0-9a-zA-Z._-]*[@][0-9a-zA-Z._-]*[.][a-z]{2,4}$", 
				'amount' : "^[-]?[0-9]+$",
				'number' : "^[-]?[0-9,]+$",
				'alfanum' : "^[0-9a-zA-Z ,.-_\\s\?\!]+$",
				'anything' : "[a-z0-9A-Z ]+",
				'words' : "^[A-Za-z]+[A-Za-z \\s]*$",
				'phone' : "^[\+]{0,1}[0-9\-]{10,}$",
				'zipcode' : "^[1-9][0-9]{3}[ ]?[a-zA-Z]{2}$",
				'plate' : "^([0-9a-zA-Z]{2}[-]){2}[0-9a-zA-Z]{2}$",
				'price' : "^[0-9.,]*(([.,][-])|([.,][0-9]{2}))?$",
				'2digitopt' : "^\d+(\,\d{2})?$",
				'2digitforce' : "^\d+\,\d\d$",
				'groeinetnumber' : "^[0-9\-]{8,}$"
	},

	validate:function()
	{
		if(this.mandatory && this.element.value.length == 0) return this.incorrect();
		if(this.element.value.length > 0 && this.standardValidations[this.rule.validation])	{
			return new RegExp(this.standardValidations[this.rule.validation]).test(this.element.value) ? this.correct() : this.incorrect();
		}
		else if(this.element.value.length > 0 && !this.standardValidations[this.rule.validation])
		{
			func = ("validate-"+this.rule.validation).camelCase();
			if(func in this) {	
				return this[func]() ? this.correct() : this.incorrect();  
			}
		}
		return this.correct();
	},
	
	incorrect: function()
	{
		el = (this.element.type && this.element.type =='checkbox') ? this.element.getParent() : this.element;
		if(!el.hasClass(this.unvalidatedClass)) {
			el.addClass(this.unvalidatedClass);	
		}
		return false;
	},

	correct: function()
	{
		el = (this.element.type && this.element.type =='checkbox') ? this.element.getParent() : this.element;
		if(el.hasClass(this.unvalidatedClass)) {
			el.removeClass(this.unvalidatedClass);
		}
		return true;
	},

	validateChecked: function(value)
	{
		return(this.element.checked);
	},

	validateBank:function(value)
	{
		value = this.element.value.split('.').join('').split(' ').join('').split('');
		var postbank  = "^[pP]{1}([0-9]{3,}$)";
		var bank = "^([0-9]{9}$)";
		var total = 0;
		if (new RegExp(bank).test(value.join(''))) {
			for (var i=1; i<10; i++) { total += value[i-1] * (10-i); }	
			if ((total % 11) == 0) return (true);
			else {
				alert('Bankrekeningnummer voldoet niet aan de 11-proef');
				return false;
			}
		}
		else if (new RegExp(postbank).test(value.join('')))	{ return (true); }
		else {
			alert('Niet genoeg cijfers voor een bankrekening! \nAls dit een postbankrekening betreft zet dan een "P" voor het getal, \nals dit een bankrekening nummer betreft zorg dan dat deze correct is.');
			return (false);
		}
	}

});



