function round(number)
{
	return Math.round(100*number)/100;
}
/*	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 assoc;
	var downpayment;
	var housevalue;
	var years;
	var monthlypayment;
	var totalpayment;
	var tax;
	var insurance;

	interest = document.mortgage.inRate.value;
	housevalue = document.mortgage.hValue.value;
	downpayment = document.mortgage.dnPmt.value;
	//downpayment = document.mortgage.hValuednPmt.value;
	years = document.mortgage.lnTerm.value;
	assoc = document.mortgage.mAsn.value;
	tax = document.mortgage.mTax.value;
	insurance = document.mortgage.mIns.value;
	document.mortgage.pmi.value=0;
	
	/*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;
	}
	
	if(assoc<1) {
		assoc=0;
	}
	
	/*Check if the downpayment is less than 20%*/
	
	if (downpayment/housevalue < 0.20) {
		//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) {
			document.mortgage.pmi.value = round ((0.0096 * loanvalue)/12);
		} else if(downpayment/housevalue < 0.1) {
			document.mortgage.pmi.value = round ((0.0078 * loanvalue)/12);
		} else if(downpayment/housevalue < 0.15) {
			document.mortgage.pmi.value = round ((0.0052 * loanvalue)/12);
		} else if(downpayment/housevalue < 0.20) {
			document.mortgage.pmi.value = round ((0.0032 * loanvalue)/12);
		}
	}
	else {
		document.mortgage.pmi.value=0;
	}
	
	monthlypayment = round(((loanvalue * monthlyrate) /(1-(1/base))));	
	totalpayment =  round(((loanvalue * monthlyrate) /(1-(1/base))) + (1*tax) + (1*insurance) + (1*assoc) + (1*document.mortgage.pmi.value));
	document.mortgage.piVal.readOnly = true;
	document.mortgage.mPmt.readOnly = true;
	document.mortgage.piVal.value = monthlypayment;
	document.mortgage.mPmt.value = totalpayment;
	}
}
	
/*	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);
	}
}
