Room links in iconbar roomlist now properly escaped with encodeURIcomponent()
[citadel.git] / webcit / static / wclib.js
1 /*
2  * JavaScript function library for WebCit.
3  *
4  * Copyright (c) 2005-2011 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20
21
22 var browserType;
23 var room_is_trash = 0;
24
25 var currentlyExpandedFloor = null;
26 var roomlist = null;
27 var currentDropTarget = null;
28
29 var supportsAddEventListener = (!!document.addEventListener);
30 var today = new Date();
31
32 var wc_log = "";
33 if (document.all) {browserType = "ie"}
34 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
35         browserType= "gecko";
36 }
37 var ns6=document.getElementById&&!document.all;
38 Event.observe(window, 'load', ToggleTaskDateOrNoDateActivate);
39 Event.observe(window, 'load', taskViewActivate);
40 //document.observe("dom:loaded", setupPrefEngine);
41 document.observe("dom:loaded", setupIconBar);
42 function CtdlRandomString()  {
43         return((Math.random()+'').substr(3));
44 }
45 function strcmp ( str1, str2 ) {
46     // http://kevin.vanzonneveld.net
47     // +   original by: Waldo Malqui Silva
48     // +      input by: Steve Hilder
49     // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
50     // +    revised by: gorthaur
51     // *     example 1: strcmp( 'waldo', 'owald' );
52     // *     returns 1: 1
53     // *     example 2: strcmp( 'owald', 'waldo' );
54     // *     returns 2: -1
55  
56     return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
57 }
58
59
60
61 function ToggleVisibility ($Which)
62 {
63         if (document.getElementById)
64         {
65                 if (document.getElementById($Which).style.display  == "none")
66                         document.getElementById($Which).style.display  = "inline";
67                 else
68                         document.getElementById($Which).style.display  = "none";
69         }
70 }
71
72 function emptyElement(element) {
73   childNodes = element.childNodes;
74   for(var i=0; i<childNodes.length; i++) {
75     try {
76     element.removeChild(childNodes[i]);
77     } catch (e) {
78       WCLog(e+"|"+e.description);
79     }
80   }
81 }
82 /** Implements superior internet explorer 'extract all child text from element' feature'. Falls back on buggy, patent violating standardized method */
83 function getTextContent(element) {
84   if (element.textContent == undefined) {
85     return element.innerText;
86   }
87   return element.textContent;
88 }
89 /** Same reasons as above */
90 function setTextContent(element, textContent) {
91   if(element.textContent == undefined) {
92     element.innerText = textContent;
93   } else {
94   element.textContent = textContent;
95   }
96 }
97
98 // We love string tokenizers.
99 function extract_token(source_string, token_num, delimiter) {
100         var i = 0;
101         var extracted_string = source_string;
102
103         if (token_num > 0) {
104                 for (i=0; i<token_num; ++i) {
105                         var j = extracted_string.indexOf(delimiter);
106                         if (j >= 0) {
107                                 extracted_string = extracted_string.substr(j+1);
108                         }
109                 }
110         }
111
112         j = extracted_string.indexOf(delimiter);
113         if (j >= 0) {
114                 extracted_string = extracted_string.substr(0, j);
115         }
116
117         return extracted_string;
118 }
119
120 function CtdlSpawnContextMenu(event, source) {
121   // remove any existing menus
122   disintergrateContextMenus(null);
123   var x = event.clientX-10; // cut a few pixels out so our mouseout works right
124   var y = event.clientY-10;
125   var contextDIV = document.createElement("div");
126   contextDIV.setAttribute("id", "ctdlContextMenu");
127   document.body.appendChild(contextDIV);
128   var sourceChildren = source.childNodes;
129   for(var j=0; j<sourceChildren.length; j++) {
130     contextDIV.appendChild(sourceChildren[j].cloneNode(true));
131   }
132   var leftRule = "left: "+x+"px;";
133   contextDIV.setAttribute("style", leftRule);
134   contextDIV.setAttribute("actual", leftRule);
135   contextDIV.style.top = y+"px";
136   contextDIV.style.display = "block";
137   $(contextDIV).observe('mouseout',disintergrateContextMenus);
138 }
139 function disintergrateContextMenus(event) {
140   var contextMenu = document.getElementById("ctdlContextMenu");
141   if (contextMenu) {
142     contextMenu.parentNode.removeChild(contextMenu);
143   }
144   Event.stopObserving(document,'click',disintergrateContextMenus);
145 }
146 // This code handles the popups for important-messages.
147 function hide_imsg_popup() {
148         if (browserType == "gecko") {
149                 document.poppedLayer = eval('document.getElementById(\'important_message\')');
150         }
151         else if (browserType == "ie") {
152                 document.poppedLayer = eval('document.all[\'important_message\']');
153         }
154         else {
155                 document.poppedLayer = eval('document.layers[\'`important_message\']');
156         }
157
158         document.poppedLayer.style.visibility = "hidden";
159 }
160
161
162 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
163 function activate_entmsg_autocompleters() {
164         new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
165         new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
166         new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
167 }
168
169 function activate_iconbar_wholist_populat0r() 
170 {
171         new Ajax.PeriodicalUpdater('online_users', 'do_template?template=who_iconbar', {method: 'get', frequency: 30});
172 }
173
174 function setupIconBar() {
175
176         /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
177         if ( $('expand_roomlist').src.substring($('expand_roomlist').src.length - 12) == "collapse.gif" ) {
178                 $('roomlist').style.display = 'block';
179                 $('roomlist').innerHTML = '';
180                 FillRooms(IconBarRoomList);
181         }
182         else {
183                 $('roomlist').style.display = 'none';
184         }
185
186         /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
187         if ( $('expand_wholist').src.substring($('expand_wholist').src.length - 12) == "collapse.gif" ) {
188                 $('online_users').style.display = 'block';
189                 activate_iconbar_wholist_populat0r();
190         }
191         else {
192                 $('online_users').style.display = 'none';
193         }
194
195 }
196
197 function GenericTreeRoomList(roomlist) {
198   var currentExpanded = ctdlLocalPrefs.readPref("rooms_expanded");
199   var curRoomName = "";
200   if (document.getElementById("rmname")) {
201     curRoomName = getTextContent(document.getElementById("rmname"));
202   }
203   currentDropTargets = new Array();
204   var iconbar = document.getElementById("iconbar");
205   var ul = document.createElement("ul");
206   roomlist.appendChild(ul);
207   // Add mailbox, because they are special
208   var mailboxLI = document.createElement("li");
209   ul.appendChild(mailboxLI);
210   var mailboxSPAN = document.createElement("span");
211   var _mailbox = getTextContent(document.getElementById("mbox_template"));
212   mailboxSPAN.appendChild(document.createTextNode(_mailbox));
213   $(mailboxSPAN).observe('click', expandFloorEvent);
214   mailboxLI.appendChild(mailboxSPAN);
215   mailboxLI.className = "floor";
216   var mailboxUL = document.createElement("ul");
217   mailboxLI.appendChild(mailboxUL);
218   var mailboxRooms = GetMailboxRooms();
219   for(var i=0; i<mailboxRooms.length; i++) {
220           var room = mailboxRooms[i];
221           currentDropTargets.push(addRoomToList(mailboxUL, room, curRoomName));
222   }
223   if (currentExpanded != null && currentExpanded == _mailbox ) {
224           expandFloor(mailboxSPAN);
225   }
226   for(var a=0; a<floors.length; a++) {
227           var floor = floors[a];
228           var floornum = floor[0];
229     
230           if (floornum != -1)
231           {
232
233                   var name = floor[1];
234                   var floorLI = document.createElement("li");
235                   ul.appendChild(floorLI);
236                   var floorSPAN = document.createElement("span");
237                   floorSPAN.appendChild(document.createTextNode(name));
238                   $(floorSPAN).observe('click', expandFloorEvent);
239                   floorLI.appendChild(floorSPAN);
240                   floorLI.className = "floor";
241                   var floorUL = document.createElement("ul");
242                   floorLI.appendChild(floorUL);
243                   var roomsForFloor = GetRoomsByFloorNum(floornum);
244                   for(var b=0; b<roomsForFloor.length; b++) {
245                           var room = roomsForFloor[b];
246                           currentDropTargets.push(addRoomToList(floorUL, room, curRoomName));
247                   }
248                   if (currentExpanded != null && currentExpanded == name) {
249                           expandFloor(floorSPAN);
250                   }
251     }
252   }
253 }
254 function IconBarRoomList() {
255   roomlist = document.getElementById("roomlist");
256   GenericTreeRoomList(roomlist);
257 }
258 function KNRoomsRoomList() {
259   roomlist = document.getElementById("roomlist_knrooms");
260   GenericTreeRoomList(roomlist);
261 }
262
263 function addRoomToList(floorUL,room, roomToEmphasize) {
264   var roomName = room[RN_ROOM_NAME];
265   var flag = room[RN_ROOM_FLAG];
266   var curView = room[RN_CUR_VIEW];
267   var view = room[RN_DEF_VIEW];
268   var raflags = room[RN_RAFLAGS];
269   var isMailBox = ((flag & QR_MAILBOX) == QR_MAILBOX);
270   var hasNewMsgs = ((raflags & UA_HASNEWMSGS) == UA_HASNEWMSGS);
271   var roomLI = document.createElement("li");
272   var roomA = document.createElement("a");
273   roomA.setAttribute("href","dotgoto?room="+encodeURIComponent(roomName));
274   roomA.appendChild(document.createTextNode(roomName));
275   roomLI.appendChild(roomA);
276   floorUL.appendChild(roomLI);
277   var className = "room ";
278   if (view == VIEW_MAILBOX) {
279     className += "room-private"
280   } else if (view == VIEW_ADDRESSBOOK) {
281     className += "room-addr";
282   } else if (view == VIEW_CALENDAR || view == VIEW_CALBRIEF) {
283     className += "room-cal";
284   } else if (view == VIEW_TASKS) {
285     className += "room-tasks";
286   } else if (view == VIEW_NOTES) {
287     className += "room-notes";
288   } else {
289     className += "room-chat";
290   }
291   if (hasNewMsgs) {
292     className += " room-newmsgs";
293   }
294   if (roomName == roomToEmphasize) {
295     className += " room-emphasized";
296   }
297   roomLI.setAttribute("class", className);
298   roomA.dropTarget = true;
299   roomA.dropHandler = roomListDropHandler;
300   return roomLI;
301 }
302
303 function roomListDropHandler(target, dropped) {
304   if (dropped.getAttribute("citadel:msgid")) {
305       var room = getTextContent(target);
306       var msgIds = "";
307       for(msgId in currentlyMarkedRows) { //defined in summaryview.js
308           msgIds += ","+msgId;
309           if (msgIds.length > 800) {
310               var mvCommand = encodeURI("g_cmd=MOVE " + msgIds + "|"+room+"|0");
311               new Ajax.Request("ajax_servcmd", {
312                   parameters: mvCommand,
313                   method: 'post',
314               });
315               msgIds = "";
316           }
317
318       }
319       var mvCommand = encodeURI("g_cmd=MOVE " + msgIds + "|"+room+"|0");
320       new Ajax.Request('ajax_servcmd', {
321           method: 'post',
322           parameters: mvCommand,
323           onComplete: deleteAllMarkedRows()});
324   }
325 }
326 function expandFloorEvent(event) {
327   expandFloor(event.target);
328 }
329 function expandFloor(target) {
330   if (target.nodeName.toLowerCase() != "span") {
331     return; // ignore clicks on child UL
332   }
333   ctdlLocalPrefs.setPref("rooms_expanded", target.firstChild.nodeValue);
334   var parentUL = target.parentNode;
335   if (currentlyExpandedFloor != null) {
336     currentlyExpandedFloor.className = currentlyExpandedFloor.className.replace("floor-expanded","");
337   }
338   parentUL.className = parentUL.className + " floor-expanded";
339   currentlyExpandedFloor = parentUL;
340 }
341
342 // These functions handle moving sticky notes around the screen by dragging them
343
344 var uid_of_note_being_dragged = 0;
345 var saved_cursor_style = 'default';
346 var note_was_dragged = 0;
347
348 function NotesDragMouseUp(evt) {
349         document.onmouseup = null;
350         document.onmousemove = null;
351         if (document.layers) {
352                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
353         }
354
355         d = $('note-' + uid_of_note_being_dragged);
356         d.style.cursor = saved_cursor_style;
357
358         // If any motion actually occurred, submit an ajax http call to record it to the server
359         if (note_was_dragged > 0) {
360                 p = 'note_uid=' + uid_of_note_being_dragged
361                         + '&left=' + d.style.left
362                         + '&top=' + d.style.top
363                         + '&r=' + CtdlRandomString();
364                 new Ajax.Request(
365                         'ajax_update_note',
366                         {
367                                 method: 'post',
368                                 parameters: p
369                         }
370                 );
371         }
372
373         uid_of_note_being_dragged = '';
374         return true;
375 }
376
377 function NotesDragMouseMove(evt) {
378         x = (ns6 ? evt.clientX : event.clientX);
379         x_increment = x - saved_x;
380         y = (ns6 ? evt.clientY : event.clientY);
381         y_increment = y - saved_y;
382
383         // Move the div
384         d = $('note-' + uid_of_note_being_dragged);
385
386         divTop = parseInt(d.style.top);
387         divLeft = parseInt(d.style.left);
388
389         d.style.top = (divTop + y_increment) + 'px';
390         d.style.left = (divLeft + x_increment) + 'px';
391
392         saved_x = x;
393         saved_y = y;
394         note_was_dragged = 1;
395         return true;
396 }
397
398
399 function NotesDragMouseDown(evt, uid) {
400         saved_x = (ns6 ? evt.clientX : event.clientX);
401         saved_y = (ns6 ? evt.clientY : event.clientY);
402         document.onmouseup = NotesDragMouseUp;
403         document.onmousemove = NotesDragMouseMove;
404         if (document.layers) {
405                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
406         }
407         uid_of_note_being_dragged = uid;
408         d = $('note-' + uid_of_note_being_dragged);
409         saved_cursor_style = d.style.cursor;
410         d.style.cursor = 'move';
411         return false;           // disable the default action
412 }
413
414
415 // Called when the user clicks on the palette icon of a sticky note to change its color.
416 // It toggles the color selector visible or invisible.
417
418 function NotesClickPalette(evt, uid) {
419         uid_of_note_being_colored = uid;
420         d = $('palette-' + uid_of_note_being_colored);
421
422         if (d.style.display) {
423                 if (d.style.display == 'none') {
424                         d.style.display = 'block';
425                 }
426                 else {
427                         d.style.display = 'none';
428                 }
429         }
430         else {
431                 d.style.display = 'block';
432         }
433
434         return true;
435 }
436
437
438 // Called when the user clicks on one of the colors in an open color selector.
439 // Sets the desired color and then closes the color selector.
440
441 function NotesClickColor(evt, uid, red, green, blue, notecolor, titlecolor) {
442         uid_of_note_being_colored = uid;
443         palette_button = $('palette-' + uid_of_note_being_colored);
444         note_div = $('note-' + uid_of_note_being_colored);
445         titlebar_div = $('titlebar-' + uid_of_note_being_colored);
446
447         // alert('FIXME red=' + red + ' green=' + green + ' blue=' + blue);
448
449         note_div.style.backgroundColor = notecolor;
450         titlebar_div.style.backgroundColor = titlecolor;
451         palette_button.style.display = 'none';
452
453         // submit an ajax http call to record it to the server
454         p = 'note_uid=' + uid_of_note_being_colored
455                 + '&red=' + red
456                 + '&green=' + green
457                 + '&blue=' + blue
458                 + '&r=' + CtdlRandomString();
459         new Ajax.Request(
460                 'ajax_update_note',
461                 {
462                         method: 'post',
463                         parameters: p
464                 }
465         );
466 }
467
468
469
470
471 // These functions handle resizing sticky notes by dragging the resize handle
472
473 var uid_of_note_being_resized = 0;
474 var saved_cursor_style = 'default';
475 var note_was_resized = 0;
476
477 function NotesResizeMouseUp(evt) {
478         document.onmouseup = null;
479         document.onmousemove = null;
480         if (document.layers) {
481                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
482         }
483
484         d = $('note-' + uid_of_note_being_resized);
485         d.style.cursor = saved_cursor_style;
486
487         // If any motion actually occurred, submit an ajax http call to record it to the server
488         if (note_was_resized > 0) {
489                 p = 'note_uid=' + uid_of_note_being_resized
490                         + '&width=' + d.style.width
491                         + '&height=' + d.style.height
492                         + '&r=' + CtdlRandomString();
493                 new Ajax.Request(
494                         'ajax_update_note',
495                         {
496                                 method: 'post',
497                                 parameters: p
498                         }
499                 );
500         }
501
502         uid_of_note_being_resized = '';
503         return false;           // disable the default action
504 }
505
506 function NotesResizeMouseMove(evt) {
507         x = (ns6 ? evt.clientX : event.clientX);
508         x_increment = x - saved_x;
509         y = (ns6 ? evt.clientY : event.clientY);
510         y_increment = y - saved_y;
511
512         // Move the div
513         d = $('note-' + uid_of_note_being_resized);
514
515         divTop = parseInt(d.style.height);
516         divLeft = parseInt(d.style.width);
517
518         newHeight = divTop + y_increment;
519         if (newHeight < 50) newHeight = 50;
520
521         newWidth = divLeft + x_increment;
522         if (newWidth < 50) newWidth = 50;
523
524         d.style.height = newHeight + 'px';
525         d.style.width = newWidth + 'px';
526
527         saved_x = x;
528         saved_y = y;
529         note_was_resized = 1;
530         return false;           // disable the default action
531 }
532
533
534 function NotesResizeMouseDown(evt, uid) {
535         saved_x = (ns6 ? evt.clientX : event.clientX);
536         saved_y = (ns6 ? evt.clientY : event.clientY);
537         document.onmouseup = NotesResizeMouseUp;
538         document.onmousemove = NotesResizeMouseMove;
539         if (document.layers) {
540                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
541         }
542         uid_of_note_being_resized = uid;
543         d = $('note-' + uid_of_note_being_resized);
544         saved_cursor_style = d.style.cursor;
545         d.style.cursor = 'move';
546         return false;           // disable the default action
547 }
548
549
550 function DeleteStickyNote(evt, uid, confirmation_prompt) {
551         uid_of_note_being_deleted = uid;
552         d = $('note-' + uid_of_note_being_deleted);
553
554         if (confirm(confirmation_prompt)) {
555                 new Effect.Puff(d);
556
557                 // submit an ajax http call to delete it on the server
558                 p = 'note_uid=' + uid_of_note_being_deleted
559                         + '&deletenote=yes'
560                         + '&r=' + CtdlRandomString();
561                 new Ajax.Request(
562                         'ajax_update_note',
563                         {
564                                 method: 'post',
565                                 parameters: p
566                         }
567                 );
568         }
569 }
570
571 function ctdl_ts_getInnerText(el) {
572         if (typeof el == "string") return el;
573         if (typeof el == "undefined") { return el };
574         if (el.innerText) return el.innerText;  //Not needed but it is faster
575         var str = "";
576         
577         var cs = el.childNodes;
578         var l = cs.length;
579         for (var i = 0; i < l; i++) {
580                 switch (cs[i].nodeType) {
581                         case 1: //ELEMENT_NODE
582                                 str += ts_getInnerText(cs[i]);
583                                 break;
584                         case 3: //TEXT_NODE
585                                 str += cs[i].nodeValue;
586                                 break;
587                 }
588         }
589         return str;
590 }
591
592
593 // Place a gradient loadscreen on an element, e.g to use before Ajax.updater
594 function CtdlLoadScreen(elementid) {
595 var elem = document.getElementById(elementid);
596 elem.innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\">&nbsp;&nbsp;Loading....</font></td></tr></table><br></div>";
597 }
598
599
600
601 // Pop open the address book (target_input is the INPUT field to populate)
602
603 function PopOpenAddressBook(target_input) {
604         $('address_book_popup').style.display = 'block';
605         p = 'target_input=' + target_input + '&r=' + CtdlRandomString();
606         new Ajax.Updater(
607                 'address_book_popup_middle_div',
608                 'display_address_book_middle_div',
609                 {
610                         method: 'get',
611                         parameters: p,
612                         evalScripts: true
613                 }
614         );
615 }
616
617 function PopulateAddressBookInnerDiv(which_addr_book, target_input) {
618         $('address_book_inner_div').innerHTML = "<div align=center><br><table border=0 cellpadding=10 bgcolor=\"#ffffff\"><tr><td><img src=\"static/throbber.gif\" /><font color=\"#AAAAAA\">&nbsp;&nbsp;Loading....</font></td></tr></table><br></div>";
619         p = 'which_addr_book=' + which_addr_book
620           + '&target_input=' + target_input
621           + '&r=' + CtdlRandomString();
622         new Ajax.Updater(
623                 'address_book_inner_div',
624                 'display_address_book_inner_div',
625                 {
626                         method: 'get',
627                         parameters: p
628                 }
629         );
630 }
631
632 // What happens when a contact is selected from the address book popup
633 // (populate the specified target)
634
635 function AddContactsToTarget(target, whichaddr) {
636         while (whichaddr.selectedIndex != -1) {
637                 if (target.value.length > 0) {
638                         target.value = target.value + ', ';
639                 }
640                 target.value = target.value + whichaddr.value;
641                 whichaddr.options[whichaddr.selectedIndex].selected = false;
642         }
643 }
644
645 // Respond to a meeting invitation
646 function RespondToInvitation(question_divname, title_divname, msgnum, cal_partnum, sc) {
647         p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
648         new Ajax.Updater(title_divname, 'respond_to_request', { method: 'post', parameters: p } );
649         Effect.Fade(question_divname, { duration: 0.5 });
650 }
651
652 // Handle a received RSVP
653 function HandleRSVP(question_divname, title_divname, msgnum, cal_partnum, sc) {
654         p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
655         new Ajax.Updater(title_divname, 'handle_rsvp', { method: 'post', parameters: p } );
656         Effect.Fade(question_divname, { duration: 0.5 });
657 }
658 /* var fakeMouse = document.createEvent("MouseEvents");
659  fakeMouse.initMouseEvent("click", true, true, window, 
660    0,0,0,0,0, false, false, false, false, 0, null); */
661 // TODO: Collapse into one function
662 function toggleTaskDtStart(event) {
663         var checkBox = $('nodtstart');
664         var checkBoxTime = $('dtstart_time_assoc');
665         var dtstart = document.getElementById("dtstart");
666         var dtstart_date = document.getElementById("dtstart_date");
667         var dtstart_time = document.getElementById("dtstart_time");
668         if (checkBox.checked) {
669                 dtstart_date.style.visibility = "hidden";
670                 dtstart_time.style.visibility = "hidden";
671         } else {
672                 if (checkBoxTime.checked) {
673                         dtstart_time.style.visibility = "visible";
674                 } else {
675                         dtstart_time.style.visibility = "hidden";
676                 }
677                 dtstart_date.style.visibility = "visible";
678                 if (dtstart.value.length == 0)
679                         dtstart.dpck._initCurrentDate();
680         }
681 }
682 function toggleTaskDue(event) {
683         var checkBox = $('nodue');
684         var checkBoxTime = $('due_time_assoc');
685         var due = document.getElementById("due");
686         var due_date = document.getElementById("due_date");
687         var due_time = document.getElementById("due_time");
688         if (checkBox.checked) {
689                 due_date.style.visibility = "hidden";
690                 due_time.style.visibility = "hidden";
691         } else {
692                 if (checkBoxTime.checked) {
693                         due_time.style.visibility = "visible";
694                 } else {
695                         due_time.style.visibility = "hidden";
696                 }
697                 due_date.style.visibility = "visible";
698                 if (due.value.length == 0)
699                         due.dpck._initCurrentDate();
700         }
701 }
702 function ToggleTaskDateOrNoDateActivate(event) {
703         var dtstart = document.getElementById("nodtstart");
704         if (dtstart != null) {
705                 toggleTaskDtStart(null);
706                 toggleTaskDue(null);
707                 $('nodtstart').observe('click', toggleTaskDtStart);
708                 $('dtstart_time_assoc').observe('click', toggleTaskDtStart);
709                 $('nodue').observe('click', toggleTaskDue);
710                 $('due_time_assoc').observe('click', toggleTaskDue);
711         } 
712 }
713 function TaskViewGatherCategoriesFromTable() {
714         var table = $('taskview');
715         
716 }
717 function attachDatePicker(relative) {
718         var dpck = new DatePicker({
719         relative: relative,
720               language: 'en', //wclang.substr(0,2),
721               disableFutureDate: false,
722               dateFormat: [ ["yyyy", "mm", "dd"], "-"],
723               showDuration: 0.2
724         });
725         document.getElementById(relative).dpck = dpck; // attach a ref to it
726 }
727 function eventEditAllDay() {
728         var allDayCheck = document.getElementById("alldayevent");
729         var dtend_time = document.getElementById("dtend_time");
730         var dtstart_time = document.getElementById("dtstart_time");
731         if(allDayCheck.checked) {
732                 dtstart_time.style.visibility = "hidden";
733                 dtend_time.style.visibility = "hidden";
734         } else {
735                 dtstart_time.style.visibility = "visible";
736                 dtend_time.style.visibility = "visible";
737         }
738 }
739
740 // Functions which handle show/hide of various elements in the recurrence editor
741
742 function RecurrenceShowHide() {
743
744         if ($('is_recur').checked) {
745                 $('rrule_div').style.display = 'block';
746         }
747         else {
748                 $('rrule_div').style.display = 'none';
749         }
750
751         if ($('freq_selector').selectedIndex == 4) {
752                 $('weekday_selector').style.display = 'block';
753         }
754         else {
755                 $('weekday_selector').style.display = 'none';
756         }
757
758         if ($('freq_selector').selectedIndex == 5) {
759                 $('monthday_selector').style.display = 'block';
760         }
761         else {
762                 $('monthday_selector').style.display = 'none';
763         }
764
765         if ($('rrend_count').checked) {
766                 $('rrcount').disabled = false;
767         }
768         else {
769                 $('rrcount').disabled = true;
770         }
771
772         if ($('rrend_until').checked) {
773                 $('rruntil').disabled = false;
774         }
775         else {
776                 $('rruntil').disabled = true;
777         }
778
779         if ($('rrmonthtype_mday').checked) {
780                 $('rrmday').disabled = false;
781         }
782         else {
783                 $('rrmday').disabled = true;
784         }
785
786         if ($('rrmonthtype_wday').checked) {
787                 $('rrmweek').disabled = false;
788                 $('rrmweekday').disabled = false;
789         }
790         else {
791                 $('rrmweek').disabled = true;
792                 $('rrmweekday').disabled = true;
793         }
794
795         if ($('freq_selector').selectedIndex == 6) {
796                 $('yearday_selector').style.display = 'block';
797         }
798         else {
799                 $('yearday_selector').style.display = 'none';
800         }
801
802         $('ymday').innerHTML = 'XXXX-' + $('dtstart').value.substr(5);
803         $('rrmday').innerHTML = $('dtstart').value.substr(8);
804
805         if ($('rryeartype_ywday').checked) {
806                 $('rrymweek').disabled = false;
807                 $('rrymweekday').disabled = false;
808                 $('rrymonth').disabled = false;
809         }
810         else {
811                 $('rrymweek').disabled = true;
812                 $('rrymweekday').disabled = true;
813                 $('rrymonth').disabled = true;
814         }
815
816 }
817
818
819 // Enable or disable the 'check attendee availability' button depending on whether
820 // the attendees list is empty
821 function EnableOrDisableCheckButton()
822 {
823         if ($('attendees_box').value.length == 0) {
824                 $('check_button').disabled = true;
825         }
826         else {
827                 $('check_button').disabled = false;
828         }
829 }
830
831
832
833
834 function launchChat(event) {
835 window.open('chat', 'ctdl_chat_window', 'toolbar=no,location=no,directories=no,copyhistory=no,status=no,scrollbars=yes,resizable=yes');
836 }
837 // logger
838 function WCLog(msg) {
839   if (!!window.console && !!console.log) {
840     console.log(msg);
841   } else if (!!window.opera && !!opera.postError) {
842     opera.postError(msg);
843   } else {
844     wc_log += msg + "\r\n";
845   }
846 }
847
848 function RefreshSMTPqueueDisplay() {
849         new Ajax.Updater('smtpqueue_inner_div',
850         'display_smtpqueue_inner_div', { method: 'get',
851                 parameters: Math.random() } );
852 }
853
854 function DeleteSMTPqueueMsg(msgnum1, msgnum2) {
855         var p = encodeURI('g_cmd=DELE ' + msgnum1 + ',' + msgnum2);
856         new Ajax.Request(
857                 'ajax_servcmd', {
858                         method: 'post',
859                         parameters: p,
860                         onComplete: RefreshSMTPqueueDisplay()
861                 }
862         );
863 }
864
865
866 function ConfirmLogoff() {
867         new Ajax.Updater(
868                 'md-content',
869                 'do_template?template=confirmlogoff',
870                 {
871                         method: 'get',
872                         evalScripts: true,
873                         onSuccess: function(cl_success) {
874                                 toggleModal(1);
875                         }
876                 }
877         );
878 }
879
880
881 function switch_to_lang(new_lang) {
882         p = 'push?url=' + encodeURI(window.location);
883         new Ajax.Request(p, { method: 'get' } );
884         window.location = 'switch_language?lang=' + new_lang ;
885 }
886
887
888 function toggle_roomlist() 
889 {
890         /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
891         if ( $('expand_roomlist').src.substring($('expand_roomlist').src.length - 12) == "collapse.gif" ) {
892                 $('roomlist').style.display = 'none';
893                 $('expand_roomlist').src = 'static/webcit_icons/expand.gif';
894                 wstate=0;
895         }
896
897         else {
898                 $('roomlist').style.display = 'block';
899                 $('expand_roomlist').src = 'static/webcit_icons/collapse.gif';
900                 $('roomlist').innerHTML = '';
901                 FillRooms(IconBarRoomList);
902                 wstate=1;
903         }
904
905         // tell the server what I did
906         p = 'toggle_roomlist_expanded_state?wstate=' + wstate + '?rand=' + Math.random() ;
907         new Ajax.Request(p, { method: 'get' } );
908
909         return false;   /* this prevents the click from registering as a roomlist button press */
910 }
911
912
913 function toggle_wholist() 
914 {
915         /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
916         if ( $('expand_wholist').src.substring($('expand_wholist').src.length - 12) == "collapse.gif" ) {
917                 $('online_users').style.display = 'none';
918                 $('expand_wholist').src = 'static/webcit_icons/expand.gif';
919                 wstate=0;
920         }
921
922         else {
923                 $('online_users').style.display = 'block';
924                 $('expand_wholist').src = 'static/webcit_icons/collapse.gif';
925                 activate_iconbar_wholist_populat0r();
926                 wstate=1;
927         }
928
929         // tell the server what I did
930         p = 'toggle_wholist_expanded_state?wstate=' + wstate + '?rand=' + Math.random() ;
931         new Ajax.Request(p, { method: 'get' } );
932
933         return false;   /* this prevents the click from registering as a wholist button press */
934 }
935
936