/*
 *
 * Copyright (c) 2007 Tulio Faria (http://www.tuliofaria.net - http://www.iwtech.com.br)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Version 1.0
 * Demo: http://www.tuliofaria.net/jquery-floatnumber/
 *
 * $LastChangedDate$
 * $Rev$
 */
(function($) {

    //Main Method
    $.fn.floatnumber = function(separator, precision) {
        return this.each(function() {
            var input = $(this);
            var valid = false;

            function blur() {
                var re = new RegExp(",", "g");
                s = input.val();


                if (s != "") {

                    s = s.replace(re, ".");

                    if (!isNaN(s)) {
                        n = parseFloat(s);
                            
                        //DA. 23/03/2009 - Use Math.round(number) if precision is 0, as we shouldnt use .toFixed(0) for rounding in this way, mostly for IE6/7.
                        if (precision == 0) {
                            s = Math.round(n).toString();
                        }
                        else {
                            s = n.toFixed(precision);
                        }

                        re2 = new RegExp("\\.", "g");
                        s = s.replace(re2, separator);

                        input.val(s);

                    }
                }
            }
            input.bind("blur", blur);
            blur();
        });
    };
})(jQuery);
