var popupLinkConfig = new Array;

// popupLinkConfig["classname"] = new Array ( "targetname", "width=550,height=350,scrollbars=yes,resizable=yes,status=yes,toolbar=yes,location=yes,menubar=yes");
popupLinkConfig["cert"]    = new Array ( "", "width=680,height=517,scrollbar=no,menubar=yes");
popupLinkConfig["glossary"] = new Array ( "help", "width=550,height=350,resizable=yes");

/* head link:   <script type="text/javascript" language="Javascript" src="logic.js"></script>  */
// sample link:  <a href="http://www.yahoo.com/" class="cert">dolor</a>

// ==========================================================================
window.onload = initPage;  
// Note: Make sure that no other javscripts assign a fuction to window.onload
// There can be only one function tied to window.onload at a time.

function initPage() {
  initPopupLinks();
  // place here any other code you wish to run when the page loads.
}

function initPopupLinks()
{
  if (!document.getElementsByTagName) return true;
  var pageLinks = document.getElementsByTagName("a");
  for (var i = 0; i < pageLinks.length; i++) 
  {
    if (((pageLinks[i].className != null) && 
         (pageLinks[i].className != "")) ||
        ((pageLinks[i].parentNode.className != null) && 
         (pageLinks[i].parentNode.className != "")))
    {
      var linkClass = " " + pageLinks[i].className + " ";
      if ((linkClass == "  ") && (pageLinks[i].parentNode.className != ""))
      {
        linkClass = " " + pageLinks[i].parentNode.className + " ";
      }
      for (var theKey in popupLinkConfig) 
      {
        if (linkClass.indexOf(" " + theKey + " ") > -1)
        {
          if ((pageLinks[i].target == "") || (pageLinks[i].target == null))
          {
            pageLinks[i].target = (popupLinkConfig[theKey][0] != "") ? popupLinkConfig[theKey][0] : theKey;
          }
          pageLinks[i].settings = popupLinkConfig[theKey][1];
          pageLinks[i].onclick = popUp;
        }
      }
    }
  }
  return true;
}

function popUp()
{
  newWin = window.open(this.href, this.target, this.settings);
  newWin.focus();
  return false;
}

function poorman_toggle(id)
{
	var tr = document.getElementById(id);
	if (tr==null) { return; }
	var bExpand = tr.style.display == '';
	tr.style.display = (bExpand ? 'none' : '');
}
function poorman_changeimage(id, sMinus, sPlus)
{
	var img = document.getElementById(id);
	if (img!=null)
	{
	    var bExpand = img.src.indexOf(sPlus) >= 0;
		if (!bExpand)
			img.src = sPlus;
		else
			img.src = sMinus;
	}
}

function Toggle_trGrpHeader1()
{
    poorman_changeimage('plusminus1', 'images/minus.gif', 'images/plus.gif');
    poorman_toggle('row2');
	var autompy2 = 0;
	var autompg2 = 1;
	document.offset2.autompy2.value = autompy2;
	document.offset2.autompg2.value = '';
}

function Toggle_trGrpHeader2()
{
    poorman_changeimage('plusminus2', 'images/minus.gif', 'images/plus.gif');
    poorman_toggle('row3');
	var autompy3 = 0;
	var autompg3 = 1;
	document.offset2.autompy3.value = autompy3;
	document.offset2.autompg3.value = '';
}

function Toggle_trGrpHeader3()
{
    poorman_changeimage('plusminus3', 'images/minus.gif', 'images/plus.gif');
    poorman_toggle('row4');
	var autompy4 = 0;
	var autompg4 = 1;
	document.offset2.autompy4.value = autompy4;
	document.offset2.autompg4.value = '';
}

function togglehide() {
	Toggle_trGrpHeader1();
	Toggle_trGrpHeader2();
	Toggle_trGrpHeader3();
	DoMath();
}

function togglehouseholdemissions()
{
	poorman_toggle('householdkwh1');
	poorman_toggle('householdkwh2');
	poorman_toggle('householdkwh3');
	poorman_toggle('householdkwh4');
	poorman_toggle('householdbillamt1');
	poorman_toggle('householdbillamt2');
	poorman_toggle('householdbillamt3');
	poorman_toggle('householdbillamt4');
	poorman_toggle('householdchangetobillamt');
	poorman_toggle('householdchangetokhw');
	if(document.offset2.householdusagetype.value=='1')
	{
		document.offset2.householdusagetype.value = '2';
	}
	else
	{
		document.offset2.householdusagetype.value = '1';
	}
	DoMath();
}

function StoreMath(){
//define timeframe
var timeframeON1 = document.offset1.timeframe.value;

//calculate amount
var lbsC = document.offset1.lbsC.value;
var amountONUR = lbsC * .00467 * timeframeON1;
var amountON1 = Math.round(amountONUR);
document.offset1.amount.value = amountON1;
}

function StoreMathGC(){
//define timeframe
var timeframeGC = document.giftcertificate.timeframe.value

//calculate amount
var lbsC2 = document.giftcertificate.lbsC2.value
var amountGCUR = lbsC2 * .00467 * timeframeGC
var amountGC = Math.round(amountGCUR)
document.giftcertificate.amount.value = amountGC
}

function DoTimeframe1(){
//var timeframe2 = document.offset2.timeframe2.value;
var timeframe2 = 1;
document.offset3.timeframe3.value = timeframe2;
DoMath();
}

function DoTimeframe2(){
var timeframe3 = document.offset3.timeframe3.value;
//document.offset2.timeframe2.value = timeframe3;
DoMath();
}

function DoMath(){
//define timeframe
//var timeframe = document.offset2.timeframe2.value;
var timeframe = 1;
var timeframetop = 12;

//calculate auto1
var autompy1 = eval(document.offset2.autompy1.value);
var autompg1 = eval(document.offset2.autompg1.value);
if (autompg1 == 0){
	var autompg1 = 1;
}
if (autompg1 == undefined){
	var autompg1 = 1;
}
var autoAveEcono1 = autompy1 / autompg1;
var autoTotal1UR = autoAveEcono1 * 20.421053 * timeframe;
var autoTotal1 = Math.round(autoTotal1UR);
document.offset2.autoTotal1.value = autoTotal1;

//calculate auto2
var autompy2 = eval(document.offset2.autompy2.value);
var autompg2 = eval(document.offset2.autompg2.value);
if (autompg2 == 0){
	var autompg2 = 1;
}
if (autompg2 == undefined){
	var autompg2 = 1;
}
var autoAveEcono2 = autompy2 / autompg2;
var autoTotal2UR = autoAveEcono2 * 20.421053 * timeframe;
var autoTotal2 = Math.round(autoTotal2UR);
document.offset2.autoTotal2.value = autoTotal2;

//calculate auto3
var autompy3 = eval(document.offset2.autompy3.value);
var autompg3 = eval(document.offset2.autompg3.value);
if (autompg3 == 0){
	var autompg3 = 1;
}
if (autompg3 == undefined){
	var autompg3 = 1;
}
var autoAveEcono3 = autompy3 / autompg3;
var autoTotal3UR = autoAveEcono3 * 20.421053 * timeframe;
var autoTotal3 = Math.round(autoTotal3UR);
document.offset2.autoTotal3.value = autoTotal3;

//calculate auto4
var autompy4 = eval(document.offset2.autompy4.value);
var autompg4 = eval(document.offset2.autompg4.value);
if (autompg4 == 0){
	var autompg4 = 1;
}
if (autompg4 == undefined){
	var autompg4 = 1;
}
var autoAveEcono4 = autompy4 / autompg4;
var autoTotal4UR = autoAveEcono4 * 20.421053 * timeframe;
var autoTotal4 = Math.round(autoTotal4UR);
document.offset2.autoTotal4.value = autoTotal4;

//calculate autoTotal, mpyTotal, mpgAve
var autoTotal = autoTotal1 + autoTotal2 + autoTotal3 + autoTotal4;
document.offset2.autoTotal.value = autoTotal;
var mpyTotal = autompy1 + autompy2 + autompy3 + autompy4;
var carcount = 1;
var mpg4ave2 = autompg2;
var mpg4ave3 = autompg3;
var mpg4ave4 = autompg4;
if (autompg2 != 1) { carcount++;} else { mpg4ave2 = 0;}
if (autompg3 != 1) { carcount++;} else { mpg4ave3 = 0;}
if (autompg4 != 1) { carcount++;} else { mpg4ave4 = 0;}
var mpgAve = (autompg1 + mpg4ave2 + mpg4ave3 + mpg4ave4) / carcount;


//calculate electric
if(document.offset2.householdusagetype.value == '2')
{
	var electric = document.offset2.electric.value;
	if(electric>0)
	{
		var electrickWh = electric / 0.1031;
		//var electricTotalUR = (electrickWh * 1.7636981) * timeframetop;
		var electricTotalUR = (electrickWh * 1.34) * timeframetop;
		var electricTotal = Math.round(electricTotalUR);
	}
	else
	{
		var electricTotal = 0;
	}
	document.offset2.electricTotal.value = electricTotal;
}
else
{
	var electricusage = document.offset2.electricusage.value;
	//var electricusageTotalUR = (electricusage * 1.7636981) * timeframetop;
	var electricusageTotalUR = (electricusage * 1.34) * timeframetop;
	var electricusageTotal = Math.round(electricusageTotalUR);
	document.offset2.electricTotalusage.value = electricusageTotal;
}

//calculate gas
if(document.offset2.householdusagetype.value == '2')
{
	var gas = document.offset2.gas.value;
	if(gas>0)
	{
		var gasCF = gas / 0.01301; // Price of gas per 1,000 Cubic Feet (ex: $13.01 will say 0.01301)
		var gasBTUs = gasCF * 1021; // 1 Cubic Foot of Natural Gas = 1021 BTUs
		var gasTherms = gasBTUs / 100000; // One therm equals 100,000 BTUs
		var gasTotalUR = (gasTherms * 22.0462262) * timeframetop; // Therms to lbs of CO2 produced
		// --- Older One I Did ---
		//var gasCF = gas / 0.09809;
		//var gasTotalUR = (gasCF * 0.01031) * 12 * timeframetop; // this was * 12 * timeframe
		// --- Orginal ---
		//var gasTotalUR = gas * 104.6507592 * timeframetop;
		var gasTotal = Math.round(gasTotalUR);
	}
	else
	{
		var gasTotal = 0;
	}
	document.offset2.gasTotal.value = gasTotal;
}
else
{
	var gasusage = document.offset2.gasusage.value;
	var gasusageTotalUR = gasusage * 22.0462262; // Therms to lbs of CO2 produced
	var gasusageTotalUR = gasusageTotalUR * timeframetop;
	var gasusageTotal = Math.round(gasusageTotalUR);
	document.offset2.gasTotalusage.value = gasusageTotal;
}

if(document.offset2.householdusagetype.value == '2')
{
	//calculate oil
	var oil = document.offset2.oil.value;
	if(oil>0)
	{
		var oilGal = oil / 2.78;
		var oilTotalUR = (oilGal * 22.384) * timeframetop;
		//var oilTotalUR = oil * 112.8101266 * timeframetop;
		var oilTotal = Math.round(oilTotalUR);
	}
	else
	{
		var oilTotal = 0;
	}
	document.offset2.oilTotal.value = oilTotal;
}
else
{
	//calculate oil
	var oilusage = document.offset2.oilusage.value;
	var oilusageTotalUR = (oilusage * 22.384) * timeframetop;
	var oilusageTotal = Math.round(oilusageTotalUR);
	document.offset2.oilTotalusage.value = oilusageTotal;
}
if(document.offset2.householdusagetype.value == '2')
{
	//calculate household total
	var houseTotal = electricTotal + gasTotal + oilTotal;
	document.offset2.houseTotal.value = houseTotal;
	var householdmaintotal = houseTotal;
}
else
{
	//calculate household total
	var houseTotalusage = electricusageTotal + gasusageTotal + oilusageTotal;
	document.offset2.houseTotalusage.value = houseTotalusage;
	var householdmaintotal = houseTotalusage;
}
//calculate airmiles
var airmiles = document.offset2.airmiles.value;
var airTotalUR = (airmiles * .42) * timeframe;
var airTotal = Math.round(airTotalUR);
document.offset2.airTotal.value = airTotal;

//calculate boat
var boatmpy = document.offset2.boatmpy.value;
var boatmpg = document.offset2.boatmpg.value;
if (boatmpg == 0){
	var boatmpg = 1;
}
if (boatmpg == undefined){
	var boatmpg = 1;
}
var boatEmissions = boatmpy * 19.564;
var boatTotalUR = boatEmissions / boatmpg;
var boatTotalURTF = boatTotalUR * timeframe;
var boatTotal = Math.round(boatTotalURTF);
document.offset2.boatTotal.value = boatTotal;

//calculate solid waste
var people = document.offset2.people.value;
var wasteTotalUR = (people * 1018) * timeframe;
var wasteTotal = Math.round(wasteTotalUR);
document.offset2.wasteTotal.value = wasteTotal;

//calculate newspaper redux
//var newspaper = document.offset3.newspaper.value;
//var newspaperReduxUR = people * 184.3 * newspaper * timeframe;
//var newspaperRedux = Math.round(newspaperReduxUR);
//document.offset3.newspaperRedux.value = newspaperRedux;

//calculate glass redux
//var glass = document.offset3.glass.value;
//var glassReduxUR = people * 25.6 * glass * timeframe;
//var glassRedux = Math.round(glassReduxUR);
//document.offset3.glassRedux.value = glassRedux;

//calculate plastic redux
//var plastic = document.offset3.plastic.value;
//var plasticReduxUR = people * 46.6 * plastic * timeframe;
//var plasticRedux = Math.round(plasticReduxUR);
//document.offset3.plasticRedux.value = plasticRedux;

//calculate alum redux
//var alum = document.offset3.alum.value;
//var alumReduxUR = people * 165.8 * alum * timeframe;
//var alumRedux = Math.round(alumReduxUR);
//document.offset3.alumRedux.value = alumRedux;

//calculate wasteRedux
//var wasteRedux = newspaperRedux + glassRedux + plasticRedux + alumRedux;
//document.offset3.wasteRedux.value = wasteRedux;

//calculate autoRedux
//var autompgCons = eval(document.offset3.autompgCons.value);
//var autompgNew = mpgAve + autompgCons;
//var autoNewEcono = mpyTotal / autompgNew;
//var autoNewTotalUR = autoNewEcono * 20.421053 * timeframe;
//var autoReduxUR = autoTotal - autoNewTotalUR;
//var autoRedux = Math.round(autoReduxUR);
//document.offset3.autoRedux.value = autoRedux;

//calculate electricRedux
//var electricCons = document.offset3.electricCons.value;
//var electricReduxUR = electricCons * electricTotal * .0033 * timeframe;
//var electricRedux = Math.round(electricReduxUR);
//document.offset3.electricRedux.value = electricRedux;

//calculate gasRedux
//var gasCons = document.offset3.gasCons.value;
//var gasReduxUR = gasCons * gasTotal * .0042 * timeframe;
//var gasRedux = Math.round(gasReduxUR);
//document.offset3.gasRedux.value = gasRedux;

//calculate oilRedux
//var oilCons = document.offset3.oilCons.value;
//var oilReduxUR = oilCons * oilTotal * .0042 * timeframe;
//var oilRedux = Math.round(oilReduxUR);
//document.offset3.oilRedux.value = oilRedux;

//calculate heatingRedux
//var heatingRedux = electricRedux + gasRedux + oilRedux;
//document.offset3.heatingRedux.value = heatingRedux;

//calculate acRedux
//var acCons = document.offset3.acCons.value;
//var acReduxUR = acCons * electricTotal * .00336 * timeframe;
//var acRedux = Math.round(acReduxUR);
//document.offset3.acRedux.value = acRedux;

//calculate bulbsRedux
//var bulbsRepl = document.offset3.bulbsRepl.value;
//var bulbsReduxUR = bulbsRepl * 100.01 * timeframe;
//var bulbsRedux = Math.round(bulbsReduxUR);
//document.offset3.bulbsRedux.value = bulbsRedux;

//calculate fridgeRedux
//var fridgeRepl = document.offset3.fridgeRepl.value;
//var fridgeReduxUR = fridgeRepl * 520.6 * timeframe;
//var fridgeRedux = Math.round(fridgeReduxUR);
//document.offset3.fridgeRedux.value = fridgeRedux;

//calculate furnaceRedux
//var furnaceRepl = document.offset3.furnaceRepl.value;
//var furnaceReduxUR = furnaceRepl * timeframe;
//var furnaceRedux = Math.round(furnaceReduxUR);
//document.offset3.furnaceRedux.value = furnaceRedux;

//calculate windowsRedux
//var windowsRepl = document.offset3.windowsRepl.value;
//var windowsReduxUR = windowsRepl * timeframe;
//var windowsRedux = Math.round(windowsReduxUR);
//document.offset3.windowsRedux.value = windowsRedux;

//calculate houseRedux
//var houseRedux = bulbsRedux + fridgeRedux + furnaceRedux + windowsRedux;
//document.offset3.houseRedux.value = houseRedux;

//calculate totalRedux
//var totalRedux = houseRedux + acRedux + heatingRedux + autoRedux + wasteRedux;
//document.offset3.totalRedux.value = totalRedux;

//calculate total
var total = autoTotal + householdmaintotal + airTotal + boatTotal + wasteTotal;
document.offset2.total.value = total;
//document.offset3.total.value = total;

//calculate totalAfterRedux
//var totalAfterRedux = total - totalRedux;
var totalAfterRedux = 0;
//document.offset3.totalAfterRedux.value = totalAfterRedux;

//calculate amount2
//var amount2UR = total * .00467;
if(total>0)
{
	var amount2Pre = total / 1340;
	amount2Pre = amount2Pre+'';
	if(amount2Pre.indexOf('.')>0)
	{
		// Value Is Not A Whole Number, Round Up
		var amountOfRECs = parseFloat(amount2Pre.substring(0,amount2Pre.indexOf('.'))) + 1;
	}
	else
	{
		var amountOfRECs = amount2Pre;
	}
}
else
{
	var amountOfRECs = 0;
}
document.offset2.recs.value = amountOfRECs;
document.offset2.amount.value = formatCurrency((amountOfRECs * document.offset2.roa.value));

//calculate amount3
var amount3UR = totalAfterRedux * .00467;
var amount3 = Math.round(amount3UR);
//document.offset3.amount.value = amount3;


}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
//return (((sign)?'':'-') + '$' + num + '.' + cents);
return num + '.' + cents;
}