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

$(function(){
	$("#calc_results").hide();
	$(".solar_type").hide();
	
	var WATT_AVE_MD = .15;
	var WATT_AVE_DC = .15;
	var WATT_AVE_VA = .15;
	
	var MD_COUNTIES = ["Allegany","Anne Arundel","Baltimore City","Baltimore","Calvert","Caroline","Carroll","Cecil","Charles","Dorchester","Frederick","Garrett","Harford","Howard","Kent","Montgomery","Prince George's","Queen Anne's","Somerset","St. Mary's","Talbot","Washington","Wicomico","Worcester"];
	var VA_COUNTIES = ["Clarke","Culpepper","Fairfax","Fauquier","Frederick","Louden","Manassas","Manassas Park","Rappahannock","Shenandoah","Stafford","Warren"];
	
	$("#state").change(function(){
		var counties = [];
		$("#county").html("");
		switch ( $(this).val() ) {
			case 'VA': counties = VA_COUNTIES; break;
			case 'MD': counties = MD_COUNTIES; break;
			default: counties = ['N/A']; break;
		}
		for ( var i = 0; i < counties.length; i++ ) {
			$("#county").html( $("#county").html() + '<option value="' + counties[i] + '">' + counties[i] + '</option>' );
		}
		if ( this.value != 'DC' && this.value != 'NJ' ) $("#county").html( '<option>- choose a county -</option>' + $("#county").html() );
		return true;
	});
	
	$("#county").change(function(){
		if ( this.value == 'Howard' || this.value == 'Montgomery' || this.value == "Prince George's" ) {
			$(".solar_type").show();
		}
		else {
			$(".solar_type").hide();
		}
	});
	
	$("#calculator").submit(function(){
		var monthly_bill = $("#total_bill").val();
		var offset = parseFloat($("#offset").val());
		var state = $("#state").val();
		
		var errors = '';
		
		if ( monthly_bill <= 0 || isNaN(monthly_bill) ) errors += 'Enter your total monthly energy bill.\n';
		if ( $("#county").val() == '' ) errors += 'Please select a county.\n';
		
		if ( errors != '' ) {
			alert(errors);
			return false;
		}
		
		switch ( state ) {
			case 'VA': WATT_AVE = WATT_AVE_VA; break;
			case 'DC': WATT_AVE = WATT_AVE_DC; break;
			default: WATT_AVE = WATT_AVE_MD; break;
		}
		var annual_household_kW = Math.round( (monthly_bill / WATT_AVE) * 100 ) / 100;
		var desired_sys_prod = annual_household_kW * offset;
		var size_multiplier = 1.22775;
		switch ( $("#orientation").val() ) {
			case 'south': size_multiplier = 1.22775; break;
			default: size_multiplier = 1.14875; break;
		}
		var system_size = (desired_sys_prod / size_multiplier * 12 / 1000);
		system_size = Math.round(system_size * 100) / 100;
		var offset_cost = desired_sys_prod * WATT_AVE * 12;
		var SREC = desired_sys_prod / 100;
		
		// calculate state grant
		var state_grant_val = system_size * 1000;
		var state_grant = 0;
		
		var grantval0;
		// if ( state_grant_val > 2000 ) {
		// 	grantval0 = 2500;
		// }
		// else {
		// 	grantval0 = (1.25 * state_grant_val);
		// }
		
		// Updated 2010-11-24
		// All system sizes are $.50 per watt
		
		state_grant = .5 * state_grant_val;
		
		// 
		// var grantval1;
		// if ( state_grant_val - 2000 > 0 && state_grant_val - 2000 < 8001 ) {
		// 	grantval1 = (state_grant_val - 2000) * .75;
		// }
		// else {
		// 	grantval1 = 4500;
		// }
		// if ( state_grant_val > 2000 ) {
		// 	state_grant += grantval1;
		// }
		// 
		// var grantval2;
		// if ( state_grant_val - 8000 > 0 && state_grant_val - 8000 < 2001 ) {
		// 	grantval2 = (state_grant_val - 8000) * .25;
		// }
		// else {
		// 	grantval2 = 10000 - (grantval0 + grantval1);
		// }
		// 
		// if ( state_grant_val > 8000 ) {
		// 	state_grant += grantval2;
		// }
		
		if ( $("#state").val() == 'MD' && state_grant > 10000 ) state_grant = 10000;
		
		var federal_tax_credit = system_size * 1000 * 8 * 0.3;
		var county_tax_credit = 0;
		var disclaimer = '';
		switch ( $("#county").val() ) {
			case 'Anne Arundel': 
				county_tax_credit = 2500; 
				disclaimer = 'This county property tax credit is equal to the lesser of the following: 50% of the cost of materials and installation of the solar energy equipment, less the amount of federal and state grants and state solar energy tax credits or $2,500';
				break;
			case "Prince George's":
			case 'Montgomery': 
			case "Howard":
				if ( $("#solar_type").val() == 'thermal' ) county_tax_credit = 1500;
				else county_tax_credit = 5000; 
				break;
			case "Harford": 
				county_tax_credit = 2500;
				disclaimer = 'This county property tax credit is equal to the lesser of the following: one year of total real property taxes or $2,500.';
				break;
			// Washington, DC
			case "N/A":
			case "":
				if ( $("#state").val() == 'DC' ) {
					// $3/Watt for first 3kW
					// $2/Watt for next 7kW
					// $1/Watt for next 10kW
					var kw_tmp = system_size * 1000;
					state_grant = 0;
					if ( kw_tmp > 3000 ) state_grant += 9000;
					else state_grant += kw_tmp * 3;
					if ( kw_tmp > 3000 ) {
						if ( kw_tmp - 3000 > 0 && kw_tmp - 3000 < 7001 ) state_grant += (kw_tmp-3000)*3;
						else state_grant += 14000;
					}
					if ( kw_tmp > 10000 ) {
						if ( kw_tmp - 10000 > 0 && kw_tmp - 10000 < 10001 ) state_grant += (kw_tmp-10000)*1;
						else state_grant += (33000 - state_grant);
					}
					county_tax_credit = 0;
				}
				//
				// Custom code for NJ
				//
				if ( state == 'NJ' ) {
					state_grant = 0;
					county_tax_credit = 0;
					// rebates for .75 per watt up to 7.5kW – nothing for systems over 10.
					if ( system_size < 10 ) {
						var the_size = system_size;
						if ( the_size > 7.5 ) the_size = 7.5;
						// .75 per watt
						state_grant = .75 * (the_size * 1000);
					}
				}
				break;
			default:
				alert( $("county").val() );
				break;
		}

		var total_incentives = federal_tax_credit + county_tax_credit + state_grant;
		$("#calc_results td.system_size").html(Math.round(system_size * 100) / 100);
		$("#calc_results td.offset_cost").html('$' + addCommas(Math.round(offset_cost * 100) / 100));
		$("#calc_results td.srecs").html(Math.round(SREC * 100)/100);
		$("#calc_results td.grant").html('$' + addCommas(Math.round(state_grant * 100) /100));
		$("#calc_results td.county_credit").html('$' + addCommas(Math.round(county_tax_credit * 100) / 100));
		$("#calc_results td.federal_credit").html('$' + addCommas(Math.round(federal_tax_credit * 100) / 100));
		$("#calc_results h3.totals span.total").html('$' + addCommas( Math.round( (county_tax_credit+federal_tax_credit+state_grant) * 100) / 100 ));
		$("#calc_results .disclaimer").remove();
		if ( disclaimer != '' ) {
			$("#calc_results .county_credit").append('*');
			$("#calc_results").append('<div class="disclaimer">* ' + disclaimer + '</div>');
		}
		$("#calc_results").show();
		window.location = "#calc_results";
		return false;
	});
	$("#calculator .submit input").click(function(){
		$(this).parent().parent().submit();
		return false;
	});
	
	$("#calculator input:first").focus();
	
});
