function IsNumeric(strString) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	
	return blnResult;
}


$(document).ready(function(){
	
	$("#energy-saving-calculators .errorMsg").hide();
	
	// ENERGY SAVINGS CALCULATOR
	$("#form-energy #nr_bulbs").change(function(){
		$("#form-energy #nr_bulbs2").val($(this).val());
	});
	$("#form-energy #cost").change(function(){
		$("#form-energy #cost2").val($(this).val());
	});
	
	$("#calculate-form-energy").click(function(){
		
		var wattage = $("#wattage").val();
		var hours = $("#hours").val();
		var nr_bulbs = $("#nr_bulbs").val();
		var cost = $("#cost").val();
		
		var wattage2 = $("#wattage2").val();
		var hours2 = $("#hours2").val();
		var nr_bulbs2 = $("#nr_bulbs2").val();
		var cost2 = $("#cost2").val();
		
		if (wattage.length==0 || hours.length==0 || nr_bulbs.length==0 || cost.length==0 || wattage2.length==0 || hours2.length==0 || nr_bulbs2.length==0 || cost2.length==0) {
			$("#form-energy .errorMsg").hide();
			$("#form-energy .errorMsg div").text("Please fill in all the empty fields");
			$("#form-energy .errorMsg").fadeIn(300);
		} else {
			
			$("#form-energy .errorMsg").hide();
			
			if (IsNumeric(wattage)==true && IsNumeric(hours)==true && IsNumeric(nr_bulbs)==true && IsNumeric(cost)==true && IsNumeric(wattage2)==true && IsNumeric(hours2)==true && IsNumeric(nr_bulbs2)==true && IsNumeric(cost2)==true) {
				var total_cost = (parseFloat(wattage) * parseFloat(hours) * parseFloat(nr_bulbs) / 1000 * parseFloat(cost)) * (parseFloat(hours2)/parseFloat(hours));
				var total_cost2 = parseFloat(wattage2) * parseFloat(hours2) * parseFloat(nr_bulbs2) / 1000 * parseFloat(cost2);
				$("#total_cost").val("$"+total_cost.toFixed(2));
				$("#total_cost2").val("$"+total_cost2.toFixed(2));
				$("#total_energy_savings").val("$"+(total_cost.toFixed(2) - total_cost2.toFixed(2)));
			} else {
				$("#form-energy .errorMsg").hide();
				$("#form-energy .errorMsg div").text("All values must be numeric");
				$("#form-energy .errorMsg").fadeIn(300);
			}
			
		}
		
		return false;
	});
	
	
	// ROI CALCULATOR
	$("#form-roi #roi-cost_per_kw").change(function(){
		$("#form-roi #roi-cost_per_kw2").val($(this).val());
	});
	$("#form-roi #roi-hours_per_day").change(function(){
		$("#form-roi #roi-hours_per_day2").val($(this).val());
	});
	
	$("#calculate-form-roi").click(function(){
		
		var price = $("#roi-price").val();
		var wattage = $("#roi-wattage").val();
		var hours = $("#roi-hours").val();
		var hours_per_day = $("#roi-hours_per_day").val();
		var cost_per_kw = $("#roi-cost_per_kw").val();
		//var cost_labor = $("#roi-cost_labor").val();
		
		var price2 = $("#roi-price2").val();
		var wattage2 = $("#roi-wattage2").val();
		var hours2 = $("#roi-hours2").val();
		//var hours_per_day2 = $("#roi-hours_per_day2").val();
		
		if (price.length==0 || wattage.length==0 || hours.length==0 || hours_per_day.length==0 || cost_per_kw.length==0 || price2.length==0 || wattage2.length==0 || hours2.length==0) {
			$("#form-roi .errorMsg").hide();
			$("#form-roi .errorMsg div").text("Please fill in all the empty fields");
			$("#form-roi .errorMsg").fadeIn(300);
		} else {
			
			$("#form-roi .errorMsg").hide();
			
			if (IsNumeric(price)==true && IsNumeric(wattage)==true && IsNumeric(hours)==true && IsNumeric(hours_per_day)==true && IsNumeric(cost_per_kw)==true && IsNumeric(price2)==true && IsNumeric(wattage2)==true && IsNumeric(hours2)==true) {
				
				var hours_per_year = parseFloat(hours_per_day) * 365;
				var hours_per_year2 = parseFloat(hours_per_day) * 365;
				var watt_per_year = parseFloat(wattage) * hours_per_year;
				var watt_per_year2 = parseFloat(wattage2) * hours_per_year2;
				var kilowatt_per_year = watt_per_year/1000;
				var kilowatt_per_year2 = watt_per_year2/1000;
				var cost_yearly = parseFloat(cost_per_kw) * kilowatt_per_year;
				var cost_yearly2 = parseFloat(cost_per_kw) * kilowatt_per_year2;
				//var cost_labor2 = parseFloat(cost_labor) * hours_per_year2 / parseFloat(hours2);
				var cost_lamp_per_year = hours_per_year / parseFloat(hours) * parseFloat(price);
				var cost_lamp_per_year2= parseFloat(price2);
				
				var total_cost_per_year = cost_yearly + cost_lamp_per_year;
				var total_cost_per_year2 = cost_yearly2 + cost_lamp_per_year2;
				var total_payback = cost_lamp_per_year2 / (total_cost_per_year - cost_yearly2);
				
				$("#roi-hours_per_year").val(hours_per_year.toFixed(2));
				$("#roi-hours_per_year2").val(hours_per_year2.toFixed(2));
				$("#roi-watt_per_year").val(watt_per_year.toFixed(2));
				$("#roi-watt_per_year2").val(watt_per_year2.toFixed(2));
				$("#roi-kilowatt_per_year").val(kilowatt_per_year.toFixed(2));
				$("#roi-kilowatt_per_year2").val(kilowatt_per_year2.toFixed(2));
				$("#roi-cost_yearly").val("$"+cost_yearly.toFixed(2));
				$("#roi-cost_yearly2").val("$"+cost_yearly2.toFixed(2));
				//$("#roi-cost_labor2").val("$"+cost_labor2.toFixed(2));
				$("#roi-cost_lamp_per_year").val("$"+cost_lamp_per_year.toFixed(2));
				$("#roi-cost_lamp_per_year2").val("$"+cost_lamp_per_year2.toFixed(2));
				
				$("#total_cost_per_year").val("$"+total_cost_per_year.toFixed(2));
				$("#total_cost_per_year2").val("$"+total_cost_per_year2.toFixed(2));
				$("#total_payback").val(total_payback.toFixed(2));
				
			} else {
				$("#form-roi .errorMsg").hide();
				$("#form-roi .errorMsg div").text("All values must be numeric");
				$("#form-roi .errorMsg").fadeIn(300);
			}
			
		}
		
		return false;
	});
	
});

