  
  var baseUri = "/farbecht/100%27000+Franken+mehr+f%C3%BCr+den+Botanischen+Garten/";

  function openPopup(url, name, width, height) {
      if (isNaN(width)) width = 300;
      if (isNaN(height)) height = 350;
      // create reasonable screen location for popup
      var position =  ",left="+
                      Math.floor((screen.availWidth - width) / 2)+
                      ",top="+
                      Math.floor((screen.availHeight - height) / 2)+
                      ",scrollbars=yes,resizable=yes";
      var popup = window.open(url, name, 'width='+width+',height='+height+position); 
      popup.focus();
  }
  
  function getElement(id) {
      return document.getElementById(id);
  }

  function setElementContent(id, html) {
      try{
          getElement(id).innerHTML = html;
      } catch (error) {
          alert("Error in setElementContent for element "+id+": "+error);
      }
  }
  
  function setStyle(id, name, value) {
      try{
          var elem = getElement(id);
          if (elem) {
              elem.style[name] = value;
          }
      } catch (error) {
          alert("Error in setStyleElement for "+id+": "+error);
      }
  }

  function showElement(id, show) {
      try{
          var elem = getElement(id);
          if (elem) {
              elem.style.display = show ? "" : "none";
          }
      } catch (error) {
          alert("Error in showElement for "+id+": "+error);
      }
  }
  
  function toggleElement(id, trigger, show, hide) {
      try{
          var elem = getElement(id);
          if (elem) {
              if (elem.style.display == "none") {
                elem.style.display = "";
                if (trigger) 
                    trigger.innerHTML = hide;
              } else {
                elem.style.display = "none";
                if (trigger) 
                    trigger.innerHTML = show;
              }
          }
      } catch (error) {
          alert("Error in toggleElement for "+id+": "+error);
      }
      return false;
  }

  function toggleCommentForm(link) {
      try{
          link.style.display = "none";
          getElement("commentForm").style.display = "";
          getElement("commentInput").focus();
      } catch (error) {
          alert("Error in toggleCommentForm: " + error);
      }
      return false;
  }


  function toggleCheckbox(checkbox) {
      if (checkbox.checked)
          checkbox.checked = "";
      else
          checkbox.checked = "on";
  }
  
  function toggleAll(name) {
      var list = getElement(name+"_list").childNodes;
      for (var i=0; i<list.length; i++) {
          if (list[i].firstChild) {
              if (list[i].firstChild.checked) 
                  list[i].firstChild.checked = "";
              else 
                  list[i].firstChild.checked = "on";
          }
      }
  }
  
  function addOption(element, text, value, selected) {
      var select = (selected == true);
      var opt = new Option(text, value, false, select);
      element.options[element.length] = opt;
      if (select)
          element.selectedIndex = element.length-1;
  }

  function toggleLink(elem, name) {
      var linkfield = opener.getElement(name+"_links");
      if (linkfield.value.indexOf(":"+elem.getAttribute("name")+":") > -1) {
          // link is currently set - remove
          var re = RegExp(":"+elem.getAttribute("name")+":");
          linkfield.value = linkfield.value.replace(re, ":");
          opener.buildList(name, elem);
          return "linkEditorPlain";
      } else {
          // link isn't set - add
          linkfield.value += elem.getAttribute("name")+":";
          opener.buildList(name, elem);
          return "linkEditorSelected";
      }
  }
  
  function toggleControl() {
     var control = getElement("ctrl");
     if (control) {
         if (control.style.display == "none") {
             control.style.display = "";
             document.cookie = "displayCtrlBar=;path=/";
         } else {
             control.style.display = "none";
             document.cookie = "displayCtrlBar=none;path=/";
         }
     }
     return;
  }
    
  function buildList(name, link) {
      var safeName = name.replace(/\s/g, "_");
      var value = getElement(name+"_links").value;
      var pages = value.split(":");
      var list = getElement(name+"_list");
      var str = "";
      for (var i in pages) {
          if (!pages[i])
              continue;
          str += "<div name=\"";
          str += pages[i];
          str += "\" style=\"padding: 2px;\">";
          // name_linknames is a client side array set by Page.dynalinkeditor macro
          str += eval(safeName + "_linknames[" + pages[i] + "]");
          str += "</div>";
      }
      setElementContent(name+"_list", str);
  }

  function openLinkPopup(url, name, width, height) {
      url += "&links=";
      var elem =  getElement(name + "_links");
      if (elem) {
          url += elem.value;
      }
      openPopup(url, name, width, height);
  }

    /** 
     * Create a XMLHttpRequest instance
     */
    function createAsyncRequest() {
        if (window.XMLHttpRequest) {
            // branch for native XMLHttpRequest object
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            // branch for IE/Windows ActiveX version
            return new ActiveXObject("Microsoft.XMLHTTP");
        }
        return null;
    }

    /**
     * Send a request to a XMLHttpRequest instance
     */
    function executeAsyncRequest(req, method, url, body) {
        if (!method) {
            method = "GET";
        } 
        req.open(method, url, true);
        if (method == "POST" && body) {
            req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            req.send(body);
        } else {
            try {
                req.send(null);
            } catch (ignore) {}
        }
    }
    
 
    /**
     * Call a XMLHttpRequest asynchronously.
     */
    function callAsyncRequest(url, handler, method, body) {
        var req = createAsyncRequest();
        if (!req) {
            alert("Error loading content: XMLHttpRequest not supported");
            return;
        }
        if (handler) {
            // create a wrapper that calls the request listener with the 
            // request as argument, so we don't have to keep track of it
            req.onreadystatechange = function() {
                handler.apply(null, [req]);
            };
        }
        executeAsyncRequest(req, method, url, body);
    }
  
    /**
     * Call a XMLHttpRequest asynchronously and write the returned body to the 
     * DOM element with the given id.
     */
    function loadContent(url, id, method, body) {
        var req = createAsyncRequest();
        if (!req) {
            alert("Error loading content: XMLHttpRequest not supported");
            return;
        }
        // create a wrapper that calls the request listener with the 
        // request as argument, so we don't have to keep track of it
        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                if (req.status == 200)
                    setElementContent(id, req.responseText);
                else
                    alert("Error loading content: " + req.statusText);
            }
        };
        executeAsyncRequest(req, method, url, body);
        return false;
    }


  /**
   * Catch F2 for asynchronously saving the page. 
   * FIXME: not finished/working, should use async HTTP request.
   */
  function handleKeyPress(evt) {
      if (!evt) 
          evt = window.event;
      if (evt && evt.target && evt.keyCode == 113) {
          var text = evt.target.firstChild.nodeValue;
          callAsyncRequest(baseUri + "edit.autosave", null, "POST", 
              evt.target.name + "=" + encodeURIComponent(text));
      }
      return false;
  }

  function initDocument() {
      var tas = document.getElementsByTagName("textarea");
      for (var i=0; i<tas.length; i++) {
          var ta = tas[i];
          addEvent(ta, "keypress", handleKeyPress);
      }
      if (document.edit) {
          var inputs = document.edit.getElementsByTagName("input");
          for (var i=0; i<inputs.length; i++) {
              if (inputs[i].type != "hidden") {
                  inputs[i].focus();
                  // inputs[i].select();
                  break;
              }
          }
      }
  }

