/* Disable form submit on enter keypress
// http://www.arraystudio.com/as-workshop/disable-form-submit-on-enter-keypress.html
// Slobodan Kovacevic on March 13, 2005 
// added by Jason Otis march 2009
*/
function disableEnterKey(e) {
	var key;
	if(window.event)
		key = window.event.keyCode;//IE
	else
		key = e.which;//firefox
	if(key == 13)
		return false;
	else
		return true;
}
/* --- */

/* 
//	show/hide sections of calculator
//	added by jason otis / march 2009 
*/

var cal_sections = new Array(["Lighting",false],["Kitchen",false],["Refrigerators",false],["Bathroom",false],["FamilyRoom",false],["Computer",false],["MajorHouseholdItems",false],["Outdoor",false],["PowerSaws",false],["ChristmasLights",false],["Miscellaneous",false]);

function toggleSection (section) {
	for (var i=0; i<cal_sections.length; i++) {
		if (section == cal_sections[i][0]) {
			if (document.getElementById("table-" + cal_sections[i][0])) {
				if (!cal_sections[i][1]) {
					cal_sections[i][1] = true;
					document.getElementById("table-" + cal_sections[i][0]).style.display = "block";
					document.getElementById("link-" + cal_sections[i][0]).className = "calc_close";
				} else {
					cal_sections[i][1] = false;
					document.getElementById("table-" + cal_sections[i][0]).style.display = "none";
					document.getElementById("link-" + cal_sections[i][0]).className = "calc_open";
				}
			}
		}
	}
}

/* --- */

var perhourcost = 0.11612;
var eepRate = 0.00193;
var taxRate = 0.05;
var taxRate2 = 1.05;
	
function isNumber(theField){
	if (theField.value == "") {
		return true;
	}

	if (isNaN(parseFloat(theField.value))) {
		alert("The field must contain a number.");
		theField.focus();
		return false;
	}
	return true;
}

function makeNumber(theValue) {
	if (theValue == "") {
		return "0";
	}
	return theValue;
}

function format(expr, decplaces) {
	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function getAppliance(theName) {
	if (theName.indexOf("num_") != -1) {
		return theName.substring(4, theName.length);
	}
	else {
		return theName.substring(5, theName.length);
	}
}

function calculateItemDay(theElem) {
	if (!isNumber(theElem)){
		theElem.focus();
		return;
	}

	var applName = getAppliance(theElem.name);
	var the_form = theElem.form;
	var aw = makeNumber(the_form.elements["aw_" + applName].value);
	var num = makeNumber(the_form.elements["num_" + applName].value);
	var hour = makeNumber(the_form.elements["hour_" + applName].value);
	var total_kwh = Math.round((aw/1000) * (num * hour) * 30);
	var total_month = (aw/1000) * (num * hour) * perhourcost * 60;
	/* var total_year = (aw/1000) * (num * hour) * perhourcost * 365; */
  var total_year = total_month * 6;

	the_form.elements["total_year_" + applName].value = format(total_year, 2);
	the_form.elements["total_kwh_" + applName].value = total_kwh ;
	the_form.elements["total_month_" + applName].value = format(total_month, 2);

	return;
}

function setTotal(theElem, total) {
	var applName = getAppliance(theElem.name);
	if (theElem.checked) {
		theElem.form.elements["total_year_" + theElem.name].value = format(total*12*perhourcost, 2);
	}
	else {
		theElem.form.elements["total_year_" + theElem.name].value = "0";
	}
	if (theElem.checked) {
		theElem.form.elements["total_kwh_" + theElem.name].value = total;
	}
	else {
		theElem.form.elements["total_kwh_" + theElem.name].value = "0";
	}
	if(theElem.checked) {
		theElem.form.elements["total_month_" + theElem.name].value = format( total*2 * perhourcost, 2 );
	}
	else {
		theElem.form.elements["total_month_" + theElem.name].value = "0";
	}		 				 
}

function setTotalHotTub(consumption) {
	document.forms[0].total_kwh_HotTub.value = consumption;
	document.forms[0].total_month_HotTub.value = format(consumption*2 * perhourcost, 2);
	document.forms[0].total_year_HotTub.value = format(consumption*12 * perhourcost, 2);
}

function setTotalWaterHeater(consumption) {          
	document.forms[0].total_kwh_ElectricWaterHeater.value = consumption;
	document.forms[0].total_month_ElectricWaterHeater.value = format(consumption*2 * perhourcost, 2);
	document.forms[0].total_year_ElectricWaterHeater.value = format(consumption*12 * perhourcost, 2);
}

function setTotalPoolPump(consumptionPoolPump,hour_poolpumlhe) {
	if (consumptionPoolPump == 0) {	 
		document.forms[0].total_kwh_poolpumlhe.value =  0;
		document.forms[0].total_month_poolpumlhe.value = format(0, 2);
		document.forms[0].total_year_poolpumlhe.value = format(0, 2);
	}			
	else {
		document.forms[0].total_kwh_poolpumlhe.value = hour_poolpumlhe * 46;
		document.forms[0].total_month_poolpumlhe.value = format(2  * hour_poolpumlhe * 46 * perhourcost, 2);
		document.forms[0].total_year_poolpumlhe.value = format(consumptionPoolPump * 12 * hour_poolpumlhe * 46 * perhourcost, 2);
	}
}

function setTotalPelletStove(consumptionPelletStove,hour_PelletStove) {
	if (consumptionPelletStove == 0) {
		document.forms[0].total_kwh_PelletStove.value = 0;
		document.forms[0].total_month_PelletStove.value = format(0, 2);		       
		document.forms[0].total_year_PelletStove.value = format(0, 2);
	}				
	else {
		document.forms[0].total_kwh_PelletStove.value =  hour_PelletStove * 15;
		document.forms[0].total_month_PelletStove.value = format(2  * hour_PelletStove * 15 * perhourcost, 2);
		document.forms[0].total_year_PelletStove.value = format(consumptionPelletStove * 12 * hour_PelletStove * 15 * perhourcost, 2);
	}
}

function calculateTotals(the_form) {
  /* Usage (kWh) and TOTAL ENERGY CHARGES */
	
	var i = 0;
	var total = 0;
	var kWhdailytotal = 0;
	var kWhmonthlytotal = 0;
	var kWhyearlytotal = 0;
	var HST_total = 0;
  var newTotal = 0;

	for (i = 0; i < the_form.elements.length; i++) {
		if (the_form.elements[i].name.indexOf("total_kwh_") == 0) {
			total += parseFloat(makeNumber(the_form.elements[i].value));
		}
	}

  kWhdailytotal = total;
	kWhmonthlytotal = total * 30;
	kWhyearlytotal = total * 365;

	the_form.kwh_daily_total.value =  format(total ,2) + ' kWh';
	the_form.kwh_monthly_total.value =  format(total *  2,2 )+ ' kWh';
	the_form.kwh_yearly_total.value = format(total *  12 ,2) + ' kWh';
  
	the_form.day_total.value = '$' + format(total * perhourcost, 2);
  the_form.day_total_eep.value = '$' + format(total * eepRate, 2);
  
  newTotal = (total * perhourcost) + (total * eepRate);
  
	the_form.day_total_HST.value = '$' + format(newTotal * taxRate, 2);
	the_form.day_total_wtax.value = '$' + format(newTotal * taxRate2 , 2);
  
  /* totals */

	var i = 0;
	var total = 0;
  var newTotal = 0;
	var HST_total = 0;

	for (i = 0; i < the_form.elements.length; i++) {
		if (the_form.elements[i].name.indexOf("total_month_") == 0) {
			total += parseFloat(makeNumber(the_form.elements[i].value)); 
		}
	}

	the_form.month_total.value = '$' + format(total, 2);
  the_form.month_total_eep.value = '$' + format((kWhdailytotal * 2) * eepRate, 2);
  
  newTotal = total + ((kWhdailytotal * 2) * eepRate);
  
	/* HST_total = '$' + format(total * taxRate, 2); */
  
	the_form.month_total_HST.value = '$' + format(newTotal * taxRate, 2);
  the_form.month_total_wtax.value = '$' + format(newTotal * taxRate2, 2);

  /* Tax */
	var i = 0;
	var total = 0;
  var newTotal = 0;
	var year_total_HST=0;
	var bill_total_HST=0;
	var month_total_HST=0;
	var HST_total=0;

	for (i = 0; i < the_form.elements.length; i++) {
		if (the_form.elements[i].name.indexOf("total_year_") == 0) {
			total += parseFloat(makeNumber(the_form.elements[i].value));
		}				
	}

	the_form.year_total.value = '$' + format(total, 2);
  the_form.year_total_eep.value = '$' + format((kWhdailytotal *  12) * eepRate, 2);
  
  newTotal = total + ((kWhdailytotal *  12) * eepRate);
  
	/* HST_total = the_form.year_total_HST.value; */
	
  the_form.year_total_HST.value ='$'+ format(newTotal * taxRate, 2);
  the_form.year_total_wtax.value = '$'+ format(newTotal * taxRate2, 2);
  
  /*
	for (i = 0; i < the_form.elements.length; i++) {
		if (the_form.elements[i].name.indexOf("total_year_") == 0) {
			total += parseFloat(makeNumber(the_form.elements[i].value));
		}		
	}
  */
}

function clearButtons(buttonGroup,buttonName) {
	document.forms[0].elements['total_kwh_' +  buttonName].value = '';
	document.forms[0].elements['total_month_' +  buttonName].value = '';
	document.forms[0].elements['total_year_' +  buttonName].value = '';

	for ( i=0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked == true) { 
			buttonGroup[i].checked = false;
		}
	}
}

function howMuch(theElem) {
	if(!isNumber(theElem)) {
		theElem.focus();
		return;
	}
	 
	var Wattage = makeNumber(document.forms[0].elements["myWattage"].value);
	var hoursUsed = makeNumber( document.forms[0].elements["myHours"].value);
	var numItems = makeNumber( document.forms[0].elements["myItems"].value);
				
	var total_day = (Wattage/1000) * (numItems * hoursUsed) * perhourcost ;
	var total_month = (Wattage/1000) * (numItems * hoursUsed) * perhourcost * 30;
	var total_year = (Wattage/1000) * (numItems * hoursUsed) * perhourcost *365;

	document.forms[0].elements[ "total_year"].value = '$ ' +format( total_year, 2 );
	document.forms[0].elements[ "total_month"].value = '$ ' +format( total_month, 2 );
	document.forms[0].elements[ "total_day"].value = '$ ' + format( total_day, 2 );
	
	return;
}