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