// JavaScript Document
			// returns the appropriate unit for the passed fuel type.
			function getHeatingFuelUnit(fuel) {
				if (fuel == "Natural Gas") {
					return "m<sup>3</sup>";
				} else {
					return "L";
				}
			}
			
			// function reduceTotal(JQuery targetElement, float reductionAmount)
			function reduceTotal(targetElement, reductionAmount) {
				newTotal =  parseFloat(targetElement.html()) - parseFloat(reductionAmount);
				newTotal = Math.round(newTotal*100)/100;
				targetElement.html(newTotal);
			}

			function calculateHeatingEmissions() {		
				amount = $("#fuel_amount").val();
				fuelMultiplier = $("#heating_fuel_multiplier").val();
				var emissions = 0;
				if ( $("#monthly").attr("checked") ) {
					emissions = (amount * fuelMultiplier * 12) / 1000;
				} else {
					emissions = (amount * fuelMultiplier) / 1000;
				}
				if (emissions > 0) {
					displayHeatingEmissions( emissions, $("#totalHeatingEmissions") );
					$("#add_heating").disabled = false;
					$("#add_heating").attr('disabled', false);
				}
				$("#hidden-heating-emissions").val(emissions);
			}

			// displayHeatingEmissions(float emissionAmount, JQuery displayElement)
			function displayHeatingEmissions( emissionAmount, displayElement ) {
				emissionAmount = Math.round(emissionAmount*100)/100;
				displayElement.html( emissionAmount );
				$("#hidden-heating-emissions").val(emissionAmount);
				$("#hidden-heating-fuel").val($("#heating_fuel_multiplier option:selected").text());
				if ( $("#monthly").attr("checked") || $("#annual").attr("checked") ) {
					$("#period-description").html("per year");
				} else {
					$("#period-description").html("");
				}
				$("#totalHeatingEmissionsSection").css('display', 'block');
			}







				// this is all the stuff that runs once the DOM for the page is ready, like onLoad but without the wait for images.
				$(document).ready(function(){
					
					showHideTable($("#heating-summary"), $("#heating-emissions-total"));
					
					// add the code to delete an entry from the summary list on clicking the delete link.
					$(".delete_entry").click( function() {
						// check the user really wants to do that.
						var answer = confirm("Delete this entry?");
						if (answer) {
							// hide the record from the page.
							$(this).parent().parent().hide();

							reduceTotal($("#heating-emissions-total"), parseFloat($(this).parent().prev().html()));

							// setup the URL to the PHP page that will actually remove the record from the DB.
							deleteURL = "./includes/delete-db-record.php?action=deleterecord&table=emissions&id=" + $(this).attr('id');

							// do an AJAX call in the background to delete the DB record.
							$.get(deleteURL);
							updateTotalDisplay();
							showHideTable($("#heating-summary"), $("#heating-emissions-total"));
						}
						});
						
					// setup watch for events that should trigger recalculation

					$("#fuel_amount").blur( function() {
						calculateHeatingEmissions();
						});

					$("#heating_fuel_multiplier").change( function() {
						calculateHeatingEmissions();
						var selectedFuel = $("#heating_fuel_multiplier option:selected").text()
						$("#hidden-heating-fuel").val(selectedFuel);
						$("span#unit").html(getHeatingFuelUnit(selectedFuel));
						$("#hidden-fuel-unit").val(getHeatingFuelUnit(selectedFuel))
						});
						
					$("#one-off").change( function() {
						calculateHeatingEmissions();
					});
					
					$("#monthly").change( function() {
						calculateHeatingEmissions();
					});
					
					$("#annual").change( function() {
						calculateHeatingEmissions();
					});		

					$("input[@name='heating-period']").click(function(){
						calculateHeatingEmissions();
					});

				}); // $(document).ready ENDS

