﻿// KnowledgeGeneListControl JScript File

var globalknowledgeGenesGridClientID;


function KGKnowledgeGeneListControl_ProcessCallBackError_Wrapped(args, context, _thisClientID)
{
  // 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
  {
    document.getElementById(_thisClientID).innerHTML = 'An error has occurred.';
  }
}

var m_selectedProfileID;
// Event handler for Author icon/name click event
function KGKnowledgeGeneListControl_Author_OnClientSideSelect(_selectedAuthorID, e, _isBackButtonClick)
{
  // Start the callback onto KGTabbedWindows to show CommunityTab
  SetSessionProperty('SelectedAuthorID', _selectedAuthorID);
  m_selectedProfileID = _selectedAuthorID;
  CancelBubble(e);
  
  /*if(WM_readCookie('UserID') == _selectedAuthorID)
    document.getElementById('CategoriesTreeCheckbox').parentNode.style.display = 'block';//KGNavigationControl_ShowHideSemanticTreeCheckBox(1); 
  else
    document.getElementById('CategoriesTreeCheckbox').parentNode.style.display = 'none';//KGNavigationControl_ShowHideSemanticTreeCheckBox(0);*/
}

function KGKnowledgeGeneListControl_Grid_OnInit(_knowledgeGenesGridClientID)
{
 globalknowledgeGenesGridClientID = _knowledgeGenesGridClientID;
}

// KGListGrid OnClientSideSelect event handler
function KGKnowledgeGeneListControl_Grid_OnClientSideSelect(_item)
{
  SetSessionProperty('SelectedKGID', _item.GetMember('KnowdeKey').Text);
  var keys = 'RemoveSelectedConcept|MapViewDisplayMode';
	var values = '0|true';
  SetSessionProperty(keys, values, true);

  //SetSessionProperty('RemoveSelectedConcept', 0);
  //SetSessionProperty('MapViewDisplayMode', 'KGMapView'); // this is set in the ASHX to MapViewDisplayMode.KGMapView.ToString()
      
  // Trigger MapView callback now if map view is visible
  MapViewControl1_CallbackTrigger_IfVisible();
        				
  // reset the page loaded variables
  // We will want to reload these pages if the tabs get selected
  globalIndexedDocsPageLoaded = false;
  globalTextViewPageLoaded = false;

  return true;
}

//Event Handle check box OnCheckeChanged 
function KGKnowledgeGeneListControl_OnCheckChanged(item, columnIndex, checkbox)
{ 
//debugger;
   if(item != null && columnIndex != null && checkbox != null)
   {
     var conceptId = -1;
     var kgId = -1;
     var conceptKey = item.GetMember("ConceptKey");
     var kgIdKey = item.GetMember("KnowledgeGeneKey");
     
     if(conceptKey != null)
       conceptId = conceptKey.Value;
     if(kgIdKey != null)  
       kgId = kgIdKey.Value;
     
     // Call back to subscribe kg and concept;
     //HandleTreeNodeCheckedChangedByKGList(checkbox,conceptId,kgId);
     SubscribeKGByKGListControl(checkbox, kgId);
     
   }  
     
   return true;
}// end function

function ChangeSubscriptionOfKGByKGListControl_Wrapped(aSubscribedButton,  kgId, _cbreference, _subscribe, _conceptId, _knowdeId)
{
  //debugger;
  if(aSubscribedButton != null)
  {
    var args = 'action=subscription|Concept=' + _conceptId + '|Checked=' + _subscribe + '|NodeID=' + _knowdeId + '|KGID=' + kgId + '|KGSubscrpition=1|SubscriptionButton=' + aSubscribedButton;
    
    eval(_cbreference);
    
    //aSubscribedButton.src = aSubscribedButton.src;
  }

  return true;
}// end function


function ConceptTreeControl_ChangeSubscriptionImage(_subscribeButton, _subscribe)
{
//debugger;

  if(_subscribe == "True")
  {
    _subscribeButton.src = "Images/Buttons/Add.png";
    _subscribe = false;
  }
  else
  {
    _subscribeButton.src = "Images/Buttons/Remove.png";
    _subscribe = true;
  }
  
  return _subscribeButton;
}


function KnowledgeGeneListControl_CallbackDone_Wrapped(args, context, contentDiv)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  var srmIndex;
  try
  {
    srmIndex = SrmControl_SetInitial('KnowledgeGeneListControl_CallbackDone_Wrapped');
  }
  catch(e){}
  
 // start the debugging session 
  //debugger;
  
  // extract all the cdatas lines from callback result
  var cdatas = args.split('<![CDATA[');
  
  // extract the predefined control client IDs
  var s = args.split('=');
  var ids = s[0].split('@');
  
  // 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(contentDiv != null)
  {
    // holder.innerHTML = args.substring(args.indexOf('=') + 1);
    contentDiv.innerHTML = args;
    
    // loop through the cdatas and process each of them
    // ignoring the first one coz it is just HTML
    var z = 0;
    for(z = 1; z < cdatas.length; z++)
    {
      // javascript starting position
      var posStart = cdatas[z].indexOf('window');
      
      // javascript ending position 
      var posEnd = cdatas[z].lastIndexOf('}') + 1;
      
      if(posStart > -1 && posEnd > -1)
      {
        // attemp to extract the javascript
        var jscript = cdatas[z].substring(posStart, posEnd);
       
        // Now we have the potential jscript we need to execute it to 
        // reinitiallise the grid
        if(jscript.length != 0)
        {        
          // redefine the init script on Page
          eval(jscript);
          var posNameStart = jscript.indexOf('window');
          var posNameEnd = jscript.indexOf('=');
          
          var functionName = jscript.substring(posNameStart, posNameEnd);
          // execute the script
          eval(functionName+'()');
        }// end if(jscript.....
      }// end if(posStart ......         
    }// end for
  }//end if(holder ....
  else
  {
    KGUserObjectSummaryControl_CallBackError('ContentDiv not found in KGUserObjectSummaryControl.', '');
  }
  SrmControl_RecordTimes(srmIndex);
}
