(function(d){d.fn.numeric=function(a,c){"boolean"===typeof a&&(a={decimal:a});a=a||{};"undefined"==typeof a.negative&&(a.negative=!0);var h=!1===a.decimal?"":a.decimal||".",b=!0===a.negative?!0:!1;c="function"==typeof c?c:function(){};return this.data("numeric.decimal",h).data("numeric.negative",b).data("numeric.callback",c).keypress(d.fn.numeric.keypress).keyup(d.fn.numeric.keyup).blur(d.fn.numeric.blur)};d.fn.numeric.keypress=function(a){var c=d.data(this,"numeric.decimal"),h=d.data(this,"numeric.negative"), b=a.charCode?a.charCode:a.keyCode?a.keyCode:0;if(13==b&&"input"==this.nodeName.toLowerCase())return!0;if(13==b)return!1;var f=!1;if(a.ctrlKey&&97==b||a.ctrlKey&&65==b||a.ctrlKey&&120==b||a.ctrlKey&&88==b||a.ctrlKey&&99==b||a.ctrlKey&&67==b||a.ctrlKey&&122==b||a.ctrlKey&&90==b||a.ctrlKey&&118==b||a.ctrlKey&&86==b||a.shiftKey&&45==b)return!0;if(48>b||57<b){var e=d(this).val();if(0!==e.indexOf("-")&&h&&45==b&&(0===e.length||0===parseInt(d.fn.getSelectionStart(this),10)))return!0;c&&b==c.charCodeAt(0)&& -1!=e.indexOf(c)&&(f=!1);8!=b&&9!=b&&13!=b&&35!=b&&36!=b&&37!=b&&39!=b&&46!=b?f=!1:"undefined"!=typeof a.charCode&&(a.keyCode==a.which&&0!==a.which?(f=!0,46==a.which&&(f=!1)):0!==a.keyCode&&0===a.charCode&&0===a.which&&(f=!0));c&&b==c.charCodeAt(0)&&(f=-1==e.indexOf(c)?!0:!1)}else f=!0;return f};d.fn.numeric.keyup=function(a){if((a=d(this).val())&&0<a.length){var c=d.fn.getSelectionStart(this),h=d.fn.getSelectionEnd(this),b=d.data(this,"numeric.decimal"),f=d.data(this,"numeric.negative");if(""!== b&&null!==b){var e=a.indexOf(b);0===e&&(this.value="0"+a);1==e&&"-"==a.charAt(0)&&(this.value="-0"+a.substring(1));a=this.value}for(var m=[0,1,2,3,4,5,6,7,8,9,"-",b],e=a.length,g=e-1;0<=g;g--){var k=a.charAt(g);0!==g&&"-"==k?a=a.substring(0,g)+a.substring(g+1):0!==g||f||"-"!=k||(a=a.substring(1));for(var n=!1,l=0;l<m.length;l++)if(k==m[l]){n=!0;break}n&&" "!=k||(a=a.substring(0,g)+a.substring(g+1))}f=a.indexOf(b);if(0<f)for(e-=1;e>f;e--)a.charAt(e)==b&&(a=a.substring(0,e)+a.substring(e+1));this.value= a;d.fn.setSelection(this,[c,h])}};d.fn.numeric.blur=function(){var a=d.data(this,"numeric.decimal"),c=d.data(this,"numeric.callback"),h=this.value;""!==h&&(RegExp("^\\d+$|^\\d*"+a+"\\d+$").exec(h)||c.apply(this))};d.fn.removeNumeric=function(){return this.data("numeric.decimal",null).data("numeric.negative",null).data("numeric.callback",null).unbind("keypress",d.fn.numeric.keypress).unbind("blur",d.fn.numeric.blur)};d.fn.getSelectionStart=function(a){if(a.createTextRange){var c=document.selection.createRange().duplicate(); c.moveEnd("character",a.value.length);return""===c.text?a.value.length:a.value.lastIndexOf(c.text)}return a.selectionStart};d.fn.getSelectionEnd=function(a){if(a.createTextRange){var c=document.selection.createRange().duplicate();c.moveStart("character",-a.value.length);return c.text.length}return a.selectionEnd};d.fn.setSelection=function(a,c){"number"==typeof c&&(c=[c,c]);if(c&&c.constructor==Array&&2==c.length)if(a.createTextRange){var d=a.createTextRange();d.collapse(!0);d.moveStart("character", c[0]);d.moveEnd("character",c[1]);d.select()}else a.setSelectionRange&&(a.focus(),a.setSelectionRange(c[0],c[1]))}})(jQuery);