// JavaScript Document
/*
Name:	estimateBill
Purpose:to estimate the user's annual bill and give them feedback as to if they are using too much electricity
Input:  A HTML form 
Output: N/A
Notes:  This function is based on the simple bill estimator provided in an excell document
*/
function estimateBill(theFrm)
{
	theFrm.txt_typical_may.value = formatCurrency(checkTypicalMay(theFrm.num_occupants.value));
	
	//calcultate estimated annual heating bill
	theFrm.txt_annual_heating_costs.value = formatCurrency(roundToTwoPlaces((parseFloat(theFrm.txt_winter_bill.value) - parseFloat(theFrm.txt_spring_bill.value))/0.2));
	
	//calcultate estimated annual cooling bill
	if(parseFloat(theFrm.txt_summer_bill.value) > parseFloat(theFrm.txt_spring_bill.value))
	{
		theFrm.txt_estimated_annual_cooling_costs.value = formatCurrency(roundToTwoPlaces((parseFloat(theFrm.txt_summer_bill.value) - parseFloat(theFrm.txt_spring_bill.value))/.34));
	}
	else theFrm.txt_estimated_annual_cooling_costs.value = formatCurrency(0);
	
	//calculate the estimated total costs per year for estimated other costs calculation
	theFrm.txt_total.value =  formatCurrency(roundToTwoPlaces((parseFloat(theFrm.txt_spring_bill.value) * 12) + parseFloat(theFrm.txt_annual_heating_costs.value) + parseFloat(theFrm.txt_estimated_annual_cooling_costs.value)));
	
	//calculate the other electical costs for the year
	temp = parseFloat(theFrm.txt_annual_heating_costs.value) + parseFloat(theFrm.txt_estimated_annual_cooling_costs.value);
	tempTotal = (parseFloat(theFrm.txt_spring_bill.value) * 12) + parseFloat(theFrm.txt_annual_heating_costs.value) + parseFloat(theFrm.txt_estimated_annual_cooling_costs.value);
	//alert((parseFloat(theFrm.txt_spring_bill.value) * 12) + parseFloat(theFrm.txt_annual_heating_costs.value) + parseFloat(theFrm.txt_estimated_annual_cooling_costs.value));
	theFrm.txt_estimated_annual_other_costs.value = formatCurrency(roundToTwoPlaces(tempTotal - temp));
	
	return false;
}

function checkTypicalMay(myNumber)
{
	switch(parseInt(myNumber))
	{
		case 1: return 50; break;
		case 2: return 72; break;
		case 3: return 100; break;
		case 4: return 117; break;
		case 5: return 126; break;
		case 6: return 150; break;
		case 7: return 155; break;
		default: return "Occupants out of range"; break;
	}
}

function roundToTwoPlaces(input)
{
	temp = parseFloat(input);
	temp = temp * 100;
	temp = Math.ceil(temp);
	return temp / 100;
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
//return (((sign)?'':'-') + '$' + num + '.' + cents);//uncomment for $ symbol to be included in the returned value
return (((sign)?'':'-') + num + '.' + cents);
}
