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