/* Gizmo G10gas2		This file is the same on zFacts & metapager

<form><input id='stAbv' name='stAb' value='US'>
<span id='here'>United States</span></form>

<script type="text/javascript" src="http://zfacts.com/giz/G10gas.js">
</script><script type="text/javascript">
<!--
gasPrice(); // An alternative is to call gas1price() to get prices for 1 state only
--></script>

*/
var decimals;
var cpm = 0;	// changes per minute;
var tdate = new Date ();		// Returns # of secs between 1970 GMT (=UTC) and JavaSript (client) start time
var months = Array('January','Febuary','March','April','May','June','July','August','September','October','November','December');
var suffix = Array('st','nd','rd','th');
var nday = tdate.getDate();
var suf = suffix[Math.min(nday-1,3)];
var dates = months[tdate.getMonth()] + ' ' + nday;
var saE;
var k=0;
var oldStAb = '';
var userTouch = 0;
var lag = 0;
var change = 1;
var autoState =1;
//                 0       1     2        3         4      5       6      7         8       9 
var IDs10 = Array('date', 'th', 'stAbv', 'stName', 'reg', 'prem', 'regW', 'premW', 'regY', 'premY'); 	// tag IDs for Gizmo 10
var nID10 = 10;			// Number of IDs (different clocks)
var oops10 = 0;
var rpage10="http://zfacts.com/p/35.html";
function gas1price(inState, dec) {		// This just fills in the 6 prices once for a given State/US
	if (dec==null) decimals = 2;
	else decimals = dec;
	setOops10();
	prices = getPrices(inState);					// Get 6 prices for this state
	for ($i=1; $i<7; $i++) {						// Set prices
		price = zRound(prices[$i], decimals);
		toID10($i+3, price);
	}
}
function gasprice(inState, dec, speed) {
	if (inState==null) inState='US'
	saE = document.getElementById('stAbv');	// memorize location of state Abrev.
	oldStAb = inState.toUpperCase();	// Intialize oldStAb so no change detected till user input, and input box
	saE.value = oldStAb;	
	if (dec==2) decimals = 2;
	else	decimals = 3;
	if (speed==null) speed=0;
	cpm = Math.max(0,Math.min(60,speed));		// speed is between 0 and 60 changes per minute
	toID10(0, dates);									// put date string in place only once.
	toID10(1, suf);									// put date suffix (st,nd,rd,th) in place only once.
	setOops10();
	looper10();
}
// <input id='stAbv' name='stAb' size='3' maxlength='2'>
function looper10() {
	saE.value = stAb = clean(saE.value);									// upperCase State Abrev.
	if (oldStAb==stAb && userTouch==0 && cpm>0) {		// No change now or in the past. Changes/min > 0
		lag += 1;
		if (lag>300/cpm) {
		//	r = Math.floor(52*Math.random())+1;
			oldStAb = SGD[autoState].substr(0,2);
			autoState += 1; if (autoState>52) autoState=1;	// DC + US makae 52
			saE.value  = stAb = oldStAb;
			change=1;	lag=0;
		}
	}
	if (oldStAb!=stAb || change) {					// save computation time by doing only when StAbv changes
		if (oldStAb!=stAb) userTouch = 1;
		if (stAb=='GO') userTouch = 0;
		change = 0;											// update data, so change done
		oldStAb = stAb;
		toID10(3, getSName(stAb));						// Set State Name
		prices = getPrices(stAb);						// Get 6 prices for this state
		for ($i=1; $i<7; $i++) {						// Set prices
			price = zRound(prices[$i], decimals);
			toID10($i+3, price);
		}
	}
	setTimeout('looper10();', 200);
}
function clean(x) {
	x = x.toUpperCase();
	re = /[^A-Z]/; //new RegExp("X");
	x = x.replace(re,'');
	return x.substr(0,2);
}
function toID10(N, x)	// Write x to tag w/ ID#N HTML page
{	if (oops10) x = " zFacts.com not linked.";
	id = IDs10[N];
	v = document.getElementById(id);
	if (v) v.firstChild.nodeValue = x;
}
function getSName(stAb) {
	for (i=0; i<SGD.length; i++) {
		x = SGD[i];
		if (x.substr(0,2)==stAb) {
			ncom = x.indexOf(',');
			return x.substr(2,ncom-2);
		}
	}
	if (stAb=='US') return "the U.S.";	// This protects against endless loop if US data missing
	return getSName('US');
}
function getPrices(stAb) {					// Returns 6 prices for state, reg, prem, regWeekAgo, premW, regYearAgo, premY
	for (i=0; i<SGD.length; i++) {
		x = SGD[i];								// ith row of global containing State Gas Data
		if (x.substr(0,2)==stAb) {			// find row for this state abrev., possibly US or DC
			prices = x.split(',');
			return prices;						// array with state in 0, 1=>Reg, 2=>Prem, etc
		}
	}
	if (stAb=='US') return "---";			// This protects against endless loop if US data missing
	return getPrices('US');					// if state Abrev not found return US data.
}
function zRound(x, n) {				// Rounds x to n decimal places and pads 2.3 and 2 with zeros = 2.30, 2.00 if n=2
	d=Math.pow(10,n);
	x = Math.round(x*d)/d;
	xs = x + '0000000000';
	i = xs.indexOf('.');
	if (i==-1) {						// if a whole # there's no decimal point.
		xs = x + '.000000000';
		i = xs.indexOf('.');
	}
	return xs.substr(0,i+n+1);
}
function setOops10() {
	var link = document.getElementById('zF'); // if there is no zF, linkus = null
	if (link != null) {
		if (link.getAttribute("href") != rpage10) oops10 = 1;
		if (link.firstChild == null) oops10 = 1;
		if (link.nodeName != 'A') oops10 = 1;
	}
	else	oops10 = 1;
}
var SGD = Array(
'---DataStart',
'AKAlaska,3.428,3.835,3.411,3.816,-124.993,-137.568',
'ALAlabama,2.713,2.986,2.709,2.982,-121.22,-127.955',
'ARArkansas,2.679,3.006,2.679,3.006,-120.821,-130.034',
'AZArizona,2.787,3.075,2.748,3.031,-125.376,-136.064',
'CACalifornia,3.083,3.335,3.079,3.332,-135.761,-142.735',
'COColorado,2.686,3.001,2.674,2.989,-125.624,-132.823',
'CTConnecticut,2.922,3.254,2.914,3.245,-131.879,-139.59',
'DCWash. DC,2.887,3.167,2.858,3.134,-123.853,-132.46',
'DEDelaware,2.756,3.057,2.739,3.038,-125.641,-135.281',
'FLFlorida,2.846,3.14,2.84,3.133,-125.398,-133.695',
'GAGeorgia,2.712,3.045,2.713,3.046,-123.115,-133.228',
'HIHawaii,3.478,3.746,3.464,3.731,-140.501,-148.902',
'IAIowa,2.747,3.031,2.74,3.023,-119.301,-126.426',
'IDIdaho,2.881,3.126,2.837,3.077,-134.105,-140.696',
'ILIllinois,2.911,3.228,2.85,3.161,-126.528,-134.808',
'INIndiana,2.784,3.08,2.718,3.007,-122.526,-130.217',
'KSKansas,2.724,2.906,2.721,2.903,-121.747,-124.771',
'KYKentucky,2.756,3.089,2.74,3.071,-120.024,-129.203',
'LALouisiana,2.719,3.033,2.701,3.014,-121.049,-129.542',
'MAMassachusetts,2.748,3.068,2.74,3.058,-124.494,-131.952',
'MDMaryland,2.794,3.046,2.753,3.002,-125.697,-133.027',
'MEMaine,2.821,3.128,2.804,3.109,-127.157,-134.019',
'MIMichigan,2.82,3.106,2.734,3.011,-126.751,-134.061',
'MNMinnesota,2.801,2.99,2.795,2.982,-116.745,-123.622',
'MOMissouri,2.625,2.892,2.603,2.868,-117.745,-124.558',
'MSMississippi,2.692,2.966,2.684,2.958,-120.649,-127.518',
'MTMontana,2.828,3.091,2.786,3.045,-134.105,-141.784',
'NCN. Carolina,2.769,3.068,2.754,3.05,-124.556,-133.006',
'NDN. Dakota,2.89,3.098,2.86,3.064,-122.136,-125.732',
'NENebraska,2.847,2.992,2.829,2.973,-123.749,-124.878',
'NHNew Hampshire,2.706,3.035,2.694,3.023,-125.279,-133.587',
'NJNew Jersey,2.632,2.929,2.628,2.923,-120.941,-130.718',
'NMNew Mexico,2.78,3.09,2.777,3.086,-129.263,-137.748',
'NVNevada,2.874,3.14,2.85,3.114,-132.092,-141.919',
'NYNew York,2.942,3.214,2.929,3.2,-132.436,-140.83',
'OHOhio,2.749,3.048,2.687,2.98,-119.037,-126.237',
'OKOklahoma,2.673,2.919,2.659,2.903,-116.8,-122.422',
'OROregon,2.888,3.092,2.883,3.087,-126.413,-133.163',
'PAPennsylvania,2.833,3.119,2.824,3.108,-126.263,-134.896',
'RIRhode Island,2.814,3.092,2.803,3.081,-129.148,-134.885',
'SCS. Carolina,2.65,2.954,2.645,2.948,-119.541,-128.45',
'SDS. Dakota,2.842,3.148,2.817,3.121,-122.915,-130.804',
'TNTennessee,2.691,2.985,2.689,2.982,-117.467,-125.145',
'TXTexas,2.68,2.937,2.659,2.914,-121.394,-129.272',
'UTUtah,2.874,3.164,2.819,3.103,-131.5,-139.952',
'VAVirginia,2.717,2.96,2.689,2.929,-123.287,-129.45',
'VTVermont,2.829,3.176,2.815,3.16,-124.54,-132.938',
'WAWashington,2.997,3.259,2.982,3.243,-133.76,-140.992',
'WIWisconsin,2.823,3.067,2.78,3.019,-127.807,-133.313',
'WVW. Virginia,2.833,3.11,2.813,3.089,-130.031,-137.617',
'WYWyoming,2.703,3.002,2.656,2.95,-128.421,-137.947',
'USthe U.S.A.,2.814,3.093,2.793,3.07,-125.269,-133.34',
'---DataStop'
);
