/**
 * filterKeys - jQuery plugin by Gary Manfredi
 * Copyright (c) 2008 Firehose
 * Take given text fields inputs and filter the keys to allow alphanumeric characters and characters in the given allows string.
 * Everything else is not allowed to pass.
 * @example $('select.email').filterKeys("_@-");
 * @cat plugin
 * @type jQuery 
 *
 */
(function($){
	$.fn.filterKeys = function(allows){ 
		allows = allows || "";
		return this.each(function(){
		    $(this).keypress(function(e){
				// allow standard alphanumeric characters + other hidden whitespace characters like backspace, arrows, tab, etc.
				var c = (e.which || e.keyCode);
				if((c <= 31) || (c >= 48 && c <= 57) || (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || (c == 127)) return; 
				
				// if part of context menu, then allow
				$(this).bind('contextmenu', function () { return false });
				
				// now prevent everything else except those explicit given
				if (allows.indexOf(String.fromCharCode(c)) == -1) e.preventDefault();
			});
		});
	};
})(jQuery);
