//	---------------------------------------------------
// 	$Id: retire.js,v 1.3 2002/01/24 17:50:09 soglesby Exp $
// 	$Source: /bbsrc/web/money/tools/scripts/RCS/retire.js,v $
//	retire.js - JavaScript for Retirement Calculator
//	---------------------------------------------------

function RetireCalc()
{
	//initial form field and flag variables
	this.cage = 25;
	this.rage = 65;
	this.curamt = 5000;
	this.rate = 6;
	this.optamt = 1000000;

	this.calculate = RetireCalcCalculate;
	this.retireTable = RetireCalcTable;
	this.set =  RetireCalcSetData;
	this.validate = RetireCalcValidate;
}

function PopulateForm(frm)
{
		// populate form with values contained/calculated by fcalc object
		frm.cage.value = rcalc.cage;
		frm.rage.value = rcalc.rage;
		frm.curamt.value = rcalc.curamt;
		frm.rate.value = rcalc.rate;
		frm.optamt.value = rcalc.optamt;
}

function RetireCalcCalculate()
{
		if ( rcalc.validate() ){
			ShowResult();
		}
}

function RetireCalcSetData(obj)
{
	var fld = obj.name;
	var val = obj.value;

	if (typeof(val) == "string"){
		//strip out any commas from the value
		val = val.replace(/,/g, "");
	}
	if ( isNaN(val) || (val == "") ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: A positive numeric value is required in this field.');
			document.retireform[fld].focus();
			return false;
	}
	else{
		this[fld] = val;
	}

	if ( (fld == 'cage') &&  ((val <= 0) || (val > 65)) ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-65.');
			document.retireform[fld].focus();
			return false;
	}
	if ( (fld == 'rage') &&  ((val < 20) || (val > 75)) ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 20-75.');
			document.retireform[fld].focus();
			return false;
	}
	if (this.cage >= this.rage){
		alert('Age is not a valid entry.\nCurrent age should not be higher than Retirement Age.');
		return false;
	}
	if ( (fld == 'rate') &&  ((val < 1) || (val > 25)) ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-25.');
			document.retireform[fld].focus();
			return false;
	}
	if ( (fld == 'curamt') &&  ((val < 0) || (val > 1000000)) ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 0-1,000,000.');
			document.retireform[fld].focus();
			return false;
	}
	if (fld == 'optamt') {
		if (document.retireform.option[1].checked  && ((val < 1) || (val > 5000000)) ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-5,000,000.');
			document.retireform[fld].focus();
			return false;
		}
		if (document.retireform.option[0].checked  && ((val < 1) || (val > 75000)) ) {
			document.retireform[fld].value = this[fld];
			alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-75,000.');
			document.retireform[fld].focus();
			return false;
		}
	}
	if (document.retireform.option[1].checked){
			if (Number(this.curamt) >= Number(this.optamt)){
			alert('Entry is not valid.\nInvalid Data: Desired Fund amount must be greater than Current Fund 

Amount.');
			document.retireform.curamt.focus();
			return false;
		}
	}
}

function RetireCalcValidate()
{
	var fld, val;
	var data = ["rage", "cage", "rate", "curamt", "optamt"];
	var label = ["Retirement Age", "Current Age", "Rate of Return", "Current Amount in Fund", "Desired amount in 

Fund/Annual contribution"];
	for(i=0; i < data.length; i++){
			fld = data[i];
			val = document.retireform[fld].value;
			if (typeof(val) == "string")
			{
				//strip out any commas from the value
				val = val.replace(/,/g, "");
				this[fld] = val;
			}
			if (isNaN(val) || (val < 0) || (val == "")){
				alert(label[i] + '\nInvalid Data: A positive numeric value is required in this field.');
				document.calcform[fld].focus();
				return false;
			}
			if ( (fld == 'cage') &&  ((val <= 0) || (val > 65)) ) {
				alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-65.');
				document.retireform[fld].focus();
				return false;
			}
			if ( (fld == 'rage') &&  ((val < 20) || (val > 75)) ) {
				alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 20-75.');
				document.retireform[fld].focus();
				return false;
			}
			if (this.cage >= this.rage){
				alert('Age is not a valid entry.\nCurrent age should not be higher than Retirement Age.');
				return false;
			}
			if ( (fld == 'rate') &&  ((val < 1) || (val > 25)) ) {
				alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-25.');
				document.retireform[fld].focus();
				return false;
			}
			if ( (fld == 'curamt') &&  ((val < 0) || (val > 1000000)) ) {
				alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 0-1,000,000.');
				document.retireform[fld].focus();
				return false;
			}
			if (fld == 'optamt') {
				if (document.retireform.option[1].checked  && ((val < 1) || (val > 5000000)) ) {
					alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-5,000,000.');
					document.retireform[fld].focus();
					return false;
			}
			if (document.retireform.option[0].checked  && ((val < 1) || (val > 75000)) ) {
				alert(val + ' is not a valid entry.\nInvalid Data: Valid range is 1-75,000.');
				document.retireform[fld].focus();
				return false;
			}
		}
		if (document.retireform.option[1].checked){
			if (Number(this.curamt) >= Number(this.optamt)){
				alert('Entry is not valid.\nInvalid Data: Desired Fund amount must be greater than Current 

Fund Amount.');
				document.retireform.curamt.focus();
				return false;
			}
		}
	}
	return true;
}



function RetireCalcTable()
{
	var table = '';
	var pctrate = this.rate/100;
    var years = this.rage - this.cage;
	var pct = Math.pow((pctrate+1), years);
	var fv=0, totfv=0;
	var contribution = new Number(0);
	var pvarray = new Array();
	var fvarray = new Array();
	var contarray = new Array();
	var currentamt = (Number(this.curamt) * (this.rate/100)) + Number(this.curamt);
	var result=0, dresult=0;
	var toptext='';
	var totyears=0;
	var retireage= new Number(0);;

	if (document.retireform.option[0].checked){ //calculate fund balance at retirement
		contribution = Number(this.optamt);
		for (i=0; i<years; i++){
			pvarray[i] = Number(currentamt) * Math.pow((pctrate+1), i);
			fv += Number(this.optamt) * Math.pow((pctrate+1), i);
			totfv = fv + pvarray[i];
			fvarray[i] = Math.round(totfv*100)/100;
			contarray[i] = Math.round(contribution*100)/100;
			contribution += Number(this.optamt);
			totyears = i;
		}
	}
	else{ //calculate required annual contribution
     	result = (Number(this.optamt) - (Number(this.curamt) * pct)) * (pctrate/(pct - 1));

		if (this.optamt <= (this.curamt * pct)){
			result = 0;
			dresult = 0;
		}
		else{
			dresult = result;
		}
		contribution += Number(dresult);
		fv = 0;
		for (i=0; i<years; i++){
			temp = currentamt * Math.pow((pctrate+1), i);
			pvarray[i] = temp;
			fv += result * Math.pow((pctrate+1), i);
			totfv = fv + pvarray[i];
				fvarray[i] = Math.round(totfv*100)/100;
				contarray[i] = Math.round(contribution*100)/100;
				contribution += result;
				totyears=i;
				if(totfv >= this.optamt) break;
		}
	}
	totyears += 1;
	retireage = Number(this.cage) + totyears;
	var contemp =  (contarray[totyears-1] / totyears)*100;
	var contribute = Math.round(contemp)/100;
	var savings = Math.round(fvarray[totyears-1]*100)/100;

	if (document.retireform.option[0].checked){ //calculate fund balance at retirement
		toptext += '<TR><TD COLSPAN=3><hr width="100%" size="1" noshade></TD></TR>'
			  +  '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">With an initial investment of $'
			  +   toCurrency(this.curamt) + '</TD></TR>'
			  +  '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">and annual contributions of $'
			  +   toCurrency(contribute) + '</TD></TR>'
			  +   '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">earning ' + this.rate + '% 

interest, '
			  +   'you will save $' + toCurrency(savings)  + '</TD></TR>'
			  +   '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">by the age of ' + retireage
			  +   '.</TD></TR>'
			  +   '<TR><TD COLSPAN=3><hr width="100%" size="1" noshade></TD></TR>';
	}
	else{
			toptext += '<TR><TD COLSPAN=3><hr width="100%" size="1" noshade></TD></TR>'
			  +  '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">With a current fund amount of $'
			  +   toCurrency(this.curamt) + '</TD></TR>'
			  +  '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">and a desired total fund amount 

of $'
			  +   toCurrency(this.optamt) + '</TD></TR>'
			  +   '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">earning ' + this.rate + '% 

interest, '
			  +   'you will need to contribute $' + toCurrency(contribute)  + '</TD></TR>'
			  +   '<TR><TD COLSPAN=3 ALIGN="center" CLASS="storybold">each year. You will reach your 

goal by the age of ' + retireage
			  +   '.</TD></TR>'
			  +   '<TR><TD COLSPAN=3><hr width="100%" size="1" noshade></TD></TR>';
	}
	table += '<TABLE B0RDER="1" cellspacing="0" width="408">'
		  +  toptext
		  +  '<TR><TD ALIGN="center" COLSPAN="5" bgcolor="cccc88" CLASS="restitle" >'
		  +	 'Fund Values for the next ' + totyears + ' Years</TD></TR>'
		  +  '<TR><TD ALIGN="center" CLASS="storybold">Year</TD>'
		  +  '<TD ALIGN="center" CLASS="storybold">Total Annual Contribution</TD>'
		  +  '<TD ALIGN="center" CLASS="storybold">Total Fund Value</TD></TR>';
	var yeartmp=0;
	for (i=0; i<totyears; i++){
		yeartmp = i+1;
		if ((i % 2) == 0) color = "bgcolor=dddddd"; else color = "bgcolor=ffffff";
		table += '<TR><TD ' + color + ' ALIGN="center" CLASS="story">'
			  + yeartmp
			  + '</TD><TD ' + color + ' ALIGN="center" CLASS="story">'
			  + toCurrency(contarray[i])
			  + '</TD><TD ' + color + ' ALIGN="center" CLASS="story">'
			  + toCurrency(fvarray[i]) + '</TD></TR>';
	}
	table += '</TABLE>';
	return table;
}

function ShowResult()
{
	var body2 = rcalc.retireTable();
	var body1 = "";
	var tail = "<br><br>";
	var all_results = body1 + body2 + tail;

	if ((navigator.appName.indexOf("Netscape") != -1) && (parseInt(navigator.appVersion) == 4)) {
				var reslayer = document.layers['retireres2'].document;
				reslayer.open();
				reslayer.write(all_results);
				reslayer.close();
	     }
		 else if ((navigator.appName.indexOf("Microsoft") != -1) ||
		 		  (parseInt(navigator.appVersion) >= 5)){
				document.getElementById("retireres").innerHTML = all_results;
	    }
		else {
			alert("This calculator can only be used in Netscape Navigator or Microsoft Internet Explorer, 

version 4 or greater");
		}

}
