function init() {
  var ie  = document.all;
  var dom = document.getElementById;
  var ns4 = document.layers;

  if (document.getElementById('calendar')) {
    calendarInit();
  }
  
  if (document.getElementById('btnDeleteFavoriteTask')) {
    toggleMenuCheckboxes('Task', false);
  }
  if (document.getElementById('btnDeleteFavoriteView')) {
    toggleMenuCheckboxes('View', false);
  }

  // only if treeview is visible
  if (document.getElementById('treeViewDivTable')) {
    innerTableWidth = getInnerTableWidth();
    optimizeTreeSize(bOptimizeTreeSize);
  }
}

function saveCookie(name, value) {
  var now = new Date();
  var expires = new Date(now.getTime() + 1000*60*60*24*365);
  document.cookie = name + '=' + value + '; expires=' + expires.toGMTString() + ';';
}

function saveNodesCookie() {
  if (ea) {
    var expandNodesString = '';
    for (expand in ea) {
      if (ea[expand] == 1) {
        expandNodesString += expand + '_';
      }
    }
    
    expandNodesString = expandNodesString.substr(0, expandNodesString.length-1);
    saveCookie('nodesCookie', expandNodesString);
  }
}

function toggleTreeViewSub(id) {
  eval('treeViewIconId = "treeViewIcon_' + id + '";');
  eval('treeViewSubId = "treeViewSub_' + id + '";');
  isMinus = document.getElementById(treeViewIconId).src == imageMinus.src;
  document.getElementById(treeViewIconId).src = isMinus ? imagePlus.src : imageMinus.src;
  document.getElementById(treeViewSubId).style.display = isMinus ? 'none' : 'block';
  ea[id] = isMinus ? 0 : 1;
  
  if (bOptimizeTreeSize == 3) {
    // refresh innerTableWidth before calling optimizeTreeSize
    innerTableWidth = getInnerTableWidth()
    optimizeTreeSize(3);
  }
  saveNodesCookie();
}


function getInnerTableWidth() {
  document.getElementById('treeViewToolbar').style.display = 'block';
  itw = document.getElementById('treeViewDivTable').offsetWidth;
  return Math.max(itw + 10, 180);
}

// position: (1|2|3)
function optimizeTreeSize(position) {
  /* for the moment i implemented this feature only for IE */
  if (!document.all)
    return false;

  treeDiv     = document.getElementById('treeViewDiv');
  treeDivTable= document.getElementById('treeViewDivTable');
  treeToolbar = document.getElementById('treeViewToolbar');

  if (!treeDiv) 
    return false;
    
  //alert(treeDiv.style.width + '#' + treeToolbar.style.display);

  switch(position) {
    case 1:
      treeDiv.style.width = '0px';
      treeToolbar.style.display = 'none';
      break;
    case 2:
      treeDiv.style.width = '180px';
      treeToolbar.style.display = 'block';
      break;
    case 3:
      treeDiv.style.width = innerTableWidth + 'px';
      treeToolbar.style.display = 'block';
      break;
  }
  bOptimizeTreeSize = position;
  saveCookie('optimizeTreeSize', position);
  saveCookie('treeViewWidth', treeDiv.style.width);
}

// 
function disableButtons(startTag) {
  inputCollection  = startTag.getElementsByTagName('input');
  buttonCollection = startTag.getElementsByTagName('button');

  // all input-tags
  for(var x = 0; x < inputCollection.length; x++) {
    inputCollection.item(x).disabled = true;
  }

  // all button-tags
  for(var x = 0; x < buttonCollection.length; x++) {
    buttonCollection.item(x).disabled = true;
  }
}

/* hides <select> and <applet> objects (for IE only) */
function toggleElement(visibility, elmID, overDiv) {
  if (document.all) {
    for(i = 0; i < document.all.tags(elmID).length; i++) {
      obj = document.all.tags(elmID)[i];  
      if(!obj || !obj.offsetParent) {
        continue;
      }

      if (!overDiv) {
        obj.style.visibility = (visibility) ? '' : 'hidden';
      } 
      else {
        objLeft = getLeft(obj);
        objTop  = getTop(obj);
        objHeight = obj.offsetHeight;
        objWidth = obj.offsetWidth;
        
        if((overDiv.offsetLeft + overDiv.offsetWidth) <= objLeft);
        else if((overDiv.style.posTop + overDiv.offsetHeight) <= objTop);
        else if(overDiv.style.posTop >= (objTop + objHeight));
        else if(overDiv.offsetLeft >= (objLeft + objWidth));
        else {
          obj.style.visibility = (visibility) ? '' : 'hidden';
        }
      }
    }
  }
}

function move(source, dest) {
  source = eval('document.forms[0].' + source);
  dest   = eval('document.forms[0].' + dest);

  for (i = source.length - 1; i >= 0; i--) {
    if (source[i].selected) {
      newEntry = new Option(source[i].text, source[i].value);
      dest.options[dest.length] = newEntry;

      source.options[i] = null;
    }
  }
}

// source is the source-selectfield, dest is the name of the hidden input field
function fillHiddenField(source, dest) {
  retVal = '';
  source = eval('document.forms[0].' + source);
  dest   = eval('document.forms[0].' + dest);
  for (i = 0; i < source.length; i++) {
    retVal = retVal + source[i].value;
    if (i < source.length - 1) retVal = retVal + ';';
  }
  dest.value = retVal;
}

function openModalWindow(url, width, height, resizable) {
  if (document.all) {
    var sFeatures = 'dialogHeight:' + height + 'px'
                 + ';dialogWidth:'  + width  + 'px'
                 + ';edge: Raised'  
                 + ';help: no'      
                 + ';status: no'      
                 + ';scroll: no'
                 + ';resizable:'    + resizable 
                 + ';';
    window.showModalDialog(url, window, sFeatures);
  }
  else { // mozilla
    // erstes drittel des bildschirms
    wleft = screen.width / 3;
    wleft = wleft - width / 2;
    wtop  = screen.height / 3;
    wtop  = wtop - height / 2;
    
    window.open(url, '', 'width='+width+',height='+height+',left='+wleft+',top='+wtop+',modal');
  }
}

function openWindow(url, width, height, resizable, targetName, scrollbars) {
  wleft = screen.width / 3;
  wleft = wleft - width / 2;
  wtop  = screen.height / 3;
  wtop  = wtop - height / 2;
  window.open(url, targetName, 'toolbar=no,location=no,directories=no,scrollbars='+scrollbars+',status=no,menubar=no,resizable='+resizable+',width='+width+',height='+height+',left='+wleft+',top='+wtop);
}

function pop(url, width, height, resizable, targetName) {
  wleft = (screen.width - width - 16) / 2;
  wtop  = (screen.height - height - 30) / 2;
  window.open(url, targetName, 'toolbar=no,location=no,directories=no,scrollbars=yes,status=no,menubar=no,resizable='+resizable+',width='+width+',height='+height+',left='+wleft+',top='+wtop);
}

function openPrint() {
  width  = 900;
  height = 600;
  targetName = 'print';
  wleft = screen.width / 3;
  wleft = wleft - width / 2;
  wtop  = screen.height / 3;
  wtop  = wtop - height / 2;
  window.open(printUrl, targetName, 'toolbar=no,location=yes,directories=no,scrollbars=yes,status=no,menubar=yes,resizable=yes,width='+width+',height='+height+',left='+wleft+',top='+wtop);
}

//Copytext to clipboard- by Gennero (patrice.gennero@voila.fr)
//Submited to DynamicDrive.com
//Visit http://www.dynamicdrive.com for this script

bBool=false
var copiedtext=""
var tempstore=""

function initiatecopy() {
  bBool=true;
}

function copyit() {
  if (bBool) {
    tempstore=copiedtext
    document.execCommand("Copy")
    copiedtext=window.clipboardData.getData("Text");
    bBool=false;
  }
}
/*
document.onselectionchange = initiatecopy
document.onmouseup = copyit
*/

// these ones indicate if the user clicked on the 
// button to show/hide divs or into the div itself
var bShowFavorites      = false;
var bShowViews          = false;
var bShowColumns        = false;
var bShowSettings       = false;
var bShowAdministration = false;
var bShowSaveView       = false;
var bShowExcelExport    = false;
var visibleDiv          = false;

// node where to move items to - set by modal dialog-box
var destinationNodeId = false;

function getLeft(l) {
  if (l.offsetParent) return (l.offsetLeft + getLeft(l.offsetParent));
  else return (l.offsetLeft);
}

function getTop(l) {
  if (l.offsetParent) return (l.offsetTop + getTop(l.offsetParent));
  else return (l.offsetTop);
}

function toggleDiv(divName) {
  mDiv   = document.getElementById('mark' + divName + 'Div');
  sDiv   = document.getElementById('show' + divName + 'Div');

  if (document.all) {
    sDiv.style.posLeft = getLeft(mDiv);
    sDiv.style.posTop  = getTop(mDiv) + mDiv.offsetHeight + 2;
  }
  else {
    sDiv.style.left = getLeft(mDiv);
    sDiv.style.top  = getTop(mDiv) + mDiv.offsetHeight + 2;
  }
  
//alert(sDiv.offsetTop + ' ' + sDiv.style.top);
  if (sDiv.style.visibility == 'visible') {
    toggleElement(true, 'SELECT', false /* show ALL select-form-fields */);
    sDiv.style.visibility = 'hidden';
    eval('bShow' + divName + ' = false;');
    visibleDiv = false;
  }
  else {
    toggleElement(false, 'SELECT', sDiv);
    sDiv.style.visibility = 'visible';
    if (!visibleDiv) {
      eval('bShow' + divName + ' = true;');
      visibleDiv = true;
    }
  }
}

function hideDiv(divName) {
  eval('elementName = "show" + divName + "Div";');
  if (!document.getElementById(elementName))
    return false;

  eval('bShow = bShow' + divName + ';');
  visible = document.getElementById(elementName).style.visibility == "visible";
  if (!bShow && visible) {
    toggleDiv(divName);
  }
  eval('bShow' + divName + ' = false;');
}


/*****************************
 *
 * toggle delete-button of 
 * favorites-dropdown-div
 *
 */

function toggleMenuCheckboxes(type, rowId) {
  if (rowId) {
    eval('checked = document.getElementById("cbMenu' + type + '_' + rowId + '" ).checked');
    eval('row = document.getElementById("rowMenu' + type + '_' + rowId + '" )');
    row.bgColor = checked ? '#ddeeff' : '#ffffff';
  }

  existsChecked   = false;
  eval('elementName = "aggregateFavorite' + type + '[]";');
  eval('form = document.favorite' + type + 'Form;');
  for (var i = 0; i < form.elements.length; i++) {
    var j = form.elements[i];
    if (j.name == elementName) {
      if (j.checked)  existsChecked = true;
    }
  }

  document.getElementById('btnDeleteFavorite' + type).disabled = !existsChecked;
}

/*****************************
 *
 * color rows of 
 * favorites-dropdown-div
 * on mouseover/-out
 *
 */

function rowMenuMouseOver(type, rowId) {
  checked = false;
  eval('if (document.getElementById("cbMenu' + type + '_' + rowId + '" )) checked = document.getElementById("cbMenu' + type + '_' + rowId + '" ).checked');
  eval('row = document.getElementById("rowMenu' + type + '_' + rowId + '" )');
  row.bgColor = checked ? '#ffffff' : '#99ccff';
  row.border = '1px solid #000000;';
}

function rowMenuMouseOut(type, rowId) {
  checked = false;
  eval('if (document.getElementById("cbMenu' + type + '_' + rowId + '" )) checked = document.getElementById("cbMenu' + type + '_' + rowId + '" ).checked');
  eval('row = document.getElementById("rowMenu' + type + '_' + rowId + '" )');
  row.bgColor = checked ? '#ddeeff' : '#ffffff';
}

function createTaskHandler() {
  location.href = baseUrl + 'Task/edit.php?nodeId=' + lastNodeId + '&itemId=-1';
}

function editNodeHandler() {
  location.href = baseUrl + 'Node/edit.php?itemId=' + lastNodeId;
}

function moveItemHandler() {
  openModalWindow(baseUrl + 'Dialog/MoveDialog.php', 420, 240, 'no');
  return destinationNodeId; 
}

function showNodeInfo() {
  openModalWindow(baseUrl + 'Dialog/NodeInfoDialog.php?actionNodeId=' + lastNodeId, 420, 370, 'no');
  hideNodeMenu();
}

function moveNodeHandler() {
  openModalWindow(baseUrl + 'Dialog/MoveDialog.php?actionNodeId=' + lastNodeId, 420, 370, 'no');
  hideNodeMenu();
  if (destinationNodeId == false) 
    return false;
  document.location = cleanUrl + '?nodeAction=moveNode&actionNodeId=' + lastNodeId + '&destinationNodeId=' + destinationNodeId;
}

function deleteNodeHandler() {
  hideNodeMenu();
  if (!confirm('Sind Sie sicher?')) 
    return false;
    
  location.href = baseUrl + '?nodeAction=deleteNode&actionNodeId=' + lastNodeId;
}

function archiveNodeHandler() {
  hideNodeMenu();
  location.href = baseUrl + '?nodeAction=archiveNode&actionNodeId=' + lastNodeId;
}

function activateNodeHandler() {
  hideNodeMenu();
  location.href = baseUrl + '?nodeAction=activateNode&actionNodeId=' + lastNodeId;
}

function addSubNodeNodeHandler() {
  location.href = baseUrl + 'Node/edit.php?itemId=-1&nodeId=' + lastNodeId;
}
  

function TADropDownMenuMouseOver(obj) {
  obj.style.cursor="hand";
  obj.style.backgroundColor="#99ccff";
}

function TADropDownMenuMouseOut(obj) {
  obj.style.backgroundColor="#ffffff";
}

var lastNodeId;

function openNodeMenu(node, nodeId, attributeByte) {
  var BIT_EDIT        = 1;
  var BIT_ADD_SUBNODE = 2;
  var BIT_MOVE        = 4;
  var BIT_ARCHIVE     = 8;
  var BIT_ACTIVATE    = 16;
  var BIT_DELETE      = 32;
  var BIT_CREATE_TASK = 64;

  nodeHtml = '<table class="dropdown">';
  if (attributeByte & BIT_CREATE_TASK) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="createTaskHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuCreateTask + '</td></tr>';
  }
  if (attributeByte & BIT_EDIT) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="editNodeHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuEdit + '</td></tr>';
  }
  if (attributeByte & BIT_ADD_SUBNODE) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="addSubNodeNodeHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuAddSubnode + '</td></tr>';
  }
  if (attributeByte & BIT_MOVE) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="moveNodeHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuMove + '</td></tr>';
  }
  if (attributeByte & BIT_ARCHIVE) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="archiveNodeHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuArchive + '</td></tr>';
  }
  if (attributeByte & BIT_ACTIVATE) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="activateNodeHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuActivate + '</td></tr>';
  }
  if (attributeByte & BIT_DELETE) {
    nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="deleteNodeHandler()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuDelete + '</td></tr>';
  }
  nodeHtml+= '<tr><td bgcolor="#000000" style="padding:0px;"><img src="'+imageUrl+'1pix.gif" width="1" height="1" alt=""></td></tr>';
  nodeHtml+= '<tr><td onmouseover="TADropDownMenuMouseOver(this)" onmouseout="TADropDownMenuMouseOut(this)" onmousedown="showNodeInfo()" style="cursor:pointer;cursor:hand;">' + textKeyNodePopMenuNodeInfo + '</td></tr>';
  nodeHtml+= '</table>';
  document.getElementById('nodeMenuDiv').innerHTML = nodeHtml;

  sDiv = document.getElementById('nodeMenuDiv');
  if (sDiv.style.visibility != 'hidden') {
    sDiv.style.visibility = 'hidden';
  }

  if (document.all) {
    sDiv.style.posLeft = getLeft(node);
    sDiv.style.posTop  = getTop(node) + 17;
  }
  else {
    sDiv.style.left = getLeft(node);
    sDiv.style.top  = getTop(node) + 17;
  }
  sDiv.style.visibility = 'visible';

  // cancelBubble: elsewise, document.onclick-handler "hideDivsOnClick" (see PageHandler.thtml) will close popup immediatly
  if (!visibleDiv) {
    window.event.cancelBubble = true;
  }
  lastNodeId = nodeId;
}

function hideNodeMenu() {
  sDiv = document.getElementById('nodeMenuDiv');
  sDiv.style.visibility = 'hidden';
}

// i do not use this one at the moment (ay, 3.10.2002)
function includeDyn(js) {
  document.write('<script src="' + baseUrl + js + '">');
  document.write('</scr' +  'ipt>');
}