function calValue(frmObj){
	var retiredExpenses_Now = frmObj.retiredExpenses_Now.value;
	var age = frmObj.age.value;
	var retiredAge = frmObj.retiredAge.value;
	var deadAge = frmObj.deadAge.value;
	var rePay = frmObj.rePay.value;
	var priceIndices = frmObj.priceIndices.value;
	var retiredExpenses_FirstMonth;
	
	var distRetired = retiredAge - age;
	var distToDead = deadAge - retiredAge;
	
	var retiredExpenses_Future = retiredExpenses_Now * Math.pow((1 + (priceIndices/12)/100), (distRetired*12));
	
	var retiredExpenses = 0; 
	var retiredExpenses_TotalF = 0; 
	
	for(var i = 0; i <= (distToDead*12)-1; i ++){
		retiredExpenses = retiredExpenses_Future * Math.pow((1 + (priceIndices/12)/100), i);
		retiredExpenses_TotalF += retiredExpenses / Math.pow((1 + (rePay/12)/100), i);
		if(i == 0){
			retiredExpenses_FirstMonth = retiredExpenses_TotalF;
		}
	}
	
	var retiredExpenses_TotalN = 0; 
	
	retiredExpenses_TotalN = retiredExpenses_TotalF / Math.pow((1 + (priceIndices/12)/100), (distRetired*12));

	var newresult = new NumberFormat(retiredExpenses_TotalF);
	newresult.setCommas(true);
	newresult.setCurrency(false);
	document.myForm1.retiredExpenses_TotalF.value = newresult.toFormatted();
	
	var newresult2 = new NumberFormat(retiredExpenses_TotalN);
	newresult2.setCommas(true);
	newresult2.setCurrency(false);
	document.myForm1.retiredExpenses_TotalN.value = newresult2.toFormatted();
	
	var newresult3 = new NumberFormat(retiredExpenses_FirstMonth);
	newresult3.setCommas(true);
	newresult3.setCurrency(false);
	document.myForm1.retiredFirstMonth.value = newresult3.toFormatted();

}

function formSubmit(frmObj){
	if(frmObj.retiredExpenses_Now.value==""){
		alert("請輸入預算退休後每月開支(現值)");
		return false;
	}else if(!(frmObj.retiredExpenses_Now.value>=0)){
		alert("預算退休後每月開支(現值)不可少於0");
		return false;
	}
	
	if(frmObj.age.value==""){
		alert("請輸入現時年齡");
		return false;
	}else if(!(frmObj.age.value>=18)){
		alert("現時年齡不可少於18");
		return false;
	}else if(!(frmObj.age.value<=100)){
		alert("現時年齡不可高於100");
		return false;
	}
	
	if(frmObj.retiredAge.value==""){
		alert("請輸入預算退休年齡");
		return false;
	}else if(!(parseInt(frmObj.retiredAge.value)>=parseInt(frmObj.age.value))){
		alert("預算退休年齡不可少於現時年齡");
		return false;
	}else if(!(frmObj.retiredAge.value>=0)){
		alert("預算退休年齡不正確");
		return false;
	}else if(!(frmObj.retiredAge.value<=100)){
		alert("預算退休年齡不可高於100");
		return false;
	}
	
	if(frmObj.deadAge.value==""){
		alert("請輸入預計壽命");
		return false;
	}else if(!(parseInt(frmObj.deadAge.value)>=parseInt(frmObj.retiredAge.value))){
		alert("預計壽命不可少於預算退休年齡");
		return false;
	}else if(!(frmObj.deadAge.value>=0)){
		alert("預計壽命不正確");
		return false;
	}else if(!(frmObj.deadAge.value<=120)){
		alert("預計壽命不可高於120");
		return false;
	}
	
	if(frmObj.rePay.value==""){
		alert("請輸入預算退休後之投資回報率");
		return false;
	}else if(isNaN(frmObj.rePay.value)==true){
		alert("預算退休後之投資回報率不正確");
		return false;
	}else if(!(frmObj.rePay.value<=100)){
		alert("預算退休後之投資回報率不可高於100%");
		return false;
	}
	
	if(frmObj.priceIndices.value==""){
		alert("請輸入預算通脹率");
		return false;
	}else if(isNaN(frmObj.priceIndices.value)==true){
		alert("預算通脹率不正確");
		return false;
	}else if(!(frmObj.priceIndices.value<=50)){
		alert("預算通脹率不可高於50%");
		return false;
	}
	
	calValue(frmObj);
	
	return false;
}

/*
 * NumberFormat -The constructor
 * num - The number to be formatted
 */
function NumberFormat(num)
{
 // member variables
 this.num;
 this.numOriginal;
 this.isCommas;
 this.isCurrency;
 this.currencyPrefix;
 this.places;
 // external methods
 this.setNumber = setNumberNF;
 this.toUnformatted = toUnformattedNF;
 this.setCommas = setCommasNF;
 this.setCurrency = setCurrencyNF;
 this.setCurrencyPrefix = setCurrencyPrefixNF;
 this.setPlaces = setPlacesNF;
 this.toFormatted = toFormattedNF;
 this.getOriginal = getOriginalNF;
 // internal methods
 this.getRounded = getRoundedNF;
 this.preserveZeros = preserveZerosNF;
 this.justNumber = justNumberNF;
 // setup defaults
 this.setNumber(num);
 this.setCommas(true);
 this.setCurrency(true);
 this.setCurrencyPrefix('$');
 this.setPlaces(2);
}
/*
 * setNumber - Sets the number
 * num - The number to be formatted
 */
function setNumberNF(num)
{
 this.numOriginal = num;
 this.num = this.justNumber(num);
}
/*
 * toUnformatted - Returns the number as just a number.
 * If the original value was '100,000', then this method will return the number 100000
 * v1.0.2 - Modified comments, because this method no longer returns the original value.
 */
function toUnformattedNF()
{
 return (this.num);
}
/*
 * getOriginal - Returns the number as it was passed in, which may include non-number characters.
 * This function is new in v1.0.2
 */
function getOriginalNF()
{
 return (this.numOriginal);
}
/*
 * setCommas - Sets a switch that indicates if there should be commas
 * isC - true, if should be commas; false, if no commas
 */
function setCommasNF(isC)
{
 this.isCommas = isC;
}
/*
 * setCurrency - Sets a switch that indicates if should be displayed as currency
 * isC - true, if should be currency; false, if not currency
 */
function setCurrencyNF(isC)
{
 this.isCurrency = isC;
}
/*
 * setCurrencyPrefix - Sets the symbol that precedes currency.
 * cp - The symbol
 */
function setCurrencyPrefixNF(cp)
{
 this.currencyPrefix = cp;
}
/*
 * setPlaces - Sets the precision of decimal places
 * p - The number of places. Any number of places less than or equal to zero is considered zero.
 */
function setPlacesNF(p)
{
 this.places = p;
}
/*
 * toFormatted - Returns the number formatted according to the settings (a string)
 */
function toFormattedNF()
{
 var pos;
 var nNum = this.num; // v1.0.1 - number as a number
 var nStr;            // v1.0.1 - number as a string
 // round decimal places
 nNum = this.getRounded(nNum);
 nStr = this.preserveZeros(Math.abs(nNum)); // this step makes nNum into a string. v1.0.1 Math.abs
 if (this.isCommas)
 {
  pos = nStr.indexOf('.');
  if (pos == -1)
  {
   pos = nStr.length;
  }
  while (pos > 0)
  {
   pos -= 3;
   if (pos <= 0) break;
   nStr = nStr.substring(0,pos) + ',' + nStr.substring(pos, nStr.length);
  }
 }
 
 nStr = (nNum < 0) ? '-' + nStr : nStr; // v1.0.1
 if (this.isCurrency)
 {
  // add dollar sign in front
  nStr = this.currencyPrefix + nStr;
 }
 return (nStr);
}
/*
 * getRounded - Used internally to round a value
 * val - The number to be rounded
 */
function getRoundedNF(val)
{
 var factor;
 var i;
 // round to a certain precision
 factor = 1;
 for (i=0; i<this.places; i++)
 { factor *= 10; }
 val *= factor;
 val = Math.round(val);
 val /= factor;
 return (val);
}
/*
 * preserveZeros - Used internally to make the number a string
 *  that preserves zeros at the end of the number
 * val - The number
 */
function preserveZerosNF(val)
{
 var i;
 // make a string - to preserve the zeros at the end
 val = val + '';
 if (this.places <= 0) return val; // leave now. no zeros are necessary - v1.0.1 less than or equal
 
 var decimalPos = val.indexOf('.');
 if (decimalPos == -1)
 {
  val += '.';
  for (i=0; i<this.places; i++)
  {
   val += '0';
  }
 }
 else
 {
  var actualDecimals = (val.length - 1) - decimalPos;
  var difference = this.places - actualDecimals;
  for (i=0; i<difference; i++)
  {
   val += '0';
  }
 }
 
 return val;
}
/*
 * justNumber - Used internally to parse the value into a floating point number.
 * If the value is not set, then return 0.
 * If the value is not a number, then replace all characters that are not 0-9, a decimal point, or a negative sign.
 *
 *  Note: The regular expression cleans up the number, but doesn't get rid of - and .
 *  Because all negative signs and all decimal points are allowed,
 *  extra negative signs or decimal points may corrupt the result.
 *  parseFloat will ignore all values after any character that is NaN.
 *
 *  A number can be entered using special notation.
 *  For example, the following is a valid number: 0.0314E+2
 *
 * This function is new in v1.0.2
 */
function justNumberNF(val)
{
 val = (val==null) ? 0 : val;
 // check if a number, otherwise try taking out non-number characters.
 if (isNaN(val))
 {
  var newVal = parseFloat(val.replace(/[^\d\.\-]/g, ''));
  // check if still not a number. Might be undefined, '', etc., so just replace with 0.
  // v1.0.3
  return (isNaN(newVal) ? 0 : newVal); 
 }
 // return 0 in place of infinite numbers.
 // v1.0.3
 else if (!isFinite(val))
 {
  return 0;
  }
 
 return val;
}

function calMPF_E(){
 type_rate = 10/100;
 //[2005.08.04 by Eric] Add Lower and Upper limit - start
 income_lower_limit = 5000;
 income_upper_limit = 20000;
 //[2005.08.04 by Eric] Add Lower and Upper limit - End
 
 //Check income
 if (document.calform1.income.value == "") {
   alert("Please enter monthly income.");
   document.calform1.income.focus();
   return false;
 }
 if (isNaN(document.calform1.income.value) || document.calform1.income.value <= 0) {
   alert("Monthly income must be positive.");
   document.calform1.income.focus();
   return false;
 }
 //Check age
 if (document.calform1.age.value == "") {
   alert("Please enter age.");
   document.calform1.age.focus();
   return false;
 }
 if (isNaN(document.calform1.age.value)) {
   alert("Age must be numeric.");
   document.calform1.age.focus();
   return false;
 }
 if (document.calform1.age.value > 65 || document.calform1.age.value < 18 ) {
   alert("Age must be in range of 18 to 65");
   document.calform1.age.focus();
   return false;
 }
 income = document.calform1.income.value;
 age = document.calform1.age.value;
 rate = document.calform1.rate[document.calform1.rate.selectedIndex].value;
 rate = rate/100;
 //[2005.08.04 by Eric] Add Lower and Upper limit - start
 if (income < income_lower_limit) {
   // income < $5000 no contribution is required for EE but ER still has to contribute 5%, i.e type_rate/2
   result = income * (type_rate/2) * ( ( Math.pow((1+(rate/12)),((65-age)*12)) - 1 ) / (rate/12) );
 }
 else if  (income >= income_upper_limit) {
   // income >= $20000 fixed M.C.=$2000
   result = income_upper_limit * type_rate * ( ( Math.pow((1+(rate/12)),((65-age)*12)) - 1 ) / (rate/12) );
 }
 else {
   result = income * type_rate * ( ( Math.pow((1+(rate/12)),((65-age)*12)) - 1 ) / (rate/12) );
 }
 //[2005.08.04 by Eric] Add Lower and Upper limit - End
 var newresult = new NumberFormat(result);
 newresult.setCommas(true);
 newresult.setCurrency(true);
 document.calform1.result.value = newresult.toFormatted();
}

function calMPF_SE(){
 type_rate = 5/100;
 //[2005.08.04 by Eric] Add Lower and Upper limit - start
 income_lower_limit = 5000;
 income_upper_limit = 20000;
 //[2005.08.04 by Eric] Add Lower and Upper limit - End 
 
 //Check income
 if (document.calform2.income.value == "") {
   alert("Please enter monthly income.");
   document.calform2.income.focus();
   return false;
 }
 if (isNaN(document.calform2.income.value) || document.calform2.income.value <= 0) {
   alert("Monthly income must be positive.");
   document.calform2.income.focus();
   return false;
 }
 //Check age
 if (document.calform2.age.value == "") {
   alert("Please enter age.");
   document.calform2.age.focus();
   return false;
 }
 if (isNaN(document.calform2.age.value)) {
   alert("Age must be numeric.");
   document.calform2.age.focus();
   return false;
 }
 if (document.calform2.age.value > 65 || document.calform2.age.value < 18 ) {
   alert("Age must be in range of 18 to 65");
   document.calform2.age.focus();
   return false;
 }
 income = document.calform2.income.value;
 age = document.calform2.age.value;
 rate = document.calform2.rate[document.calform2.rate.selectedIndex].value;
 rate = rate/100;
 //[2005.08.04 by Eric] Add Lower and Upper limit - start
 if (income < income_lower_limit) {
   // income < $5000 no contribution is required for SEP
   result = 0;
 }
 else if  (income >= income_upper_limit) {
   // income >= $20000 fixed m.c=$1000
   result = income_upper_limit * type_rate * ( ( Math.pow((1+(rate/12)),((65-age)*12)) - 1 ) / (rate/12) );
 } else {
   result = income * type_rate * ( ( Math.pow((1+(rate/12)),((65-age)*12)) - 1 ) / (rate/12) );
 }
 var newresult = new NumberFormat(result);
 newresult.setCommas(true);
 newresult.setCurrency(true);
 document.calform2.result.value = newresult.toFormatted();
}

function changeDemical(i){
	if(!isNaN(i.value)){
		tmpVal = parseFloat(i.value);
		tmpVal = tmpVal * 10;
		tmp = Math.round(tmpVal)/10;
		i.value = tmp;
	}
}

function calMPF_E_Disc(){
	window.open ('popup_mpf_edu_cal_cmab_1.html','w1','status=no,resizable=yes,scrollbars=yes,width=445,height=250');
}

function calMPF_SE_Disc(){
	window.open ('popup_mpf_edu_cal_cmab_2.html','w1','status=no,resizable=yes,scrollbars=yes,width=445,height=250');
}