// Calculations for Millennia Logix advanced mortgage calculator
// Performs montly payment calc and places results into fields on form
// Copyright 1998-2005 Millennia Logix, Inc by Paul F Johnson
function calcul8() {

//		First, verify the input ...

	if (document.calculator.PurchPrice.value == "") {
		alert("Please fill in Purchase Price of the home.");
		document.calculator.PurchPrice.focus();
		return false
		}

	var dp = trimString(document.calculator.Downpayment.value)
	if (dp == "") { dp = 0 }
	document.calculator.Downpayment.value = dp

	if (document.calculator.Downpayment.value == 0) {
		if (confirm("You entered a Down Payment of $0. Is that what you wanted?")) { }
		else { 
			document.calculator.Downpayment.focus();
			return false
			}
		}

	if (document.calculator.LoanTerm.value == "") {
		alert("Please fill in the number of years you want the mortgage for.");
		document.calculator.LoanTerm.value = '30'
		document.calculator.LoanTerm.focus();
		return false
		}

	if (document.calculator.IntRate.value == "") {
		alert("Please fill in the Interest Rate.");
		document.calculator.IntRate.value = '5.500'
		document.calculator.IntRate.focus();
		return false
		}

	// Then do traditional calculation of monthly payment amount
	var rPrincipal = (document.forms[0].PurchPrice.value)-(document.forms[0].Downpayment.value);
	var rAnnualRate = (document.forms[0].IntRate.value);
	var rRateFactor = rAnnualRate  / 1200;
	var rNumPmts = (document.forms[0].LoanTerm.value) * 12;
	var rNumerator = rRateFactor * Math.pow((1 + rRateFactor), rNumPmts);
	var rDenominator = Math.pow((1 + rRateFactor), rNumPmts) - 1;
	var rPayment = Round2Up(rPrincipal * rNumerator / rDenominator);
	var rTaxes = (document.forms[0].Taxes.value) / 12;
	var rTotPayment = rPayment + rTaxes;
	if (rPrincipal < 1000) {
		alert ("Principal amount is too small.  Try again.");
		return;
	}
	else if (rAnnualRate < 1) {
		alert ("Interest rate less than 1%.  Try again.");
		return;
	}
	else if (rAnnualRate >= 40) {
		alert ("Interest rate greater than 40%.  Try again.");
		return;
	}

	var rMonInterest = rPrincipal * rRateFactor
	if (rMonInterest >= rPayment) {
		alert ("Monthly Interest is more than Payment. Loan will never be paid off.");
		return;
		}

	// Figure total interest for monthly mortgage
	var rTotInterest = (rPayment * rNumPmts) - rPrincipal;

// Figure total interest and time to pay off if bi-weekly
// by generating amortization schedule
	var rBiPayment = Round2Up(rPayment / 2);
	var rBiTaxes = (document.forms[0].Taxes.value) / 26;
	var rBiRateFactor = (document.forms[0].IntRate.value) * 14 / 36500;
	var rBiTotPayment = rBiPayment + rBiTaxes;
	var i = 0;
	var p = 0;
	var rPrinBal = rPrincipal;
	var rBiTotInt = 0;
	var rBiNumPmts = 0;
	while(rPrinBal >= (rBiPayment - 1)) {
			// Interest for this payment
        	i = rPrinBal * rBiRateFactor;
			// Principal portion of payment
			p = rBiPayment - i;
			if(p < 1) {
				// Principal reduction too small to pay off mortgage!
				break;
				}
			// Total interest, number payments
			rBiTotInt = rBiTotInt + i; 
			rBiNumPmts = rBiNumPmts + 1;       	
        	rPrinBal = rPrinBal - p;
       }
	var rBiSavings = rTotInterest - rBiTotInt;
	var rBiYears = (rBiNumPmts * 14 / 365.25);
	
// Figure total interest and time to pay off if monthly with extra payments
// by generating amortization schedule

	document.forms[0].Payment2.value = "";
	document.forms[0].savings_extra.value = "";
	document.forms[0].length_extra.value = "";

	var rEMPayment1 = (document.forms[0].extra_monthly.value) * 1;
	var rEMPayment2 = (document.forms[0].extra_yearly.value) * 1;
	var rEMPayment3 = (document.forms[0].extra_onetime.value) * 1;
	var rEM2Mon = (document.forms[0].extra_yearly_month.value) * 1;	
	var rEM3Mon = (document.forms[0].extra_onetime_month.value) * 1;	
	var rEM3Yr = (document.forms[0].extra_onetime_year.value) * 1;	
	var rSMon = (document.forms[0].start_month.value) * 1;	
	var rSYr = (document.forms[0].start_year.value) * 1;	
	
	if ((rEMPayment1 < 0) || (rEMPayment2 < 0) || (rEMPayment3 < 0)) {
		alert ("Addiitonal Payments cannot be negative.");
		return;	
		}
		
	if ((rEMPayment1 > 0) || (rEMPayment2 > 0) || (rEMPayment3 > 0)) {
		var rEMTotPayment = rPayment + rTaxes + rEMPayment1;
		var rEMPayment = rPayment + rEMPayment1;
//		alert("*" + rEMPayment1 + "*" + rEMPayment)
		var iem = 0;
		var pem = 0;
		var rEMPrinBal = rPrincipal;
		var rEMTotInt = 0;
		var rEMNumPmts = 0;
		var rCMon = rSMon;
		var rCYr = rSYr;
		while(rEMPrinBal >= rEMPayment) {
			// Interest for this payment
        	iem = rEMPrinBal * rRateFactor;
			// Principal portion of payment
			pem = rEMPayment - iem;
			if(pem < 1) {
				// Principal reduction too small to pay off mortgage!
				break;
				}

//		Get extra payment if the selected month
			var rExtra2 = 0
			if ((rEMPayment2 > 0) && (rCMon == rEM2Mon )) { rExtra2 = rEMPayment2 }
			var rExtra3 = 0
			if ((rEMPayment3 > 0) && (rCMon == rEM3Mon )&& (rCYr == rEM3Yr )) { rExtra3 = rEMPayment3 }

//		Total interest, number payments
			rEMTotInt = rEMTotInt + iem; 
			rEMNumPmts = rEMNumPmts + 1;       	
        	rEMPrinBal = rEMPrinBal - pem;
			if ((rEMPrinBal - (rExtra2 + rExtra3)) >= 0 ) {rEMPrinBal = rEMPrinBal - (rExtra2 + rExtra3)}
			else {rEMPrinBal = 0}

//		Maintain the date counters
			rCMon = rCMon + 1;
			if (rCMon == 13) {
				rCMon = 1;
				rCYr = rCYr + 1;
				}
     	  	}
		if (rEMPrinBal > 0) {
        	iem = rEMPrinBal * rRateFactor;
			rEMTotInt = rEMTotInt + iem; 
			rEMNumPmts = rEMNumPmts + 1;       	
			}
		var rEMSavings = rTotInterest - rEMTotInt;
		var rEMYears = (rEMNumPmts / 12);
		}

// Figure total interest and time to pay off if biweekly with extra payments
// by generating amortization schedule

	document.forms[0].BiPayment2.value = "";
	document.forms[0].BiSavings_extra.value = "";
	document.forms[0].BiYears_extra.value = "";

//	var rEBPayment1 = (document.forms[0].extra_monthly.value) * 1;
//	var rEBPayment2 = (document.forms[0].extra_yearly.value) * 1;
//	var rEBPayment3 = (document.forms[0].extra_onetime.value) * 1;
//	var rEB2Mon = (document.forms[0].extra_yearly_month.value) * 1;	
//	var rEB3Mon = (document.forms[0].extra_onetime_month.value) * 1;	
//	var rEB3Yr = (document.forms[0].extra_onetime_year.value) * 1;	
//	var rSMon = (document.forms[0].start_month.value) * 1;	
//	var rSYr = (document.forms[0].start_year.value) * 1;	
	
	if ((rEMPayment1 < 0) || (rEMPayment2 < 0) || (rEMPayment3 < 0)) {
		alert ("Addiitonal Payments cannot be negative.");
		return;	
		}
		
	rEBPayment1 = Round2Up(rEMPayment1 / 2);
	rMonthChange = 0
	rEBPayPeriod = 14
	var dtNext = new Date(rSYr, rSMon-1, 1)
	var dtLast = new Date(rSYr, rSMon-1, 1)
	dtLast.setDate(dtLast.getDate()-rEBPayPeriod)
//	alert ("Next =" + dtNext + " Last =" + dtLast)
	
	if ((rEMPayment1 > 0) || (rEMPayment2 > 0) || (rEMPayment3 > 0)) {
		var rEBTotPayment = rBiPayment + rBiTaxes + rEBPayment1;
		var rEBPayment = rBiPayment + rEBPayment1;
		var ieb = 0;
		var peb = 0;
		var rEBPrinBal = rPrincipal;
		var rEBTotInt = 0;
		var rEBNumPmts = 0;
		var rNMon = dtNext.getMonth() + 1;
		var rNYr = dtNext.getYear();
		var rLMon = dtLast.getMonth() + 1;
		var rLYr = dtLast.getYear();
//		alert ("rNMon =" + rNMon + " rNYr =" + rNYr + " rLMon =" + rLMon + " rLYr =" + rLYr + " rEM2Mon =" + rEM2Mon + " rEM3Mon =" + rEM3Mon + " rEM3Yr =" + rEM3Yr)
		var rExtra2 = 0
		var rExtra3 = 0
		while(rEBPrinBal >= rEBPayment) {
			// Interest for this payment
        	ieb = rEBPrinBal * rBiRateFactor;
			// Principal portion of payment
			peb = rEBPayment - ieb;
			if(peb < 1) {
				// Principal reduction too small to pay off mortgage!
				break;
				}

//		Get extra payment if the selected month
//			var rExtra2 = 0
//			if ((rEMPayment2 > 0) && (rCMon == rEM2Mon )) { rExtra2 = rEMPayment2 }
//			var rExtra3 = 0
//			if ((rEMPayment3 > 0) && (rCMon == rEM3Mon )&& (rCYr == rEM3Yr )) { rExtra3 = rEMPayment3 }

			rExtra2 = 0
			rExtra3 = 0
			if (rLMon != rNMon) {
				rMonthChange = 0
				if (rEM2Mon == rNMon) { 
					rExtra2 = rEMPayment2 
//			alert ("Next =" + dtNext + " rExtra2 =" + rExtra2 + " rNMon =" + rNMon + " rNYr =" + rNYr + " rLMon =" + rLMon + " rLYr =" + rLYr)
					
					}
				if ((rEM3Yr == rNYr) && (rEM3Mon == rNMon)) { 
					rExtra3 = rEMPayment3 
//			alert ("Next =" + dtNext + " rExtra3 =" + rExtra3 + " rNMon =" + rNMon + " rNYr =" + rNYr + " rLMon =" + rLMon + " rLYr =" + rLYr)
					}
				}
			else { rMonthChange = rMonthChange + 1}

//		Total interest, number payments
			rEBTotInt = rEBTotInt + ieb; 
			rEBNumPmts = rEBNumPmts + 1;       	
        	rEBPrinBal = rEBPrinBal - peb;
			if ((rEBPrinBal - (rExtra2 + rExtra3)) >= 0 ) {rEBPrinBal = rEBPrinBal - (rExtra2 + rExtra3)}
			else {rEBPrinBal = 0}

//		Maintain the date counters
			dtLast.setDate(dtLast.getDate()+rEBPayPeriod)
			dtNext.setDate(dtNext.getDate()+rEBPayPeriod)
			rNMon = dtNext.getMonth() + 1;
			rNYr = dtNext.getYear();
			rLMon = dtLast.getMonth() + 1;
			rLYr = dtLast.getYear();
//			alert ("Next =" + dtNext + " Last =" + dtLast + " rNMon =" + rNMon + " rNYr =" + rNYr + " rLMon =" + rLMon + " rLYr =" + rLYr)

//			rCMon = rCMon + 1;
//			if (rCMon == 13) {
//				rCMon = 1;
//				rCYr = rCYr + 1;
//				}
     	  	}
		if (rEBPrinBal > 0) {
        	ieb = rEBPrinBal * rBiRateFactor;
			rEBTotInt = rEBTotInt + ieb; 
			rEBNumPmts = rEBNumPmts + 1;       	
			}
		var rEBSavings = rTotInterest - rEBTotInt;
		var rEBYears = (rEBNumPmts * 14 / 365.25);
		}
	
	// Display results
	document.forms[0].Payment.value = Round2DollarsUp(rTotPayment);
	document.forms[0].BiPayment.value = Round2Dollars(rBiTotPayment);
	document.forms[0].BiSavings.value = Round2FiveComma(rBiSavings);
	document.forms[0].BiYears.value = Round2(rBiYears);
	document.forms[0].term.value = document.forms[0].LoanTerm.value
	if ((rEMPayment1 > 0) || (rEMPayment2 > 0) || (rEMPayment3 > 0)) {
		document.forms[0].Payment2.value = Round2Dollars(rEMTotPayment);
		document.forms[0].savings_extra.value = Round2FiveComma(rEMSavings);
		document.forms[0].length_extra.value = Round2(rEMYears);

		document.forms[0].BiPayment2.value = Round2Dollars(rEBTotPayment);
		document.forms[0].BiSavings_extra.value = Round2FiveComma(rEBSavings);
		document.forms[0].BiYears_extra.value = Round2(rEBYears);
		}

	// Finally do PMI warning
	if (rPrincipal > (document.forms[0].PurchPrice.value) * .8) {
		ShowPMIdisclaimer()
	}
	else {
		HidePMIdisclaimer()
	}

//	Set the flag that a calculation has been performed
	bCalc = true

}