function init()
{
	var theCompare1 = document.getElementById("compare_1");
	var theCompare2 = document.getElementById("compare_2");
	var theUnit1 = document.getElementById("unit1");
	var theUnit2 = document.getElementById("unit2");
	var thePrice1 = document.getElementById("price1");
	var thePrice2 = document.getElementById("price2");
	var theButton = document.getElementById("calculate"); 
	var theGraph = document.getElementById("graph"); 
	
	if (theCompare1 && theCompare2 && theUnit1 && theUnit2 && thePrice1 && thePrice2 && theButton && theGraph)
	{	
		theCompare1.unit = theUnit1;
		theCompare2.unit = theUnit2;

		theCompare1.price = thePrice1;
		theCompare2.price = thePrice2;
				
		theCompare1.onchange = function () {
			setUnit(this, this.unit, this.price);
		}

		theCompare2.onchange = function () {
			setUnit(this, this.unit, this.price);
		}


		theCompare1.selectedIndex = 0;
		theCompare2.selectedIndex = 0;
		theUnit1.innerHTML = "";
		theUnit2.innerHTML = "";
		thePrice1.value = "";
		thePrice2.value = "";		
		
		theButton.onclick = function () {
			var c1_value = theCompare1.options[theCompare1.selectedIndex].value;
			var c2_value = theCompare2.options[theCompare2.selectedIndex].value;
			var price1_value = thePrice1.value;
			var price2_value = thePrice2.value;
									
			if (c1_value == "")
			{
				alert("Please select an option for the first Energy Source.");
				theCompare1.focus();
			}
			else if (c2_value == "")
			{
				alert("Please select an option for the second Energy Source.");
				theCompare2.focus();
			}
			else if (c1_value == c2_value)
			{
				alert("The values cannot be the same for both Energy Sources.");
				theCompare1.focus();
			}
			else if (price1_value != "" && ! price1_value.match(/^\d*\.*\d*$/))
			{
				alert("Please enter a valid unit price.");
				thePrice1.focus();
			}
			else if (price2_value != "" && ! price2_value.match(/^\d*\.*\d*$/))
			{
				alert("Please enter a valid unit price.");
				thePrice2.focus();
			}
			else
			{
				theGraph.src = "graph.py?compare_1=" + c1_value + "&compare_2=" + c2_value + "&price1=" + thePrice1.value + "&price2=" + thePrice2.value;
			}
				
		};
		
	}
}

function setUnit(theField, theUnit, thePrice)
{
	if (theField && theUnit && thePrice)
	{
		var theValue = theField.options[theField.selectedIndex].value;
		
		if (theValue && theUnit && thePrice)
		{
			for (var x=0; x<unitArray.length; x++)
			{
				thisArray = unitArray[x];
				if (thisArray[0] == theValue)
				{
					theUnit.innerHTML = " per " + thisArray[1];
					thePrice.value = thisArray[2].toFixed(2);
				}
			}		
		}
		else if (theUnit)
		{
			theUnit.innerHTML = "";
		}
	}
}