/* based on functions originally encoded by Michael D. Lewis  simulating a TI BAII calculator
tested and found to reporduce sotheby's lehman tables */

function disp10y (x) {
	curInt=x.interest.options[x.interest.selectedIndex].value;
	curInt=curInt/12/100;
	lAmt=-x.pv.options[x.pv.selectedIndex].value;
	x.mo10yIonly.value=formatCurrencyCents(curInt*lAmt);
	}

function num_format(x) { // format numbers with two digits
	sgn = (x < 0);
	x = Math.abs(x);
	x = Math.floor((x * 100) + .5);
	j = 3;
	y = "";
	while(((j--) > 0) || (x > 0)) {
		y = (x % 10) + y;
		x = Math.floor(x / 10);
		if(j == 1) {
			y = "." + y;
		}
	}
	if(sgn) {
		y = "-" + y;
	}
	return(y);
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
// return (((sign)?'':'-') + '$' + num + '.' + cents);
return (((sign)?'':'-') + '$' + num ); // no cents
}
function formatCurrencyCents(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function num_formatNoD(x) { // format numbers without decimal
	sgn = (x < 0);
	x = Math.abs(x);
	x = Math.floor((x * 100) + .5);
	j = 1;
	y = "";
	while(((j--) > 0) || (x > 0)) {
		y = (x % 10) + y;
		x = Math.floor(x / 10);
		if(j == 1) {
			y = "." + y;
		}
	}
	if(sgn) {
		y = "-" + y;
	}
	return(y);
}
function num_format12(x) { // format numbers to twelve decimals
	sgn = (x < 0);
	x = Math.abs(x);
	x = Math.floor((x * 1000000000000) + .5);
	j = 13;
	y = "";
	while(((j--) > 0) || (x > 0)) {
		y = (x % 10) + y;
		x = Math.floor(x / 10);
		if(j == 1) {
			y = "." + y;
		}
	}
	if(sgn) {
		y = "-" + y;
	}
	return(y);
}

function years(x) {
	
	n = parseFloat(x.n.value);
	p = parseFloat(x.p.value);
	n = n*p;
	x.n.value = num_format(n);
}

function calcMonthly (x) {
	curInt=x.interest.options[x.interest.selectedIndex].value;
	x.r.value = parseFloat(curInt);
	x.n.value=360;comp(x,'pmt');
	x.mo30y.value=formatCurrencyCents(x.pmt.value);
	x.n.value=240;comp(x,'pmt');
	x.mo20y.value=formatCurrencyCents(x.pmt.value);
	x.n.value=180;comp(x,'pmt');
	x.mo15y.value=formatCurrencyCents(x.pmt.value);
	}

function calcMoPay (x) { // call it with currently selected value
	curVal=x.income.options[x.income.selectedIndex].value;
	incomeAnnual = parseFloat(curVal);     //annual income
	percentDevote = parseFloat(x.percentDevote.value);     //percentage of income for mortgage
	MoPay = incomeAnnual * percentDevote * 0.01 /12;
	x.MoPay.value = num_format(MoPay);
	x.pmt.value = num_format(MoPay);
	}
function calcPVafford(x) {
	curInt=x.interest.options[x.interest.selectedIndex].value;
	x.r.value = parseFloat(curInt);
	comp(x,'pv');	x.afford.value=formatCurrency(x.pv.value);
	}
function calcPVatRates(x) {
	x.r.value = parseFloat(5.25);comp(x,'pv');	x.i525.value=formatCurrency(x.pv.value);
	x.r.value = parseFloat(5.50);comp(x,'pv');	x.i550.value=formatCurrency(x.pv.value);
	x.r.value = parseFloat(5.75);comp(x,'pv');	x.i575.value=formatCurrency(x.pv.value);
	x.r.value = parseFloat(6.00);comp(x,'pv');	x.i600.value=formatCurrency(x.pv.value);
	x.r.value = parseFloat(6.25);comp(x,'pv');	x.i625.value=formatCurrency(x.pv.value);
//	x.r.value = parseFloat(6.50);comp(x,'pv');	x.i650.value=x.pv.value;
	x.r.value = parseFloat(6.75);comp(x,'pv');	x.i675.value=formatCurrency(x.pv.value);
	x.r.value = parseFloat(7.00);comp(x,'pv');	x.i700.value=formatCurrency(x.pv.value);
//	x.r.value = parseFloat(7.25);comp(x,'pv');	x.i725.value=x.pv.value;
	x.r.value = parseFloat(7.5);comp(x,'pv');	x.i750.value=formatCurrency(x.pv.value);
	}
	
function comp(x,v) { 

	pv = parseFloat(x.pv.value);       //Present Value Input
	fv = parseFloat(x.fv.value);       //Future Value Input
	n = parseFloat(x.n.value);         //Number of Compounding periods
	pmt = parseFloat(x.pmt.value);     //Payment Input
	c = parseFloat(x.c.value);         //Compounding periods per year 
	p = parseFloat(x.p.value);	   //Periods per Year	
	r = parseFloat(x.r.value);	   //Interest per Year
	k = parseInt(x.k.value);	           //k=0 for end of period 	
 	
	var z = (r * .01)/c;
	var y = c/p;
	var i = (Math.exp(y*Math.log(z+1))-1)
	if ((k==0)||(k==1)) {
		var g=1+i*k}
	else {  
		var g=1}	

		// test and compute all cases
		
		if (v == 'pv') {
			if ((fv==0)&&(pmt==0))
				{alert("One or more of the required values is missing!")
				return}			
			if (i == 0){
				pv = -(fv + pmt * n);
				x.pv.value = num_format(pv)}
			else {
				q = Math.pow(1+i,-n);
				t = (pmt*g)/i;		
				pv = ( t-fv)*q-t;
				pv = -1* pv;
 				x.pv.value = num_format(pv)}
		}
		
		if (v == 'fv') {
			if ((pv==0)&&(pmt==0))
				{alert("One or more of the required values is missing!")
				return}		
			if (i == 0) {
				fv = -(pv + pmt * n);
				x.fv.value = num_format(fv)}
			else {
				q = Math.pow(1+i,n);
				t = (pmt*g)/i;
				fv = t-q*(pv+t);
				x.fv.value = num_format(fv)}
		}
		
		if (v == 'n') {
			if ((fv==0)&&(pv==0))
				{alert("One or more of the required values is missing!")
				return}					
			if (i == 0) {
				n = -(pv + fv)/ pmt;
				x.n.value = num_format(n)}
			else {
				n = (Math.log((pmt*g-fv*i)/(pmt*g+pv*i)))/ Math.log(1 + i)
				x.n.value = num_format(n)}
		}
		
		if (v == 'pmt') {
			if ((fv==0)&&(pv==0))
				{alert("One or more of the required values is missing!")
				return}		
			if(i == 0){
                        	q = Math.pow(1+i,n);
				pmt = -(pv + fv)/n;
				x.pmt.value = num_format(pmt)}
			else {
				q = Math.pow(1+i,n);
				pmt = -i/g*(pv+((pv+fv)/(q-1)))
				x.pmt.value = num_format(pmt)}

		}

		if(v == 'r') {
			if ((fv==0)&&(pv==0))
				{alert("One or more of the required values is missing!")
				return}		
			if(pmt ==0) {
				i=Math.pow((-fv/pv),(1/n))-1}	
			else{
				r=0;
				i=.00001;
				while ((pv+pmt*(1+i*k)*((1-Math.pow(1+i,-n))/i)+fv*Math.pow(1+i,-n))>0) {i=i+.00001}}
				z = i;
				y = p/c;
				r = 100*c*(Math.exp(y*Math.log(z+1))-1);
				x.r.value = num_format(r);
		}


}


function amort(x) {

	pv = parseFloat(x.pv.value);        //Present Value Input
	fv = parseFloat(x.fv.value);        //Future Value Input
	n = parseFloat(x.n.value);          //Number of Compounding periods
	pmt= parseFloat(x.pmt.value);       //Payment Input
	c = parseFloat(x.c.value);          //Compounding periods per year 
	p = parseFloat(x.p.value);	    //Periods per Year	
	r = parseFloat(x.r.value);	    //Interest per Year
	k = parseInt(x.k.value);	    //k=0 for end k=1 for begin 	
 	p1 = parseInt(x.p1.value);	    //	
	p2 = parseInt(x.p2.value);
	bal = parseFloat(x.bal.value);
	prin = parseFloat(x.prin.value);
	nt = parseFloat(x.nt.value);

	var z = (r * .01)/c;
	var y = c/p;
	var i = (Math.exp(y*Math.log(z+1))-1)
	if ((k==0)||(k==1)) {
		var g=1+i*k}
	else {  
		var g=1}
	var l = n-(p2-p1+1)
	var q = (1-Math.pow(1+i,-l))/i
	if (pmt == 0) {alert("You must compute the payment first!")}
	else {
	bal = -pmt*g*q;
	bal = num_format12(bal)
	x.bal.value = num_format(bal)
	nt = -pmt*(p2-p1+1)-(pv-bal);
	nt = num_format12(nt)
	x.nt.value = num_format(nt)
	prin =pv-bal;
	prin = num_format12(prin)
	x.prin.value = num_format(prin)
	}
}

