/* 
 * More info at: http://phpjs.org
 * 
 * This is version: 2.47
 * php.js is copyright 2009 Kevin van Zonneveld.
 * 
 * Portions copyright Brett Zamir (http://brettz9.blogspot.com), Kevin van
 * Zonneveld (http://kevin.vanzonneveld.net), Onno Marsman, Michael White
 * (http://getsprink.com), Waldo Malqui Silva, Paulo Ricardo F. Santos, Jack,
 * Philip Peterson, Jonas Raoni Soares Silva (http://www.jsfromhell.com), Ates
 * Goral (http://magnetiq.com), Legaev Andrey, Martijn Wieringa, Nate,
 * Philippe Baumann, Enrique Gonzalez, Webtoolkit.info
 * (http://www.webtoolkit.info/), Ash Searle (http://hexmen.com/blog/), travc,
 * Jani Hartikainen, Carlos R. L. Rodrigues (http://www.jsfromhell.com), d3x,
 * Johnny Mast (http://www.phpvrouwen.nl), marrtins, Alex, Erkekjetter, Andrea
 * Giammarchi (http://webreflection.blogspot.com), GeekFG
 * (http://geekfg.blogspot.com), Mirek Slugen, Breaking Par Consulting Inc
 * (http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256CFB006C45F7),
 * Ole Vrijenhoek, Josh Fraser
 * (http://onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/),
 * Steven Levithan (http://blog.stevenlevithan.com), Public Domain
 * (http://www.json.org/json2.js), Marc Palau, mdsjack
 * (http://www.mdsjack.bo.it), David, Arpad Ray (mailto:arpad@php.net),
 * Pellentesque Malesuada, Thunder.m, Aman Gupta, Tyler Akins
 * (http://rumkin.com), Alfonso Jimenez (http://www.alfonsojimenez.com), Karol
 * Kowalski, AJ, Caio Ariede (http://caioariede.com), Sakimori, gorthaur,
 * Steve Hilder, john (http://www.jd-tech.net), David James, class_exists,
 * noname, Steve Clay, T. Wild, Hyam Singer
 * (http://www.impact-computing.com/), kenneth, Subhasis Deb, Pyerre, Felix
 * Geisendoerfer (http://www.debuggable.com/felix), djmix, Jon Hohle, Douglas
 * Crockford (http://javascript.crockford.com), mktime, sankai, Sanjoy Roy,
 * 0m3r, Marco, Thiago Mata (http://thiagomata.blog.com), madipta, Gilbert,
 * ger, Bryan Elliott, David Randall, Ozh, T0bsn, Tim Wiel, Peter-Paul Koch
 * (http://www.quirksmode.org/js/beat.html), Bayron Guevara, MeEtc
 * (http://yass.meetcweb.com), Brad Touesnard, XoraX (http://www.xorax.info),
 * echo is bad, J A R, duncan, Paul, Linuxworld, Marc Jansen, Der Simon
 * (http://innerdom.sourceforge.net/), Lincoln Ramsay, LH, Francesco, Slawomir
 * Kaniecki, marc andreu, Eric Nagel, Bobby Drake, rezna, Mick@el, Pierre-Luc
 * Paour, Martin Pool, Kirk Strobeck, Pul, Luke Godfrey, Christian Doebler,
 * YUI Library:
 * http://developer.yahoo.com/yui/docs/YAHOO.util.DateLocale.html, Blues at
 * http://hacks.bluesmoon.info/strftime/strftime.js, penutbutterjelly, Gabriel
 * Paderni, Blues (http://tech.bluesmoon.info/), Anton Ongson, Simon Willison
 * (http://simonwillison.net), Kristof Coomans (SCK-CEN Belgian Nucleair
 * Research Centre), Saulo Vallory, hitwork, Norman "zEh" Fuchs, sowberry,
 * Yves Sucaet, Nick Callen, ejsanders, johnrembo, dptr1988, Pedro Tainha
 * (http://www.pedrotainha.com), T.Wild, uestla, Valentina De Rosa,
 * strcasecmp, strcmp, metjay, DxGx, Alexander Ermolaev
 * (http://snippets.dzone.com/user/AlexanderErmolaev), ChaosNo1, Andreas,
 * Garagoth, Manish, Cord, Matt Bradley, Robin, FremyCompany, Tim de Koning,
 * taith, Victor, stensi, Arno, Nathan, nobbler, Mateusz "loonquawl" Zalega,
 * ReverseSyntax, Jalal Berrami, Francois, Scott Cariss, Tod Gentille, baris
 * ozdil, booeyOH, Cagri Ekin, Luke Smith (http://lucassmith.name), Ben Bryan,
 * Leslie Hoare, Andrej Pavlovic, Dino, mk.keck, Rival, Diogo Resende, Yannoo,
 * gabriel paderni, FGFEmperor, jakes, Atli ?or, Howard Yeend, Allan Jensen
 * (http://www.winternet.no), Benjamin Lupton
 * 
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL KEVIN VAN ZONNEVELD BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */ 


function number_format (number, decimals, dec_point, thousands_sep) {
    // Formats a number with grouped thousands
    //
    // version: 906.1806
    var n = number, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

