// requires jQuery.js, commonCalculator.js
jQuery(function() {
	jQuery("#calculatorForm").bind('submit', calculate);	
	calculate();
});

function calculate() {
	var hours1 = getInputMoney("#hours1");
	var hours2 = getInputMoney("#hours2");
	var hourly1 = getInputMoney("#hourly1");
	var hourly2 = getInputMoney("#hourly2");	
	var shelteredContributionRate = getInputRate("#shelteredContributionRate");
	var preTaxDeductions = getInputMoney("#otherPreTaxDeductions");
	var payPeriod = jQuery("#payPeriod option:selected").val();
	var filingStatus = jQuery("#filingStatus option:selected").val();
	var numberOfAllowances = getInputNumber("#numberOfAllowances");
	var postTaxDeductions = getInputMoney("#postTaxDeductions");
	var postTaxReimbursements = getInputMoney("#postTaxReimbursements");
	var stateAndLocalWithholdingRate = getInputRate("#stateAndLocalWithholding");
	var totalPay = (hours1 * hourly1) + (hours2 * hourly2);
	
	var paycheck = new Calculators.planning.GrossToNetPaycheck(totalPay, 0, shelteredContributionRate, preTaxDeductions, payPeriod, filingStatus, numberOfAllowances, postTaxDeductions, postTaxReimbursements, stateAndLocalWithholdingRate);
	
	jQuery("#reportGross").text(paycheck.getGross().formatted());
	jQuery("#reportDeductions").text(paycheck.getTotalPreTaxDeductions().formatted());
	jQuery("#reportFederal").text(paycheck.getFederalWithholding().formatted());
	jQuery("#reportOASDI").text(paycheck.getOASDI().formatted());
	jQuery("#reportMedicare").text(paycheck.getMedicare().formatted());
	jQuery("#reportState").text(paycheck.getStateAndLocalWithholding().formatted());
	jQuery("#reportPostTaxNet").text(paycheck.getPostTaxNet().formatted());
	jQuery("#reportNet").text(paycheck.getNet().formatted());
	plotValues(paycheck);	
	
	return false;
}

function plotValues(paycheck){
	var chart = new FusionCharts("FusionCharts/FCF_Pie3D.swf", "graphId", "250", "240");
	var dataXml = generateDataXml(paycheck);
	chart.setDataXML(dataXml);
	chart.render("graph");
};

function generateDataXml(paycheck){
	var returnXml = new Array();
	returnXml.push("<?xml version='1.0'?>\n");
	returnXml.push("<graph caption='Paycheck' xAxisName='' yAxisName='' showNames='0' showValues='0' decimalPrecision='2' formatNumberScale='0' numberPrefix='$' formatNumber='1' animation='0' baseFontColor='999999' pieYScale='60'>");
	returnXml.push("  <set name='Net pay' value='");
	returnXml.push(paycheck.getNet().getAmount());
	returnXml.push("' hoverText='Net pay: ");
	returnXml.push(paycheck.getNet().formatted());
	returnXml.push("' color='afd8f8' />");	
	if(paycheck.getFederalWithholding().getAmount() > 0.0) {
		returnXml.push("  <set name='Federal withholding' value='");
		returnXml.push(paycheck.getFederalWithholding().getAmount());
		returnXml.push("' hoverText='Federal withholding: ");
		returnXml.push(paycheck.getFederalWithholding().formatted());
		returnXml.push("' color='ff0000' />");
	}	
	if(paycheck.getTotalPreTaxDeductions().getAmount() > 0.0) {
		returnXml.push("  <set name='Pre-tax deductions' value='");
		returnXml.push(paycheck.getTotalPreTaxDeductions().getAmount());
		returnXml.push("' hoverText='Total pre-tax deductions: ");
		returnXml.push(paycheck.getTotalPreTaxDeductions().formatted());
		returnXml.push("' color='f6bd0f' />");
	}
	if(paycheck.getOASDI().getAmount() > 0.0) {
		returnXml.push("  <set name='FICA OASDI' value='");
		returnXml.push(paycheck.getOASDI().getAmount());
		returnXml.push("' hoverText='FICA OASDI (Soc. Security): ");
		returnXml.push(paycheck.getOASDI().formatted());
		returnXml.push("' color='33CC33' />");
	}
	if(paycheck.getMedicare().getAmount() > 0.0) {
		returnXml.push("  <set name='FICA Medicare' value='");
		returnXml.push(paycheck.getMedicare().getAmount());
		returnXml.push("' hoverText='FICA Medicare: ");
		returnXml.push(paycheck.getMedicare().formatted());
		returnXml.push("' color='0000ff' />");
	}
	if (paycheck.getStateAndLocalWithholding().getAmount() > 0.0) {
		returnXml.push("  <set name='State and Local' value='");
		returnXml.push(paycheck.getStateAndLocalWithholding().getAmount());
		returnXml.push("' hoverText='State and local withholding: ");
		returnXml.push(paycheck.getStateAndLocalWithholding().formatted());
		returnXml.push("' color='336666' />");	
	}
	if (paycheck.getPostTaxNet().getAmount() < 0.0) {
		returnXml.push("  <set name='Post-tax net' value='");
		returnXml.push(paycheck.getPostTaxNet().getAmount() * -1);
		returnXml.push("' hoverText='Post-tax net deductions: ");
		returnXml.push(new Calculators.type.Money(paycheck.getPostTaxNet().getAmount() * -1).formatted());
		returnXml.push("' color='996699' />");	
	}	
	returnXml.push("</graph>");

	return returnXml.join("");
}

