/* 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.953,4.075,4.003,4.181,2.86,3.223',
'ALAlabama,3.562,3.833,3.593,3.873,2.62,2.849',
'ARArkansas,3.539,3.798,3.572,3.833,2.612,2.895',
'AZArizona,3.37,3.579,3.375,3.577,2.868,3.188',
'CACalifornia,3.79,3.985,3.816,4.011,3.16,3.367',
'COColorado,3.507,3.711,3.509,3.705,2.669,2.892',
'CTConnecticut,4.026,4.294,4.056,4.314,2.869,3.121',
'DCWash. DC,3.898,4.219,3.902,4.219,2.656,2.924',
'DEDelaware,3.69,3.972,3.734,4.007,2.692,2.966',
'FLFlorida,3.674,3.95,3.716,3.99,2.7,2.961',
'GAGeorgia,3.654,3.959,3.718,4.02,2.643,2.96',
'HIHawaii,4.147,4.331,4.131,4.3,3.215,3.489',
'IAIowa,3.622,3.764,3.665,3.816,2.667,2.886',
'IDIdaho,3.691,3.896,3.671,3.864,2.908,3.102',
'ILIllinois,3.858,4.119,3.915,4.177,2.829,3.078',
'INIndiana,3.683,3.888,3.734,3.935,2.739,2.973',
'KSKansas,3.584,3.775,3.639,3.824,2.722,2.849',
'KYKentucky,3.598,3.792,3.662,3.873,2.683,2.95',
'LALouisiana,3.574,3.828,3.618,3.865,2.616,2.884',
'MAMassachusetts,3.773,4.023,3.797,4.045,2.681,2.925',
'MDMaryland,3.682,3.959,3.713,3.985,2.694,2.917',
'MEMaine,3.793,4.021,3.82,4.039,2.767,2.996',
'MIMichigan,3.753,3.954,3.801,3.99,2.834,3.061',
'MNMinnesota,3.709,3.914,3.784,4,2.733,2.949',
'MOMissouri,3.48,3.713,3.57,3.818,2.632,2.844',
'MSMississippi,3.537,3.785,3.561,3.805,2.608,2.839',
'MTMontana,3.747,3.92,3.742,3.919,2.908,3.126',
'NCN. Carolina,3.679,3.935,3.705,3.953,2.674,2.955',
'NDN. Dakota,3.75,3.895,3.781,3.931,2.73,2.87',
'NENebraska,3.703,3.825,3.751,3.87,2.766,2.851',
'NHNew Hampshire,3.713,3.96,3.731,3.974,2.726,2.986',
'NJNew Jersey,3.629,3.901,3.668,3.923,2.592,2.866',
'NMNew Mexico,3.534,3.751,3.57,3.769,2.794,3.067',
'NVNevada,3.538,3.752,3.553,3.77,3.022,3.325',
'NYNew York,3.951,4.21,3.973,4.231,2.865,3.094',
'OHOhio,3.618,3.824,3.658,3.864,2.719,2.919',
'OKOklahoma,3.53,3.715,3.583,3.777,2.611,2.795',
'OROregon,3.756,3.965,3.765,3.973,2.892,3.12',
'PAPennsylvania,3.704,3.969,3.739,3.998,2.706,2.958',
'RIRhode Island,3.836,4.104,3.864,4.122,2.81,3.015',
'SCS. Carolina,3.483,3.744,3.523,3.773,2.566,2.854',
'SDS. Dakota,3.736,3.905,3.761,3.927,2.748,2.986',
'TNTennessee,3.546,3.8,3.576,3.828,2.626,2.857',
'TXTexas,3.572,3.841,3.62,3.886,2.617,2.882',
'UTUtah,3.545,3.748,3.512,3.719,2.852,3.086',
'VAVirginia,3.577,3.867,3.621,3.901,2.646,2.876',
'VTVermont,3.739,3.954,3.763,3.978,2.71,2.972',
'WAWashington,3.814,4.025,3.817,4.032,3.12,3.347',
'WIWisconsin,3.701,3.925,3.756,3.986,2.857,3.044',
'WVW. Virginia,3.679,3.889,3.7,3.911,2.791,3.057',
'WYWyoming,3.569,3.752,3.548,3.716,2.785,3.042',
'USthe U.S.A.,3.677,3.915,3.715,3.95,2.769,3.004',
'---DataStop'
);

