﻿function counter(id,initial,rate,tofixed,start,end,time) {
	// start, end, and time should be in seconds
	// if applicable, initial should also be in seconds
	this.element = document.getElementById(id);
	this.current = initial;
	this.rate = rate;
	this.tofixed = tofixed
	this.start = start;
	this.end = end;
	this.time = time;

	this.format = function(number) {
		number = number.toFixed(this.tofixed);
		number += '';
		x = number.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;
	}

	this.element.innerHTML = this.format(this.current);


}

counter.prototype.update = function(interval) {
	this.time += interval*.001;
	if (this.time>=86400) {
		this.time -= 86400;
	}
	if ( (this.time>=this.start)&&(this.time<=this.end) ) {
		this.current += this.rate*interval*.001;
	}
	this.element.innerHTML = this.format(this.current);
	var thisobj = this;
	setTimeout(function() { thisobj.update(interval) },interval)
}