//		Title: 		Pension Caculator//		Version: 		2.0//		Last Update:	4 October 2000////function formatText(num)  {   var i, count=0;   var text="";      for (i=num.length;i>=0;i--)  {      if (count==4) {         count = 	1;         text = num.substr(i,1) + "," + text;      } else {         text = num.substr(i,1) + text;      }      count = count + 1;   }   return text;}function clearResult(form)  {   document.all.tb_mandatoryportion.innerHTML = "0";   document.all.tb_voluntaryportion.innerHTML = "0";   document.all.tb_amountatretirement.innerHTML = "0";   document.all.tb_amountattodayvalue.innerHTML = "0";   document.all.tb_noofyear.innerHTML = "0";}function Format()  {   this.toNumber = toNumber;}function toNumber(num)   {var result = "";return result;}// Field Checkingfunction checkMonthlyIncome(form) {   clearResult(form);   if (form.tb_monthlyincome.value=="") {      alert("請輸入每月現金入息。");        form.tb_monthlyincome.focus();      return false; }  if (isNaN(form.tb_monthlyincome.value) || form.tb_monthlyincome.value<0) {    alert("每月現金入息必須為正數，請再輸入。");    form.tb_monthlyincome.focus();    return  false; }  return true;}function checkIncomeGrowthRate(form) {   clearResult(form);   if (form.tb_incomegrowthrate.value=="") {      alert("請輸入每年現金入息之加幅。");       form.tb_incomegrowthrate.focus();      return false;   }   if (isNaN(form.tb_incomegrowthrate.value)) {      alert("每年現金入息之加幅必須為數字，請再輸入。");       form.tb_incomegrowthrate.focus();      return false;   }   return true;}function checkEmployerVoluntaryContribution(form) {   clearResult(form);   if (form.tb_employervoluntarycontribution.value=="") {      alert("請輸入僱主之自願性供款佔現金入息。");      form.tb_employervoluntarycontribution.focus();      return false;   }   if (isNaN(form.tb_employervoluntarycontribution.value)) {      alert("僱主之自願性供款佔現金入息必須為數字，請再輸入。");      form.tb_employervoluntarycontribution.focus();      return false;   }   if (form.tb_employervoluntarycontribution.value<0 || form.tb_employervoluntarycontribution.value>100) {      alert("僱主之自願性供款佔現金入息必須介乎於0至100之間。");      form.tb_employervoluntarycontribution.focus();      return false;   }   return true;}function checkEmployeeVoluntaryContribution(form) {   clearResult(form);   if (form.tb_employeevoluntarycontribution.value=="") {      alert("請輸入閣下之自願性供款佔現金入息。");      form.tb_employeevoluntarycontribution.focus();      return false;   }   if (isNaN(form.tb_employeevoluntarycontribution.value)) {      alert("閣下之自願性供款佔現金入息必須為數字，請再輸入。");      form.tb_employeevoluntarycontribution.focus();      return false;   }   if (form.tb_employeevoluntarycontribution.value<0 || form.tb_employeevoluntarycontribution.value>100) {      alert("閣下之自願性供款佔現金入息必須介乎於0至100之間。");      form.tb_employeevoluntarycontribution.focus();      return false;   }   return true;}function checkAge(form) {		clearResult(form);	if ( form.dd_age.value == "" ) {   		alert("請輸入現時之年齡。");   		form.dd_age.focus();   		return false;	}	if (isNaN(form.dd_age.value)) {      	alert("現時之年齡必須為數字，請再輸入。");      	form.dd_age.focus();      	return false;   	}   	if (form.dd_age.value<18 || form.dd_age.value>65) {    	alert("現時之年齡必須介乎於18至65之間。");      	form.dd_age.focus();      	return false;   }   return true;}function checkRetirementAge(form)  {   clearResult(form);   if ( form.dd_retirementage.value == "" ) {   		alert("請輸入退休年齡。");   		form.dd_retirementage.focus();   		return false;	}	if (isNaN(form.dd_retirementage.value)) {      	alert("退休年齡必須為數字，請再輸入。");      	form.dd_retirementage.focus();      	return false;   	}   	if (form.dd_retirementage.value<60 || form.dd_retirementage.value>65) {    	alert("退休年齡必須介乎於60至65之間。");      	form.dd_retirementage.focus();      	return false;   }   return true;}function checkInvestmentReturn(form) {   clearResult(form);   if (form.tb_investmentreturn.value=="") {      alert("請輸入投資回報率。");      form.tb_investmentreturn.focus();      return false;   }   if (isNaN(form.tb_investmentreturn.value)) {      alert("投資回報率必須為數字，請再輸入。");      form.tb_investmentreturn.focus();      return false;   }   if (form.tb_investmentreturn.value<-100 || form.tb_investmentreturn.value>100)   {      alert("投資回報率必須介乎於-100至100之間，請再輸入。");      form.tb_investmentreturn.focus();      return false;   }   return true;}function checkInflationRate(form) {   clearResult(form);   if (form.tb_inflationrate.value=="") {      alert("請輸入通脹率。");      form.tb_inflationrate.focus();      return false;   }   if (isNaN(form.tb_inflationrate.value)) {      alert("通脹率必須為數字，請再輸入。");      form.tb_inflationrate.focus();      return false;   }   if (form.tb_inflationrate.value<-50 || form.tb_inflationrate.value>50)   {      alert("通脹率必須介乎於-50至50之間，請再輸入。");      form.tb_inflationrate.focus();      return false;   }   return true;}function checkAgeDiff(form)   {   clearResult(form);   if (form.dd_age.value=="") {      alert("請輸入現時之年齡。");      form.dd_age.focus();      return false;   }      if (isNaN(form.dd_age.value)) {      	alert("現時之年齡必須為數字，請再輸入。");      	form.dd_age.focus();      	return false;   	}   	if (form.dd_age.value<18 || form.dd_age.value>65) {    	alert("現時之年齡必須介乎於18至65之間。");      	form.dd_age.focus();      	return false;   }   if (form.dd_retirementage.value=="") {      alert("請輸入退休年齡。");      form.dd_retirementage.focus();      return false;   }      if (isNaN(form.dd_retirementage.value)) {      	alert("退休年齡必須為數字，請再輸入。");      	form.dd_retirementage.focus();      	return false;   	}   	if (form.dd_retirementage.value<60 || form.dd_retirementage.value>65) {    	alert("退休年齡必須介乎於60至65之間。");      	form.dd_retirementage.focus();      	return false;   }      if ((form.dd_retirementage.value-form.dd_age.value) <= 0 )   {      alert("退休年齡必須大於現時之年齡。");      form.dd_age.focus();      return false;    }   return true;}      // Employeefunction Employee(form)  { this.MonthlyIncome = getMonthlyIncome(form); this.IncomeGrowthRate = getIncomeGrowthRate(form); this.EmployerVoluntaryContribution = getEmployerVoluntaryContribution(form); this.EmployeeVoluntaryContribution = getEmployeeVoluntaryContribution(form); this.Age = getAge(form); this.RetirementAge = getRetirementAge(form); this.InvestmentReturn = getInvestmentReturn(form); this.InflationRate = getInflationRate(form);}function getMonthlyIncome(form) {   var income = 0;   income = form.tb_monthlyincome.value * 1;   return income;}function getIncomeGrowthRate(form) {   var rate = 0;   rate = form.tb_incomegrowthrate.value * 1;   return rate;}function getEmployerVoluntaryContribution(form) {   var contribution = 0;   contribution = form.tb_employervoluntarycontribution.value * 1;   return contribution;}function getEmployeeVoluntaryContribution(form) {   var contribution = 0;   contribution = form.tb_employeevoluntarycontribution.value * 1;   return contribution;}function getAge(form) {   var age = 0;   age = form.dd_age.value * 1;   return age;}function getRetirementAge(form) {   var age = 0;   age = form.dd_retirementage.value * 1;   return age;}function getInvestmentReturn(form) {   var rate = 0;   rate = form.tb_investmentreturn.value * 1;   return rate;}function getInflationRate(form) {   var rate = 0;   rate = form.tb_inflationrate.value * 1;   return rate;}                                                                                                                             // Employee's Pensionfunction EmployeePension(employee) {   var monthly_income = employee.MonthlyIncome;   var income_growth_rate = employee.IncomeGrowthRate;   var employer_voluntary_contribution = employee.EmployerVoluntaryContribution;   var employee_voluntary_contribution = employee.EmployeeVoluntaryContribution;   var current_age = employee.Age;   var investment_return = employee.InvestmentReturn;   var retirement_age = employee.RetirementAge;   var employer_mandatory_contribution = 5;   var employee_mandatory_contribution = 5;   var inflation_rate = employee.InflationRate;   var max_income = 20000;  // max. amount to be invested of each month under the government law   var actual_retirement_age = employee.RetirementAge;  // retiremnet age under the government law   var year = 0;   var mandatory = 0;   var voluntary = 0;   var return_on_mandatory = 0;   var return_on_voluntary = 0;   var amount_mandatory_portion = 0;   var amount_voluntary_portion = 0;   var amount_at_today_value = 0;   // the remaining time to retirement age   var period = retirement_age - current_age;      // calculate the return of each year   for (year=0;year<period;year++) {    // calulate the actual monthly income of that year      if ((year > 0)) {         monthly_income = monthly_income * (1 + (income_growth_rate/100));      }          // calulate the actual max income of that year      if ((year > 0) && (year % 3 == 0)) {         max_income = max_income * Math.pow(1 + (inflation_rate/100), 3);         max_income = Math.round(max_income/1000)*1000;      }          // calculate the actual amount of mandatory contribution to be invested      if (monthly_income > max_income) {           if (monthly_income >= 5000) { // if monthly income < 5000, employee would not to contribution               mandatory = max_income * ((employee_mandatory_contribution + employer_mandatory_contribution)/100);                       } else {               mandatory = max_income * ((employer_mandatory_contribution)/100);                       }      } else {           if (monthly_income >= 5000) { // if monthly income < 5000, employee would not to contribution               mandatory = monthly_income * ((employee_mandatory_contribution + employer_mandatory_contribution)/100);           } else {               mandatory = monthly_income * ((employer_mandatory_contribution)/100);           }      }       // calculate the actual amount of voluntary contribution to be invested      voluntary = monthly_income * ((employee_voluntary_contribution + employer_voluntary_contribution)/100);      //alert(mandatory);      //alert(voluntary);          // calculate the amount of investment return base on the budget of the mandatory contribution      return_on_mandatory = calculateYearlyReturn(mandatory, investment_return/100, (actual_retirement_age-current_age-year-1));	// calculate the amount of investment return base on the budget of the voluntory contribution      return_on_voluntary = calculateYearlyReturn(voluntary, investment_return/100, (actual_retirement_age-current_age-year-1));            //  alert(return_on_mandatory);    //  alert(return_on_voluntary);           // accumulate the amount of investment return of the mandatory contribution     amount_mandatory_portion = amount_mandatory_portion + return_on_mandatory;         // accumulate the amount of investment return of the voluntary contribution     amount_voluntary_portion = amount_voluntary_portion + return_on_voluntary;   }   this.MandatoryPortion = amount_mandatory_portion;   this.VoluntaryPortion = amount_voluntary_portion;   this.AmountAtRetirement = Math.round(amount_voluntary_portion + amount_mandatory_portion);   this.AmountAtTodayValue = this.AmountAtRetirement / Math.pow(1 + (inflation_rate/100), (actual_retirement_age - current_age));}function calculateYearlyReturn(principle, rate, period)   {   var amount=0;   var rate1=0;      rate1 = Math.pow((1+(rate/12)),12) - 1;   amount = FV(rate/12,12,principle);   amount = amount * Math.pow((1+rate1),period);      return amount;   }function FV(rate, period, amount)   {   var value = 0;      if (rate==0)   {      value = amount * period;   } else {      value = Math.pow(1+rate, period) - 1;      value = amount*(value/rate);   }         return value;}// Mainfunction main(form)  {  // Valid all required field before calculating the pension  if (!checkMonthlyIncome(form)) return;  if (!checkIncomeGrowthRate(form)) return;  if (!checkEmployerVoluntaryContribution(form)) return;  if (!checkEmployeeVoluntaryContribution(form)) return;  if (!checkAge(form)) return;  if (!checkInvestmentReturn(form)) return;  if (!checkInflationRate(form)) return;  if (!checkAgeDiff(form)) return;  // Create the object of Employee, to collect all required information about the employee   e = new Employee(form);  // Create the object of Pension Calculator, to evaluate the pension of the employee   ep = new EmployeePension(e);  // Output the results   //alert(ep.MandatoryPortion);   //alert(ep.VoluntaryPortion);   //alert(ep.AmountAtRetirement);   //alert(ep.AmountAtTodayValue);      document.all.tb_mandatoryportion.innerHTML = Math.round(ep.MandatoryPortion);   document.all.tb_voluntaryportion.innerHTML = Math.round(ep.VoluntaryPortion);   document.all.tb_amountatretirement.innerHTML = Math.round(ep.AmountAtRetirement);   document.all.tb_amountattodayvalue.innerHTML = Math.round(ep.AmountAtTodayValue);   document.all.tb_noofyear.innerHTML = e.RetirementAge - e.Age;      document.all.tb_mandatoryportion.innerHTML = formatText(document.all.tb_mandatoryportion.innerHTML);   document.all.tb_voluntaryportion.innerHTML = formatText(document.all.tb_voluntaryportion.innerHTML);   document.all.tb_amountatretirement.innerHTML = formatText(document.all.tb_amountatretirement.innerHTML);   document.all.tb_amountattodayvalue.innerHTML = formatText(document.all.tb_amountattodayvalue.innerHTML);}function reset(form){	form.tb_monthlyincome.value="";	form.tb_incomegrowthrate.value="";	form.dd_age.value="";	form.dd_retirementage.value="";		form.tb_employervoluntarycontribution.value="";	form.tb_employeevoluntarycontribution.value="";		form.tb_investmentreturn.value=""	form.tb_inflationrate.value=""	   	document.all.tb_mandatoryportion.innerHTML = "0";   	document.all.tb_voluntaryportion.innerHTML = "0";   	document.all.tb_amountatretirement.innerHTML = "0";   	document.all.tb_amountattodayvalue.innerHTML = "0";   	document.all.tb_noofyear.innerHTML = "0";}