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