$.validator.addMethod("check", function(value) {
      return value.toLowerCase().indexOf("www") != 0;
   }, 'No external url is allowed.');
   
$.validator.setDefaults({
	submitHandler: function(form) {
				avg_hours=parseFloat($("#avg_hours").val());
				avg_kwh=parseFloat($("#avg_kwh").val());
				watt=parseFloat($("#watt").val());
				labor_cost=parseFloat($("#labor_cost").val());
				bulb_cost=parseFloat($("#bulb_cost").val());
				bulb_lifetime=parseFloat($("#bulb_lifetime").val());
				selected_year=parseFloat($("#select_year option:selected").val());
				selected_percentage=parseFloat($("#select_percentage option:selected").val());
				selected_times=parseFloat($("#select_times option:selected").val());
				
				//alert("avg_hours="+avg_hours+", avg_kwh="+avg_kwh+", watt="+watt+", labor_cost="+labor_cost+", bulb_cost="+bulb_cost+", bulb_lifetime="+bulb_lifetime);
				
				light_hr=parseFloat(365*selected_year*avg_hours);
				//alert("light_hr="+light_hr+", bulb_lifetime="+bulb_lifetime);
				bcnt=Math.ceil(parseFloat(light_hr/bulb_lifetime)); 
				//alert("bcnt="+bcnt);
				led_cnt=Math.ceil(parseFloat(light_hr/50000));
				labor_cost_total=labor_cost*bcnt;
				labor_cost_total_led=labor_cost*led_cnt;

				
				total_electric_cost=parseFloat(avg_hours*avg_kwh*watt*365*selected_year/1000);
				led_total_electric_cost=parseFloat(avg_hours*avg_kwh*watt*selected_percentage*365*selected_year/1000);
				
				total_bulb_cost=parseFloat(bulb_cost*bcnt);
				led_total_bulb_cost=parseFloat(bulb_cost*selected_times*led_cnt);
				
				tcost=Math.round(parseFloat(labor_cost_total+total_electric_cost+total_bulb_cost)*100)/100;
				led_tcost=Math.round(parseFloat(labor_cost_total_led+led_total_electric_cost+led_total_bulb_cost)*100)/100;
				
				diff=Math.round((tcost-led_tcost)*100)/100;
				
				
				//alert("diff="+diff);
				if (diff>=0) {
						msg='<p class="msg">- Congratulation,  you will save <font color="red">$'+diff+' per light bulb </font> in '+selected_year+' year period by switching to LEDs lighting. </p><p>You can save more with rebate.</p>';
					} else {
						msg='<p>-	Based on above information, your existing solutions  are cost efficient, but have you considered all cost of your existing lighting solutions ? such as, benefits of labor, cost of ballast ? You often can get rebate by switching to LED lights.</p>';
					}
				$("#calculate_result").show(function() {
					$("#year2").val(selected_year);
					$("#year3").val(selected_year);
					$("#year4").val(selected_year);
					$("#total_bulb").val(bcnt);
					$("#total_labor_cost").val(labor_cost_total);
					$("#total_cost").val(tcost);
					$("#led_total_cost").val(led_tcost);
					
				});
				$("#response").html(msg);
				$(form).resetForm();
				return false;
			}
});

$().ready(function() {
		$("#how_to_calculate").click(function() {
			$("#calculator_assumption").show();
			return false;
		});
		
	
	// validate signup form on keyup and submit
	$("#simple_calculator").validate({
		rules: {
			avg_hours: {
				required: true,
				number: true
			},
			avg_kwh: {
				required: true,
				number: true
			},
			watt: {
				required: true,
				number: true
			},
			labor_cost: {
				required: true,
				number: true
			},
			bulb_cost: {
				required: true,
				number: true
			},
			bulb_lifetime: {
				required: true,
				number: true
			}
		},
		messages: {
			avg_hours: "Please enter a number",
			avg_kwh: "Please enter a number",
			watt: "Please enter a number",
			labor_cost: "Please enter a number",
			bulb_cost: "Please enter a number",
			bulb_lifetime: "Please enter a number"
		}
	});
});