/*
 ****************************************************************************************************
 *
 * COPYRIGHT NOTICE:
 *   (C) Copyright 2008 DealerTitan, Inc. All rights reserved.
 *
 * FILE NAME:
 *   selector.js
 *
 * PURPOSE:
 *   Javascript functions for selector
 *
 * NOTES:
 *
 * ABBREVIATIONS/ACRONYMS:
 *
 ****************************************************************************************************
*/

var X_Pos;
var Y_Pos;

//***************************************************************************************************
//Author: Chris Ayers
//Date: 10/13/08
//Function: Display the selector DIV in the correct location
//***************************************************************************************************
function Display_Selector(event, 
                          ID_List, 
                          Filter_List,
                          Selector_Title,
                          Field_Name,
                          Selector_Type,
                          Field_Type,
                          Create_Flag)
{
  // Refresh the selector div
  Refresh_Selector(ID_List, 
                   Filter_List,
                   Selector_Title,
                   Field_Name,
                   Selector_Type,
                   Field_Type,
                   Create_Flag);
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 11/6/08
//Function: Refresh the selector
//***************************************************************************************************
function Refresh_Selector(ID_List, 
                          Filter_List,
                          Selector_Title,
                          Field_Name,
                          Selector_Type,
                          Field_Type,
                          Create_Flag,
                          Search_Text,
                          Filter_List)
{
  if (Search_Text == undefined)
  {
    Search_Text = "";
  }
  
  if (Filter_List == undefined)
  {
    Filter_List = "";
  }
  
  Open_Modal_Content("Modal_Content",
                     "Display_Selector",
                     {
                       ID_List:ID_List,
                       Filter_List:Filter_List,
                       Selector_Title:Selector_Title,
                       Field_Name:Field_Name,
                       Selector_Type:Selector_Type,
                       Field_Type:Field_Type,
                       Create_Flag:Create_Flag,
                       Search_Text:Search_Text,
                       Filter_List:Filter_List
                     },
                     700,
                     550);
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 11/6/08
//Function: Close the selector
//***************************************************************************************************
function Close_Selector(Field_Name)
{
  // Close the modal
  jQuery.modal.close();
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 10/13/08
//Function: Update list of things that were selected using the selector
//***************************************************************************************************
function Save_Selector(Filter_List,
                       Selector_Title,
                       Field_Name,
                       Selector_Type,
                       Field_Type,
                       Create_Flag)
{
  var Form_Ref = document.getElementById("Selector_Form");
  
  var ID_Array = Get_Form_Checked_Items_By_Name(Form_Ref,Field_Name);
  var ID_List = '';
  
  // Close the selector
  Close_Selector(Field_Name);
  
  // Create a list of things to add
  for (var i=0;i<ID_Array.length;i++)
  {
    ID = ID_Array[i];
    if (i!=0)
    {
      ID_List+=',';
    }
    ID_List+=ID;
  }
  
  // Update the hidden input
  jQuery('#'+Field_Name+'_ID_List').val(ID_List).change();
  
  // Update the selector list
  jQuery('#'+Field_Name+"_Div").load( 'index.php', 
    'Action=Function&Function=Update_Selector&ID_List='+ID_List+'&Filter_List='+Filter_List+'&Selector_Title='+Selector_Title+'&Field_Name='+Field_Name+'&Selector_Type='+Selector_Type+'&Field_Type='+Field_Type+'&Create_Flag='+Create_Flag
    );
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 10/13/08
//Function: Update list of things that were selected using the selector
//***************************************************************************************************
function Update_Selector(Filter_List,
                         Selector_Title,
                         Field_Name,
                         Selector_Type,
                         Field_Type,
                         Create_Flag)
{
  var Div_ID = "Complete_"+Field_Name+"_Selector";
  var Form_Ref = document.getElementById("Selector_Form");
  var ID_Array = Get_Form_Checked_Items_By_Name(Form_Ref,Field_Name);
  
  // Show that the div is loading
  document.getElementById(Div_ID).innerHTML = 
        "<div style='width:100%;height:100px;text-align:center;padding-top:50px;'><img src='images/activity.gif'></div>";
  
  var ID_List = '';
  
  // Create a list of things to add
  for (var i=0;i<ID_Array.length;i++)
  {
    ID = ID_Array[i];
    if (i!=0)
    {
      ID_List+=',';
    }
    ID_List+=ID;
  }
  
  // Refresh the selector
  Refresh_Selector(ID_List, 
                   Filter_List,
                   Selector_Title,
                   Field_Name,
                   Selector_Type,
                   Field_Type,
                   Create_Flag);
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 10/13/08
//Function: Filter the selector
//***************************************************************************************************
function Filter_Selector(ID_List,
                         Selector_Title,
                         Field_Name,
                         Selector_Type,
                         Field_Type,
                         Create_Flag)
{
  var Search_Text_Field = Field_Name+"_Search_Text";
  
  var Filter_1_Field = Field_Name+"_Filter_1";
  var Filter_2_Field = Field_Name+"_Filter_2";
  var Search_Text = jQuery("#"+Search_Text_Field).val();

  if (jQuery("#"+Filter_1_Field).size())
  {
    var Filter_1_Ref = document.getElementById(Filter_1_Field); 
    var Filter_1_Val = Filter_1_Ref.options[Filter_1_Ref.selectedIndex].value; 
  }
  
  if (jQuery("#"+Filter_2_Field).size())
  {
    var Filter_2_Ref = document.getElementById(Filter_2_Field); 
    var Filter_2_Val = Filter_2_Ref.options[Filter_2_Ref.selectedIndex].value; 
  }
  
  var Filter_List = Filter_1_Val+","+Filter_2_Val;
  
  // Refresh the selector
  Refresh_Selector(ID_List, 
                   Filter_List,
                   Selector_Title,
                   Field_Name,
                   Selector_Type,
                   Field_Type,
                   Create_Flag,
                   Search_Text,
                   Filter_List);
}
