/*
 ****************************************************************************************************
 *
 * COPYRIGHT NOTICE:
 *   (C) Copyright 2009 DealerTitan, Inc. All rights reserved.
 *
 * FILE NAME:
 *   finance.js
 *
 * PURPOSE:
 *   Finance Page Javascript functions
 *
 * NOTES:
 *
 * ABBREVIATIONS/ACRONYMS:
 *
 ****************************************************************************************************
*/

function EXP(a,b)
{
  var t = 1;
  for(x = 1; x <= b; x++)
  { 
    t = t * a;
  }
  ret = t;
  return(t);
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 5/27/09
//Function: Calculate the payment
//***************************************************************************************************
function Calculate_Payment()
{
  // Get the data
  var Loan_Amount = jQuery("#Loan_Amount").val();
  Loan_Amount = Loan_Amount.replace(/\,/g,'');
  var Interest_Rate = jQuery("#Interest_Rate").val();
  var Num_Months = jQuery("#Num_Months").val();
  
  jQuery("#Payment").html("");
  
  if (isNaN(parseInt(Loan_Amount)) ||
      Loan_Amount < 0)
  {
    jAlert("Please enter the Loan Amount for the payment you want calculated.");
  }
  else if (isNaN(parseInt(Interest_Rate)) ||
           Interest_Rate < 0)
  {
    jAlert("Please enter the Interest Rate for the payment you want calculated.");
  }
  else if (isNaN(parseInt(Num_Months)) ||
           Num_Months < 0)
  {
    jAlert("Please enter the Number of Months for the payment you want calculated.");
  }
  else
  {
  	var r = ( Interest_Rate * .01 ) / 12;
  	Payment = ( Loan_Amount * r ) / ( 1 - ( 1 / ( EXP( 1 + r, Num_Months ) ) ) );
  	jQuery("#Payment").html("Payment: $"+Payment.toFixed(2));
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 5/27/09
//Function: Validate the Finance form
//***************************************************************************************************
function Validate_Finance_Form(Form_Ref)
{
  var reason = "";

  reason += Validate_Empty(Form_Ref.First_Name);
  reason += Validate_Empty(Form_Ref.Last_Name);
  
  reason += Validate_Email(Form_Ref.Email);
  reason += Validate_Empty(Form_Ref.Day_Phone_1);
  reason += Validate_Empty(Form_Ref.Day_Phone_2);
  reason += Validate_Empty(Form_Ref.Day_Phone_3);
  
  reason += Validate_Empty(Form_Ref.Address);
  reason += Validate_Empty(Form_Ref.City);
  reason += Validate_Empty(Form_Ref.Zip_Code);
  
  reason += Validate_Empty(Form_Ref.Social_1);
  reason += Validate_Empty(Form_Ref.Social_2);
  reason += Validate_Empty(Form_Ref.Social_3);
  
  reason += Validate_Empty(Form_Ref.Employer_Name);
  reason += Validate_Empty(Form_Ref.Monthly_Income);
  reason += Validate_Empty(Form_Ref.Monthly_Payment);
  
  if (jQuery("#Years_On_Job").val() == -1)
  {
    reason += "You have not selected an option for the number of years you have been employed.\n";
  }
  if (jQuery("#Months_On_Job").val() == -1)
  {
    reason += "You have not selected an option for the number of months you have been employed.\n";
  }
  
  if (jQuery("#Years_At_Residence").val() == -1)
  {
    reason += "You have not selected an option for the number of years you have lived at your residence.\n";
  }
  if (jQuery("#Months_At_Residence").val() == -1)
  {
    reason += "You have not selected an option for the number of months you have lived at your residence.\n";
  }
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

