function solarCalculate() {
	frm = document.getElementById('solarcalc');
	monthlyKWHusedA = frm.generatedA.value-0;
	monthlyKWHusedB = frm.generatedB.value-0;
	solarPortionA = frm.needsA.value-0;
	solarPortionB = frm.needsB.value-0;
	generatedAv = monthlyKWHusedA * solarPortionA;
	generatedBv = monthlyKWHusedB * solarPortionB;

	sunA = frm.sunA.options[frm.sunA.selectedIndex].value-0;
	sunB = frm.sunB.options[frm.sunB.selectedIndex].value-0;
	sizeAv = Math.round(generatedAv/sunA *10)/10;
	sizeBv = Math.round(generatedBv/sunB *10)/10;
	insert(sizeAv,"sizeA");
	insert(sizeBv,"sizeB");

	installrateA = frm.installrateA.options[frm.installrateA.selectedIndex].value;
	installrateB = frm.installrateB.options[frm.installrateB.selectedIndex].value;
	installcostAv = Math.round(sizeAv * installrateA * 1000);
	installcostBv = Math.round(sizeBv * installrateB * 1000);
	insert(installcostAv,"installcostA");
	insert(installcostBv,"installcostB");

	wattrebateA = frm.wattrebateA.options[frm.wattrebateA.selectedIndex].value;
	wattrebateB = frm.wattrebateB.options[frm.wattrebateB.selectedIndex].value;
	rebateAv = Math.round(sizeAv * wattrebateA *1000);
	rebateBv = Math.round(sizeBv * wattrebateB *1000);
	insert(rebateAv,"rebateA");
	insert(rebateBv,"rebateB");

	taxcreditusedA = frm.taxcreditusedA.options[frm.taxcreditusedA.selectedIndex].value;
	taxcreditusedB = frm.taxcreditusedB.options[frm.taxcreditusedB.selectedIndex].value;
	taxcreditAv = Math.round( (installcostAv-rebateAv) * 0.30 * taxcreditusedA);
	taxcreditBv = Math.round( (installcostBv-rebateBv) * 0.30 * taxcreditusedB);
	insert(taxcreditAv,"taxcreditA");
	insert(taxcreditBv,"taxcreditB");

	netcostAv = installcostAv - rebateAv - taxcreditAv;
	netcostBv = installcostBv - rebateBv - taxcreditBv;
	insert(netcostAv,"netcostA");
	insert(netcostAv,"netcostA2");
	insert(netcostBv,"netcostB");
	insert(netcostBv,"netcostB2");

// GRID ELECTRICITY COST FOR THE SAME PERIOD
	gridRate = frm.gridrate.options[frm.gridrate.selectedIndex].value/100;
	//inflationRate = 1+frm.inflationRateField.value-0;
	//inflationFactor = exp(inflationRate,21);
	inflationRate=1;
	inflationFactor=1; // DON'T ADJUST FOR INFLATION, B/C IT'S OFFSET BY OPPORTUNITY COSTS!
	insert(Math.round(generatedAv*gridRate * 12 * 21 * inflationFactor), "totalGridCostA");
	insert(Math.round(generatedBv*gridRate * 12 * 21 * inflationFactor), "totalGridCostB");

// PAYBACK TIME
	paybackA = payback(generatedAv * gridRate * 12, netcostAv);
	paybackB = payback(generatedBv * gridRate * 12, netcostBv);
	insert( Math.round(paybackA*10)/10, "paybackA");
	insert( Math.round(paybackB*10)/10, "paybackB");

// MONTHLY COST
	intRate = frm.interestRate.options[frm.interestRate.selectedIndex].value/12;
	denominator = exp((1+intRate),240);
	monthlycostAv = Math.round(( netcostAv * intRate * exp(1+intRate,240) ) / (denominator-1));
	monthlycostBv = Math.round(( netcostBv * intRate * exp(1+intRate,240) ) / (denominator-1));
	insert(monthlycostAv,"monthlycostA");
	insert(monthlycostBv,"monthlycostB");

	kwhpriceAv = Math.round(netcostAv / (generatedAv * 12 * 21) * 100) /100;
	kwhpriceBv = Math.round(netcostBv / (generatedBv * 12 * 21) * 100) /100;
	if (kwhpriceAv*10==Math.floor(kwhpriceAv*10)) { kwhpriceAv+="0";}
	if (kwhpriceBv*10==Math.floor(kwhpriceBv*10)) { kwhpriceBv+="0";}
	insert(kwhpriceAv,"kwhpriceA");
	insert(kwhpriceBv,"kwhpriceB");
}

function debug(what) {
	document.getElementById('debugArea').innerHTML=what;
}

function payback(valueOfGeneratedKWHperYear,netcost) {
//	inflationFactor= inflationRate;
	inflactionFactor=1; // DON'T ADJUST FOR INFLATION, B/C IT'S OFFSET BY OPPORTUNITY COSTS!
	yearlySavingsSoFar = 0;
	for (years=1; years<=1000; years++) {
		yearlySavingsSoFar += valueOfGeneratedKWHperYear * inflationFactor;
		if (yearlySavingsSoFar > netcost) { break;}
		inflationFactor *= inflationRate;
	}
	// Adjustment for fractional years
	fractionalYear = (yearlySavingsSoFar-netcost)/(valueOfGeneratedKWHperYear*inflationFactor);
	return years-fractionalYear-0;
}

function exp(x,e) {
 if (e<0) { e=-e; inverted=1;} else {inverted=0;}
	runningValue=x;
	for (count=1; count<e; count++) {
		runningValue=runningValue*x;
	}
	if (inverted) {runningValue = 1/runningValue;}
	return runningValue;
}

function insert(theValue,theSpan) {
	if (theValue>=1000) {
	  theValue = theValue+"";
	  len = theValue.length;
	  theValue = theValue.substring(0,len-3)+','+theValue.substring(len-3,len)};
	document.getElementById(theSpan).innerHTML = theValue;
}

solarCalculate();