// JavaScript Document

function init() {
  if (!document.getElementById) return
  var imgOriginSrc;
  var imgTemp = new Array();
  var imgarr = document.getElementsByTagName('img');
  for (var i = 0; i < imgarr.length; i++) {
    if (imgarr[i].getAttribute('hsrc')) {
        imgTemp[i] = new Image();
        imgTemp[i].src = imgarr[i].getAttribute('hsrc');
        imgarr[i].onmouseover = function() {
            imgOriginSrc = this.getAttribute('src');
            this.setAttribute('src',this.getAttribute('hsrc'))
        }
        imgarr[i].onmouseout = function() {
            this.setAttribute('src',imgOriginSrc)
        }
    }
  }
}
onload=init;

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.bfirstname.value == "" || form.bfirstname.value == 0) {
   alert("Please enter your First Name.");
   form.bfirstname.focus();
   } else
if(form.blastname.value == "" || form.blastname.value == 0) {
   alert("Please enter your Last Name.");
   form.blastname.focus();
   } else
if(form.propstreet.value == "" || form.propstreet.value == 0) {
   alert("Please enter your Current Street Addtess.");
   form.propstreet.focus();
   } else
if(form.propcity.value == "" || form.propcity.value == 0) {
   alert("Please enter your Current City.");
   form.propstreet.focus();
   } else
if(form.propstate.value == "" || form.propstate.value == 0) {
   alert("Please enter your Current State.");
   form.propcity.focus();
   } else
if(form.propzip.value == "" || form.propzip.value == 0) {
   alert("Please enter your Current Zip Code.");
   form.propzip.focus();
   } else
if(form.email.value == "" || form.email.value == 0) {
   alert("Please enter your Email Address.");
   form.email.focus();
   } else
if(form.phonenum.value == "" || form.phonenum.value == 0) {
   alert("Please enter a Contact Phone Number.");
   form.phonenum.focus();
   } else
if(form.grossPay.value == "" || form.grossPay.value == 0) {
   alert("Please enter your Total Monthly Income.");
   form.grossPay.focus();
   } else
if(form.intRate.value == "" || form.intRate.value == 0) {
   alert("Please enter an Interest Rate you have been quoted or leave the default.");
   form.intRate.focus();
   } else 

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = form.email.value;
   if(reg.test(address) == false) {
      alert('Invalid Email Address.');
     form.email.focus();
      return false;
   } else {

//START REQUIRED FIELD VARIFICATION

//CONVERT TO NUMBERS
form.grossPay.value = parseFloat(form.grossPay.value);
form.moDebts.value = parseFloat(form.moDebts.value);
form.downPay.value = parseFloat(form.downPay.value);
form.intRate.value = parseFloat(form.intRate.value);
form.moInsurance.value = parseFloat(form.moInsurance.value);
form.moPropTax.value = parseFloat(form.moPropTax.value);


//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;

//MORTGAGE PAYMENT CAN'T EXCEED 38% OF MONTHLY INCOME
var maxIncomePmt = monthlyIncome * .38;

//MORTGAGE PAYMENT PLUS DEBT PMTS CAN'T EXCEED 48% OF MONTHLY INCOME
var maxDebtToIncomePmt = .48 * monthlyIncome - eval(VmonthlyDebtPmts);

//USE THE LOWER OF 38% OR 48% 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 Total Monthly Income or lower your Total 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 / .00;

//COMPUTE MAXIMUM LOAN AMOUNT BASED ON DOWN PAYMENT
var maxDownPayLoan = maxDownPayPrice * .100;
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 = 360;
    if(form.term.selectedIndex == 0) {
       Vterm = 360;
       } else
       if(form.term.selectedIndex == 1) {
       Vterm = 180;
       } else
       if(form.term.selectedIndex == 2) {
       Vterm = 240;
       } else
       if(form.term.selectedIndex == 3) {
       Vterm = 480;
       }

//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;
var finalDownPay = VdownPmt;

//ADDED FOR ROUNDING TO $100
finalDownPay = (finalDownPay - (finalDownPay % 100));
finalMaxLoanAmt = (finalMaxLoanAmt - (finalMaxLoanAmt % 100));

 var factor2 = 1;

    for (var j2 = 0; j2 < Vterm; j2++) {

        factor2 = factor2 * (eval(1) + eval(VintRate));
        }


    maxMoPmt = (finalMaxLoanAmt * factor2 * VintRate) / (eval(factor2) - eval(1));

//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 = ("Based upon your Information, you could qualify for a house that costs " + form.homePrice.value + ".  This amount is a high estimate and your actual amount may vary.  Call us at (800) 870-7613 for more information.")

//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 = ("Your Total Monthly Income from all sources.  This is the amount before taxes are deducted.");
}

function skipTo(form) {
form.enterHelp.value = "";
form.moDebts.focus();
}

function help02(form) {
document.mortCalc.enterHelp.value = ("The Total of your non-mortgage Monthly Debt Payments.  This would include car loans, student loans, credit card payments, etc.");
}

function help03(form) {
document.mortCalc.enterHelp.value = ("The Total Amount available for your Down Payment.");
}

function help04(form) {
document.mortCalc.enterHelp.value = ("The Annual Interest Rate you expect to pay on this mortgage.");
}

function help05(form) {
document.mortCalc.enterHelp.value = ("The Estimated Monthly Insurance Payment.  As a general rule of thumb, you can expect to pay around 1% of the purchase price per year.");
}

function help06(form) {
document.mortCalc.enterHelp.value = ("The Estimated Monthly Property Tax.  As a general rule of thumb, you can expect to pay around 2% of the purchase price per year.");
}

function help07(form) {
document.mortCalc.enterHelp.value = ("Length of Mortgage.");
}


//GIVE RESULT EXPLANATIONS
function help08(form) {
document.mortCalc.resultHelp.value = ("This is your Original Down Payment Amount.");
}

function help09(form) {
document.mortCalc.resultHelp.value = ("This is the Maximum Home Loan you would qualify for based on your current information.");
}

function help10(form) {
document.mortCalc.resultHelp.value = ("This is Home Price you could afford - the total of your Down Payment and your Maximum Home Loan amount.");
}

function help11(form) {
document.mortCalc.resultHelp.value = ("This is your Monthly Mortgage Payment based upon your current information.");
}

function helpClear(form) {
document.mortCalc.resultHelp.value = "";
}


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();
}
