
function FormUpdate() {
    var form = document.NZLIncomeNeedsCalc;
    IncomeLifeNeeds(form);
}

function IncomeLifeNeeds(form) {
                    	var rent = parseFloat(form.RentMortgage.value);
	var monthlydebts = parseFloat(form.MonthlyDebts.value);
	var expenses = parseFloat(form.Expenses.value);
	
	if(!ValidateNum(rent)) {
		alert('Please enter a number value');
		form.RentMortgage.value = 0;
		rent = 0;
	}

	if(!ValidateNum(monthlydebts)) {
		alert('Please enter a number value');
		form.MonthlyDebts.value = 0;
		monthlydebts = 0;
	}

	if(!ValidateNum(expenses)) {
		alert('Please enter a number value');
		form.Expenses.value = 0;
		expenses = 0;
	}

	var totalexpenses = rent+monthlydebts+expenses;
	form.TotalExpenses.value = FmtPrice(totalexpenses);

	var ongoingincome = parseFloat(form.OngoingIncome.value);
	var partnersincome = parseFloat(form.PartnersIncome.value);
	var existinginsurance = parseFloat(form.ExistingInsurance.value);
	
	if(!ValidateNum(ongoingincome)) {
		alert('Please enter a number value');
		form.OngoingIncome.value = 0;
		ongoingincome = 0;
	}

	if(!ValidateNum(partnersincome)) {
		alert('Please enter a number value');
		form.PartnersIncome.value = 0;
		partnersincome = 0;
	}

	if(!ValidateNum(existinginsurance)) {
		alert('Please enter a number value');
		form.ExistingInsurance.value = 0;
		existinginsurance = 0;
	}

	var totalongoingincome = ongoingincome+partnersincome+existinginsurance;
	form.TotalOngoingIncome.value = FmtPrice(totalongoingincome);

	var totalexpensesongoing = totalexpenses-totalongoingincome;
	form.TotalExpensesOngoing.value = FmtPrice(totalexpensesongoing);

                    	var annualincome = form.AnnualIncome.value;
                    	
	if(!ValidateNum(annualincome)) {
		alert('Please enter a number value');
		form.AnnualIncome.value = 0;
		annualincome = 0;
	}

	var grossmonthly = annualincome/24;
	form.GrossMonthly.value = FmtPrice(grossmonthly);
	document.NZLIncomeRequest.AnnualIncome.value = FmtPrice(grossmonthly);
		
	if (totalexpensesongoing <= grossmonthly) 
	{
	form.IncomeProtection.value = FmtPrice(totalexpensesongoing);
	document.NZLIncomeRequest.IncomeProtection.value = FmtPrice(totalexpensesongoing);
	}
	else
	{
	form.IncomeProtection.value = FmtPrice(grossmonthly);
	document.NZLIncomeRequest.IncomeProtection.value = FmtPrice(grossmonthly);
	}
}

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;
}


