// requires jQuery.js, commonCalculator.js
jQuery(function() {
	jQuery("#calculatorForm").bind('submit', calculate);	
	calculate();
});

function calculate() {
	var pay = getInputMoney("#pay");
	var shelteredContributionRate = getInputRate("#shelteredContributionRate");
	var payPeriod = jQuery("#payPeriod option:selected").val();
	var filingStatus = jQuery("#filingStatus option:selected").val();
	var numberOfAllowances = getInputNumber("#numberOfAllowances");
	var stateAndLocalWithholdingRate = getInputRate("#stateAndLocalWithholding");
	var childcare = getInputMoney("#childcare");
	var medical = getInputMoney("#medical");	
	var dental = getInputMoney("#dental");
	var drug = getInputMoney("#drug");
	var otherFSA = getInputMoney("#otherFSA");
	
	var flexibleSpendingAccount = new Calculators.planning.FlexibleSpendingAccount(pay, stateAndLocalWithholdingRate, shelteredContributionRate, filingStatus, payPeriod, numberOfAllowances, childcare, medical, dental, drug, otherFSA);
	//console.debug(paycheck.getGross());
	//console.debug(paycheck.getTotalPreTaxDeductions());
	
	jQuery("#reportFlexSpendingAccount").text("$0.00");
	jQuery("#reportFlexSpendingAccountFSA").text(flexibleSpendingAccount.getTotalFSA().formatted());	
	jQuery("#reportHealthcare").text(flexibleSpendingAccount.getTotalFSA().formatted());	
	jQuery("#reportHealthcareFSA").text("$0.00");		
	jQuery("#reportFederal").text(flexibleSpendingAccount.getWithholding().formatted());
	jQuery("#reportFederalFSA").text(flexibleSpendingAccount.getWithholdingFSA().formatted());	
	jQuery("#reportMedicare").text(flexibleSpendingAccount.getMedicare().formatted());
	jQuery("#reportMedicareFSA").text(flexibleSpendingAccount.getMedicareFSA().formatted());
	jQuery("#reportOASDI").text(flexibleSpendingAccount.getOASDI().formatted());
	jQuery("#reportOASDIFSA").text(flexibleSpendingAccount.getOASDIFSA().formatted());	
	jQuery("#reportState").text(flexibleSpendingAccount.getStateAndLocalWithholding().formatted());	
	jQuery("#reportStateFSA").text(flexibleSpendingAccount.getStateAndLocalWithholdingFSA().formatted());	
	jQuery("#reportNet").text(flexibleSpendingAccount.getNetPay().formatted());	
	jQuery("#reportFSA").text(flexibleSpendingAccount.getNetPayFSA().formatted());	
	jQuery("#reportSavings").text(flexibleSpendingAccount.getSavings().formatted());
	jQuery("#reportSheltered").text(flexibleSpendingAccount.getShelteredContributions().formatted());
	jQuery("#reportShelteredFSA").text(flexibleSpendingAccount.getShelteredContributionsFSA().formatted());	
	plotValues(flexibleSpendingAccount);
	return false;
}

function plotValues(flexibleSpendingAccount){
	var chart = new FusionCharts("FusionCharts/FCF_Column3D.swf", "graphId", "250", "240");
	var dataXml = generateDataXml(flexibleSpendingAccount);
	chart.setDataXML(dataXml);
	chart.render("graph");
};

function generateDataXml(flexibleSpendingAccount){
	var returnXml = new Array();
	returnXml.push("<?xml version='1.0'?>\n");
	returnXml.push("<graph caption='Net Pay' xAxisName='' yAxisName='' showNames='1' showValues='0' decimalPrecision='2' formatNumberScale='0' numberPrefix='$' formatNumber='1' animation='0' baseFontColor='999999' hoverCapSepChar=': '>");
	returnXml.push("  <set name='Without FSA' value='")
  returnXml.push(flexibleSpendingAccount.getNetPay().getAmount());
	returnXml.push("' color='afd8f8'/>");
	returnXml.push("  <set name='With FSA' value='");
  returnXml.push(flexibleSpendingAccount.getNetPayFSA().getAmount());								 
	returnXml.push("' color='f6bd0f'/>");								 
	returnXml.push("</graph>");

	return returnXml.join("");
}

