
/**********************************|***********************************
'                    Web Solutions - Pappas Mortgage
'                               Web Site
'                            Copyright 2005
'                         All Rights Reserved
'                       Web Solutions Division of
'                          JRA Systems, L.L.C.
'
' Application: Pappas Mortgage - Web Site
' Module Type: JavaScript / code
' Module Name: Mortgage_Calculator.js
'
' Contains client side JavaScript code for the common Client search
' combo boxes.
'
'
' This class calculates loan payments based on the following formula:
'
' Regular Payments On A Loan
'
'              I * P / N
'   R = -----------------------
'                    1
'        1 - -----------------
'            (I/N + 1)^(N * Y)
'
'   Where: R = Regular Payment
'          I = Annual Interest Rate
'          P = Principal
'          N = Number Of Payments Per Year
'          Y = Number Of Years
'
'*********************************************************************/

/**********************************************************************
' Variable declarations
'*********************************************************************/

/**********************************************************************
' Event handler to force a form submission from a combo box change
' triggered by the Search Representative combo box.
'
' function
' SubmitOnChange()
'
'*********************************************************************/

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


/**********************************************************************
' Event handler to force a form submission from a combo box change
' triggered by the Search Representative combo box.
'
' function
' SubmitOnChange()
'
'*********************************************************************/

function 
SubmitOnChange(objForm)
{
  objForm.btnHiddenGo.value = "Y"
  objForm.submit()
}

/**********************************************************************
*
* Event button code to compute the payment amount for the parameters
* entered.  The formula is shown below:
*
* Regular Payments On A Loan
*
*              I * P / N
*   R = -----------------------
*                    1
*        1 - -----------------
*            (I/N + 1)^(N * Y)
*
*   Where: R = Regular Payment
*          I = Annual Interest Rate
*          P = Principal
*          N = Number Of Payments Per Year
*          Y = Number Of Years
*
*  Calculate_Payment
*  {
*    objForm
*  }
'*********************************************************************/

function
Calculate_Payment(objForm)

{
var  dblFutureValue;
var  dblInterestRate;
var  dblPmtsPerYr;
var  dblPurchasePrice;

var  dblTerm1;
var  dblTerm2;
var  dblTerm3;
var  dblTerm4;
var  dblTerm5;
var  dblTaxes_And_Insurances;
var  dblTtl_With_Misc;

/**********************************************************************
' Capture Variables
'*********************************************************************/

  dblFutureValue   = 0;
  dblInterestRate  = parseFloat(objForm.dblInterest_Rate.value)/100;
  dblPmtsPerYr     = parseFloat(objForm.intPayments_Per_Year.value);
  dblPurchasePrice = parseFloat(objForm.dblLoan_Amount.value);

  if (! isNaN(parseFloat(objForm.dblTax_Misc.value)))
  {
    dblTaxes_And_Insurances = parseFloat(objForm.dblTax_Misc.value);
  }
  else
  {
    dblTaxes_And_Insurances = 0;
  }

/**********************************************************************
' Calculate payment
'*********************************************************************/

  dblTerm1 = dblInterestRate * dblPurchasePrice / dblPmtsPerYr;
  dblTerm2 = (dblInterestRate / dblPmtsPerYr) + 1.00;
  dblTerm3 = Math.pow(dblTerm2, dblPmtsPerYr * objForm.dblLoan_Term.value);
  dblTerm4 = 1.0 - (1.0 / dblTerm3);
  dblTerm5 = dblTerm1 / dblTerm4;

/**********************************************************************
' Add taxes, insurances, and fees to monthly payment.
'*********************************************************************/

  dblTtl_With_Misc = dblTerm5 + (dblTaxes_And_Insurances / 12);

/**********************************************************************
' Display monthly payment.
'*********************************************************************/

  objForm.dblEstimated_Payment.value = 
    addCommas(dblTtl_With_Misc.toFixed(2));
  
  return false;
}

