User:Al Maghi/Gadget-WarCost.js

/** * Cost of War * * GadgetWarCost 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 */ //Available datas: //var Gadget_WarCost_WhichWar="US in Afghanistan since 2001"; //var Gadget_WarCost_WhichWar="US in Iraq since 2003"; //var Gadget_WarCost_WhichWar="US in Wars since 2001";            //(default) //var Gadget_WarCost_WhichWar="French military budget since 2009"; //var Gadget_WarCost_WhichWar="my custom war budget";             //See below to customize further

//////////////////////Start CUSTOMIZATION////////////////////// // //Customize your counter by setting the cost and the dates of war budget. var TotalBudgetOfCustomWar; //=300000000; // budget militaire français 2009 - 2014 var startOfCustomWar; //=new Date("Jan 1, 2009"); var customWarBudgetedThrough; //=new Date("Dec 31, 2014"); var WarCostmoney="$"; var WarCostCounterDesc="War: "; var separator = ","; var WarCostLink="Cost_of_War&action=purge"; var WarCostCounterFontSize = 'xx-small'; var WarCostCounterFontWeight = 'bolder'; var Gadget_WarCost_WhichWar; //Options: ="US in Iraq since 2003" or ="US in Afghanistan since 2001" or ="French military budget since 2009" //Default: ="US in Wars since 2001" ////////////Datas: 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 totalFM=186000000000; //budget militaire français 2009 - 2014 var startOfFM=new Date("Jan 1, 2009"); var FMBudgetedThrough=new Date("Dec 31, 2014"); //(Style customization: Customize id #Gadget-WarCost in your custom CSS.) // /////////////////End CUSTOMIZATION///////////////// function GadgetWarCost{ GadgetWarCost.node = addPortletLink( 'p-personal', mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/index.php?title=' + WarCostLink, '', 'Gadget-WarCost' ); GadgetWarCost.node.style.fontSize = WarCostCounterFontSize; GadgetWarCost.node.style.fontWeight = WarCostCounterFontWeight; RunWarCost; } addOnloadHook(GadgetWarCost); function RunWarCost{ var warcostNode = GadgetWarCost.node; if( !warcostNode ) {return;} if ( !isset(Gadget_WarCost_WhichWar)) { Gadget_WarCost_WhichWar = "US in Wars since 2001"; } if (Gadget_WarCost_WhichWar == "US in Wars since 2001") { calculateAg; calculateIraq; calculateTotal; var OutNumberWarCost=costOfTotal; } else if (Gadget_WarCost_WhichWar == "US in Iraq since 2003") { calculateIraq; var OutNumberWarCost=costOfIraq; } else if (Gadget_WarCost_WhichWar =="US in Afghanistan since 2001") { calculateAg; var OutNumberWarCost=costOfAg; } else if (Gadget_WarCost_WhichWar =="French military budget since 2009") { calculateFrenchMilitary; WarCostmoney ="€"; var OutNumberWarCost=costOfFrenchMilitary; } else if (Gadget_WarCost_WhichWar =="my custom war budget") { calculateCustom; var OutNumberWarCost=costOfCustomWar; } else {alert("The value of parameter Gadget_WarCost_WhichWar is wrong in your custom JS.");} if (WarCostmoney=="$") { var OutTextCostOfWar=WarCostCounterDesc+WarCostmoney+number_str(OutNumberWarCost);} else { var OutTextCostOfWar=WarCostCounterDesc+number_str(OutNumberWarCost)+WarCostmoney; } warcostNode.firstChild.replaceChild( document.createTextNode( OutTextCostOfWar ), warcostNode.firstChild.firstChild ); window.setTimeout(RunWarCost, 100); } function calculateCustom{ calculateWarCost(startOfCustomWar,customWarBudgetedThrough,TotalBudgetOfCustomWar); costOfCustomWar=costOfWarAmount; } function calculateFrenchMilitary { calculateWarCost(startOfFM,FMBudgetedThrough,totalFM); costOfFrenchMilitary=costOfWarAmount; } function calculateIraq { calculateWarCost(startOfIraqWar,IraqBudgetedThrough,totalIraq); costOfIraq=costOfWarAmount; } function calculateAg { calculateWarCost(startOfAgWar,AgBudgetedThrough,totalAg); costOfAg=costOfWarAmount; } function calculateTotal { costOfTotal = (costOfAg + costOfIraq); if (costOfTotal <= 0) { alert ("Cost of War Javascript 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 calculateWarCost (startOfWar,BudgetedThrough,totalMoney) { var totalMS = BudgetedThrough - startOfWar;		// total MS for dollars allocated var ratePerMS    = totalMoney / totalMS;		// the rate per MS of the war so far var curDate = new Date;				// today's date var diff = curDate - startOfWar;			// MS between today and start of the war costOfWarAmount = diff * ratePerMS;   // cost of war at this time } 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;} function isset { var a=arguments; var l=a.length; var i=0; if (l==0) { throw new Error('Empty isset'); } while (i!=l) {  if (typeof(a[i])=='undefined' || a[i]===null) { return false; } else { i++; }	} return true; }