/*
 ****************************************************************************************************
 *
 * COPYRIGHT NOTICE:
 *   (C) Copyright 2008 DealerTitan, Inc. All rights reserved.
 *
 * FILE NAME:
 *   common_functions.js
 *
 * PURPOSE:
 *   Common Javascript functions
 *
 * NOTES:
 *
 * ABBREVIATIONS/ACRONYMS:
 *
 ****************************************************************************************************
*/
var Opacity = 1.0;
var Fade_Timer;

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/28/08
//Function: Fade the visibility of the desired element out or in
//***************************************************************************************************
function Fade_Item(Element_Name)
{
  clearInterval(Fade_Timer);
  if (document.getElementById(Element_Name).style.visibility == "visible") 
  {
    Opacity = 1.0;
    document.getElementById(Element_Name).style.filter = "alpha(style=0,opacity=100)";
    document.getElementById(Element_Name).style.opacity = 1.0;
    Fade_Timer = setInterval("Fade_Item_Out('"+Element_Name+"')", 50);
  }
  else 
  {
    Opacity = 0;
    document.getElementById(Element_Name).style.visibility = "visible";
    Fade_Timer = setInterval("Fade_Item_In('"+Element_Name+"')", 50);
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/28/08
//Function: Fade out the visibility of the desired element
//***************************************************************************************************
function Fade_Item_Out(Element_Name)
{
  if (Opacity > 0)
  {
    Opacity -= .1;
  } 
  else 
  {
    document.getElementById(Element_Name).style.visibility = "hidden" ;
    clearInterval(Fade_Timer);
  }
  document.getElementById(Element_Name).style.filter =
    "alpha(style=0,opacity=" + (Opacity*100) + ")";
  document.getElementById(Element_Name).style.opacity = Opacity;
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/28/08
//Function: Fade in the visibility of the desired element
//***************************************************************************************************
function Fade_Item_In(Element_Name)
{
  if (Opacity < 1.0)
  {
    Opacity = 1.0;
  } 
  else 
  {
    document.getElementById(Element_Name).style.visibility = "visible";
    clearInterval(Fade_Timer);
  }
  document.getElementById(Element_Name).style.filter = 
    "alpha(style=0,opacity=" + (Opacity*100) + ")";
  document.getElementById(Element_Name).style.opacity = Opacity;
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/28/08
//Function: Toggle the visibility of the busy notifier object
//***************************************************************************************************
function Toggle_Busy_Notifier()
{
  Fade_Item("Busy_Notifier");
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 1/29/09
//Function: Append debug to the debug div
//***************************************************************************************************
function Append_Debug(Debug)
{
  jQuery("#Debug").html(jQuery("#Debug").html()+Debug+"<br>");
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/28/08
//Function: Get all of the names of the checked items in the form
//***************************************************************************************************
function Get_Form_Checked_Items(Form_ID)
{
  var Temp_Array = new Array();
  jQuery("#"+Form_ID).find("input:checked").each(function()
  {
    Temp_Array.push(jQuery(this).val());
  });
  
  return Temp_Array.join(",");
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 1/13/09
//Function: Create the prototype parameters for a standard action of call function
//***************************************************************************************************
function Create_Prototype_Parameters(Function,Options_Array)
{
  var Params = "Action=Function&Function="+Function;
  
  for (i=0;i<Options_Array.length;i++)
  {
    Params += "&"+Options_Array[i][0]+"="+Options_Array[i][1];
  }
  
  return Params;
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/28/08
//Function: Encode the symbols with a string that will be decoded later
//***************************************************************************************************
function Symbol_Encode(str) 
{
  try
  {
    str=str.replace(/\&/g,'\AMPERSAND_SYMBOL');
    str=str.replace(/\%/g,'\PERCENT_SYMBOL');
    str=str.replace(/\+/g,'\PLUS_SYMBOL');
    str=(str+'').replace(/([\\"'])/g, "\\$1").replace(/\0/g, "\\0");
  }
  catch (err)
  {
    // do nothing, this usually happens when str is an integer
  }
  
  return str;
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/26/09
//Function: Update the element to show that it is loading
//***************************************************************************************************
function Display_Loading($Element,
                         Size)
{
  if (Size == undefined)
  {
    Size = "Large";
  }
  
  if (Size == "Large")
  {
    $Element.html("<div style='display:block;text-align:center;margin-top:200px;margin-bottom:200px;'>"+
                  "<img src='images/loading_big.gif'><div>Please Wait</div></div>");
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/4/09
//Function: Display the modal content
//***************************************************************************************************
function Open_Modal_Content(Div_ID,
                            Function,
                            Parameters,
                            Width,
                            Height,
                            Callback_Function)
{
  if (Callback_Function == undefined)
  {
    Callback_Function = null;
  }
  
  // Check for valid width
  if (Width == undefined)
  {
    Width = 400;
  }
  
  // Check for valid Height
  if (Height == undefined)
  {
    Height = 400;
  }
  
  // Initialize the content to the loading image
  jQuery("#"+Div_ID).html("<div style='display:block;text-align:center;margin-top:200px;margin-bottom:200px;'><img src='images/activity.gif'><br>Loading, Please Wait</div>");
  
  // Show the modal window
  jQuery("#"+Div_ID).modal({containerCss: {width: Width+"px",height: Height+"px"}});
  
  Parameters = jQuery.extend({Function_Name:Function}, Parameters);
  
  // Load the modal content
  jQuery("#"+Div_ID).load("index.php",Parameters,Callback_Function);
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 6/25/09
//Function: Validate that a field is not empty
//***************************************************************************************************
function Validate_Empty(fld) {
  var error = "";

  if (fld.value.length == 0)
  {
    fld.style.background = 'Yellow'; 
    error = "The required field ("+fld.id+") has not been filled in.\n"
  }
  else
  {
    fld.style.background = 'White';
  }
  return error;   
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 6/25/09
//Function: Trim the string
//***************************************************************************************************
function Trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

//***************************************************************************************************
//Author: Chris Ayers
//Date: 6/25/09
//Function: Validate the email address
//***************************************************************************************************
function Validate_Email(fld) {
  var error="";
  var tfld = Trim(fld.value);                        // value of field with whitespace trimmed off
  var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
  
  if (fld.value == "") 
  {
    fld.style.background = 'Yellow';
    error = "You didn't enter an email address.\n";
  } 
  else if (!emailFilter.test(tfld)) 
  {              //test email for illegal characters
    fld.style.background = 'Yellow';
    error = "Please enter a valid email address.\n";
  } 
  else if (fld.value.match(illegalChars))
  {
    fld.style.background = 'Yellow';
    error = "The email address contains illegal characters.\n";
  } 
  else 
  {
    fld.style.background = 'White';
  }
  return error;
}
