function Fn(x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue) {
	var y;
	y = (1 * LoanAmountValue) + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow(1.0 + clci, -x)) / clci);
	y = (y * 1) + clcfv * Math.pow(1.0 + clci, -x);
	return y;
}

function Fi(x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue) {
	var y;
	y = (LoanAmountValue * 1) + ((1 + (x * clcs)) * clcpmt * (1 - Math.pow(1.0 + x, -clcn)) / x);
	y = (y * 1) + clcfv * Math.pow(1.0 + x, -clcn);
	return y;
}

function Fpmt(x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue) {
	var y = 0;
	y = ((1 + (clci * clcs)) * (1 - Math.pow(1.0 + clci, -clcn)) / clci);
	y = y * x;
	y = (y + (LoanAmountValue * 1));
	y = (y * 1) + (clcfv * Math.pow(1.0 + clci, -clcn));
	return y;
}

function Ffv(x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue) {
	var y;
	y = (LoanAmountValue * 1) + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow(1.0 + clci, -clcn)) / clci);
	y = (y * 1) + x * Math.pow(1.0 + clci, -clcn);
	return y;
}

function Fpv(x, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue) {
	var y;
	y = (x * 1) + ((1 + (clci * clcs)) * clcpmt * (1 - Math.pow(1.0 + clci, -clcn)) / clci);
	y = (y * 1) + clcfv * Math.pow(1.0 + clci, -clcn);
	return y;
}

function Calculate(i_type, sval, ev, iv, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue) {
	var d_temp, d_temp2, eval, val1, val2;
	switch (i_type) {
		case 0:
			while (true) {
				val1 = Fn((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = sval * val1;
				val2 = Fn(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = d_temp - (sval+ev)*val2;
				d_temp2 = val1 - val2;
				d_temp = d_temp/d_temp2;
				eval = d_temp;
				if ( (Math.abs(eval) > ev) && (++iv < 100) )
					sval = eval;
				else
					break;
			}
			break;
		case 1:
			while (true) {
				val1 = Fi((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = sval * val1;
				val2 = Fi(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = d_temp - (sval+ev)*val2;
				d_temp2 = val1 - val2;
				d_temp = d_temp/d_temp2;
				eval = d_temp;
				if ((Math.abs(eval) > ev) && (++iv < 100))
					sval = eval;
				else
					break;
			}
			break;
		case 2:
			while (true) {
				val1 = Fpmt((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = sval * val1;
				val2 = Fpmt(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = d_temp - (sval+ev)*val2;
				d_temp2 = val1 - val2;
				d_temp = d_temp/d_temp2;
				eval = d_temp;
				if ((Math.abs(eval) > ev) && (++iv < 100))
					sval = eval;
				else
					break;
			}
			break;
		case 3:
			while (true) {
				val1 = Fpv((sval+ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = sval * val1;
				val2 = Fpv(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = d_temp - (sval+ev)*val2;
				d_temp2 = val1 - val2;
				d_temp = d_temp/d_temp2;
				eval = d_temp;
				if ((Math.abs(eval) > ev) && (++iv < 100))
					sval = eval;
				else
					break;
			}
			break;
		case 4:
			while (true) {
				val1 = Ffv((sval + ev), clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = sval * val1;
				val2 = Ffv(sval, clcs, clcn, clcfv, clci, clcpmt, LoanAmountValue);
				d_temp = d_temp - (sval + ev) * val2;
				d_temp2 = val1 - val2;
				d_temp = d_temp / d_temp2;
				eval = d_temp;
				if ((Math.abs(eval) > ev) && (++iv < 100))
					sval = eval;
				else
					break;
			}
			break;
	}
	return eval;
}

function checkNumber(objNode) {
	Num = objNode.value;
	decimals = 0;
	for (var i = 0; i < Num.length; i++) {
		ch = Num.charAt(i);
		if (ch == '.')
			decimals++;
		if (((ch < '0' || '9' < ch) && ch != '.') || decimals > 1) {
			window.alert('Please enter a valid positive number!');
			objNode.focus();
			return 0;
		}
	}
	return Num;
}

function checkResidualAmount() {
	var txtLoanAmt = $('txtLoanAmt');
	var txtArticlePrice = $('txtArticlePrice');
	var txtResidual = $('txtResidual');
	var txtValue = $('txtValue');

	var perc = (txtLoanAmt.value / txtArticlePrice.value) * 100;
	if (txtResidual.value > perc) {
		window.alert('Residual must be less then the LOAN AMOUNT, and will be set to zero.');
		txtResidual.value = 0;
		txtValue.value = 0;
	}
}

function numberFormat(intNum) {
	intNum = Math.round(intNum * 100) / 100 + 0.0001;
	strNum = intNum.toString();
	intNum = strNum.substring(0, strNum.lastIndexOf('.') + 3);
	return (intNum == 0.00) ? 0 : intNum;
}

function txtArticlePrice_onChange(form) {
	var txtArticlePrice = $('txtArticlePrice');
	var txtDeposit = $('txtDeposit');
	var txtTradeIn = $('txtTradeIn');
	var txtLoanAmt = $('txtLoanAmt');
	var txtValue = $('txtValue');
	var txtResidual = $('txtResidual');

	flag = 1;
	txtArticlePrice.value = checkNumber(txtArticlePrice);
	if (txtArticlePrice.value < -(-txtDeposit.value - txtTradeIn.value)) {
		txtDeposit.value = 0;
		txtTradeIn.value = 0;
	}
	txtArticlePrice.value = numberFormat(txtArticlePrice.value);
	txtLoanAmt.value = numberFormat(txtArticlePrice.value - txtDeposit.value - txtTradeIn.value);
	txtValue.value = numberFormat((txtArticlePrice.value * txtResidual.value) / 100);
	checkResidualAmount();
}

function txtDeposit_onChange() {
	var txtDeposit = $('txtDeposit');
	var txtArticlePrice = $('txtArticlePrice');
	var txtTradeIn = $('txtTradeIn');
	var txtLoanAmt = $('txtLoanAmt');

	flag = 1;
	txtDeposit.value = checkNumber(txtDeposit);
	if ((txtDeposit.value * 1) > (txtArticlePrice.value * 1)) {
		window.alert('Deposit cannot exceed Article Price');
		txtDeposit.value = txtArticlePrice.value;
		txtDeposit.focus();
	}
	if (txtDeposit.value > (txtArticlePrice.value - txtTradeIn.value)) {
		txtTradeIn.value = 0;
	}
	txtLoanAmt.value = numberFormat(txtArticlePrice.value - txtDeposit.value - txtTradeIn.value);
	txtDeposit.value = numberFormat(txtDeposit.value);
	checkResidualAmount();
}

function txtTradeIn_onChange() {
	var txtTradeIn = $('txtTradeIn');
	var txtDeposit = $('txtDeposit');
	var txtArticlePrice = $('txtArticlePrice');
	var txtLoanAmt = $('txtLoanAmt');

	flag = 1;
	txtTradeIn.value = checkNumber(txtTradeIn);
	if (txtDeposit.value > (txtArticlePrice.value - txtTradeIn.value)) {
		window.alert('Deposit + Trade-in cannot exceed Article Price');
		txtTradeIn.value = txtArticlePrice.value - txtDeposit.value;
		txtTradeIn.focus();
	}
	txtLoanAmt.value = numberFormat(txtArticlePrice.value - txtDeposit.value - txtTradeIn.value);
	txtTradeIn.value = numberFormat(txtTradeIn.value);
	checkResidualAmount();
}

function txtLoanAmt_onChange() {
	var txtLoanAmt = $('textLoanAmt');
	var txtDeposit = $('textDeposit');
	var txtTradeIn = $('textTradeIn');
	var txtArticlePrice = $('textArticlePrice');
	var txtResidual = $('textResidual');

	flag = 1;
	txtLoanAmt.value = checkNumber(txtLoanAmt);
	if (txtLoanAmt.value < -(-txtDeposit.value - txtTradeIn.value)) {
		txtDeposit.value = 0;
		txtTradeIn.value = 0;
	}
	txtArticlePrice.value = numberFormat(-(-txtLoanAmt.value - txtDeposit.value - txtTradeIn.value));
	txtValue.value = numberFormat((txtArticlePrice.value * txtResidual.value) / 100);
	txtLoanAmt.value = numberFormat(txtLoanAmt.value);
	checkResidualAmount();
}

function txtInterest_onChange() {
	var txtInterest = $('txtInterest');

	flag = 1;
	txtInterest.value = checkNumber(txtInterest);
	if (txtInterest.value > 100) {
		window.alert('Percentage was greater than 100. Automatic conversion procedure initiated.');
		txtInterest.focus();
	}
	while (txtInterest.value > 100)
		txtInterest.value /= 10;
}

function txtResidual_onChange() {
	var txtResidual = $('txtResidual');
	var txtLoanAmt = $('txtLoanAmt');
	var txtArticlePrice = $('txtArticlePrice');
	var txtValue = $('txtValue');

	flag = 1;
	txtResidual.value = checkNumber(txtResidual);
	if (txtResidual.value > 100) {
		window.alert('Percentage was greater than 100. Automatic conversion procedure initiated.');
		txtResidual.focus();
	}
	while (txtResidual.value > 100)
		txtResidual.value /= 10;
	perc = (txtLoanAmt.value / txtArticlePrice.value) * 100;
	if ((txtResidual.value * 1) > (perc * 1)) {
		window.alert('Residual must be less then the LOAN AMOUNT.');
		txtResidual.value = 0;
		txtResidual.focus();
	}
	txtValue.value = numberFormat((txtArticlePrice.value * txtResidual.value) / 100);
}

function txtValue_onChange() {
	var txtValue = $('txtValue');
	var txtArticlePrice = $('txtArticlePrice');
	var txtLoanAmt = $('txtLoanAmt');

	flag = 1;
	txtValue.value = checkNumber(txtValue);
	if ((txtValue.value * 1) > (txtArticlePrice.value * 1)) {
		window.alert('Residual Value cannot exceed the Article Price.');
		txtValue.value = 0;
		txtValue.focus();
	}
	if ((txtValue.value * 1) > (txtLoanAmt.value * 1)) {
		window.alert('Residual Value cannot exceed the LOAN AMOUNT.');
		txtValue.value = 0;
		txtValue.focus();
	}
	txtResidual.value = numberFormat((txtValue.value / txtArticlePrice.value) * 100);
}

function txtTerms_onChange() {
	var txtTerms = $('txtTerms');

	flag = 1;
	txtTerms.value = checkNumber(txtTerms);
	if (txtTerms.value > 60) {
		window.alert('Term cannot be greater than 60 months.');
		txtTerms.value = 60;
	}
	strTerms = txtTerms.value.toString();
	if (strTerms.lastIndexOf('.') > -1) {
		window.alert('The Term can only be full months, ie: 30 months NOT 30,5 months.');
		txtTerms.value = 0;
		txtTerms.focus();
		txtTerms.select();
		return;
	}
	return;
}

function selPmtFreq_onChange() {
	flag = 1;
}

function txtExtraPmt_onChange() {
	var txtExtraPmt = $('txtExtraPmt');
	var txtLoanAmt = $('txtLoanAmt');

	txtExtraPmt.value = checkNumber(txtExtraPmt);
	if ((txtExtraPmt.value * 1) > (txtLoanAmt.value * 1)) {
		window.alert('The Extra Payment cannot exceed the Loan Amount');
		txtExtraPmt.value = 0;
	}
}

function calculatePayment() {
	var txtExtraPmt = $('txtExtraPmt');
	var txtArticlePrice = $('txtArticlePrice');
	var txtInterest = $('txtInterest');
	var selPmtFreq = $('selPmtFreq');
	var txtTerms = $('txtTerms');
	var txtValue = $('txtValue');
	var txtLoanAmt = $('txtLoanAmt');
	var txtPmt = $('txtPmt');

	if (flag == 1) {
		txtExtraPmt.value = 0;
		var value, d_temp, d_temp2;
		ev = Math.pow(10, (-6));
		clcpv = tmppv = txtArticlePrice.value;
		clcz = ((txtInterest.value / 100) / 360) * 30;
		clcpv = clcpv + (clcpv * clcz);
		payFreqValue = 0;
		if (selPmtFreq.selectedIndex == 0) payFreqValue = 1;
		if (selPmtFreq.selectedIndex == 1) payFreqValue = 2;
		if (selPmtFreq.selectedIndex == 2) payFreqValue = 4;
		if (selPmtFreq.selectedIndex == 3) payFreqValue = 12;
		clci = txtInterest.value / 100 / payFreqValue;
		clcn = txtTerms.value * payFreqValue / 12;
		clcnstr = '' + clcn;
		if (clcnstr.lastIndexOf('.') > -1) {
			alert("The 'Term / Payment Frequency' does not relate to an exact number of payments,\r eg: Semi-annual Payments must have a Term which is a multiple of 6 months.");
			txtTerms.focus();
			txtTerms.select();
			return;
		}
		clcfv = (txtValue.value) * -1;
		clcs = 0;
		clcpmt = 0.0;
		sval = 1.0;
		iv = 0;
		Pmt = 2;
		if (clcn == 0) {
			temp = 0;
			temp3 = temp;
		} else if (clci == 0) {
			temp = txtLoanAmt.value / clcn;
			temp3 = temp;
		} else {
			temp = Math.abs(Calculate(Pmt, sval, ev, iv, clcs, clcn, clcfv, clci, clcpmt, txtLoanAmt.value));
			temp3 = temp;
		}
		txtPmt.value = numberFormat(temp);
		flag = 0;
	}
	if (txtExtraPmt.value > 0) {
		if (txtInterest.value == 0) {
			clcpmt = (txtPmt.value * 1) + (txtExtraPmt.value * 1);
			value = Math.round(txtLoanAmt.value / clcpmt);
			value = value * (12 / payFreqValue);
		} else {
			clcpmt = -(temp3 * 1) - (txtExtraPmt.value * 1);
			clcn = 0.0;
			sval = 1.0;
			iv = 0;
			value = Calculate(0, sval, ev, iv, clcs, clcn, clcfv, clci, clcpmt, txtLoanAmt.value);
		}
		txtTerms.value = Math.abs(Math.round(value));
	}
	return false;
}

var flag = 0;
var form = '';

function window_onLoad() {
	Event.observe($('txtArticlePrice'), 'change', txtArticlePrice_onChange);
	Event.observe($('txtDeposit'), 'change', txtDeposit_onChange);
	Event.observe($('txtTradeIn'), 'change', txtTradeIn_onChange);
	Event.observe($('txtLoanAmt'), 'change', txtLoanAmt_onChange);
	Event.observe($('txtInterest'), 'change', txtInterest_onChange);
	Event.observe($('txtResidual'), 'change', txtResidual_onChange);
	Event.observe($('txtValue'), 'change', txtValue_onChange);
	Event.observe($('txtTerms'), 'change', txtTerms_onChange);
	Event.observe($('selPmtFreq'), 'change', selPmtFreq_onChange);
	Event.observe($('txtPmt'), 'change', calculatePayment);
	Event.observe($('txtExtraPmt'), 'change', txtExtraPmt_onChange);
	var valid = new Validation('form_quote', {useTitles:true,immediate:true});

}

Event.observe(window, 'load', window_onLoad);
