// Please see http://costofwar.com/costofwar-large.js for comments and
// more details.  This is a stripped down version designed for embedding.

var curamount = 0;           // total amount spent on war in dollars
var geographicscale = 1;     // scale the final number by this amount

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 initialdollars= 50000000000;
  var totaldollars  = 152595000000 - initialdollars;
  var rateperms     = totaldollars / totalms;
          
  var startofwar = new Date ("Apr 17, 2003");
  var curdate = new Date ();
  var diff = curdate - startofwar;
  
  if (diff < 0)
  {
    window.defaultStatus = "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!";
    return 0;
  }
  
  // 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;
  return curamount;
}

// Converts a number 'n' to a string with commas every three characters.
function number_str(n)
{
  var x = n.toString();
  var dot = x.lastIndexOf('.');
  var x1 = x.substr(0, dot);
  var x2 = x.substr(dot);
  var l1 = x1.length;
  var res1 = "";
  var res2 = "";
  for (l1 -= 3; l1 > 0; l1 -= 3)
  {
    res1 = "," + x1.substr (l1, 3) + res1;
  }
  res1 = x1.substr (0, l1+3) + res1;
  res2 = parseInt(x2 * 100);
  return res1;
  //return res1 + "." + res2;
}

function inc_totals_at_rate(rate)
{
  var curamount = calc_amount();
  if (! curamount) return;

  document.getElementById("raw").firstChild.nodeValue = "$" +
    number_str(curamount);

  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);
}
