/**************************************************
 * Plugin do jQuery  
 */

if(jQuery) {
	jQuery.fn.extend({
		/**
		 * 
		 */
		formValidation: function(json) {
			var id 	  = $(this)._formValidationId();
			
			if(id) {
				Validation.initialize(id);
				Validation.fromJson(id, json);				
			}
			return this;		
		},
		
		addValidator: function(field, type, message) {
			var id 	  = $(this)._formValidationId();
			
			if(id) {
				Validation.add(id, field, type, message);
			}
			
			return this;	
		},
		
		/**
		 * 
		 */
		isValid: function() {
			var id 	  = $(this)._formValidationId();
			
			return Validation.checkAll(id);			
		},
		
		/**
		 * 
		 */
		_formValidationId: function() {
			var $this = $(this);
			 
			return $this.is('form')? $this.attr('id') : '';		
		}
	});	
}


/**************************************************
 * Standardowa biblioteka
 */
/**
 *
 */
var Validation = {
		
	/**
	 *
	 */	
   	initialize: function(form) {
		if(! this.validators) {
			this.validators   = {};
		}	
		
		this.validators[form] = [];
   	},

	/**
	 *
	 */
	add: function(form, field, type, message) {
		this.validators[form].push(new Validator(field, type, message));			
		return true;
	},
	
	/**
	 * 
	 */
	fromJson: function(form, jsonString) {
		$.each(jsonString, function(field, v) {
			//one field can have many validators
			$.each(v, function(i, data){
				Validation.add(form, field, data.name, data.message);
			});
		});
	},
	
	/**
	 *
	 */
	check: function(form, type, field) {
		var ret = true;
		try {
			ret = eval('Checkers.' + type + '("' + $('[@name=' + field +']', $('#' + form)).val() + '")');
		} catch(e) {
			$.logger(e,4);			
		}

		return ret;
	},
	
	/** 
	 * 
	 */
	checkAll: function(form) {
		var validator 	= null;
		var result 		= new ValidationResult();
		
		if(form && this.validators[form]) {
			for(var k in this.validators[form]) {
				validator = this.validators[form][k];	
				if(! this.check(form, validator.type, validator.field)) {
					result.addError(validator.field, validator.message);
				}			
			}
		}
				
		return result;
	}
};	

function ValidationResult() { 
	this.messages 	= null;
	this.firstField	= null;	
}

/**
 *
 */
ValidationResult.prototype= {	
	/** 
	 * 
	 */
	addError: function(field, message) {
		if(this.messages == null) {
			this.messages = {};
			this.firstField = field;
		}
		if(! this.messages[field]) {
			this.messages[field] = [];
		}
		
		this.messages[field].push(message);
	},
	
	/** 
	 * 
	 */
	isValid: function() {
		return (this.messages == null);
	},
	
	/** 
	 * 
	 */
	getError: function(field) {
		var msg = null;
		if(! this.isValid()) {
			if(field) {
				msg = (this.messages[field])? this.messages[field] : new Array();
			} else {
				msg = this.messages[this.firstField][0];
			}
		}		
		return msg;
	},
	
	/** 
	 * 
	 */
	getErrors: function() {
		return (this.messages != null)? this.messages : new Object();
	},  
	
	getFirstError: function() {
		return this.getError(null);
	}	
};



/**
 *
 */
function Validator(field, type, message) {
		this.field		= field
		this.type 		= type;
		this.message 	= message;
}
/**
 *
 */
var Checkers = {

	/**
	 *
	 */
	required: function(value) {
		return (value != undefined) && (value != '');					
	},

	/**
	 *
	 */
	email: function(value) {
		var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return Checkers._regex(value, regex);
	},
	
	/**
	 *
	 */
	date: function(value) {
		var regex = /^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}$/;
		return Checkers._regex(value, regex);
	},
	/**
	 *
	 */
	notzero: function(value) {
		if (! Checkers.required(value)) {
			return true;
		}
		return (value != 0);
	},
	
	length: function(value) {
		return true;				
	},
	
	match: function(value) {
		return true;
	},
	
	/**
	 * 
	 */
	_regex: function(value, regex) {
		if (! Checkers.required(value)) {
			return true;
		}
		return regex.test(value);		
	}
}
