function init() { /* The standard YUI onready */
	var validator = {
		/* "static variables" */
		maxConsumption : {electricity:70000,natural_gas:7000,no2:7000,propane:7000,kerosene:7000}, 
		validate : function(form){
				var valid = true;
			try {
				/* clear error messages */
				var errorHeading = document.getElementById("errorHeading")
					errorHeading.style.display = "none";
				for(var idx = 1; idx < 21; idx++){
					this.hideError(idx);
					}
				/* Validate Home */
				valid = this.validateZip(form.zip) && valid;
				valid = this.validatePeople(form.people) && valid;
				valid = this.validateArea(form.area) && valid;
				/* Validate Fuels */
				valid = this.validateFuels(form.energy_source_id) && valid;
				/* Validate Energy Use */
				valid = this.validateBilling(form) && valid;
				valid = this.validateStartDate(form) && valid;
				/* client-side fuel validation is incomplete */
				valid = (form.dm_A.checked? this.validateAnnual(form) : this.validateMonthly(form)) && valid;
				}
			catch(e) {
				valid = false;
				//document.getElementById('error_1').style.display = "";
				this.showError(1);
				}
				return valid;
			},
			
		validateZip : function (zip) {
			var isValid = false;
			if(zip.value){
				zip = zip.value;
				zip = zip.substring(0, 5);/* Bogus ZIP can still be passed from URL, so we sanitize here */
				var regexZip = new RegExp(/(^\d{5}$)/);
				isValid = regexZip.test(zip) && zip > 500; /* test against regex and the lowest actual ZIPs */
				}
			if(!isValid){	/* Unhide associated error message */
				//document.getElementById('error_2').style.display = "";
				this.showError(2);
				}
			return isValid;
			},
			
		 validatePeople : function (people){
		 	var isValid = false;
		 	if(people.value){
		 		people = Number(people.value);
		 		isValid = people > 0 && people < 11;		 		
				}
			if(!isValid){	/* Unhide associated error message */
				if(people <= 0 || isNaN(people)){ document.getElementById('error_3').style.display = ""; } /* not a good number */
				else if(people > 10){ document.getElementById('error_12').style.display = "";	} /* must be too many then*/
				}
		 	return isValid;
			},
			
		validateArea : function (area) {
			var isValid = false;
			if(area.value){
				area = Number(area.value);
				isValid = area > 0 && area <= 15000;
				}
			if(!isValid){
				if(area <= 0 || isNaN(area)){ document.getElementById('error_4').style.display = ""; }
				else if(area > 15000){ document.getElementById('error_13').style.display = ""; }
				}
			return isValid;
			},
			
		validateFuels : function (fuels){
			var isValid = false;
			var fuelCount = 0;
			if(fuels){
				for(var fuel = 0; fuel < fuels.length; fuel++){
					if(fuels[fuel].checked){
						fuelCount += 1;
						}
					}
				isValid = fuelCount < 3;
				}
			if(!isValid){
				document.getElementById('error_5').style.display = "";
				}
			return isValid;
			},
		
		validateBilling : function(form){
			var isValid = false;
			if(form.dm_A && form.dm_M){
				var c1 = form.dm_A.checked;
				var c2 = form.dm_M.checked;
				isValid = (!c1 != !c2);
				} 
			if(!isValid){
				this.showError(6);
				}
			return isValid;
			},
			
		validateStartDate : function (form){
			var isValid = false;
			if(form.year.value){
				if(form.month.value){
					var year = Number(form.year.value);
					var month = Number(form.month.value);
					var now = new Date();
					var thisYear = now.getYear() + 1900;
					var thisMonth = now.getMonth() + 1;
					var span = (thisYear - year)*12 + (thisMonth - month);
					isValid = span > 12;
					if(!isValid){this.showError(11);}
				} else { /* no month */
					isValid = false;
					this.showError(7);
					}
				} else { /* no year */
					isValid = false;
					this.showError(8);
					}
			return isValid;
			},
		
		validateAnnual : function(form){
			var isValid = false;
			if(form.dm_A.checked){ /* sanity check */
				var temp = false;
				if(form.A_ue1.value > 0){
					var eValid = this.checkFuelTotal(Number(form.A_ue1.value),"electricity");
					if (eValid == false) {
						this.showError(19);
					}					
					isValid = eValid;
				} else {
					isValid = false;
					this.showError(18);
				}
				if(form.energy_source_id_n.checked){/* natural gas */
					if (form.A_un1.value > 0) {
						var ngValid = this.checkFuelTotal(Number(form.A_un1.value),"natural_gas");
						if (ngValid == false) {
							this.showError(14);
							}
						isValid &= ngValid;	
					}else{
						isValid = false;
						this.showError(18);
						}
				}
				if(form.energy_source_id_f.checked){/* fuel oil */
					if (form.A_uf1.value > 0) {
						var oValid = this.checkFuelTotal(Number(form.A_uf1.value),"no2");
						if (oValid == false) {
							this.showError(15);
							}
						isValid &= oValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_p.checked){/* propane */
					if (form.A_up1.value > 0) {
						var pValid = this.checkFuelTotal(Number(form.A_up1.value),"propane");
						if (pValid == false) {
							this.showError(16);
							}
						isValid &= pValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_k.checked){/* kerosene */
					if (form.A_uk1.value > 0) {
						var kValid = this.checkFuelTotal(Number(form.A_uk1.value),"kerosene");
						if (kValid == false) {
							this.showError(17);
							}
						isValid &= kValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_c.checked){/* coal */
					if (form.A_uc1.value > 0) {
						var cValid = true;
						isValid &= cValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_w.checked){/* wood */
					if (form.A_uw1.value > 0) {
						var wValid = true;
						isValid &= wValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
			}
			return isValid;
			},

		validateMonthly : function(form){
			var isValid = false;
			if(form.dm_M.checked){ /* sanity check */
				var temp = false;
				var electricityValue = 0;
				for(var i = 1; i < 12; i++){
					var element = eval("form.M_ue" + i );
					if (!isNaN(parseFloat(element.value))) {
						electricityValue = electricityValue + Number(element.value);
					}
				}
				if(electricityValue > 0){
					isValid = this.checkFuelTotal(Number(electricityValue),"electricity");
					if (isValid == false) {
						this.showError(19);
					}					
				} else {
					isValid = false;
					this.showError(18);
				}
				if(form.energy_source_id_n.checked){/* natural gas */
					var naturalgasValue = 0;
					for(var i = 1; i < 12; i++){
						var element = eval("form.M_un" + i );
						if (!isNaN(parseFloat(element.value))) {
							naturalgasValue = naturalgasValue + Number(element.value);
						}
					}					
					if (naturalgasValue > 0) {
						var ngValid = this.checkFuelTotal(Number(naturalgasValue),"natural_gas");
						if (ngValid == false) {
							this.showError(14);
						}
						isValid &= ngValid
					}else{
						isValid = false;
						this.showError(18);
					}
				}				
				if(form.energy_source_id_f.checked){/* fuel oil */
					var fueloilValue = 0;
					for(var i = 1; i < 12; i++){
						var element = eval("form.M_uf" + i );
						if (!isNaN(parseFloat(element.value))) {
							fueloilValue = fueloilValue + Number(element.value);
						}
					}					
					if (fueloilValue > 0) {
						var oValid = this.checkFuelTotal(Number(fueloilValue),"no2");
						if (oValid == false) {
							this.showError(15);
						}
						isValid &= oValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_p.checked){/* propane */
					var propaneValue = 0;
					for(var i = 1; i < 12; i++){
						var element = eval("form.M_up" + i );
						if (!isNaN(parseFloat(element.value))) {
							propaneValue = propaneValue + Number(element.value);
						}
					}					
					if (propaneValue > 0) {
						var pValid = this.checkFuelTotal(Number(propaneValue),"propane");
						if (pValid == false) {
							this.showError(16);
						}
						isValid &= pValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_k.checked){/* kerosene */
					var keroseneValue = 0;
					for(var i = 1; i < 12; i++){
						var element = eval("form.M_uk" + i );
						if (!isNaN(parseFloat(element.value))) {
							keroseneValue = keroseneValue + Number(element.value);
						}
					}					
					if (keroseneValue > 0) {
						var kValid = this.checkFuelTotal(Number(keroseneValue),"kerosene");
						if (kValid == false) {
							this.showError(17);
						}
						isValid &= kValid; 
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_c.checked){/* coal */
					var coalValue = 0;
					for(var i = 1; i < 12; i++){
						var element = eval("form.M_uc" + i );
						if (!isNaN(parseFloat(element.value))) {
							coalValue = coalValue + Number(element.value);
						}
					}					
					if (coalValue > 0) {
						var cValid = true;
						isValid &= cValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				if(form.energy_source_id_w.checked){/* wood */
					var woodValue = 0;
					for(var i = 1; i < 12; i++){
						var element = eval("form.M_uw" + i );
						if (!isNaN(parseFloat(element.value))) {
							woodValue = woodValue + Number(element.value);
						}
					}					
					if (woodValue > 0) {
						var wValid = true;
						isValid &= wValid;
					}else{
						isValid = false;
						this.showError(18);
					}
				}
				}
			return isValid;
			},
		
		/* Helper functions */
		checkFuelTotal : function(total, type){
			var max = this.maxConsumption[type];
			return total > 0 && total <= max;
			},
		
		showError : function(error_id){
			document.getElementById("error_"+error_id).style.display = "";
			},

		hideError : function(error_id){
			document.getElementById("error_"+error_id).style.display = "none";
			},
		
		/* TODO: is this even used? */
		isBetween : function(args, number, min, max){ /* test if numeric value is within min/max exclusive */
			/*  if(args is Object){get args members} */ 
			number = Number(number);
			min = Number(min);
			max = Number(max);
			return number < max && number > min;
			}
		};

	function validateGetStartedForm(event){
		var form = this;
		/* validate inputs*/
		var valid = validator.validate(form);
		if(!valid){
			YAHOO.util.Event.preventDefault(event);
			var errorHeading = document.getElementById("errorHeading")
			errorHeading.style.display = "";
			jQuery.scrollTo(errorHeading);
			}
		else { /* Submission data is valid - clean the cost input values */
			jQuery(".cost").each(function(){
				var tempCost = "";
				var tempCostL = "";
				var tempCostR = "00";
				if(this.value){ /* truncate the LHS and RHS and concatenate = total cents, no rounding.  Then divide by 100 */
					tempCost = this.value.toString().split(".");
					tempCostL = (tempCost[0].length > 12? tempCost[0].slice(tempCost[0].length - 12) : tempCost[0]);
					if(tempCost.length > 1){
						tempCostR =	(tempCost[1].length > 2? tempCost[1].slice(0,2) : tempCost[1]);
						}
					tempCost = (tempCostL + tempCostR)/100;
					this.value = tempCost.toFixed(2);
					}
				});
			}
		}
	
	YAHOO.util.Event.addListener("showGetStartedForm", "submit", validateGetStartedForm); 
	}

if(YAHOO){
	YAHOO.util.Event.onDOMReady(init);
	}
	
