﻿function myNetworkControl_AddRemove(_userAddingId, _userAddedId, _action)
{
  if(_userAddingId != null && _userAddedId != null && _action != null)
  {
    var args = 'action=' + _action + '|UserAddingId=' + _userAddingId + '|UserAddedId=' + _userAddedId;
    myNetworkControl_CallbackTrigger(args);
  }
  return true;
}// end function

function myNetworkControl_CallbackDone(args, context)
{ 
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  // extract the predefined control client IDs
  var s = args.split('|');
  if(s.length > 1)
  {
    var json = s[0];
    var htmlText = s[1];
    
    var retMsg = eval( '(' + json + ')' );
    var holderId = retMsg.HolderId;
    
    // search for the control container
    var holder = document.getElementById(holderId);
    
    // 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)
    {
      SetOuterHtml(holder,htmlText);   
      var action = retMsg.Action;
      if (action == "add" || action == "remove")
      {
        var userAddedId = retMsg.UserAddedId;
        if (window.Com_KGProfileHeaderControl_CallbackTrigger && window.Com_GetSelectedProfileId && Com_GetSelectedProfileId() == userAddedId)
          setTimeout("Com_KGProfileHeaderControl_CallbackTrigger('action=repopulate')", 10);
      }
    }//end if(holder ....
    else
    {
      myNetworkControl_ProcessCallBackError('ContentDiv not found in KGMyNetworkControl.', '');
    }
  }
}

function myNetworkControl_ProcessCallBackError(_args, _context)
{
  alert('Error in KGMyNetworkControl callback');
}

function myNetworkControl_AddToMyNetwork(_userAddingId, _userAddedId)
{
  myNetworkControl_AddRemove(_userAddingId, _userAddedId, 'add');
}

function myNetworkControl_RemoveFromMyNetwork(_userAddingId, _userAddedId, e)
{
  myNetworkControl_AddRemove(_userAddingId, _userAddedId, 'remove');
  CancelBubble(e);
}

function MyNWSitesControl_RemoveLink(_personID, _linkID, e)
{
  var args = 'action=remove|id=' + _linkID;
  MyNWSitesControl_CallbackTrigger(args);
  CancelBubble(e);
}

function MyNWSitesControl_CreateLink_Wrapped(_nameTextBoxID, _urlTextBoxID)
{
  var validated = Page_ClientValidate('NewMyNWSiteValidation');
  if(validated)
  {
    // trigger callback to save the link to DB
    var namebox = document.getElementById(_nameTextBoxID);
    var urlbox = document.getElementById(_urlTextBoxID);
    if(namebox != null && urlbox != null)
    {
      var args = 'action=add|name=' + namebox.value.replace('|', '[@hkpipe@]') + '|url=' + urlbox.value.replace('|', '[@hkpipe@]');
      MyNWSitesControl_CallbackTrigger(args);
      MyNWSites_SaveLoadingIcon('Saving...', true);
    }
  }
}

function MyNWSitesControl_ProcessCallBackError(_args, _context)
{
  var error = 'Error in KGMyNetworkingSitesControl callback.';
  if(_args.length > 0)
    error += ' ' + _args;
  alert(error);
}

function MyNWSitesControl_CallbackTrigger_Wrapped(_args, _callbackScript)
{
  var args;

  // Construct the callback arguments
  if(_args != null)
  { 
    args = _args;
    
    if(args.length > 0)
    {
      // trigger the callback
      eval(_callbackScript);
    }
  }
}

function MyNWSitesControl_CallbackDone(args, context)
{ 
  MyNWSites_SaveLoadingIcon('', false);
  
  if(args.indexOf('Exception') == 0)
    alert(args);
  else
  {
    var results = args.split('|');
    if(results.length > 1)
    {
      var id = results[0];
      var isValid = results[1];
      var htmlText = results[2];
      
      // search for the control container
      var holder = document.getElementById(id);
      
      if(holder != null)
      {
        SetOuterHtml(holder,htmlText);  
        
        if (isValid && isValid.toLowerCase() != "false")
          MyNWSitesControl_ResetFields(); 
      }//end if(holder ....
      else
      {
        MyNWSitesControl_ProcessCallBackError('ContentDiv not found.', '');
      }
    }
  }
}

function MyNWSitesControl_ResetFields_Wrapped(_nameTextBoxID, _urlTextBoxID)
{
    var namebox = document.getElementById(_nameTextBoxID);
    var urlbox = document.getElementById(_urlTextBoxID);
    if(namebox != null && urlbox != null)
    {
      namebox.value = '';
      urlbox.value = 'http://';
    }
    
    MyNWSitesControl_ResetValidators();
}

function MyNWSitesControl_ResetValidators_Wrapped(_validatorSummaryCellId)
{
  var validatorSummaryCell = document.getElementById(_validatorSummaryCellId);
  
  if (validatorSummaryCell)
  {
    var valSum = validatorSummaryCell.firstChild;
    if (valSum)
      validatorSummaryCell.removeChild(valSum);
    // originally when the control first loads, the validator summary cell is empty
  }
}

// Hide or show save loading div
function MyNWSites_SaveLoadingIcon_Wrapped(_loadingDivID, _btnID, _loadingText, _show)
{
  var loadingDiv = document.getElementById(_loadingDivID);
  var saveBtn = document.getElementById(_btnID);
  if(loadingDiv != null && saveBtn != null)
  {
    if(_show == true)
    {
      saveBtn.style.display = 'none';
      loadingDiv.innerHTML = '<table cellspacing="0" cellpadding="0" border="0"><tr><td style="font-size:10px;">&nbsp;' + _loadingText + '...</td><td><img src="images/spinner.gif" width="16" height="16" border="0"></td></tr></table>';
      loadingDiv.style.display = 'block';
    }else
    {
      loadingDiv.style.display = 'none';
      loadingDiv.innerHTML = '';
      saveBtn.style.display = 'block';
    }
  }

}
