/*
	Copyright (c) 2004-2009, The Dojo Foundation All Rights Reserved.
	Available via Academic Free License >= 2.1 OR the modified BSD license.
	see: http://dojotoolkit.org/license for details
*/


if(!dojo._hasResource["dojo.number"]){
dojo._hasResource["dojo.number"]=true;
dojo.provide("dojo.number");
dojo.require("dojo.i18n");
dojo.requireLocalization("dojo.cldr","number",null,"ROOT,ar,ca,cs,da,de,de-de,el,en,en-au,en-gb,en-us,es,es-es,fi,fr,he,hu,it,ja,ja-jp,ko,ko-kr,nb,nl,pl,pt,pt-pt,ru,sk,sl,sv,th,tr,zh,zh-cn,zh-tw");
dojo.require("dojo.string");
dojo.require("dojo.regexp");
dojo.number.format=function(_1,_2){
_2=dojo.mixin({},_2||{});
var _3=dojo.i18n.normalizeLocale(_2.locale);
var _4=dojo.i18n.getLocalization("dojo.cldr","number",_3);
_2.customs=_4;
var _5=_2.pattern||_4[(_2.type||"decimal")+"Format"];
if(isNaN(_1)){
return null;
}
return dojo.number._applyPattern(_1,_5,_2);
};
dojo.number._numberPatternRE=/[#0,]*[#0](?:\.0*#*)?/;
dojo.number._applyPattern=function(_6,_7,_8){
_8=_8||{};
var _9=_8.customs.group;
var _a=_8.customs.decimal;
var _b=_7.split(";");
var _c=_b[0];
_7=_b[(_6<0)?1:0]||("-"+_c);
if(_7.indexOf("%")!=-1){
_6*=100;
}else{
if(_7.indexOf("‰")!=-1){
_6*=1000;
}else{
if(_7.indexOf("¤")!=-1){
_9=_8.customs.currencyGroup||_9;
_a=_8.customs.currencyDecimal||_a;
_7=_7.replace(/\u00a4{1,3}/,function(_d){
var _e=["symbol","currency","displayName"][_d.length-1];
return _8[_e]||_8.currency||"";
});
}else{
if(_7.indexOf("E")!=-1){
throw new Error("exponential notation not supported");
}
}
}
}
var _f=dojo.number._numberPatternRE;
var _10=_c.match(_f);
if(!_10){
throw new Error("unable to find a number expression in pattern: "+_7);
}
if(_8.fractional===false){
_8.places=0;
}
return _7.replace(_f,dojo.number._formatAbsolute(_6,_10[0],{decimal:_a,group:_9,places:_8.places,round:_8.round}));
};
dojo.number.round=function(_11,_12,_13){
var _14=Math.log(Math.abs(_11))/Math.log(10);
var _15=10/(_13||10);
var _16=Math.pow(10,-14+_14);
return (_15*(+_11+(_11>0?_16:-_16))).toFixed(_12)/_15;
};
if((0.9).toFixed()==0){
(function(){
var _17=dojo.number.round;
dojo.number.round=function(v,p,m){
var d=Math.pow(10,-p||0),a=Math.abs(v);
if(!v||a>=d||a*Math.pow(10,p+1)<5){
d=0;
}
return _17(v,p,m)+(v>0?d:-d);
};
})();
}
dojo.number._formatAbsolute=function(_1d,_1e,_1f){
_1f=_1f||{};
if(_1f.places===true){
_1f.places=0;
}
if(_1f.places===Infinity){
_1f.places=6;
}
var _20=_1e.split(".");
var _21=(_1f.places>=0)?_1f.places:(_20[1]&&_20[1].length)||0;
if(!(_1f.round<0)){
_1d=dojo.number.round(_1d,_21,_1f.round);
}
var _22=String(Math.abs(_1d)).split(".");
var _23=_22[1]||"";
if(_1f.places){
var _24=dojo.isString(_1f.places)&&_1f.places.indexOf(",");
if(_24){
_1f.places=_1f.places.substring(_24+1);
}
_22[1]=dojo.string.pad(_23.substr(0,_1f.places),_1f.places,"0",true);
}else{
if(_20[1]&&_1f.places!==0){
var pad=_20[1].lastIndexOf("0")+1;
if(pad>_23.length){
_22[1]=dojo.string.pad(_23,pad,"0",true);
}
var _26=_20[1].length;
if(_26<_23.length){
_22[1]=_23.substr(0,_26);
}
}else{
if(_22[1]){
_22.pop();
}
}
}
var _27=_20[0].replace(",","");
pad=_27.indexOf("0");
if(pad!=-1){
pad=_27.length-pad;
if(pad>_22[0].length){
_22[0]=dojo.string.pad(_22[0],pad);
}
if(_27.indexOf("#")==-1){
_22[0]=_22[0].substr(_22[0].length-pad);
}
}
var _28=_20[0].lastIndexOf(",");
var _29,_2a;
if(_28!=-1){
_29=_20[0].length-_28-1;
var _2b=_20[0].substr(0,_28);
_28=_2b.lastIndexOf(",");
if(_28!=-1){
_2a=_2b.length-_28-1;
}
}
var _2c=[];
for(var _2d=_22[0];_2d;){
var off=_2d.length-_29;
_2c.push((off>0)?_2d.substr(off):_2d);
_2d=(off>0)?_2d.slice(0,off):"";
if(_2a){
_29=_2a;
delete _2a;
}
}
_22[0]=_2c.reverse().join(_1f.group||",");
return _22.join(_1f.decimal||".");
};
dojo.number.regexp=function(_2f){
return dojo.number._parseInfo(_2f).regexp;
};
dojo.number._parseInfo=function(_30){
_30=_30||{};
var _31=dojo.i18n.normalizeLocale(_30.locale);
var _32=dojo.i18n.getLocalization("dojo.cldr","number",_31);
var _33=_30.pattern||_32[(_30.type||"decimal")+"Format"];
var _34=_32.group;
var _35=_32.decimal;
var _36=1;
if(_33.indexOf("%")!=-1){
_36/=100;
}else{
if(_33.indexOf("‰")!=-1){
_36/=1000;
}else{
var _37=_33.indexOf("¤")!=-1;
if(_37){
_34=_32.currencyGroup||_34;
_35=_32.currencyDecimal||_35;
}
}
}
var _38=_33.split(";");
if(_38.length==1){
_38.push("-"+_38[0]);
}
var re=dojo.regexp.buildGroupRE(_38,function(_3a){
_3a="(?:"+dojo.regexp.escapeString(_3a,".")+")";
return _3a.replace(dojo.number._numberPatternRE,function(_3b){
var _3c={signed:false,separator:_30.strict?_34:[_34,""],fractional:_30.fractional,decimal:_35,exponent:false};
var _3d=_3b.split(".");
var _3e=_30.places;
if(_3d.length==1||_3e===0){
_3c.fractional=false;
}else{
if(_3e===undefined){
_3e=_30.pattern?_3d[1].lastIndexOf("0")+1:Infinity;
}
if(_3e&&_30.fractional==undefined){
_3c.fractional=true;
}
if(!_30.places&&(_3e<_3d[1].length)){
_3e+=","+_3d[1].length;
}
_3c.places=_3e;
}
var _3f=_3d[0].split(",");
if(_3f.length>1){
_3c.groupSize=_3f.pop().length;
if(_3f.length>1){
_3c.groupSize2=_3f.pop().length;
}
}
return "("+dojo.number._realNumberRegexp(_3c)+")";
});
},true);
if(_37){
re=re.replace(/([\s\xa0]*)(\u00a4{1,3})([\s\xa0]*)/g,function(_40,_41,_42,_43){
var _44=["symbol","currency","displayName"][_42.length-1];
var _45=dojo.regexp.escapeString(_30[_44]||_30.currency||"");
_41=_41?"[\\s\\xa0]":"";
_43=_43?"[\\s\\xa0]":"";
if(!_30.strict){
if(_41){
_41+="*";
}
if(_43){
_43+="*";
}
return "(?:"+_41+_45+_43+")?";
}
return _41+_45+_43;
});
}
return {regexp:re.replace(/[\xa0 ]/g,"[\\s\\xa0]"),group:_34,decimal:_35,factor:_36};
};
dojo.number.parse=function(_46,_47){
var _48=dojo.number._parseInfo(_47);
var _49=(new RegExp("^"+_48.regexp+"$")).exec(_46);
if(!_49){
return NaN;
}
var _4a=_49[1];
if(!_49[1]){
if(!_49[2]){
return NaN;
}
_4a=_49[2];
_48.factor*=-1;
}
_4a=_4a.replace(new RegExp("["+_48.group+"\\s\\xa0"+"]","g"),"").replace(_48.decimal,".");
return _4a*_48.factor;
};
dojo.number._realNumberRegexp=function(_4b){
_4b=_4b||{};
if(!("places" in _4b)){
_4b.places=Infinity;
}
if(typeof _4b.decimal!="string"){
_4b.decimal=".";
}
if(!("fractional" in _4b)||/^0/.test(_4b.places)){
_4b.fractional=[true,false];
}
if(!("exponent" in _4b)){
_4b.exponent=[true,false];
}
if(!("eSigned" in _4b)){
_4b.eSigned=[true,false];
}
var _4c=dojo.number._integerRegexp(_4b);
var _4d=dojo.regexp.buildGroupRE(_4b.fractional,function(q){
var re="";
if(q&&(_4b.places!==0)){
re="\\"+_4b.decimal;
if(_4b.places==Infinity){
re="(?:"+re+"\\d+)?";
}else{
re+="\\d{"+_4b.places+"}";
}
}
return re;
},true);
var _50=dojo.regexp.buildGroupRE(_4b.exponent,function(q){
if(q){
return "([eE]"+dojo.number._integerRegexp({signed:_4b.eSigned})+")";
}
return "";
});
var _52=_4c+_4d;
if(_4d){
_52="(?:(?:"+_52+")|(?:"+_4d+"))";
}
return _52+_50;
};
dojo.number._integerRegexp=function(_53){
_53=_53||{};
if(!("signed" in _53)){
_53.signed=[true,false];
}
if(!("separator" in _53)){
_53.separator="";
}else{
if(!("groupSize" in _53)){
_53.groupSize=3;
}
}
var _54=dojo.regexp.buildGroupRE(_53.signed,function(q){
return q?"[-+]":"";
},true);
var _56=dojo.regexp.buildGroupRE(_53.separator,function(sep){
if(!sep){
return "(?:\\d+)";
}
sep=dojo.regexp.escapeString(sep);
if(sep==" "){
sep="\\s";
}else{
if(sep==" "){
sep="\\s\\xa0";
}
}
var grp=_53.groupSize,_59=_53.groupSize2;
if(_59){
var _5a="(?:0|[1-9]\\d{0,"+(_59-1)+"}(?:["+sep+"]\\d{"+_59+"})*["+sep+"]\\d{"+grp+"})";
return ((grp-_59)>0)?"(?:"+_5a+"|(?:0|[1-9]\\d{0,"+(grp-1)+"}))":_5a;
}
return "(?:0|[1-9]\\d{0,"+(grp-1)+"}(?:["+sep+"]\\d{"+grp+"})*)";
},true);
return _54+_56;
};
}
