/*
 * License: comercial
 * Author: Catalin Prescure
 * Credits: mootools, Valerio Proietti, <http://mad4milk.net>
*/

var J_validation = new Class({
	
	// Initialize class
	// Get options vars from parent
	// Create a copy for local & remote arrays (otherwise will be modified by merge)
	// Set valid var
	// Add Events to form fields
	initialize: function(options) {
		
		this.setOptions(options);

		this.options.valid = true;

		this.localValidation		= new J_local_validation({vars: this.options.vars});
		this.remoteValidation	= new J_remote_validation({vars: this.options.vars});
		this.tools					= new J_form_tools();

		this.local	= this.options.vars.validation['local'].copy();
		this.remote	= this.options.vars.validation['remote'].copy();
		this.fields = this.options.vars.validation['local'].merge(this.options.vars.validation['remote']);

		this.setValid();
		this.addEvents();
	},
	
	// First valid is true
	// Check ( local ) every element to see if there is valid
	// Set valid to false if there are any field not valid
	setValid: function() {
		
		this.options.valid = true;

		this.fields.each(function(field){

			if (this.localValidation.check(field, this.options.vars) != "") {
				this.options.valid = false;
			}
		}, this);
		
		if (valid.constructor === Array) {
			// array valid var
			valid[this.options.vars.valid_name] = this.options.valid;
			//console.log(this.options.vars.valid_name + ": " + valid[this.options.vars.valid_name]);
		} else {
			// simple valid var
			valid = this.options.valid;
		}
	},
	
	// Add Events
	// Set Form submit property (valid: false or true)
	// Add Events for each field
	// onClick: reset color to black
	// onBlur: check local or remote
	// onKeypress Enter: idem
	addEvents: function() {

		this.fields.each(function(field){
			
			if ( field ) {
				
				$(field).addEvents({
					
					'click': function(){
						this.tools.lineColor(field, 'black');
						this.tools.error(field).setHTML('');
					}.bind(this),

					'select': function(){
						this.tools.lineColor(field, 'black');
						this.tools.error(field).setHTML('');
					}.bind(this),

					'blur': function(){
						this.checkField(field);
						this.setValid();
						this.clearErrors();
					}.bind(this),
					 
					'keydown': function(event){
						var event = new Event(event);
						if (event.key == 'enter') {
							this.checkField(field);
							this.setValid();
							this.clearErrors();
						}
					}.bind(this)
				});
			}
		}, this);
	},
	

	// If field is "local" type check local (validation.js)
	// If field is "remote" type check with ajax
	// Reset valid var
	// Clear errors (if valid is true)
	checkField: function(field) {
	
		//if(this.local.contains(field)) { make local validation for all vars
		if(this.fields.contains(field)) {
			this.tools.setError(field, this.localValidation.check(field, this.options.vars));
		}

		if(this.remote.contains(field)) {
			this.remoteValidation.check(field);
		}
		this.setValid();
		this.clearErrors();
	},

	// Set Element error text to null if valid is true
	clearErrors: function() {

		// clear all errors if valid true
		if (this.options.valid == true) {
			this.fields.each(function(field){
				this.tools.error(field).setHTML('');
			}, this);
		}
	}

});

J_validation.implement(new Options, new Events);
