 |
Calculate Your Investment Goal
function calcComp(){
var curAmount, curSavedNow, intYears, pctRateReturn, pctPRate;
var intTermMonths, temp1, temp2, temp3;
curSavedNow = document.calculator.curSavedNow.value;
if (valNumericStr("savings", curSavedNow, 0, 1) == false) curSavedNow = 0.00;
else curSavedNow = parseFloat(curSavedNow);
pctRateReturn = document.calculator.txtRateReturn.value;
if (valNumericStr("rate of return", pctRateReturn, 0, 1) == false) pctRateReturn = 12.60;
else pctRateReturn = parseFloat(pctRateReturn);
curAmount = document.calculator.curAmount.value;
if (valNumericStr("total amount", curAmount, 0, 1) == false) curAmount = 0.00;
else curAmount = parseFloat(curAmount);
intYears = document.calculator.intYears.value;
if (valNumericStr("years available to save", intYears, 0, 1) == false) intYears = 12;
else intYears = parseInt(intYears, 10);
// calculate
pctPRate = periodizeRate(pctRateReturn, 12, 1);
temp1 = parseFloat(pctPRate) + 1;
temp2 = Math.pow(temp1, intYears*12);
curSavedNow *= temp2;
curAmount -= curSavedNow;
if (curAmount < 0) curAmount = 0;
temp3 = parseFloat(curAmount)/parseFloat(temp2);
return formatFloat(temp3, 2);
}
function valNumericStr(strFieldName, strValue, intBlankFlag, intMsgFlag){
if (strValue.length < 1){
if (intBlankFlag == 0){
if (intMsgFlag == 1)
alert("Please enter a value for " + strFieldName + ".");
return false;
}
}else{
var decPtAt = 0;
for (var i = 0; i < strValue.length; i++){
var localChar = strValue.charAt(i);
if (localChar < "0" || localChar > "9"){
if (localChar == "." && decPtAt == 0){
decPtAt = i + 1;
}else{
if (intMsgFlag == 1)
alert("Please remove the " + localChar + " character from " + strFieldName + ".");
return false;
}
}
}
}
return true;
}
function periodizeRate(annualRate, compoundPerYear, decimalFlag){
if (decimalFlag == 1) periodicRate = annualRate/(compoundPerYear * 100);
else periodicRate = annualRate/compoundPerYear;
return periodicRate;
}
function formatFloat (expr, decplaces){
var str = "" + Math.round( eval(expr) * Math.pow(10, decplaces) );
while (str.length <= decplaces){
str = "0" + str;
}
var decpoint = str.length - decplaces;
return str.substring(0, decpoint) + "." + str.substring(decpoint, str.length);
}
function setRate(){
intChoice = document.calculator.selected.selectedIndex;
document.calculator.txtRateReturn.value = document.calculator.selected.options[intChoice].value;
}
|
 |