User:Al Maghi/Gadget-WarCost.js

From mediawiki.org

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Cost of War
 *
 * liveWarCost function adds a counter of the cost of war.
 *
 * Inspired with UTC Clock JavaScript gadget and Niko's JavaScript on http://costofwar.com
 *
 * Authors: Niko (niko@alum.mit.edu), fr:user:Al_Maghi
 * Last revision: Nov 21, 2009
 */
 
 
//////////////////////Start CUSTOMIZATION//////////////////////
//Customize by setting the cost and the dates of military budget.
 
var totalIraq = 687000000000; // total dollars allocated to Iraq War.
var startOfIraqWar = new Date("Mar 20, 2003"); // start of Iraq War.
var IraqBudgetedThrough = new Date("Sept 30, 2009"); // current budget goes to 
 
var totalAg  = 228000000000; // total dollars allocated to Afghanistan War.
var startOfAgWar = new Date("Oct 7, 2001"); 		// start of Afghanistan War.
var AgbudgetedThrough = new Date("Sept 30, 2009");	// current budget goes to
 
var WarCostCounterDesc="War: ";var WarCostLink="Gadgets";
var WarCostmoney="$";var separator = ",";
/////////////////End CUSTOMIZATION/////////////////
 
function liveWarCost(){
	liveWarCost.node = addPortletLink( 'p-personal', wgServer + wgScriptPath + '/index.php?title=' + WarCostLink, '', 'Gadget-WarCost' );
	liveWarCost.node.style.fontSize = 'xx-small';
	liveWarCost.node.style.fontWeight = 'bolder';
	showWarCost();
}
addOnloadHook(liveWarCost);
 
function showWarCost(){
	var warcostNode = liveWarCost.node;
	if( !warcostNode ) {return;}
 
  calculateAg();
  calculateIraq();
  calculateTotal();
 
 var costOfWar=WarCostCounterDesc+WarCostmoney+number_str(costOfTotal);
warcostNode.firstChild.replaceChild( document.createTextNode( costOfWar ), warcostNode.firstChild.firstChild );
	window.setTimeout(showWarCost, 100);
}
function calculateIraq () {
  var totalMS = IraqBudgetedThrough - startOfIraqWar;		// total MS for dollars allocated
  var ratePerMS     = totalIraq / totalMS;		// the rate per MS of the war so far
  var curDate = new Date();				// today's date
  var diff = curDate - startOfIraqWar;			// MS between today and start of the war
  costOfIraq = (diff * ratePerMS);    // cost of Iraq war at this time
}
function calculateAg () {
  var totalMS = AgbudgetedThrough - startOfAgWar;		// total MS for dollars allocated
  var ratePerMS     = totalAg / totalMS;		// the rate per MS of the war so far 
  var curDate = new Date();				// today's date
  var diff = curDate - startOfAgWar;			// MS between today and start of the war
  costOfAg = (diff * ratePerMS);      // cost of Afghanistan war at this time
}
function calculateTotal () {
  costOfTotal = (costOfAg + costOfIraq);
  if (costOfTotal <= 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!");
  }
}
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=separator+x.substr(l,3)+res;}
 res=x.substr(0,l+3)+res;return res;
}