Object.extend(Number.prototype, {
    toSiteString: function(precision){

        var ret=new Number(this).toFixed(precision || Mx.precision);

        var strs=ret.toString().split('.');
        var strs_base=this.toString().split('.');
        var int=strs[0], float=!strs_base[1]?undefined:strs[1];
        var ints='', first_digits='';
        var first_digit_count=int.length%3;

        if(int.length>=3 && parseInt(int)>0){
            if(first_digit_count!=0){
                first_digits=int.substring(0,first_digit_count);
                int=int.substring(first_digit_count,int.length);
            }
            var c=0;
            int.scan(/\d{3}/, function(matched){
                ints+=(c>0?((LC_LANG=='ru')?' ':','):'')+matched[0]; c++;
            });
        }
        if ( !ints ) ints=int;

        return first_digits+(first_digits?((LC_LANG=='ru')?' ':','):'')+ ints + (float?((LC_LANG=='ru'?',':'.')+float):'') ;
    },
    toSiteString2: function(precision){

        var ret=new Number(this).toFixed(precision || Mx.precision);

        var strs=ret.toString().split('.');
        var strs_base=this.toString().split('.');
        var int=strs[0], float=!strs_base[1]?undefined:strs[1];
        var ints='', first_digits='';
        var first_digit_count=int.length%3;

        if(int.length>=3 && parseInt(int>0)){
            if(first_digit_count!=0){
                first_digits=int.substring(0,first_digit_count);
                int=int.substring(first_digit_count,int.length);
            }
            var c=0;
            int.scan(/\d{3}/, function(matched){
                ints+=(c>0?((LC_LANG=='ru')?' ':','):'')+matched[0]; c++;
            });
        }
        else
            ints=int;

        return first_digits+(first_digits?((LC_LANG=='ru')?' ':','):'')+ ints + (float?((LC_LANG=='ru'?',':'.')+float):'');
    }
});

Date.prototype.toLocaleDateString = function (options) {
    if(!options) options=$H({year: true});
//    if (LC_LANG=='ru'){
        var year=(options.year)?('.' + this.getFullYear().toPaddedString(4)):'';
        return (
            this.getDate().toPaddedString(2) + '.' +
           (this.getMonth()+1).toPaddedString(2)  + year

         );
/*    }
    else{
        var year=(options.year)?(this.getFullYear().toPaddedString(4)+'-'):'';
        return (
            year +
           (this.getMonth()+1).toPaddedString(2) + '-' +
            this.getDate().toPaddedString(2)
        );
    }*/
}

Object.extend(Date.prototype, {

    monthNames: (LC_LANG=='ru')?
	['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря']:
	['January','February','March','April','May','June','July','August','Septemper','October','November','December'],

    fromDbString: function(date_time_str){
        /* convert from sortable date-time format to JS */
        var utc=[this.getFullYear(),this.getMonth(),this.getDate(),this.getHours(),this.getMinutes(),this.getSeconds()];
        var i=0;
        new String(date_time_str).scan(
            /\d+/,
            function(matched){
                utc[i++]=matched[0];
            }
        );
        this.setTime(new Date(utc[0],new Number(utc[1])-1,utc[2],utc[3],utc[4],utc[5]).getTime());
        return this;
    },

    toSiteString: function (options) {
        options=$H(options);
        var time_string=this.toLocaleTimeString(options);
        if(options){
            time_string='';
            if(options.hours){
                time_string=this.getHours().toPaddedString(2);
                if(options.minutes){
                   time_string+=':'+this.getMinutes().toPaddedString(2);
                   if(options.seconds)
                       time_string+=':'+this.getSeconds().toPaddedString(2);
                }
            }
        }
        if(time_string=='00:00' || time_string=='00:00:00')
            time_string=''
        return this.toLocaleDateString(options) + (time_string?' ':'') + time_string;
    },

    toSiteDateString: function (options) {
        return this.toLocaleDateString(options);
    },

    toFileNameString: function () {
        return (
                this.getFullYear().toPaddedString(4) + '_' +
                (this.getMonth()+1).toPaddedString(2) + '_' +
                this.getDate().toPaddedString(2)
                );
    }

});

Date.getTimeSpanById=function(id){
    var span=undefined;
    $A(Date.timeSpans).each(function(e){
        if(e.id==id) return (span=e);
    });
    return span;
}

Date.prototype.toLocaleString = function () {
    return this.toLocaleDateString() + ' ' + this.toLocaleTimeString();
}
