function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2);
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3) { var nn = n.substr(n.length-3);
	a.unshift(nn);
	n = n.substr(0,n.length-3); }
	if(n.length > 0) { a.unshift(n); }	
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }	
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}

function calculate()
{
	var InputA, InputB, InputC, InputD, DisplayA, DisplayB, DisplayC;
	InputA = parseFloat(document.getElementById("City_Tax_Withheld").value.replace(/[\$\,]/g,""));
	if (isNaN(InputA)) { InputA = 0; }
	InputB = parseFloat(document.getElementById("City_Tax_Paid").value.replace(/[\$\,]/g,""));
	if (isNaN(InputB)) { InputB = 0; }
	InputC = parseFloat(document.getElementById("Estimated_Tax_Paid").value.replace(/[\$\,]/g,""));
	if (isNaN(InputC)) { InputC = 0; }
	InputD = parseFloat(document.getElementById("Refund_Received").value.replace(/[\$\,]/g,""));
	if (isNaN(InputD)) { InputD = 0; }	
	DisplayA = InputA + InputB + InputC;
	DisplayB = DisplayA - InputD;
	if (DisplayB <= 0) { DisplayC = "$" + 0.00; }
	else if (DisplayB > 0 && DisplayB <= 100.49) { DisplayC = "$" + Math.round(DisplayB * 0.20).toFixed(2); }	
	else if (DisplayB >= 100.50 && DisplayB <= 150.49) { DisplayC = "$" + Math.round(((DisplayB - 100) * 0.10) + 20).toFixed(2); }	
	else if (DisplayB >= 150.50 && DisplayB < 1000000) { DisplayC = "$" + CommaFormatted(Math.round(((DisplayB - 150) *0.05) + 25).toFixed(2)); }
	else { DisplayC = "Cannot be calculated."; }
	document.getElementById("City_Tax_Withheld").value = "$" + CommaFormatted(InputA.toFixed(2));
	document.getElementById("City_Tax_Paid").value = "$" + CommaFormatted(InputB.toFixed(2));
	document.getElementById("Estimated_Tax_Paid").value = "$" + CommaFormatted(InputC.toFixed(2));
	document.getElementById("Refund_Received").value = "$" + CommaFormatted(InputD.toFixed(2));
	document.getElementById("Subtotal").value = "$" + CommaFormatted(DisplayA.toFixed(2));
	document.getElementById("Total_City_Tax_Paid").value = "$" + CommaFormatted(Math.round(DisplayB).toFixed(2));
	document.getElementById("Eligible_City_Income_Tax_Credit").value = DisplayC;
	return false;
}