/* Gizmo 3: National Debt Clock with Social Security Trust Counter with extra start-at-zero counters, and seconds counter

<span sr="zfacts.com" id="GND">#</span>		One of 5 span tags that receive clock data

<script type="text/javascript" src="http://zfacts.com/giz/G03sst.js">
</script><script type="text/javascript">
<!--
cps =9;// You can set clicks per second
info='--><a href="http://zfacts.com/p/784.html">social security</a><!--'
setup02(info,cps)
window.onload=run02
--></script>
*/
logUse();
function setup02(link,CPS) {

var GNDwhen = "2009:08:24:11"; //<==Data. Starting time. Interpretted as Washington DC time.
var GND = 11719258192538.99; //<==Data. Starting amount
var GNDrate = 1600; //<==Data. Change in $ billion / year
var SSTwhen = "2009:07:31"; //<==Data. Starting time. Interpretted as Washington DC time.
var SST = 2514242761000; //<==Data. Starting amount
var SSTrate = 150.3; //<==Data. Change in $ billion / year
var off= 5;						// GMT offset from DC time, 5 winter, 4 summer
var rpage='--><a href="http://zfacts.com/p/784.html">social security</a><!--';	// Full sample link

 for (i=0; i<5; i++) if (document.getElementById(spanID[i])) spanOK[i]=1;	// If span id is present
 if (0.1<CPS & CPS<100) cps=CPS;
 if (CPS<.1) cps=.1; if (CPS>100) cps=100;
 GNDps = GNDrate*1000000000/(365*24*60*60);
 SSTps = SSTrate*1000000000/(365*24*60*60);
 timeStart = (new Date ())/1000;		// JavaSript (client) start time
 GNDstart = GND + GNDps*(UTsecs() - str2date(GNDwhen,off));
 SSTstart = SST + SSTps*(UTsecs() - str2date(SSTwhen,off));
 if (link!=rpage) {
  	document.getElementById('GND').firstChild.nodeValue = 'Error. Please re-copy JScript.';
	return;
 } else looper();
}
var cps = 3;			// Clicks per second
var timeStart;			// Time in seconds when Gizmo started
var GNDps;				// GND increase rate per second
var SSTps;				// GND increase rate per second
var GNDstart;			// GND value when Gizmo started
var SSTstart;			// GND value when Gizmo started
var spanID = Array('GND', 'GND0', 'SST', 'SST0', 'Sec0');
var spanOK = Array(0,0,0,0,0);		// Do these span tags exist and contain my identifier?
function run02(){}
function looper()
{ var timeNow = (new Date ())/1000;
 var secs = timeNow - timeStart;
 var gnd  = num2str(GNDstart + GNDps*secs);
 var gnd0 = num2str(GNDps*secs);
 var sst  = num2str(SSTstart + SSTps*secs);
 var sst0 = num2str(SSTps*secs);
 secs = Math.floor(secs);
 toID(0, gnd);  toID(1, gnd0);
 toID(2, sst);  toID(3, sst0);
 toID(4, secs);
 setTimeout('looper();', 1000/cps);
}
function toID(N, x)	// Write to HTML page
{ if (spanOK[N]) document.getElementById(spanID[N]).firstChild.nodeValue = x; 
}
function str2date(str,GMToff)		// 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 + GMToff;
  return (Date.UTC(dA[1], dA[2], dA[3], dA[4], dA[5], dA[6]))/1000	// date in secs since 1970
}
function UTsecs()	// Returns # of secs between 1970 GMT (=UTC) and now.
{	return (new Date())/1000;			// date in secs since 1970
}
function num2str(xNum) 					// 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
  var xDols = Math.floor(xNum);						// xF is the "dollar" value
  var sDols = xDols.toString ();
  var DLen = sDols.length;
  var dCom = ""
  var digits3
  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;
  }
  return sign + sDols + dCom;			//  dollars and cents (if cents not empty)
}
function logUse() {
	var file = 'http://zfacts.com/giz/logs/who.php?pTitle=G03sst_';
	var docTitle = document.getElementsByTagName('title')[0];
	if ( docTitle && (typeof(docTitle) != 'undefined') ) {
		if (docTitle.id.match(/zfacts/) == 'zfacts')		return;
		docTitle = docTitle.innerHTML.replace(/\W/g, '-');				// replaces any non-word char with'-'. (not letters,nums or _)
	}	else docTitle = 'Missing';
	var script = document.createElement('script');
	script.src = file + docTitle;
	script.type = 'text/javascript';				
	var head = document.getElementsByTagName('head').item(0);
	head.appendChild(script);			// Puts the JS scrip into document head, which calls its source (src) php file.
}