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