var cache = '';
var payment = 0;

function check_int ( field ) {
  var num = field.value;
  if ( isNaN ( num ) ) {
    return false;
  } else {
    field.value = '' + parseInt ( num );
    return true;
  }
}

function check_dollar ( field ) {
  var num = get_dollar ( field );
  if ( isNaN ( num ) ) {
    return false;
  } else {
    field.value = float_to_dollar ( num );
    return true;
  }
}

function check_float ( field ) {
  var num = field.value;
  if ( num.indexOf ( "." ) != -1 ) {
    while ( num.charAt ( num.length - 1 ) == "0" ) {
      num = num.substring ( 0, num.length - 1 );
    }
    if ( num.charAt ( num.length - 1 ) == "." ) {
      num = num.substring ( 0, num.length - 1 );
    }
  }
  if ( "" + parseFloat ( num ) != num ) {
    return false;
  } else {
    return true;
  }
}

function validate ( theForm ) {
  var err_str = 'Improper value in field(s):';
  var errno = 0;
  if ( !check_dollar ( theForm.price ) ) {
    err_str += " Price\n";
    errno = 1;
  }
  if ( !check_dollar ( theForm.dp ) ) {
    err_str += " Down Payment\n";
    errno = 1;
  }
  if ( !check_float ( theForm.dp_pct ) ) {
    err_str += " Down Payment Percent\n";
    errno = 1;
  }
  if ( !check_int ( theForm.months ) ) {
    err_str += " Loan Term (months)\n";
    errno = 1;
  }
  if ( !check_float ( theForm.years ) ) {
    err_str += " Loan Term (years)\n";
    errno = 1;
  }
  if ( !check_float ( theForm.rate ) ) {
    err_str += " Annual Interest Rate\n";
    errno = 1;
  }
  if ( errno ) {
    alert ( err_str );
    return false;
  }
  return true;
}

function get_dollar ( field ) {
  var string = field.value;
  if ( string.charAt ( 0 ) == "$" ) {
    string = string.substring ( 1, string.length );
  }

  var posit = string.lastIndexOf ( "," );
  while ( posit != -1 ) {
    string = string.substring ( 0, posit ) + string.substring ( posit + 1, string.length );
    posit = string.lastIndexOf ( ",", posit );
  }
  return parseFloat ( string );
}

function float_to_dollar ( num ) {
  var string = "" + Math.round ( num );
  posit = string.length;
  posit -= 4;
  while ( posit >= 0 ) {
    string = string.substring ( 0, posit + 1 ) + "," + string.substring ( posit + 1, string.length );
    posit -= 3;
  }

  return string;
}

function y_to_m ( theForm ) {
  var yrs = parseFloat ( theForm.years.value );
  var mos = Math.round ( yrs * 12.0 );
  yrs = parseFloat ( mos ) / 12.0;
  theForm.months.value = "" + mos;
  theForm.years.value = "" + yrs;
}

function m_to_y ( theForm ) {
  var mos = parseFloat ( theForm.months.value );
  var yrs = Math.round ( mos / 12.0 );
  theForm.months.value = "" + mos;
  theForm.years.value = "" + yrs;
}

function calc_payment ( theForm ) {
  // get data from the form
  var price = get_dollar ( theForm.price );
  var dp = get_dollar ( theForm.dp );
  var rate = parseFloat ( theForm.rate.value );
  var term = parseInt ( theForm.months.value );
  //var payment = 0;
  
  // calc initial values
  var amt = price - dp;
  //alert ( 'amt: ' + amt + '\nprice: ' + price + '\ndp: ' + dp + '\nterm: ' + term );
  var mon_rate = rate / ( 12.0 * 100 );

  
  if ( mon_rate == 0 ) {
    payment = amt / term;
  } else {
    payment = amt * ( mon_rate / ( 1 - Math.pow ( ( 1 + mon_rate ), ( -1 * term ) ) ) );
  }
	theForm.payment.value = "$" +float_to_dollar(payment);
}

function calc_dp ( theForm ) {
  var price = get_dollar ( theForm.price );
  var dp = get_dollar ( theForm.dp );
  var rate = parseFloat ( theForm.rate );
  var term = parseInt ( theForm.months );
  var payment = get_dollar ( theForm.payment );
  var p2 = 0;
  var new_dp = 0;
  // calc initial values
  var amt = price - dp;
  var mon_rate = rate / ( 12.0 * 100 );
  
  if ( mon_rate == 0 ) {
    p2 = payment * term;
  } else {
    p2 = payment * ( ( 1 - Math.pow ( ( 1 + mon_rate ), ( -1 * term ) ) ) / mon_rate );
  }
  new_dp = dp + ( amt - p2 );
  theForm.dp.value =new_dp;
  check_dollar ( theForm.dp );

  calc_dp_pct ( theForm );
  calc_cp_amt ( theForm );
}

function calc_dp_pct ( theForm ) {
  var price = get_dollar ( theForm.price );
  var dp = get_dollar ( theForm.dp );
  var dp_pct = 0;
  var dp_pct_str = "";
  var posit = 0;

  dp_pct = ( dp / price ) * 100;
  if ( dp_pct >= 0 && dp_pct <= 100 ) {
    dp_pct_str += "" + dp_pct;
    posit = dp_pct_str.indexOf ( "." );
    if ( dp_pct_str.length > ( posit + 4 ) ) {
      dp_pct_str = ( dp_pct_str.substring ( 0, posit + 4 ) );
    }
    theForm.dp_pct.value = "" + dp_pct_str;
  } else if ( dp_pct < 0 ) {
    theForm.dp_pct.value = "0";
    calc_dp_amt ( theForm );
  } else {
    theForm.dp_pct.value = "100";
    calc_dp_amt ( theForm );
  }
}

function calc_dp_amt ( theForm ) {
  var price = get_dollar ( theForm.price );
  var dp_pct = parseFloat ( theForm.dp_pct.value );
  var dp = 0;
  
  if ( dp_pct < 0 ) {
    theForm.dp_pct.value = "0";
    calc_dp_amt ( theForm );
  } else if ( dp_pct > 100 ) {
    theForm.dp_pct.value = "100";
    calc_dp_amt ( theForm );
  } else {
    dp = ( dp_pct / 100 ) * price;
    dp = float_to_dollar ( dp );
    theForm.dp.value = "" + dp;
  }
}

function on_change_price ( theForm ) {
  if ( validate ( theForm ) ) {
    if ( get_dollar ( theForm.price ) < get_dollar ( theForm.dp ) ) {
      theForm.dp.value = theForm.price.value;
      calc_dp_pct ( theForm );
    } else {
      calc_dp_pct ( theForm );
    }
    calc_payment ( theForm );
  } else {
    theForm.price.value = "$" + cache;
  }
}

function on_change_dp ( theForm ) {
  if ( validate ( theForm ) ) {
    calc_dp_pct ( theForm );
    calc_payment ( theForm );
  } else {
    theForm.dp.value = "$" + cache;
  }
}

function on_change_dp_pct ( theForm ) {
  if ( validate ( theForm ) ) {
    calc_dp_amt ( theForm );
    calc_payment ( theForm );
  } else {
    theForm.dp_pct.value = cache;
  }
}

function on_change_months ( theForm ) {
  if ( validate ( theForm ) ) {
    m_to_y ( theForm );
    calc_payment ( theForm );
  } else {
    theForm.months.value = cache;
  }
}

function on_change_years ( theForm ) {
  if ( validate ( theForm ) ) {
    y_to_m ( theForm );
    calc_payment ( theForm );
  } else {
    theForm.years.value = cache;
  }
}

function on_change_rate ( theForm ) {
  if ( validate ( theForm ) ) {
    calc_payment ( theForm );
  } else {
    theForm.rate.value = cache;
  }
}

function on_change_payment ( theForm ) {
  if ( validate ( theForm ) ) {
    calc_payment ( theForm );
  } else {
    theForm.payment.value = cache;
  }
}

function set_cache ( field ) {
  cache = field.value;
}