<!--//--------- Superannuation Calculator ---------------------------
//Modifications:
//DRT 2007-02-09 Created for new calculator
//DRT 2007-02-19 Take 15% cont fee from full employer conts, before subtracting admin fee and insurance premiums
//DRT 2007-02-21 Remove fields no longer needed e.g. salary, SGC, etc.
//DRT 2007-07-23 WO#9690 Updates after Simpler Super - set Fund Rate default to 8%
//
//Functions in this file
//-----------------------
// function CalcPeriod()
// function CalcEmpContAnnual()
// function CalcMemVolContAnnual()
// function CalcInsuranceAnnual()
// function CheckUnitValue(sValue)
// function CalcCover(sOption, sValue)
// function CalcSuper()
// function CheckValues()
// function AgeToday(sAge)
// function BeginBal(sBeginBal)
// function CPI(sCPI)
// function EmpCont(sEmpCont)
// function FundRate(sFundRate)
// function Insurance(sFieldName)
// function MemVolCont(sMemVolCont)
// function RetireAge(sRetire)
// function DisplayGraph()
//-------------------------------------------------------------------
//------------------------ Pay Period -------------------------------
//
// Display correct pay period name.
// Recalculate values, using annual amounts and new pay period value.
//
function CalcPeriod() {
	var iIndex;
	var fPayPeriod;
	var fTemp;
	var fTemp1;
	var sTemp;
	//
	// Get the new pay period name (e.g. Month) and value (e.g. 12).
	//
	iIndex = document.forms[0].PayPeriod.selectedIndex;
	document.forms[0].PayPeriodName.value = document.forms[0].PayPeriod.options[iIndex].text;
	document.forms[0].PayPeriodValue.value = document.forms[0].PayPeriod.options[iIndex].value;
	//
	// Display the new Pay Period name in the appropriate places.
	//
	sTemp = document.forms[0].PayPeriodName.value;
	document.forms[0].txtEmpContPeriod.value = sTemp;
	document.forms[0].txtMemVolContPeriod.value = sTemp;
	//
	// Recalculate values to display
	//
	fPayPeriod = parseFloat(document.forms[0].PayPeriodValue.value);

	fTemp1 = parseFloat(document.forms[0].txtEmpContAnnual.value);
	if (fTemp1 > 0.0) {
		fTemp = new Number(fTemp1 / fPayPeriod);
		sTemp = "" + fTemp;
		document.forms[0].txtEmpCont.value = FormatDecimalNumber(sTemp,2);
	}
	
	fTemp1 = parseFloat(document.forms[0].txtMemVolContAnnual.value);
	if (fTemp1 > 0.0) {
		fTemp = new Number(fTemp1 / fPayPeriod);
		sTemp = "" + fTemp;
		document.forms[0].txtMemVolCont.value = FormatDecimalNumber(sTemp,2);
	}
	return true;
}
//-------------------------------------------------------------------
//------------------- Employer Contributions ------------------------
//
// Check that a number > 0 has been entered.
// Calculate annual value using amount entered and pay period value.
//
function CalcEmpContAnnual() {
	if (!EmpCont(document.forms[0].txtEmpCont.value)) {
		return false;
	}
	var fPayPeriod = parseFloat(document.forms[0].PayPeriodValue.value);
	var fEmpCont = parseFloat(document.forms[0].txtEmpCont.value);
	var fEmpContAnnual = new Number(fEmpCont * fPayPeriod);
	var sTemp;			// used to convert number to string
	
	sTemp = "" + fEmpCont;
	document.forms[0].txtEmpCont.value = FormatDecimalNumber(sTemp,2);
	sTemp = "" + fEmpContAnnual;
	document.forms[0].txtEmpContAnnual.value = FormatDecimalNumber(sTemp,2);

	return true;
}
//-------------------------------------------------------------------
//----------------- Member Voluntary Contributions ------------------
//
// Check that a number > 0 has been entered.
// Calculate annual value using amount entered and pay period value.
// Display amounts with 2 decimal places - convert number to string before
// calling function.
//
function CalcMemVolContAnnual() {
	if (!MemVolCont(document.forms[0].txtMemVolCont.value)) {
		return false;
	}
	
	var fPayPeriod = parseFloat(document.forms[0].PayPeriodValue.value);
	var fMemVolCont = parseFloat(document.forms[0].txtMemVolCont.value);
	var fMemVolContAnnual = new Number(fMemVolCont * fPayPeriod);
	var sTemp;			// used to convert number to string
	
	sTemp = "" + fMemVolCont;
	document.forms[0].txtMemVolCont.value = FormatDecimalNumber(sTemp,2);
	sTemp = "" + fMemVolContAnnual;
	document.forms[0].txtMemVolContAnnual.value = FormatDecimalNumber(sTemp,2);

	return true;
}
//-------------------------------------------------------------------
//------------------- Annual Insurance premiums ---------------------
//
// Calculate annual insurance premiums based on cover and price per $1000.
// Calculate total annual insurance.
//
function CalcInsuranceAnnual() {
	var fAnnualPremium;				// annual insurance as number
	var sAnnualPremium;				// annual insurance as string
	
	// Death Only / Death and TPD
	fAnnualPremium = new Number(parseFloat(document.forms[0].txtManualDeathTPDCover.value) / 1000.0 * parseFloat(document.forms[0].txtManualDeathTPDPer1000.value));
	sAnnualPremium = new String(fAnnualPremium);	// convert to string for Format
	document.forms[0].txtManualDeathTPDAnnual.value = FormatDecimalNumber(sAnnualPremium,2);
	
	// Salary Continuance
	fAnnualPremium = new Number(parseFloat(document.forms[0].txtManualSalaryContinuanceCover.value) / 100.0 * parseFloat(document.forms[0].txtManualSalaryContinuancePer100.value));
	sAnnualPremium = new String(fAnnualPremium);	// convert to string for Format
	document.forms[0].txtManualSalaryContinuanceAnnual.value = FormatDecimalNumber(sAnnualPremium,2);
	
	// Total Annual
	fAnnualPremium = new Number(parseFloat(document.forms[0].txtManualDeathTPDAnnual.value) + parseFloat(document.forms[0].txtManualSalaryContinuanceAnnual.value));	
	sAnnualPremium = new String(fAnnualPremium);	// convert to string for Format
	document.forms[0].txtInsuranceAnnual.value = FormatDecimalNumber(sAnnualPremium,2);

	return true;
}
//-------------------------------------------------------------------
//--------------------- Check Insurance Cover -----------------------
//
// Check insurance cover and premium entered.
// If blank, set to zero.
//
function CheckUnitValue(sfieldName) {
	var fUnits;
	var sUnits = document.forms[0].elements[sfieldName].value;	// field value
	
	// If blank, set to 0
	if (sUnits == "") {
		sUnits = "0";
		document.forms[0].elements[sfieldName].value = "0";
	}
	
	// If not numeric, set to 0
	if (!isDecimalNumber(sUnits)) {
		alert ("Please enter a number for insurance cover.\nResetting to 0.");
		sUnits = "0";
		document.forms[0].elements[sfieldName].value = "0";
		document.forms[0].elements[sfieldName].focus();
		document.forms[0].elements[sfieldName].select();
	}
	
	// Check not negative
	fUnits = parseFloat(sUnits);
	if (fUnits < 0.00) {
		alert ("Insurance cover cannot be less than zero.\nResetting to 0.");
		sUnits = "0";
		document.forms[0].elements[sfieldName].value = "0";
		document.forms[0].elements[sfieldName].focus();
		document.forms[0].elements[sfieldName].select();		
	}
	return true;
}
//-------------------------------------------------------------------
//------------------------- Insurance Cover -------------------------
//
// If blank, set to 0.
// Check amount entered is integer and between 0 and upper limit. 
// Calculate annual insurance.
//
function CalcCover(sOption, sfieldName) {

	var fAnnualPremium;
	var sAnnualPremium;

	// Check value entered is OK
	CheckUnitValue(sfieldName);
	
	var fUnits;
	var sUnits = document.forms[0].elements[sfieldName].value;	// field value
	
	if (sOption == "1"){
		// Check cover requested not greater than $1M cover
		fUnits = parseFloat(sUnits);
		if (fUnits > 1000000.00) {
			alert ("Death Only / Death and TPD Insurance cover must be between 0 and 1000000.\nResetting to 0.");
			sUnits = "0";
			document.forms[0].elements[sfieldName].value = "0";
			document.forms[0].elements[sfieldName].focus();
			document.forms[0].elements[sfieldName].select();		
		}
	}
	if (sOption == "2"){
		// Check premium per $1000 not greater than $50
		fUnits = parseFloat(sUnits);
		if (fUnits > 50.00) {
			alert ("Death Only / Death and TPD Insurance premiums per $1000 cover must be between 0 and 50.\nResetting to 0.");
			sUnits = "0";
			document.forms[0].elements[sfieldName].value = "0";
			document.forms[0].elements[sfieldName].focus();
			document.forms[0].elements[sfieldName].select();		
		}
	}

	if (sOption == "11"){
		// Check cover requested not greater than $10,000 cover
		fUnits = parseFloat(sUnits);
		if (fUnits > 10000.00) {
			alert ("Salary Continuance insurance cover must be between 0 and 10000.\nResetting to 0.");
			sUnits = "0";
			document.forms[0].elements[sfieldName].value = "0";
			document.forms[0].elements[sfieldName].focus();
			document.forms[0].elements[sfieldName].select();		
		}
	}
	if (sOption == "12"){
		// Check premium per $1000 not greater than $90
		fUnits = parseFloat(sUnits);
		if (fUnits > 90.00) {
			alert ("Salary Continuance insurance premiums per $100 cover must be between 0 and 90.\nResetting to 0.");
			sUnits = "0";
			document.forms[0].elements[sfieldName].value = "0";
			document.forms[0].elements[sfieldName].focus();
			document.forms[0].elements[sfieldName].select();		
		}
	}

	// Calculate annual insurance.
	CalcInsuranceAnnual();

	return true;
}
//*******************************************************************
//*******************************************************************
//
//------------ Calculate the projected retirement balance -----------
//------------------------- CalcSuper() -----------------------------
//
// Check all input fields first, in case an error has not been 
// corrected by the user.
// If all values valid, calculate prospective balance.
//
function CalcSuper() {
  if (!CheckValues()) {
		return;
  }
  //
  // Read values from form and convert to integer or decimal values.
  // Subtract current age from retirement age to use as loop counter.
  //
  var iAgeToday = parseInt(document.forms[0].txtAgeToday.value,10);
  var iRetireAge = parseInt(document.forms[0].txtRetireAge.value,10);
  var iYears = new Number(iRetireAge - iAgeToday);
  //
  // Divide rates by 100 to convert to percentages.
  //  
  var fFundRate = parseFloat(document.forms[0].txtFundRate.value);
  var fCPI = parseFloat(document.forms[0].txtCPI.value);
  var fFundPercent = new Number(fFundRate / 100.00);
  var fCPIPercent = new Number(fCPI / 100.00);
  //
  // All employer contributions are taxed at 15%.
  // Member voluntary contributions are not taxed, as tax has already been paid.
  // Annual fees are $2.00 per week.
  //
  var fEmpContAnnual = parseFloat(document.forms[0].txtEmpContAnnual.value);
  var fMemVolContAnnual = parseFloat(document.forms[0].txtMemVolContAnnual.value);
  var fInsuranceAnnual = new Number(parseFloat(document.forms[0].txtInsuranceAnnual.value));
  var fAnnualFees = new Number(52.00 * 2.00);

  var fBeginBal = parseFloat(document.forms[0].txtBeginBal.value);
  var OpenBal = new Number(fBeginBal);
  var CloseBal = 0;
  var sCloseBal = "";
  var fInterest;
  var sMsg = "";
  //
  // For each year, calculate interest on the opening balance. 
  // Take tax from employer contributions.
  // Add interest, employer and member contributions; subtract fees and insurance, to get the closing balance. 
  // Make this the opening balance for the next year.
  //
  for (var i = 1; i <= iYears; i++) {
	fInterest = new Number(OpenBal * fFundPercent);
	if (fInterest < 0.0) {
		fInterest = 0.00;
	}
	fEmpContTotal = new Number(fEmpContAnnual * 0.85);	// take tax out
	CloseBal = new Number(OpenBal + fInterest + fEmpContTotal + fMemVolContAnnual - fAnnualFees - fInsuranceAnnual + 0.5);
	sCloseBal = new String(CloseBal);
    OpenBal = parseInt(sCloseBal,10);
    
    // increment contributions by CPI
    fEmpContAnnual = new Number(fEmpContAnnual * (1 + fCPIPercent));
    fMemVolContAnnual = new Number(fMemVolContAnnual * (1 + fCPIPercent));
  }
  if (OpenBal < 0) {
		OpenBal = 0;
  }
  // 
  // Calculate present day value from future value by subtracting CPI each year.
  // Correct formula is FutureValue * 1 / (1 + CPI)raised to the NumberOfYears
  // No exponentiation in JavaScript, so loop around.
  //
  var sPresentBal = "";
  var fPresentBal = new Number(OpenBal);
  var fPresentPercent = new Number (1.00 / (1.00 + fCPIPercent));
  for (var j = iYears; j > 0; j--) {
		fPresentBal = fPresentBal * fPresentPercent;
  }
  //
  // Display values on page
  //
  sCloseBal = new String(OpenBal);
  sPresentBal = new String(fPresentBal);
  document.forms[0].txtRetireBal.value = FormatIntegerMoney(sCloseBal);
  document.forms[0].txtTrueRetireBal.value = FormatIntegerMoney(sPresentBal);
  document.forms[0].hiddenRetireBal.value = OpenBal;
  
  return;
}
//
//*******************************************************************
//*******************************************************************
//
//--------------- 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.
//
// Check retirement age before current age, as current age needs a 
// retirement age to be less than. 
//
function CheckValues() {
  if (!RetireAge(document.forms[0].txtRetireAge.value)) {
		return false;
  }
  if (!AgeToday(document.forms[0].txtAgeToday.value)) {
		return false;
  }
  if (!BeginBal(document.forms[0].txtBeginBal.value)) {
		return false;
  }
  if (!EmpCont(document.forms[0].txtEmpCont.value)) {
		return false;
  }
  if (!MemVolCont(document.forms[0].txtMemVolCont.value)) {
		return false;
  }
  if (!Insurance()) {
		return false;
  }
  if (!FundRate(document.forms[0].txtFundRate.value)) {
		return false;
  }
  if (!CPI(document.forms[0].txtCPI.value)) {
		return false;
  }
  return true;
}
//-------------------------------------------------------------------
// Check the Age is numeric and between 18 and 90.
// If no value, set to 30.
// Cannot be greater than retirement age.
// 
function AgeToday(sAge) {
  if (sAge == "") {
    document.forms[0].txtAgeToday.value = 30;
    sAge = 30;
  }
  else {
	if (!isIntegerNumber(sAge)) {
		alert("Please enter a number between 18 and 90 for your current age.");
		document.forms[0].txtAgeToday.focus();
		document.forms[0].txtAgeToday.select();
		return false;
	}
  }
  if ((sAge < 18) || (sAge > 90)) {
	alert("Please enter a number between 18 and 90 for your current age.");
	document.forms[0].txtAgeToday.focus();
	document.forms[0].txtAgeToday.select();
	return false;
  }
  if (sAge > document.forms[0].txtRetireAge.value) {
	alert("In this calculator, your current age cannot be later than your specified retirement age.");
	document.forms[0].txtAgeToday.focus();
	document.forms[0].txtAgeToday.select();
	return false;
  }
  return true;
}
//-------------------------------------------------------------------
// Check the Beginning Balance is numeric.
// If no value, set to zero.
// If negative, set to zero.
//
function BeginBal(sBeginBal) {
  if (sBeginBal == "") {
    document.forms[0].txtBeginBal.value = 0;
    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) {
    document.forms[0].txtBeginBal.value = 0;
  }
  return true;
}
//-------------------------------------------------------------------
// 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.0) {
	document.forms[0].txtCPI.value = "0.0";
	fCPI = 0.0;
  }
  else {
	if (fCPI > 20.0) {
		document.forms[0].txtCPI.value = "20.0";
		fCPI = 20.0;
	}
  }
  var fFundRate = parseFloat(document.forms[0].txtFundRate.value);
  if (fCPI >= fFundRate) {
	alert("The Estimated CPI increase must be less than the Estimated Fund Crediting Rate.");
	document.forms[0].txtCPI.focus();
	document.forms[0].txtCPI.select();
	return false;
  }
  return true;
}
//-------------------------------------------------------------------
// Check the Employer Contribution is numeric.
// If no value, set to zero.
// If negative, set to zero.
//
function EmpCont(sEmpCont) {
	if (sEmpCont == "") {
	    document.forms[0].txtEmpCont.value = 0;
	    document.forms[0].txtEmpContAnnual.value = 0;
	    return true;
	}
	if (!isDecimalNumber(sEmpCont)) {
		alert("Please enter a number for the Employer Contribution.");
	    document.forms[0].txtEmpContAnnual.value = 0;
		document.forms[0].txtEmpCont.focus();
		document.forms[0].txtEmpCont.select();
		return false;
	}
	if (sEmpCont < 0) {
	    document.forms[0].txtEmpCont.value = 0;
	    document.forms[0].txtEmpContAnnual.value = 0;
	    return true;
	}
	return true;
}
//-------------------------------------------------------------------
// Check the Fund Rate is numeric.
// If no value, set to default.
// Cannot be less than CPI.
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.0) {
    document.forms[0].txtFundRate.value = "0.0";
    fFundRate = 0.0;
  }
  else {
	if (fFundRate > 20.0) {
		document.forms[0].txtFundRate.value = "20.0";
		fFundRate = 20.0;
	}
  }
  var fCPI = parseFloat(document.forms[0].txtCPI.value);
  if (fFundRate <= fCPI) {
	alert("The Estimated Fund Crediting Rate must be greater than the Estimated CPI increase.");
	document.forms[0].txtFundRate.focus();
	document.forms[0].txtFundRate.select();
	return false;
  }
  return true;
}
//-------------------------------------------------------------------
// Check insurance.
// 
function Insurance() {

	CalcCover("1", "txtManualDeathTPDCover");
	CalcCover("2", "txtManualDeathTPDPer1000");
	CalcCover("11", "txtManualSalaryContinuanceCover");
	CalcCover("12", "txtManualSalaryContinuancePer100");

	return true;
}
//-------------------------------------------------------------------
// Check the Member Voluntary Contribution is numeric.
// If no value, set to zero.
// If negative, set to zero.
//
function MemVolCont(sMemVolCont) {
	if (sMemVolCont == "") {
	    document.forms[0].txtMemVolCont.value = 0;
	    document.forms[0].txtMemVolContAnnual.value = 0;
	    return true;
	}
	if (!isDecimalNumber(sMemVolCont)) {
		alert("Please enter a number for the Member Voluntary Contribution.");
	    document.forms[0].txtMemVolContAnnual.value = 0;
		document.forms[0].txtMemVolCont.focus();
		document.forms[0].txtMemVolCont.select();
		return false;
	}
	if (sMemVolCont < 0) {
	    document.forms[0].txtMemVolCont.value = 0;
	    document.forms[0].txtMemVolContAnnual.value = 0;
	    return true;
	}
	return true;
}
//-------------------------------------------------------------------
// Check the Retirement Age is numeric and between 55 and 100.
// If no value, set to 65.
// Cannot be earlier than current age.
//
function RetireAge(sRetire) {
  if (sRetire == "") {
    document.forms[0].txtRetireAge.value = 65;
    sRetire = 65;
  }
  else {
	if (!isIntegerNumber(sRetire)) {
		alert("Please enter a number between 55 and 100 for your retirement age.");
		document.forms[0].txtRetireAge.focus();
		document.forms[0].txtRetireAge.select();
		return false;
	}
  }
  if ((sRetire < 55) || (sRetire > 100)) {
	alert("Please enter a number between 55 and 100 for your retirement age.");
	document.forms[0].txtRetireAge.focus();
	document.forms[0].txtRetireAge.select();
	return false;
  }
  if (sRetire < document.forms[0].txtAgeToday.value) {
	alert("In this calculator, your specified retirement age cannot be earlier than your current age.");
	document.forms[0].txtRetireAge.focus();
	document.forms[0].txtRetireAge.select();
	return false;
  }
  return true;
}
//
//*******************************************************************
//*******************************************************************
//
//------------- 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;
	}
	if (document.forms[0].hiddenRetireBal.value == "0") {
		alert ("There is no projected balance to display.\n\nPlease enter different values.");
		return;
	}
	document.forms[0].submit();
}
//===================================================================
//-->

