

// Constructor:
// id     div ID name
// start  starting number
// rate   increment per second
// delay  update interval in milliseconds
function Counter(id, start, rate, delay)
{
    var div = document.getElementById(id);
    var starttime = new Date();
    var update = function()
    {
        var now = new Date();
        var timespan = (now.valueOf() - starttime.valueOf())/1000;
        div.innerHTML = Math.round(start+(timespan*rate));
        setTimeout(update, delay);
    }
    update();
    setTimeout(update, delay);
}

// Constructor:
// id     div ID name
// start  starting number
// rate   increment per second
// delay  update interval (changed to divide by 50 to speed up odometer)
function CounterWithMax(id, start, end, rate, delay)
{
    var div = document.getElementById(id);
    var starttime = new Date();
    var curNum = 0
    var update = function()
    {
        var now = new Date();
        var timespan = (now.valueOf() - starttime.valueOf())/50;
	curNum = Math.round(start+(timespan*rate));
        
	//When end is reached, exit 
	if(curNum >= end){
	    div.innerHTML = formatCurrency(end,false);
	    return;
	}else{
	    div.innerHTML = formatCurrency(curNum,false);
            setTimeout(update, delay);
	}
    }
    update();
    setTimeout(update, delay);
}

function formatCurrency(strValue,doCents)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	if(doCents)
	    return (((blnSign)?'':'-') + dblValue + '.' + strCents);
	else
	    return (((blnSign)?'':'-') + dblValue);
}

