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