/*
 * Copyright (c) 2001-2007, TIBCO Software Inc.
 * Use, modification, and distribution subject to terms of license.
 */
jsx3.Class.defineClass("jsx3.util.NumberFormat",null,null,function(g,n){g.getIntegerInstance=function(b){return g.iE(b,".integer");};g.getNumberInstance=function(r){return g.iE(r,"");};g.getCurrencyInstance=function(k){return g.iE(k,".currency");};g.getPercentInstance=function(h){return g.iE(h,".percent");};g.iE=function(e,c){var L=jsx3.System.getLocaleProperties(e);var ec="format.number"+c+"._instance";var Mc=L.get(ec);if(!L.containsKey(ec)||Mc==null){var z=L.get("format.number"+c);Mc=new g(z,e);L.set("format.number"+c+"._instance",Mc);}return Mc;};n.Go=0;n.Dd="";n.RC="";n.Sn=null;n.qt=null;n.FA=false;n.Ry=Number.MAX_VALUE;n.NE=0;n.Sf=0;n.pA=0;n.Ow=1;n.nv=false;n.init=function(s,p){this.Nz=s;this.BB=p||jsx3.System.getLocale();this.UE();};n.getLocale=function(){return this.BB;};n.setLocale=function(b){this.BB=b;this.UE();};n.format=function(i){var Jb=jsx3.System.getLocaleProperties(this.BB);if(isNaN(i)){return Jb.get("number.NaN");}else{if(typeof(i)!="number")i=Number(i);var jb=i>=0;var Cb=jb?this.Dd:this.Sn!=null?this.Sn:this.Dd+Jb.get("number.minus");var bc=jb?this.RC:this.qt!=null?this.qt:this.RC;var Hb=null;if(!isFinite(i)){Hb=Jb.get("number.infinity");}else{i=this.Ow*Math.abs(i);var ec=g.Kg(i);var vb=ec[0];var Db=ec[1];if(this.Sf<vb.length-Db){var uc=vb.splice(Db+this.Sf,vb.length-Db-this.Sf);if(g.DG(vb,uc))Db++;}var Bc=Db>=0?vb.slice(0,Db):vb;var sb=Db>=0?vb.slice(Db):[];g.KB(Bc,this.Ry,this.NE,true);g.KB(sb,this.Sf,this.pA,false);var Sb=Jb.get("number.zero");g.Qk(Bc,Sb);g.Qk(sb,Sb);if(this.Go>0){for(var lc=Bc.length-this.Go;lc>=1;lc=lc-this.Go){Bc.splice(lc,0,Jb.get("number.grouping"));}}Hb=Bc.join("");if(this.FA||sb.length>0)Hb=Hb+(Jb.get(this.nv?"number.currency.decimal":"number.decimal")+sb.join(""));}return Cb+Hb+bc;}};g.Kg=function(r){if(r<0)throw new jsx3.Exception();var Tb=Math.log(r)*Math.LOG10E;if(!isFinite(Tb)){if(r==0)return [[0],0];throw new jsx3.Exception();}var cb=r.toString();var dc=[];var Hc=null;var X="0".charCodeAt(0);var G=".".charCodeAt(0);for(var Jc=0;Jc<cb.length;Jc++){var rc=cb.charCodeAt(Jc);if(rc>=X&&rc<=X+9){dc.push(rc-X);}else{if(rc==G){}else{break;}}}if(Tb>=0){var lb=Math.floor(Tb+1);if(dc.length>lb){Hc=lb;}else{if(dc.length<lb){for(var Jc=dc.length;Jc<lb;Jc++)dc.push(0);}Hc=dc.length;}}else{var R=Math.ceil(-1-Tb);var ab=dc.indexOf(0);dc.splice(0,ab+1);for(var Jc=0;Jc<R;Jc++){if(dc[Jc]!=0)dc.splice(Jc,0,0);}Hc=0;}return [dc,Hc];};g.KB=function(m,h,q,i){if(m.length>h){if(i){m.splice(0,m.length-h);}else{m.splice(h,m.length-h);}}else{if(m.length<q){var Mb=i?"unshift":"push";for(var F=m.length;F<q;F++)m[Mb]("0");}}};g.DG=function(j,k){if(k[0]>=5){for(var qc=j.length-1;qc>=0;qc--){var hb=j[qc]+1;if(hb>=10){j[qc]=0;if(qc==0){j.unshift(1);return true;}}else{j[qc]=hb;break;}}}return false;};g.Qk=function(r,q){var Ub=q.charCodeAt(0);for(var Sb=0;Sb<r.length;Sb++)r[Sb]=String.fromCharCode(r[Sb]+Ub);};g.fl="0#,.";n.UE=function(){var E=jsx3.System.getLocaleProperties(this.BB);var ec=this.Nz;var Ic=0;var Bc=ec.length;var Fc=1;var eb=false;var wc=null,tc=null;while(Ic<Bc){var Zb=ec.charAt(Ic);if(Fc==1){if(Zb=="'"){if(ec.charAt(Ic+1)=="'"){this.Dd+=Zb;Ic=Ic+2;}else{eb=!eb;Ic++;}}else{if(eb){this.Dd+=Zb;Ic++;}else{if(g.fl.indexOf(Zb)>=0){wc=Ic;Fc++;}else{this.Dd+=this.js(Zb,E);Ic++;}}}}else{if(Fc==2){if(g.fl.indexOf(Zb)>=0){Ic++;}else{tc=Ic;Fc++;}}else{if(Fc==3){if(Zb=="'"){if(ec.charAt(Ic+1)=="'"){this.RC+=Zb;Ic++;}else{eb=!eb;}}else{if(eb){this.RC+=Zb;}else{if(Zb==";"){this.Sn="";this.qt="";Fc++;}else{this.RC+=this.js(Zb,E);}}}Ic++;}else{if(Fc==4){if(Zb=="'"){if(ec.charAt(Ic+1)=="'"){this.Sn+=Zb;Ic=Ic+2;}else{eb=!eb;Ic++;}}else{if(eb){this.Sn+=Zb;Ic++;}else{if(g.fl.indexOf(Zb)>=0){Fc++;}else{this.Sn+=this.js(Zb,E);Ic++;}}}}else{if(Fc==5){if(g.fl.indexOf(Zb)>=0){Ic++;}else{Fc++;}}else{if(Fc==6){if(Zb=="'"){if(ec.charAt(Ic+1)=="'"){this.qt+=Zb;Ic++;}else{eb=!eb;}}else{if(eb){this.qt+=Zb;}else{this.qt+=this.js(Zb,E);}}Ic++;}}}}}}}if(tc==null)tc=Bc;if(wc==null)throw new jsx3.Exception("no number part in number format: "+ec);this.Uw(ec.substring(wc,tc));};n.Uw=function(m){var Gb=m.indexOf(".");if(Gb<0)Gb=m.length;else{if(Gb==m.length-1)this.FA=true;}var sc=m.lastIndexOf(",");if(sc>=0){var Gc=Gb-sc-1;if(Gc<1)throw new jsx3.Exception("grouping marker after decimal marker: "+m);this.Go=Gc;}for(var lb=0;lb<Gb;lb++){if(m.charAt(lb)=="0")this.NE++;}for(var lb=Gb+1;lb<m.length;lb++){if(m.charAt(lb)=="0"){this.pA++;this.Sf++;}else{if(m.charAt(lb)=="#"){this.Sf++;}}}};n.js=function(o,r){if(o=="$"){this.nv=true;return r.get("number.currency");}else{if(o=="%"){this.Ow=100;return r.get("number.percent");}else{if(o=="-"){return r.get("number.minus");}else{return o;}}}};n.toString=function(){return this.Nz;};});
