(function(g){function w(a,c){g.each(c,function(b,d){if("function"===typeof d)c[b]=d(a,c,b);else if("string"===typeof d){var e=d.substr(0,4);"fun:"===e?"function"===typeof g.autoNumeric[d.substr(4)]?c[b]=g.autoNumeric[d.substr(4)](a,c,b):c[b]=null:"css:"===e&&(c[b]=g(d.substr(4)).val())}})}function s(a,c){"string"===typeof a[c]&&(a[c]=new BigDecimal(a[c]))}function l(a,c){var b=g.extend({},g.fn.autoNumeric.defaults,c);g.metadata&&(b=g.extend(b,a.metadata()));w(a,b);var d=b.vMax.toString().split("."),
e=b.vMin||0===b.vMin?b.vMin.toString().split("."):[];s(b,"vMax");s(b,"vMin");"string"===typeof b.mDec&&(b.mDec*=1);b.aNeg=0>b.vMin?"-":"";"number"!==typeof b.mDec&&(b.mDec=Math.max((d[1]?d[1]:"").length,(e[1]?e[1]:"").length));null===b.altDec&&0<b.mDec&&("."===b.aDec&&","!==b.aSep?b.altDec=",":","===b.aDec&&"."!==b.aSep&&(b.altDec="."));d=b.aNeg?"([-\\"+b.aNeg+"]?)":"(-?)";b._aNegReg=d;b._skipFirst=RegExp(d+"[^-"+(b.aNeg?"\\"+b.aNeg:"")+"\\"+b.aDec+"\\d].*?(\\d|\\"+b.aDec+"\\d)");b._skipLast=RegExp("(\\d\\"+
b.aDec+"?)[^\\"+b.aDec+"\\d]\\D*$");e=(b.aNeg?b.aNeg:"-")+b.aNum+"\\"+b.aDec;b.altDec&&b.altDec!==b.aSep&&(e+=b.altDec);b._allowed=RegExp("[^"+e+"]","gi");b._numReg=RegExp(d+"(?:\\"+b.aDec+"?(\\d+\\"+b.aDec+"\\d+)|(\\d*(?:\\"+b.aDec+"\\d*)?))");return b}function h(a,c,b){if(c.aSign)for(;-1<a.indexOf(c.aSign);)a=a.replace(c.aSign,"");a=a.replace(c._skipFirst,"$1$2");a=a.replace(c._skipLast,"$1");a=a.replace(c._allowed,"");c.altDec&&(a=a.replace(c.altDec,c.aDec));a=(a=a.match(c._numReg))?[a[1],a[2],
a[3]].join(""):"";b&&(c="^"+c._aNegReg+"0*(\\d"+("leading"===b?")":"|$)"),c=RegExp(c),a=a.replace(c,"$1$2"));return a}function t(a,c,b){if(c&&b){var d=a.split(c);d[1]&&d[1].length>b&&(0<b?(d[1]=d[1].substring(0,b),a=d.join(c)):a=d[0])}return a}function m(a,c,b){c&&"."!==c&&(a=a.replace(c,"."));b&&"-"!==b&&(a=a.replace(b,"-"));a.match(/\d/)||(a+="0");return a}function u(a,c,b){b&&"-"!==b&&(a=a.replace("-",b));c&&"."!==c&&(a=a.replace(".",c));return a}function n(a,c){a=h(a,c);a=t(a,c.aDec,c.mDec);a=
m(a,c.aDec,c.aNeg);var b=new BigDecimal(a);return b.isGreaterThanOrEqualTo(c.vMin)&&b.isLessThanOrEqualTo(c.vMax)}function p(a,c){a=h(a,c);if(""===a||a===c.aNeg)return"zero"===c.wEmpty?a+"0":"sign"===c.wEmpty?a+c.aSign:a;var b="",b=2===c.dGroup?/(\d)((\d)(\d{2}?)+)$/:4===c.dGroup?/(\d)((\d{4}?)+)$/:/(\d)((\d{3}?)+)$/,d=a.split(c.aDec);c.altDec&&1===d.length&&(d=a.split(c.altDec));var e=d[0];if(c.aSep)for(;b.test(e);)e=e.replace(b,"$1"+c.aSep+"$2");0!==c.mDec&&1<d.length?(d[1].length>c.mDec&&(d[1]=
d[1].substring(0,c.mDec)),a=e+c.aDec+d[1]):a=e;c.aSign&&(b=-1!==a.indexOf(c.aNeg),a=a.replace(c.aNeg,""),a="p"===c.pSign?c.aSign+a:a+c.aSign,b&&(a=c.aNeg+a));return a}function q(a,c,b,d){a=""===a?"0":a+="";var e="",f=0,g="";"-"===a.charAt(0)&&(g=0===1*a?"":"-",a=a.replace("-",""));if(0<1*a)for(;"0"===a.substr(0,1)&&1<a.length;)a=a.substr(1);f=a.lastIndexOf(".");0===f&&(a="0"+a,f=1);if(-1===f||f===a.length-1){if(d&&0<c){e=-1===f?a+".":a;for(f=0;f<c;f++)e+="0";return g+e}return g+a}var k=a.length-1-
f;if(k===c)return g+a;if(k<c&&d){e=a;for(f=k;f<c;f++)e+="0";return g+e}d=f+c;for(var k=1*a.charAt(d+1),h=[],f=0;f<=d;f++)h[f]=a.charAt(f);a="."===a.charAt(d)?a.charAt(d-1)%2:a.charAt(d)%2;if(4<k&&"S"===b||4<k&&"A"===b&&""===g||5<k&&"A"===b&&"-"===g||5<k&&"s"===b||5<k&&"a"===b&&""===g||4<k&&"a"===b&&"-"===g||5<k&&"B"===b||5===k&&"B"===b&&1===a||0<k&&"C"===b&&""===g||0<k&&"F"===b&&"-"===g||0<k&&"U"===b)for(f=h.length-1;0<=f&&!("."!==h[f]&&(h[f]++,10>h[f]));f--);for(f=0;f<=d;f++)e="."===h[f]||10>h[f]||
0===f?e+h[f]:e+"0";0===c&&(e=e.replace(".",""));return g+e}function v(a,c){this.options=c;this.that=a;this.$that=g(a);this.formatted=!1;this.io=l(this.$that,this.options);this.value=a.value}function r(a){"string"===typeof a&&(a=a.replace(/\[/g,"\\[").replace(/\]/g,"\\]"),a="#"+a.replace(/(:|\.)/g,"\\$1"));return g(a)}v.prototype={init:function(a){this.value=this.that.value;this.io=l(this.$that,this.options);this.cmdKey=a.metaKey;this.shiftKey=a.shiftKey;var c=this.that,b={};if(void 0===c.selectionStart){c.focus();
var d=document.selection.createRange();b.length=d.text.length;d.moveStart("character",-c.value.length);b.end=d.text.length;b.start=b.end-b.length}else b.start=c.selectionStart,b.end=c.selectionEnd,b.length=b.end-b.start;this.selection=b;if("keydown"===a.type||"keyup"===a.type)this.kdCode=a.keyCode;this.which=a.which;this.formatted=this.processed=!1},setSelection:function(a,c,b){a=Math.max(a,0);c=Math.min(c,this.that.value.length);this.selection={start:a,end:c,length:c-a};if(void 0===b||b)b=this.that,
void 0===b.selectionStart?(b.focus(),b=b.createTextRange(),b.collapse(!0),b.moveEnd("character",c),b.moveStart("character",a),b.select()):(b.selectionStart=a,b.selectionEnd=c)},setPosition:function(a,c){this.setSelection(a,a,c)},getBeforeAfter:function(){var a=this.value,c=a.substring(0,this.selection.start),a=a.substring(this.selection.end,a.length);return[c,a]},getBeforeAfterStriped:function(){var a=this.getBeforeAfter();a[0]=h(a[0],this.io);a[1]=h(a[1],this.io);return a},normalizeParts:function(a,
c){var b=this.io;c=h(c,b);var d=c.match(/^\d/)?!0:"leading";a=h(a,b,d);(""===a||a===b.aNeg)&&""<c&&(c=c.replace(/^0*(\d)/,"$1"));d=a+c;if(b.aDec){var e=d.match(RegExp("^"+b._aNegReg+"\\"+b.aDec));e&&(a=a.replace(e[1],e[1]+"0"),d=a+c)}"zero"!==b.wEmpty||d!==b.aNeg&&""!==d||(a+="0");return[a,c]},setValueParts:function(a,c){var b=this.io,d=this.normalizeParts(a,c),e=d.join(""),d=d[0].length;return n(e,b)?(e=t(e,b.aDec,b.mDec),d>e.length&&(d=e.length),this.value=e,this.setPosition(d,!1),!0):!1},signPosition:function(){var a=
this.io,c=a.aSign,b=this.that;if(c){c=c.length;if("p"===a.pSign)return a.aNeg&&b.value&&b.value.charAt(0)===a.aNeg?[1,c+1]:[0,c];a=b.value.length;return[a-c,a]}return[1E3,-1]},expandSelectionOnSign:function(a){var c=this.signPosition(),b=this.selection;b.start<c[1]&&b.end>c[0]&&((b.start<c[0]||b.end>c[1])&&this.value.substring(Math.max(b.start,c[0]),Math.min(b.end,c[1])).match(/^\s*$/)?b.start<c[0]?this.setSelection(b.start,c[0],a):this.setSelection(c[1],b.end,a):this.setSelection(Math.min(b.start,
c[0]),Math.max(b.end,c[1]),a))},checkPaste:function(){if(void 0!==this.valuePartsBeforePaste){var a=this.getBeforeAfter(),c=this.valuePartsBeforePaste;delete this.valuePartsBeforePaste;a[0]=a[0].substr(0,c[0].length)+h(a[0].substr(c[0].length),this.io);this.setValueParts(a[0],a[1])||(this.value=c.join(""),this.setPosition(c[0].length,!1))}},skipAllways:function(a){var c=this.kdCode,b=this.which,d=this.cmdKey;if(17===c&&"keyup"===a.type&&void 0!==this.valuePartsBeforePaste)return this.checkPaste(),
!1;if(112<=c&&123>=c||91<=c&&93>=c||9<=c&&31>=c||8>c&&(0===b||b===c)||144===c||145===c||45===c||d&&65===c)return!0;if(d&&(67===c||86===c||88===c))return"keydown"===a.type&&this.expandSelectionOnSign(),86===c&&("keydown"===a.type||"keypress"===a.type?void 0===this.valuePartsBeforePaste&&(this.valuePartsBeforePaste=this.getBeforeAfter()):this.checkPaste()),"keydown"===a.type||"keypress"===a.type||67===c;if(d)return!0;if(37===c||39===c){var b=this.io.aSep,d=this.selection.start,e=this.that.value;"keydown"===
a.type&&b&&!this.shiftKey&&(37===c&&e.charAt(d-2)===b?this.setPosition(d-1):39===c&&e.charAt(d)===b&&this.setPosition(d+1));return!0}return 34<=c&&40>=c?!0:!1},processAllways:function(){var a;return 8===this.kdCode||46===this.kdCode?(this.selection.length?(this.expandSelectionOnSign(!1),a=this.getBeforeAfterStriped()):(a=this.getBeforeAfterStriped(),8===this.kdCode?a[0]=a[0].substring(0,a[0].length-1):a[1]=a[1].substring(1,a[1].length)),this.setValueParts(a[0],a[1]),!0):!1},processKeypress:function(){var a=
this.io,c=String.fromCharCode(this.which),b=this.getBeforeAfterStriped(),d=b[0],b=b[1];if(c===a.aDec||a.altDec&&c===a.altDec||("."===c||","===c)&&110===this.kdCode){if(!a.mDec||!a.aDec||a.aNeg&&-1<b.indexOf(a.aNeg)||-1<d.indexOf(a.aDec)||0<b.indexOf(a.aDec))return!0;0===b.indexOf(a.aDec)&&(b=b.substr(1));this.setValueParts(d+a.aDec,b);return!0}if("-"===c||"+"===c){if(!a.aNeg)return!0;""===d&&-1<b.indexOf(a.aNeg)&&(d=a.aNeg,b=b.substring(1,b.length));d=d.charAt(0)===a.aNeg?d.substring(1,d.length):
"-"===c?a.aNeg+d:d;this.setValueParts(d,b);return!0}"0"<=c&&"9">=c&&(a.aNeg&&""===d&&-1<b.indexOf(a.aNeg)&&(d=a.aNeg,b=b.substring(1,b.length)),this.setValueParts(d+c,b));return!0},formatQuick:function(){var a=this.io,c=this.getBeforeAfterStriped(),b=p(this.value,this.io),d=b.length;if(b){var c=c[0].split(""),e;for(e in c)c[e].match("\\d")||(c[e]="\\"+c[e]);e=RegExp("^.*?"+c.join(".*?"));(e=b.match(e))?(d=e[0].length,(0===d&&b.charAt(0)!==a.aNeg||1===d&&b.charAt(0)===a.aNeg)&&a.aSign&&"p"===a.pSign&&
(d=this.io.aSign.length+("-"===b.charAt(0)?1:0))):a.aSign&&"s"===a.pSign&&(d-=a.aSign.length)}this.that.value=b;this.setPosition(d);this.formatted=!0}};g.fn.autoNumeric=function(a){return this.each(function(){var c=g(this),b=new v(this,a);g(this).data("AutoNumericOptions",a);b.io.aForm&&(this.value||"empty"!==b.io.wEmpty)&&c.autoNumericSet(c.autoNumericGet(a),a);c.keydown(function(a){b.init(a);if(b.skipAllways(a))return b.processed=!0;if(b.processAllways())return b.processed=!0,b.formatQuick(),a.preventDefault(),
!1;b.formatted=!1;return!0}).keypress(function(a){a.stopImmediatePropagation();var c=b.processed;b.init(a);if(b.skipAllways(a))return!0;if(c)return a.preventDefault(),!1;if(b.processAllways()||b.processKeypress())return b.formatQuick(),a.preventDefault(),!1;b.formatted=!1}).keyup(function(a){b.init(a);a=b.skipAllways(a);b.kdCode=0;delete b.valuePartsBeforePaste;if(a||""===this.value)return!0;b.formatted||b.formatQuick()}).focusout(function(a){var e=b.io,f=c.val();a=f;""!==f&&(f=h(f,e),n(f,e)?(f=m(f,
e.aDec,e.aNeg),f=q(f,e.mDec,e.mRound,e.aPad),f=u(f,e.aDec,e.aNeg)):f="");e=p(f,e);e!==a&&c.val(e);e!==b.inVal&&(c.change(),delete b.inVal)}).focusin(function(a){b.inVal=c.val()})})};g.autoNumeric={};g.autoNumeric.Strip=function(a,c){var b=l(r(a),c),d=r(a).val(),d=h(d,b),d=m(d,b.aDec,b.aNeg);0===1*d&&(d="0");return d};g.autoNumeric.Format=function(a,c,b){c+="";a=l(r(a),b);c=q(c,a.mDec,a.mRound,a.aPad);c=u(c,a.aDec,a.aNeg);n(c,a)||(c=q("",a.mDec,a.mRound,a.aPad));return p(c,a)};g.fn.autoNumericGet=
function(a){return g.fn.autoNumeric.Strip(this,a)};g.fn.autoNumericSet=function(a,c){return this.val(g.fn.autoNumeric.Format(this,a,c))};g.autoNumeric.defaults={aNum:"0123456789",aSep:",",dGroup:"3",aDec:".",altDec:null,aSign:"",pSign:"p",vMax:"999999999.99",vMin:"0.00",mDec:null,mRound:"S",aPad:!0,wEmpty:"empty",aForm:!1};g.fn.autoNumeric.defaults=g.autoNumeric.defaults;g.fn.autoNumeric.Strip=g.autoNumeric.Strip;g.fn.autoNumeric.Format=g.autoNumeric.Format})(jQuery);
