

function roundIt(obj) {
	newnum = Math.round(obj*100)/100;
	return newnum;
}

function getValue(obj) {
	thisValue = $('#'+obj).html();
	
	if (thisValue == '') {
		return 0;
	} else if(!isNaN(thisValue)) {
		return parseFloat(thisValue);
	} else {
		return 0;
	}
}


function perPerson() {
	var people = $('#people').val();
		if(!isNaN(parseInt(people))) {
			newvalue = getValue('totalpounds') / parseInt(people);
			$('#perperson').html(roundIt(newvalue)); } else { $('#perperson').html('');
		}
}


function totalPounds(obj) {
	newtotal = getValue('calc_electricity') + getValue('calc_naturalgas') + getValue('calc_oil') + getValue('calc_propane') + getValue('calc_vehicle1') + getValue('calc_vehicle2') + getValue('calc_airtravel');
	$('#totalpounds').html(roundIt(newtotal));
	perPerson();
	return;
}




$(document).ready(function() {

	// when the field changes, recalc the total to the right
	$('#electricity').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 1.24;
			$('#calc_electricity').html(roundIt(newvalue)); } else { $('#calc_electricity').html(''); }
		// add to grand total
		totalPounds();
	});

	$('#naturalgas').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 100000 * 0.0001304;
			$('#calc_naturalgas').html(roundIt(newvalue)); } else { $('#calc_naturalgas').html('');
		}
		// add to grand total
		totalPounds();
	});

	$('#oil').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 11.81 * 2.205;
			$('#calc_oil').html(roundIt(newvalue)); } else { $('#calc_oil').html('');
		}
		// add to grand total
		totalPounds();
	});

	$('#propane').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 5.75 * 2.205;
			$('#calc_propane').html(roundIt(newvalue)); } else { $('#calc_propane').html('');
		}
		// add to grand total
		totalPounds();
	});
	
	$('#vehicle1').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 8.87 * 2.205;
			$('#calc_vehicle1').html(roundIt(newvalue)); } else { $('#calc_vehicle1').html('');
		}
		// add to grand total
		totalPounds();
	});

	$('#vehicle2').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 8.87 * 2.205;
			$('#calc_vehicle2').html(roundIt(newvalue)); } else { $('#calc_vehicle2').html('');
		}
		// add to grand total
		totalPounds();
	});

	$('#airtravel').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) * 1.61 * (0.18 * 2.205);
			$('#calc_airtravel').html(roundIt(newvalue)); } else { $('#calc_airtravel').html('');
		}
		// add to grand total
		totalPounds();
	});
	
	$('#people').keyup(function() {
		//thisvalue = $(this).val();
		perPerson();
	});
	
	$('#small').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) / 29;
			$('#calc_small').html(roundIt(newvalue)); } else { $('#calc_small').html('');
		}
	});
	
	$('#medium').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) / 26;
			$('#calc_medium').html(roundIt(newvalue)); } else { $('#calc_medium').html('');
		}
	});
	
	$('#large').keyup(function() {
		thisvalue = $(this).val();
		if(!isNaN(parseInt(thisvalue))) {
			newvalue = parseInt(thisvalue) / 21.5;
			$('#calc_large').html(roundIt(newvalue)); } else { $('#calc_large').html('');
		}
	});

}); 