<!--//--------- Pension Calculator Calculations ---------------------
// Modifications:
// DRT 2007-07-23 - WO#9690 Update for after Simpler Super
//-------------------------------------------------------------------
//--------------- Validate all input fields -------------------------
//
// Check all input fields, in case an error has not been 
// corrected by the user.
// Return false if any field is invalid, else return true.
//
function CheckValues() {
  if (!RetireAge(document.forms[0].txtRetireAge.value)) {
		return false;
  }
  if (!BeginBal(document.forms[0].txtBeginBal.value)) {
		return false;
  }
  if (!FundRate(document.forms[0].txtFundRate.value)) {
		return false;
  }
  if (!CPI(document.forms[0].txtCPI.value)) {
		return false;
  }
  if (!NomAmount(document.forms[0].txtNomAmount.value)) {
		return false;
  }
  if (!NomPercent(document.forms[0].txtNomPercent.value)) {
		return false;
  }
  return true;
}
//-------------------------------------------------------------------
//----------------------- Beginning Balance -------------------------
// Check the Beginning Balance is numeric and greater than zero.
// If no value, set to default.
// Display as an integer number.
//
function BeginBal(sBeginBal) {
  if (sBeginBal == "") {
    document.forms[0].txtBeginBal.value = 100000;
    return true;
  }
  if (!isDecimalNumber(sBeginBal)) {
	alert("Please enter a number for the Beginning Balance.");
	document.forms[0].txtBeginBal.focus();
	document.forms[0].txtBeginBal.select();
	return false;
  }
  if (sBeginBal <= 0) {
	alert("Please enter a number greater than zero for the Beginning Balance.");
	document.forms[0].txtBeginBal.focus();
	document.forms[0].txtBeginBal.select();
	return false;
  }
  document.forms[0].txtBeginBal.value = FormatIntegerNumber(sBeginBal);
  return true;
}
//-------------------------------------------------------------------
//----------------------------- CPI ---------------------------------
// Check the CPI is numeric.
// If no value, set to default.
// Cannot be greater than Fund Rate.
function CPI(sCPI) {
  if (sCPI == "") {
    document.forms[0].txtCPI.value = "3.0";
    sCPI = "3.0";
  }
  else {
	if (!isDecimalNumber(sCPI)) {
		alert("Please enter a number between 0 and 20 for the CPI.");
		document.forms[0].txtCPI.focus();
		document.forms[0].txtCPI.select();
		return false;
	}
  }
  var fCPI = parseFloat(sCPI);
  if (fCPI < 0) {
	document.forms[0].txtCPI.value = "0.0";
	fCPI = 0.0;
  }
  else {
	if (sCPI > 20) {
		document.forms[0].txtCPI.value = "20.0";
		fCPI = 20.0;
	}
  }
  var fFundRate = parseFloat(document.forms[0].txtFundRate.value);
  if (fFundRate < fCPI) {
	alert("The Estimated CPI increase cannot be higher than the Fund Crediting Rate.");
	document.forms[0].txtCPI.focus();
	document.forms[0].txtCPI.select();
	return false;
  }
  return true;
}
//-------------------------------------------------------------------
//-------------------------- Fund Rate ------------------------------
// Check the Fund Rate is numeric.
// If no value, set to default.
// Cannot be less than CPI.
// DRT - 2005-07-07 Changed default rate from 6.25 to 6.0
//
function FundRate(sFundRate) {
  if (sFundRate == "") {
    document.forms[0].txtFundRate.value = "8.0";
    sFundRate = "8.0";
  }
  else {
	if (!isDecimalNumber(sFundRate)) {
		alert("Please enter a number between 0 and 20 for the Fund Crediting Rate.");
		document.forms[0].txtFundRate.focus();
		document.forms[0].txtFundRate.select();
		return false;
	}
  }
  var fFundRate = parseFloat(sFundRate);
  if (fFundRate < 0) {
    document.forms[0].txtFundRate.value = "0.0";
    fFundRate = 0.0;
  }
  else {
	if (fFundRate > 20) {
		document.forms[0].txtFundRate.value = "20.0";
		fFundRate = 20.0;
	}
  }
  var fCPI = parseFloat(document.forms[0].txtCPI.value);
  if (fFundRate < fCPI) {
	alert("The Fund Crediting Rate cannot be less than the Estimated CPI increase.");
	document.forms[0].txtFundRate.focus();
	document.forms[0].txtFundRate.select();
	return false;
  }
  return true;
}
//-------------------------------------------------------------------
//------------------------- Nominated Amount ------------------------
// If no value, set to zero.
// Must be numeric, either zero, or between minimum and maximum values.
// If negative, set to zero.
// Must be between minimum and maximum pension amounts.
// If OK, set Nominated Percent to zero.
//
function NomAmount(sNomAmount) {
  if (sNomAmount == "") {
    document.forms[0].txtNomAmount.value = "0";
    return true;
  }
  if (!isDecimalNumber(sNomAmount)) {
	alert("Please enter a number for the Nominated Amount.");
	document.forms[0].txtNomAmount.focus();
	document.forms[0].txtNomAmount.select();
	return false;
  }
  var fNomAmount = parseFloat(sNomAmount);
  if (fNomAmount == 0.0) {
    return true;
  }
  if (fNomAmount < 0.0) {
    document.forms[0].txtNomAmount.value = "0";
    return true;
  }
  //
  // Get minimum and maximum pension amounts for comparison
  //
  var fMin = parseFloat(document.forms[0].txtMinPen.value);
  var fMax = parseFloat(document.forms[0].txtMaxPen.value);
  if (fNomAmount < fMin) {
	var sMsg = "The nominated amount must be zero, or between the Minimum and Maximum pension amounts.";
	sMsg = sMsg + "\n\nSince the value entered (" + fNomAmount + ") is less than the Minimum of " + fMin + ", that value will be used.";
	alert(sMsg);
	sNomAmount = document.forms[0].txtMinPen.value;
  }
  else {
	if (fNomAmount > fMax) {
		var sMsg = "The nominated amount must be zero, or between the Minimum and Maximum pension amounts.";
		sMsg = sMsg + "\n\nSince the value entered (" + fNomAmount + ") is greater than the Maximum of " + fMax + ", that value will be used.";
		alert(sMsg);
		sNomAmount = document.forms[0].txtMaxPen.value;
	}
  }
  //
  // If OK, set Nominated Percent to zero - cannot have both.
  //
  document.forms[0].txtNomAmount.value = FormatIntegerNumber(sNomAmount);
  document.forms[0].txtNomPercent.value = "0";
  return true;
}
//-------------------------------------------------------------------
//------------------------- Nominated Percent -----------------------
// If no value, set to zero.
// Must be numeric, between 0 and 100.
// If negative, set to zero; if greater than 100, set to 100.
// If OK, set Nominated Amount to zero.
//
function NomPercent(sNomPercent) {
  if (sNomPercent == "") {
    document.forms[0].txtNomPercent.value = "0";
    return true;
  }
  if (!isDecimalNumber(sNomPercent)) {
	alert("Please enter a number between 0 and 100 for the Nominated Percentage.");
	document.forms[0].txtNomPercent.focus();
	document.forms[0].txtNomPercent.select();
	return false;
  }
  var fNomPercent = parseFloat(sNomPercent);
  if (fNomPercent < 0) {
	alert("The percentage value entered is less than zero, so zero will be used.");
    document.forms[0].txtNomPercent.value = "0";
  }
  else {
	if (fNomPercent > 100) {
		alert("The percentage value entered is greater than 100, so 100 will be used.");
		document.forms[0].txtNomPercent.value = "100";
	}
  }
  //
  // If OK and not zero, set Nominated Amount to zero - cannot have both.
  //
  if (fNomPercent > 0) {
    document.forms[0].txtNomAmount.value = "0";
  }
  return true;
}
//-------------------------------------------------------------------
//----------------------- Retirement Age ----------------------------
// Check the Retirement Age is numeric and between 55 and 65.
// If no value, set to 55.
//
function RetireAge(sRetire) {
  if (sRetire == "") {
    document.forms[0].txtRetireAge.value = 55;
    return true;
  }
  if (!isIntegerNumber(sRetire)) {
	alert("Please enter a number between 55 and 65 for your retirement age.");
	document.forms[0].txtRetireAge.focus();
	document.forms[0].txtRetireAge.select();
	return false;
  }
  if ((sRetire < 55) || (sRetire > 65)) {
	alert("Please enter a number between 55 and 65 for your retirement age.");
	document.forms[0].txtRetireAge.focus();
	document.forms[0].txtRetireAge.select();
	return false;
  }
  return true;
}
//-------------------------------------------------------------------
//------------- Display first year pension values -------------------
//
// Check all input fields first, in case an error has not been 
// corrected by the user.
// If all values valid, calculate minimum and maximum pensions allowed.
// Calculate how long the money will last.
//
function CalcPension() {
	if (!CheckValues()) {
		return;
	}
	var sTemp = "";			// temporary string variable
	var fTemp = 0.0;		// temporary real number
	var fFees = 0.0;		// annual fees
	
	// Take fees before calculating minimum and maximum pensions
	// Fees are 1% up to $50,000 or flat $500 (changed 3 July 2002 - DRT)
	// DRT - 2005-05-11 Fees now $1.00 per week.
	// DRT - 2005-07-07 Fees now $1.50 per week.
	// DRT - 2007-07-24 Fees now zero.
	var fBeginBal = parseFloat(document.forms[0].txtBeginBal.value);
//	fFees = new Number(52.00 * 1.50);			//Weeks x weekly fee
	fFees = 0.0;
	fBeginBal = new Number(fBeginBal - fFees);
	sBeginBal = new String(fBeginBal);
	
	var	iMaxBal = parseInt(sBeginBal,10);
	var	iMinBal = iMaxBal;
	var fMinPF = 0.0;
	var fMaxPF = 0.0;
	var fMinPension = 0.0;
	var fMaxPension = 0.0;
	var fNomPension = 0.0;
	var fNomPercent = parseFloat(document.forms[0].txtNomPercent.value) / 100.0;
	var fNomAmount = parseFloat(document.forms[0].txtNomAmount.value);

	// Use age to read minimum and maximum Pension Factors from hidden fields.
	var sAge = document.forms[0].txtRetireAge.value;
	var iAge = parseInt(sAge,10);
	sTemp = new String ("MinPF" + sAge);
	fTemp = document.forms[0].elements[sTemp].value;
	fMinPF = parseFloat(fTemp);
	sTemp = new String ("MaxPF" + sAge);
	fTemp = document.forms[0].elements[sTemp].value;
	fMaxPF = parseFloat(fTemp);
	
	// Calculate minimum and maximum pension for first year
	if (iMaxBal > 0) {
		fMinPension = iMinBal * fMinPF;
		fMaxPension = iMaxBal * fMaxPF;
		
		// Calculate nominated amount
		if (fNomAmount > 0) {
			fNomPension = fNomAmount;
		}
		else {
			fNomPension = fMinPension + (fMaxPension - fMinPension) * fNomPercent;
		}
	}
	//
	// Store integer values in hidden fields to compare with any nominated amount.
	// Display values as currency in whole dollars.
	//
	sTemp = new String(fMinPension);
	document.forms[0].txtMinPen.value = FormatIntegerNumber(sTemp);
	document.forms[0].txtMinPenDisplay.value = FormatIntegerMoney(sTemp);
	sTemp = new String(fMaxPension);
	document.forms[0].txtMaxPen.value = FormatIntegerNumber(sTemp);
	document.forms[0].txtMaxPenDisplay.value = FormatIntegerMoney(sTemp);
	sTemp = new String(fNomPension);
	document.forms[0].txtNomPenDisplay.value = FormatIntegerMoney(sTemp);

	return;
}
//-------------------------------------------------------------------
//------------- Display graph and table of values -------------------
//
// Check all input fields first, in case an error has not been 
// corrected by the user.
// If all values valid, submit the form to another active server page
// to calculate the values and display the graph.
//
function DisplayGraph() {
	if (!CheckValues()) {
		return;
	}
	document.forms[0].submit();
}
//===================================================================
//-->
