function FrontPage_Form1_Validator(theForm)
{

  if (theForm.Income.value == "")
  {
    alert("Please enter a value for the \"Income\" field.");
    theForm.Income.focus();
    return (false);
  }

  if (theForm.Income.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"Income\" field.");
    theForm.Income.focus();
    return (false);
  }

  if (theForm.Income.value.length > 6)
  {
    alert("Please enter at most 6 characters in the \"Income\" field.");
    theForm.Income.focus();
    return (false);
  }

  var checkOK = "0123456789-.,";
  var checkStr = theForm.Income.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Income\" field.");
    theForm.Income.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the \"Income\" field.");
    theForm.Income.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "999999"))
  {
    alert("Please enter a value greater than or equal to \"0\" and less than or equal to \"999999\" in the \"Income\" field.");
    theForm.Income.focus();
    return (false);
  }

  var radioSelected = false;
  for (i = 0;  i < theForm.JointLoan.length;  i++)
  {
    if (theForm.JointLoan[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {
    alert("Please select one of the \"Single\" options.");
    return (false);
  }

  if (theForm.ExtraSecurity.value.length > 7)
  {
    alert("Please enter at most 7 characters in the \"ExtraSecurity\" field.");
    theForm.ExtraSecurity.focus();
    return (false);
  }

  var checkOK = "0123456789-.,";
  var checkStr = theForm.ExtraSecurity.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"ExtraSecurity\" field.");
    theForm.ExtraSecurity.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the \"ExtraSecurity\" field.");
    theForm.ExtraSecurity.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "9999999"))
  {
    alert("Please enter a value greater than or equal to \"0\" and less than or equal to \"9999999\" in the \"ExtraSecurity\" field.");
    theForm.ExtraSecurity.focus();
    return (false);
  }

  if (theForm.Deposit.value == "")
  {
    alert("Please enter a value for the \"Deposit\" field.");
    theForm.Deposit.focus();
    return (false);
  }

  if (theForm.Deposit.value.length < 1)
  {
    alert("Please enter at least 1 characters in the \"Deposit\" field.");
    theForm.Deposit.focus();
    return (false);
  }

  if (theForm.Deposit.value.length > 6)
  {
    alert("Please enter at most 6 characters in the \"Deposit\" field.");
    theForm.Deposit.focus();
    return (false);
  }

  var checkOK = "0123456789-.,";
  var checkStr = theForm.Deposit.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"Deposit\" field.");
    theForm.Deposit.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the \"Deposit\" field.");
    theForm.Deposit.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseFloat(allNum);
  if (chkVal != "" && !(prsVal >= "0" && prsVal <= "999999"))
  {
    alert("Please enter a value greater than or equal to \"0\" and less than or equal to \"999999\" in the \"Deposit\" field.");
    theForm.Deposit.focus();
    return (false);
  }

  var checkOK = "0123456789-.,";
  var checkStr = theForm.MaxLoan.value;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    if (ch == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch != ",")
      allNum += ch;
  }
  if (!allValid)
  {
    alert("Please enter only digit characters in the \"MaxLoan\" field.");
    theForm.MaxLoan.focus();
    return (false);
  }

  if (decPoints > 1)
  {
    alert("Please enter a valid number in the \"MaxLoan\" field.");
    theForm.MaxLoan.focus();
    return (false);
  }
  return (true);
}
function floor(number)
{
        return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function calc()
{
        if (!FrontPage_Form1_Validator(document.FrontPage_Form1))
        {
                document.FrontPage_Form1.MaxLoan.value = 0;
                document.FrontPage_Form1.Suggestions.value = "";
                return;
        }

   var MaxLTV = 0.95;
        var IntRate = 0.094;

        var MaxRTI = 0.0;
        if (document.FrontPage_Form1.JointLoan[0].checked)
                MaxRTI = 0.30;
        else
                MaxRTI = 0.35;

        var Deposit = 1.0 * document.FrontPage_Form1.Deposit.value;
   var ExtraSecurity = 1.0 * document.FrontPage_Form1.ExtraSecurity.value;
   var Income = 1.0 * document.FrontPage_Form1.Income.value;
   var Term = 1 * document.FrontPage_Form1.Term.value;
          
   var MaxLoanByDeposit = MaxLTV * (Deposit+ExtraSecurity)/(1-MaxLTV);

        var MaxPmt = MaxRTI*Income;

        var MaxLoanByPmt = MaxPmt * (1 - Math.pow(1+IntRate/12,-Term*12)) / (IntRate/12);

        document.FrontPage_Form1.MaxLoan.value = floor(Math.min(MaxLoanByDeposit, MaxLoanByPmt));
        if (MaxLoanByDeposit < MaxLoanByPmt)
                document.FrontPage_Form1.Suggestions.value = "You could afford to borrow more if you were able to increase your deposit.";
        else
                document.FrontPage_Form1.Suggestions.value = "You could borrow more if you were able to show any additional income, such as rental or investment income.";
        return;
}
