calculator

<script type=”text/javascript” src=”https://raw.githubusercontent.com/jquery/jquery-ui/master/external/jquery-1.11.3/jquery.js”></script>

<script type=”text/javascript”>
jQuery(document).ready( function($) {
// You can safely use $ here now, eg. $(‘selector’).whatever();

} );
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, “,”);
}
function isNumberKey(evt) {
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
function clearFunction() {
document.getElementById(“age”).value = “”;
document.getElementById(“retire”).value = “”;
document.getElementById(“service”).value = “”;
document.getElementById(“salary”).value = “”;
document.getElementById(“expense”).value = “”;
document.getElementById(“tspbalance”).value = “”;
document.getElementById(“tspcontribution”).value = “”;
}
function backToCalc() {
// clearFunction();
document.getElementById(“Calculator”).style.display = ‘block’;
document.getElementById(“summary”).style.display = ‘none’;
}
function submitFunction() {

//inputs
// var age = parseInt($(“#age”).val());
// var retire = parseInt($(“#retire”).val());
// var service = parseInt($(“#service”).val());
// var salary = parseInt($(“#salary”).val());
// var expense = parseInt($(“#expense”).val());
// var tspbalance = parseInt($(“#tspbalance”).val());
// var tspcontrib = parseInt($(“#tspcontribution”).val());
var age = parseInt(document.getElementById(“age”).value);
var retire = parseInt(document.getElementById(“retire”).value);
var service = parseInt(document.getElementById(“service”).value);
var salary = parseInt(document.getElementById(“salary”).value);
var expense = parseInt(document.getElementById(“expense”).value);
var tspbalance = parseInt(document.getElementById(“tspbalance”).value);
var tspcontrib = parseInt(document.getElementById(“tspcontribution”).value);
var years = retire – age;

//assumptions
//Assumed Inflation for Expenses 2.50%
//Assumed Salary COLA 1.50%
//Assumed agency contribution rate (% of salary) 5%
//combined agency + end user TSP contribution = (SALARY * AGENCY CONTRIB)+ANNUAL CONTRIB
var combinedTSPContrib = (salary * 0.05) + tspcontrib;

//FUTURE VALUE OF SALARY
//FORMULA = SALARY*(1+SALARY COLA)^ REMAINING SERVICE
var FVSalary = (salary * Math.pow((1 + 0.015), years)).toFixed(0);
//alert(FVSalary);
//FUTURE VALUE OF EXPENSES
//FORMULA = EXPENSE*(1+INFLATION FOR EXPENSE)^ REMAINING SERVICE
var FVExpense = (expense * Math.pow((1 + 0.025), years)).toFixed(0);

//ASSUMED POST TAX INCOME RATE 80%

var yearsoffederal = years + service;
//Projected Retirement expense: FVExpense
//Projected Pension Income (yearsoffederal*0.01)*FVSalary
var PPI = Math.round((yearsoffederal * 0.01) * FVSalary);
//Projected Social Security Income
// FVSalary*0.25
var PSSI = Math.round(FVSalary * 0.25);
//Projected TSP income NEED
//=FVExpense-((PPI+PSSI)*80%) 80% is POST TAX INCOME
var PTIN = Math.round(FVExpense – ((PPI + PSSI) * 0.80));
//Needed TSP balance at retirement:
//=(PTIN/80%)*25
//alert(PTIN);
var NTSPB = Math.round((PTIN / 0.80) * 25);
//Annual Rate of Return needed for your TSP to reach its goal:
//alert(rate(years, -combinedTSPContrib, -tspbalance, NTSPB));
var AnnualRate = (rate(years, -combinedTSPContrib, -tspbalance, NTSPB) * 100);
//alert(years+”::::::”+-combinedTSPContrib+”::::::::::::::”+-tspbalance+”:::::::::::”+NTSPB);
if(expense>salary){
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
AnnualRate = xhttp.responseText;
document.getElementById(“AnnualRate”).innerHTML = (parseFloat(AnnualRate,10)*100).toFixed(2)+”%”;
}
};
xhttp.open(“POST”, “../CalculateServer.php”, true);
xhttp.setRequestHeader(“Content-type”, “application/x-www-form-urlencoded”);
//alert(years+”::::::”+-combinedTSPContrib+”::::::::::::::”+-tspbalance+”:::::::::::”+NTSPB);
xhttp.send(“a=”+years+”&b=”+-combinedTSPContrib+”&c=”+-tspbalance+”&d=”+NTSPB);
}
// $(“#Calculator”).hide();
// $(“#summary”).show();
document.getElementById(“Calculator”).style.display = ‘none’;
document.getElementById(“summary”).style.display = ‘block’;
if (isNaN(years))
years = 0;
document.getElementById(“YTR”).innerHTML = years;
// $(“#YTR”).html(years);
if (isNaN(yearsoffederal))
yearsoffederal = 0;
document.getElementById(“FYS”).innerHTML = yearsoffederal;
// $(“#FYS”).html(yearsoffederal);
if (isNaN(FVExpense))
FVExpense = 0;
document.getElementById(“PRE”).innerHTML = “$” + numberWithCommas(FVExpense);
// $(“#PRE”).html(“$” + FVExpense);
if (isNaN(PPI))
PPI = 0;
document.getElementById(“PPI”).innerHTML = “$” + numberWithCommas(PPI);
// $(“#PPI”).html(“$” + PPI);
if (isNaN(PSSI))
PSSI = 0;
document.getElementById(“PSSI”).innerHTML = “$” + numberWithCommas(PSSI);
// $(“#PSSI”).html(“$” + PSSI);
if (isNaN(PTIN))
PTIN = 0;
// $(“#PTIN”).html(“$” + PTIN);
document.getElementById(“PTIN”).innerHTML = “$” + numberWithCommas(PTIN);
if (isNaN(NTSPB))
NTSPB = 0;
// $(“#NTSPB”).html(“$” + NTSPB);
document.getElementById(“NTSPB”).innerHTML = “$” + numberWithCommas(NTSPB);
// $(“#AnnualRate”).html(AnnualRate + “%”);
document.getElementById(“AnnualRate”).innerHTML = AnnualRate.toFixed(2)+”%”;
}
function rate(nper, pmt, pv, fv, type, guess) {
if (guess == null) guess = 0.01;
if (fv == null) fv = 0;
if (type == null) type = 0;

var FINANCIAL_MAX_ITERATIONS = 256;//Bet accuracy with 128
var FINANCIAL_PRECISION = 0.0000001;//1.0e-8

var y, y0, y1, x0, x1 = 0, f = 0, i = 0;
var rate = guess;
if (Math.abs(rate) < FINANCIAL_PRECISION) {
y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
} else {
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt * (1 / rate + type) * (f – 1) + fv;
}
y0 = pv + pmt * nper + fv;
y1 = pv * f + pmt * (1 / rate + type) * (f – 1) + fv;

// find root by Newton secant method
i = x0 = 0.0;
x1 = rate;
while ((Math.abs(y0 – y1) > FINANCIAL_PRECISION) && (i < FINANCIAL_MAX_ITERATIONS)) {
rate = (y1 * x0 – y0 * x1) / (y1 – y0);
x0 = x1;
x1 = rate;

if (Math.abs(rate) < FINANCIAL_PRECISION) {
y = pv * (1 + nper * rate) + pmt * (1 + rate * type) * nper + fv;
} else {
f = Math.exp(nper * Math.log(1 + rate));
y = pv * f + pmt * (1 / rate + type) * (f – 1) + fv;
}

y0 = y1;
y1 = y;
++i;
}
return rate;}
</script>