function get_selected(elm) {
	if ((elm.type == "select-one") || (elm.type == "select-multiple")) {
		value = " ";
		for (var i = 0; i < elm.options.length; i++) {
			if (elm.options[i].selected)
				value += elm.options[i].value;
		}
	}
	else {
		value = null;
	}
	
	return value;
}					

function format_dollars(inAmount) {
	var isNeg = "";
	
	if (inAmount < 0) {
		isNeg = "-";
		inAmount = 0 - inAmount;
	}	
	
	var num = inAmount.toString().replace(/\$|\,|\s/g,'');
	
	if (isNaN(num)) num = "0";
	var cents = Math.floor((num*100+0.5)%100);
	num = Math.floor(num).toString();
	if (isNaN(cents)) return "";
	if (cents < 10) cents = "0" + cents;
	
	return (isNeg + num + '.' + cents);
}

function calc(f) {

	var watts_val = f.inc_watts.value; //get_selected(f.watts);
	var kWh_val = get_selected(f.kWh);
	var cfl_val = f.cfl_watts.value;
	var quantity = f.quantity.value;
	var dailyHours = f.hours.value;
	var inc_bulb_cost = f.inc_bulb_cost.value;
	var cfl_bulb_cost = f.cfl_bulb_cost.value;
	var cfl_bulb_life = get_selected(f.cfl_bulb_life);
	var current_lamp_hours_val = f.current_lamp_hours.value;
	var labor_cost = f.labor.value;

	if (isNaN(watts_val)) watts_val = 0;
	if (isNaN(kWh_val)) kWh_val = 0;
	if (isNaN(current_lamp_hours_val)) current_lamp_hours_val = 0;
	if (isNaN(cfl_val)) cfl_val = 0;
	if (isNaN(quantity)) quantity = 0;
	if (isNaN(dailyHours)) dailyHours = 0;
	if (isNaN(inc_bulb_cost)) inc_bulb_cost = 0;
	if (isNaN(cfl_bulb_cost)) cfl_bulb_cost = 0;
	if (isNaN(labor_cost)) labor_cost = 0;
	
//	if (watts_val == 60) cfl_val = 15;
//	else if (watts_val == 75) cfl_val = 20;
//	else if (watts_val == 100) cfl_val = 27;
	
	inc_cost = monthly_energy_savings(watts_val, kWh_val, dailyHours) * quantity;
	cfl_cost = monthly_energy_savings(cfl_val, kWh_val, dailyHours) * quantity;
	savings = inc_cost - cfl_cost;
	
	switch_cost = (cfl_bulb_cost-inc_bulb_cost)*quantity;
	payback_period = switch_cost/savings;
	
	
	var LampLife = 1000;	// average lamplife in hours for incandescant bulb
	var sLampLife = cfl_bulb_life;  // average lifetime for springlamps in hours

	// now calculate the savings after payback using springlamps
	// (life of bulb in hours / hours burned per month) - number of months for ROI
	/* Making changes in all this 1/17/03
	LifeAfterPayback = sLampLife/(dailyHours*30) - payback_period;       //in months
	total_energy_savings = LifeAfterPayback * savings;
	cfl_incremental_savings = ((cfl_bulb_life/current_lamp_hours_val)*inc_bulb_cost) - cfl_bulb_cost;
	ext_labor_cost = cfl_incremental_savings* quantity * labor_cost;
	bulb_replacement_cost = cfl_incremental_savings * quantity * inc_bulb_cost;
	long_term_savings = total_energy_savings + ext_labor_cost + bulb_replacement_cost;	
	*/
	LifeAfterPayback = sLampLife/(dailyHours*30) - payback_period;       //in months
	total_energy_savings = LifeAfterPayback * savings;
	cfl_incremental_savings = ((cfl_bulb_life/current_lamp_hours_val)*inc_bulb_cost)-cfl_bulb_cost;
	ext_labor_cost = (cfl_bulb_life/current_lamp_hours_val) * quantity * labor_cost;
	bulb_replacement_cost = cfl_incremental_savings * quantity;
	
	// new energy savings as given by Dave Johnson
	energy_savings = (watts_val-cfl_val)*cfl_bulb_life*kWh_val*quantity/1000;	

	// initialize these to 0 if they are Not A Number (NaN)
	if (isNaN(total_energy_savings)) total_energy_savings = 0;
	if (isNaN(energy_savings)) energy_savings = 0;
	if (isNaN(ext_labor_cost)) ext_labor_cost = 0;
	if (isNaN(bulb_replacement_cost)) bulb_replacement_cost = 0;
	
	long_term_savings = total_energy_savings + ext_labor_cost + bulb_replacement_cost;
	
	//f.cfl_watts.value = cfl_val;
	f.inc_cost.value = "$" + format_dollars(inc_cost);
	f.cfl_cost.value = "$" + format_dollars(cfl_cost);
	f.savings.value = "$" + format_dollars(savings);
	f.switch_cost.value = "$" + format_dollars(switch_cost);
	f.payback_period.value = format_dollars(payback_period);
//	f.total_energy_savings.value = "$" + format_dollars(total_energy_savings);
	f.total_energy_savings.value = "$" + format_dollars(energy_savings);
//	f.bulb_replacement_cost.value = "$" + format_dollars(bulb_replacement_cost);
	f.labor_cost.value = "$" + format_dollars(ext_labor_cost);
	f.long_term_savings.value = "$" + format_dollars(long_term_savings);	
	f.new_lamp_cost.value = "$" + format_dollars(cfl_bulb_cost);

	return false;
}

function monthly_energy_savings(watts, kWh, dailyHours) {
	return (watts * dailyHours * 30 * kWh)/1000;
}

function go() {
	calc(document.calc_form); 
}	