For sites in guest mode, redirect to the landing page after we're logged out.
[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="+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     }
310     var mvCommand = encodeURI("g_cmd=MOVE " + msgIds + "|"+room+"|0");
311     new Ajax.Request('ajax_servcmd', {
312       method: 'post',
313           parameters: mvCommand,
314           onComplete: deleteAllMarkedRows()});
315     } 
316 }
317 function expandFloorEvent(event) {
318   expandFloor(event.target);
319 }
320 function expandFloor(target) {
321   if (target.nodeName.toLowerCase() != "span") {
322     return; // ignore clicks on child UL
323   }
324   ctdlLocalPrefs.setPref("rooms_expanded", target.firstChild.nodeValue);
325   var parentUL = target.parentNode;
326   if (currentlyExpandedFloor != null) {
327     currentlyExpandedFloor.className = currentlyExpandedFloor.className.replace("floor-expanded","");
328   }
329   parentUL.className = parentUL.className + " floor-expanded";
330   currentlyExpandedFloor = parentUL;
331 }
332
333 // These functions handle moving sticky notes around the screen by dragging them
334
335 var uid_of_note_being_dragged = 0;
336 var saved_cursor_style = 'default';
337 var note_was_dragged = 0;
338
339 function NotesDragMouseUp(evt) {
340         document.onmouseup = null;
341         document.onmousemove = null;
342         if (document.layers) {
343                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
344         }
345
346         d = $('note-' + uid_of_note_being_dragged);
347         d.style.cursor = saved_cursor_style;
348
349         // If any motion actually occurred, submit an ajax http call to record it to the server
350         if (note_was_dragged > 0) {
351                 p = 'note_uid=' + uid_of_note_being_dragged
352                         + '&left=' + d.style.left
353                         + '&top=' + d.style.top
354                         + '&r=' + CtdlRandomString();
355                 new Ajax.Request(
356                         'ajax_update_note',
357                         {
358                                 method: 'post',
359                                 parameters: p
360                         }
361                 );
362         }
363
364         uid_of_note_being_dragged = '';
365         return true;
366 }
367
368 function NotesDragMouseMove(evt) {
369         x = (ns6 ? evt.clientX : event.clientX);
370         x_increment = x - saved_x;
371         y = (ns6 ? evt.clientY : event.clientY);
372         y_increment = y - saved_y;
373
374         // Move the div
375         d = $('note-' + uid_of_note_being_dragged);
376
377         divTop = parseInt(d.style.top);
378         divLeft = parseInt(d.style.left);
379
380         d.style.top = (divTop + y_increment) + 'px';
381         d.style.left = (divLeft + x_increment) + 'px';
382
383         saved_x = x;
384         saved_y = y;
385         note_was_dragged = 1;
386         return true;
387 }
388
389
390 function NotesDragMouseDown(evt, uid) {
391         saved_x = (ns6 ? evt.clientX : event.clientX);
392         saved_y = (ns6 ? evt.clientY : event.clientY);
393         document.onmouseup = NotesDragMouseUp;
394         document.onmousemove = NotesDragMouseMove;
395         if (document.layers) {
396                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
397         }
398         uid_of_note_being_dragged = uid;
399         d = $('note-' + uid_of_note_being_dragged);
400         saved_cursor_style = d.style.cursor;
401         d.style.cursor = 'move';
402         return false;           // disable the default action
403 }
404
405
406 // Called when the user clicks on the palette icon of a sticky note to change its color.
407 // It toggles the color selector visible or invisible.
408
409 function NotesClickPalette(evt, uid) {
410         uid_of_note_being_colored = uid;
411         d = $('palette-' + uid_of_note_being_colored);
412
413         if (d.style.display) {
414                 if (d.style.display == 'none') {
415                         d.style.display = 'block';
416                 }
417                 else {
418                         d.style.display = 'none';
419                 }
420         }
421         else {
422                 d.style.display = 'block';
423         }
424
425         return true;
426 }
427
428
429 // Called when the user clicks on one of the colors in an open color selector.
430 // Sets the desired color and then closes the color selector.
431
432 function NotesClickColor(evt, uid, red, green, blue, notecolor, titlecolor) {
433         uid_of_note_being_colored = uid;
434         palette_button = $('palette-' + uid_of_note_being_colored);
435         note_div = $('note-' + uid_of_note_being_colored);
436         titlebar_div = $('titlebar-' + uid_of_note_being_colored);
437
438         // alert('FIXME red=' + red + ' green=' + green + ' blue=' + blue);
439
440         note_div.style.backgroundColor = notecolor;
441         titlebar_div.style.backgroundColor = titlecolor;
442         palette_button.style.display = 'none';
443
444         // submit an ajax http call to record it to the server
445         p = 'note_uid=' + uid_of_note_being_colored
446                 + '&red=' + red
447                 + '&green=' + green
448                 + '&blue=' + blue
449                 + '&r=' + CtdlRandomString();
450         new Ajax.Request(
451                 'ajax_update_note',
452                 {
453                         method: 'post',
454                         parameters: p
455                 }
456         );
457 }
458
459
460
461
462 // These functions handle resizing sticky notes by dragging the resize handle
463
464 var uid_of_note_being_resized = 0;
465 var saved_cursor_style = 'default';
466 var note_was_resized = 0;
467
468 function NotesResizeMouseUp(evt) {
469         document.onmouseup = null;
470         document.onmousemove = null;
471         if (document.layers) {
472                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
473         }
474
475         d = $('note-' + uid_of_note_being_resized);
476         d.style.cursor = saved_cursor_style;
477
478         // If any motion actually occurred, submit an ajax http call to record it to the server
479         if (note_was_resized > 0) {
480                 p = 'note_uid=' + uid_of_note_being_resized
481                         + '&width=' + d.style.width
482                         + '&height=' + d.style.height
483                         + '&r=' + CtdlRandomString();
484                 new Ajax.Request(
485                         'ajax_update_note',
486                         {
487                                 method: 'post',
488                                 parameters: p
489                         }
490                 );
491         }
492
493         uid_of_note_being_resized = '';
494         return false;           // disable the default action
495 }
496
497 function NotesResizeMouseMove(evt) {
498         x = (ns6 ? evt.clientX : event.clientX);
499         x_increment = x - saved_x;
500         y = (ns6 ? evt.clientY : event.clientY);
501         y_increment = y - saved_y;
502
503         // Move the div
504         d = $('note-' + uid_of_note_being_resized);
505
506         divTop = parseInt(d.style.height);
507         divLeft = parseInt(d.style.width);
508
509         newHeight = divTop + y_increment;
510         if (newHeight < 50) newHeight = 50;
511
512         newWidth = divLeft + x_increment;
513         if (newWidth < 50) newWidth = 50;
514
515         d.style.height = newHeight + 'px';
516         d.style.width = newWidth + 'px';
517
518         saved_x = x;
519         saved_y = y;
520         note_was_resized = 1;
521         return false;           // disable the default action
522 }
523
524
525 function NotesResizeMouseDown(evt, uid) {
526         saved_x = (ns6 ? evt.clientX : event.clientX);
527         saved_y = (ns6 ? evt.clientY : event.clientY);
528         document.onmouseup = NotesResizeMouseUp;
529         document.onmousemove = NotesResizeMouseMove;
530         if (document.layers) {
531                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
532         }
533         uid_of_note_being_resized = uid;
534         d = $('note-' + uid_of_note_being_resized);
535         saved_cursor_style = d.style.cursor;
536         d.style.cursor = 'move';
537         return false;           // disable the default action
538 }
539
540
541 function DeleteStickyNote(evt, uid, confirmation_prompt) {
542         uid_of_note_being_deleted = uid;
543         d = $('note-' + uid_of_note_being_deleted);
544
545         if (confirm(confirmation_prompt)) {
546                 new Effect.Puff(d);
547
548                 // submit an ajax http call to delete it on the server
549                 p = 'note_uid=' + uid_of_note_being_deleted
550                         + '&deletenote=yes'
551                         + '&r=' + CtdlRandomString();
552                 new Ajax.Request(
553                         'ajax_update_note',
554                         {
555                                 method: 'post',
556                                 parameters: p
557                         }
558                 );
559         }
560 }
561
562 function ctdl_ts_getInnerText(el) {
563         if (typeof el == "string") return el;
564         if (typeof el == "undefined") { return el };
565         if (el.innerText) return el.innerText;  //Not needed but it is faster
566         var str = "";
567         
568         var cs = el.childNodes;
569         var l = cs.length;
570         for (var i = 0; i < l; i++) {
571                 switch (cs[i].nodeType) {
572                         case 1: //ELEMENT_NODE
573                                 str += ts_getInnerText(cs[i]);
574                                 break;
575                         case 3: //TEXT_NODE
576                                 str += cs[i].nodeValue;
577                                 break;
578                 }
579         }
580         return str;
581 }
582
583
584 // Place a gradient loadscreen on an element, e.g to use before Ajax.updater
585 function CtdlLoadScreen(elementid) {
586 var elem = document.getElementById(elementid);
587 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>";
588 }
589
590
591
592 // Pop open the address book (target_input is the INPUT field to populate)
593
594 function PopOpenAddressBook(target_input) {
595         $('address_book_popup').style.display = 'block';
596         p = 'target_input=' + target_input + '&r=' + CtdlRandomString();
597         new Ajax.Updater(
598                 'address_book_popup_middle_div',
599                 'display_address_book_middle_div',
600                 {
601                         method: 'get',
602                         parameters: p,
603                         evalScripts: true
604                 }
605         );
606 }
607
608 function PopulateAddressBookInnerDiv(which_addr_book, target_input) {
609         $('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>";
610         p = 'which_addr_book=' + which_addr_book
611           + '&target_input=' + target_input
612           + '&r=' + CtdlRandomString();
613         new Ajax.Updater(
614                 'address_book_inner_div',
615                 'display_address_book_inner_div',
616                 {
617                         method: 'get',
618                         parameters: p
619                 }
620         );
621 }
622
623 // What happens when a contact is selected from the address book popup
624 // (populate the specified target)
625
626 function AddContactsToTarget(target, whichaddr) {
627         while (whichaddr.selectedIndex != -1) {
628                 if (target.value.length > 0) {
629                         target.value = target.value + ', ';
630                 }
631                 target.value = target.value + whichaddr.value;
632                 whichaddr.options[whichaddr.selectedIndex].selected = false;
633         }
634 }
635
636 // Respond to a meeting invitation
637 function RespondToInvitation(question_divname, title_divname, msgnum, cal_partnum, sc) {
638         p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
639         new Ajax.Updater(title_divname, 'respond_to_request', { method: 'post', parameters: p } );
640         Effect.Fade(question_divname, { duration: 0.5 });
641 }
642
643 // Handle a received RSVP
644 function HandleRSVP(question_divname, title_divname, msgnum, cal_partnum, sc) {
645         p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
646         new Ajax.Updater(title_divname, 'handle_rsvp', { method: 'post', parameters: p } );
647         Effect.Fade(question_divname, { duration: 0.5 });
648 }
649 /* var fakeMouse = document.createEvent("MouseEvents");
650  fakeMouse.initMouseEvent("click", true, true, window, 
651    0,0,0,0,0, false, false, false, false, 0, null); */
652 // TODO: Collapse into one function
653 function toggleTaskDtStart(event) {
654         var checkBox = $('nodtstart');
655         var checkBoxTime = $('dtstart_time_assoc');
656         var dtstart = document.getElementById("dtstart");
657         var dtstart_date = document.getElementById("dtstart_date");
658         var dtstart_time = document.getElementById("dtstart_time");
659         if (checkBox.checked) {
660                 dtstart_date.style.visibility = "hidden";
661                 dtstart_time.style.visibility = "hidden";
662         } else {
663                 if (checkBoxTime.checked) {
664                         dtstart_time.style.visibility = "visible";
665                 } else {
666                         dtstart_time.style.visibility = "hidden";
667                 }
668                 dtstart_date.style.visibility = "visible";
669                 if (dtstart.value.length == 0)
670                         dtstart.dpck._initCurrentDate();
671         }
672 }
673 function toggleTaskDue(event) {
674         var checkBox = $('nodue');
675         var checkBoxTime = $('due_time_assoc');
676         var due = document.getElementById("due");
677         var due_date = document.getElementById("due_date");
678         var due_time = document.getElementById("due_time");
679         if (checkBox.checked) {
680                 due_date.style.visibility = "hidden";
681                 due_time.style.visibility = "hidden";
682         } else {
683                 if (checkBoxTime.checked) {
684                         due_time.style.visibility = "visible";
685                 } else {
686                         due_time.style.visibility = "hidden";
687                 }
688                 due_date.style.visibility = "visible";
689                 if (due.value.length == 0)
690                         due.dpck._initCurrentDate();
691         }
692 }
693 function ToggleTaskDateOrNoDateActivate(event) {
694         var dtstart = document.getElementById("nodtstart");
695         if (dtstart != null) {
696                 toggleTaskDtStart(null);
697                 toggleTaskDue(null);
698                 $('nodtstart').observe('click', toggleTaskDtStart);
699                 $('dtstart_time_assoc').observe('click', toggleTaskDtStart);
700                 $('nodue').observe('click', toggleTaskDue);
701                 $('due_time_assoc').observe('click', toggleTaskDue);
702         } 
703 }
704 function TaskViewGatherCategoriesFromTable() {
705         var table = $('taskview');
706         
707 }
708 function attachDatePicker(relative) {
709         var dpck = new DatePicker({
710         relative: relative,
711               language: 'en', //wclang.substr(0,2),
712               disableFutureDate: false,
713               dateFormat: [ ["yyyy", "mm", "dd"], "-"],
714               showDuration: 0.2
715         });
716         document.getElementById(relative).dpck = dpck; // attach a ref to it
717 }
718 function eventEditAllDay() {
719         var allDayCheck = document.getElementById("alldayevent");
720         var dtend_time = document.getElementById("dtend_time");
721         var dtstart_time = document.getElementById("dtstart_time");
722         if(allDayCheck.checked) {
723                 dtstart_time.style.visibility = "hidden";
724                 dtend_time.style.visibility = "hidden";
725         } else {
726                 dtstart_time.style.visibility = "visible";
727                 dtend_time.style.visibility = "visible";
728         }
729 }
730
731 // Functions which handle show/hide of various elements in the recurrence editor
732
733 function RecurrenceShowHide() {
734
735         if ($('is_recur').checked) {
736                 $('rrule_div').style.display = 'block';
737         }
738         else {
739                 $('rrule_div').style.display = 'none';
740         }
741
742         if ($('freq_selector').selectedIndex == 4) {
743                 $('weekday_selector').style.display = 'block';
744         }
745         else {
746                 $('weekday_selector').style.display = 'none';
747         }
748
749         if ($('freq_selector').selectedIndex == 5) {
750                 $('monthday_selector').style.display = 'block';
751         }
752         else {
753                 $('monthday_selector').style.display = 'none';
754         }
755
756         if ($('rrend_count').checked) {
757                 $('rrcount').disabled = false;
758         }
759         else {
760                 $('rrcount').disabled = true;
761         }
762
763         if ($('rrend_until').checked) {
764                 $('rruntil').disabled = false;
765         }
766         else {
767                 $('rruntil').disabled = true;
768         }
769
770         if ($('rrmonthtype_mday').checked) {
771                 $('rrmday').disabled = false;
772         }
773         else {
774                 $('rrmday').disabled = true;
775         }
776
777         if ($('rrmonthtype_wday').checked) {
778                 $('rrmweek').disabled = false;
779                 $('rrmweekday').disabled = false;
780         }
781         else {
782                 $('rrmweek').disabled = true;
783                 $('rrmweekday').disabled = true;
784         }
785
786         if ($('freq_selector').selectedIndex == 6) {
787                 $('yearday_selector').style.display = 'block';
788         }
789         else {
790                 $('yearday_selector').style.display = 'none';
791         }
792
793         $('ymday').innerHTML = 'XXXX-' + $('dtstart').value.substr(5);
794         $('rrmday').innerHTML = $('dtstart').value.substr(8);
795
796         if ($('rryeartype_ywday').checked) {
797                 $('rrymweek').disabled = false;
798                 $('rrymweekday').disabled = false;
799                 $('rrymonth').disabled = false;
800         }
801         else {
802                 $('rrymweek').disabled = true;
803                 $('rrymweekday').disabled = true;
804                 $('rrymonth').disabled = true;
805         }
806
807 }
808
809
810 // Enable or disable the 'check attendee availability' button depending on whether
811 // the attendees list is empty
812 function EnableOrDisableCheckButton()
813 {
814         if ($('attendees_box').value.length == 0) {
815                 $('check_button').disabled = true;
816         }
817         else {
818                 $('check_button').disabled = false;
819         }
820 }
821
822
823
824
825 function launchChat(event) {
826 window.open('chat', 'ctdl_chat_window', 'toolbar=no,location=no,directories=no,copyhistory=no,status=no,scrollbars=yes,resizable=yes');
827 }
828 // logger
829 function WCLog(msg) {
830   if (!!window.console && !!console.log) {
831     console.log(msg);
832   } else if (!!window.opera && !!opera.postError) {
833     opera.postError(msg);
834   } else {
835     wc_log += msg + "\r\n";
836   }
837 }
838
839 function RefreshSMTPqueueDisplay() {
840         new Ajax.Updater('smtpqueue_inner_div',
841         'display_smtpqueue_inner_div', { method: 'get',
842                 parameters: Math.random() } );
843 }
844
845 function DeleteSMTPqueueMsg(msgnum1, msgnum2) {
846         var p = encodeURI('g_cmd=DELE ' + msgnum1 + ',' + msgnum2);
847         new Ajax.Request(
848                 'ajax_servcmd', {
849                         method: 'post',
850                         parameters: p,
851                         onComplete: RefreshSMTPqueueDisplay()
852                 }
853         );
854 }
855
856
857 function ConfirmLogoff() {
858         new Ajax.Updater(
859                 'md-content',
860                 'do_template?template=confirmlogoff',
861                 {
862                         method: 'get',
863                         evalScripts: true,
864                         onSuccess: function(cl_success) {
865                                 toggleModal(1);
866                         }
867                 }
868         );
869 }
870
871
872 function switch_to_lang(new_lang) {
873         p = 'push?url=' + encodeURI(window.location);
874         new Ajax.Request(p, { method: 'get' } );
875         window.location = 'switch_language?lang=' + new_lang ;
876 }
877
878
879 function toggle_roomlist() 
880 {
881         /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
882         if ( $('expand_roomlist').src.substring($('expand_roomlist').src.length - 12) == "collapse.gif" ) {
883                 $('roomlist').style.display = 'none';
884                 $('expand_roomlist').src = 'static/expand.gif';
885                 wstate=0;
886         }
887
888         else {
889                 $('roomlist').style.display = 'block';
890                 $('expand_roomlist').src = 'static/collapse.gif';
891                 $('roomlist').innerHTML = '';
892                 FillRooms(IconBarRoomList);
893                 wstate=1;
894         }
895
896         // tell the server what I did
897         p = 'toggle_roomlist_expanded_state?wstate=' + wstate + '?rand=' + Math.random() ;
898         new Ajax.Request(p, { method: 'get' } );
899
900         return false;   /* this prevents the click from registering as a roomlist button press */
901 }
902
903
904 function toggle_wholist() 
905 {
906         /* WARNING: VILE, SLEAZY HACK.  We determine the state of the box based on the image loaded. */
907         if ( $('expand_wholist').src.substring($('expand_wholist').src.length - 12) == "collapse.gif" ) {
908                 $('online_users').style.display = 'none';
909                 $('expand_wholist').src = 'static/expand.gif';
910                 wstate=0;
911         }
912
913         else {
914                 $('online_users').style.display = 'block';
915                 $('expand_wholist').src = 'static/collapse.gif';
916                 activate_iconbar_wholist_populat0r();
917                 wstate=1;
918         }
919
920         // tell the server what I did
921         p = 'toggle_wholist_expanded_state?wstate=' + wstate + '?rand=' + Math.random() ;
922         new Ajax.Request(p, { method: 'get' } );
923
924         return false;   /* this prevents the click from registering as a wholist button press */
925 }
926
927