<!--
var isIE = document.all?true:false;
var isNS = document.layers?true:false;

function onlyDigits(evt) {
	var _ret = true;
	if (isIE) {
		if (! ( window.event.keyCode == 44 || window.event.keyCode == 46 || (window.event.keyCode >= 48 && window.event.keyCode <= 57)) ) {
			window.event.keyCode = 0;
			_ret = false;
		}
	}

	if (isNS) {
		if (! ( evt.which == 44 || evt.which == 46 || (evt.which >= 48 && evt.which <= 57)) ) {
			evt.which = 0;
			_ret = false;
		}
	}

	return (_ret);
}


if (isNS) document.captureEvents(Event.KEYPRESS);
document.onkeypress=onlyDigits;










function isNumeric(fieldStr)
{
	var regPattern = /(^[\d-]?([\d\.]+)$)/;

	return regPattern.test(fieldStr);
}

function removeComma(num)
{
// All variables are decalared here
	var re =/,/g;

	var str = num.toString();

// This function looks at the incoming string and removes all the commas from it.
	str = str.replace(re,"");

	str = parseFloat(str);

    if (isNaN(str)) {
        str = "";
    }

	return str;

}


function formatIntAmount(Expr)
{
layerLoc=document.Calculator;
// All variables are decalared here
	var DecPlaces = 0;

	var str = "" + Math.round(eval(Expr) * Math.pow(10, DecPlaces));
	var DecPoint;
	var Result;
	var re;

// This function looks at the incoming string and formats it with two decimal places
// and a comma for every 3 digits.
	while (str.length <= DecPlaces) {
		str = "0" + str;
	}
        
	DecPoint = str.length - DecPlaces;

	Result = str.substring(0, DecPoint);

	re = /(-?\d+)(\d{3})/;

	while (re.test(Result)) {
		Result = Result.replace(re, "$1,$2");
	}

	return Result;
}

function fieldFormat_incomeReqd() {
				 layerLoc=document.Calculator;

				 var incomeReqd_field = removeComma(layerLoc.incomeReqd.value);
				 layerLoc.incomeReqd.value = formatIntAmount(incomeReqd_field);
}

function fieldFormat_currSuper() {

				 layerLoc=document.Calculator;
				 
				 var currSuper_field = removeComma(layerLoc.currSuper.value);
				 layerLoc.currSuper.value = formatIntAmount(currSuper_field);
}

function fieldFormat_currSavings() {

				 layerLoc=document.Calculator;

				 var currSavings_field = removeComma(layerLoc.currSavings.value);
				 layerLoc.currSavings.value = formatIntAmount(currSavings_field);
}

function calculate(){

	layerLoc=document.Calculator;
	
    var age, sex, incomeReqd, currSuper, CurrSaving;
    
    // clear result incase this is 2nd time thru or the user has entered value
    layerLoc.result.value = "";
    // validate user input
    age = layerLoc.age.value;
    if (age == "") {
        alert ( "Please enter your age");
        layerLoc.age.focus( );
        return;
    }
    age = parseInt(age);
    if (isNaN(age)) {
        alert ( "Age must be numeric");
        layerLoc.age.focus( );
        return;
    }
    if ( age < 25 | age > 60) {
         alert ( "Age must be between 25 and 60");
         layerLoc.age.focus( );
         return;
    }	
    	
    sex = "";	
    for (i = 0; i < layerLoc.sex.length; i++){ 
        if (layerLoc.sex[i].checked == true) {
            sex = layerLoc.sex[i].value;
            break;
        }
    }
    if (sex == "") {
        alert ( "Please select your gender");
        layerLoc.sex[0].focus( );
        return;
    }
		
    incomeReqd = removeComma(layerLoc.incomeReqd.value); 
    if (incomeReqd == "") {
        alert ( "Please enter amount you require to live on");
        layerLoc.incomeReqd.focus( );
        return;
    }

	incomeReqd = parseInt(incomeReqd);
    if (isNaN(incomeReqd)) {
        alert ( "Amount must be numeric");
        layerLoc.incomeReqd.focus( );
        return;
    }

    if (incomeReqd < 0) {
        alert ( "Amount must be positive");
        layerLoc.incomeReqd.focus( );
        return;
    }

    currSuper = removeComma(layerLoc.currSuper.value); 
    if (currSuper == "") {
        currSuper = 0; 
        layerLoc.currSuper.value = 0;
    }
    currSuper = parseInt(currSuper);
    if (isNaN(currSuper)) {
        alert ( "Amount must be numeric");
        layerLoc.currSuper.focus( );
        return;
    }

    if (currSuper < 0) {
        alert ( "Amount must be positive");
        layerLoc.currSuper.focus( );
        return;
    }

    currSavings = removeComma(layerLoc.currSavings.value); 
    if (currSavings == "") {
        currSavings = 0; 
        layerLoc.currSavings.value = 0;
    }
    currSavings = parseInt(currSavings);
    if (isNaN(currSavings)) {
        alert ( "Amount must be numeric");
        layerLoc.currSavings.focus( );
        return;
    }

    if (currSavings < 0) {
        alert ( "Amount must be positive");
        layerLoc.currSavings.focus( );
        return;
    }	

    // calculate required contributions based on spreadsheet supplied by MLC sc.xls
    var lifeExp, accumSuper, accumSavings, accumTotal;

    if (sex == "male") {
        lifeExp = lifeExpM;
    } else {
        lifeExp = lifeExpF;
    }
    
    // --------------------------------------------------------------------
    //** Accumulated Savings at Retirement Age
    // --------------------------------------------------------------------
    //** Accumulated Super at Retirement Age
    accumSuper = currSuper * Math.pow((1+fundRateBefore),(retirementAge-age));

    //** Accumulated Ordinary Savings at Retirement Age
    accumSavings = currSavings * Math.pow((1+ordinaryRate),(retirementAge-age));

    //** Accumulated Total
    accumTotal = accumSuper + accumSavings;

    // --------------------------------------------------------------------
    //** Income generated from ordinary savings (per annum)
    // --------------------------------------------------------------------
    var eRate, annnuityDue, sIncome, incomeID, annumIncome, need, shortfall, contrib, sn;

    // ** Earning rate adjusted for inflation
    eRate =((1+ordinaryRate)/(1+inflationRate))-1;

    // ** Annuity due ?? always uses female life exp ??
    annuityDue = ((1-(Math.pow((1+eRate), -(lifeExp+1))))/eRate)*(1+eRate);

    // ** Income per annum
    sIncome = accumSavings/annuityDue;
     
    // --------------------------------------------------------------------
    //** What you need at Retirement age
    // --------------------------------------------------------------------
    //** Per annum income required (Today's dollars)

 
    //** Per annum income adjusted for inflation
    incomeID =  incomeReqd * Math.pow((1+inflationRate),(retirementAge-age));

    //** Per annum income generated from ordinary savings
 
    //** Per annum income at retirement age  
    annumIncome = incomeID-sIncome;  

    // ** Earning rate adjusted for inflation
    eRate =((1+fundRateAfter)/(1+inflationRate))-1;

    // ** Annuity due
    annuityDue = ((1-Math.pow((1+eRate), -(lifeExp+1)))/eRate)*(1+eRate);

    // ** Need
    need = annumIncome * annuityDue;

    // --------------------------------------------------------------------
    //** What you need to contribute:     
    // --------------------------------------------------------------------
    //** Shortfall     
    shortfall = need - accumSuper;
     
    // ** Earning rate adjusted for inflation
    eRate =((1+fundRateBefore)/(1+inflationRate))-1;

    // ** Annuity due
    annuityDue = ((1-1/Math.pow((1+eRate), (retirementAge-age)))/eRate)*(1+eRate);

    // ** Sn
    sn = annuityDue*Math.pow((1+fundRateBefore),(retirementAge-age));
     
    // ** Annual Contribution
    contrib = Math.round((shortfall/sn)/0.85);
    if (contrib < 0) {
        contrib = 0;
    }

    // ** format answer for display??

    layerLoc.result.value = formatIntAmount(contrib);
    layerLoc.result.focus();  
    return;	
}

function submitData() {
	var locationURL = "https://www.nab.com.au/cgi-bin/contactBanker.pl";
	var loadType= "WEBGDW";
	var trackingIdPrefix = "ST";
	var sourceCode ="STSUPER";

	var columns = "";
	var data = "";
	var errorMsg = "";
	var reEnterMsg = " <p> Please click the 'Close' button and reenter it.</p>";

	var form = document.Calculator;

    // Prepare form values for submission
	var age = form.age.value.toString();

	var genderCalc = "";
	if	(form.sex[0].checked) {
		genderCalc = "F"
	}

	if	(form.sex[1].checked) {
		genderCalc = "M"
	}

	var incomeRequired = removeComma(form.incomeReqd.value).toString();
	var investedSuper = removeComma(form.currSuper.value).toString();
	var investedSavings = removeComma(form.currSavings.value).toString();
	var contribution = removeComma(form.result.value).toString();

    // Validate each form field
    errorMsg += isFieldValid(age,"Age ",0,2,"custom","(^[0-9]{0,2}\$)");
	errorMsg += isFieldValid(genderCalc,"Gender ",0,1,"custom","^(F|M)?\$");
	errorMsg += isFieldValid(incomeRequired,"Income required to live on in retirement ",0,15,"custom","(^([0-9]{0,12}[\.]?[0-9]?[0-9])?\$)?");
	errorMsg += isFieldValid(investedSuper,"Amount currently invested in superannuation ",0,15,"custom","(^([0-9]{0,12}[\.]?[0-9]?[0-9])?\$)?");
	errorMsg += isFieldValid(investedSavings,"Amount currently invested in ordinary savings ",0,15,"custom","(^([0-9]{0,12}[\.]?[0-9]?[0-9])?\$)?");
	errorMsg += isFieldValid(contribution,"Annual contribution required ",0,15,"custom","(^([0-9]{0,12}[\.]?[0-9]?[0-9])?\$)?");
	
    columns = "age,genderCalc,incomeRequired,investedSuper,investedSavings,contribution";
    
	data = age + "," + genderCalc + "," + incomeRequired + "," + investedSuper + "," + investedSavings + "," + contribution;

	//if errorMsg, do not go to form
	if (isEmpty(errorMsg)) {
		var url = locationURL + "?natLead.type=" + loadType +"&natLead.columns=" + columns +"&natLead.trackingIdPrefix=" + trackingIdPrefix + "&natLead.data=" + data + "&natLead.sourceCode=" + sourceCode;
		globalWindowOpen(url,'SuperCalc',620,400,1,0,0,1,0,0,0,150,150);
	} else {
		popup = window.open("","SuperCalc","width=400,height=400,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
		popup.document.write("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head>");
		popup.document.write("<link rel='stylesheet' href='/css/National/GlobalStyle.css' type='text/css'></head>");
		popup.document.write("<title> The National - Superannuation Calculator </title>");
		popup.document.write("<body>");
		
		popup.document.write("<strong> Superannuation Calculator</strong><br><br>");
		popup.document.write(errorMsg);
		popup.document.write("<br>");
		popup.document.write(reEnterMsg);
		popup.document.write("<center><form><input type=button value=Close onClick=javascript:window.close();></center></form>");
		popup.document.write("</body>");
		popup.document.write("</html>");
		popup.document.close();
	}
}
// --> 

