function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	/*while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}*/
	while (rgx.test(x1)) {
	    x1 = x1.replace(rgx, '$1' + ' ' + '$2');
	}
	return x1 + x2;
	//var newX = x1.split('.');
	//alert(newX[0]);
	//return newX[0] ;
}

function dcountup(startingdate, baseunit){
    //
    this.OriginalValue = 38756685452; //387,566,854,52;
    this.SavingsPerSecond = 1157;    
    //alert(this.SavingsPerSecond * 86400 * 31);
    startingdate = "November 1, 2008 00:01:00"
	this.currentTime=new Date()
	this.startingdate=new Date(startingdate)
	this.timesup=false
	this.baseunit=baseunit
	this.start()
}

dcountup.prototype.oncountup=function(){} //default action for "oncountup"

dcountup.prototype.start = function() {
    var thisobj = this
    this.currentTime.setSeconds(this.currentTime.getSeconds() + 1)
    var timediff = (this.currentTime - this.startingdate) / 1000 //difference btw target date and current date, in seconds
    var oneMinute = 60 //minute unit in seconds
    var oneHour = 60 * 60 //hour unit in seconds
    var oneDay = 60 * 60 * 24//day unit in seconds
    //var dayfield=Math.floor((timediff/oneDay)*814480)

    var dayfield = Math.floor(timediff * this.SavingsPerSecond);

    var hourfield = Math.floor((timediff - dayfield * oneDay) / oneHour)
    var minutefield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour) / oneMinute)
    var secondfield = Math.floor((timediff - dayfield * oneDay - hourfield * oneHour - minutefield * oneMinute))
    if (this.baseunit == "hours") { //if base unit is hours, set "hourfield" to be topmost level
        hourfield = dayfield * 24 + hourfield
        dayfield = "n/a"
    }
    else if (this.baseunit == "minutes") { //if base unit is minutes, set "minutefield" to be topmost level
        minutefield = dayfield * 24 * 60 + hourfield * 60 + minutefield
        dayfield = hourfield = "n/a"
    }
    else if (this.baseunit == "seconds") { //if base unit is seconds, set "secondfield" to be topmost level
        var secondfield = timediff
        dayfield = hourfield = minutefield = "n/a"
    }
    //alert(dayfield)
    dayfield = dayfield + this.OriginalValue;
    dayfield = Math.floor(dayfield / 100)
    //alert(dayfield)
    var newday, deci
    var rand_no = Math.random();
    var rand_no2 = Math.random();
    rand_no = rand_no * 100
    rand_no = Math.floor(rand_no)
    if (rand_no < 10) {
        deci = addCommas(rand_no) + "0"
    }
    else {
        deci = addCommas(rand_no)
    }
    //alert(addCommas(dayfield))
    //dayfield = dayfield * 1000;

    newday = addCommas(dayfield);// +"." + deci
    //newday = addCommas(dayfield)
    var result = { days: newday, hours: hourfield, minutes: minutefield, seconds: secondfield }
    this.oncountup(result)
    setTimeout(function() { thisobj.start() }, 1000) //update results every second
}


