<!--

function round(x)
{
  return Math.round(x*100)/100;
}

function invalid_number(num,lower,upper)
{
  // empty fields, non-digits, and numbers outside of our range should return invalid
  if ( (num == "") || (isNaN(num)) || (num < lower) || (num > upper) )
    {
      return 1;
    }
  else
    {
      return 0;  
    }
}

function CalculatePayment() 
{
  // remove nondigits
  var rate;
  var price;
  var percent_down;
  var num_years;
  var interest_rate;


  price = document.mortgage.price.value;
  percent_down = document.mortgage.percent_down.value;
  num_years = document.mortgage.num_years.value;
  interest_rate = document.mortgage.interest_rate.value;


  if (invalid_number(price,0,999999999999))
    {
      price = 0;
    }

  if (invalid_number(percent_down,0,100))
    {
      percent_down = 0;
    }

  if (invalid_number(num_years,0,100))
    {
      num_years = 0;
    }

  if (invalid_number(interest_rate,0,100))
    {
      interest_rate = 0;
    }

  percent_down = percent_down / 100.00;
  principal = price - (price * percent_down);
  num_payments = num_years * 12.00;
  irate = interest_rate / 100 / 12.00;
  MonthlyPayment = (principal * irate) / (1 - Math.pow((1 + irate), (-1 * num_payments)));

  document.mortgage.payment.value = round(MonthlyPayment);
  document.mortgage.totalpayments.value = round(MonthlyPayment * num_payments);
  document.mortgage.totalinterest.value = round(MonthlyPayment * num_payments - (price - (price * percent_down)));

  }

//-->
