$(document).ready(function() 	{
	
	$("#days_in").DefaultValue("60");
	$("#per_in").DefaultValue("50");
	$("#calcForm").keyup(function(calMe) {

		$("#systemSize").calc(						   
				"khours / days * percent / 100 / 4",
			// define the variables used in the equation, these can be a jQuery object
			{
				khours: $("#khours_in"),
				days: $("#days_in"),
				percent: $("#per_in")
			},
			// define the formatting callback, the results of the calculation are passed to this function
			function (s){
				// return the number as a watt amount
				return  s.toFixed(2) + " Kilowatt";
			},
			
			
			
			// define the finish callback, this runs after the calculation has been complete
			function ($this){
				// sum the total of the selector
				var sum = $this.sum();
				
				$("#systemSize").text(
					// round the results to 2 digits
					sum.toFixed(2) + " Kilowatt"
				);
				
				$("#gridTie").calc(  // Initial Cost 
				"system * dollar * 1000",
					{ system: $("#systemSize"), dollar: $("#dollarRate") },
				function (s){ return "$" + s.toFixed(0); },
				function ($this){ var sum = $this.sum(); $("#gridTie").text( "$" + sum.toFixed(0) ); }
				);
				
					$("#gridTieNYSD").calc(  // NYSERDA rebate
					"initial * rebateRate * .01",
						{ initial: $("#gridTie"), rebateRate: $("#rebateRate") },
					function (s){ return "- $" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridTieNYSD").text( "- $" + sum.toFixed(0) ); }
					);
					
					$("#gridTieOut").calc(  // Out of Pocket Cost
					"initial - rebate",
						{ initial: $("#gridTie"), rebate: $("#gridTieNYSD") },
					function (s){ return "$" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridTieOut").text( "$" + sum.toFixed(0) ); }
					);
					
					$("#gridTieTax").calc(  // Tax Credits 
					"outOfPocket * taxCreditRate *.01",
						{ outOfPocket: $("#gridTieOut"), taxCreditRate: $("#taxCreditRate") },
					function (s){ return "- $" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridTieTax").text( "- $" + sum.toFixed(0) ); }
					);
					
					$("#gridTieFin").calc(  // Final Cost
					"outOfPocket - taxCredit",
						{ outOfPocket: $("#gridTieOut"), taxCredit: $("#gridTieTax") },
					function (s){ return "$" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridTieFin").text( "$" + sum.toFixed(0) ); }
					);
				
				$("#gridBattery").calc(  // Initial Cost 
				"system * dollar * 1500",
					{ system: $("#systemSize"), dollar: $("#dollarRate") },
				function (s){ return "$" + s.toFixed(0); },
				function ($this){ var sum = $this.sum(); $("#gridBattery").text( "$" + sum.toFixed(0) ); }
				);
				
					$("#gridBatteryNYSD").calc(  // NYSERDA rebate
					"initial * rebateRate * .01",
						{ initial: $("#gridBattery"), rebateRate: $("#rebateRate") },
					function (s){ return "- $" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridBatteryNYSD").text( "- $" + sum.toFixed(0) ); }
					);
					
					$("#gridBatteryOut").calc(    // Out of Pocket Cost
					"initial - rebate",
						{ initial: $("#gridBattery"), rebate: $("#gridBatteryNYSD") },
					function (s){ return "$" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridBatteryOut").text( "$" + sum.toFixed(0) ); }
					);
					
					$("#gridBatteryTax").calc(  // Tax Credits 
					"outOfPocket * .3",
						{ outOfPocket: $("#gridBatteryOut") },
					function (s){ return "- $" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridBatteryTax").text( "- $" + sum.toFixed(0) ); }
					);
					
					$("#gridBatteryFin").calc(    // Final Cost
					"outOfPocket * .7",
						{ outOfPocket: $("#gridBatteryOut") },
					function (s){ return "$" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#gridBatteryFin").text( "$" + sum.toFixed(0) ); }
					);
				
				$("#standAlone").calc(  // Initial Cost 
				"system * dollar * 2000",
					{ system: $("#systemSize"), dollar: $("#dollarRate") },
				function (s){ return "$" + s.toFixed(0); },
				function ($this){ var sum = $this.sum(); $("#standAlone").text( "$" + sum.toFixed(0) ); }
				);
				
					$("#standAloneNYSD").calc(  // NYSERDA rebate
					"initial * rebateRate * .01",
						{ initial: $("#standAlone"), rebateRate: $("#rebateRate") },
					function (s){ return "- $" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#standAloneNYSD").text( "- $" + sum.toFixed(0) ); }
					);
					
					$("#standAloneOut").calc(   // Out of Pocket Cost
					"initial - rebate",
						{ initial: $("#standAlone"), rebate: $("#standAloneNYSD") },
					function (s){ return "$" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#standAloneOut").text( "$" + sum.toFixed(0) ); }
					);
					
					$("#standAloneTax").calc(    // Tax Credits 
					"outOfPocket * .3",
						{ outOfPocket: $("#standAloneOut") },
					function (s){ return "- $" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#standAloneTax").text( "- $" + sum.toFixed(0) ); }
					);
					
					$("#standAloneFin").calc(     // Final Cost
					"outOfPocket * .7",
						{ outOfPocket: $("#standAloneOut") },
					function (s){ return "$" + s.toFixed(0); },
					function ($this){ var sum = $this.sum(); $("#standAloneFin").text( "$" + sum.toFixed(0) ); }
					);
			}	
		);
	});
});