// Welcome Visitors: I have tried to make the JavaScript here inviting
// so that you can read it and point out any mistakes I may have made
// in the calculation.  Feel free to e-mail me at niko@alum.mit.edu 
// if something is confusing or you think you have found a mistake.

// ===========================================================================
// Number Display and Updater

// This function modifies the global variable 'curamount' 
// to contain the total number of dollars spent on the war so far given the 
// estimates described in numbers.html.  
var totalms       = 21557600000;
var initialdollars= 0;
var totaldollars  = 4520000000 - initialdollars;
var rateperms     = totaldollars / totalms;
thisyear			= new Date ();

var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var now = new Date();

// dateStr = Date();
// dateStr = monthNames[now.getMonth()]+ " " +now.getDate()+", "+now.getFullYear()+" 15:41:03";
// var startofspend = new Date (dateStr);

 dateStr = "April 20, "+thisyear.getFullYear()+" 22:00:00";
 var startofspend = new Date (dateStr);


function calc_amount ()
{
	var curdate = new Date ();
	var diff = curdate - startofspend;
	curamount = (initialdollars + diff * rateperms);
}

function number_str (n)
{
	var x = n.toString ();
	var dot = x.lastIndexOf ('.');
	x = x.substr (0, dot);
	var l = x.length;
	var res = "";
	for (l -= 3; l > 0; l -= 3)
	{
		res = "," + x.substr (l, 3) + res;
	}
	res = x.substr (0, l+3) + res;
	return res;
}

function inc_totals_at_rate(rate)
{
	calc_amount ();
	document.getElementById ("raw").innerHTML = "$" + number_str(curamount);
	setTimeout('inc_totals_at_rate('+rate+');', rate);
}

function inc_totals ()
{
	inc_totals_at_rate (50);
}
