
﻿
(function($){$.fn.autoNumeric=function(options){var opts=$.extend({},$.fn.autoNumeric.defaults,options);return this.each(function(){var iv=$(this);var ii=this.id;var io=$.metadata?$.extend({},opts,iv.metadata()):opts;io.mDec=isNaN(io.mDec*1)?$('#'+io.mDec).val()*1:io.mDec*1;var kdCode='';var selectLength=0;var caretPos=0;var inLength=0;var charLeft=0;var numLeft=0;var numRight=0;var cmdKey=false;$(this).keydown(function(e){io=$.metadata?$.extend({},opts,iv.metadata()):opts;io.mDec=isNaN(io.mDec*1)?$('#'+io.mDec).val()*1:io.mDec*1;cmdKey=false;if(!e){e=window.event;}
if(e.keyCode){kdCode=e.keyCode;}
else if(e.which){kdCode=e.which;}
if(e.metaKey){cmdKey=true;}
if(document.selection){this.focus();var select=document.selection.createRange();selectLength=document.selection.createRange().text.length;select.moveStart('character',-this.value.length);caretPos=(select.text.length-selectLength)*1;}
else if(this.selectionStart||this.selectionStart=='0'){selectLength=this.selectionEnd*1-this.selectionStart*1;caretPos=this.selectionStart*1;}
inLength=this.value.length;}).keypress(function(e){var allowed=io.aNum+io.aNeg+io.aDec;charLeft=(this.value.lastIndexOf(io.aDec)==-1)?inLength:inLength-(inLength-this.value.lastIndexOf(io.aDec));numLeft=autoCount(this.value,0,charLeft);if(this.value.lastIndexOf(io.aDec)!=-1){numRight=autoCount(this.value,charLeft,inLength);}
if((e.ctrlKey||cmdKey)&&(kdCode==65||kdCode==67||kdCode==86||kdCode==88)){return true;}
if(kdCode==8||kdCode==9||kdCode==13||kdCode==35||kdCode==36||kdCode==37||kdCode==39||kdCode==46){return true;}
var kpCode='';if(!e){e=window.event;}
if(e.keyCode){kpCode=e.keyCode;}
else if(e.which){kpCode=e.which;}
var cCode=String.fromCharCode(kpCode);if(allowed.indexOf(cCode)==-1){e.preventDefault();}
if(cCode==io.aDec){if(selectLength==inLength&&selectLength>0){return;}
if(caretPos<=this.value.lastIndexOf('-')||this.value.indexOf(io.aDec)!=-1||io.mDec===0){e.preventDefault();}
if(caretPos<=this.value.lastIndexOf(io.aSep)&&this.value.lastIndexOf(io.aSep)!=-1&&io.aSep!==''){if(io.pSign=='s'&&io.aSign.indexOf(' ')>=0){var subStr=this.value.substring(0,this.value.length-io.aSign.length);var subStrPos=subStr.lastIndexOf(' ');if(caretPos>subStrPos&&caretPos>=subStr.length-io.mDec){return;}
else{e.preventDefault();}}
else{e.preventDefault();}}
if(io.aSign===''&&caretPos<this.value.length-io.mDec){e.preventDefault();}
if(io.aSign!==''&&io.pSign=='p'&&(this.value.length-caretPos>io.mDec||caretPos<io.aSign.length)){e.preventDefault();}
if(io.aSign!==''&&io.pSign=='s'&&(caretPos>this.value.length-io.aSign.length||caretPos<this.value.length-(io.aSign.length+io.mDec))){e.preventDefault();}}
if(kpCode==45&&(caretPos>0||this.value.indexOf('-')!=-1||io.aNeg==='')){if(selectLength>=1&&caretPos===0){return;}
else{e.preventDefault();}}
if(kpCode>=48&&kpCode<=57){if(selectLength>0){return;}
if(caretPos<io.aSign.length&&io.aSign!==''&&io.pSign=='p'&&inLength>0){e.preventDefault();}
if(caretPos>this.value.length-io.aSign.length&&io.aSign!==''&&io.pSign=='s'&&this.value!==''){e.preventDefault();}
if(caretPos==this.value.lastIndexOf('-')){e.preventDefault();}
if(numLeft>=io.mNum&&caretPos<=charLeft){e.preventDefault();}
if(this.value.indexOf(io.aDec)!=-1&&caretPos>=charLeft+1&&numRight>=io.mDec){e.preventDefault();}}}).keyup(function(e){if(this.value===''){return;}
if(io.aSep===''||e.keyCode==9||e.keyCode==20||e.keyCode==35||e.keyCode==36||e.keyCode==37||e.keyCode==39||kdCode==9||kdCode==13||kdCode==20||kdCode==35||kdCode==36||kdCode==37||kdCode==39){return;}
$(this).val(autoGroup(this.value,io));var outLength=this.value.length;charLeft=(this.value.lastIndexOf(io.aDec)==-1)?outLength:outLength-(outLength-this.value.lastIndexOf(io.aDec));numLeft=autoCount(this.value,0,charLeft);if(numLeft>io.mNum){$(this).val('');}
var setCaret=0;if(inLength<outLength){setCaret=(outLength==io.aSign.length+1&&io.pSign=='s')?1:caretPos+(outLength-inLength);}
if(inLength>outLength){if(selectLength===0){if((inLength-2)==outLength){if(kdCode==8){setCaret=(caretPos-2);}
else if(kdCode==46){setCaret=caretPos;}
else{setCaret=(caretPos-1);}}
else{setCaret=(kdCode==8)?caretPos-1:caretPos;}}
if(selectLength>0&&selectLength<inLength){setCaret=(outLength-(inLength-(caretPos+selectLength)));}
if(selectLength==inLength){setCaret=(outLength==io.aSign.length+1&&io.pSign=='s')?1:1+io.aSign.length;}}
if(inLength==outLength){if(selectLength>=0){setCaret=caretPos+selectLength;}
if(this.value.charAt(caretPos-1)==io.aSep&&kdCode==8){setCaret=(caretPos-1);}
else if(this.value.charAt(caretPos)==io.aSep&&kdCode==46){setCaret=(caretPos+1);}}
var iField=this;iField.focus();if(document.selection){var iRange=iField.createTextRange();iRange.collapse(true);iRange.moveStart("character",setCaret);iRange.moveEnd("character",0);iRange.select();}
else if(iField.selectionStart||iField.selectionStart=='0'){iField.selectionStart=setCaret;iField.selectionEnd=setCaret;}}).bind('change focusout',function(){if($(this).val()!==''){autoCheck(iv,this,io);}}).bind('paste',function(){setTimeout(function(){autoCheck(iv,this,io);},0);});});};function autoId(myid){myid=myid.replace(/\[/g,"\\[").replace(/\]/g,"\\]");return'#'+myid.replace(/(:|\.)/g,'\\$1');}
function autoCount(str,start,end){var chr='';var numCount=0;for(j=start;j<end;j++){chr=str.charAt(j);if(chr>='0'&&chr<='9'){numCount++;}}
return numCount;}
function autoGroup(iv,io){if(io.aSep!==''){var digitalGroup='';if(io.dGroup==2){digitalGroup=/(\d)((\d)(\d{2}?)+)$/;}
else if(io.dGroup==4){digitalGroup=/(\d)((\d{4}?)+)$/;}
else{digitalGroup=/(\d)((\d{3}?)+)$/;}
for(k=0;k<io.aSign.length;k++){iv=iv.replace(io.aSign.charAt(k),'').replace("\u00A0",'');}
iv=iv.split(io.aSep).join('');var ivSplit=iv.split(io.aDec);var s=ivSplit[0];while(digitalGroup.test(s)){s=s.replace(digitalGroup,'$1'+io.aSep+'$2');}
if(io.mDec!==0&&ivSplit.length>1){iv=s+io.aDec+ivSplit[1];}
else{iv=s;}
if(iv.indexOf('-')!==-1&&io.aSign!==''&&io.pSign=='p'){iv=iv.replace('-','');return'-'+io.aSign+iv;}
else if(iv.indexOf('-')==-1&&io.aSign!==''&&io.pSign=='p'){return io.aSign+iv;}
if(iv.indexOf('-')!==-1&&io.aSign!==''&&io.pSign=='s'){iv=iv.replace('-','');return'-'+iv+io.aSign;}
else if(iv.indexOf('-')==-1&&io.aSign!==''&&io.pSign=='s'){return iv+io.aSign;}
else{return iv;}}
else{return iv;}}
function autoRound(iv,mDec,mRound,aPad){iv=(iv==='')?'0':iv+='';var ivRounded='';var i=0;var nSign='';if(iv.charAt(0)=='-'){nSign=(iv*1===0)?'':'-';iv=iv.replace('-','');}
if((iv*1)>0){while(iv.substr(0,1)=='0'&&iv.length>1){iv=iv.substr(1);}}
var dPos=iv.lastIndexOf('.');if(dPos===0){iv='0'+iv;dPos=1;}
if(dPos==-1||dPos==iv.length-1){if(aPad&&mDec>0){ivRounded=(dPos==-1)?iv+'.':iv;for(i=0;i<mDec;i++){ivRounded+='0';}
return nSign+ivRounded;}
else{return nSign+iv;}}
var cDec=(iv.length-1)-dPos;if(cDec==mDec){return nSign+iv;}
if(cDec<mDec&&aPad){ivRounded=iv;for(i=cDec;i<mDec;i++){ivRounded+='0';}
return nSign+ivRounded;}
var rLength=dPos+mDec;var tRound=iv.charAt(rLength+1)*1;var ivArray=[];for(i=0;i<=rLength;i++){ivArray[i]=iv.charAt(i);}
var odd=(iv.charAt(rLength)=='.')?(iv.charAt(rLength-1)%2):(iv.charAt(rLength)%2);if((tRound>4&&mRound==='S')||(tRound>4&&mRound==='A'&&nSign==='')||(tRound>5&&mRound==='A'&&nSign=='-')||(tRound>5&&mRound==='s')||(tRound>5&&mRound==='a'&&nSign==='')||(tRound>4&&mRound==='a'&&nSign=='-')||(tRound>5&&mRound==='B')||(tRound==5&&mRound==='B'&&odd==1)||(tRound>0&&mRound==='C'&&nSign==='')||(tRound>0&&mRound==='F'&&nSign=='-')||(tRound>0&&mRound==='U')){for(i=(ivArray.length-1);i>=0;i--){if(ivArray[i]=='.'){continue;}
ivArray[i]++;if(ivArray[i]<10){break;}}}
for(i=0;i<=rLength;i++){if(ivArray[i]=='.'||ivArray[i]<10||i===0){ivRounded+=ivArray[i];}
else{ivRounded+='0';}}
if(mDec===0){ivRounded=ivRounded.replace('.','');}
return nSign+ivRounded;}
function autoCheck(iv,obj,io){iv=iv.val();if(iv.length>100){$(obj).val('');return false;}
var eNeg='';if(io.aNeg=='-'){eNeg='\\-';}
var reg=new RegExp('[^'+eNeg+io.aNum+io.aDec+']','gi');var testPaste=iv.replace(reg,'');if(testPaste.lastIndexOf('-')>0||testPaste.indexOf(io.aDec)!=testPaste.lastIndexOf(io.aDec)){testPaste='';}
var rePaste='';var nNeg=0;var nSign='';var i=0;var s=testPaste.split('');for(i=0;i<s.length;i++){if(i===0&&s[i]=='-'){nNeg=1;nSign='-';continue;}
if(s[i]==io.aDec&&s.length-1==i){break;}
if(rePaste.length===0&&s[i]=='0'&&(s[i+1]>=0||s[i+1]<=9)){continue;}
else{rePaste=rePaste+s[i];}}
rePaste=nSign+rePaste;if(rePaste.indexOf(io.aDec)==-1&&rePaste.length>(io.mNum+nNeg)){rePaste='';}
if(rePaste.indexOf(io.aDec)>(io.mNum+nNeg)){rePaste='';}
if(rePaste.indexOf(io.aDec)!=-1&&(io.aDec!='.')){rePaste=rePaste.replace(io.aDec,'.');}
rePaste=autoRound(rePaste,io.mDec,io.mRound,io.aPad);if(io.aDec!='.'){rePaste=rePaste.replace('.',io.aDec);}
if(rePaste!==''&&io.aSep!==''){rePaste=autoGroup(rePaste,io);}
$(obj).val(rePaste);return false;}
$.fn.autoNumeric.Strip=function(value,options){var opts=$.extend({},$.fn.autoNumeric.defaults,options);var io=opts;io.mDec=isNaN(io.mDec*1)?$('#'+io.mDec).val()*1:io.mDec*1;var iv=value;iv=iv.replace(io.aSign,'').replace('\u00A0','');var reg=new RegExp('[^'+'\\-'+io.aNum+io.aDec+']','gi');iv=iv.replace(reg,'');var nSign='';if(iv.charAt(0)=='-'){nSign=(iv*1===0)?'':'-';iv=iv.replace('-','');}
iv=iv.replace(io.aDec,'.');if(iv*1>0){while(iv.substr(0,1)=='0'&&iv.length>1){iv=iv.substr(1);}}
iv=(iv.lastIndexOf('.')===0)?('0'+iv):iv;iv=(iv*1===0)?'0':iv;return nSign+iv;};$.fn.autoNumeric.Format=function(iv,options){iv+='';var opts=$.extend({},$.fn.autoNumeric.defaults,options);var io=opts;io.mDec=isNaN(io.mDec*1)?$('#'+io.mDec).val()*1:io.mDec*1;iv=autoRound(iv,io.mDec,io.mRound,io.aPad);var nNeg=0;if(iv.indexOf('-')!=-1&&io.aNeg===''){iv='';}
else if(iv.indexOf('-')!=-1&&io.aNeg=='-'){nNeg=1;}
if(iv.indexOf('.')==-1&&iv.length>(io.mNum+nNeg)){iv='';}
else if(iv.indexOf('.')>(io.mNum+nNeg)){iv='';}
if(io.aDec!='.'){iv=iv.replace('.',io.aDec);}
return autoGroup(iv,io);};$.fn.autoNumeric.defaults={aNum:'0123456789',aNeg:'',aSep:',',aDec:'.',aSign:'',pSign:'p',mNum:9,mDec:2,dGroup:3,mRound:'S',aPad:true};})(jQuery);
