/*
SS CALC

AUTHUR: [AC][20080729]

---
INPUT:
Current Cost per KWH ($)		[F10] [0.08,0.085,0.09,...0.20]
System Size (KW-DC)				[F12] [2,2.5,3,...35]
Electric Bill for Last Year		[F14] [1000,1500,2000,...20000]

---
CONSTANTS:

---
OUTPUT:
(Group: Estimated Summary)
<1> Net System Cost					[L9]=[L19]
<2> Electricity Savings				[L10]=[Sheet2.J6]
<3> Funds Returned in				[L11]=[Sheet2.M12]
<4> Energy Savings					[L12]=[L10/F14]

(Group: Net System Cost Detail)
<5> Total System Cost				[L16]=[Sheet2.J2]
<6> Florida Rebate					[L17]=[Sheet2.J3]
<7> Federal Tax Credit				[L18]=[Sheet2.J4]
<1> Net System Cost					[L19]=[L16-L17-L18]

(Group: Overall Environmental Benefits)
<8> Pounds of CO2 every year			[Sheet2.O2]
<9> Re-forestation of Trees (Sq.Ft)		[Sheet2.O3]
<10> Driving Mileage Equivalent			[Sheet2.O4]
<11> Driving Mileage Multiplier			[Sheet2.P4]

---
FORMULA:

L9 = L19
L10 = Sheet2.J6
L11 = Sheet2.M12
L12 = L10/F14

L16 = Sheet2.J2
L17 = Sheet2.J3
L18 = Sheet2.J4
L19 = L16-L17-L18

(
Ý: what needed from sheet2:
J2, J3, J4, J6,
O2, O3, O4,
P4,
M12
)

SHEET2 WORKSHEET
J2 = Sheet1.F12*1000*6
J3 = IF($Sheet1.F12*1000*2>10000;10000;$Sheet1.F12*1000*2)
J4 = 0.3*J2
J6 = $Sheet1.F12*$Sheet1.F10*5.7*365*0.85

O2 = 1.556*$Sheet1.F12*1000
O3 = 5.2383*$Sheet1.F12*1000
O4 = 1.86694*$Sheet1.F12*1000

P4 = O4/2534

"Saving of Year N" = Sheet1.F10 * Sheet1.F12 * 5.7 * 365 * 0.8 * (1.07)^(N-1)

M12 = N-Years when Sheet1.L19 passes accumulated "Saving of Year N"


===
MODEL:

input:
- cost			[F10]
- systemSize	[F12]
- billLastYear	[F14]

formula:

<5> totalSystemCost = systemSize * 1000 * 9
<6> floridaRebate = MAX(systemSize * 1000 * 4, 20000)
<7> fedTaxCredit = MAX(0.3 * totalSystemCost, 5400)
<1> netSystemCost = totalSystemCost - floridaRebate - fedTaxCredit

<2> electricitySavings = systemSize * cost * 5.7 * 365 * 0.8
<3> fundsReturnIn = function() {
		return N-Years, when netSystemCost passes accumulated "Saving of Year N",
		where "Saving of Year N" = electricitySavings * (1.07)^(N-1)
	}

<4> energySavings = electricitySavings / billLastYear

<8> yearlyCO2 = 1.556 * systemSize * 1000
<9> reforestation = 5.2383 * systemSize * 1000
<10> drivingMileageEquivalent = 1.86694 * systemSize * 1000
<11> drivingMileageMultiplier = drivingMileageEquivalent / 2534


===


*/

function SolarCalc_Model () {

	var me = this;
/* INPUT VARS */
	// Current Cost per KWH ($)		[F10] [0.08,0.085,0.09,...0.20]
	this.cost = 0;
	// System Size (KW-DC)				[F12] [2,2.5,3,...35]
	this.systemSize = 0;
	// Electric Bill for Last Year		[F14] [1000,1500,2000,...20000]
	this.billLastYear = 0;

/* OUTPUT VARS */
	this.totalSystemCost = 0;
	this.floridaRebate = 0;
	this.fedTaxCredit = 0;
	this.netSystemCost = 0;
	this.electricitySavings = 0;
	this.fundsReturnIn = 0;
	this.energySavings = 0;
	this.yearlyCO2 = 0;
	this.reforestation = 0;
	this.drivingMileageEquivalent = 0;
	this.drivingMileageMultiplier = 0;

/* FORMULAS */
	this.calculate = function () {
	
		this.totalSystemCost = this.systemSize * 1000 * 6;
		this.floridaRebate = Math.min(this.systemSize * 1000 * 2, 10000);
		this.fedTaxCredit = 0.3 * this.totalSystemCost;
		this.netSystemCost = this.totalSystemCost - this.floridaRebate - this.fedTaxCredit;
		
		this.electricitySavings = this.systemSize * this.cost * 5.7 * 365 * 0.85;
		this.fundsReturnIn = this.calcFundsReturnIn();	

		this.energySavings = this.electricitySavings / this.billLastYear;
		this.yearlyCO2 = 1.556 * this.systemSize * 1000;
		this.reforestation = 5.2383 * this.systemSize * 1000;
		this.drivingMileageEquivalent = 1.86694 * this.systemSize * 1000;
		this.drivingMileageMultiplier = this.drivingMileageEquivalent / 2534;
		
		return me;
	}

	this.savingsOfYear = function(N) {
		return this.electricitySavings * Math.pow(1.07, N-1);
	}
	this.accumulatedSavingsOfYear = function(N) {
		var sum = 0;
		for (var ii=1; ii<=N; ii++) {
			sum += this.savingsOfYear(ii);
		}
		return sum;
	}
	this.calcFundsReturnIn = function() {
		var fuseBreak = 999;
		for (var ii=1; ii<=fuseBreak; ii++) {
			if (this.netSystemCost < this.accumulatedSavingsOfYear(ii)) {
				return ii-1;
			}
		}
		return fuseBreak;
	}
	
	this.setInput = function(set) {
		this.cost = set.cost;
		this.systemSize = set.systemSize;
		this.billLastYear = set.billLastYear;
	}

}



function SolarCalc_View () {

	var me = this;
	
	this.display = function(Model) {
		$('#totalSystemCost').html(formatCurrency(Model.totalSystemCost, true, false));
		$('#floridaRebate').html('('+formatCurrency(Model.floridaRebate, true, false)+')');
		$('#fedTaxCredit').html('('+formatCurrency(Model.fedTaxCredit, true, false)+')');
		$('#netSystemCost').html(formatCurrency(Model.netSystemCost, true, false));
		$('#netSystemCost_mirror').html($('#netSystemCost').html());
		$('#electricitySavings').html(formatCurrency(Model.electricitySavings, true, false));
		$('#fundsReturnIn').html(Math.round(Model.fundsReturnIn) + 'Yrs');
		$('#energySavings').html(Math.round(Model.energySavings*100) + '%');
		$('#yearlyCO2').html(formatCurrency(Model.yearlyCO2, false, false));
		$('#reforestation').html(formatCurrency(Model.reforestation, false, false));
		$('#drivingMileageEquivalent').html(formatCurrency(Model.drivingMileageEquivalent, false, false));
		$('#drivingMileageMultiplier').html(Math.round(Model.drivingMileageMultiplier*10)/10);
		me.writeChart(Model);
	}
	
	this.getInput = function() {
		return {
			cost: $('#cost').val(),
			systemSize: $('#systemSize').val(),
			billLastYear: $('#billLastYear').val()
		}
	}
	
	this.writeInput = function() {
	
		// write cost input
		var html_cost = '<select id="cost">';
		for (var ii=0.14;ii<=0.205;ii+=0.005) {
			var ii_round = Math.round(ii*1000)/1000;
			html_cost += '<option value="' + ii_round + '">$' + ii_round + '</option>';
		}
		html_cost += '</select>';
		$('#cost_con').html(html_cost);

		// write systemSize input
		var html_systemSize = '<select id="systemSize">';
		for (var ii=2;ii<=35;ii+=0.5) {
			html_systemSize += '<option value="' + ii + '">' + ii + '</option>';
		}
		html_systemSize += '</select>';
		$('#systemSize_con').html(html_systemSize);

		// write billLastYear input
		var html_billLastYear = '<select id="billLastYear">';
		for (var ii=1000;ii<=20000;ii+=500) {
			html_billLastYear += '<option value="' + ii + '"> $' + ii + '</option>';
		}
		html_billLastYear += '</select>';
		$('#billLastYear_con').html(html_billLastYear);

	}
	
	this.writeChart = function(Model) {
		var netSystemCost = Model.netSystemCost;
		var floridaRebate = Model.floridaRebate;
		var fedTaxCredit = Model.fedTaxCredit;
		var pie_total = netSystemCost + floridaRebate + fedTaxCredit;
		var percent_netSystemCost = Math.round(netSystemCost/pie_total*100);
		var percent_floridaRebate = Math.round(floridaRebate/pie_total*100);
		var percent_fedTaxCredit = Math.round(fedTaxCredit/pie_total*100);
		
		var url = 'http://chart.apis.google.com/chart?cht=p3&chd=t:'+percent_netSystemCost+','+percent_floridaRebate+','+percent_fedTaxCredit+'&chs=255x100&chl=net%20cost|rebate|tax%20credit&chco=00aa00,ffdd00,0000ff';
		var html = '<img src="' +url+ '">';
		// if changed, write new image
		if ($('#cost-chart').html().replace(/&amp;/g, '&') != html) {
			$('#cost-chart').html(html);
		}
	}
	
}

function SolarCalc_Controller() {
	var me = this;
	this.Model = new SolarCalc_Model();
	this.View = new SolarCalc_View();
	this.init = function() {
		// write input fields to view
		me.View.writeInput();

		// patch onchange event to input fields
		$('#cost').change(me.updateCalc);
		$('#systemSize').change(me.updateCalc);
		$('#billLastYear').change(me.updateCalc);
		
		// updateCalc for the first time
		me.updateCalc();
	}
	this.updateCalc = function() {
		// update input
		me.Model.setInput(me.View.getInput()); 
		// calculate & display
		me.View.display(me.Model.calculate());
	}
}


<!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
<!-- Web Site:  http://www7.ewebcity.com/cyanide7 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function formatCurrency(num, withDollarSign, withCents) { // [AC][20080729] added withDollarSign, withCents
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	
	/* [START][AC][20080729] */
	var withDollarSign = (withDollarSign==null)?true:withDollarSign;
	var withCents = (withCents==null)?true:withCents;
	
	var dollarSign = '';
	if (withDollarSign) {
		dollarSign = '$';
	}

	var result = ((sign)?'':'-') + dollarSign + num;
	if (withCents) {
		result += '.' + cents;
	}
	/* [END][AC][20080729] */

	return result;
}
//  End -->

