// Wrap code in an self-executing anonymous function and 
// pass jQuery into it so we can use the "$" shortcut without 
// causing potential conflicts with already existing functions.
(function($) {
    var validation = function() {
        var rules = {  // Private object
            required : {
               check: function(value) {
                   if(value) {
                       return true;
                   } else {
                       return false;
                   }
               }
            },
            
            max_x_words : {
                check: function(value, x) {
            		var str = jQuery.trim(value);
        			var value_words = str.split(" ");
        			var value_number_of_words = value_words.length;
        			var max_words = x;        	
        		
                    if(value_number_of_words <= max_words) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
        }
        
        var testPattern = function(value, pattern) {   // Private Method

            var regExp = new RegExp("^"+pattern+"$","");
            return regExp.test(value);
        }
        
        return { // Public methods
            
            addRule : function(name, rule) {

                rules[name] = rule;
            },
            getRule : function(name) {

                return rules[name];
            }
        }
    }
    //A new instance of our object in the jQuery namespace.
    $.validation = new validation();
})(jQuery); 
// Again, we're passing jQuery into the function 
// so we can use $ without potential conflicts. 
