
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function dcountup(startingdate, baseunit){
	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 result={timediff: timediff}
	this.oncountup(result)
	setTimeout(function(){thisobj.start()}, 1000) //update results every second
}


//SYNTAX: myvariable=new dcountup(past_date_and_time_string, "baseunit")
var Debt=new dcountup("January 1, 2009 13:30:00", "days")

Debt.oncountup=function(result){
	//result is an object containing the current count up date/time, updated every second
	//Available properties: result["timediff"]
	var mycountainer=document.getElementById("cpcontainer")
	mycountainer.innerHTML=" <span class='dcountstyle'>"+formatCurrency(33.225*result['timediff'])+" </span>"
}

