// JavaScript Document
<!-- 

// 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 () {
  // 'totalms' is the number of milliseconds between Apr 17, 2003 (at which 
  // point $50 billion had been spent) and Sep 30, 2004 (at which point 
  // we'll have spent $135 billion).
  //
  // We just accrue the amount linearly for now.  Close enough for government
  // work, as they say.
  //var totalms       = 53825644800;
  //var totalms       = 93139200000;
//below value is number of milliseconds between april 17 2003 and september 30 2006-------------------------
  var totalms       = 743215511111;
  var initialdollars= 0;
  var totaldollars  = 1000000000 - initialdollars;
  var rateperms     = totaldollars / totalms;
//----------------------------------------------------------------------------------------------------------          
  var startofwar = new Date ("Feb 28, 2001");
//----------------------------------------------------------------------------------------------------------
  var curdate = new Date ();
  var diff = curdate - startofwar;
  
  if (diff < 0) {
    alert ("costofwar.com uses your computers date to calculate the cost. "+
           "Yours must be wrong because according to your computer the war "+
           "hasn't even started yet!");
  }
  
  // Do the actual calculations and get the amount before and after interest.
  // Note that we include the geographicscale as well.
  curamount = (initialdollars + diff * rateperms) * 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);
  //firefox fix of original just above -----------------------------------------------------
  //document.getElementById ("raw").innerHTML =
  //" " + 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);

}
function inc_totals_at_rate2(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);
}

