var costOfAf=0;
var costOfIraq=0;
var costOfTotal=0;
var geographicscale=1;

startFiscalYear = new Date('Sat Oct  1 00:00:00 2011'); 		// start of current fiscal year

function calculateIraq () {
 	 var previouslyAllocated = 797310713001;		 // amount previously allocated for all prior years combined
	 var ratePerMS = 0.318675948653;
	 var curDate = new Date();				// today's date
	 var diff = curDate - startFiscalYear;			// MS between today and start of fiscal year
	 costOfIraq = (previouslyAllocated + (diff * ratePerMS)) * geographicscale;    // cost of Iraq war at this time
}

function calculateAf () {
	var previouslyAllocated = 459786487495;		// amount previously allocated for all prior years combined
  	var ratePerMS = 3.51199622774;		// the rate per MS of the war for current fiscal year
	var curDate = new Date();				// today's date
	var diff = curDate - startFiscalYear;			// MS between today and start of fiscal year
	costOfAf = (previouslyAllocated + (diff * ratePerMS)) * geographicscale;    // cost of Afghanistan war at this time
}






//updates cost of Afghanistan war
function updateCostOfAf(rate) {
  calculateAf();
  costOfWarAf_Total = document.getElementById("costOfWarAf_Total");
  costOfWarAf_Total.innerHTML = "$" + number_str(costOfAf);
  setTimeout('updateCostOfAf('+rate+');', rate);
 }

//updates cost of Iraq war
function updateCostOfIraq(rate) {
  calculateIraq();
  //costOfWarIraq_Total = document.getElementById("costOfWarIraq_Total");
  costOfWarIraq_Total.innerHTML = "$" + number_str(costOfIraq);
  setTimeout('updateCostOfIraq('+rate+');', rate);
}

//updates Total Cost of War 
function updateCostOfTotal(rate) {
  calculateAf();    // find the totals of the others first
  calculateIraq();  
  costOfTotal=(costOfAf + costOfIraq);

  var costOfWarTotal_Total = document.getElementById("costOfWarTotal_Total");
  costOfWarTotal_Total.innerHTML = "$" + number_str(costOfTotal);
  setTimeout('updateCostOfTotal(' + rate + ');', rate);
}



function initCostOfIraq() {
   var costOfWarIraq = document.getElementById("costOfWarIraq");  	//find our costOfWarIraq div
   costOfWarIraq_Title = document.createElement('div');  		//create costOfWarIraq_Title
   costOfWarIraq_Title.setAttribute('id','costOfWarIraq_Title'); 
   costOfWarIraq_Total = document.createElement('div'); 		//create costOfWarIraq_Total
   costOfWarIraq_Total.setAttribute('id','costOfWarIraq_Total');
   costOfWarIraq_Link = document.createElement('div');		//create costOfWarIraq_Link
   costOfWarIraq_Link.setAttribute('id','costOfWarIraq_Link');
  
  costOfWarIraq.appendChild(costOfWarIraq_Title);		//append all three to our parent
  costOfWarIraq.appendChild(costOfWarIraq_Total);
  costOfWarIraq.appendChild(costOfWarIraq_Link);
  
  costOfWarIraq_Title.innerHTML = 'Cost of War in Iraq';    	// set the title
  costOfWarIraq_Link.innerHTML = 'See the cost to your community at <a href="http://www.costofwar.com" title="CostOfWar.com brought to you by National Priorities Project" target="_blank">www.costofwar.com</a>';
 }



function initCostOfAf() {
   var costOfWarAf = document.getElementById("costOfWarAf");  	//find our costOfWarAf div
   costOfWarAf_Title = document.createElement('div');  		//create costOfWarAfTitle
   costOfWarAf_Title.setAttribute('id','costOfWarAf_Title'); 
   costOfWarAf_Total = document.createElement('div'); 		//create costOfWarAfTotal
   costOfWarAf_Total.setAttribute('id','costOfWarAf_Total');
   costOfWarAf_Link = document.createElement('div');		//create costOfWarAfLink
   costOfWarAf_Link.setAttribute('id','costOfWarAf_Link');
  
  costOfWarAf.appendChild(costOfWarAf_Title);		//append all three to our parent
  costOfWarAf.appendChild(costOfWarAf_Total);
  costOfWarAf.appendChild(costOfWarAf_Link);
  
  costOfWarAf_Title.innerHTML = 'Cost of War in Afghanistan';    	// set the title
  costOfWarAf_Link.innerHTML = 'See the cost to your community at <a href="http://www.costofwar.com" title="CostOfWar.com brought to you by National Priorities Project" target="_blank">www.costofwar.com</a>';
 }


function initCostOfTotal() {
   var costOfWarTotal = document.getElementById("costOfWarTotal");  	//find our costOfWarTotal div
   var costOfWarTotal_Title = document.createElement('div');  		//create costOfWarTotalTitle
   costOfWarTotal_Title.setAttribute('id','costOfWarTotal_Title'); 
   var costOfWarTotal_Total = document.createElement('div'); 		//create costOfWarTotalTotal
   costOfWarTotal_Total.setAttribute('id','costOfWarTotal_Total');
   var costOfWarTotal_Link = document.createElement('div');		//create costOfWarTotalLink
   costOfWarTotal_Link.setAttribute('id','costOfWarTotal_Link');
  
  costOfWarTotal.appendChild(costOfWarTotal_Title);		//append all three to our parent
  costOfWarTotal.appendChild(costOfWarTotal_Total);
  costOfWarTotal.appendChild(costOfWarTotal_Link);
  
  costOfWarTotal_Title.innerHTML = 'Cost of U.S. Wars Since 2001';    	// set the title
  costOfWarTotal_Link.innerHTML = 'See the cost to your community at <a href="http://www.costofwar.com" title="CostOfWar.com brought to you by National Priorities Project" target="_blank">www.costofwar.com</a>';
 }



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





/* LEGACY FUNCTIONS 
* These functions allow backwards compatibility with the thousands of websites out
* in the wild that already link to the cost of war counter. 
*

function inc_totals_at_rate(rate) {
  calculateIraq();
  var raw = document.getElementById("raw");
  if (raw != null) {
  raw.setAttribute('id','costOfIraq');
  setTimeout('updateIraq('+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);
}

*/

