function installListeners()
{
	var submit_element = document.getElementById('submit');
	
	
	if(submit_element)
	{
		// set up the listener for the year field
		AttachEvent(submit_element, 'click', doCalculate, false);
	}

	var redo = document.getElementById('redo');

	if(redo)
	{
		AttachEvent(redo, 'click', startAgain, false);
	}

}

AttachEvent(window, 'load', installListeners, false);

function startAgain()
{
	var hideme = document.getElementById('hideme');
	hideme.style.display = 'block';

	var results = document.getElementById('results');
	results.style.display = 'none';	
}

function doCalculate()
{
	// do tables
	var buildings = document.getElementById('tbuildings');
	var processplant = document.getElementById('tprocessplant');
	var compressedair = document.getElementById('tcompressedair');
	var spaceandwaterheating = document.getElementById('tspaceandwaterheating');
	var lighting = document.getElementById('tlighting');
	var aircon = document.getElementById('taircon');
	var fridge = document.getElementById('tfridge');
	var office = document.getElementById('toffice');
	var electricmotors = document.getElementById('telectricmotors');

	// set them all to invisible
	buildings.style.display = 'none';
	processplant.style.display = 'none';
	compressedair.style.display = 'none';
	spaceandwaterheating.style.display = 'none';
	lighting.style.display = 'none';
	aircon.style.display = 'none';
	fridge.style.display = 'none';
	office.style.display = 'none';

	// get value of dropdown
	var dropdown = document.getElementById('btype');
	var selected = dropdown.selectedIndex;

	// show relevant boxes
	if(selected == 1) {
		buildings.style.display = 'block';
		processplant.style.display = 'block';
	}
	else if(selected == 2) {
		buildings.style.display = 'block';
		processplant.style.display = 'block';
	}
	else if(selected == 3) {
		buildings.style.display = 'block';
		compressedair.style.display = 'block';
		spaceandwaterheating.style.display = 'block';
		lighting.style.display = 'block';
		electricmotors.style.display = 'block';
		processplant.style.display = 'block';
	}
	else if(selected == 4) {
		aircon.style.display = 'block';	
		buildings.style.display = 'block';
		spaceandwaterheating.style.display = 'block';
		lighting.style.display = 'block';
		fridge.style.display = 'block';
	}
	else if(selected == 5) {
		aircon.style.display = 'block';
		buildings.style.display = 'block';
		spaceandwaterheating.style.display = 'block';
		lighting.style.display = 'block';
		office.style.display = 'block';
	}
	else if(selected == 6) {
		buildings.style.display = 'block';
		spaceandwaterheating.style.display = 'block';
		lighting.style.display = 'block';
		office.style.display = 'block';
	}
	else if(selected == 7) {
		aircon.style.display = 'block';
		buildings.style.display = 'block';
		spaceandwaterheating.style.display = 'block';
		lighting.style.display = 'block';
		office.stye.display = 'block';
	}
	else if(selected == 8) {
		aircon.style.display = 'block';
		buildings.style.display = 'block';
		spaceandwaterheating.style.display = 'block';
		lighting.style.display = 'block';
	}
	else
	{
		alert('Please choose a category for your business');return;
	}

	var results = document.getElementById('results');
	results.style.display = 'block';

	// get score
	var no1 = getMyValue(document.myForm.elements[0]);
	var no2 = getMyValue(document.myForm.elements[2]);
	var no3 = getMyValue(document.myForm.elements[4]); 
	var no4 = getMyValue(document.myForm.elements[6]);
	var no5 = getMyValue(document.myForm.elements[8]);
	var no6 = getMyValue(document.myForm.elements[10]);
	var no7 = getMyValue(document.myForm.elements[12]);
	var no8 = getMyValue(document.myForm.elements[14]);
	var no9 = getMyValue(document.myForm.elements[16]);
	var no10 = getMyValue(document.myForm.elements[18]);

	var percent = no1 + no2 + no3 + no4 + no5 + no6 + no7 + no8 + no9 + no10;

	// hide form
	var hideme = document.getElementById('hideme');
	hideme.style.display = 'none';

	// show it
	var energyscore = document.getElementById('energyscore');
	var energytext = document.getElementById('energytext');

	energyscore.innerHTML = percent * 10 + "%";


	// energy score related text
	if(percent == 0) {
		energytext.innerHTML = 'A very low score. Your business is just becoming energy aware and there is a lot of room for improvement. However, you have taken the first step by visiting our site so you are moving in the right direction! Read the information below and try to implement the savings ideas that are applicable to your business.';
	}
	else if(percent == 1) {
		energytext.innerHTML = 'A low score. Your business is just becoming energy aware and there is a lot of room for improvement. However, you have taken the first step by visiting our site so you are moving in the right direction! Read the information below and try to implement the savings ideas that are applicable to your business. ';
	}
	else if(percent == 2) {
		energytext.innerHTML = 'A fairly low score. Your business is just becoming energy aware and there is a lot of room for improvement. However, you have taken time to visit our site so you are moving in the right direction! Read the information below and try to implement the savings ideas that are applicable to your business. ';
	}
	else if(percent == 3) {
		energytext.innerHTML = 'A fairly low score. Your business is just becoming energy aware and there is still room for improvement. However, you have taken time to visit our site so you are moving in the right direction! Read the information below and try to implement the savings ideas that are applicable to your business. ';
	}
	else if(percent == 4) {
		energytext.innerHTML = 'A below average score! Your business is just becoming energy aware and there is still some room for improvement. However, you have taken time to visit our site so you are moving in the right direction! Read the information below and try to implement the savings ideas that are applicable to your business. ';
	}
	else if(percent == 5) {
		energytext.innerHTML = 'An average score! Your business is energy aware, but there is some room for improvement. Take some time to read the information below and try to implement the savings ideas that are applicable to your business. ';
	}
	else if(percent == 6) {
		energytext.innerHTML = 'A good score! Your business is energy aware, but there is still some room for improvement. Take some time to read the information below and try to implement the savings ideas that are applicable to your business.';
	}
	else if(percent == 7 || percent == 8) {
		energytext.innerHTML = 'Well done! Your business is very energy aware, but there is still room for improvement. Take some time to read the information below and try to implement the savings ideas that are applicable to your business.';
	}
	else if(percent == 9) {
		energytext.innerHTML = 'Well done! Your business is extremely energy aware. There are still a few ways to save energy that you have not thought about. Take some time to read the information below and try to implement the savings ideas that are applicable to your business.';
	}
	else if(percent == 10) {
		energytext.innerHTML = 'Well done! Congratulations on achieving a perfect score. However, we bet there are still a few ways to save energy that you may not have thought about. Take some time to read the information below and try to implement the savings ideas that are applicable to your business.';
	}

}

function getMyValue(obj)
{
	if(obj.checked)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function getRadioValue(obj)
{
	for(var i = 0; i < obj.length; i++)
	{
		if(obj[i].checked)
		{
			return obj[i].value;
		}
	}
	return null;
}