// JavaScript Document
// My first JavaScript. 7/22/96 Paul Lutus lutusp@arachnoid.com
function num_format(x) { // format numbers with two digits
	sgn = (x < 0);
	x = Math.abs(x);
	x = Math.floor((x * 100) + .5);
	i = 3;
	y = "";
	while(((i--) > 0) || (x > 0)) {
		y = (x % 10) + y;
		x = Math.floor(x / 10);
		if(i == 1) {
			y = "." + y;
		}
	}
	if(sgn) {
		y = "-" + y;
	}
	return(y);
}
function comp(x,v) { // general entry point for all cases
	
	// convert all entry fields into variables
	
	pv = parseFloat(x.pv.value);
	fv = parseFloat(x.fv.value);
	np = parseFloat(x.np.value*12);
	pmt = parseFloat(-x.pmt.value);
	if(x.ir.value == "") {
		alert("You must enter an interest rate (ir).");
	}
	else {
		ir = eval((x.ir.value/12)) / 100;
		
		// test and compute all cases
		
		if (v == 'pv') {
			q = Math.pow(1 + ir,-np);
			pv = (q * fv * ir + pmt - q * pmt)/ir;
			x.pv.value = num_format(pv);
		}
		
		if (v == 'fv') {
			q = Math.pow(1+ir,np);
			fv = pv * q - pmt * ((q - 1)/ir);
			x.fv.value = num_format(fv);
		}
		
		if (v == 'np') {
			np = (Math.log(fv * ir - pmt) - Math.log(pv * ir - pmt))/ Math.log(1 + ir);
			if(np == 0) {
				alert("Can't compute Number of Periods for the present values.");
			}
			else {
				x.np.value = num_format(np);
			}
		}
		
		if (v == 'pmt') {
			q = Math.pow(1+ir,np);
			pmt = ((-fv + pv * q)/(q - 1)) * ir;
			x.pmt.value = num_format(pmt);
		}
		
		if(v == 'ir') { // I am so lazy!
			alert("Sorry! Can't calculate interest rate.\nYou must enter this value.");
		}
	}
} // function comp
// end hide -->