﻿// JScript File
function KGDetailsControl_CallbackDone(args, context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
    
  if(args.toString() == "true")
  {
		// Toggle the control to hide it 
		KGDetailsControl_ToggleControl();
	  // update left hand panel with new details
  	if (window.LeftPaneMyKnowledgeControl_CallbackTrigger)
	    setTimeout("LeftPaneMyKnowledgeControl_CallbackTrigger('Action=repopulate')", 1);
		return;
  }
  // Test if we have any callback result
  // YES: Continue processing 
  //  NO: Fail the callback with error message
  if(args.length > 0)
  {
    var token = args.split('|');
		
    // Test if we have a correct formatted callback result
    // FORMAT is <ControlDivId>@<HTML of the control>
    // YES: Continue processing 
    //  NO: Fail the callback with error message
    if(token.length > 0)
    {
      // Split the callback result into holderId and CallbackResult string
      var holderID = token[0];
      var callbackResult  = token[1];
			
      // Test if we have valid values
      // YES: Continue processing 
      //  NO: Fail the callback with error message
      if(holderID.length > 0 && callbackResult.length > 0)
      {
        // Get a handle on the DOM element for the control 
        var holder = document.getElementById(holderID);
				
        // Test if we have the correct DOM element
        // YES: we update the control with new HTML
        //  NO: fail the callback with error message
        if(holder != null)
        {
          // debugger;
          SetOuterHtml(holder,callbackResult);
          KGDetailsControl_ToggleControl();
        }
        else
          KGDetailsControl_CallbackError('Control div cannot be found.');
      }
      else
        KGDetailsControl_CallbackError('Malformed Callback Reuslt.');
    }
    else
      KGDetailsControl_CallbackError('Malformed Callback Reuslt.');
  }
  else
    KGDetailsControl_CallbackError('Empty callback result string.');			
    
  // make sure the publish control is hidden, otherwise it will pop to top left after rerender
  if(g_publishHolderId != null && window.KGPublishOptionControl_HideMenu)
  {
    KGPublishOptionControl_HideMenu(g_publishHolderId);
  }
	
}

function KGDetailsControl_CallbackError(_msg)
{
    alert('AJAX callback failed on KGDetailsControl:' + _msg);
}

function KGDetailsControl_CancelButton_Click()
{
  // Toggle the control to hide it 
  KGDetailsControl_ToggleControl();
}

function KGDetailsControl_SaveButton_Click()
{
	KGDetailsControl_CallbackTrigger(null, true);
}

function KGDetailsControl_CallbackTrigger_Wrapped(_kgId, _save, _detailsNameID, _detailsDescID, _cbRef)
{
	var args;
	if(_save == true)
	{
	  var detailsName = document.getElementById(_detailsNameID);
		var detailsDesc = document.getElementById(_detailsDescID);
		
		var args='save=true|name=' + detailsName.value + '|desc=' + detailsDesc.value;
		eval(_cbRef);
	}
	else
	{
		if(_kgId<=0)
			return;
		var args='kgId='+_kgId;
		eval(_cbRef);
	}
}