﻿// JScript File
// Write times to the SRMDB via a callback to SrmControl

var m_srmGetTimes; // whether to record times, set in SrmControl.cs from web.config
var m_srmT0 = new Array(); // when action was first triggered, as now.getTime (easier to convert to string than date-time object)
var m_srmAction = new Array(); // name of the action plus any messages separated by ','
var m_srmCallbacks = new Array(); // number of callbacks triggered by event, to determine which is the last one


// Set the initial variables when the action is triggered
// returns srmIndex, used later to refer back to that event
function SrmControl_SetInitial(_action)
{
  if (m_srmGetTimes!= null && m_srmGetTimes == "True")
  {
    //debugger;
    var now = new Date();
    var srmIndex = m_srmT0.length;
    m_srmT0[srmIndex] = now.getTime();
    m_srmAction[srmIndex] = _action;
    m_srmCallbacks[srmIndex] = 1;
    return srmIndex;
  }
  return 0;
}

// Call SrmControl_CallbackTrigger if this is the final callback
function SrmControl_RecordTimes(_srmIndex)
{
  //debugger;
  if (m_srmGetTimes != null && m_srmGetTimes == "True" && _srmIndex != null && _srmIndex != 'undefined'&& _srmIndex >= 0)
  {
    try
    {
      if (m_srmCallbacks[_srmIndex] != null)
      {
        m_srmCallbacks[_srmIndex]--;
        if (m_srmCallbacks[_srmIndex] == 0)
          setTimeout("SrmControl_CallbackTrigger("+_srmIndex+")", 10);
        
      }
    }
    catch(e) {}
  }
}


//Send the time back to the server to put into the SRMDB
function SrmControl_CallbackTrigger_Wrapped(_cbreference, _srmIndex)
{
  if (m_srmGetTimes!= null && m_srmGetTimes == "True")
  {
    var now = new Date();
    var timingDiff = Math.round(now.getTime() - m_srmT0[_srmIndex]) / 1000;
    var args = "Message=Time," + m_srmAction[_srmIndex] + ":" + timingDiff; 
    //var args = "Message=" + m_srmAction[_srmIndex] + ",Now=" + now.getTime() + ":" + timingDiff; 
    if(_cbreference != null && _cbreference != "")
      eval(_cbreference);
    
    m_srmT0[_srmIndex] = null;
    m_srmAction[_srmIndex] = null;
    m_srmCallbacks[_srmIndex] = null;
  }
}

//Send the time back to the server to put into the SRMDB
function SrmControl_CallbackTrigger_AfterDelay_Wrapped(_cbreference, _srmIndex, _srmDelay)
{
  if (m_srmGetTimes!= null && m_srmGetTimes == "True")
  {
    var now = new Date();
    var timingDiff = Math.round(now.getTime() - m_srmT0[_srmIndex] - _srmDelay) / 1000;
    var args = "Message=Time," + m_srmAction[_srmIndex] + ":" + timingDiff; 
    //var args = "Message=" + m_srmAction[_srmIndex] + ",Now=" + now.getTime() + ":" + timingDiff; 
    if(_cbreference != null && _cbreference != "")
      eval(_cbreference);
    
    m_srmT0[_srmIndex] = null;
    m_srmAction[_srmIndex] = null;
    m_srmCallbacks[_srmIndex] = null;
  }
}

function SrmControl_CallbackDone(args)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if (args != 'ok')
    SrmControl_ProcessCallBackError(args);
}

function SrmControl_ProcessCallBackError(args)
{
  // Here we are dealing with authentication timeout before callback, 
  // the message from the server is 'elogin' signify an error with 
  // authentication. The response will be trimmed to form the args 
  // 'login' here..... 
  if(args == 'login')
  {
    window.location='home.aspx';
  }
  else
  {
    //alert("Callback error in SrmControl");
    var temp = 5;
  }
}

function SrmControl_QuickCallback_Wrapped(_cbreference, _message)
{
  args = 'Message=' + _message + '|Category=Troubleshooting';
  eval(_cbreference);
}

