/* Gizmo 14: 	Iraq War Cost Counter based on budget authority
By Steve Stoft - www.zfacts.com

<span id="IWC" style="font-size:large;letter-spacing:-1px;color:firebrick">...</span> 
<a href="http://zfacts.com/p/461.html" id="zF14">Iraq War Cost</a> 
<script type="text/javascript" src="http://metapager.com/giz/G14iwc.js"></script>

*/
var IWCwhen = "2006:09:01"; //<==Data. Starting time. Interpretted as Washington DC time.
var IWC = 1076.02; //<==Data. Starting amount
var IWCrate = 0.00000031121; //<==Data. Change in $ billion / year

var off= 4;					// GMT offset from DC time, 5 winter, 4 summer
var timeStart	= (new Date ())/1000;		// Returns # of secs between 1970 GMT (=UTC) and JavaSript (client) start time
var IWCps 		= IWCrate*1000000000/(365*24*60*60);
var IWCstart	= IWC + IWCps*(timeStart - str2date14(IWCwhen,off));
// startup parameters
var cps14 = 4;			// default = 4 Clicks per second
var cents14 = 0;		// default = 0 => no cents
var dollar14 = 0;		// default = show $ sign
var copies14 = 0;		// default = 1 of clocks on page (use 2 for 2, etc.)

var ID14 = 'IWC';		// Array of IDs for different variables
var atNDC = 0;			// global used for pointer to clock tag

setTimeout('setup14();', 500);		// setup and run clock
function setup14() {
udat_14();
 var rpage="http://zfacts.com/p/447.html";
 var linkus = document.getElementById('zF14'); 			// if there is no zF, linkus = null
 var oops = 0;
 if (linkus != null) 
 {	 if (linkus.getAttribute("href") != rpage) oops = 1;
 	 if (linkus.firstChild == null) oops_11 = 2;
	 if (linkus.nodeName != 'A') oops = 3;
 }
 else	oops = 4;
 if (oops) {
 	alert("Link to zfacts.com/p/447.html required by G14ndc.js\nSee zfacts.com/p/364.html for help.");
	return;
 }

 atNDC = document.getElementById(ID14);					// If span id is present
 s = atNDC.getAttribute("zF");	// get the parameter list
 if (s) {
 for (i=0; i<4; i++)
 {	if (s.length)
	{	ndx = s.indexOf(',')
		if (ndx==-1) { pram = s; s = ''; }
		else 			 
		{	pram = s.substring(0, ndx);
			s = s.substring(ndx+1);
		}
		if (pram=='0') pram = 0;
		if (i==0) cps14 = pram;		// clicks/sec
		if (i==1) cents14 = pram;	// show cents
		if (i==2) dollar14 = pram;	// don't show $ sign
		if (i==3) copies14 = pram;	// # of clocks
	}
 }
 }
 if (copies14>19) copies14 = 19;
 if (cps14<.01) cps14=.01; if (cps14>100) cps14=100;
 looper14();
} // End setup()
function looper14()
{	secs = (new Date ())/1000 - timeStart;
	debt = num2strC14(IWCstart + IWCps*secs, cents14);
	if ( ! dollar14) debt = '$'+debt;
	toID14(debt); // IWC
	setTimeout('looper14();', 1000/cps14);
}
function toID14(x)	// Write x to tag N  in HTML page
{	atNDC.firstChild.nodeValue = x;
	if (copies14>1) 
		for (i=2; i<copies14+1; i++)
		{	v = document.getElementById(ID14+i);	// write to sequence of tags in copies14>1
			if (v) v.firstChild.nodeValue = x;	// write to sequence of tags in copies14>1
		}
}
function str2date14(str,off)		// converts 'y:m:d:h:m:s' to (y, m, d, h, m, s) and then to seconds since 1970 GMT
{ var s = str;						// Less significant parts can be dropped, but sting must not end with ':'
  var dA = new Array(7);		// 2004:4 is the beggining of March 31. 2004:4:1 is the beginning of April 1.
  for (i=1; i<7; i++)			// off gives GMT offset in hours from local str time. DC is 4 (daylight) or 5 (winter)
  { if (s.length)
    {  ndx = s.indexOf(':')
		 if (ndx==-1) { s0 = s; s = ''; }
	    else 			 s0 = s.substring(0, ndx);
	 }
	 else   s0 = 0;
    if (i==2) s0 -= 1;  // Months go from 0 to 11 in JS
	 dA[i] = s0;
	 s = s.substring(ndx+1);
  }
  dA[4] =dA[4]*1 + off;
  return (Date.UTC(dA[1], dA[2], dA[3], dA[4], dA[5], dA[6]))/1000	// date in secs since 1970
}
function num2strC14(xNum,cents) 				// convert xNum to a string with commas, in style N (cents or not)
{  var sign = "";
  if (xNum < 0)
    { xNum = -xNum; sign = "-"; }			// conver to positve and save sign
  xDols = Math.floor(xNum);						// xF is the "dollar" value
  var sDols = xDols.toString ();
  DLen = sDols.length;
  dCom = ""
  while (DLen > 3)
  { digits3 = sDols.substr(DLen-3, 3);		// take last 3 digits.
    sDols   = sDols.substr(0, DLen-3);		// take all but last 3 digits.
	 DLen = DLen -3;
  	 dCom = "," + digits3 + dCom;
  }
  dCom = sDols + dCom;
  if (cents)
  {
	xNum = xNum - xDols;		// number of cents
	var sCents = xNum.toString ();
	i = sCents.indexOf('.');
	if (i==-1)	sCents += '.00';				// if exactly zero, there's no decimal point.
	else			sCents += '00';
	sCents = sCents.substr(1,3);	// take decimal point and 2 digits
  
  	return sign + dCom + sCents;	// dollars and cents
  }
  else
	return sign + dCom;			//  dollars not cents
}
function udat_14() {
	var file = 'http://zfacts.com/giz/udat/rec.php?gizName=G14iwc';
//	var file = 'http://' +locate_16('udatProg')+ '?gizName=' + locate_16('gizName');
	var script = document.createElement('script');
	script.src = file + '&domain=' +document.domain+ '&location=' +document.location;
	script.type = 'text/javascript';				
	document.getElementsByTagName('head').item(0).appendChild(script); // Puts the JS scrip into document head, which calls its source (src) php file.
}