// -------------------------------------------------------------------------
// Global variable definitions.
// -------------------------------------------------------------------------
// array holding the ids of the nodes to copy
gCopyArray = new Array();
// array holding the ids of the nodes to delete
gDeleteArray = new Array();
// array holding the types of the nodes to insert
gInsertArray = new Array();

// -------------------------------------------------------------------------
// Utility functions.
// -------------------------------------------------------------------------
function addToStringArray(val, strArray, delim)
{
  if (typeof(delim)=="undefined") delim = ",";
  if (strArray != '')
  {
    var realArray = strArray.split(delim);
    realArray[realArray.length] = val;  
    return realArray.join(delim);
  }
  else
    return val;
}
function deleteFromStringArray(val, strArray, delim)
{
  if (typeof(delim)=="undefined") delim = ",";
  var realArray = strArray.split(delim);
  var tmpArray = new Array();
  for (i=0;i<realArray.length;i++)
    if (realArray[i] != val)
      tmpArray[tmpArray.length] = realArray[i];
  return tmpArray.join(delim);
}
// Post form data with given action
function submitAction(_action)
{
  setAction(_action);
  document.forms[0].submit();
  document.forms[0].target = '';
}
// Open new window without posting form data
function newWindow(_controller, _context, _action, _name, _windowDef)
{
  newWindowEx(_controller, _context, _action, _name, _windowDef, '');
}
// Open new window without posting form data but with additional query string
// _additionalQueryString: e.g. "&oid=...."
function newWindowEx(_controller, _context, _action, _name, _windowDef, _additionalQueryString)
{
  _name = window.open(document.forms[0].action+
            "?controller="+_controller+
            "&context="+_context+
            "&usr_action="+_action+_additionalQueryString, 
            _name, _windowDef);
}
function setController(_controller) { document.forms[0].controller.value=_controller; }
function setContext(_context) { document.forms[0].context.value=_context; }
function setAction(_action) { document.forms[0].usr_action.value=_action; }
function setVariable(_name, _val) { eval('document.forms[0].'+_name+'.value = "'+_val+'";'); }
function setTarget(_target) { document.forms[0].target=_target; }

// -------------------------------------------------------------------------
// CMS functions.
//
// To allow accumulation of actions (etc. copy more than one node) all 
// 'do...' functions don't submit the form. They just update the appropriate
// form variables.
// The submission of the form will actually be done by a call to 'submitAction'.
// The following controller will be chosen based on the action given to 
// 'submitAction' so don't expect that variables used in other action contexts
// will be considered in action processing.
//
// NOTE: all functions act on the first form in the document.
//
//
// form variable conventions:
//
// action:   variablename:            value:         description:
// =================================================================================
// display   oid                      oid            display node with oid 'oid'.
// save      value-datatype-name-oid  wert           change variable 'name' of
//                                                   type 'datatype' at node with
//                                                   oid 'oid'. NOTE: the '-' in variablename
//                                                   may be any application defined delimiter
// new       poid                     oid            add new node(s) of type(s) 
//           newtypes                 types array*   'newtypes' to parent node
//                                                   with oid 'poid'.
// delete    deleteoids               oids  array*   delete node(s) with oid(s)
//                                                   'deleteoids'
// paste     poid                     oid            add existing node(s) with oid(s)
//           clipboardoids            oids  array*   'clipboardoids' to parent node
//                                                   with oid 'poid'
//
// *array is a comma-separated string
//
//------------------------------------------------------------------------

//
// Display node with oid 'oid'.
//
function doDisplay(_oid)
{
  document.forms[0].oid.value=_oid;
}
//
// Create a new node of type 'type'.
// We actually only store the type of the node to create for later creation.
// The parent node of the 'new' action is defined by using 'doSetParent'.
//
function doNew(_type)
{
  document.forms[0].newtypes.value=addToStringArray(_type, document.forms[0].newtypes.value);
}
//
// Copy node with oid 'oid'.
// We actually only store the oid of the node to copy for later paste.
// The parent node of the 'paste' action is defined by using 'doSetParent'.
//
function doCopy(_oid)
{
  document.forms[0].clipboardoids.value=addToStringArray(_oid, document.forms[0].clipboardoids.value);
}
//
// Set the parent node for 'new' and 'paste' action to the node with oid 'poid'.
//
function doSetParent(_poid)
{
  document.forms[0].poid.value=_poid;
}
//
// Delete node with oid 'oid'.
// We actually only store the oid of the node to delete for later deletion.
//
function doDelete(_oid, _confirm, _text)
{
  if (typeof(_confirm)=="undefined") _confirm = true;
  if (_confirm)
  {
    if (typeof(_text)=="undefined")
      _text = "Really Delete Node with OID "+_oid+"."
    check = confirm(_text);
  }
  else
    check = true;
  if (check==true)
  {
    document.forms[0].deleteoids.value=addToStringArray(_oid, document.forms[0].deleteoids.value);
  }
  return check;
}
//
// Save all changes.
//
function doSave()
{
  // nothing to do
  // all information is edited in the 'value-datatype-name-id' variables
}

