
/* Merged Plone Javascript file
 * This file is dynamically assembled from separate parts.
 * Some of these parts have 3rd party licenses or copyright information attached
 * Such information is valid for that section,
 * not for the entire composite file
 * originating files are separated by - filename.js -
 */

/* - ioi.js - */
// Copyright (C) 2007  InterNLnet
// started by Goldmund, Wyldebeast & Wunderliebe
// adapted InterNlnet
//
var tag_request = false;


/**
* Make a request to the server, using either synchronous or asynchronous mode.
*/
function makeRequest(url, async, callback) {

  tag_request = false;

  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    tag_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    try {
      tag_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        tag_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  if (!tag_request) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }

  tag_request.onreadystatechange = callback;
  tag_request.open('GET', url, async);
  tag_request.send(null);
}



/* - hierarchywidget.js - */
// Copyright (C) 2007  InterNLnet
//

// Based on Zope Page template from zope/app/form/browser/orderedSelectionList.pt

var destName = '_to';
var hiddenName = '_hidden';
var levelNum = 4;
var splitter = ' + '

//check if item we about to add is already there
function alreadyThere(name, val)
{
    var sel = document.getElementById(name+destName);

    for ( k=0; k< sel.length; k++)
      if (sel.options[k].value==val)
      {
      	return 1;
      }
      return 0;
}

function categoryAdd(name)
  {
    var tgt = document.getElementById(name+destName);

    var level = new Array();
    var i;
    for (i=0; i < levelNum; i++)
    {
      level[i] = document.getElementById(name+'_level'+i.toString());
    }

    if (level[0].selectedIndex <= 0) selectionError();
    else
    {
      var newText = new Array();
      var newValue = '';
      for (i=0; i < level.length; i++)
      {
        l = level[i]
        if (l.selectedIndex > 0)
        {
          newText[i] = l.options[l.selectedIndex].text;
          newValue = l.options[l.selectedIndex].value;
        }
      }
      
      if ( alreadyThere(name, newValue) ) return;
      
      // create a new virtal object with values of item to copy
      newOpt = new Option(newText.join(splitter), newValue);
      // append virtual object to targe
      tgt.options[tgt.length] = newOpt;
      // want to select newly created item
      newOpt.selected = true;

      //update hidden fields
      copyDataForSubmit(name);
    }
  }


// move item from "to" selection back to "from" selection
function categoryDel(name)
  {
    // shortcuts for selection field
    var sel = document.getElementById(name+destName);

    while (sel.selectedIndex > -1)
      if (sel.options[sel.selectedIndex].selected)
      {
        // delete item in source
        sel.options[sel.selectedIndex] = null;
      }

    //update hidden fields
    copyDataForSubmit(name);
  }

// copy each item of "sel" into one hidden input field
function copyDataForSubmit(name)
  {
  // shortcuts for selection field and hidden data field
  var sel = document.getElementById(name+destName);
  var toDataContainer = document.getElementById(name+hiddenName);

  // delete all child nodes (--> complete content) of "toDataContainer" span
  while (toDataContainer.hasChildNodes())
      toDataContainer.removeChild(toDataContainer.firstChild);

  // create new hidden input fields - one for each selection item of
  // "sel" selection
  for (var i = 0; i < sel.options.length; i++)
    {
    // create virtual node with suitable attributes
    var newNode = document.createElement("input");
    var newAttr = document.createAttribute("name");
    newAttr.nodeValue = name + ':list';
    newNode.setAttributeNode(newAttr);

    newAttr = document.createAttribute("type");
    newAttr.nodeValue = "hidden";
    newNode.setAttributeNode(newAttr);

    newAttr = document.createAttribute("value");
    newAttr.nodeValue = sel.options[i].value;
    newNode.setAttributeNode(newAttr);

    // actually append virtual node to DOM tree
    toDataContainer.appendChild(newNode);
    }
  }

// error message for missing selection
function selectionError()
  {alert("Must select something!")}

