
<!--
//CLOSING COST VARIABLES
var closingCosts, loanAmt, loanTerm, interestRate, discountPoints, downPayment = "";

//RESULTS VARIABLES
var mortCosts, monthPay, refiPay, payDiff, breakMonth = "";


function PMICalculator (loanAmt, downPaymentAmt)
{
  var loanAmt = document.refinancecalculator.refinance_new_loanamt.value;
  var salesPrice = document.refinancecalculator.refinance_current_value.value;

  var LTV = loanAmt/salesPrice;
  var PMIRate = true;
  var LTVThreshold = 0.8;
  var monthlyMI = 0;

  if (LTV < LTVThreshold)
	{
        PMIRate = false;
    }

  if (PMIRate)
	 {
		if ((LTV < 0.85)||(LTV == 0.85))
		  {
			PMIRate = 0.0032;
		  }
		else if ((LTV < 0.90)||(LTV == 0.90))
		  {
			PMIRate = 0.0052;
		  }
		else if ((LTV < 0.95)||(LTV == 0.95))
		  {
			PMIRate = 0.0078;
		  }
		else
		  {
			PMIRate = 0.0090;
		  }

        monthlyMI = loanAmt * PMIRate/12;
	}

  return monthlyMI;
}

function closingCostCaculate(loanAmt, loanTerm, interestRate, programPoints, downPaymentAmt)
{
  //Default properties per landscape
  var taxServiceFee = new Number(75);
  var feeToCharge = new Number(350);
  var creditReportFee = new Number(20.7);
  var appraisalFee = new Number(300);
  var floodCertFee = new Number(15.5);
  var interestDays = new Number(15);
  var prePaidInterest = new Number((interestRate / 365) * loanAmt * interestDays);
  var originationFee = new Number(0);

  if ((programPoints > 0.01)||(programPoints == 0.01))
    {
  		originationFee = 0.01 * loanAmt;
    }

  var discountPoints = new Number(0);
  if (programPoints > 0.01)
    {
      discountPoints = (programPoints - 0.01) * loanAmt;
    }
  else if ((programPoints > 0)&&(programPoints < 0.01))
    {
      discountPoints = programPoints * loanAmt;
    }

  var paidByLender = new Number(0);
  if (programPoints < 0.0)
    {
      paidByLender = programPoints * loanAmt;
    }

  var monthlyMI = PMICalculator(loanAmt, downPaymentAmt);
  var MIFee = new Number(monthlyMI * 2);
  var closingCosts = new Number(taxServiceFee + feeToCharge + creditReportFee + appraisalFee + floodCertFee + paidByLender + discountPoints + originationFee + prePaidInterest + MIFee)
  return(closingCosts);
}

function calculate_refi()
{
  var mi = document.refinancecalculator.refinance_new_rate.value / 1200;
  var base = 1;
  var mbase = 1 + mi;
  for (i=0; i<document.refinancecalculator.refinance_new_term.value * 12; i++)
  {
    base = base * mbase;
  }

//additional var definitions for calculating closing cost
  loanAmt = document.refinancecalculator.refinance_new_loanamt.value;
  loanTerm = document.refinancecalculator.refinance_new_term.options[document.refinancecalculator.refinance_new_term.selectedIndex].value * 12;
  interestRate = document.refinancecalculator.refinance_new_rate.value / 100;
  programPoints = 0.00;
  discountPoints = programPoints;
  downPayment = document.refinancecalculator.refinance_current_value.value - loanAmt;
  closingCosts = floor(closingCostCaculate(loanAmt, loanTerm, interestRate, programPoints, downPayment));

  //These calculations are used in the results section and apply the values to JavaScript variables (as strings)
  mortCosts = closingCosts.toString();
  monthPay = document.refinancecalculator.refinance_current_pi.value.toString();
  refiPay = floor(document.refinancecalculator.refinance_new_loanamt.value * mi / ( 1 - (1/base))).toString();
  payDiff = floor(document.refinancecalculator.refinance_current_pi.value - document.refinancecalculator.refinance_new_loanamt.value * mi / ( 1 - (1/base))).toString();
  var monthb = eval(mortCosts / payDiff);
  breakMonth = parseInt(monthb);

  // now display the results!
  document.getElementById('mortCosts').innerHTML = "$" + mortCosts;
  document.getElementById('monthPay').innerHTML = "$" + monthPay;
  document.getElementById('refiPay').innerHTML = "$" + refiPay;
  document.getElementById('payDiff').innerHTML = "$" + payDiff;
  document.getElementById('breakMonth').innerHTML = breakMonth;

  document.getElementById('instructions').style.display="none";
  document.getElementById('results').style.display="block";

  // alert("mcosts" + mortCosts);
  return false;

 }

//Browsercheck
var ie = document.all?1:0;
var n = document.layers?1:0;
//This checks for Netscape 6.2 or Greater
var gecko = ((navigator.appName == "Netscape")&&((navigator.appVersion.substring(0,3)==5)||(navigator.appVersion.substring(0,3)>5)))?1:0;

function makeChangeResultsObj(obj)
{
    this.writeref = (n) ? eval('document.'+ obj +'.document') : eval(obj);
    this.writeIt = b_writeIt;
}

function b_writeIt(text)
{
  if(n)
    {
      this.writeref.write(text);
      this.writeref.close();
    }
  else if(ie)
    {
     this.writeref.innerHTML=text;
    }

  //ADDED FOR 6.X COMPLIANCE - NEW CHANGE METHOD
  else if(gecko)
    {
     this.innerHTML=text;
    }
}

function showResults()
{
  mortCosts = commasDecimals(mortCosts);
  monthPay = commasDecimals(monthPay);
  refiPay = commasDecimals(refiPay);
  payDiff = commasDecimals(payDiff);
  //alert('called');
	//BUILDS RESULTS
	var changeMsg =
	"<TABLE CELLPADDING=\"0\" CELLSPACING=\"0\" WIDTH=\"0\">" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD style=\"background-color\:#ffffff\" CLASS=\"inside\" COLSPAN=\"5\" HEIGHT=\"30\" VALIGN=\"MIDDLE\"><span class=\"headers\" id=\"learnTheme\">Results</span></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\"><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"5\"></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\" HEIGHT=\"30\" VALIGN=\"MIDDLE\">Based on the information you provided above:</TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\"><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"10\"></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"2\" VALIGN=\"MIDDLE\">Estimated costs to obtain a new mortgage:</TD>" +
	"    <TD>&nbsp;</TD>" +
	"    <TD COLSPAN=\"2\" ALIGN=\"CENTER\" VALIGN=\"MIDDLE\">" + "<B>" + "$" + mortCosts + "</B>" + "</TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\"><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br><img src=\"/images/misc/learn-hr-calculator.gif\" width=\"431\" height=\"1\" border=\"0\" alt=\"\" \/><br><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"2\" VALIGN=\"MIDDLE\">Current Monthly Payment:</TD>" +
	"    <TD>&nbsp;</TD>" +
	"    <TD COLSPAN=\"2\" ALIGN=\"CENTER\" VALIGN=\"MIDDLE\">" + "<B>" + "$" + monthPay + "</B>" + "</TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\"><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br><img src=\"/images/misc/learn-hr-calculator.gif\" width=\"431\" height=\"1\" border=\"0\" alt=\"\" \/><br><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"2\" VALIGN=\"MIDDLE\">Refinance Payment:</TD>" +
	"    <TD>&nbsp;</TD>" +
	"    <TD COLSPAN=\"2\" ALIGN=\"CENTER\" VALIGN=\"MIDDLE\">" + "<B>" + "$" + refiPay + "</B>" + "</TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\"><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br><img src=\"/images/misc/learn-hr-calculator.gif\" width=\"431\" height=\"1\" border=\"0\" alt=\"\" \/><br><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"2\" VALIGN=\"MIDDLE\">How much lower the new payment would be than the current one:</TD>" +
	"    <TD>&nbsp;</TD>" +
	"    <TD COLSPAN=\"2\" ALIGN=\"CENTER\" VALIGN=\"MIDDLE\">" + "<B>" + "$" + payDiff + "</B>" + "</TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"5\"><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br><img src=\"/images/misc/learn-hr-calculator.gif\" width=\"431\" height=\"1\" border=\"0\" alt=\"\" \/><br><IMG SRC=\"/images/spacer.gif\" WIDTH=\"1\" HEIGHT=\"3\"><br></TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD COLSPAN=\"2\" VALIGN=\"MIDDLE\">Months to break even:</TD>" +
	"    <TD>&nbsp;</TD>" +
	"    <TD COLSPAN=\"2\" ALIGN=\"CENTER\" VALIGN=\"MIDDLE\">" + "<B>" + breakMonth + " " + "month(s) </B>" +  "</TD>" +
	"  </TR>" +
	"  <TR ALIGN=\"left\" VALIGN=\"top\">" +
	"    <TD><IMG SRC=\"/images/spacer.gif\" WIDTH=\"22\" HEIGHT=\"1\"></TD>" +
	"    <TD><IMG SRC=\"/images/spacer.gif\" WIDTH=\"150\" HEIGHT=\"1\"></TD>" +
	"    <TD><IMG SRC=\"/images/spacer.gif\" WIDTH=\"5\" HEIGHT=\"1\"></TD>" +
	"    <TD><IMG SRC=\"/images/spacer.gif\" WIDTH=\"22\" HEIGHT=\"1\"></TD>" +
	"    <TD><IMG SRC=\"/images/spacer.gif\" WIDTH=\"231\" HEIGHT=\"1\"></TD>" +
	"  </TR>" +
	"</TABLE>";

    if(ie || n)
    {
      oMessage=new makeChangeResultsObj('objResults');
      oMessage.writeIt(changeMsg);
    }

      //ADDED FOR 6.X COMPLIANCE - NEW CHANGE METHOD
    else if (gecko)
    {
      var results = document.getElementById("objResults");
      results.writeIt = b_writeIt;
      results.writeIt(changeMsg);
    }
}


function isBlank(data)
{
  if (data.length == 0)
      return 1;

  for (i = 0; i < (data.length); i++)
    {
      if (data.charAt(i) != " ")
        return 0;
    }

  return 1;
}

function isNumOnly(data)
{
  var i = new Number(data.length);
  var j = new Number(0);
  var k = new Number();

  for (j=0; j<i; j++)
    {
      if ((isNaN(data.charAt(j)) == true))
	    return 0;
    }
    return 1;
}

function isInvalidCharacter(data)
{
  for (i=0; i<data.length; i++)
    {
      if ((data.charAt(i)=="~")||(data.charAt(i)=="!")||(data.charAt(i)=="@")||(data.charAt(i)=="#")
        ||(data.charAt(i)=="$")||(data.charAt(i)=="%")||(data.charAt(i)=="^")||(data.charAt(i)=="&")
        ||(data.charAt(i)=="*")||(data.charAt(i)=="(")||(data.charAt(i)==")")||(data.charAt(i)=="_")
        ||(data.charAt(i)=="+")||(data.charAt(i)=="=")||(data.charAt(i)=="{")||(data.charAt(i)=="[")
        ||(data.charAt(i)=="|")||(data.charAt(i)=="<")||(data.charAt(i)==",")||(data.charAt(i)==">")
        ||(data.charAt(i)=="}")||(data.charAt(i)=="]")||(data.charAt(i)=="?")||(data.charAt(i)=="/"))
        {
          return 1;
        }
    }
  return 0;
}


function isAlphaOnly(data, characterTest, spaceTest)
{
  i = new Number(data.length);
  j = new Number(0);

  if (characterTest == 0)
    {
      if (isInvalidCharacter(data) == 1)
          return 0;
    }

  if (spaceTest == 0)
    {
      for (j=0; j<i; j++)
        {
          if (data.charAt(j) == " ")
              return 0;
        }
    }

  for (j=0; j<i; j++)
    {
      if (isNaN(data.charAt(j)) == false)
	  return 0;
    }
  if ((j==i)&&(i!=0))
      return 1;
}


function isExactLength(data, num)
{

  var howLong = new Number(data.length);
  var rightLength = new Number(num);

  return ((howLong < rightLength)||(howLong > rightLength));
}


function isMinAndMaxLength(data, min, max)
{
  var minimum = new Number(min);
  var maximum = new Number(max);

  return ((data.length < minimum) || (data.length > maximum));
}


function isBetweenMinAndMax(data, min, max)
{
  return ((data < min)||(data > max));
}


function checkForm(form)
{
  var errorMsg = "";
  var i;
  var message = new Array();
  var inputs = new Array(form.length);

  var currentloan, currentpi, currentrate, currentvalue, newterm, newrate, newamount = "";

  for (i = 0; i < form.length; i++)
       inputs[i] = form.elements[i].name;

  for (i = 0; i < form.length; i++)
    {
      if (inputs[i] == "refinance_current_loan")
          currentloan = i;
      else if (inputs[i] == "refinance_current_rate")
          currentrate = i;
      else if (inputs[i] == "refinance_current_pi")
          currentpi = i;

      else if (inputs[i] == "refinance_current_value")
          currentvalue = i;
      else if (inputs[i] == "refinance_new_term")
          newterm = i;
      else if (inputs[i] == "refinance_new_rate")
          newrate = i;
      else if (inputs[i] == "refinance_new_loanamt")
          newamount = i;
    }
  message[0] = "Please enter your current loan balance.\n";
  message[1] = "Your current loan balance should be free of letters or special characters.\n";
  message[2] = "Your current loan balance should be greater than $0 and less than $1,000,000,000.\n";

  message[3] = "Please enter your current principal and interest.\n";
  message[4] = "The current principal and interest of your loan should be free of letters and special characters.\n";
  message[5] = "The current principal and interest of your loan should be less than $1,000,000,000.\n";

  message[6] = "Please enter your current interest rate.\n";
  message[7] = "Your current interest rate value should be free of letters and special characters.\n";
  message[8] = "Your current interest rate value should be greater than 0% and less than 51%.\n";

  message[9] = "Please enter the amount of the new loan.\n";
  message[10] = "The new loan amount should be free of letters or special characters.\n";
  message[11] = "The  new loan amount should be greater than $0 and less than $1,000,000,000.\n";

  message[12] = "Please enter your new interest rate.\n";
  message[13] = "Your new interest rate value should be free of letters and special characters.\n";
  message[14] = "Your new interest rate value should be greater than 0% and less than 51%.";

  message[15] = "Please enter the estimated value of your property.\n";
  message[16] = "The estimated value of your property should be free of letters or special characters.\n";
  message[17] = "The estimated value of your property should be greater than $0 and less\n" +  "than $1,000,000,000.\n";


  if (isBlank(form.elements[currentloan].value) == 1)
      errorMsg += message[0];
  else if (isNaN(form.elements[currentloan].value) == true)
      errorMsg += message[1];
  else if ((form.elements[currentloan].value <= 0)||(form.elements[currentloan].value >=1000000000))
      errorMsg += message[2];
  else
      var cloan = new Number(form.elements[currentloan].value);

  if (isBlank(form.elements[currentpi].value) == 1)
      errorMsg += message[3];
  else if (isNaN(form.elements[currentpi].value) == true)
      errorMsg += message[4];
  else if ((form.elements[currentpi].value <= 0)||(form.elements[currentpi].value >=1000000000))
      errorMsg += message[5];
  else
      var cloan = new Number(form.elements[currentloan].value);


  if (isBlank(form.elements[currentrate].value) == 1)
      errorMsg += message[6];
  else if (isNaN(form.elements[currentrate].value) == true)
      errorMsg += message[7];
  else if ((form.elements[currentrate].value <= 0)||(form.elements[currentrate].value >=51))
      errorMsg += message[8];


  if (isBlank(form.elements[currentvalue].value) == 1)
      errorMsg += message[15];
  else if (isNumOnly(form.elements[currentvalue].value) == 0)
      errorMsg += message[16];
  else if ((form.elements[currentvalue].value <= 0)||(form.elements[currentvalue].value >=1000000000))
      errorMsg += message[17];
  else
      var cvalue = new Number(form.elements[currentvalue].value);


  if (isBlank(form.elements[newrate].value) == 1)
      errorMsg += message[12];
  else if (isNaN(form.elements[newrate].value) == true)
      errorMsg += message[13];
  else if ((form.elements[newrate].value <= 0)||(form.elements[newrate].value >= 51))
      errorMsg += message[14];


  if (isBlank(form.elements[newamount].value) == 1)
      errorMsg += message[9];
  else if (isNaN(form.elements[newamount].value) == true)
      errorMsg += message[10];
  else if ((form.elements[newamount].value <= 0)||(form.elements[newamount].value >= 1000000000))
      errorMsg += message[11];
  else
      var nloan = new Number(form.elements[newamount].value);

//No need to return anything here
  if (errorMsg.length == 0)
    {
//If there's no error message, calculate
      calculate();
      showResults();
      return true;
    }
  else
    {
//If there's an error message, just show it
      alert("Before continuing, the following fields require attention:\n\n" + errorMsg);
      return false;
    }
}

//-->
