
function FormUpdate() {
    var form = document.NZLLifeNeedsCalc;
    CalcLifeNeeds(form);
}

function   CalcLifeNeeds(form) {
                    	var funeral = parseFloat(form.FuneralCosts.value);
	var debts = parseFloat(form.Debts.value);
	var tax = parseFloat(form.TaxGST.value);
	var mortgage = parseFloat(form.Mortgage.value);
	var other = parseFloat(form.Other.value);

	if(!ValidateNum(funeral)) {
		alert('Please enter a number value for Funeral Costs');
		form.FuneralCosts.value = 0;
		funeral = 0;
	}

	if(!ValidateNum(debts)) {
		alert('Please enter a number value for Overdraft, Personal loans, Credit cards...');
		form.Debts.value = 0;
		debts = 0;
	}

	if(!ValidateNum(tax)) {
		alert('Please enter a number value for Outstanding Tax, GST');
		form.TaxGST.value = 0;
		tax = 0;
	}

	if(!ValidateNum(mortgage)) {
		alert('Please enter a number value for Mortgage');
		form.Mortgage.value = 0;
		mortgage = 0;
	}

	if(!ValidateNum(other)) {
		alert('Please enter a number value for Other');
		form.Other.value = 0;
		other = 0;
	}

	var totalexpenses = funeral+debts+tax+mortgage+other;
	form.TotalExpenses.value = FmtPrice(totalexpenses);


	var needliveyear = form.NeedLiveYear.value;
	var yearsneedsupport = form.YearsNeedSupport.value;

	if(!ValidateNum(needliveyear)) {
		alert('Please enter a number value');
		form.NeedLiveYear.value = 0;
		needliveyear = 0;
	}

	if(!ValidateNum(yearsneedsupport)) {
		alert('Please enter a number value');
		form.YearsNeedSupport.value = 0;
		yearsneedsupport = 0;
	}

	var totalfutureincome = needliveyear*yearsneedsupport;
	form.TotalFutureIncome.value = FmtPrice(totalfutureincome);

                    	var cashsavings = parseFloat(form.CashSavings.value);
                    	var salesassets = parseFloat(form.SalesAssets.value);
                    	var existinglife = parseFloat(form.ExistingLife.value);
                    	
	if(!ValidateNum(cashsavings)) {
		alert('Please enter a number value');
		form.CashSavings.value = 0;
		cashsavings = 0;
	}
	
	if(!ValidateNum(salesassets)) {
		alert('Please enter a number value');
		form.SalesAssets.value = 0;
		salesassets = 0;
	}

	if(!ValidateNum(existinglife)) {
		alert('Please enter a number value');
		form.ExistingLife.value = 0;
		existinglife = 0;
	}

	var totalassets = cashsavings+salesassets+existinglife;
                    	form.TotalAssets.value = FmtPrice(totalassets);

                    	var calculationtotal = (totalexpenses+totalfutureincome)-totalassets;
                    	form.LifeInsurance.value = FmtPrice(calculationtotal);
	}

function ValidateNum(checkVal) {

	returnVal = parseFloat(checkVal);

	if(isNaN(returnVal)) return false;
	else if(returnVal < 0) return false;
	else return true;
}


function FmtPrice(result) {
                    result = ConvNumber(result, 2, ".");
                    return result;
}

function ConvNumber(expr, decplaces, point) {

	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	return (str.substring(0,decpoint) + point + str.substring(decpoint,str.length));
}

function ValidateNum(checkVal) {

	returnVal = parseFloat(checkVal);

	if(isNaN(returnVal)) return false;
	else if(returnVal < 0) return false;
	else return true;
}


