////////////////////////////////////////////////////////////////////////////
//                                                                        //
//          Copyright (C) 2011 ANSYS Inc.  All rights reserved.           //
//     Unauthorized use, distribution or duplication is prohibited.      //
//                                                                        //
// This file contains proprietary software licensed from ANSYS Inc.       //
// This header must remain in any source code despite modifications or    //
// enhancements by any party.                                             //
//                                                                        //
////////////////////////////////////////////////////////////////////////////
//
// $Workfile: $
// $Revision: 610226 $
// $Date: 2013-01-07 03:27:38 -0500 (Mon, 07 Jan 2013) $
// $Author: mrao $
//
// Decription: JScript for the AG (ANSYS Geometry) Workbench GUI
//             Implements the toolbox
//
// All declared identifiers start with 'ag'
////////////////////////////////////////////////////////////////////////////

function agToolboxCreate()
{
  // Declare some enum constants needed in this file!
  ag._WT_EDITBOX = 2;
  ag._WT_BUTTON = 4;
  ag._WT_CHECKBOX = 6;
  ag._AT_RIGHT = 2;
  ag._MISSING_ICON = "dummy";
  ag._WBTB_FIXEDSINGLE = 1;
  ag._WBTB_ALLOWDRAG = false;
  
  // Set imagelist & styles
  ag.toolbox.ImageList = ag.imageList;
  ag.toolbox.BorderStyle = ag._WBTB_FIXEDSINGLE;
  
  // Disable drag-and-drop
  ag.toolbox.AllowDragItems = ag._WBTB_ALLOWDRAG;
  
  // Keep a pointer to last selected item
  ag.tbxLastItem = null;
  
  // 'Static' controls are used as 'labels' for editboxes
  // They also need IDs; below integer is maintained for uniqueness
  ag.tbxLastStatic = 99000;
  
  // We keep a list of all the controls; mainly for: agTooboxPrefs
  var aux = "Scripting.Dictionary";

  ag.tbxControlsDict = CreateActiveXObject(aux);
  
  ag.tbxControlsArray = new Array();
  ag.tbxControls = 0;
  
  // Add groups (menus) to toolbox
  // Add items (menu items) to group
  // Add controls (Editbox, Button) to item
  // NOTE: *Both* numeric *and* string IDs must be unique!
  var group, item, control;
  
  group = ag.toolbox.Groups.Add ("SkMenu", agString("SkMenu"));
  {
    // ID range: 500 < id < 600; 550 < conrol id < 600
    item = agTbxItem (group, 503,"SkLine", "drawline");
    item = agTbxItem (group, 504,"SkLineTangent", "drawlinetangent");
    item = agTbxItem (group, 505,"SkLine2Tan", "drawlinetangent");
    item = agTbxItem (group, 506,"SkPolyLine", "drawpolyline");
    item = agTbxItem (group, 507,"SkPolyGon", "drawpolygon");
    control = agTbxEditbox (item, 557, "SkPolyGonSides", 0);
    item = agTbxItem (group, 508,"SkRectangle", "drawrectangle");
    control = agTbxCheckbox (item, 554,"SkRectangleAutoFillet");
    item = agTbxItem (group, 509,"SkRectangle3Pt", "drawrectangle3point");
    control = agTbxCheckbox (item, 555,"SkRectangle3PtAutoFillet");
    item = agTbxItem (group, 510,"SkOval", "drawslot");
    item = agTbxItem (group, 511,"SkCircle", "drawcircle");
    item = agTbxItem (group, 512,"SkCircle3Tan", "drawcircle3tangent");
    item = agTbxItem (group, 513,"SkArcTangent", "drawarctangent");
    item = agTbxItem (group, 514,"SkArc3Pt", "drawarc3pt");
    item = agTbxItem (group, 515,"SkArcCenter", "drawarccenter");
    item = agTbxItem (group, 516,"SkEllipse", "drawellipse");
    item = agTbxItem (group, 517,"SkSpline", "drawcurve");
    control = agTbxCheckbox (item, 556,"SkSplineFlex");
    item = agTbxItem (group, 518,"SkPoint", "drawpoint");
    item = agTbxItem (group, 519,"SkPointInt", "drawpointintersection");
  }
  
  group = ag.toolbox.Groups.Add ("SkModify", agString("SkModify"));
  {
    // ID range: 600 < id < 700; 650 < conrol id < 600
    //unitType   -> 0(no units), 1(length), 2(angle)
    item = agTbxItem (group, 601, "SkFillet", "modfillet");
    control = agTbxEditbox (item, 651, "SkFilletRadius", 1);
    item = agTbxItem (group, 602, "SkChamfer", "modchamfer");
    control = agTbxEditbox (item, 652, "SkChamferLength", 1);
    item = agTbxItem (group, 603, "SkCorner", "modcorner");
    item = agTbxItem (group, 604, "SkTrim", "modtrim");
    control = agTbxCheckbox (item, 664,"SkTrimIgnoreAxis");
    item = agTbxItem (group, 605, "SkExtend", "modextend");
    control = agTbxCheckbox (item, 665,"SkExtendIgnoreAxis");
    item = agTbxItem (group, 606, "SkSplit", "modsplit");
    ag.splitSegCtrl = null;
    item = agTbxItem (group, 607, "SkDrag", "moddrag");
    item = agTbxItem (group, 608, "SkCut", "modcut");
    item = agTbxItem (group, 609, "SkCopy", "modcopy");
    item = agTbxItem (group, 611, "SkPaste", "modpaste");
    control = agTbxEditbox (item, 658, "SkPasteScaleFactor", 0);
    control = agTbxEditbox (item, 659, "SkPasteAngle", 4);
    item = agTbxItem (group, 612, "SkMove", "modmove");
    control = agTbxEditbox (item, 660, "SkMoveScaleFactor", 0);
    control = agTbxEditbox (item, 661, "SkMoveAngle", 4);
    item = agTbxItem (group, 613, "SkReplicate", "modreplicate");
    control = agTbxEditbox (item, 662, "SkReplicateScaleFactor", 0);
    control = agTbxEditbox (item, 663, "SkReplicateAngle", 4);
//    if (ag.m.BetaFeatures != 0)
    item = agTbxItem (group, 615, "SkDuplicate", "modduplicate");
    item = agTbxItem (group, 614, "SkOffset", "modoffset");
//    if (ag.m.AlphaFeatures != 0)
    item = agTbxItem (group, 616, "SkSplineEdit", "editspline");
  }
  
  group = ag.toolbox.Groups.Add ("SkDimension", agString("SkDimension"));
  {
    // ID range: 200 < id < 300; 250 < conrol id < 300
    item = agTbxItem (group, 201,"SkDimenGeneral", "dimgeneral");
    item = agTbxItem (group, 202,"SkDimenHorizontal", "dimhorizontal");
    item = agTbxItem (group, 203,"SkDimenVertical", "dimvertical");
    item = agTbxItem (group, 204,"SkDimenDistance", "dimdistance");
    item = agTbxItem (group, 205,"SkDimenRadius", "dimradius");
    item = agTbxItem (group, 206,"SkDimenDiameter", "dimdiameter");
    item = agTbxItem (group, 207,"SkDimenAngle", "dimangle");
    item = agTbxItem (group, 208,"SkSemiAuto", "dimautomatic");
    item = agTbxItem (group, 210,"SkDimenEdit", "dimedit");
    item = agTbxItem (group, 211,"SkMove", "dimmove");
    item = agTbxItem (group, 212,"SkAnimate", "dimanimate");
    control = agTbxEditbox (item, 253, "SkDimAnimateCycles", 0);
    item = agTbxItem (group, 213,"SkDimenDisplay", "dimdisplay");
    control = agTbxCheckbox (item, 251,"SkDimenDisplayValue");
    control = agTbxCheckbox (item, 252,"SkDimenDisplayName");
  }
  
  group = ag.toolbox.Groups.Add ("SkConstraint", agString("SkConstraint"));
  {
    // ID range: 1300 < id < 1400; 1350 < conrol id < 1400
    item = agTbxItem (group, 1301,"SkFixed", "confixed");
    control = agTbxCheckbox (item, 1353,"SkFixEnds");
    item = agTbxItem (group, 1302,"SkHorizontal", "conhorizontal");
    item = agTbxItem (group, 1303,"SkVertical", "convertical");
    item = agTbxItem (group, 1304,"SkPerpendicular", "conperpendicular");
    item = agTbxItem (group, 1305,"SkTangent", "contangent");
    item = agTbxItem (group, 1306,"SkCoincident", "concoincident");
    item = agTbxItem (group, 1307,"SkMidpoint", "conmidpoint");
    item = agTbxItem (group, 1308,"SkSymmetry", "consymmetric");
    //control = agTbxCheckbox (item, 1354,"SkSymmetryEnds");
    item = agTbxItem (group, 1309,"SkParallel", "conparallel");
    item = agTbxItem (group, 1310,"SkConcentric", "conconcentric");
    item = agTbxItem (group, 1311,"SkEqualRadius", "conequalradius");
    item = agTbxItem (group, 1312,"SkEqualLength", "conequallength");
    item = agTbxItem (group, 1313,"SkEqualDistance", "conequaldistance");
    //item = agTbxItem (group, 1316,"SkEvaluate", ag._MISSING_ICON);
    item = agTbxItem (group, 1318,"SkAutoConstraints", "conautoconstraints");
    control = agTbxCheckbox (item, 1351,"SkAutoConCursor");
    control = agTbxCheckbox (item, 1352,"SkAutoConGlobal");
  }
  
  group = ag.toolbox.Groups.Add ("SkSettings", agString("SkSettings"));
  {
    // ID range: 1500 < id < 1600; 1550 < control id < 1600
    item = agTbxItem (group, 1503,"SkGrid", "utilgrid");
    control = agTbxCheckbox (item, 1551, "SkGridSnapOn");
    control = agTbxCheckbox (item, 1552, "SkGridOn");
    item = agTbxItem (group, 1504,"SkGridSpacing", "utilspacing");
    control = agTbxEditbox (item, 1554, "SkGridMajorDist", 1);
    item = agTbxItem (group, 1505,"SkGridLines", "utilspacing");
    control = agTbxEditbox (item, 1553, "SkGridMinorLines", 0);
    item = agTbxItem (group, 1506,"SkGridSnaps", "utilspacing");
    control = agTbxEditbox (item, 1555, "SkGridSnapMinor", 0);
  }
  
  
  agToolboxPrefs();
}

function agToolboxPrefs (name)
{
  // Loads the pref values from the applet to the GUI
  
  // If the optional <name> parameter is given,
  // then it's only done for the named control
  if (typeof (name) == "string")
    {
      if(name == "SkDimenDisplayValue" || name == "SkDimenDisplayName")
        {
          agToolboxGetPrefForControl (ag.tbxControlsDict.Item ("SkDimenDisplayValue"));
          agToolboxGetPrefForControl (ag.tbxControlsDict.Item ("SkDimenDisplayName"));
        }
      else
        agToolboxGetPrefForControl (ag.tbxControlsDict.Item (name));
    }
  else
    {
      for (var i = 0; i < ag.tbxControls; i++)
        agToolboxGetPrefForControl (ag.tbxControlsArray[i]);
    }
}

function agToolboxGetPrefForControl (control)
{
  // Refinement
  with (control)
    {
      switch (Type)
        {
        case ag._WT_EDITBOX:
          {
            Tag.Value = ag.gui.getPrefValue (Name);
            var toolboxValue = ag.gui.localizeDecimals (1, Tag.Value);
            Text = agUnits (toolboxValue, Tag.unitType);
            break;
          }
        case ag._WT_CHECKBOX:
          {
            Value = ag.gui.getPrefValue (Name);
            break;
          }
        }
    }
}

function agTbxItem (group, id,name, icon_name)
{
  // Refinement: adds toolbox item to <group>
  // Both the integer <id> and the string <name> MUST be unique Identifiers
  // The <name> ID doubles as the localized string ID for label
  // NOTE:
  //    The item's Tag stores the item id
  
  var localized = agString(name);
  if (localized == "") localized = name;  // no localized string yet!?
  
  var item = group.Items.Add (name,localized, icon_name);
  item.Text = localized;
  item.Tag = id;
  return item;
}

function agTbxEditbox (item, id,name, unitType)
{
  // Refinement: adds an Editbox control to toolbox <item>
  // Both the integer <id> and the string <name> MUST be unique Identifiers
  // NOTE:
  //    The editbox' Tag stores the "current value"
  //    Cf. agToolboxPrefs
  
  var WT_STATIC = 1;
  
  var control = item.Controls.Add (ag._WT_EDITBOX, name, id, 60);
  control.Tag = new Object();
  control.Tag.unitType = unitType;
  var label = agString(name);
  if (label != "")
    {
      ag.tbxLastStatic ++;
      var control2 = item.Controls.Add (WT_STATIC, name, ag.tbxLastStatic, 10);
      control2.Caption = label;  // caption decides on control's width
      control2.Alignment = ag._AT_RIGHT;
    }
  agRememberControl (name, control);
  return control;
}

function agTbxCheckbox (item, id,name)
{
  // Refinement: adds an Checkbox control to toolbox <item>
  // Both the integer <id> and the string <name> MUST be unique Identifiers
  var label = agString(name);
  var control = item.Controls.Add (ag._WT_CHECKBOX, name, id, 10);
  control.Caption = label;  // caption decides on control's width
  control.LeftText = true;
  control.Alignment = ag._AT_RIGHT;
  agRememberControl (name, control);
  return control;
}

function agTbxButton (item, id, name)
{
  // Refinement: adds a Button control to toolbox <item>
  // Both the integer <id> and the string <name> MUST be unique Identifiers
  // The <name> ID doubles as the localized string ID for label
  
  var localized = agString(name);
  if (localized == "") localized = name;  // no localized string yet!?
  
  var control;
  control = item.Controls.Add (ag._WT_BUTTON, name, id, 60);
  control.Caption = localized;
  return control;
}

function agRemoveControls(item)
{
  var controls = item.Controls;
  var count = controls.Count;
  var index = 0;
  for(index = 1; index <= count; index++)
    {
      controls.Remove(1);
    }
}

function agRememberControl (name, control)
{
  // Refinement
  ag.tbxControlsDict.Add (name, control);
  ag.tbxControlsArray[ag.tbxControls] = control;
  ag.tbxControls ++;
}

function agForgetControl (name, control)
{
  // Remove control from Dictionary and list
  if(ag.tbxControlsDict.Exists(name))
    {
      ag.tbxControlsDict.Remove(name);
    }
  
  var i, j;
  for(i=0; i<ag.tbxControls; i++)
    {
      if(ag.tbxControlsArray[i] == control)
        {
          //set the reference to null
          ag.tbxControlsArray[i] = null;
          for(j=i+1; j<ag.tbxControls; j++)
            {
              ag.tbxControlsArray[j-1] = ag.tbxControlsArray[j];
            }
          ag.tbxControls --;
          break;
        }
    }
}

function agTbxItemByName (groupName, name)
{
  // Access a toolbox item object by <name> AND <groupName>
  // NOTE: This is also used from test scripts!
  var item;
  try
    {
      var group = ag.toolbox.groups.Item (groupName);
      item = group.Items.Item (name);
      var id = item.Tag;  // (just verifying that it's an item)
    }
  catch(e)
    {
      agError ("agTbxItemByName (\"" + groupName
               + ", " + name + "\") -- FAILED!");
    }
  return item;
}

function agTbxItemControlByName (item, name)
{
  // Access a control object by its <name>, given the <item> object
  // NOTE: This is also used from test scripts!
  var control;
  try
    {
      control = item.Controls.Item (name);
      var nm = control.Tag;  // (just verifying that it's a control)
    }
  catch(e)
    {
      agError ("agTbxItemControlByName (..., \""
               + name + "\") -- FAILED!");
    }
  
  return control;
}

function agTbxControlByName (groupName, itemName, name)
{
  // Access a control object by its <name> AND <groupName>, <itemName>
  // NOTE: This is also used from test scripts!
  var control;
  try
    {
      var group = ag.toolbox.groups.Item (groupName);
      var item = group.Items.Item (itemName);
      var id = item.Tag;  // (just verifying that it's an item)
      control = item.Controls.Item (name);
      var nm = control.Tag;  // (just verifying that it's a control)
    }
  catch(e)
    {
      agError ("agTbxControlByName (\"" + groupName
               + "\", \"" + itemName +  "\", \"" + name + "\") -- FAILED!");
    }
  
  return control;
}

function agToolbox_OnGroupChangedDelegate(sender, args) {
  ag.shouldBodyTreeRefresh = 0;
  agToolbox_OnGroupChanged(args.Group);
  ag.shouldBodyTreeRefresh = 1;
}

function agToolbox_OnGroupChanged (group)
{
  // Do nothing if DM is already busy
  if (ag.m.Busy)
    return;

  //agEcho ("OnGroupChanged [" + group.Text + "]");
  //Clear 'last clicked'
  ag.tbxLastItem = null;
  if (group.Name == "SkDimension")
    {
      // Default: General Dimension state
      var item = agTbxItemByName ("SkDimension", "SkDimenGeneral");
      agToolbox_OnItemClicked (item);
    }
  else
    {
      if (! ag.gui.IsSelectState()) agSelectionMode(true);
    }
}

function agToolbox_OnItemClickedDelegate(sender, args)
{
  agToolbox_OnItemClicked(args.Item);
}

function agToolbox_OnItemClicked (item)
{
  // Do nothing if DM is already busy
  if (ag.m.Busy)
    return;

  // It's not obvious why this test is needed, but it seems to be...
  if ((item == null) || (item == ag.tbxLastItem))
    return;
  
  // Remember last item selected
  ag.tbxLastItem = item;
  if (item.Selected != 1) item.Selected = 1;  // eg., for GUI/Applet tests
  
  // BoxZoom messes up selection for toolbox items.
  // If it is on, then revert back to Rotation.
  if (ag.bBZoom.Check)
    agGraphicsHandler("TbRotate", 101);
  
  // must do this to make sure we always update the edit boxes or check boxes
  var controls = item.Controls;
  var i, size;
  size = controls.Count;
  for (i = 0; i<size; i++)
    agToolboxGetPrefForControl(controls.Item(i+1));
  
  // Ensure we're in a select state
  if (! ag.gui.IsSelectState()) agSelectionMode(false);
  
  // Handle event
  var id = item.Tag;
  if       (500 < id && id < 600)  agSketchingEvent (id);
  else if  (600 < id && id < 700)  agModifyEvent (id);
  else if  (200 < id && id < 300)  agDimensionEvent (id);
  else if (1300 < id && id < 1400) agConstraintEvent (id);
  else if (1500 < id && id < 1600) agSettingsEvent (id);
  else
    agError ("OnItemClicked, ID: " + id + ", name: [" + item.Name + "]");
  
  agUncheckAllGraphicsButtons();  // Clear all graphics buttons
}

function agToolboxClear (groupIndex)
{
  // Unselect last item selected
  // Optional parameter: <groupIndex> ... also switch to given group
  
  if (ag.tbxLastItem != null)
    {
      ag.tbxLastItem.Selected = 0;
      ag.tbxLastItem = null;
    }
  
  if (typeof(groupIndex) == "number")
    {
      var group = ag.toolbox.groups.Item (groupIndex);
      ag.toolbox.SelectedGroup  = group;
    }
}

function agToolboxRefresh()
{
  // Refresh if any last item selected

  if (ag.tbxLastItem != null)
  {
    var item = ag.tbxLastItem;
    var controls = item.Controls;
    var i, size;
    size = controls.Count;
    for (i = 0; i < size; i++)
      agToolboxGetPrefForControl(controls.Item(i + 1));

    // Ensure we're in a select state
    if (!ag.gui.IsSelectState()) agSelectionMode(false);

    // Handle event
    var id = item.Tag;
    if (500 < id && id < 600) agSketchingEvent(id);
    else if (600 < id && id < 700) agModifyEvent(id);
    else if (200 < id && id < 300) agDimensionEvent(id);
    else if (1300 < id && id < 1400) agConstraintEvent(id);
    else if (1500 < id && id < 1600) agSettingsEvent(id);
    else
      agError("OnItemClicked, ID: " + id + ", name: [" + item.Name + "]");
  }
}

function agToolbox_OnCommandDelegate(sender, args)
{
  agToolbox_OnCommand(args.Name, args.ID, args.Code, args.Control, args.Item);
}

function agToolbox_OnCommand (name, id, code, control, item)
{
  // Redirect toolbox control events based on control.Type
  var _WT_HITENTER = 1;
  var _WT_KILLFOCUS = 512;
  var _WT_SETFOCUS = 256;
  var okay = true;
  
  // The following branch is for debugging only
  if (true)
    {
      if ((name != control.name) || (id != control.id))
        {
          agError ("I am very confused..."); // & this should never happen! :)
          return;
        }
      if (((550 < id) && (id < 600)) // SkMenu
          || ((650 < id) && (id < 700)) // SkModify
          || ((250 < id) && (id < 300)) // SkDimension
          || ((1350 < id) && (id < 1400)) // SkConstraint
          || ((1550 < id) && (id < 1600)) // SkUtilities
          )
        okay = true;
      else
        okay = false;
    }
  
  if (okay)
    switch (control.Type)
      {
      case ag._WT_EDITBOX:
        {
          switch (code)
            {
            case _WT_HITENTER:
              {
                var oldValue = control.Tag.Value;
                var toolboxStr = ag.gui.localizeDecimals (0, control.Text);
                var toolboxValue = Number (toolboxStr);
                if(control.Text.length < 1 || isNaN(toolboxValue))
                  {
                    toolboxValue = oldValue;
                    control.Tag.Value = oldValue;
                  }
                if (toolboxValue != oldValue)
                  {
                    control.Tag.Value = toolboxValue;
                    agToolbox_OnEditboxChanged (item, control, toolboxValue);
                  }
                break;
              }
            case _WT_KILLFOCUS:
              {
                var oldValue = control.Tag.Value;
                var toolboxStr = ag.gui.localizeDecimals (0, control.Text);
                var toolboxValue = Number (toolboxStr);
                if(control.Text.length < 1 || isNaN(toolboxValue))
                  {
                    toolboxValue = oldValue;
                    control.Tag.Value = oldValue;
                    //Ok, it's not really changed, but value needs units.
                    agToolbox_OnEditboxChanged (item, control, toolboxValue);
                  }
                if (toolboxValue != oldValue)
                  {
                    control.Tag.Value = toolboxValue;
                    agToolbox_OnEditboxChanged (item, control, toolboxValue);
                  }
                toolboxValue = ag.gui.localizeDecimals (1, control.Tag.Value);
                control.Text = agUnits(toolboxValue, control.Tag.unitType);
                
                break;
              }
            case _WT_SETFOCUS:
              {
                control.Text = control.Tag.Value;
                break;
              } 
            default:
              agError ("Invalid Editbox code: " + code);
            }
          break;
        }
      case ag._WT_BUTTON:
        {
          agToolbox_OnButtonClicked (item, control);
          break;
        }
      case ag._WT_CHECKBOX:
        {
          agToolbox_OnCheckboxClicked (item, control);
          break;
        }
      default:
        okay = false;
      }
  if (! okay)
    {
      agError ("Invalid OnCommand(" + control.Type + ") [" + name
               + "],  item Name: [" + item.Name
               + "],  item.Text: [" + item.Text
               + "],  code: [" + code
               + "]");
    }
}

function agToolbox_OnEditboxChanged (item, control, newValue)
{
  // Send the newValue to the applet
  ag.gui.setPrefValue (control.Name, newValue);
  
  // Reload the pref value, in case user input was rejected
  agToolboxPrefs (control.Name);
}

function agToolbox_OnButtonClicked (item, control)
{
  // Send control.Name to applet?
  ag.gui.toolBoxButtonHit (control.Name);
  
}

function agToolbox_OnCheckboxClicked (item, control)
{
  // Send control.Value to applet (0 or 1, depending on checked or not)
  var check = control.Value;
  ag.gui.setPrefValue (control.Name, check);
  
  // Reload the pref value, in case user input was rejected
  agToolboxPrefs (control.Name);
}

function agToolboxSplitText()
{
  var mode  = ag.gui.getPrefValue ("SkSplitMode");
  var group = ag.toolbox.Groups("SkModify");
  var item  = group.Items("SkSplit");
  var newText;
  
  if(mode == 0)
    newText = agString("SkSplitSelect");
  else if(mode == 1)
    newText = agString("SkSplitPoint");
  else if(mode == 2)
    newText = agString("SkSplitAllPoints");
  else if(mode == 3)
    newText = agString("SkSplitEqual");
  else
    newText = agString("SkSplit");
  
  ag.toolbox.Groups("SkModify").Items("SkSplit").Text = newText;
  
  if(mode == 3) //Split into equal segments
    {
      if(ag.splitSegCtrl == null)
        {
          ag.splitSegCtrl = agTbxEditbox (item, 653, "SkSplitSegments", 0);
          agToolboxGetPrefForControl(ag.splitSegCtrl);
        }
    }
  else
    {
      if(ag.splitSegCtrl != null)
        {
          agForgetControl("SkSplitSegments", ag.splitSegCtrl);
          ag.splitSegCtrl = null;
          agRemoveControls(item);
        }
    }
  ag.toolbox.Refresh();
}

function agToolbox_OnKeyUpDelegate(sender, args)
{
  agToolbox_OnKeyUp(args.KeyCode, args.Shift);
}

function agToolbox_OnKeyUp(keyCode, shift)
{
  agKeyUp(keyCode, shift, 3);
}
