﻿// KGCommunityLinkControl JScript File

function KGAddCommunityLinkControlCallbackDone(_args, _context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if (_args.indexOf("Exception") == -1)
  {
    // reload the communityTab
    window.setTimeout('LoadCommunityTab("ListView")', 1);
    
    // First off is to convert our arguments into a proper object. What comes back should be JSON 
    // so we simply evaluate it into an object
    var retMessage = eval( '(' + _args + ')');
		
    var userMsg = '<p>Community Link successfully added';
    switch (retMessage.Context)
    {
      case 'KG':
        userMsg += ' to entire KG.';
        break;
      case 'Knowde':
        userMsg += ' to this Knowde only.';
        break;
      case 'Hows':
        userMsg += ' to this Knowde and Hows.';
        break;
    }
    userMsg += '</p>';
  	
    userMsg += '<input type="button" class="CommunityLinkControlBtn" value="Add another" onclick="m_AddCommunityLinkControl.Initialise();"/>'
    m_AddCommunityLinkControl.HideOrShowLoadingDiv(true, userMsg, true);
  }
  else
  {
    alert(_args);
  }
}

function KGAddCommunityLinkControlCallbackError(_args, _context)
{
  alert(_args);
}

function OpenKGAddCommunityLinkControl(e)
{
  if (isAuthenticated())
  {
    e = e || window.event;
    
    m_AddCommunityLinkControl.Initialise();
    ShowKGAddCommunityLinkControl();
    
    var x;
    var y;
    // Get cursor position with respect to the page.
    if (window.scrollX)
    {
      x = e.clientX + window.scrollX - 20;
      y = e.clientY + window.scrollY - 20;
    }
    else if (e)
    {
      x = e.clientX + document.documentElement.scrollLeft
        + document.body.scrollLeft - 80;
      y = e.clientY + document.documentElement.scrollTop
        + document.body.scrollTop - 50;
      
      if (x + m_AddCommunityLinkControl.ContentDiv.scrollWidth > document.documentElement.scrollWidth)
        x = document.documentElement.scrollWidth - m_AddCommunityLinkControl.ContentDiv.scrollWidth;
      
      if (y + m_AddCommunityLinkControl.ContentDiv.scrollHeight > document.documentElement.scrollHeight)
        y = document.documentElement.scrollHeight - m_AddCommunityLinkControl.ContentDiv.scrollHeight;
    }
    
    UpdateKGAddCommunityLinkControlPos('absolute', x, y);
  }
  else // user not signed in
  {
    m_AddCommunityLinkControl.HandleAnonymousUser();
  }
}

// AddCommunityLinkControl object definition
function AddCommunityLinkControl(_urlTxtBoxID, _nameTxtBoxID, _contextSelectorID, _contentDivID, _loadingDivID, _kgURLDivID, _callbackScript, _rootURL)
{
  this.UrlTxtBoxID = _urlTxtBoxID;
  this.NameTxtBoxID = _nameTxtBoxID;
  this.ContextSelectorID = _contextSelectorID;
  this.ContentDivID = _contentDivID;
  this.LoadingDivID = _loadingDivID;
  this.KGURLDivID = _kgURLDivID;
  
  this.UrlTxtBox;
  this.NameTxtBox;
  this.ContextSelector;
  this.ContentDiv;
  this.LoadingDiv;
  this.KGURLDiv;
  
  this.rootURL;
  
  this.CallbackScript = _callbackScript;
  
  this.Initialise = function()
  {
    if(this.UrlTxtBox == null)
      this.UrlTxtBox = document.getElementById(this.UrlTxtBoxID);
      
    if(this.NameTxtBox == null)
      this.NameTxtBox = document.getElementById(this.NameTxtBoxID);
      
    if(this.ContextSelector == null)
      this.ContextSelector = document.getElementById(this.ContextSelectorID);
    
    if(this.ContentDiv == null)
      this.ContentDiv = document.getElementById(this.ContentDivID);
    
    if(this.LoadingDiv == null)
      this.LoadingDiv = document.getElementById(this.LoadingDivID);
    
    if(this.KGURLDiv == null)
      this.KGURLDiv = document.getElementById(this.KGURLDivID);
    
    if(this.rootURL == null)
      this.rootURL = _rootURL;
    
    this.ResetInputFields();
    this.SetKGURL();
    this.HideOrShowLoadingDiv(false);
  };
  
  this.ResetInputFields = function()
  {
    this.UrlTxtBox.value = 'http://';
    this.NameTxtBox.value = '';
  };
  
  this.HideOrShowLoadingDiv = function(_show, _message, _messageOnly)
  {
    var div = this.LoadingDiv; 
    
    var message = _message;
    if(message == null)
      message = 'Adding Link...';
     
    if (div != null)
    {
      if(_show != null && _show == true)
      {
        // clear the div
        div.innerHTML = '';
        
        if (!_messageOnly)
          div.innerHTML = "<img src='Images/LoadingBar.gif' width='21px' height='21px'/>&nbsp;";
        
        div.innerHTML += message;
        div.style.display = 'block';
        
        // hide the content
        this.ContentDiv.style.display = 'none';
      }
      else if(div.style.display != 'none')
      {
        div.style.display = 'none';
        div.innerHTML = '';
        
        // reset the content display style
        this.ContentDiv.style.display = '';
      }
    }
  };

  this.CallbackTrigger = function(_args)
  {
    // Construct the callback arguments
    if(_args != null)
    { 
      var args = _args;
      
      if(args.length > 0)
      {
        // show the loading div
        this.HideOrShowLoadingDiv(true);
        
        // trigger the callback
        eval(this.CallbackScript);
      }
    }
  };
  
  this.LinkBtnClicked = function()
  {
    var validated = Page_ClientValidate('KGAddCommunityLinkValidation');
    if(validated)
    {
      var args = 'Action=AddLink';
      args += '|URL=' + encodeURIComponent(this.UrlTxtBox.value);
      args += '|Name=' + this.NameTxtBox.value;
      args += '|Context=' + this.ContextSelector.value;
      this.CallbackTrigger(args);
      
    }
  };
  
  this.HandleAnonymousUser = function()
  {
    var currentKnowdeID = returnKnowdeID(globalLastSelectedKnowde);
    // redirect to sign in sign up page
    redirectToSignInSignUpPage('AddCommunityLink', Active_KGID, currentKnowdeID);
  };
  
  this.SetKGURL = function()
  {
    // set the KGURL
    var queryString = '&#63;kgid&#61;'// html code for '?kgid='
    this.KGURLDiv.innerHTML = this.rootURL + queryString + Active_KGID;
  };
}

function KGDeleteCommunityLinkControlCallbackDone(_args, _context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if (_args.indexOf("Exception") == -1)
  {
    // reload the communityTab
    window.setTimeout('LoadCommunityTab("ListView")', 1);
    
	  // First off is to convert our arguments into a proper object. What comes back should be JSON 
	  // so we simply evaluate it into an object
	  var retMessage = eval( '(' + _args + ')');
		
		HideKGDeleteCommunityLinkControl();
  }
  else
  {
    alert(_args);
  }
}

function KGDeleteCommunityLinkControlCallbackError(_args, _context)
{
  alert(_args);
}

function OpenKGDeleteCommunityLinkControl(e, _knowdeCommunityLinkID, _addedByCurrentUser, _KGContext)
{
  e = e || window.event;

  m_DeleteCommunityLinkControl.Initialise();
  // set the KnowdeCommunityLinkID
  m_DeleteCommunityLinkControl.KnowdeCommunityLinkID = _knowdeCommunityLinkID;
  m_DeleteCommunityLinkControl.SetKGContextMode(_KGContext); 
  // if the link wasn't added by the current user
  if (!_addedByCurrentUser)
    m_DeleteCommunityLinkControl.SetUserMsg('The user who added this link will be informed of its removal');
  
  ShowKGDeleteCommunityLinkControl();
  
  var x;
  var y;
  // Get cursor position with respect to the page.
  if (window.scrollX)
  {
    x = e.clientX + window.scrollX - 20;
    y = e.clientY + window.scrollY - 20;
  }
  else if (e)
  {
    x = e.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft - 400;
    y = e.clientY + document.documentElement.scrollTop
      + document.body.scrollTop - 50;
    
    if (x + m_DeleteCommunityLinkControl.ContentDiv.scrollWidth > document.documentElement.scrollWidth)
      x = document.documentElement.scrollWidth - m_DeleteCommunityLinkControl.ContentDiv.scrollWidth;
    
    if (y + m_DeleteCommunityLinkControl.ContentDiv.scrollHeight > document.documentElement.scrollHeight)
      y = document.documentElement.scrollHeight - m_DeleteCommunityLinkControl.ContentDiv.scrollHeight;
  }
  
  UpdateKGDeleteCommunityLinkControlPos('absolute', x, y);
}

// DeleteCommunityLinkControl object definition
function DeleteCommunityLinkControl(_contextSelectLblID, _contextSelectorID, _contentDivID, _loadingDivID, _userMsgID, _callbackScript)
{
  this.ContextSelectLblID = _contextSelectLblID;
  this.ContextSelectorID = _contextSelectorID;
  this.ContentDivID = _contentDivID;
  this.LoadingDivID = _loadingDivID;
  this.UserMsgID = _userMsgID;
  
  this.KnowdeCommunityLinkID;
  
  this.ContextSelectLbl;
  this.ContextSelector;
  this.ContentDiv;
  this.LoadingDiv;
  this.UserMsg;
  
  this.KGContext;
  
  this.CallbackScript = _callbackScript;
  
  this.Initialise = function()
  {
    if(this.ContextSelectLbl == null)
      this.ContextSelectLbl = document.getElementById(this.ContextSelectLblID);
    
    if(this.ContextSelector == null)
      this.ContextSelector = document.getElementById(this.ContextSelectorID);
    
    if(this.ContentDiv == null)
      this.ContentDiv = document.getElementById(this.ContentDivID);
    
    if(this.LoadingDiv == null)
      this.LoadingDiv = document.getElementById(this.LoadingDivID);
    
    if(this.UserMsg == null)
      this.UserMsg = document.getElementById(this.UserMsgID);

    this.KnowdeCommunityLinkID = null;
    this.HideOrShowLoadingDiv(false);
    // clear user message
    this.SetUserMsg('');
    
    this.KGContext = false;
  };
  
  this.SetUserMsg = function(_message)
  {
    this.UserMsg.innerHTML = _message;
  };
  
  this.SetKGContextMode = function(_KGContext)
  {
    if (_KGContext)
    {
      this.KGContext = true;
      this.ContextSelector.style.visibility = "hidden";
      this.ContextSelectLbl.innerHTML = "Remove the link from Entire Knowledge Gene:";
    }
    else
    {
      this.KGContext = false;
      this.ContextSelector.style.visibility = "";
      this.ContextSelectLbl.innerHTML = "Remove the link from:";
    }
  };
  
  this.HideOrShowLoadingDiv = function(_show, _message, _messageOnly)
  {
    var div = this.LoadingDiv; 
    
    var message = _message;
    if(message == null)
      message = 'Deleting Link...';
    
    if (div != null)
    {
      if(_show != null && _show == true)
      {
        // clear the div
        div.innerHTML = '';
        
        if (!_messageOnly)
          div.innerHTML = "<img src='Images/LoadingBar.gif' width='21px' height='21px'/>&nbsp;";
        
        div.innerHTML += message;
        div.style.display = 'block';
        
        // hide the content
        this.ContentDiv.style.display = 'none';
      }
      else if(div.style.display != 'none')
      {
        div.style.display = 'none';
        div.innerHTML = '';
        
        // reset the content display style
        this.ContentDiv.style.display = '';
      }
    }
  };

  this.CallbackTrigger = function(_args)
  {
    // Construct the callback arguments
    if(_args != null)
    { 
      var args = _args;
      
      if(args.length > 0)
      {
        // show the loading div
        this.HideOrShowLoadingDiv(true);
        
        // trigger the callback
        eval(this.CallbackScript);
      }
    }
  };
  
  this.DeleteBtnClicked = function()
  {
    var args = 'Action=DeleteLink';
    args += '|KCLID=' + this.KnowdeCommunityLinkID;
    if (this.KGContext)
      args += '|Context=KG';
    else
      args += '|Context=' + this.ContextSelector.value;
    this.CallbackTrigger(args);
    
  };
}