(function ($) {
	$.fn.inputFilter = function () {
		return this.each(function () {
			var input = $(this);
			var md = input.metadata();
			
			function errorMessage(errmsg)
			{
				input.bt(errmsg, $.fn.bt.defaultErrorTipOptions).btOn();
			}
			
			if (typeof md.allowChar != 'undefined' ||
				typeof md.denyChar != 'undefined')
			{
				input.keypress(function (e) {
					var errmsg = typeof md.checkCharErrMsg != 'undefined' 
							? md.checkCharErrMsg : 'Character "' + String.fromCharCode(e.which) + '" is not allowed.';
					if (e.ctrlKey || e.altKey || e.metaKey) 
					{	//Ignore
						return true;
					} 
					else if ((e.which >= 32 && e.which <= 125) || e.which > 186)
					{	//typeable characters
						if (typeof md.allowChar != 'undefined' &&
							RegExp('[^' + md.allowChar + ']').test(String.fromCharCode(e.which)))
						{
							errorMessage(errmsg);
							return false;
						}
						if (typeof md.denyChar != 'undefined' &&
							RegExp('[' + md.denyChar + ']').test(String.fromCharCode(e.which)))
						{
							errorMessage(errmsg);
							return false;
						}
						return true;
					}
				});
			}
			
			if (typeof md.checkValue != 'undefined')
			{
				var errmsg = typeof md.checkValueErrMsg != 'undefined' 
					? md.checkValueErrMsg : 'Input has an invalid value.';
				input.change(function (e) {
					var v = $.trim(input.val());
					if (!RegExp(md.checkValue).test(v))
						errorMessage(errmsg);
				});
			}
		});
	}
	
	$(document).ready(function () {
		$('input:text.filter,textarea.filter').inputFilter();
	});
})(jQuery);
