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