/**
 * cpf-calculator.js
 * Author: Adam Marsh
 */
/*
 * TO MODIFY FORMULAS, GO TO CALCULATION FUNCTIONS AT END OF FILE
 *
 */
var bills = new Array("Electric bills","F6BD0F"); // name and color
var payments = new Array("Loan payments","2AD62A"); // name and color

/**
 * updateChart method is called, when user changes any chart attribute
 * Here, we generate the XML data again and build the chart.		  
 *	@param	domId	domId of the Chart
*/
function updateChart(domId){			
	// Update its XML using updateChartXML method defined in FusionCharts.js
	updateChartXML(domId, generateXML());
}

/**
 * generateXML method returns the XML data for the chart
 *	@param	animate		Boolean value indicating to animate the chart.
 *	@return				XML Data for the entire chart.
*/		
function generateXML(){			
	// Variable to store XML
	var strXML="";
	
	// <graph> element
	strXML = "<graph numberPrefix='$' decimalPrecision='0' animation='1' showValues='0' caption='Your average monthly after-tax payments of $" + calcAvgPayment() + " vs. electric bills' xAxisName='Year' yAxisName='Cumulative Cost'>";

	// Store <categories> and child <category> elements
	strXML = strXML + "<categories>";
		for (var i=1; i<=25; i++) {
		if (i%2) strXML = strXML + "<category name='" + i + "' />"; //odd years only
		else strXML = strXML + "<category name='' />";
	}	
	strXML = strXML + "</categories>";

	// Generate XML		
	strXML = strXML + getChartXML(0);
	strXML = strXML + getChartXML(1);

	// Close <graph> element
	strXML = strXML + "</graph>";

	// Return data
	return strXML;
}

/**
 * getChartXML method returns the <dataset> and <set> elements XML for
 * a particular item 
 *	@param	itemIndex		item index
 *	@return					XML Data for the item
*/
function getChartXML(itemIndex){		
	var chartXML;

	//Create <dataset> element 
	if (itemIndex == 0) {			// electric bills
			chartXML = "<dataset seriesName='"+bills[0]+"' color='"+bills[1]+"' >";			
	} else if (itemIndex == 1) { 	// loan payments
			chartXML = "<dataset seriesName='"+payments[0]+"' color='"+payments[1]+"' >";			
	}

	//Create set elements
	if (itemIndex == 0) {			// electric bills
		chartXML = chartXML + calcBills();
	} else if (itemIndex == 1) { 	// loan payments
		chartXML = chartXML + calcPayments();	
	}

	//Close <dataset> element
	chartXML = chartXML + "</dataset>";
	
	//Return dataset data
	return chartXML;			
}


/* CALCULATION FUNCTIONS ARE BELOW */

/**
 * calcBills method returns the monthly electric bills in XML. 
 *	@return					monthly electric bills.
*/
function calcBills(){
	var XML;
	var bill;
	var totalBills;

	XML = "";
	totalBills = 0;
	
	for (var i=1; i<=25; i++) {
		bill = this.document.q_form.q0_Averageelectricbill.value*12*Math.pow(1.05,i);
		totalBills = totalBills + bill;
		XML = XML + "<set value='" + totalBills + "' />";
	}	
	return XML;
}


/**
 * calcAvgPayment method returns the avg monthly loan payment. 
 *	@return					avg monthly loan payment.
*/
function calcAvgPayment(){		
	var payment;
	var amt;
	var intRate;
	var months;

	// amt = pricequote - taxcredit - rebate						
	amt = this.document.q_form.q4_Solarsystempricequote.value;
	if (this.document.q_form.q15_Checkifthisincludesyourfederaltaxcredit.checked==false) amt = amt - 2000;
	amt = amt - this.document.q_form.q6_Rebate.value;

	intRate = 0.075/12;
	months = 25*12;

	// payment = -PMT(7.5%/12,25*12,pricequote)						
	payment = Math.floor((amt*intRate)/(1-Math.pow(1+intRate,(-1*months)))*100)/100;

	// after-tax
	payment = payment*(1-0.15);

	// clean up
	payment = payment.toFixed(2);

	return payment;			
}


/**
 * calcPayments method returns the monthly loan payments in XML. 
 *	@return					monthly loan payments.
*/
function calcPayments(){		
	var payment;
	var XML;
	XML = "";

	// for now we just use the avg payment (variance in interest tax deduction not accounted for)
	payment = calcAvgPayment()
	
	// fill in XML
	for (var i=1; i<=25; i++) {
		XML = XML + "<set value='" + payment*12*i + "' />";	
	}

	return XML;			
}
