household=1; // Household or individual
document.getElementById('byhousehold').style.background='black';
document.getElementById('byperson').style.background='#666';
calculate();
//--------------------------------------------
function forWhom(theChoice) {
	frm=document.carbocalc;
	if (theChoice=='household') {
	document.getElementById('byhousehold').style.background='black';
	document.getElementById('byperson').style.background='#666';
	household=1;
	document.getElementById('youyour').innerHTML='r household';
	document.getElementById('houseperson').innerHTML='household';
	document.getElementById('avghome').innerHTML='12.7';
	document.getElementById('avgdriving').innerHTML='11.6';
	document.getElementById('avgfood').innerHTML='11.7';
	document.getElementById('avgflying').innerHTML='2.3';
	document.getElementById('total').innerHTML='38.3';

	frm.electric.value=920; frm.electype.selectedIndex=0;
	frm.gas.value=102;  frm.gastype.selectedIndex=1;
	frm.oil.value=77;	frm.gastype.selectedIndex=1;

	frm.carmiles1.value=968; frm.carmiles2.value=968;
	document.getElementById('car2').style.display='block';
	
	frm.adults.selectedIndex=1;
	frm.children.selectedIndex=1;
	document.getElementById('adultschildren').style.display='block';

	frm.flights.selectedIndex=4;
	frm.flyhours.selectedIndex=13;
}
	else {
	document.getElementById('byhousehold').style.background='#666';
	document.getElementById('byperson').style.background='black';
	household=0;
	document.getElementById('youyour').innerHTML='';document.getElementById('houseperson').innerHTML='person';
	document.getElementById('avghome').innerHTML='6.5';
	document.getElementById('avgdriving').innerHTML='6.0';
	document.getElementById('avgfood').innerHTML='4.5';
	document.getElementById('avgflying').innerHTML='0';
	document.getElementById('total').innerHTML='17.0';

	frm.electric.value=460; frm.electype.selectedIndex=0;
	frm.gas.value= 54;  frm.gastype.selectedIndex=1;
	frm.oil.value=39;	frm.gastype.selectedIndex=1;

	frm.carmiles1.value=1003; frm.carmiles2.value=0;
	document.getElementById('car2').style.display='none';

	frm.adults.selectedIndex=0;
	frm.children.selectedIndex=0;
	document.getElementById('adultschildren').style.display='none';
	
	frm.flights.selectedIndex=0;
	frm.flyhours.selectedIndex=0;
	}
	calculate();
}
//--------------------------------------------
function calculate() {

frm = document.carbocalc;

// HOME ENERGY
	if (frm.electype.options[frm.electype.selectedIndex].value=='energy')
		etons= frm.electric.value *1.72/2000*12;
	else etons= (frm.electric.value-10)/0.11 *1.72/2000*12;

	if (frm.gastype.options[frm.gastype.selectedIndex].value=='energy')
		gtons= frm.gas.value *0.0055*12; // 0.0055 tons per therm
	else gtons= (frm.gas.value-10)/1.50*0.0055*12; // $1.50/therm
	if (gtons<0) gtons=0;
	
	if (frm.oiltype.options[frm.oiltype.selectedIndex].value=='energy')
		otons= frm.oil.value *24.2/2000*12;
	else otons= (frm.oil.value-10)/4.00 *24.2/2000*12;
	if (otons<0) otons=0;

//document.getElementById('poopoo').innerHTML = '<'+'p>'+Math.round(etons*10)/10+"/"+Math.round(gtons*10)/10+"/"+Math.round(otons*10)/10; 
	housetons= etons+gtons+otons;
	frm.housetons.value= Math.round(housetons*10)/10;


// DRIVING AND CRYING
	mpg1 = frm.mpg1.options[frm.mpg1.selectedIndex].value*0.917; // Account for fact that MPG rating is overstated
	miles1 = frm.carmiles1.value*12;
	cargallons1 = miles1/mpg1;
	if (household) {mpg2=frm.mpg2.options[frm.mpg2.selectedIndex].value*0.917;miles2=frm.carmiles2.value*12; cargallons2=miles2/mpg2;}
	else cargallons2=0;
	cartons = Math.round((cargallons1+cargallons2)*20.4/2000*10)/10;
	frm.cartons.value=cartons;

// EATING;

	if (household)eaters=frm.adults.options[frm.adults.selectedIndex].value-0 + frm.children.options[frm.children.selectedIndex].value*0.6;
	else eaters=1;
	dietfactor=frm.diet.options[frm.diet.selectedIndex].value;
	diettons = eaters*dietfactor;
	frm.diettons.value=Math.round(diettons*10)/10;

	
// FLYING AND CRYING;
	flights=frm.flights.options[frm.flights.selectedIndex].value-0;
	if (!flights) flytons=0;
	else {
	  flyhours=frm.flyhours.options[frm.flyhours.selectedIndex].value-0;
	  avghours = flyhours/flights;
	  avgflymiles= 200+(avghours-1)*525;
	  if (avgflymiles<=727)		 flyfactor=0.64;
	  else if (avgflymiles<2575) flyfactor=0.44;
	  else 						 flyfactor=0.39;
	  avgflypounds = avgflymiles * flyfactor;
	  flytons=avgflypounds/2000*flights*1.9; // 1.9 is radiative forcing factor
	}
	if (flytons<0) flytons=0;
	frm.flytons.value=Math.round(flytons*10)/10;
//document.getElementById('houseperson').innerHTML=("avghours=" +avghours+ "<br>avgmiles=" +avgflymiles+ "<br>avgflytons=" +avgflytons+ "<br>flytons=" +flytons); 

// TOTAL BABY;
	frm.total.value=Math.round( (housetons+cartons+diettons+flytons)*10)/10;
}

//--------------------------------------------
function showHelp(whichHelp,whereToShow) {
	theTop=findTop(document.getElementById(whereToShow));
	showObj=document.getElementById(whichHelp).style;
	showObj.top=theTop;
	showObj.display='block';
}

function findTop(obj) {
//----------------------------------------------------------------
	curtop = 0;
	if(obj.offsetParent)
		while(1) {
			curtop += obj.offsetTop;
			if(!obj.offsetParent)
				break;
			obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}