function calculateField(form) {

	if (validateForm(form)) {

		if (form.numpayments.value == null || form.numpayments.value.length == 0)

			calculateNumpayments(form);

		if (form.monpayments.value == null || form.monpayments.value.length == 0)

			calculatePayment(form);

		if (form.principal.value == null || form.principal.value.length == 0)

			calculatePrincipal(form);

		if (form.interest.value == null || form.interest.value.length == 0)

			calculateInterest(form);

		return;

	}

}



/////////////////////// FORM VALIDATION ///////////////////////////////////



// Check for an empty field

function isFieldEmpty(theField) {

	if(theField.value == "" || theField.value == null)

		return true;

	else

		return false;

}



// Check if the item is a positive number

function isPosNum(item) {

	oneDecimal = false;	// only allow one decimal point

	itemStr = item.toString(); 

    for (var i = 0; i < itemStr.length; i++) {

		var oneChar = itemStr.charAt(i);

		if (oneChar == "." && !oneDecimal) {

			oneDecimal = true;

			continue;	// skip ahead once first decimal is found

		}

        if (oneChar < "0" || oneChar > "9") { // any additional decimal points

			return false;					  // will be caught here

			

		}

	}

	return true;

}



// Make sure 3 out of 4 fields have a value

function validateForm(form) {

var numEmpty = 0;

fieldOK = true;

	if (isFieldEmpty(form.numpayments))

		numEmpty++;

	else if (isPosNum(form.numpayments.value) == false) 

		 {

			alert("Please enter a positive number for the Number of Payments.");

			fieldOK = false;

			return false;

		 }

	if (isFieldEmpty(form.monpayments))

			numEmpty++; 

	else if (isPosNum(form.monpayments.value) == false) 

		 {

			alert("Please enter a positive number for the Monthly Payment.");

			fieldOK = false;

			return false;

		 }

	if (isFieldEmpty(form.principal))

			numEmpty++;

	else if (isPosNum(form.principal.value) == false) 

		 {

			alert("Please enter a positive number for the Principal.");

			fieldOK = false;

			return false;

		 }

	if (isFieldEmpty(form.interest))

			numEmpty++;

	else if (isPosNum(form.interest.value) == false) 

		 {

			alert("Please enter a positive number for the Interest.");

			fieldOK = false;

			return false;

		 }

		 	

	if (numEmpty == 1)

		return true;

	else if (numEmpty == 0) {

			alert("Please leave 1 field blank.");

			return false;

		 }

	else if (numEmpty == 2) {

			numEmpty--;

			alert("Please fill in " + numEmpty + " more field.");

			return false;

		}

	else if (numEmpty > 1) {

			numEmpty--;

			alert("Please fill in " + numEmpty + " more fields.");

			return false;

		 }

		

}



///////////////////////////////////////////////////////////////////////////////

function round2(x) {

	return Math.round(x*100)/100;

}

	

function clearForm(form) {

	form.numpayments.value = "";

	form.interest.value = "";

	form.principal.value = "";

	form.monpayments.value = "";

}



function calculatePayment(form) {

/* Let P = Principal Amount

	   n = Number of payments/months

	   r = Interest Rate

	  mp = Monthly payment

	  

   Monthly Payment Formula:

   		

		mp = P * r * (1+r)^n

			-----------------

			  (1+r)^n - 1

			  

*/



/* First obtain the interest rate and then compute the

   compound rate for the length of the loan */



	var r = getInterest(form);

	var cr = calculateCPI(form,r);

		

	// Calculate Monthly Payment:

	form.monpayments.value = round2((form.principal.value * cr * r) / (cr - 1));

	

}



function calculatePrincipal(form) {

	var r = getInterest(form);

	var cr = calculateCPI(form,r);

	form.principal.value = Math.round(((cr - 1) * form.monpayments.value) / (cr * r));



}



function calculateP2(form,Int,Rpt) {

	Int = Int/1200;

	var cr = calculateCPI(form,Int);

	var amt = ((cr - 1) * Rpt) / (cr * Int);

	return amt;



}



function calculateNumpayments(form) {

	var r = getInterest(form);

	var n = (Math.log(form.monpayments.value) - Math.log((form.monpayments.value - (form.principal.value * r)))) / Math.log(1 + r);



	n = Math.round(n);

	return form.numpayments.value = n;

	

}





function getInterest(form) {

var r = form.interest.value;

	if (r < 1.0)

		r = r/12;

	else {

	// Take r/100 for decimal value then r/12 for monthly rate

		r = r/100.0;

		form.interest.value = r;

		r = r/12;

		

	return r;

	}

}



function calculateCPI(form,rate) {

	// Compute Accumulation Factor

	var cr = 1;

	for (i=0; i<form.numpayments.value; i++)

		cr = cr * (1+rate);

	return cr;

}



function calculateInterest(form) {

	

	var  IntMin = 0,

         IntMax = 100,

         Int, 

         DummyAmt;

		 

	var Rpt = form.monpayments.value;

	var Amt = form.principal.value;



		do {

                        

             Int = (IntMin + IntMax)/2;

             DummyAmt = calculateP2(form,Int,Rpt);

             if (DummyAmt < Amt) {

                 IntMax = Int;

             }

             else {

                 IntMin = Int;

             }

                

    } while (Math.abs(DummyAmt - Amt) > 0.1);



	            Int += 0.005;

                Int *= 100;

                Int = Math.floor(Int);

                Int /= 100;

				return form.interest.value = Int;

              

        }	 
