
function FormUpdate() {
    var form = document.NZLQuickCalc;
    QuickCalc(form);
}

function   QuickCalc(form) {
	var lifecost1 = parseFloat(form.LifeCost1.value);
	var lifeInsurance = parseFloat(form.LifeInsurance1.value);
	var agelife1 = parseFloat(form.AgeLife1.value);


	if(!ValidateAge(agelife1)) {
	  alert('Quotes for this product can only be provided between the ages of 16 and 55. For quotes over this age go to www.adviser.co.nz or contact us.');
	}

	if(!ValidateNum(lifecost1)) {
		lifecost1 = 0;
	}

	if(!PriceMinMax(lifeInsurance)) {
	  alert('The minimum for this benefit is $30,000 and the maximum is $500,000. Please remove any commas or currency dollar signs. If you require sums outside these limits visit www.adviser.co.nz and a personalised quote will be emailed to you.');
	}

	var CostYear = lifecost1*12;
	var fivePercent = CostYear/100*4.75;
	var lifeCostYear = CostYear-fivePercent;

	if(!ValidateNum(lifeCostYear)) {
		lifeCostYear = 0;
	}


	form.LifeCost1.value = FmtPrice(lifecost1);
	form.LifeCostYear1.value = FmtPrice(lifeCostYear);
}

function PriceMinMax(checkVal) {

	returnVal = parseFloat(checkVal);
	
	if(returnVal == 0) return true;	
	else if(returnVal < 30000) return false;
	else if(returnVal > 500000) return false;
	else return true;
}


function ValidateTotal(checkVal) {

	returnVal = parseFloat(checkVal);

	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;
}

function ValidateLifeCost(checkVal) {

	returnVal = parseFloat(checkVal);

	if(isNaN(returnVal)) return false;
	else return true;
}

function ValidateAge(checkVal) {

	returnVal = parseFloat(checkVal);

	if(returnVal < 16) return false;
	else if(returnVal > 55) return false;
	else return true;
}
