		
function calculateSavings() {
	var lamp_watts       = get_number_value(document.getElementById('lamp_watts').value); 
	var lamp_watts_2     = get_number_value(document.getElementById('lamp_watts_2').value); 
	var lamp_life 		 = get_number_value(document.getElementById('lamp_life').value);  
	var lamp_life_2		 = get_number_value(document.getElementById('lamp_life_2').value);  
	var lamp_cost 		 = get_number_value(document.getElementById('lamp_cost').value);  
	var lamp_cost_2		 = get_number_value(document.getElementById('lamp_cost_2').value);  
	var electricity_rate = get_number_value(document.getElementById('electricity_rate').value);  
	var hours 			 = get_number_value(document.getElementById('hours').value);  
	var sockets			 = get_number_value(document.getElementById('sockets').value);
		
	var investment         = sockets * lamp_cost;
	var monthly_savings    = ((((lamp_watts_2 - lamp_watts) * hours * sockets * 365)/1000)*electricity_rate)/12;
	var total_savings      = ((lamp_life/hours)/365) * 12 * monthly_savings;
	var lamp_change_cost   = (((lamp_life/lamp_life_2) * sockets) * lamp_cost_2) - (lamp_cost * sockets);
	var total_cost_savings = total_savings + lamp_change_cost;
	var time_installation  = lamp_life/((hours*365)/12);
	var pay_back_period    = investment/(total_cost_savings/time_installation);
	var cycle_air          = (((lamp_watts_2 - lamp_watts) * lamp_life)/1000) * sockets * 7.18 * 0.0001;
	var cycle_air_lbs      = cycle_air *2204.6;
	var air                = cycle_air/5.23;
	var air_2              = cycle_air/0.039;
	var electricity_saved  = cycle_air/7.7;
	var gas_saved          = parseInt(cycle_air/(8.89 * 0.001));

	document.getElementById('investment').value         = '$' + format_currency(investment); 
	document.getElementById('monthly_savings').value    = '$' + format_currency(monthly_savings);
	document.getElementById('total_savings').value      = '$' + format_currency(total_savings);
	document.getElementById('total_cost_savings').value = '$' + format_currency(total_cost_savings);				
	document.getElementById('pay_back_period').value    = format_currency(pay_back_period);		
	document.getElementById('cycle_air').value          = isNaN(cycle_air_lbs) ? 0 : addCommas(Math.round(cycle_air_lbs));
	document.getElementById('air').value                = format_currency(air);
	document.getElementById('air_2').value              = addCommas(Math.round(air_2));
	document.getElementById('electricity_saved').value  = format_currency(electricity_saved);
	document.getElementById('gas_saved').value          = addCommas(Math.round(gas_saved));//isNaN(gas_saved) ? 0 : gas_saved;			
}   		

function get_number_value(value)
{
	value = value.replace('$', '');
	value = value.replace('%', '');
	value = value.replace(',', '');

	value = parseFloat(value);	

	if (isNaN(value)) {
		value = 0;
	}			
	return value;
}	

function format_currency(num) {
	num = parseFloat(num);
	num = isNaN(num) ? 0.00 : num.toFixed(2);
	num += '';
	        
	var x   = num.split('.');
	var x1  = isNaN(parseInt(x[0])) ? '0'  : x[0];
	var x2  = isNaN(parseInt(x[1])) ? '00' : x[1];

	var rgx = /(\d+)(\d{3})/;

	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + "." + x2;
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
	return x1 + x2;
}

window.onload=calculateSavings;

