// global variables.
var g_start_date, g_end_date, g_end, g_start, g_increase;

function start() {
	repeat();
}

function repeat() {
	var d_now = new Date(); 
	document.getElementById('counter').innerHTML = addCommas(g_start + Math.ceil(((d_now.getTime()-g_start_date.getTime()) * g_increase)));
	setTimeout("repeat()",1500);
}

function setupCO2Counter(start_date,end_date,start_gal,end_gal) {
	// starting and ending gallons.
	g_start = start_gal.toString().replace(/\$|\,/g,'') * 1;
	g_end = end_gal.toString().replace(/\$|\,/g,'') * 1;

	// set the starting date and the ending date.
	g_start_date = new Date(start_date);
	g_end_date = new Date(end_date);

	// calculate the number of gallons per millisecond.
	g_increase = (g_end - g_start) / (g_end_date.getTime()-g_start_date.getTime());

	var d_now = new Date();
	document.getElementById('counter').innerHTML = addCommas(g_start + Math.ceil(((d_now.getTime()-g_start_date.getTime()) * g_increase)));
}

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');
	}
	return x1 + x2;
}