﻿// JScript File

var m_KGAppAlertingControlLoaded;
var m_KGAppAlertPageLoaded;
var globalAppAlertGridClientID;
var m_KGAppAlertNResets = 0;
var m_KGAppAlertPreLoaded = 0;

function KGAppAlertingControl_PreLoadApp(_appId, _url)
{
  if(m_KGAppAlertPreLoaded == 0)
  {
    // auto scroll to bottom of the page so can see the alerting details
    var scrollPan = document.getElementById('Splitter1_pane_1');
    if(scrollPan != null && scrollPan.childNodes.length > 0)
      scrollPan.childNodes[0].scrollTop = scrollPan.childNodes[0].scrollHeight;
      
    if(_url.indexOf('@') == -1) // just query string, not url, append empty one
      _url = '@' + _url;
      
    window.setTimeout("KGExecuteMapControl_ShowMenu(null, 'MyAlert', '" + _appId + "', 0, null, null, null, '" + _url + "')", 1000);
    m_KGAppAlertPreLoaded = 1;
  }
}

// KGAppAlertingControlGrid OnClientSideSelect event handler
function KGAppAlertingControl_Grid_OnClientSideSelect_Wrapped(_appId, _alertUrl, _alertUrlQS, _redirectUrl, _loggedInUser)
{
  var strApps = _appId + ''; //make sure it's a string not an int for all the ExecuteMap string manipulation
  if (strApps && (_alertUrl.length > 0 || _alertUrlQS.length > 0) )
    KGExecuteMapControl_ShowMenu(e, 'MyAlert', strApps, 0, null, null, null,  _alertUrl + '@' + _alertUrlQS)

	return true;
}

function KGAppAlertingControl_CallbackDone_Wrapped(args, context, contentDiv)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if(args.length == 0 || args.indexOf("System exception caught") == 0) // this might be too specific and miss some exceptions, but need to avoid showing message box if e.g. a KG happens to have 'exception' in its name or description
  {
    if (args.indexOf('deadlocked on lock resources with another process') > -1)
      alert("Sorry, our servers are too busy at the moment\n\nPlease try again later");
    else
      alert("AJAX Failure:\n\n" + args);
    return;
  }
  
  // search for the control container
  var holder = document.getElementById(contentDiv);
  
  // Test if the container is found
  // Yes: replace the innerHTML with the callback result
  //  No: call CallbackError with Content div not found error message
  if(holder != null)
  {
    // holder.innerHTML = args.substring(args.indexOf('=') + 1);
    holder.innerHTML = args;
    
  }//end if(holder ....
  else
  {
    KGMyKnowledgeControl_CallbackError('ContentDiv not found in KGAppAlertingControl.', '');
  }
}

function KGAppAlertingControl_CallbackTrigger_Wrapped(args, _cbreference)
{
  eval(_cbreference);
}

function KGAppAlertingControl_CallbackError(args, context)
{
  // 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
  {
    // It was left empty before the edit[PL08JAN2008]
  }
}

