function checkForm() {

	var housePrice;
	var IR;
	var years;
	var monthPay;
	var month;
	var timetype;
	var interestRate;

	//validate the incoming variables
	//Principle Loan Balance Checks
	if (document.calcform.price.value == "") {
		alert("Principal Loan Balance is required");
		return;
	}
	if (NumericCheck(document.calcform.price.value)=="-1") {
		alert("Principal Loan Balance is not numeric");
		return;
	}
	if (document.calcform.interest.value == "") {
		alert("Annual Interest Rate is required");
		return;
	}
	if (document.calcform.time.value == "") {
		alert("Amortization Length is required");
		return;
	}
	if (NumericCheck(document.calcform.monthprepmt.value)=="-1") {
		alert("Monthly Principal Prepayment Amount is not numeric");
		return;
	}
	if (NumericCheck(document.calcform.annualprepayt.value)=="-1") {
		alert("Annual Principal Prepayment Amount is not numeric");
		return;
	}
	if (NumericCheck(document.calcform.oneprepay.value)=="-1") {
		alert("One-Time Principal Prepayment Amount is not numeric");
		return;
	}
	if (NumericCheck(document.calcform.pymon.value)=="-1") {
		alert("One-Time Principal Prepayment Month is not numeric");
		return;
	}
	if (document.calcform.pymon.value != "" && document.calcform.oneprepay.value == "") {
		alert("One-Time Principal Prepayment Month is required, since One-Time Principal Prepayment Amount has been entered");
		return;
	}
	if (document.calcform.pymon.value == "" && document.calcform.oneprepay.value !== "") {
		alert("One-Time Principal Prepayment Amount is required, since One-Time Principal Prepayment Month has been entered");
		return;
	}

	housePrice = parseFloat(document.calcform.price.value);
	IR = parseFloat(document.calcform.interest.value);
	years = parseFloat(document.calcform.time.value);
	timetype = document.calcform.duration[document.calcform.duration.selectedIndex].value;
	month = years;
	interestRate = IR/1200;

	if (timetype == "Yrs") {
		month = years * 12;
	}

	negmonth =- 1 * month;
	var bottom = 1 - (Math.pow(interestRate+1,negmonth));
	var top = interestRate;
	var mid = top / bottom;
	monthPay = (housePrice * mid);
	document.calcform.monthPay.value = eval(monthPay);
	document.calcform.month.value = month;
}

function NumericCheck(cnumber) {
	var oneChar = "";
	var decimalFlag = 0;
	checkZero = 0;
	for (var i = 0; i < cnumber.length; i++) {
		oneChar = cnumber.charAt(i);
		if ((oneChar < "0" || oneChar > "9") && (oneChar != ",") && (oneChar != ".") && (oneChar != "$") ) {
			return "-1";
		}
		if ((oneChar == "$") || (oneChar == ",")) {
			cnumber = cnumber.substring(0,i) + cnumber.substring((i + 1),cnumber.length);
			i--;
		}
		if (oneChar == ".") {
			if ((cnumber.length == 1) || (decimalFlag == 1)) {
				return "-1";
			} else {
				decimalFlag = 1;
			}
		}
	}
	if (cnumber == "") {
		return "0";
	} else {
		return cnumber;
	}
}

function Currency(money1) {
	var money = new String(money1);
	var tempdollaramt = "";
	if (money.indexOf("e") != -1) {
		tempdollaramt = "$0.00"
		return tempdollaramt;
	}
	if (money.indexOf(".",[0]) == -1) {
		money = money + ".00";
	}
	var decimal = money.indexOf(".",[0]);
	money = money.substring(0,decimal+3);
	var dollarsonly = "";
	var negamount = "";
	if (parseInt(money) > 0) {
		dollarsonly = money.substring(0, money.indexOf("."));
	} else {
		negamount = "-";
		dollarsonly = money.substring(1, money.indexOf("."));
	}
	var decimalandcents = money.substring(money.indexOf("."), money.length);
	var lenDollarsOnly = dollarsonly.length;
	if (lenDollarsOnly < 4) {
		tempdollaramt = "$" + money;
		return tempdollaramt;
	}
	while (lenDollarsOnly > 3) {
		tempdollaramt = "," + dollarsonly.substring(lenDollarsOnly-3,lenDollarsOnly) + tempdollaramt;
		lenDollarsOnly = lenDollarsOnly - 3;
	}
	if (lenDollarsOnly > 0) {
		tempdollaramt = dollarsonly.substring(0, lenDollarsOnly) + tempdollaramt;
	}
	tempdollaramt = "$" + negamount + tempdollaramt + decimalandcents;
	return tempdollaramt;
}

function generateReport() {
	var startmonth;
	startmonth = document.calcform.startm.value;
	var monthofpayment = new String;
	month = document.calcform.month.value;
	monthPay = document.calcform.monthPay.value;
	Houseprice = document.calcform.price.value;
	Ir = document.calcform.interest.value;
	Ir = Ir/1200;
	StaryearValue = document.calcform.startyear.value;
	
	var lengthoftimemethod = "";
	var lengthoftime = 0;
	
	if (document.calcform.startm.value > "") {
		document.calcform.startm.selectedIndex = document.calcform.startm.value;
	}
	
	if (document.calcform.startyear.value > "") {
		document.calcform.startyear.value = document.calcform.startyear.value;
	}
	
	if( month >= 0 ) {
		var html = "";
		var amortTable = "";
		var disponetimemonth = document.calcform.pymon.value;
	
		amortTable += "<p><b>The amortization of the mortgage:</b><p></center>";
		amortTable += "<table border=1  cellspacing=0 bordercolor=#000000>";
		amortTable +="<tr><th width=125>Month/Year</th><th width=125>Interest</th><th width=125>Principal</th><th width=125>Balance</th></tr>";
	
		//Loop through the payments and compute amortization
		var totalinterestpaid=0;
		var numpayments=0;
	
		var totalinterestfullamort=(monthPay * month) - Houseprice;
	
		var monthprepay = parseFloat(document.calcform.monthprepmt.value);
		var annualprepay = parseFloat(document.calcform.annualprepayt.value);
		var onetimeprepay = parseFloat(document.calcform.oneprepay.value);
		var onetimemonth = parseFloat(document.calcform.pymon.value);
		var principal = 0;
		var interest = 0;
		var startmonth = parseInt(document.calcform.startm.value);
		var year = StaryearValue;
		for(y = 0;parseFloat(Houseprice) > .01; y++) {
			interest = Houseprice * Ir;
			
			if (startmonth == 12) {
				if (annualprepay > 0 && (y > 0)) {
					if ((Houseprice - (annualprepay - interest)) < 0) {
						amortTable +="<tr><td colspan=4><b>Annual Prepayment resulted in an overpayment, reduced payment to:", (Houseprice + interest), "</b></td></tr>";
						totalinterestpaid = totalinterestpaid + interest;
						continue;
					}
					else {
						Houseprice = Houseprice - annualprepay;
						amortTable +="<tr><td width=150><b>Annual Prepayment "+ (parseInt(StaryearValue)+1) +"</b></td>"+"<td>"+Currency(0)+"</td><td>"+Currency(annualprepay)+"<td>"+Currency(Houseprice)+"</td></tr>";
						
					}
				}
			}
			if ((parseInt(onetimemonth) > 0) && (numpayments+1 == parseInt(onetimemonth))) {
				if ((Houseprice - (onetimeprepay - interest)) < 0) {
					amortTable +="<tr><td colspan=4><b>One Time Prepayment resulted in an overpayment, reduced payment to:", Currency(Houseprice + interest), "</b></td></tr>";
					totalinterestpaid = totalinterestpaid + interest;
					Houseprice = 0;
					continue;
				}
				else {
					Houseprice = Houseprice - onetimeprepay;
					amortTable +="<tr><td width=150><b>One Time Prepayment</b></td>"+"<td>"+Currency(0)+"</td><td>"+Currency(onetimeprepay)+"<td>"+Currency(Houseprice)+"</td></tr>";
				}
			}
	
			if (monthprepay > 0) {
				if ((Houseprice - (monthprepay - interest)) < 0) {
					amortTable +="<tr><td colspan=4><b>Monthly Prepayment resulted in an overpayment, reduced payment to: ", Currency(Houseprice + interest), "</b></td></tr>";
					totalinterestpaid = totalinterestpaid + interest;
					Houseprice = 0;
					continue;
				}
				else {
					Houseprice = Houseprice - monthprepay;
				}
			}
			if(startmonth > 11) {
				startmonth = 0;
				StaryearValue++;
			}
			switch(startmonth.toString()) {
				case "0":monthofpayment="January";
					break;
				case "1":monthofpayment="February";
					break;
				case "2":monthofpayment="March";
					break;
				case "3":monthofpayment="April";
					break;
				case "4":monthofpayment="May";
					break;
				case "5":monthofpayment="June";
					break;
				case "6":monthofpayment="July";
					break;
				case "7":monthofpayment="August";
					break;
				case "8":monthofpayment="September";
					break;
				case "9":monthofpayment="October";
					break;
				case "10":monthofpayment="November";
					break;
				case "11":monthofpayment="December";
			}
	
			interest = Houseprice * Ir;
	
			if ((Houseprice - (monthPay - interest)) < -.01) {
				var tempMonthPayment = Houseprice + interest;
				principal  = tempMonthPayment - interest;
				Houseprice = Houseprice - principal;
				amortTable +="<tr><td colspan=4>Final Payment was reduced because of prepays.  Final Payment amount was ", Currency(tempMonthPayment), "</td></tr>";
				amortTable +="<tr><td width=125>"+monthofpayment+" "+StaryearValue+"</td>"+"<td>"+Currency(interest)+"</td><td>"+Currency(principal)+"<td>"+Currency(Houseprice)+"</td></tr>";
				Houseprice = 0;
			} else {
				principal = monthPay - interest;
				Houseprice = Houseprice - principal;
				amortTable +="<tr><td width=125>"+monthofpayment+" "+StaryearValue+"</td>"+"<td>"+Currency(interest)+"</td><td>"+Currency(principal)+"<td>"+Currency(Houseprice)+"</td></tr>";
			}
			totalinterestpaid = totalinterestpaid + interest;
			numpayments++;
			startmonth++;
			
		}
	
		amortTable +="</table>";
		amortTable +="<h4>Final Summary</h2>";
		amortTable +="<table border=1 cellspacing=0 bordercolor=#000000>";
		amortTable +="<tr><td>Total Interest (including prepayments)</td><td>" + Currency(totalinterestpaid) + "</td></tr>";
		amortTable +="<tr><td>Total Interest (without prepayments)</td><td>" + Currency(totalinterestfullamort) + "</td></tr>";
		amortTable +="<tr><td>Average Interest Paid Each Month</td><td>" + Currency(totalinterestpaid/month) + "</td></tr>";
		amortTable +="<tr><td colspan=2 align=middle><b>Savings from Prepayments</b></td></tr>";
		amortTable +="<tr><td>Total Interest Saved</td><td>" + Currency(totalinterestfullamort-totalinterestpaid) + "</td></tr>";
		amortTable +="<tr><td>Years shorter loan</td><td>" + parseInt(((month - numpayments) / 12) * 100) / 100 + "</td></tr>";
		amortTable +="</table>";
	
		document.getElementById( "resultsPane" ).innerHTML = amortTable;
	}
}

/** functions **/

/* checking functions */
function checkPrice() {
	var price = document.calcform.price.value;
	if(price.indexOf(",") >= 0) {
		alert("Please do not put commas in the Price number")
	}
}

function checkAnnualInterest() {
	if (NumericCheck(document.calcform.interest.value)=="-1") {
		alert("Annual Interest Rate is not numeric");
		return;
	}
}

function checkAmortization() {
	if (NumericCheck(document.calcform.time.value)=="-1") {
		alert("Amortization Length is not numeric");
		return;
	}
}

function getFieldValue (strFieldName) {
	var strFieldValue;
	var objRegExp = new RegExp(strFieldName + "=([^&]+)","gi");
	if (objRegExp.test(location.search)) {
		strFieldValue = unescape(RegExp.$1);
	} else {
		strFieldValue="";
	}
	return strFieldValue;
}

function replacePunct(element) { //v2.0
	var c= element.value;
	c=c.replace(/[|,|\$]| /g, '');
	element.value = c;
	return true;
}

/*window.onload = generateReport();*/
