
function FormUpdate() {
    var form = document.NZLCritillNeedsCalc;
    CritillLifeNeeds(form);
}

function CritillLifeNeeds(form) {
                    	var medical = parseFloat(form.MedicalCosts.value);
	var extrasupport = parseFloat(form.ExtraSupportCosts.value);
	var debts = parseFloat(form.Debts.value);
	var tax = parseFloat(form.TaxGST.value);
	var mortgage = parseFloat(form.Mortgage.value);
	var othercosts = parseFloat(form.OtherCosts.value);
	
	if(!ValidateNum(medical)) {
		alert('Please enter a number value');
		form.MedicalCosts.value = 0;
		medical = 0;
	}
	
	if(!ValidateNum(extrasupport)) {
		alert('Please enter a number value');
		form.ExtraSupportCosts.value = 0;
		extrasupport = 0;
	}
	
	if(!ValidateNum(debts)) {
		alert('Please enter a number value');
		form.Debts.value = 0;
		debts = 0;
	}
	
	if(!ValidateNum(tax)) {
		alert('Please enter a number value');
		form.TaxGST.value = 0;
		tax = 0;
	}
	
	if(!ValidateNum(mortgage)) {
		alert('Please enter a number value');
		form.Mortgage.value = 0;
		mortgage = 0;
	}
	
	if(!ValidateNum(othercosts)) {
		alert('Please enter a number value');
		form.OtherCosts.value = 0;
		othercosts = 0;
	}
	
	var totalongoingexpenses = medical+extrasupport+debts+tax+mortgage+othercosts;
	form.TotalOngoingExpenses.value = FmtPrice(totalongoingexpenses);

	var needlivemonth = form.NeedLiveMonth.value;
	var monthsneedsupport = form.MonthsNeedSupport.value;
	
	if(!ValidateNum(needlivemonth)) {
		alert('Please enter a number value');
		form.NeedLiveMonth.value = 0;
		needlivemonth = 0;
	}
	
	if(!ValidateNum(monthsneedsupport)) {
		alert('Please enter a number value');
		form.MonthsNeedSupport.value = 0;
		monthsneedsupport = 0;
	}
	
	var totalrequiredincome = needlivemonth*monthsneedsupport;
	form.TotalRequiredIncome.value = FmtPrice(totalrequiredincome);

	var cash = parseFloat(form.CashSavings.value);
	var existinginsurance = parseFloat(form.ExistingInsurance.value);
	
	if(!ValidateNum(cash)) {
		alert('Please enter a number value');
		form.CashSavings.value = 0;
		cash = 0;
	}
	
	if(!ValidateNum(existinginsurance)) {
		alert('Please enter a number value');
		form.ExistingInsurance.value = 0;
		existinginsurance = 0;
	}
	var totalassets = cash+existinginsurance;
	form.TotalAssets.value = FmtPrice(totalassets);

	var critillinsurance = (totalongoingexpenses+totalrequiredincome)-totalassets;
	form.CritillInsurance.value = FmtPrice(critillinsurance);
}

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;
}


