var frm;
function fetchobj(o)
{   // Figure out what our browser is capable of...
    if (document.layers)
	{
        return document.layers[o];
    }
    else if (document.all)
    {
        return document.all[o];
    }
    else if (document.getElementById)
    {
        return document.getElementById(o);
    }
    else return false;
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1].substr(0,2) : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
function stripCommas(nStr)
{
	return nStr.replace(/(\D)/g, '');
}
function getotherms() {
	dgeobj    = fetchobj('dge1');
	mplier    = (dgeobj.checked) ? 1.35 : 1.25;
	fuelval   = stripCommas(frm.fuelcons1.value);
	scfval    = (fuelval.length > 0) ? fuelval * mplier : '';

	flbl      = fetchobj('fuellabel1');
	scfp      = fetchobj('scfper1');
	flbl.innerHTML      = (dgeobj.checked) ? 'DGE' : 'GGE';
	scfp.innerHTML      = (scfval) ? '<b> = ' + addCommas(scfval) + ' Therms</b>' : '';
}
function calcrates() {
	dgeobj    = fetchobj('dge2');
	mplier    = (dgeobj.checked) ? 135 : 125;
	fuelval   = stripCommas(frm.fuelcons2.value);
	scfval    = (fuelval.length > 0) ? fuelval * mplier : '';

	flbl      = fetchobj('fuellabel2');
	scfp      = fetchobj('scfper2');
	flbl.innerHTML      = (dgeobj.checked) ? 'DGE' : 'GGE';
	scfp.innerHTML      = (scfval) ? '<b> = ' + addCommas(scfval) + ' SCFM</b>' : '';
}
function getoscf() {
	dgeobj    = fetchobj('dge');
	mplier    = (dgeobj.checked) ? 135 : 125;

	vehval    = stripCommas(frm.numveh.value);
	fuelval   = stripCommas(frm.fuelcons.value);
	ftimeval  = stripCommas(frm.fueltime.value);
	numhoses  = stripCommas(frm.numhoses.value);

	scfval    = (fuelval.length > 0) ? fuelval * mplier : '';
	fleetval  = (scfval.toString().length > 0 && vehval.length > 0) ? scfval * vehval : '';
	scfmval   = (fleetval.toString().length > 0 && ftimeval.length > 0) ? Math.round(fleetval / ftimeval) : '';
	fillval1  = (ftimeval.length > 0 && vehval.length > 0) ? Math.round(ftimeval / vehval) : '';
	fillvala  = (fillval1.toString().length > 0 && numhoses.length > 0) ? fillval1 * numhoses : '';

	flbl      = fetchobj('fuellabel');
	scfp      = fetchobj('scfper');
	fleetfl   = fetchobj('fleetfuel');
	scf       = fetchobj('scfm');
	filltm    = fetchobj('filltime');

	flbl.innerHTML      = (dgeobj.checked) ? 'DGE' : 'GGE';
	scfp.innerHTML      = (scfval) ? '<b> = ' + addCommas(scfval) + ' scf</b>' : '';
	fleetfl.innerHTML   = (fleetval) ? '<b> = ' + addCommas(fleetval) + ' scf</b>' : '';
	scf.innerHTML       = (scfmval) ? '<b> = ' + addCommas(scfmval) + ' scfm</b>' : '';
	filltm.innerHTML    = (fillvala) ? ' It will take ' + addCommas(fillvala) + ' minutes to fill ' + vehval + ' vehicles on ' + addCommas(numhoses) + ' hoses, or ' + addCommas(fillval1) + ' minutes for a single vehicle.' : '';
}
function init2() {
	frm = fetchobj('fornetscape');
	getoscf();
	frm.fuelcons1.focus();
	YAHOO.util.Event.on(fetchobj('tab1'), 'click', function() {sleep100(frm.fuelcons1);});
	YAHOO.util.Event.on(fetchobj('tab2'), 'click', function() {sleep100(frm.fuelcons2);});
	YAHOO.util.Event.on(fetchobj('tab3'), 'click', function() {sleep100(frm.fuelcons);});
}
function sleep100(w) {
	if (w) setTimeout(function() {w.focus();}, 100);
}