	/* Round the dollar amount into 2 decimal places */
	function cent(amount) 
	{
		amount -= 0;
		amount = Math.round(amount * 100)/100;
		return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
	}//cent
		
	/*  Calculate the monthly and yearly cost for each appliances */
	function calGasCost(field) 
	{
	    var id = field.name.substr(6);
		var f = field.form;

		var appUsage=0, galBathUse=0, BTU=0, gasRate=0, monthlyCost=0, yearlyCost=0, therm1=0.16, therm2=0.08, therm3=0.12;
		
		var unitSelector = f.elements["option" + id];
		var strUnit = unitSelector.options[unitSelector.selectedIndex].value;

		var tempSelector = f.elements["optionClothesWasher"];
		var temp = tempSelector.options[tempSelector.selectedIndex].value;
		
		var tempSelector2 = f.elements["optionFrontLoadWasher"];
		var temp2 = tempSelector2.options[tempSelector2.selectedIndex].value;
		
		appUsage = parseFloat(f.elements["usages" + id].value);
		galBathUse = parseFloat(f.elements["bathtub"].value);

		BTU = parseFloat(f.elements["therms" + id].value);
		gasRate = parseFloat(f.elements["price"].value);  //cost charge on gas usage per therm
		
		/*
		  24 hours/day
		  168 hours/week
		  730.5 hours/month
		  30.4375 days/month     (365.25/12)
		*/
		if(strUnit == "minutes/day") {								//calculate cost per day for range
		  	appUsage = ((appUsage/60)/24) * 730.5;
			monthlyCost = appUsage * (BTU/100000) * gasRate ;  
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "hours/day")	{							//calculate cost per day for range
		  	appUsage = (appUsage/24) * 730.5;
			monthlyCost = appUsage * (BTU/100000) * gasRate ;
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "hours/week") {							//calculate cost per week for range
			appUsage = (appUsage/168) * 730.5;
			monthlyCost = appUsage * (BTU/100000) * gasRate ;
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "hours/month") {							//calculate cost per month for range
		 	appUsage = appUsage * 1;
			monthlyCost = appUsage * (BTU/100000) * gasRate ;
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "fireplace") {							//calculate cost per year for fireplace
			if(isNaN(appUsage) || (appUsage == 0)){
				monthlyCost = 0; 									//assume the monthly hour usage of swimming pool = 100 hours
				yearlyCost = 0;
			}
			else{
				monthlyCost = 20 * (BTU/100000) * gasRate ; 		//assume the monthly hour usage of swimming pool = 100 hours
				yearlyCost = monthlyCost * appUsage;
			}
		}//if
		/************************************************************************************************************/		
		else if(strUnit == "swimmingPool") {						//calculate cost for swimming pool heater							
			if(isNaN(appUsage) || (appUsage == 0)){
				monthlyCost = 0; 									//assume the monthly hour usage of swimming pool = 100 hours
				yearlyCost = 0;
			}
			else{
				monthlyCost = 100 * (BTU/100000) * gasRate ; 		//assume the monthly hour usage of swimming pool = 100 hours
				yearlyCost = monthlyCost * appUsage;
			}
		}//if
		/************************************************************************************************************/
		else if(strUnit == "loads/day") {							//calculate cost per day for clothes dryer
			appUsage = appUsage * 30.4375;   						//assume that usage is load
			monthlyCost = appUsage * (BTU/100000) * gasRate ;  
			yearlyCost = monthlyCost * 12;			
		}//if
		else if(strUnit == "loads/week") {							//calculate cost per week for clothes dryer
			appUsage = appUsage * 4;    							//assume that usage is load
			monthlyCost = appUsage * (BTU/100000) * gasRate ;  
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "loads/month") {							//calculate cost per month for clothes dryer
			appUsage = appUsage * 1;   								//assume that usage is load
			monthlyCost = appUsage * (BTU/100000) * gasRate ;  
			yearlyCost = monthlyCost * 12;
		}//if
		/************************************************************************************************************/
		else if(strUnit == "dLoads/day") {							//calculate cost cost per day for dishwasher
			appUsage = appUsage * 30.4375;   						// # of load used per day x 30 days
			monthlyCost = appUsage * gasRate * therm3;  
			yearlyCost = monthlyCost * 12;			
		}//if
		else if(strUnit == "dLoads/week") {							//calculate cost per week for dishwasher
			appUsage = appUsage * 4;    							// # of load used per day x 4 weeks
			monthlyCost = appUsage  * gasRate * therm3;  
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "dLoads/month") {						//calculate cost per month for dishwasher
			appUsage = appUsage * 1;   								// # of load used per day x 1 month
			monthlyCost = appUsage * gasRate * therm3;  
			yearlyCost = monthlyCost * 12;
		}//if

		/************************************************************************************************************/
		else if(strUnit == "cLoads/day") {							//calculate cost per day for clothes washer
			appUsage = appUsage * 30.4375;    						// # of load used per day x 30 days
			if(temp == "Hot") {
				monthlyCost = appUsage * gasRate * therm1;
				yearlyCost = monthlyCost * 12;
			}//if
			else if(temp == "Warm") {
				monthlyCost = appUsage * gasRate * (therm1/2);
				yearlyCost = monthlyCost * 12;
			}//if
			else { //temp =="Cold"
				monthlyCost = appUsage * 0;
				yearlyCost = monthlyCost * 12;
			}//if					
		}//if
		else if(strUnit == "cLoads/week") {							//calculate cost per week for clothes washer
			appUsage = appUsage * 4;     							// # of load used per day x 4 weeks
			if(temp == "Hot") {
				monthlyCost = appUsage  * gasRate * therm1;
				yearlyCost = monthlyCost * 12;
			}//if
			else if(temp == "Warm") {
				monthlyCost = appUsage * gasRate * (therm1/2);
				yearlyCost = monthlyCost * 12;
			}//if
			else { //if(temp == "Cold") 
				monthlyCost = appUsage * 0;
				yearlyCost = monthlyCost * 12;
			}//if	
		}//if
		else if(strUnit == "cLoads/month") {						//calculate cost per month for clothes washer
			appUsage = appUsage * 1;   								// # of load used per day x 1 month
			if(temp == "Hot") {
				monthlyCost = appUsage  * gasRate * therm1;
				yearlyCost = monthlyCost * 12;
			}//if
			else if(temp == "Warm") {
				monthlyCost = appUsage * gasRate * (therm1/2);
				yearlyCost = monthlyCost * 12;
			}//if
			else { //if(temp == "Cold") 
				monthlyCost = appUsage * 0;
				yearlyCost = monthlyCost * 12;
			}//if
		}//if
		/************************************************************************************************************/
		else if(strUnit == "FLoads/day") {							//calculate cost per day for front load washer
			appUsage = appUsage * 30.4375;			  				// # of load used per day x 30 days
			if(temp2 == "Hot") {
				monthlyCost = appUsage * gasRate * therm2;
				yearlyCost = monthlyCost * 12;
			}//if
			else if(temp2 == "Warm") {
				monthlyCost = appUsage * gasRate * (therm2/2);
				yearlyCost = monthlyCost * 12;
			}//if
			else { //if(temp2 == "Cold") 
				monthlyCost = appUsage * 0;
				yearlyCost = monthlyCost * 12;
			}//if					
		}//if
		else if(strUnit == "FLoads/week") {							//calculate cost per week for front load washer
			appUsage = appUsage * 4;     							// # of load used per day x 4 weeks
			if(temp2 == "Hot") {
				monthlyCost = appUsage * gasRate * therm2;
				yearlyCost = monthlyCost * 12;
			}//if
			else if(temp2 == "Warm") {
				monthlyCost = appUsage * gasRate * (therm2/2);
				yearlyCost = monthlyCost * 12;
			}//if
			else { //if(temp == "Cold") 
				monthlyCost = appUsage * 0;
				yearlyCost = monthlyCost * 12;
			}//if	
		}//if
		else if(strUnit == "FLoads/month") {						//calculate cost per month for front load washer
			appUsage = appUsage * 1;   								// # of load used per day x 1 month
			if(temp2 == "Hot") {
				monthlyCost = appUsage * gasRate * therm2;
				yearlyCost = monthlyCost * 12;
			}//if
			else if(temp2 == "Warm") {
				monthlyCost = appUsage * gasRate * (therm2/2);
				yearlyCost = monthlyCost * 12;
			}//if
			else { //if(temp == "Cold") 
				monthlyCost = appUsage * 0;
				yearlyCost = monthlyCost * 12;
			}//if
		}//if
		/************************************************************************************************************/
		else if(strUnit == "showersMin/day") {							//calculate cost per day for showers
			var therm = (((appUsage * 2.5) * 8 * 65)/66000);
			monthlyCost = therm * gasRate * 30.4375;		// number of showers used per day x 30.4375 days
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "showersMin/week") {						//calculate cost per week for showers
			var therm = (((appUsage * 2.5) * 8 * 65)/66000);			
			monthlyCost = therm * gasRate * 4;			// number of showers used per day x 4 weeks
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "showersMin/month") {						//calculate cost per month for showers
			var therm = (((appUsage * 2.5) * 8 * 65)/66000);
			monthlyCost = therm * gasRate * 1;			
			yearlyCost = monthlyCost * 12;
		}//if
		/************************************************************************************************************/
		else if(strUnit == "baths/day") {							//calculate cost per day for baths
			var therm = ((galBathUse * 8 * 65)/66000);
			monthlyCost = appUsage * therm * gasRate * 30.4375;	// # of bath used per day x 30.4375 days
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "baths/week") {							//calculate cost per week for baths
			var therm = ((galBathUse * 8 * 65)/66000);
			monthlyCost = appUsage * therm * gasRate * 4;			// # of bath used per day x 4 weeks
			yearlyCost = monthlyCost * 12;
		}//if
		else if(strUnit == "baths/month") {							//calculate cost per month for baths
			var therm = ((galBathUse * 8 * 65)/66000);
			monthlyCost = appUsage * therm * gasRate * 1;			// # of bath used per day x 1 month
			yearlyCost = monthlyCost * 12;
		}//if

		f["moCost" + id].value = cent(parseFloat(monthlyCost) || "0.00");
		f["yrCost" + id].value = cent(parseFloat(yearlyCost) || "0.00");
		
		calcTotalCost(f);	
	}//calGasCost()

    /* Check and uncheck the checkbox for the clothes dryer - pilot light appliance */
	function appCheck(field)   
	{
		var id = field.name.substr(6);
		var f = field.form;
		var monthlyCost=0, yearlyCost=0;
		
		var BTU = parseFloat(f.elements["therms" + id].value);
		var gasRate = parseFloat(f.elements["price"].value);
		var therm = (BTU/100000);

		if (f.elements["ckboxs" + id].checked) {
			monthlyCost = therm * gasRate * 24 * 30.4375;
			yearlyCost = monthlyCost * 12;
			
			f["moCost" + id].value = cent(parseFloat(monthlyCost) || "0.00");
			f["yrCost" + id].value = cent(parseFloat(yearlyCost) || "0.00");
		}//if
		else { //if (!f.elements["ckboxs" + id].checked)
		  	f["moCost" + id].value = "0.00";
			f["yrCost" + id].value = "0.00";
		}//if
		calcTotalCost(f);
	}//appCheck
	
	/*  Total all the gas appliances cost for the monthly and yearly  */
	function calcTotalCost(f)
	{
     	var i=1, monthTotal=0, yearTotal=0;

		while((f["usages" + i]) || (f["ckboxs"+i])) {
			monthTotal += parseFloat((f["moCost" + i].value) || "0.00");
	  		yearTotal += parseFloat((f["yrCost" + i].value) || "0.00");
	       ++i;	       
	    }//while
		
	    f["TotalMonthCost"].value = cent(parseFloat(monthTotal));
	    f["TotalYearCost"].value = cent(parseFloat(yearTotal));
	}//calcTotalCost()
	
	/* This function recalculate (update) all the individual fields */
	function recalIndividualFields(f) 
	{
	    var i=1;
	    while((f["usages" + i]) || (f["ckboxs"+i])) {
			if(f["usages" +i]) {
				calGasCost(f["usages"+ i]);
			}//if
			if(f["ckboxs"+ i]) {
				appCheck(f["ckboxs"+ i]);
			}
			++i;
	    }//while
	}//recalIndividualFields
