(function($){

	$.fn.alphanumeric = function(p) { 

		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- ",
			nchars: "",
			allow: ""
		  }, p);	

		return this.each
			(
				function() 
				{

					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyz";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');

					$(this).keypress
						(
							function (e)
								{
								
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
									
								}
								
						);
						
					$(this).bind('contextmenu',function () {return false});
									
				}
			);

	};

	$.fn.numeric = function(p) {
	
		var az = "abcdefghijklmnopqrstuvwxyz";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);	
		  	
		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};
	
	$.fn.alpha = function(p) {

		var nm = "1234567890";

		p = $.extend({
			nchars: nm
		  }, p);	

		return this.each (function()
			{
				$(this).alphanumeric(p);
			}
		);
			
	};
	

	
	$.fn.realnumeric = function(decimal_precision) {

		decimal_precision = decimal_precision || 2;
		this.keypress(
			function(e)
			{
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				
				// allow enter/return key (only when in an input box)
				if(key == 13 && this.nodeName.toLowerCase() == "input")
				{
					return true;
				}
				else if(key == 13)
				{
					return false;
				}
				var allow = false;
				// allow Ctrl+A
				if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
				// allow Ctrl+X (cut)
				if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
				// allow Ctrl+C (copy)
				if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
				// allow Ctrl+Z (undo)
				if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
				// allow or deny Ctrl+V (paste), Shift+Ins
				if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
				|| (e.shiftKey && key == 45)) return true;
				// if a number was not pressed
				if(key < 48 || key > 57)
				{
					/* '-' only allowed at start */
					if(key == 45 && this.value.length == 0) return true;
					/* only one decimal separator allowed */
					if((key == ".".charCodeAt(0) || key == ",".charCodeAt(0)) && this.value.indexOf(".") == -1)
					{
						allow = false;
					}
					// check for other keys that have special purposes
					if(
						key != 8 /* backspace */ &&
						key != 9 /* tab */ &&
						key != 13 /* enter */ &&
						key != 35 /* end */ &&
						key != 36 /* home */ &&
						key != 37 /* left */ &&
						key != 39 /* right */ &&
						key != 46 /* del */
					)
					{
						allow = false;
					}
					else
					{
						// for detecting special keys (listed above)
						// IE does not support 'charCode' and ignores them in keypress anyway
						if(typeof e.charCode != "undefined")
						{
							// special keys have 'keyCode' and 'which' the same (e.g. backspace)
							if(e.keyCode == e.which && e.which != 0)
							{
								allow = true;
							}
							// or keyCode != 0 and 'charCode'/'which' = 0
							else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
							{
								allow = true;
							}
						}
					}
					// if key pressed is the decimal and it is not already in the field
					if((key == ".".charCodeAt(0) || key == ",".charCodeAt(0)) && this.value.indexOf(".") == -1)
					{
						allow = true;
					}
				}
				else
				{
					/*
					 * ilosc miejsc po przecinku- decimal_precision
					 */
					if (this.value.indexOf('.') > -1)
					{
						if (this.value.replace(/.*\./, "").length > decimal_precision - 1)
						{
							allow = false;
						}
						else
						{
							allow = true;
						}
					}
					else
					{
						allow = true;
					}
				}
				return allow;
			}
		);
		
		
		this.keyup(
			function(e)
			{
				this.value = this.value.replace(/\,/gi, ".");
			}
		);
		return this;
			
	};	

})(jQuery);

