/**
 * Validation used with the form builder
 */
( function($){
	/**
	 * Validation function
	 *
	 * @param	url		ajax url for validation checks
	 * @param	file	file to validate
	 */
	$.fn.validate = function( url, file ){
		// Process the fields
		return this.each( function(){
			// Split the rules
			this.rules = $(this).attr('rel');
			this.span = $(this).parent('div').children('span[class=message]');

			// Add Blur
			$(this).blur( function(){

				// Checking the rules
				var data = {
					rules: this.rules,
					value: this.value,
					file: file,
					name: this.name
				};

				// Needed for ajax call
				var input = this;

				$.post( url, data, function( response ){
					// Response
					response = jsonParse(response);

					// Check the stuff
					if( response.success ){
						$(input.span).removeClass('error').html( ' ' ).addClass('success');
					} else {
						$(input.span).removeClass('success').html( response.message ).addClass('error');
					}

				});

			});
		});

	};

})(jQuery);