/* 
	Function PMT
	ARG1 = Interest Rate
	ARG2 = Amort in Months
	ARG3 = Loan Amount
	ARG4 = Unused
*/
function PMT(arg1, arg2, arg3, arg4) {
  var result;
  result = new Number(0);

  result = 1 + arg1 / 1200;
  result = Math.pow(result, (-arg2));
  result = 1 - result;
  result = (arg3 * arg1 / 1200) / result;

  return result;
}

/* 
	Function PV
	ARG1 = Interest Rate
	ARG2 = Amort in Months
	ARG3 = PMT value
	ARG4 = FV value
*/
function PV(arg1, arg2, arg3, arg4) 
{
  var result, temp2;
  result = new Number(0);
  temp2  = new Number(0);

  result = 1 + arg1 / 1200;
  result = Math.pow(result, (arg2));

  temp2 = arg1 / 1200;
  temp2 = arg4 + arg3 * ((Math.pow((1+arg1/1200),arg2) - 1) / temp2);

  result = temp2 / result;

  return result;
}

function strPercentageToNum(ln)
{
  var ln2;
  ln2 = new String(ln);

  chr = '';
  result = '';

  for (i=0; i<ln2.length; i++) {
    chr = ln2.charAt(i);

    if(chr == "%") { 
      continue;
    } 
    else {
      result += chr;
    }
  }
  return result;       
}

function strDollarsToNum(ln)
{
  var ln2;
  ln2 = new String(ln);

  chr = '';
  result = '';

  for (i=0; i<ln2.length; i++) {
    chr = ln2.charAt(i);

    if( chr == "," || chr == "$" ) { 
      continue;
    } 
    else {
      result += chr;
    }
  }

  return result;       
}

function strNumToDollars(ln, dec)
{
  var ln2;
  ln2 = new String(ln);
  ln2 = strDollarsToNum(ln2);

  var first = '';
  var last = '';

  var pos = ln2.indexOf(".");
  if(pos == -1) {
    first = ln2;
    if(dec=="0") last = '';
    else last  = '.00';
  } 
  else {
    first = ln2.substring(0, pos); 
    last  = ln2.substring(pos, ln2.length);

    if(dec=="0") {
      last = '';
    }
    else {    
     if(last.length < 3) {
      if(last.length==2) {
        last += "0";
      }
      if(last.length==1) {
        last += "00";
      }   
     }
    }
  }
  if(!first) {
     first += "0";
  }

  result = '';
  j 	 = 0;  

  for (i=first.length; i>0; i--) {
//    result = first.substring(i-1,i) + result;

    result = first.charAt(i-1) + result;
    j+=1;
    if(j>2 && i>1) { 
      if(first.charAt(0)!="-") {
        result = "," + result; 
      } else if (i>2) {
        result = "," + result; 
      }
      j = 0;      
    }

  }

  result = "$" + result + last;  

  return result;       
}

function doFixPerc(input, dec) 
{
  if( !input ) {
    if(dec=="0") input="0";
    else input = "0.00";
  }

  input = strPercentageToNum(input);

  if( isNaN(parseFloat(input)) ) {
    alert("Only numbers allowed for input values.");
    if(dec=="0") return "0";   
    else return "0.00";
  }
  if( input < 0 ) {
    alert("Please enter positive values.");
    if(dec=="0") return "0";
    else return "0.00";
  }

  var first = '';
  var last = '';

  var pos = input.indexOf(".");
  if(pos == -1) {
    first = input;
    if(dec=="0") last  = '';
    else last = '.00';
  } 
  else {
    first = input.substring(0, pos); 
    last  = input.substring(pos, input.length);

    if(last.length < 3) {
      if(last.length==2) {
        last += "0";
      }
      if(last.length==1) {
        last += "00";
      }   
    }
  }
  if(!first) {
     first += "0";
  }

  return first + last;
}

function doAdjust(what, type, dec) 
{
  var value = what.value;
  var v = new String(value);

  if(type==0) {
    value  = doFixPerc(value, dec) + "%";
  }
  if(type==1) {
    if( !value ) {
      if(dec=="0") value = "0";
      else value = "0.00";
    }

    MM = 0;
    if(	v.substring(v.length-2, v.length) == "MM" ||
	v.substring(v.length-2, v.length) == "mm" ) {
	v = v.substring(0, v.length-2);
	v += "000000";
	value = parseInt(v);
	MM = 1;
    }

    if( !MM && (v.substring(v.length-1, v.length) == "M" ||
	v.substring(v.length-1, v.length) == "m")) {
	v = v.substring(0, v.length-1);
	v += "000";
	value = parseInt(v);
    }

    value = strDollarsToNum(value);

    if( isNaN(value) ) {
      alert("Only numbers allowed for input values.");
      if(dec=="0") value = "0";
      else value = "0.00";
    }
    if( value < 0 ) {
      alert("Please enter positive values.");
      if(dec=="0") value = "0";
      else value = "0.00";
    }
    value  = strNumToDollars(value, dec);
  }

  what.value	= value;
}

function doYearMonth() 
{
  term_yr 	= document.form1.input_termyr.value;
  term_mo	= document.form1.input_termmo.value;
  amort_yr	= document.form1.input_amortyr.value;
  amort_mo	= document.form1.input_amortmo.value;
  amortized_yr	= document.form1.input_amortized_yr.value;
  amortized_mo	= document.form1.input_amortized_mo.value;

  if( !term_yr && !term_mo) {
    alert("Please enter a value for Term of Loan.");
    return;
  }
  if( !term_yr ) term_yr = 0;
  if( !term_mo ) term_mo = 0;
  if( !amort_yr && !amort_mo) {
    alert("Please enter a value for Amortization of Loan.");
    return;
  }
  if( !amort_yr ) amort_yr = 0;
  if( !amort_mo ) amort_mo = 0;
  if( !amortized_yr && !amortized_mo) {
    alert("Please enter a value for Amortization Term to Maturity.");
    return;
  }
  if( !amortized_yr ) amortized_yr = 0;
  if( !amortized_mo ) amortized_mo = 0;

  if( isNaN(parseFloat(term_yr)) || isNaN(parseFloat(term_mo)) ||
      isNaN(parseInt(amort_yr)) || isNaN(parseInt(amort_mo)) ||
      isNaN(parseInt(amortized_yr)) || isNaN(parseInt(amortized_mo))) {
    alert("Only numbers allowed for input values.");
    return;
  }

  TermMo  = parseInt(term_yr) * 12 + parseInt(term_mo);
  AmortMo = parseInt(amort_yr) * 12 + parseInt(amort_mo);
  AmortizedMo = parseInt(amortized_yr) * 12 + parseInt(amortized_mo);

  if( TermMo <= 0 ) {
    alert("Please enter positive Term values.");
    document.form1.input_termyr.value	= 1;
    document.form1.input_termmo.value	= 0;
    return;
  }
  if( AmortMo <= 0 ) {
    alert("Please enter positive Amortization values.");
    document.form1.input_amortyr.value	= 1;
    document.form1.input_amortmo.value	= 0;
    return;
  }
  if( AmortizedMo <= 0 ) {
    alert("Please enter positive Amortized values.");
    document.form1.input_amortized_yr.value	= 1;
    document.form1.input_amortized_mo.value	= 0;
    return;
  }

  if( TermMo > 360 ) {
    alert("That exceeds the maximum for Term to Maturity. Max=30yrs");
    document.form1.input_termyr.value	= 30;
    document.form1.input_termmo.value	= 0;
    return;
  }
  if( AmortMo > 360 ) {
    alert("That exceeds the maximum for Amortization Term. Max=30yrs");
    document.form1.input_amortyr.value	= 30;
    document.form1.input_amortmo.value	= 0;
    return;
  }
  if( TermMo >= AmortMo ) {
    alert("Term to Maturity must be lower than Amortization");
    document.form1.input_termyr.value	= Math.floor(AmortMo / 12)-1;
    document.form1.input_termmo.value	= AmortMo % 12;
    return;
  }
  if( AmortizedMo > TermMo ) {
    alert("That exceeds the Term to Maturity. Max=Term");
    document.form1.input_amortized_yr.value	= Math.floor(TermMo / 12);
    document.form1.input_amortized_mo.value	= TermMo % 12;
    return;
  }

  term_yr	= Math.floor(TermMo / 12);
  term_mo  	= TermMo % 12;
  amort_yr	= Math.floor(AmortMo / 12);
  amort_mo	= AmortMo % 12;
  amortized_yr	= Math.floor(AmortizedMo / 12);
  amortized_mo	= AmortizedMo % 12;

  document.form1.input_termyr.value	= term_yr;
  document.form1.input_termmo.value	= term_mo;
  document.form1.input_amortyr.value	= amort_yr;
  document.form1.input_amortmo.value	= amort_mo;
  document.form1.input_amortized_yr.value	= amortized_yr;
  document.form1.input_amortized_mo.value	= amortized_mo;
  return;
}


function doCalculations(input_loan, input_termyr, input_termmo, 
                input_amortyr, input_amortmo, input_rate, 
		input_amortized_yr, input_amortized_mo, input_pre_penalty,
	   	input_remain_yield) {

  input_loan  = strDollarsToNum(input_loan);
  input_rate  = strPercentageToNum(input_rate);
  input_pre_penalty = strPercentageToNum(input_pre_penalty);
  input_remain_yield = strPercentageToNum(input_remain_yield);

  if( !input_loan ) {
    alert("Please enter a value for Loan Amount.");
    return;
  }
  if( !input_termyr && !input_termmo) {
    alert("Please enter a value for Term of Loan Year/Months.");
    return;
  }
  if( !input_termyr ) input_termyr = 0;
  if( !input_termmo ) input_termmo = 0;

  if( !input_amortyr && !input_amortmo) {
    alert("Please enter a value for Amortization of Loan Year/Months.");
    return;
  }
  if( !input_amortyr ) input_amortyr = 0;
  if( !input_amortmo ) input_amortmo = 0;

  if( !input_rate ) {
    alert("Please enter a value for Interest Rate.");
    return;
  }
  if( !input_amortized_yr && !input_amortized_mo) {
    alert("Please enter a value for amortized Year/Months.");
    return;
  }
  if( !input_amortized_yr ) input_amortized_yr = 0;
  if( !input_amortized_mo ) input_amortized_mo = 0;

  if( !input_remain_yield ) {
    alert("Please enter a value for Equivalent Yield.");
    return;
  }

  var mo_debt	= 0;

  if( isNaN(parseInt(input_loan)) || isNaN(parseFloat(input_termyr)) || 
      isNaN(parseFloat(input_termmo)) || isNaN(parseInt(input_amortyr)) || 
      isNaN(parseInt(input_amortmo)) || isNaN(parseInt(input_rate)) || 
      isNaN(parseInt(input_amortized_yr)) || isNaN(parseInt(input_amortized_mo)) ||
      isNaN(parseInt(input_remain_yield)) ) {
    alert("Only numbers allowed for input values.");
    return;
  }

  if( parseInt(input_loan) < 0) {
    alert("Please enter a positive Loan Amount.");
    return;
  }
  if( parseInt(input_loan) > 100000000) {
    alert("That exceeds the $100,000,000 Loan Amount.");
    return;
  }

  if( parseInt(input_rate ) < 1) {
    alert("Interest Rate must be minimum 1.00%.");
    return;
  }
  if( input_rate > 20) {
    alert("That exceeds the 20.00% Interest Rate.");
    return;
  }

  if( parseInt(input_pre_penalty ) < 0) {
    alert("Prepayment Penalty must be minimum 0.00%.");
    return;
  }
  if( input_pre_penalty > 20) {
    alert("That exceeds the 20.00% Prepayment Penalty.");
    return;
  }

  if( parseInt(input_remain_yield ) < 1) {
    alert("Equivalent Yield must be minimum 1.00%.");
    return;
  }
  if( input_remain_yield > 20) {
    alert("That exceeds the 20.00% Equivalent Yield.");
    return;
  }


  TermMo  = parseInt(input_termyr) * 12 + parseInt(input_termmo);
  AmortMo = parseInt(input_amortyr) * 12 + parseInt(input_amortmo);
  AmortizedMo = parseInt(input_amortized_yr) * 12 + parseInt(input_amortized_mo);

  if( TermMo <= 0 ) {
    alert("Please enter positive Term values.");
    return;
  }
  if( AmortMo <= 0 ) {
    alert("Please enter positive Amortization values.");
    return;
  }

  if( AmortizedMo <= 0 ) {
    alert("Please enter a positive Months Amortized Year/Month.");
    return;
  }

  RemainMo = TermMo - AmortizedMo;
  if( RemainMo < 0 ) {
    alert("Please enter correct Term or Amortization values.");
    return;
  }

  RemainTerm = AmortMo - TermMo;
  if( RemainTerm <= 0 ) {
    alert("Please enter correct Term or Amortization values.");
    return;
  }

  mo_debt   	= PMT(input_rate, AmortMo, input_loan, 0); 

//  AmortMo	= AmortMo - AmortizedMo;
  AmortMo	= AmortMo - (TermMo - AmortizedMo);

  p_balance	= PV(input_rate, AmortMo, mo_debt, 0);

  AmortMo	= RemainTerm;
  p_balloon	= PV(input_rate, AmortMo, mo_debt, 0);

//  pv_future	= PV(input_remain_yield, RemainMo, mo_debt, 0);
  pv_future	= PV(input_remain_yield, AmortizedMo, mo_debt, 0);

//  pv_balloon    = PV(input_remain_yield, RemainMo, 0, p_balloon);
  pv_balloon    = PV(input_remain_yield, AmortizedMo, 0, p_balloon);

  pv_total      = pv_future + pv_balloon;

  calc_excess   = pv_total - p_balance;
  calc_excess   = Math.round(calc_excess * 100) / 100;

  calc_prepaid  = Math.round(p_balance * input_pre_penalty) / 100;

  if( isNaN(mo_debt) )      mo_debt = 0;
  if( isNaN(p_balance) )    p_balance = 0;
  if( isNaN(p_balloon) )    p_balloon = 0;
  if( isNaN(pv_future) )    pv_future = 0;
  if( isNaN(pv_balloon) )   pv_balloon = 0;
  if( isNaN(pv_total) )     pv_total = 0;
  if( isNaN(p_balance) )    p_balance = 0;
  if( isNaN(calc_excess) )  calc_excess = 0;
  if( isNaN(calc_prepaid) ) calc_prepaid = 0;

  mo_debt   = Math.round(mo_debt * 100) / 100;
  p_balance = Math.round(p_balance * 100) / 100;  
  p_balloon = Math.round(p_balloon * 100) / 100;
  pv_future = Math.round(pv_future * 100) / 100;
  pv_balloon= Math.round(pv_balloon * 100) / 100;
  pv_total  = Math.round(pv_total * 100) / 100;

//  document.form1.calc_payment.value	= strNumToDollars(mo_debt);
//  document.form1.calc_prebalance.value	= strNumToDollars(p_balance);
//  document.form1.calc_balloon.value	= strNumToDollars(p_balloon);

//  document.form1.calc_pv_future.value	= strNumToDollars(pv_future);
//  document.form1.calc_pv_balloon.value	= strNumToDollars(pv_balloon);
//  document.form1.calc_pv_total.value	= strNumToDollars(pv_total);

//  document.form1.calc_less_principal.value	= strNumToDollars(p_balance);

  document.form1.calc_excess.value	= strNumToDollars(calc_excess, 1);
  document.form1.calc_prepaid.value	= strNumToDollars(calc_prepaid, 1);
 
  if(calc_excess > calc_prepaid) {
    document.form1.calc_pre_penalty.value	= strNumToDollars(calc_excess, 1);
  }
  else {
    document.form1.calc_pre_penalty.value	= strNumToDollars(calc_prepaid, 1);
  }
}

