/*
 ****************************************************************************************************
 *
 * COPYRIGHT NOTICE:
 *   (C) Copyright 2009 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: 1/13/09
//Function: Create the prototype parameters for a standard action of call function
//***************************************************************************************************
function Create_Prototype_Parameters(Function,Options_Array)
{
  var Params = "Function_Name="+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: Get all of the names of the checked items in the form
//***************************************************************************************************
function Get_Form_Checked_Items_By_Name(Form_Ref, Name_Value)
{
  // Get a list of the selected CPR Scenarios
  var Item_ID_Array = Array();
  
  for (var i=0;i<Form_Ref.elements.length;i++)
  {
    if (Form_Ref.elements[i].type=="checkbox" && 
        Form_Ref.elements[i].checked==true &&
        Form_Ref.elements[i].name==Name_Value)
    {
      Item_ID_Array.push(Form_Ref.elements[i].value);
    }
    else if (Form_Ref.elements[i].type=="radio" && 
             Form_Ref.elements[i].checked==true &&
             Form_Ref.elements[i].name==Name_Value)
    {
      Item_ID_Array.push(Form_Ref.elements[i].value);
    }
    
  }
  
  return Item_ID_Array;
}

//***************************************************************************************************
//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,'\\\\');
//    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>Loading, Please Wait</div></div>");
  }
}

//***************************************************************************************************
//Author: Chris Ayers
//Date: 2/4/09
//Function: Display the modal content
//***************************************************************************************************
function Open_Modal_Content(Div_ID,
                            Function_Name,
                            Parameters,
                            Width,
                            Height,
                            Callback_Function)
{
  if (Callback_Function == undefined)
  {
    Callback_Function = null;
  }
  
  // Check for valid width
  if (Width == undefined)
  {
    Width = 850;
  }
  
  // Check for valid Height
  if (Height == undefined)
  {
    Height = 600;
  }
  
  // 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_Name}, Parameters);
  
  // Load the modal content
  jQuery("#"+Div_ID).load("index.php",Parameters,Callback_Function);
}

//***************************************************************************************************
//Author: Tony Miller
//Date: 3/16/09
//Function: Initialize all the date pickers currrently displayed
//***************************************************************************************************
function Initialize_Date_Pickers()
{
  // Loop through all the objects that are date pickers
  jQuery('.date-pick').each( function() 
  {
    var Date_Input = this;
    
    jQuery(Date_Input).DatePicker({
    	format:'Y-m-d',
    	date: jQuery(Date_Input).val(),
    	current: jQuery(Date_Input).val(),
    	starts: 1,
    	onBeforeShow: function(){
    		jQuery(Date_Input).DatePickerSetDate(jQuery(Date_Input).val(), true);
    	},
    	onChange: function(formated, dates){
    		jQuery(Date_Input).val(formated);
    		jQuery(Date_Input).change();
    		jQuery(Date_Input).DatePickerHide();
    	}
    });
  });
}

//***************************************************************************************************
//Author: Tony Miller
//Date: 3/19/09
//Function: Initializes buttons
//***************************************************************************************************
function Init_Buttons()
{
  jQuery(document).ready(function () 
  {
    jQuery(".Styled_Button").each(function()
    {
      var $Button = jQuery(this);
      if (!$Button.attr("styled"))
      {
        // Mark the button as styled
        $Button.attr("styled",true);
        
        // Style the button
        $Button.styledButton({
          			'orientation' : $Button.attr("orientation"),
          			'dropdown' : $Button.attr('dropdown') ? { 'element' : 'ul' } : undefined,
          			'role' : $Button.attr('dropdown') ? 'select' : 'button',
          			'action' : function () {
          							      if ($Button.attr("permission") == 1)
                              {
                                if ($Button.attr("target") != undefined)
                                {
          							          window.location=$Button.attr("target");
          							        }
          							        else if ($Button.attr("action") != undefined)
          							        {
          							          var Action_Function = $Button.attr("action");
          							          setTimeout(Action_Function,0);
          							        }
          							      }
                              else
                              {
                                Open_Modal_Content("Modal_Content",
                                                   "Display_Request_Access_Form",
                                                   {
                                                     Requested_Action:$Button.attr("permission_action")
                                                   },
                                                   850,
                                                   600);
                              }
          						      }
          		});

        if ($Button.attr("permission") != 1)
        {
          $Button.css("color","#125b9a");
        }
        
        if ($Button.attr("shortcut") != undefined && 
            $Button.attr("permission") == 1)
        {
          jQuery.keyboard(
          {
            keyCode:$Button.attr("shortcut"),
            callback:function()
            {
          		if ($Button.attr("target") != undefined)
              {
			          window.location=$Button.attr("target");
			        }
			        else if ($Button.attr("action") != undefined)
			        {
			          var Action_Function = $Button.attr("action");
			          setTimeout(Action_Function,0);
			        }
          		return true;
            }
         });
        }
      }
    });
  });
}

