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