//*******************************************
//DO NOT REMOVE THIS COPYWRITE INFO!
//Mortgage Qualification Calculator V1
//1998 Daniel C. Peterson ALL RIGHTS RESERVED
//Created: 08/21/1998
//Last Modified: 07/26/2002
//This script may not be copied, edited, distributed or reproduced
//without express written permission from
//Daniel C. Peterson of Web Winder Website Services
//For commercial use rates, contact:
//Dan Peterson:
//Web Winder Website Services
//P.O. Box 11
//Bemidji, MN  56619
//dan@webwinder.com
//http://www.webwinder.com
//Commercial User Licence #:4519-1056-60-1019
//Commercial Licence Date:2007-04-09
//*******************************************



function stripNum(num) {

var iPercent
var iDollar
var iSpace
var iComma
var numLength = num.length

//lalalla Line #114

if(numLength > 0) {

   num=num.toString();

   iPercent = num.indexOf("%");
   if(iPercent >= 0) {
      num=num.substring(0,iPercent) + "" + num.substring(iPercent + 1,numLength);
      numLength=num.length;
      }
   iDollar = num.indexOf("$");
   if(iDollar >= 0) {
      num=num.substring(0,iDollar) + "" + num.substring(iDollar + 1,numLength);
      numLength=num.length;
      }
   iSpace = num.indexOf(" ");
   if(iSpace >= 0) {
      num=num.substring(0,iSpace) + "" + num.substring(iSpace + 1,numLength);
      numLength=num.length;
      }
   iComma = num.indexOf(",");
   if(iComma >= 0) {
      while(iComma >=1) {
         num=num.substring(0,iComma) + "" + num.substring(iComma + 1,numLength);
         numLength=num.length;
         iComma = num.indexOf(",");
      }
      }

      num = eval(num);


} else {

num = 0;

}

return num;

}




function computeMonthlyPayment(prin, numPmts, intRate) {

var pmtAmt = 0;

if(intRate == 0) {
   pmtAmt = prin / numPmts;
} else {
   
   if (intRate >= 1.0) {
     intRate = intRate / 100.0;
   }
   intRate /= 12;

   var pow = 1;
   for (var j = 0; j < numPmts; j++)
      pow = pow * (1 + intRate);

   pmtAmt = (prin * pow * intRate) / (pow - 1);

}

return pmtAmt;

}




function formatNumber(num) {

var isNeg=0;

    if(num < 0) {
       num=num*-1;
       isNeg=1;
    }

    
	onum=Math.round(num*100)/100;
		
	integer=Math.floor(onum);

	if (Math.ceil(onum) == integer) {
		decimal="00";
	} else{
		decimal=Math.round((onum-integer)*100)
	}
	decimal=decimal.toString();
	if (decimal.length<2) decimal="0"+decimal;

	integer=integer.toString();
	var tmpnum="";
	var tmpinteger="";
	var y=0;

	for (x=integer.length;x>0;x--) {
		tmpnum=tmpnum+integer.charAt(x-1);
		y=y+1;
		if (y==3 & x>1) {
			tmpnum=tmpnum+",";
			y=0;
		}
	}

	for (x=tmpnum.length;x>0;x--) {
		tmpinteger=tmpinteger+tmpnum.charAt(x-1);
	}
		
	finNum=tmpinteger+"."+decimal;

    if(isNeg == 1) {
       finNum = "-" + finNum;
    }

	return finNum;
}


function computeForm(form) {

var alert_txt = "";

//**************SET RATIOS HERE*****************
var house_pmt_to_income = .33;
var debt_to_income = .41;
//**************END SET RATIOS*****************

//IF REQUIRED FIELDS EMPTY, ALERT AND KILL SCRIPT
if(form.grossPay.value == "" || form.grossPay.value == 0) {
   alert("Please enter your gross annual income.");
   form.grossPay.focus();
   } else
if(form.downPay.value == "" || form.downPay.value == 0) {
   alert_txt = "Please enter the amount you have available for the down payment. ";
   alert_txt += "You will likely need at least 5 percent of the purchase price ";
   alert_txt += "in order to qualify for the mortgage.";
   alert(alert_txt);
   form.downPay.focus();
   } else
if(form.intRate.value == "" || form.intRate.value == 0) {
   alert("Please enter the annual interest rate you expect to pay.");
   form.intRate.focus();
   } else {
//START REQUIRED FIELD VARIFICATION

//SET OPTIONAL BLANK FIELDS EQUAL TO ZERO;
var VmonthlyDebtPmts = stripNum(form.moDebts.value);
if(VmonthlyDebtPmts == 0) {
   VmonthlyDebtPmts = 0;
   } else {
   VmonthlyDebtPmts = VmonthlyDebtPmts;
   }

var VmoInsurance = stripNum(form.moInsurance.value);
if(VmoInsurance == 0) {
   VmoInsurance = 0;
   } else {
   VmoInsurance = VmoInsurance;
   }

var VmoPropTax = stripNum(form.moPropTax.value);
if(VmoPropTax == 0) {
   VmoPropTax = 0;
   } else {
   VmoPropTax = VmoPropTax;
   }


//COMPUTE MONTHLY INCOME BASED ON ANNUAL INCOME
var VgrossPay = stripNum(form.grossPay.value);
var monthlyIncome = VgrossPay /12;

//MORTGAGE PAYMENT CAN'T EXCEED 28% OF MONTHLY INCOME
var maxIncomePmt = monthlyIncome * house_pmt_to_income;

//MORTGAGE PAYMENT PLUS DEBT PMTS CAN'T EXCEED 36% OF MONTHLY INCOME
var maxDebtToIncomePmt = debt_to_income * monthlyIncome - eval(VmonthlyDebtPmts);

//USE THE LOWER OF house_pmt_to_income% OR debt_to_income% AS MAXIMUM HOUSE PAYMENT
var maxHousePmt = 0;
if(maxIncomePmt > maxDebtToIncomePmt) {
   maxHousePmt = maxDebtToIncomePmt;
   } else {
   maxHousePmt = maxIncomePmt;
   }

//IF MAX HOUSE PAYMENT IS LESS THAN $1, ALERT & KILL SCRIPT
if(maxHousePmt < 1) {
   form.downPay2.value = "";
   form.loanAmt.value = "";
   form.homePrice.value = "";
   form.moPay.value = "";
   alert_txt = "Based on industry standards you would not qualify for a home mortgage. ";
   alert_txt += "In order to qualify you will need to either increase your annual ";
   alert_txt += "income or lower your monthly debt payments, or a combination of both.";
   alert(alert_txt);
   } else {
//START HOUSE PAYMENT VERIFICATION

//ADJUST HOUSE PAYMENT DOWN TO REFLECT MONTHLY INSURANCE & TAX
maxHousePmt = eval(maxHousePmt) - (eval(VmoInsurance) + eval(VmoPropTax));

//COMPUTE MAXIMUM HOME PRICE BASED ON DOWN PAYMENT
var VdownPmt = stripNum(form.downPay.value);
var maxDownPayPrice = VdownPmt / .07;

//COMPUTE MAXIMUM LOAN AMOUNT BASED ON DOWN PAYMENT
var maxDownPayLoan = maxDownPayPrice * .93;
var maxLoan = maxDownPayLoan;

//GATHER VARIABLES FOR PAYMENT AND PRINCIPLE COMPUTATIONS
var VintRate = stripNum(form.intRate.value);
     if(VintRate >= 1) {
     VintRate = VintRate / 100;
     } else {
     VintRate = VintRate;
     }

    VintRate = VintRate / 12;


    var Vterm = 0;
    if(form.term.selectedIndex == 0) {
       Vterm = 180;
       } else
       if(form.term.selectedIndex == 1) {
       Vterm = 240;
       } else {
       Vterm = 360;
       }

//COMPUTE PRINCIPAL PAID ON MAXIMUM MONTHLY PAYMENT
   var prin = eval(maxHousePmt) - (eval(maxHousePmt * VintRate));
   var intPort = 0;
   var prinPort =0;
   var count = 1;

   while(count < Vterm) {
      intPort = prin * VintRate;
      prinPort = eval(maxHousePmt) - eval(intPort);
      prin = eval(prin) + eval(prinPort);
      count = count + 1;
      if(count > 360) {break; } else {continue; }
      }

var maxPmtLoanAmt = prin;

//COMPUTE MONTHLY PAYMENT BASED ON MAXIMUM LOAN AMOUNT

    var factor = 1;

    for (var j = 0; j < Vterm; j++) {

        factor = factor * (eval(1) + eval(VintRate));
        }


    var maxLoanPmt = (maxLoan * factor * VintRate) / (eval(factor) - eval(1));

//CHOOSE THE LESSOR OF THE TWO PAYMENT AMOUNTS
var maxMoPmt = 0;
if(maxHousePmt > maxLoanPmt) {
   maxMoPmt = maxLoanPmt;
   } else {
   maxMoPmt = maxHousePmt;
   }

//CALCULATE FINAL TOTALS BASED ON FINAL MAX MONTHLY PAYMENT
var prin2 = eval(maxMoPmt) - (eval(maxMoPmt * VintRate));
   var intPort2 = 0;
   var prinPort2 =0;
   var count2 = 1;

   while(count2 < Vterm) {
      intPort2 = prin2 * VintRate;
      prinPort2 = eval(maxMoPmt) - eval(intPort2);
      prin2 = eval(prin2) + eval(prinPort2);
      count2 = count2 + 1;
      if(count2 > 360) {break; } else {continue; }
      }

var finalMaxLoanAmt = prin2;

//CALCULATE CLOSING COSTS
var closeCost = finalMaxLoanAmt * .02;
var finalDownPay = VdownPmt - closeCost;

//ENTER TOTALS
form.downPay2.value = "$" + formatNumber(finalDownPay);
form.loanAmt.value = "$" + formatNumber(finalMaxLoanAmt);
form.homePrice.value = "$" + formatNumber(eval(finalDownPay) + eval(finalMaxLoanAmt));
var finalMoPay = computeMonthlyPayment(finalMaxLoanAmt, Vterm, VintRate * 12 * 100)
//form.moPay.value = "$" + formatNumber(maxMoPmt);
form.moPay.value = "$" + formatNumber(finalMoPay);

var help_txt = "";

//DISPLAY SUMMARY
   help_txt = "After testing your entries against mortgage industry standards the ";
   help_txt += "highest priced house you could qualify for is " + form.homePrice.value + ".  ";
   help_txt += "Obviously this amount is merely an estimate.  The actual ";
   help_txt += "amount will vary from lender to lender.";
   form.enterHelp.value = help_txt;

//DISPLAY SUMMARY
   help_txt = "Place your mouse over the question marks at left ";
   help_txt += "to see an explanation of each result.";
   form.resultHelp.value = help_txt;

//END HOUSE PAYMENT VERIFICATION
      }

//END REQUIRED FIELD VARIFICATION
   }

}

//GIVE ENTRY INSTRUCTIONS
function help01(form) {
var help_txt = "ENTER: Your gross annual household income.  ";
help_txt += "This is the amount before taxes are deducted.";
document.mortCalc.enterHelp.value = help_txt;
}

function skipTo(form) {
form.enterHelp.value = "";
form.downPay.focus();
}

function help02(form) {
var help_txt = "ENTER: The total of your non-mortgage monthly debt payments. ";
help_txt += "This would include car loans, student loans, ";
help_txt += "credit card payments and so on.";
document.mortCalc.enterHelp.value = help_txt;
}

function help03(form) {
var help_txt = "ENTER: The amount you have available to cover the mortgage ";
help_txt += "down payment and closing costs.";
document.mortCalc.enterHelp.value = help_txt;
}

function help04(form) {
var help_txt = "ENTER: The annual interest rate you expect to pay on this mortgage. ";
help_txt += "You can enter the rate either as a percentage (8.25) or ";
help_txt += "as a decimal (.0825), whichever you prefer.";
document.mortCalc.enterHelp.value = help_txt;
}

function help05(form) {
var help_txt = "ENTER: The monthly insurance payment you expect to pay. As a ";
help_txt += "rule of thumb, you can expect to pay .125% (home ";
help_txt += "price X .00125) of the purchase price per month.";
document.mortCalc.enterHelp.value = help_txt;
}

function help06(form) {
var help_txt = "ENTER: The monthly property tax payment you expect to pay. ";
help_txt += "As a rule of thumb, you can expect to pay .027% (home price ";
help_txt += "X .00027) of the purchase price per month.";
document.mortCalc.enterHelp.value = help_txt;
}

function help07(form) {
document.mortCalc.enterHelp.value = ("ENTER: Length of Mortgage.");
}


//GIVE RESULT EXPLANATIONS
function help08(form) {
var help_txt = "RESULT: This is your original down payment amount less ";
help_txt += "an estimated 2% for closing costs.";
document.mortCalc.resultHelp.value = help_txt;
}

function help09(form) {
var help_txt = "RESULT: This is the maximum mortgage you would qualify for ";
help_txt += "based on your current entries.";
document.mortCalc.resultHelp.value = help_txt;
}

function help10(form) {
var help_txt = "RESULT: This is home price you could afford (the total ";
help_txt += "of your down payment and your maximum mortgage amount.";
document.mortCalc.resultHelp.value = help_txt;
}

function help11(form) {
var help_txt = "RESULT: This is your maximum monthly mortgage payment ";
help_txt += "based upon your current entries.";
document.mortCalc.resultHelp.value = help_txt;
}
