<!--

// 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.

// Set up some global variables:
var curamount=0;  	   // total amount spent on war in dollars
var curscale=0;   	   // number of dollars per alternate scale
var curunit="";   	   // name of the alternate scale
var geographicscale=1; // scale the final number by this amount

// ===========================================================================
// Custom Units (like "public housing")

// This function is called from the links below that change the unit.
// It updates the global variables above, which are in turn read by
// the function inc_totals() below which actually modifies the content
// of the web page that you see.
function set_unit (altscale, altunit) {
  curscale = altscale;
  curunit = altunit;
}

// ===========================================================================
// 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.
function calc_amount () {
  startofwar = new Date ("Jan 1, 2010"); // see numbers.html for explanation
  curdate = new Date ();
  diff = curdate - startofwar;

  if (diff < 0) {
    alert ("whereisthemoney.com uses your computer's date to calculate the cost. "+
           "Yours must be wrong because according to your computer 2003 "+
	   "hasn't even started yet!");
  }

  // Define some constants:
  var millisperyear = 32140800000;                   // 12*30*24*60*60*1000
  var wastepermilli = 1100000000000 / millisperyear; // 1.1 trillion over 2000

  // Do the actual calculations.
  curamount = diff * wastepermilli;

  // Finally apply any geographic scaling.
  curamount = curamount * geographicscale;
}

// Converts a number 'n' to a string with commas every three characters.
function number_str (n) {
  //var x = n.toFixed (0);  this doesn't seem to work on some browsers
  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;
}

// This function actually modifies the web page --- first it determines
// the total dollars spent on the war via calc_amount(), then it modifies
// the two items in the web page, which are identified by their id ('raw'
// and 'alt').  It then sets itself up to be called again in 200 milliseconds
// so as to continuously update the page.
function inc_totals_at_rate(rate) {

  // first calculate total dollars spent.
  calc_amount ();

  // redisplay the raw total
  document.getElementById ("raw").firstChild.nodeValue =
  "$" + number_str(curamount);

  // convert units to the scale selected by the user, if any, and display.
  try {
    if (curscale != 0) {
  	    var altamount = curamount / curscale;
  	    document.getElementById ("alt").firstChild.nodeValue =
  	    number_str(altamount) + curunit;
    }
    else {
  	    document.getElementById ("alt").firstChild.nodeValue = "";
    }
  } catch (e)
  {
     // ignore if there is no 'alt' element
  }

  // run this function again in 100 milliseconds
  setTimeout('inc_totals_at_rate('+rate+');', rate);

}

// For backwards compatibility, this function will cause the totals to
// increment at a rate of 100ms.
function inc_totals ()
{
  inc_totals_at_rate (100);
}

// ===========================================================================
// Geographic Scaling

var geodata = new Array("Alabama", 0.008657563,
"Alaska", 0.001523673,
"Arizona", 0.012408762,
"Arizona", 0.009049383,
"California", 0.126128707,
"Colorado", 0.017177407,
"Connecticut", 0.020526634,
"Delaware", 0.00484063,
"Florida", 0.047519741,
"Georgia", 0.027983359,
"Hawai'i", 0.003185388,
"Idaho", 0.003838789,
"Illinois", 0.053818304,
"Indiana", 0.015986756,
"Iowa",	0.007101802,
"Kansas", 0.008760253,
"Kentucky", 0.008462535,
"Louisiana", 0.009151404,
"Maine", 0.002732366,
"Maryland", 0.028788176,
"Massachusetts", 0.033117986,
"Michigan", 0.033104576,
"Minnesota", 0.027270735,
"Mississippi", 0.004436978,
"Missouri", 0.020013174,
"Montana", 0.001608851,
"Nebraska", 0.006247129,
"Nevada", 0.005856702,
"New Hampshire", 0.003903256,
"New Jersey", 0.044934607,
"New Mexico", 0.003064247,
"New York", 0.093059104,
"North Carolina", 0.022347956,
"North Dakota", 0.001401179,
"Ohio", 0.040633067,
"Oklahoma", 0.010054049,
"Oregon", 0.009448752,
"Pennsylvania",	0.041596131,
"Rhode Island",	0.003664824,
"South Carolina", 0.00778348,
"South Dakota",	0.001881503,
"Tennessee", 0.017391128,
"Texas", 0.076750781,
"Utah", 0.004630549,
"Vermont", 0.001609875,
"Virginia", 0.024464663,
"Washington", 0.021156614,
"West Virginia", 0.002426541,
"Wisconsin", 0.017090208,
"Wyoming", 0.00140972
);


// There are subpages of costofwar.com that display the cost of war just
// for individual parts of the country.  For example, Montgomery County, MD
// plays .0039 of the total taxes for the war.  Setting this value allows
// us to scale the numbers to account for that.
function set_custom_geographic (geoscale) {
  geographicscale = geoscale;
}

// Called on the change of a pulldown which selects a locale.  Just calls
// set_custom_geographic with the right scaling value.  The pulldown
// should have had its values created by fill_pulldown() below.
function choose_locale (pulldown) {
  set_custom_geographic (pulldown.options[pulldown.selectedIndex].value);
}

// This function sets the options for the pulldown menu: the names are the
// names from the list above, and the value is the geographic scale.
function fill_pulldown (slider) {
  var opts = slider.options;
  opts.length =  0; // remove all existing options
  opts[0] = new Option ("Entire US", 1, true, true);
  for (var i = 0; i < geodata.length; i += 2) {
    opts[opts.length] = new Option (geodata[i],
					  			    geodata[i+1],
									false,
									false);
  }
}


// -->

