function round(tmpNum)
{
	var numStr = "";
	var decimalNum = 2;
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum));
	numStr = new String(tmpNum);
	numStr = numStr.substring(0,numStr.length-2) + "." + numStr.substring(numStr.length-2);
	return numStr;
}
/*	This is the main function which gets the information
	from the form and calculates the monthly payment */

function updateLoan() {
	document.mortgage.lnAmt.value = (document.mortgage.hValue.value - document.mortgage.dnPmt.value);
}

function dnUpdate(oEl) {
	if(oEl.tagName=='SELECT') {
		// changed pct
		if(oEl.options[oEl.selectedIndex].value!='') {
			document.mortgage.dnPmt.value = Math.round(document.mortgage.hValue.value * (oEl.options[oEl.selectedIndex].value/100))
		}
	} else {
		if(oEl.value!=0) {
			var pct = round(oEl.value / document.mortgage.hValue.value * 100);
			var bfound = false;
			for(i=1;i<document.mortgage.dnPct.options.length;i++) {
				var oVal = document.mortgage.dnPct.options[i].value;
				if(pct == oVal) {
					document.mortgage.dnPct.selectedIndex == i;
					document.mortgage.dnPct.options[i].selected=true;
					bfound = true;
				}
			}
			if(!bfound) {
				document.mortgage.dnPct.selectedIndex=0;
			}
		}else{
			document.mortgage.dnPct.selectedIndex=0;
		}
	}
	updateLoan();
}

function calculate()
{
	var base=1;
	var monthlyrate;
	var loanvalue;
	var interest;
	var downpayment;
	var housevalue;
	var years;
	var monthlypayment;
	var totalpayment;
	var tax;
	var insurance;
	var pmi;

	interest = document.mortgage.inRate.value;
	housevalue = document.mortgage.hValue.value;
	downpayment = document.mortgage.dnPmt.value;
	years = document.mortgage.lnTerm.value;
	tax = document.mortgage.mTax.value;
	insurance = document.mortgage.mIns.value;
	document.mortgage.pmi.value=0.00;
	
	/*Check if the values are all entered and OK */
	
	if (checkvalues()){ 	
	monthlyrate =  interest / 1200;
	var mbase=1 + monthlyrate;
	loanvalue = housevalue - downpayment;	
		
	for (var i=0;i< (years*12);i++)
	{
		base=base*mbase;
	}
	
	/*Check if the downpayment is less than 20%*/
	
	if (downpayment/housevalue < 0.15) {
		//alert ("As your down payment is less than 20% of the total house value, Please be informed that you may have to pay PMI");
		if(downpayment/housevalue < 0.05) {
			pmi = round((0.0078 * loanvalue)/12);
		} else if(downpayment/housevalue < 0.1) {
			pmi = round((0.0052 * loanvalue)/12);
		} else if(downpayment/housevalue < 0.15) {
			pmi = round((0.0032 * loanvalue)/12);
		}
	}
	else {
		pmi=0;
	}
	document.mortgage.pmi.value = (pmi);
	
	fullPayment = round(parseFloat((loanvalue * monthlyrate) /(1-(1/base))));	
	
	monthlypayment = parseFloat(round((loanvalue * (interest / 100) * 30) / 360));
	
	monthlySavings = round(fullPayment-monthlypayment);
	
	termSavings = round(monthlySavings * 60);
	
	totalpayment =  round(monthlypayment + (1*tax) + (1*insurance) + (1*pmi));

	document.mortgage.piVal.readOnly = true;
	document.mortgage.mPmt.readOnly = true;
	document.mortgage.piVal.value = monthlypayment;
	document.mortgage.mPmt.value = totalpayment;
	document.mortgage.monthlySavings.value = monthlySavings;
	document.mortgage.termSavings.value = termSavings;
		if (document.mortgage.mInc) {
			document.mortgage.mInc.value = Math.round(totalpayment * 12 * 3.448275);
		}
	}
	document.mortgage.mPmt.focus();
}
	
/*	This function does the validation of the inputs
	in the form */
	
function checkvalues()
{
	var interest;
	var downpayment;
	var housevalue;
	var years;
	var tax;
	var insurance;
	
	interest = document.mortgage.inRate.value;
	years = document.mortgage.lnTerm.value;
	tax = document.mortgage.mTax.value;
	insurance = document.mortgage.mIns.value;
	
	if (interest <=0 || isNaN(interest)==true)
	{
		document.mortgage.mPmt.value = "";
		document.mortgage.piVal.value = "";
		alert("Please enter the interest value as an integer greater than 0");	
		document.mortgage.inRate.focus();		
		return (false);
	}
	if (years <=0 || years > 30 || isNaN(years)==true)
	{
		document.mortgage.mPmt.value = "";
		document.mortgage.piVal.value = "";
		alert("Please enter the number of years as an integer value greater than 0 or less than 30");	
		document.mortgage.lnTerm.focus();		
		return (false);
	}
	if (tax <0 || isNaN(tax)==true)
	{
		document.mortgage.mPmt.value = "";
		document.mortgage.piVal.value = "";
		//alert("Please enter the Tax Amount as an integer value greater than 0");	
		document.mortgage.mTax.focus();
		return (false);
	}
	if (insurance <0 || isNaN(insurance)==true)
	{
		document.mortgage.mPmt.value = "";
		document.mortgage.piVal.value = "";
		alert("Please enter Insurance Amount as an integer greater than 0");	
		document.mortgage.mIns.focus();		
		return (false);
	}
	else
	{
		return (true);
	}
}