if(typeof com != 'object') com = {};
if(typeof com.ivivos != 'object') com.ivivos = {};
if(typeof com.ivivos.AmortizationCalculator != 'object') {
	com.ivivos.AmortizationCalculator = new function () {
		this.UNSOLVABLE_ERROR = {}
		this.solveFor_x = function (P, D, i, N) {return (P-D)*(((1-(1+i))/(1-Math.pow(1+i,N)))+i);}
		this.solveFor_N = function (P, D, i, x) {return Math.log(((1-(1+i))/((x/(P-D))-i)-1)/-1)/Math.log(1+i);}
		this.solveFor_D = function (P, i, N, x) {return P - (x / (((1-(1+i))/(1-Math.pow(1+i,N)))+i));}
		this.solveFor_P = function (D, i, N, x) {return (x / (((1-(1+i))/(1-Math.pow(1+i,N)))+i)) + D;}
		this.solve = function(frm, P, D, i, N, x, f) {
			P = parseFloat(P); D = parseFloat(D); i = parseFloat(i); N = parseFloat(N); x = parseFloat(x);
			if(isNaN(P) && !( isNaN(D) || isNaN(i) || isNaN(N) || isNaN(x) )) {
				P = this.solveFor_P(D, i, N, x);
				frm.P.value = Math.round(P * 100) / 100;
			} else if(isNaN(D) && !( isNaN(P) || isNaN(i) || isNaN(N) || isNaN(x) )) {
				D = this.solveFor_D(P, i, N, x);
				frm.D.value = Math.round(D * 100) / 100;
			} else if(isNaN(N) && !( isNaN(D) || isNaN(i) || isNaN(P) || isNaN(x) )) N = this.solveFor_N(P, D, i, x);
			else if(isNaN(x) && !( isNaN(D) || isNaN(i) || isNaN(N) || isNaN(P) )) {
				x = this.solveFor_x(P, D, i, N);
				frm.x.value = Math.round(x * 100) / 100;
			} else {
				alert('Please supply all fields execept the one you want to determin.');
				return;
			}
			if(frm.showAll.value == 'true') {
				var win = window.open(null, null, 'height=200,width=400');
				win.document.writeln('<html><head><SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">onblur = function () {window.close();}</SCRIPT><style type="text/css">*, TABLE TR TD, TABLE TH {font-size: 10pt; font-family: sans-serif; }</style></head><body>');
				win.document.writeln('<table class="summary"><tr><th align="right">Principal:</th><td align="right">$' + String(Math.round(P * 100) / 100) + '</td><th align="right">Annual Intrest Rate:</th><td align="right">' + String(i * f * 100) + '%</td></tr>');
				win.document.writeln('<tr><th align="right">Down Payment:</th><td align="right">$' + String(Math.round(D * 100) / 100) + '</td><th align="right">Periodic Intrest Rate:</th><td align="right">' + String(Math.round(i * 1000000) / 10000) + '%</td></tr>');
				win.document.writeln('<tr><th align="right">Payment Amount:</th><td align="right">$' + String(Math.round(x * 100) / 100) + '</td><th align="right">Number of Payments:</th><td align="right">' + String(N) + '</td></tr>');
				win.document.writeln('<tr><th align="right">Total Repaid:</th><td align="right">$' + String(Math.round((Math.round(x * 100) / 100) * N * 100) / 100) + '</td><th align="right">Total Intrest:</th><td align="right">$' + String(Math.round(((Math.round(x * 100) / 100) * N - P) * 100 ) / 100) + '</td></tr>');
				win.document.writeln('</table></body></html>');
				win.document.close();
			}
		}
	}
}
function AmortSolve(frm) {
	var P = parseFloat( String(frm.P.value).replace(/[^\.0-9]/ig, '') );
	var D = parseFloat( String(frm.D.value).replace(/[^\.0-9]/ig, '') );
	var x = parseFloat( String(frm.x.value).replace(/[^\.0-9]/ig, '') );
	var I = parseFloat( String(frm.I.value).replace(/[^\.0-9]/ig, '') );
	var f = parseFloat( String(frm.f.value).replace(/[^\.0-9]/ig, '') );
	var Y = parseFloat( String(frm.Y.value).replace(/[^\.0-9]/ig, '') );
	if(isNaN(I) || isNaN (f) || isNaN(Y)) {
		alert('Annual Intrest Rate, Payments/Year, and Years of Loan cannot be solved for.');
		return;
	} else if (
		isNaN(P) && (isNaN(D) || isNaN(x))
		|| isNaN(D) && (isNaN(P) || isNaN(x))
		|| isNaN(x) && (isNaN(P) || isNaN(D))
	) {
		alert('Only one field can be solved for at a time.');
		return;
	}
	var N = Y * f;
	var i = (I / 100) / f;
	com.ivivos.AmortizationCalculator.solve(frm, P, D, i, N, x, f);
}

