function jumpBox(list) {
   location.href = list.options[list.selectedIndex].value
   }

//
//
//

function formatNum(Vnum) {

   if(Vnum > 99000000) {
   alert("Sorry, this will not generate numbers larger that 99 million.");
   focus();
   } else {
   
   var V10million = parseInt(Vnum / 10000000);

   var V1million = (Vnum % 10000000)  / 1000000;
      if(V1million / 1000000 == 1) {
      V1million = 1;
      } else
      if(V1million < 1) {
      V1million = "0";
     } else {
      V1million = parseInt(V1million,10);
     }

    var V100thousand = (Vnum % 1000000)  / 100000;
      if(V100thousand / 100000 == 1) {
      V100thousand = 1;
      } else
      if(V100thousand < 1) {
      V100thousand = "0";
     } else {
      V100thousand = parseInt(V100thousand,10);
     }

   var V10thousand = (Vnum % 100000)  / 10000;
      if(V10thousand / 10000 == 1) {
      V10thousand = 1;
      } else
      if(V10thousand < 1) {
      V10thousand = "0";
      } else {
      V10thousand = parseInt(V10thousand,10);
      }

   var V1thousand = (Vnum % 10000)  / 1000;
      if(V1thousand / 1000 == 1) {
      V1thousand = 1;
      } else
      if(V1thousand < 1) {
      V1thousand = "0";
     } else {
      V1thousand = parseInt(V1thousand,10);
     }

   var Vhundreds = (Vnum % 1000)  / 100;
      if(Vhundreds / 100 == 1) {
      Vhundreds = 1;
      } else
      if(Vhundreds < 1) {
      Vhundreds = "0";
     } else {
      Vhundreds = parseInt(Vhundreds,10);
     }

   var Vtens = (Vnum % 100)  / 10;
      if(Vtens / 10 == 1) {
      Vtens = 1;
      } else
      if(Vtens < 1) {
      Vtens = "0";
     } else {
      Vtens = parseInt(Vtens,10);
     }

   var Vones = (Vnum % 10)  / 1;
      if(Vones / 1 == 1) {
      Vones = 1;
      } else
      if(Vones < 1) {
      Vones = "0";
     } else {
      Vones = parseInt(Vones,10);
     }

//START UPGRADE
var Vcents = 0;

if(Vnum % 1 * 100 < 1) {
   Vcents = 0;
   } else {
   Vcents = parseInt(((eval(Vnum % 1) * 100)),10);
   }
//END UPGRADE

 if(Vcents < 1) {
  Vcents = "00";
  }
  else
  if(Vcents % 10 == 0) {
  Vcents = Vcents + "0";
  }
  else
  if(Vcents % 10 == Vcents) {
  Vcents = "0" + Vcents;
  } else {
  Vcents = Vcents;
  }

  if(Vcents == "900") {
  Vcents = "90";
  } else
  if(Vcents == "800") {
  Vcents = "80";
  } else 
  if(Vcents == "700") {
  Vcents = "70";
  } else 
  if(Vcents == "600") {
  Vcents = "60";
  } else 
  if(Vcents == "500") {
  Vcents = "50";
  } else 
  if(Vcents == "400") {
  Vcents = "40";
  } else 
  if(Vcents == "300") {
  Vcents = "30";
  } else
  if(Vcents == "200") {
  Vcents = "20";
  } else
  if(Vcents == "100") {
  Vcents = "10";
  } else {
  Vcents = Vcents;
  }

   
   var Vformat = "";

   if(Vnum >= 10000000) {
   Vformat = (V10million + "" + V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1000000) {
   Vformat = (V1million + "," + V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 100000) {
   Vformat = (V100thousand + "" + V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 10000) {
   Vformat = (V10thousand + "" + V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1000) {
   Vformat = (V1thousand + "," + Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 100) {
   Vformat = (Vhundreds + "" + Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 10) {
   Vformat = (Vtens + "" + Vones + "." + Vcents);
   }
   else
   if(Vnum >= 1) {
   Vformat = (Vones + "." + Vcents);
   } else {
   Vformat = ("0." + Vcents);
   }

  return Vformat;

  }
}



function computeForm(form) {

//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("Please enter the amount you have available for the down payment. You will likely need at least 5 percent of the purchase price in order to qualify for the mortgage.");
   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 = form.moDebts.value;
if(VmonthlyDebtPmts == "") {
   VmonthlyDebtPmts = 0;
   } else {
   VmonthlyDebtPmts = VmonthlyDebtPmts;
   }

var VmoInsurance = form.moInsurance.value;
if(VmoInsurance == "") {
   VmoInsurance = 0;
   } else {
   VmoInsurance = VmoInsurance;
   }

var VmoPropTax = form.moPropTax.value;
if(VmoPropTax == "") {
   VmoPropTax = 0;
   } else {
   VmoPropTax = VmoPropTax;
   }


//COMPUTE MONTHLY INCOME BASED ON ANNUAL INCOME
var VgrossPay = form.grossPay.value;
var monthlyIncome = VgrossPay /12;

//MORTGAGE PAYMENT CAN'T EXCEED 28% OF MONTHLY INCOME
var maxIncomePmt = monthlyIncome * .28;

//MORTGAGE PAYMENT PLUS DEBT PMTS CAN'T EXCEED 36% OF MONTHLY INCOME
var maxDebtToIncomePmt = .36 * monthlyIncome - eval(VmonthlyDebtPmts);

//USE THE LOWER OF 28% OR 36% 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("Based on industry standards you would not qualify for a home mortgage. In order to qualify you will need to either increase your annual income or lower your monthly debt payments, or a combination of both.")
   } 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 = 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 = 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 = "$" + formatNum(finalDownPay);
form.loanAmt.value = "$" + formatNum(finalMaxLoanAmt);
form.homePrice.value = "$" + formatNum(eval(finalDownPay) + eval(finalMaxLoanAmt));
form.moPay.value = "$" + formatNum(maxMoPmt);

//DISPLAY SUMMARY
form.enterHelp.value = ("After testing your entries against mortgage industry standards the highest priced house you could qualify for is " + form.homePrice.value + ".  Obviously this amount is merely an estimate.  The actual amount will vary from lender to lender.")

//DISPLAY SUMMARY
form.resultHelp.value = ("Place your mouse over the question marks at left to see an explanation of each result.")

//END HOUSE PAYMENT VERIFICATION
      }

//END REQUIRED FIELD VARIFICATION
   }

}

//GIVE ENTRY INSTRUCTIONS
function help01(form) {
document.mortCalc.enterHelp.value = ("ENTER: Your gross annual household income.  This is the amount before taxes are deducted.");
}

function skipTo(form) {
form.enterHelp.value = "";
form.moDebts.focus();
}

function help02(form) {
document.mortCalc.enterHelp.value = ("ENTER: The total of your non-mortgage monthly debt payments. This would include car loans, student loans, credit card payments and so on.");
}

function help03(form) {
document.mortCalc.enterHelp.value = ("ENTER: The amount you have available to cover the mortgage down payment and closing costs.");
}

function help04(form) {
document.mortCalc.enterHelp.value = ("ENTER: The annual interest rate you expect to pay on this mortgage. You can enter the rate either as a percentage (8.25) or as a decimal (.0825), whichever you prefer.");
}

function help05(form) {
document.mortCalc.enterHelp.value = ("ENTER: The monthly insurance payment you expect to pay. As a rule of thumb, you can expect to pay .125% (home price X .00125) of the purchase price per month.");
}

function help06(form) {
document.mortCalc.enterHelp.value = ("ENTER: The monthly property tax payment you expect to pay. As a rule of thumb, you can expect to pay .027% (home price X .00027) of the purchase price per month.");
}

function help07(form) {
document.mortCalc.enterHelp.value = ("ENTER: Length of Mortgage.");
}


//GIVE RESULT EXPLANATIONS
function help08(form) {
document.mortCalc.resultHelp.value = ("RESULT: This is your original down payment amount less an estimated 2% for closing costs.");
}

function help09(form) {
document.mortCalc.resultHelp.value = ("RESULT: This is the maximum mortgage you would qualify for based on your current entries.");
}

function help10(form) {
document.mortCalc.resultHelp.value = ("RESULT: This is home price you could afford (the total of your down payment and your maximum mortgage amount.");
}

function help11(form) {
document.mortCalc.resultHelp.value = ("RESULT: This is your maximum monthly mortgage payment based upon your current entries.");
}


function clearForm(form)

{
form.grossPay.value = "";
form.moDebts.value = "";
form.downPay.value = "";
form.intRate.value = "";
form.moInsurance.value = "";
form.moPropTax.value = "";
form.homePrice.value = "";
form.moPay.value = "";
form.loanAmt.value = "";
form.downPay2.value = "";
form.enterHelp.value = "";
form.resultHelp.value = "";
form.grossPay.focus();

}