﻿// KGPersonalListControl JScript File

// ProcessCallBackError
function KGPersonalKGListControl_ProcessCallBackError(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
  {
    var s = args.split('='); 
    var holder = document.getElementById(s[0]);
    if(holder != null)
    {
      holder.innerHTML = 'An error has occurred. ' + s[1];
    }
    else
    {
      alert('Exception occurred. '+ s[1]);
    }
  }
}

// RePopulateCallbackDone - Also used when Subscription changed as well
function PersonalKGList_RepopulateCallbackDone_Wrapped(args, context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if(args.indexOf('Status=Error') > -1)
  {
    var statusInfo = args.split('[@|@]'); 
    var status = statusInfo[0].split('=');
    if(status[1]=='Error')
    {
      var holderID = statusInfo[1].split('=');
      var exception = statusInfo[2].split('=');
      var callbackerror = holderID[1] + '=' + exception[1];
      KGPersonalKGListControl_ProcessCallBackError(callbackerror, '');
    }
  }
  else
  {
    var repop = args.split('[@|@]');
    var repopulateStatus = repop[0].split('=');
    if(repopulateStatus[1] == 'true')
    {
      args =  args.substring(args.indexOf('[@|@]') + 5);
      var s = args.split('='); 
      var holder = document.getElementById(s[0]);
      if(holder != null)
      {
        holder.innerHTML = args.substring(args.indexOf('=')+1);
        
        // need to redeclare the storage and the main function before calling it
        // therefore get the text of it
        // storage first
        var remainString = args;
        var continueLoop = 0;
        do
        {
          continueLoop = 0;
          var pos1 = remainString.indexOf('![CDATA[');
          if ( pos1 > - 1)
          {
            var string2 = remainString.substring(pos1, args.length);
            
            // now the init string
            var pos4 = string2.indexOf('window.ComponentArt_Init_', 0);
            var pos5 = string2.indexOf('//]]>', pos4 + 1);
            var funpos = string2.indexOf('=', pos4 + 1);
            if ( pos4 > -1 && pos5 > -1 && funpos > -1)
            {
              var string3 = string2.substring(0, pos5);
              var pos6 = string3.lastIndexOf('}');
              if ( pos6 > -1 )
              {
                // init function name
                var functionName = string2.substring(pos4, funpos-1);
                functionName = functionName.replace(' ','');
                functionName = functionName + '()';
                var evalInitString = string3.substring(pos4, pos6 + 1);
                
                // eval the string
                eval(evalInitString);
                
                // now execute it now that we've redefined it
                eval(functionName);
                
                // set status
                continueLoop = 1;
              } // end if pos6
            } // end if pos4 and pos5

            // get remain string
            remainString = string2.substring(pos5 + 1);

          } // end if pos1
          else
          {
            remainString = '';
          }
        }
        while(remainString.length > 0 && continueLoop == 1) // end while length
      }
      else
      {
        KGPersonalKGListControl_ProcessCallBackError('Holder div not found.', '');
      }
    } // end if need repopulation block
  } // end status error else block
}
