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