﻿// JScript File
var m_newTextVersionControl; // the newTextVersionControl overlay object

function KGNewTextVersionControl_CallbackDone(_args, _context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if (_args.indexOf("Exception") == -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 + ')');
    
    switch (retMessage.Action)
    {
      case "CreateNewVersion":
        //update m_textViewDocVersion with ID for new Version
        m_textViewDocVersion = retMessage.DocVersionID;
        setTimeout("LoadTextView()",20); //needs a timeout to avoid an infinite loop caused by AJAX problems 
        if (!retMessage.Existing)
        {
          //add to the docVersion Drop down list and set selected
          m_buttonToolbar_docVersionSelect.AddOption(retMessage.Name, retMessage.DocVersionID, m_buttonToolbar_docVersionSelect.newOption);
          var newVersion = eval("[{\"verID\":"+retMessage.DocVersionID+",\"name\":\""+retMessage.Name+"\"}]");
          if (m_KGDocVersionArray != null)
            m_KGDocVersionArray = m_KGDocVersionArray.concat(newVersion);
          else
            m_KGDocVersionArray = newVersion;
          // if this is the first non default text versions, we now need to add the delete option
          if (m_buttonToolbar_docVersionSelect.deleteOption == null)
            m_buttonToolbar_docVersionSelect.AddDeleteOption()
        }
        
        m_newTextVersionControl.Cancel();
        break;
    }
  }
  else
  {
    alert(_args);
  }
}

function KGNewTextVersionControl_CallbackError(_args, _context)
{
  alert(_args);
}

function KGNewTextVersionControl_CallbackTrigger_Wrapped(_args, _callbackScript)
{
  var args;

  // Construct the callback arguments
  if(_args != null)
  { 
    args = _args;
    
    if(args.length > 0)
    {
      // trigger the callback
      eval(_callbackScript);
    }
  }
}

// KGNewTextVersionControl class
function KGNewTextVersionControl()
{
  if (!window.KGNewTextVersionControl_GetTextBox)
    return false;

  this.TextBox = KGNewTextVersionControl_GetTextBox();
  
  this.Open = function()
  {
    if (window.ShowKGNewTextVersionControl)
      ShowKGNewTextVersionControl();
  };
  
  this.Cancel = function()
  {
    // select the current docVersion in the m_buttonToolbar_docVersionSelect
    var optionID = m_buttonToolbar_docVersionSelect.CreateOptionId(m_textViewDocVersion);
    document.getElementById(optionID).selected = true;
    
    // close the NewTextVersionControl
    this.Close();
  };
  
  this.Close = function()
  {
    // clear the textBox
    this.TextBox.value = "";
    if (window.HideKGNewTextVersionControl)
      HideKGNewTextVersionControl();
    
    // enable the drop down list
    m_buttonToolbar_docVersionSelect.select.disabled = false;
  };
  
  this.AddNewTextVersion = function()
  {
    if (this.TextBox != null)
    {
      // TODO: save current Text View version
      SaveTextView();
      
      // get the input text
      var newTextVersionName = this.TextBox.value;
      // callback to this control
      var args = 'Action=CreateNewVersion';
      args += '|Name=' + newTextVersionName;
      args += '|KGID=' + m_buttonToolbar_docVersionSelect.KGID;
      KGNewTextVersionControl_CallbackTrigger(args);
    }
  };
  
  return true;
}

function KGNewTextVersionControl_GetTextBox_Wrapped(_textBoxId)
{
  var textBox = document.getElementById(_textBoxId);
  return textBox;
}


var m_deleteTextVersionControl; // the deleteTextVersionControl overlay object

function KGDeleteTextVersionControl_CallbackDone(_args, _context)
{
  // Check for a session time out before completing the callback
  if (CheckSessionTimeout())
    return;
  
  if (_args.indexOf("Exception") == -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 + ')');
    
    switch (retMessage.Action)
    {
      case "DeleteVersions":
        
        for (versionIndex in retMessage.DeletedVersions)
        {
          m_buttonToolbar_docVersionSelect.Remove(retMessage.DeletedVersions[versionIndex]);
          // remove any entries with this verID from the m_KGDocVersionArray
          for(var k=m_KGDocVersionArray.length-1; k >=0 ; k--)
          {
            if(m_KGDocVersionArray[k].verID == retMessage.DeletedVersions[versionIndex])
              m_KGDocVersionArray.splice(k,1);
            if (m_textViewDocVersion == retMessage.DeletedVersions[versionIndex])
              m_textViewDocVersion = null;
          }
          
          // remove any entries with this verID from the m_KnowdeDocVersionArray
          for(var k=m_KnowdeDocVersionArray.length-1; k >=0 ; k--)
          {
            if(m_KnowdeDocVersionArray[k].verID == retMessage.DeletedVersions[versionIndex])
              m_KnowdeDocVersionArray.splice(k,1);
          }
        }
        
        // if there are no more versions, remove the delete option
        if (m_KGDocVersionArray == null || m_KGDocVersionArray.length <= 0)
          m_buttonToolbar_docVersionSelect.Remove("delete");
        m_deleteTextVersionControl.Cancel();
        break;
    }
  }
  else
  {
    alert(_args);
  }
}

function KGDeleteTextVersionControl_CallbackError(_args, _context)
{
  alert(_args);
}

function KGDeleteTextVersionControl_CallbackTrigger_Wrapped(_args, _callbackScript)
{
  var args;

  // Construct the callback arguments
  if(_args != null)
  { 
    args = _args;
    
    if(args.length > 0)
    {
      // trigger the callback
      eval(_callbackScript);
    }
  }
}

// KGDeleteTextVersionControl class
function KGDeleteTextVersionControl()
{
  if (!window.KGDeleteTextVersionControl_GetVersionsBox)
    return false;
  
  this.VersionsBox = KGDeleteTextVersionControl_GetVersionsBox();
  
  this.Open = function()
  {
    if (window.ShowKGDeleteTextVersionControl)
    {
      this.Populate(m_KGDocVersionArray)
      ShowKGDeleteTextVersionControl();
    }
  };
  
  this.Cancel = function()
  {
    // select the current docVersion in the m_buttonToolbar_docVersionSelect
    var optionID = m_buttonToolbar_docVersionSelect.CreateOptionId(m_textViewDocVersion);
    document.getElementById(optionID).selected = true;
    
    // close the DeleteTextVersionControl
    this.Close();
  };
  
  this.Close = function()
  {
    if (window.HideKGDeleteTextVersionControl)
      HideKGDeleteTextVersionControl();
    
    // enable the drop down list
    m_buttonToolbar_docVersionSelect.select.disabled = false;
  };
  
  this.DeleteTextVersions = function()
  {
    if (this.VersionsBox != null)
    {      
      // TODO: get the selected versions
      var selectedValues = this.SelectedValues()
      
      if (selectedValues.length > 0)
      {
        var values = selectedValues[0];
        
        for (var index=1; index < selectedValues.length; index++)
        {
          values += ',' + selectedValues[index];
        }
        
        // callback to this control
        var args = 'Action=DeleteVersions';
        args += '|Versions=' + values;
        args += '|KGID=' + m_buttonToolbar_docVersionSelect.KGID;
        KGDeleteTextVersionControl_CallbackTrigger(args);
      }
      else
      {
        alert('No Selected Text Versions')
      }
    }
  };
  
  this.Initialise = function()
  {
    // clear all entries in the droplist
    while (this.VersionsBox.length > 0)
    {
      this.VersionsBox.remove(this.VersionsBox.length-1);
    }
  };
  
  // generates an element id for an option in the list
  this.CreateOptionId = function(_value)
  {
    if (_value == null)
      _value = "default";
    
    return this.VersionsBox.id + "|" + _value;
  };
  
  // add an option to the select drop list
  // _text and _value inputs
  this.AddOption = function(_text, _value, _before)
  {
    // create a new option object
    var docVersion = document.createElement('option');
    docVersion.text = _text;
    docVersion.value = _value;
    docVersion.id = this.CreateOptionId(_value);
    // add to the droplist
    
    if (_before != null)
    {
      try
      {
        this.VersionsBox.add(docVersion, _before); // standards compliant
      }
      catch(ex)
      {
        this.VersionsBox.add(docVersion, _before.index); // IE only
      }
    }
    else
    {
      try
      {
        this.VersionsBox.add(docVersion, null); // standards compliant
      }
      catch(ex)
      {
        this.VersionsBox.add(docVersion); // IE only
      }
    }
    
    return docVersion;
  };
  
  // adds each entry in the _docVersions array to the listbox
  this.Populate = function(_docVersions)
  {
    // clear the list box
    this.Initialise();    
    
    if (_docVersions != null)
    {
      // loop through _docVersions array
      for (var arrayIndex=0; arrayIndex < _docVersions.length; arrayIndex++)
      {
        this.AddOption(_docVersions[arrayIndex].name, _docVersions[arrayIndex].verID);
      }
    }
  };
  
  this.SelectedValues = function ()
  {
    var selectedValues = new Array();
    
    for (var index=0; index < this.VersionsBox.options.length; index++)
    {
      if (this.VersionsBox.options[index].selected)
      {
        selectedValues.push(this.VersionsBox.options[index].value);
      }
    }
    
    return selectedValues;
  };
  
  return true;
}

function KGDeleteTextVersionControl_GetVersionsBox_Wrapped(_versionsBoxId)
{
  var versionBox = document.getElementById(_versionsBoxId);
  return versionBox;
}