/*
	Filename: roi_calculator.js
	Created On: January 9th, 2008
	Author: Chad Norris
	Description: Provides functionality to change labels and combo boxes based on fuel types, fuel measurement units,
		         and 
*/

function checkElectricity(name)
{
	if(name == "electricity_kwh")
	{
		document.getElementById("electricity_month").value = "";
		document.getElementById("electricity_used").value = "";
		
		if(isNaN(document.getElementById("electricity_kwh").value))
			document.getElementById("cost").value = "";
		else
			document.getElementById("cost").value = document.getElementById("electricity_kwh").value;
	}
	
	if(name == "electricity_month" || name == "electricity_used")
	{
		document.getElementById("electricity_kwh").value = ""	;
		var cost_kwh = document.getElementById("electricity_month").value / document.getElementById("electricity_used").value;
		
		if(isNaN(cost_kwh) || cost_kwh == "Infinity")
			document.getElementById("cost").value = "";
		else
			document.getElementById("cost").value = cost_kwh;
	}
}

function checkFuel(name)
{
	if(name == "fuel_cost")
	{
		document.getElementById("fuel_cost_month").value = "";
		document.getElementById("fuel_used").value = "";
		
		if(isNaN(document.getElementById("fuel_cost").value))
			document.getElementById("generated_cost").value = "";
		else
			document.getElementById("generated_cost").value = document.getElementById("fuel_cost").value;
	}
	
	if(name == "fuel_cost_month" || name == "fuel_used")
	{
		document.getElementById("fuel_cost").value = ""	;
		var cost_fuel = document.getElementById("fuel_cost_month").value / document.getElementById("fuel_used").value;
		
		if(isNaN(cost_fuel) || cost_fuel == "Infinity")
			document.getElementById("generated_cost").value = "";
		else
			document.getElementById("generated_cost").value = cost_fuel;
	}		
}

//Change labels and combo boxes when fuel measurement units are changed
function changeUnits()
{
	var elem = document.getElementById("fuel_units");
	var fuel = document.getElementById("fuel_type");
	var label = document.getElementById("fuel_cost_label");
	var fuel_used_label = document.getElementById("used_year_label");
	var prompt_string = "";
	var fuel_used = "";
	

	prompt_string = "Cost of " + fuel.value + " per " + elem.value.replace("_"," ") + ": ";
	fuel_used = "Annual " + elem.value.replace("_"," ") + " of " + fuel.value + " used:";
	
	label.innerHTML = prompt_string;
	fuel_used_label.innerHTML = fuel_used;
	
}

//Change lables and combo boxes when fuel type is selected
function changeFuel()
{
	var elem = document.getElementById("fuel_type");
	var label = document.getElementById("fuel_units_label");
	var bill_label = document.getElementById("fuel_total_label");
	var units = document.getElementById("fuel_units");
	var compare_furnace = document.getElementById("furnace");
	var compare_hw = document.getElementById("water_heater");
	var furnace_compare_text = "Electric Heat,HE ,Mid-eff ,Standard ";
	var furnace_compare_value = "electric,high,med,standard";
	var hw_compare_value = "electric,high,standard";
	var hw_compare_text = "Electric Water Heater,HE ,Standard ";
	var furnace_compare_array = furnace_compare_text.split(",");
	var hw_compare_array = hw_compare_text.split(",");
	var furnace_value_array = furnace_compare_value.split(",");
	var hw_value_array = hw_compare_value.split(",");
	var option_text = "";	
	var furnace_option = "";
	var hw_option = "";
	var option = "";
	
	var gas_units = "Cubic Meters (m3),Therms,Giga-Joules";
	var gas_values_array = "cubic_meters,therms,giga_joules".split(",");
	var other_values_array = "liters,gallons".split(",");
	var propane_units = "Litres, US Gallons";
	var oil_units = "Litres,US Gallons";
	var electricity_units = "None,None";
	
	var unit_array = Array(1);
	
	while(units.hasChildNodes())
	{
		units.removeChild(units.lastChild);
	}
	
	while(compare_furnace.hasChildNodes())
	{
		compare_furnace.removeChild(compare_furnace.lastChild);	
	}
	
	while(compare_hw.hasChildNodes())
	{
		compare_hw.removeChild(compare_hw.lastChild);	
	}	
	
	switch(elem.value)
	{
		case "Gas":
			document.getElementById("fuel_section").style.display = "block";		
			label.innerHTML = "Units Natural Gas is sold in:";
			bill_label.innerHTML = "Total Annual Gas Bill:";
			unit_array = gas_units.split(",");
		break;
		
		case "Propane":
			document.getElementById("fuel_section").style.display = "block";		
			label.innerHTML = "Units Propane is sold in:";
			bill_label.innerHTML = "Total Annual Propane Bill:";
			unit_array = propane_units.split(",");
		break;
		
		case "Oil":
			document.getElementById("fuel_section").style.display = "block";		
			label.innerHTML = "Units Oil is sold in";
			bill_label.innerHTML = "Total Annual Oil Bill:";
			unit_array = oil_units.split(",");
		break;
		
		case "Electricity":
			document.getElementById("fuel_section").style.display = "none";
			label.innerHTML = " ";
			unit_array = electricity_units.split(",");
			document.getElementById("generated_cost").value="0";
			
		break;
	}
	
	
	if(elem.value != "Electricity")
	{
		for(var x = 0; x < furnace_compare_array.length; x++)
		{
			furnace_option = document.createElement("option");
			
			if(furnace_compare_array[x] != "Electric Heat")
				option_text =  furnace_compare_array[x] + elem.value;
			else
				option_text = furnace_compare_array[x];
			
			furnace_option.setAttribute("value",furnace_value_array[x]);
			furnace_option.innerHTML = option_text;
			
			compare_furnace.appendChild(furnace_option);
		}
	}
	else
	{
		furnace_option = document.createElement("option");
		furnace_option.setAttribute("value","electric");
		furnace_option.innerHTML = "Electric Heat";
		compare_furnace.appendChild(furnace_option);
	}
	
	if(elem.value != "Electricity")
	{
		for(var y=0; y < hw_compare_array.length; y++)
		{
			hw_option = document.createElement("option");
			
			if(hw_compare_array[y] != "Electric Water Heater")
				option_text = hw_compare_array[y] + elem.value + " HW";
			else
				option_text = hw_compare_array[y];
				
			hw_option.setAttribute("value",hw_value_array[y]);
			hw_option.innerHTML = option_text;
			
			compare_hw.appendChild(hw_option);
		}
	}
	else
	{
			hw_option = document.createElement("option");
			hw_option.setAttribute("value","electric");
			hw_option.innerHTML = "Electric Water Heater";
			compare_hw.appendChild(hw_option);		
	}
	
	
	
	for(var i = 0; i < unit_array.length; i++)
	{
		option = document.createElement("option");
		if(elem.value == "Gas")
			option.setAttribute("value", gas_values_array[i]);
		else
			option.setAttribute("value", other_values_array[i]);
			
		option.innerHTML = unit_array[i];
		
		units.appendChild(option);
	}
	
	changeUnits();
}
