/* 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.5,3.916,3.523,3.941,2.712,3.072',
'ALAlabama,2.515,2.77,2.508,2.76,2.387,2.661',
'ARArkansas,2.523,2.83,2.502,2.808,2.379,2.704',
'AZArizona,2.687,2.964,2.702,2.981,2.72,3.039',
'CACalifornia,3.038,3.286,3.066,3.318,3.114,3.264',
'COColorado,2.775,3.1,2.752,3.076,2.474,2.724',
'CTConnecticut,2.789,3.106,2.798,3.116,2.778,3.019',
'DCWash. DC,2.704,2.966,2.72,2.983,2.515,2.807',
'DEDelaware,2.533,2.809,2.538,2.815,2.581,2.89',
'FLFlorida,2.642,2.915,2.628,2.899,2.516,2.805',
'GAGeorgia,2.564,2.878,2.539,2.85,2.472,2.801',
'HIHawaii,3.468,3.736,3.457,3.724,3.048,3.326',
'IAIowa,2.67,2.945,2.637,2.909,2.402,2.656',
'IDIdaho,2.981,3.234,2.984,3.237,2.725,2.932',
'ILIllinois,2.788,3.092,2.781,3.084,2.547,2.832',
'INIndiana,2.684,2.969,2.663,2.946,2.467,2.735',
'KSKansas,2.62,2.795,2.625,2.8,2.451,2.621',
'KYKentucky,2.67,2.993,2.632,2.95,2.416,2.714',
'LALouisiana,2.543,2.838,2.546,2.84,2.383,2.694',
'MAMassachusetts,2.628,2.933,2.635,2.941,2.593,2.832',
'MDMaryland,2.585,2.818,2.587,2.82,2.582,2.842',
'MEMaine,2.68,2.972,2.681,2.973,2.679,2.898',
'MIMichigan,2.766,3.047,2.736,3.013,2.552,2.816',
'MNMinnesota,2.698,2.879,2.671,2.85,2.444,2.707',
'MOMissouri,2.493,2.746,2.497,2.751,2.37,2.616',
'MSMississippi,2.504,2.759,2.492,2.747,2.375,2.652',
'MTMontana,2.892,3.16,2.894,3.163,2.725,2.954',
'NCN. Carolina,2.594,2.875,2.592,2.871,2.501,2.796',
'NDN. Dakota,2.779,2.979,2.769,2.968,2.459,2.641',
'NENebraska,2.756,2.896,2.749,2.889,2.491,2.623',
'NHNew Hampshire,2.608,2.927,2.616,2.935,2.639,2.889',
'NJNew Jersey,2.49,2.771,2.503,2.785,2.484,2.792',
'NMNew Mexico,2.755,3.061,2.758,3.064,2.545,2.865',
'NVNevada,2.862,3.127,2.891,3.158,2.866,3.17',
'NYNew York,2.817,3.077,2.829,3.091,2.807,3.037',
'OHOhio,2.67,2.961,2.645,2.933,2.428,2.692',
'OKOklahoma,2.586,2.824,2.556,2.79,2.351,2.572',
'OROregon,2.96,3.17,2.975,3.186,2.742,2.974',
'PAPennsylvania,2.634,2.9,2.639,2.905,2.594,2.881',
'RIRhode Island,2.683,2.948,2.685,2.951,2.721,2.917',
'SCS. Carolina,2.455,2.736,2.446,2.727,2.4,2.7',
'SDS. Dakota,2.742,3.037,2.718,3.011,2.475,2.748',
'TNTennessee,2.509,2.783,2.501,2.774,2.365,2.629',
'TXTexas,2.518,2.76,2.514,2.755,2.397,2.711',
'UTUtah,2.916,3.21,2.923,3.217,2.672,2.916',
'VAVirginia,2.515,2.74,2.508,2.732,2.475,2.721',
'VTVermont,2.687,3.016,2.688,3.017,2.624,2.875',
'WAWashington,3.027,3.291,3.048,3.315,2.951,3.15',
'WIWisconsin,2.708,2.941,2.685,2.917,2.573,2.8',
'WVW. Virginia,2.744,3.013,2.729,2.996,2.611,2.893',
'WYWyoming,2.781,3.09,2.788,3.097,2.609,2.874',
'USthe U.S.A.,2.698,2.965,2.695,2.962,2.585,2.85',
'---DataStop'
);
