/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Tree
// Moregroupware.org

var ie4=document.all;
// &&navigator.userAgent.indexOf("Opera")==-1
var ns6=document.getElementById&&!document.all;
var ns4=document.layers;
var newfolder_subfolder="-1";
var newfolder_function="new";
var newfolder_account="0";
var newfolder_id="";

function showmenu(e,account,id){
  var linkset='';
  
  bit_sidebar=false;
  bit_protect=true;
  bit_confirm=false;
  is_folder=false;
  is_public=false;
  
  imgpath="templates/default/media/";

  seperator='<img src="'+imgpath+'spacer.gif" alt="" width="1" height="3" border="0"><br>';
  seperator+='<img src="'+imgpath+'spacer_grey.gif" alt="" width="90" height="1" border="0"><br>';
  seperator+='<img src="'+imgpath+'spacer.gif" alt="" width="1" height="3" border="0"><br>';

  if (id >= 0) {
    thisfield=eval("field_"+account+"_"+id);
    bit_sidebar=thisfield.bit_sidebar;
    bit_protect=thisfield.bit_protect;
    bit_confirm=thisfield.bit_confirm;
    is_public=thisfield.ispublic;
    is_folder=true;
  }
  if (id==-2) {
    is_public=true;
  }

  markedmails=false;
  if (parent.rightframe.maillist && typeof(parent.rightframe.maillist.marked_count) != "undefined" && parent.rightframe.maillist.marked_count > 0) {
    myparent=parent.rightframe.maillist;
          markedmails=true; 
        }
  
  linkset='<div class="menuitems" onclick="openfolder('+account+','+id+');"><a href="#" onclick="return false;">'+lang_open+'</a></div>'
  linkset+=seperator
  
  if (is_folder && (!bit_protect || is_public || account!=0)) {
    mystr = "";
    if (bit_sidebar) {
      if (lang_sidebar_rem_2 != "") {
        mystr = '<br><a href="#" onclick="return false;">'+lang_sidebar_rem_2+'</a>';
      }
      linkset+='<div class="menuitems" onclick="modify_sidebar('+account+','+id+');"><a href="#" onclick="return false;">'+lang_sidebar_rem_1+'</a>'+mystr+'</div>'
    } else {
      if (lang_sidebar_add_2 != "") {
        mystr = '<br><a href="#" onclick="return false;">'+lang_sidebar_add_2+'</a>';
      }
      linkset+='<div class="menuitems" onclick="modify_sidebar('+account+','+id+');"><a href="#" onclick="return false;">'+lang_sidebar_add_1+'</a>'+mystr+'</div>'
    }
    linkset+=seperator
  }
  // linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_cut+'</a></div>'
  // linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_copy+'</a></div>'
  // linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_paste+'</a></div>'
  // linkset+=seperator

  if (markedmails && account==myparent.account) {
    linkset+='<div class="menuitems" onclick="myparent.copysubmit('+id+','+account+')"><a href="#" onclick="return false;">'+lang_copy_mails+'</a></div>'
    linkset+='<div class="menuitems" onclick="myparent.movesubmit('+id+','+account+')"><a href="#" onclick="return false;">'+lang_move_mails+'</a></div>'
  } else {
    linkset+='<div class="menuitems2"><a href="#" onclick="return false">'+lang_copy_mails+'</a></div>'
    linkset+='<div class="menuitems2"><a href="#" onclick="return false">'+lang_move_mails+'</a></div>'
  }
  linkset+=seperator

  if (bit_confirm && account == 0) {
    linkset+='<div class="menuitems" onclick="empty_trash('+account+','+is_public+');"><a href="#" onclick="return false;">'+lang_empty_trash+'</a></div>'
  }
  if (!bit_protect) {
    linkset+='<div class="menuitems" onclick="deletefolder('+account+','+id+','+bit_confirm+');"><a href="#" onclick="return false;">'+lang_delete+'</a></div>'
    linkset+='<div class="menuitems" onclick="renamefolder('+account+','+id+');"><a href="#" onclick="return false;">'+lang_rename+'</a></div>'
  } else {
    linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_delete+'</a></div>'
    linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_rename+'</a></div>'
  }
  
  linkset+=seperator
  linkset+='<div class="menuitems" onclick="newfolder('+account+','+id+');"><a href="#" onclick="return false;">'+lang_newfolder+'</a></div>'

  linkset+=seperator
  if (account == 0) {
    linkset+='<div class="menuitems" onclick="newlink('+account+','+id+');"><a href="#" onclick="return false;">'+lang_newlink+'</a></div>'
  } else {
    linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_newlink+'</a></div>'
  }

  linkset+=seperator
  
  if (account == 0) {
    linkset+='<div class="menuitems" onclick="properties('+account+','+id+');"><a href="#" onclick="properties('+account+','+id+'); return false;">'+lang_properties+'</a></div>'
  } else {
    linkset+='<div class="menuitems2"><a href="#" onclick="return false;">'+lang_properties+'</a></div>'
  }

  which=linkset;
  
  if (!document.all&&!document.getElementById&&!document.layers) return

  clearhidemenu()
  menuobj=ie4? document.all.popmenu : ns6? document.getElementById("popmenu") : ns4? document.popmenu : ""
  menuobj.thestyle=(ie4||ns6)? menuobj.style : menuobj

  if (ie4||ns6) { 

  menuobj.innerHTML=which; 
  } else {
      menuobj.document.write('<layer name=gui bgColor=#E6E6E6 width=100 onmouseover="clearhidemenu()" onmouseout="hidemenu()">'+which+'</layer>')
      menuobj.document.close()
  }

  var tabA = menuobj.getElementsByTagName('A');
  var w=0;
  var i;
  for (i=0; i<tabA.length ;i++) if (tabA[i].offsetWidth > w) w = tabA[i].offsetWidth;
  if (w > 100) menuobj.style.width=w;
  
  menuobj.contentwidth=(ie4||ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width
  menuobj.contentheight=(ie4||ns6)? menuobj.offsetHeight : menuobj.document.gui.document.height
  eventX=ie4? event.clientX : ns6? e.clientX : e.x
  eventY=ie4? event.clientY : ns6? e.clientY : e.y
  
  var rightedge=ie4? document.body.clientWidth-eventX : window.innerWidth-eventX
  var bottomedge=ie4? document.body.clientHeight-eventY : window.innerHeight-eventY

  if (rightedge<menuobj.contentwidth) {
	menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX
//    menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX-menuobj.contentwidth : ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth;
  } else {
    menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX
  }

  if (bottomedge<menuobj.contentheight) {
    menuobj.thestyle.top=ie4? document.body.scrollTop+eventY-menuobj.contentheight : ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight;
  } else {
    menuobj.thestyle.top=ie4? document.body.scrollTop+event.clientY : ns6? window.pageYOffset+eventY : eventY;
  }
  
  if (ie4||ns6||ns4) {
    hidemenu();
    if (window.delayhide2) { clearTimeout(delayhide2); }
    if (window.delayhide) { clearTimeout(delayhide); }

    delayhide2=setTimeout("showmenu2()",10);
  }

  return false
}

function showmenu_link(e,id,idnum,thelink,thetarget,thedesc){
  var linkset='';

  imgpath="templates/default/media/";

  seperator='<img src="'+imgpath+'spacer.gif" alt="" width="1" height="3" border="0"><br>';
  seperator+='<img src="'+imgpath+'spacer_grey.gif" alt="" width="90" height="1" border="0"><br>';
  seperator+='<img src="'+imgpath+'spacer.gif" alt="" width="1" height="3" border="0"><br>';

  linkset='<div class="menuitems" onclick="clickOnLink(\''+id+'\',\''+thelink+'\',\''+thetarget+'\')"><a href="#" onclick="return false;">'+lang_open+'</a></div>'
  linkset+=seperator

  linkset+='<div class="menuitems" onclick="deletelink(\''+idnum+'\');"><a href="#" onclick="return false;">'+lang_delete+'</a></div>'
  linkset+='<div class="menuitems" onclick="editlink(\''+id+'\');"><a href="#" onclick="return false;">'+lang_edit+'</a></div>'
  
  which=linkset;
  
  if (!document.all&&!document.getElementById&&!document.layers) return

  clearhidemenu()
  menuobj=ie4? document.all.popmenu : ns6? document.getElementById("popmenu") : ns4? document.popmenu : ""
  menuobj.thestyle=(ie4||ns6)? menuobj.style : menuobj

  if (ie4||ns6) { 

  menuobj.innerHTML=which; 
  } else {
      menuobj.document.write('<layer name=gui bgColor=#E6E6E6 width=100 onmouseover="clearhidemenu()" onmouseout="hidemenu()">'+which+'</layer>')
      menuobj.document.close()
  }

  var tabA = menuobj.getElementsByTagName('A');
  var w=0;
  var i;
  for (i=0; i<tabA.length ;i++) if (tabA[i].offsetWidth > w) w = tabA[i].offsetWidth;
  if (w > 100) menuobj.style.width=w;
  
  menuobj.contentwidth=(ie4||ns6)? menuobj.offsetWidth : menuobj.document.gui.document.width
  menuobj.contentheight=(ie4||ns6)? menuobj.offsetHeight : menuobj.document.gui.document.height
  eventX=ie4? event.clientX : ns6? e.clientX : e.x
  eventY=ie4? event.clientY : ns6? e.clientY : e.y
  
  //Find out how close the mouse is to the corner of the window
  var rightedge=ie4? document.body.clientWidth-eventX : window.innerWidth-eventX
  var bottomedge=ie4? document.body.clientHeight-eventY : window.innerHeight-eventY

  //if the horizontal distance isn't enough to accomodate the width of the context menu
  if (rightedge<menuobj.contentwidth) {
    //move the horizontal position of the menu to the left by it's width
    menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX-menuobj.contentwidth : ns6? window.pageXOffset+eventX-menuobj.contentwidth : eventX-menuobj.contentwidth;
  } else {
    //position the horizontal position of the menu where the mouse was clicked
    menuobj.thestyle.left=ie4? document.body.scrollLeft+eventX : ns6? window.pageXOffset+eventX : eventX
  }

  //same concept with the vertical position
  if (bottomedge<menuobj.contentheight) {
    menuobj.thestyle.top=ie4? document.body.scrollTop+eventY-menuobj.contentheight : ns6? window.pageYOffset+eventY-menuobj.contentheight : eventY-menuobj.contentheight;
  } else {
    menuobj.thestyle.top=ie4? document.body.scrollTop+event.clientY : ns6? window.pageYOffset+eventY : eventY;
  }
  
  if (ie4||ns6||ns4) {
    hidemenu();
    if (window.delayhide2) { clearTimeout(delayhide2); }
    if (window.delayhide) { clearTimeout(delayhide); }

    delayhide2=setTimeout("showmenu2()",10);
  }

  return false
}

function showmenu2(){
  menuobj.thestyle.visibility="visible"
}

function modify_sidebar(account,id) {

  if (parent.mailfolders) {
    myfield=eval('field_'+account+'_'+id);
    if (myfield.bit_sidebar) {
      myfield.bit_sidebar=false;
      parent.mailfolders.location="folders.php?account="+account+"&action=remove&subfolder="+id+"&"+session;
    } else {
      myfield.bit_sidebar=true;
      parent.mailfolders.location="folders.php?account="+account+"&action=add&subfolder="+id+"&"+session;
    }
  }
  return false;
}

function openfolder(account,id) {
  
  if (parent.rightframe) {
    tframe=parent.rightframe;
    if (id == -2) {
      highlightObjLink(fieldpublic);
      tframe.location="myfolders.php?mode=public&"+session;
    } else if (id == -1) {
      highlightObjLink(foldersTree);
      tframe.location="myfolders.php?mode=my&"+session;
    } else {
      highlightObjLink(eval("field_"+account+"_"+id));
      tframe.location="index2.php?account="+account+"&mbox_id="+id+"&"+session;
    }
  }
  return false;
}

function properties(account,id) {
  
  if (parent.rightframe) {
    tframe=parent.rightframe;

    if (id == -2) {
      highlightObjLink(fieldpublic);
    } else if (id == -1) {
      highlightObjLink(foldersTree);
    } else {
      highlightObjLink(eval("field_"+account+"_"+id));
    }
    tframe.location="properties.php?account="+account+"&mbox_id="+id+"&"+session;
  }
  return false;
}

function show_new_form () {

  Browser = navigator.appName;
  if (navigator.appName.substring(0,9) == "Microsoft") Browser = 'MSIE';
  if (Browser != 'MSIE') {
    width=document.body.clientWidth;
    height=document.body.clientHeight+5;
  } else {
    width=document.body.clientWidth;
    height=document.body.clientHeight;
  }

  if (document.getElementById('create_new_form')) {

    myfield2=document.getElementById('create_new_form_link');
    myfield2.style.visibility = 'hidden';
    myfield2=document.getElementById('create_new_form');
    myfield2.style.visibility = 'visible';

    myfield2.style.left=(width-164) / 2
    myfield2.style.top=ie4? (height-80)+document.body.scrollTop : (height-80)+window.pageYOffset;
  }
}

function show_new_form_link () {

  Browser = navigator.appName;
  if (navigator.appName.substring(0,9) == "Microsoft") Browser = 'MSIE';
  if (Browser != 'MSIE') {
    width=document.body.clientWidth;
    height=document.body.clientHeight+5;
  } else {
    width=document.body.clientWidth;
    height=document.body.clientHeight;
  }

  if (document.getElementById('create_new_form_link')) {

    myfield2=document.getElementById('create_new_form');
    myfield2.style.visibility = 'hidden';

    myfield2=document.getElementById('create_new_form_link');
    myfield2.style.visibility = 'visible';

    myfield2.style.left=(width-164) / 2
    myfield2.style.top=ie4? (height-120)+document.body.scrollTop : (height-120)+window.pageYOffset;
  }
}

function renamefolder(account,id) {

  if (id != -1 && id != -2) {
    show_new_form();
    
    myfield=eval('field_'+account+'_'+id);

    newfolder_function="rename";
    newfolder_subfolder = id;
    newfolder_account = account;
    test=getElById('newfolder_field');
    test3=getElById('newfolder_field2');
    test2=getElById('newfolder_input');
    myfield=eval('field_'+account+'_'+id);

    test.value=renamefolder_str;
    test2.value=myfield.desc;    
    test3.value="(Renaming \""+myfield.desc+"\")";    
  }
  
  return false;
}

function do_rename_folder() {

  if (newfolder_function=="rename") {

    test2=getElById('newfolder_input');
    top.treeframe.location="tree.php?account="+newfolder_account+"&rename="+newfolder_subfolder+"&mboxname="+test2.value+"&"+session;

    newfolder_function="new";
    newfolder_account="0";
    test=getElById('newfolder_field');
    test.value=newfolder_str;
    newfolder_subfolder=-1;
    test2.value="";
    
    myfield2=document.getElementById('create_new_form');
    myfield2.style.visibility = 'hidden';

  } else {
    do_new_folder();
  }
}

function do_new_folder() {

  test2=getElById('newfolder_input');
  if (test2.value != "") {
    highlightObjLink(foldersTree);
    document.location="tree.php?account="+newfolder_account+"&createnew="+newfolder_subfolder+"&mboxname="+test2.value+"&"+session;
  } else {
    myfield2=document.getElementById('create_new_form');
    myfield2.style.visibility = 'hidden';
  }
}

function do_new_link() {
  test=getElById('newlink_input');
  test2=getElById('newlink_input2');
  test3=getElById('newlink_input3');
  
  if (test.value != "" && test2.value != "") {
    if (test3.checked) {test3.value="checked";}
    highlightObjLink(foldersTree);
    document.location="tree.php?link_id="+newfolder_id+"&account="+newfolder_account+"&createnewlink="+newfolder_subfolder+"&href="+test.value+"&desc="+test2.value+"&newwindow="+test3.value+"&"+session;
  } else {
    myfield2=document.getElementById('create_new_form_link');
    myfield2.style.visibility = 'hidden';
  }
}

function hide_link_form() {
  myfield2=document.getElementById('create_new_form_link');
  myfield2.style.visibility = 'hidden';
}

function hide_folder_form() {
  myfield2=document.getElementById('create_new_form');
  myfield2.style.visibility = 'hidden';
}

function newfolder(account,id) {
  
  show_new_form();

  if (id == -1) {
    id="foldersTree";
    newfolder_subfolder=-1;
  } else if (id == -2) {
    id="fieldpublic";
    newfolder_subfolder=-2;
  } else {
    newfolder_subfolder=id;
    id="field_"+account+"_"+id;
  }
  newfolder_account=account;
  newfolder_function="new";
  test=getElById('newfolder_field');
  test2=getElById('newfolder_field2');

  myfield=eval(id);
  if (id == -1) {
    test.value=newfolder_str;
    test2.value="("+subfolder_of+" \""+myfield.desc+"\")";
  } else {
    test.value=newsubfolder;
    test2.value="("+subfolder_of+" \""+myfield.desc+"\")";
  }
  return false;
}

function newlink(account,id) {
  
  show_new_form_link();

  if (id == -1) {
    id="foldersTree";
    newfolder_subfolder=-1;
  } else if (id == -2) {
    id="fieldpublic";
    newfolder_subfolder=-2;
  } else {
    newfolder_subfolder=id;
    id="field_"+account+"_"+id;
  }
  newfolder_account=account;
  newfolder_id = "";
  
  test=getElById('newlink_input');
  test2=getElById('newlink_input2');
  test3=getElById('newlink_input3');
  
  test.value="http://";
  test2.value="";
  test3.checked=false;

  test=getElById('newlink_field');
  test2=getElById('newlink_field2');

  myfield=eval(id);

  test.value=newlink_str;
  test2.value="("+sublink_of+" \""+myfield.desc+"\")";

  return false;
}

function editlink(id) {
  
  show_new_form_link();

  myfield = findObj(id);

  myfield2 = myfield.parentObj;
  
  test=getElById('newlink_input');
  test2=getElById('newlink_input2');
  test3=getElById('newlink_input3');

  test.value = myfield.link.replace("norefer.php?url=","");
  test2.value = myfield.desc;

  newfolder_subfolder = myfield2.idnum;
  newfolder_account = myfield2.account;
  newfolder_id = myfield.idnum;
  
  if (myfield.target!="rightframe") {
    test3.checked = true;
  }

  test=getElById('newlink_field');
  test2=getElById('newlink_field2');

  test.value=editlink_str;
  test2.value="("+sublink_of2+" \""+myfield2.desc+"\")";

  return false;
}

function deletefolder(account,id,bit_confirm) {
  if (!bit_confirm || confirm(confirm_delete+" '"+eval('field_'+account+'_'+id+'.desc')+"' ?")) {
    highlightObjLink(foldersTree);
    document.location="tree.php?account="+account+"&delete="+id+"&"+session;
  }
  return false;
}

function deletelink(id) {
  document.location="tree.php?delete_link="+id+"&"+session;
  return false;
}

function empty_trash(account,is_public) {
  if (confirm(confirm_empty_trash)) {
    highlightObjLink(foldersTree);
    document.location="tree.php?account="+account+"&is_public="+is_public+"&empty_trash=true&"+session;
  }
  return false;
}

function contains_ns6(a, b) {
  //Determines if 1 element in contained in another- by Brainjar.com
  while (b.parentNode) {
    if ((b = b.parentNode) == a) {
      return true;
    }
  }
  return false;
}

function hidemenu(){
  if (window.menuobj) menuobj.thestyle.visibility=(ie4||ns6)? "hidden" : "hide"
}

function dynamichide(e){
if (ie4&&!menuobj.contains(e.toElement)) { hidemenu(); }
  else if (ns6&&e.currentTarget!= e.relatedTarget&& !contains_ns6(e.currentTarget, e.relatedTarget)) { hidemenu(); }
}

function delayhidemenu(){
  if (ie4||ns6||ns4) {
    if (window.delayhide2) { clearTimeout(delayhide2); }
    delayhide=setTimeout("hidemenu()",500);
  }
}

function clearhidemenu() {
  if (window.delayhide) { clearTimeout(delayhide); }
}

function highlightmenu(e,state){
  if (document.all) {
    source_el=event.srcElement;
  } else if (document.getElementById) {
    source_el=e.target
  }
  if (source_el.className=="menuitems" || source_el.className=="menuitems2") {
    source_el.id=(state=="on")? "mouseoverstyle" : ""
  } else {
    while (source_el.id!="popmenu") {
      source_el=document.getElementById? source_el.parentNode : source_el.parentElement;
      if (source_el.className=="menuitems" || source_el.className=="menuitems2") {
        source_el.id=(state=="on")? "mouseoverstyle" : ""
      }
    }
  }
}

function op() {}

if (ie4||ns6) {
  if (window.delayhide2) { clearTimeout(delayhide2); }
  document.onclick=hidemenu;
}

function setreads(account,num,read,unread) {

  str = 'fielddesc_'+account+'_'+num;
  str2 = 'field_'+account+'_'+num;

  if (is_loaded && typeof(num) != 'undefined' && typeof(read) != 'undefined' && typeof(unread) != 'undefined' && typeof(test=document.getElementById(str))=="object") {

    if (eval(str2)) {
      myfield=eval(str2);

      myfield.read=read;
      myfield.unread=unread;
    
      if (test != null && typeof(test) == 'object') {
        mydesc=" ";
		second="";
		third="";
        if (myfield.read!=0) {
          if (myfield.unread!=0) {
		    mydesc = "(";
			second = myfield.unread;
			third = " / "+myfield.read+")  ";
          } else {
		    mydesc = "("+myfield.read+")  ";
		  }
        }
        test.firstChild.nodeValue=myfield.desc+" "+mydesc;
		test.childNodes[1].firstChild.nodeValue = second;
		test.childNodes[2].firstChild.nodeValue = third;
      }
    }
  }
}

function show_new_folder(account,id) {
  if (typeof(id) != 'undefined' && typeof(account) != 'undefined') {
    parent.rightframe.location="index2.php?account="+account+"&mbox_id="+id+"&"+session;
  }
}

var is_loaded = false;

function openfield(account,id) {

    str = "field_"+account+"_"+id;
    
    if (is_loaded && typeof(eval(str)) != "undefined" && typeof(eval(str)) != null) {
      test=eval(str);
      test.forceOpeningOfAncestorFolders();
      highlightObjLink(eval(str));
      
      if (!eval(str).isOpen) {
        clickOnNodeObj(eval(str));
      }
    }
}
