/*
 ****************************************************************************************************
 *
 * COPYRIGHT NOTICE:
 *   (C) Copyright 2009 DealerTitan, Inc. All rights reserved.
 *
 * FILE NAME:
 *   inventory.js
 *
 * PURPOSE:
 *   Javascript functions for inventory
 *
 * NOTES:
 *
 * ABBREVIATIONS/ACRONYMS:
 *
 ****************************************************************************************************
*/

function Calculate_Inventory_Payment()
{
  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("#Inventory_Calculator_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("#Inventory_Calculator_Payment").html("$"+Payment.toFixed(2));
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 7/8/09
//Function: Setups and calculates the inventory payment calculator
//***************************************************************************************************
function Inventory_Payment_Calculator()
{
  jQuery(".Inventory_Payment_Input").change(function(){
    Calculate_Inventory_Payment();
  });
  
  Calculate_Inventory_Payment();
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 7/8/09
//Function: Submit the inventory question
//***************************************************************************************************
function Submit_Inventory_Question()
{
  // Get the data
  var Inventory_ID = Symbol_Encode(jQuery("#Modal_Content #Inventory_ID").val());
  var Name = Symbol_Encode(jQuery("#Modal_Content #Name").val());
  var Email = Symbol_Encode(jQuery("#Modal_Content #Email").val());
  var Phone = Symbol_Encode(jQuery("#Modal_Content #Phone").val());
  var Question = Symbol_Encode(jQuery("#Modal_Content #Question").val());
  
  if (Name == "")
  {
    jAlert("Please enter your Name.");
  }
  else if (Email == "" &&
           Phone == "")
  {
    jAlert("Please enter either your Email Address or Phone Number.");
  }
  else if (Question == "")
  {
    jAlert("Please enter your Question.");
  }
  else
  {
    // Display loading in the modal
    Display_Loading(jQuery("#Modal_Content"));
    
    // Perform the ajax call
    jQuery.post("index.php",
                {
                  Function_Name:"Submit_Inventory_Question",
                  Inventory_ID:Inventory_ID,
                  Name:Name,
                  Email:Email,
                  Phone:Phone,
                  Question:Question
                },
                function(data)
                {
                  // Close the modal
                  jQuery.modal.close();
                             
                  if (data != "")
                  {
                    jAlert(data, "Error sending Question");
                  }
                  else
                  {
                    jAlert("Question sent successfully.  We will respond shortly.","Question Sent");
                  }
                });
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 7/8/09
//Function: Submit the inventory Offer
//***************************************************************************************************
function Submit_Inventory_Offer()
{
  // Get the data
  var Inventory_ID = Symbol_Encode(jQuery("#Modal_Content #Inventory_ID").val());
  var Name = Symbol_Encode(jQuery("#Modal_Content #Name").val());
  var Email = Symbol_Encode(jQuery("#Modal_Content #Email").val());
  var Phone = Symbol_Encode(jQuery("#Modal_Content #Phone").val());
  var Offer = Symbol_Encode(jQuery("#Modal_Content #Offer").val());
  var Comments = Symbol_Encode(jQuery("#Modal_Content #Comments").val());
  
  if (Name == "")
  {
    jAlert("Please enter your Name.");
  }
  else if (Email == "" &&
           Phone == "")
  {
    jAlert("Please enter either your Email Address or Phone Number.");
  }
  else if (Offer == "")
  {
    jAlert("Please enter your Offer.");
  }
  else
  {
    // Display loading in the modal
    Display_Loading(jQuery("#Modal_Content"));
    
    // Perform the ajax call
    jQuery.post("index.php",
                {
                  Function_Name:"Submit_Inventory_Offer",
                  Inventory_ID:Inventory_ID,
                  Name:Name,
                  Email:Email,
                  Phone:Phone,
                  Offer:Offer,
                  Comments:Comments
                },
                function(data)
                {
                  // Close the modal
                  jQuery.modal.close();
                             
                  if (data != "")
                  {
                    jAlert(data, "Error sending Offer");
                  }
                  else
                  {
                    jAlert("Offer sent successfully.  We will respond shortly.","Offer Sent");
                  }
                });
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 7/8/09
//Function: Submit the inventory quote
//***************************************************************************************************
function Submit_Inventory_Quote()
{
  // Get the data
  var Inventory_ID = Symbol_Encode(jQuery("#Modal_Content #Inventory_ID").val());
  var Name = Symbol_Encode(jQuery("#Modal_Content #Name").val());
  var Email = Symbol_Encode(jQuery("#Modal_Content #Email").val());
  var Phone = Symbol_Encode(jQuery("#Modal_Content #Phone").val());
  var Comments = Symbol_Encode(jQuery("#Modal_Content #Comments").val());
  
  if (Name == "")
  {
    jAlert("Please enter your Name.");
  }
  else if (Email == "" &&
           Phone == "")
  {
    jAlert("Please enter either your Email Address or Phone Number.");
  }
  else
  {
    // Display loading in the modal
    Display_Loading(jQuery("#Modal_Content"));
    
    // Perform the ajax call
    jQuery.post("index.php",
                {
                  Function_Name:"Submit_Inventory_Quote",
                  Inventory_ID:Inventory_ID,
                  Name:Name,
                  Email:Email,
                  Phone:Phone,
                  Comments:Comments
                },
                function(data)
                {
                  // Close the modal
                  jQuery.modal.close();
                             
                  if (data != "")
                  {
                    jAlert(data, "Error sending Quote Request");
                  }
                  else
                  {
                    jAlert("Quote Request sent successfully.  We will respond shortly.","Quote Request Sent");
                  }
                });
  }
}
