aaaaaaaaaaaaaaaaaaaaaagggggggh internet explorer sucks
[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 if (document.layers) {browserType = "nn4"}
13 if (document.all) {browserType = "ie"}
14 if (window.navigator.userAgent.toLowerCase().match("gecko")) {
15         browserType= "gecko"
16 }
17
18 var ns6=document.getElementById&&!document.all;
19 Event.observe(window, 'load', ToggleTaskDateOrNoDateActivate);
20 Event.observe(window, 'load', taskViewActivate);
21 function CtdlRandomString()  {
22         return((Math.random()+'').substr(3));
23 }
24
25
26
27 // We love string tokenizers.
28 function extract_token(source_string, token_num, delimiter) {
29         var i = 0;
30         var extracted_string = source_string;
31
32         if (token_num > 0) {
33                 for (i=0; i<token_num; ++i) {
34                         var j = extracted_string.indexOf(delimiter);
35                         if (j >= 0) {
36                                 extracted_string = extracted_string.substr(j+1);
37                         }
38                 }
39         }
40
41         j = extracted_string.indexOf(delimiter);
42         if (j >= 0) {
43                 extracted_string = extracted_string.substr(0, j);
44         }
45
46         return extracted_string;
47 }
48
49
50
51 // This code handles the popups for important-messages.
52 function hide_imsg_popup() {
53         if (browserType == "gecko" )
54                 document.poppedLayer = eval('document.getElementById(\'important_message\')');
55         else if (browserType == "ie")
56                 document.poppedLayer = eval('document.all[\'important_message\']');
57         else
58                 document.poppedLayer = eval('document.layers[\'`important_message\']');
59
60         document.poppedLayer.style.visibility = "hidden";
61 }
62
63
64 // This function activates the ajax-powered recipient autocompleters on the message entry screen.
65 function activate_entmsg_autocompleters() {
66         new Ajax.Autocompleter('cc_id', 'cc_name_choices', 'cc_autocomplete', {} );
67         new Ajax.Autocompleter('bcc_id', 'bcc_name_choices', 'bcc_autocomplete', {} );
68         new Ajax.Autocompleter('recp_id', 'recp_name_choices', 'recp_autocomplete', {} );
69 }
70
71
72
73 // Toggle the icon bar between menu/roomlist...
74 var which_div_expanded = null;
75 var num_drop_targets = 0;
76 var drop_targets_elements = new Array();
77 var drop_targets_roomnames = new Array();
78
79 function switch_to_room_list() {
80         $('iconbar').innerHTML = $('iconbar').innerHTML.substr(0, $('iconbar').innerHTML.indexOf('switch'));
81         CtdlLoadScreen('iconbar');
82         new Ajax.Updater('iconbar', 'iconbar_ajax_rooms', { method: 'get' } );
83 }
84
85 function expand_floor(floor_div) {
86         if (which_div_expanded != null) {
87                 if ($(which_div_expanded) != null) {
88                         $(which_div_expanded).style.display = 'none' ;
89                 }
90         }
91
92         // clicking on the already-expanded floor causes the whole list to collapse
93         if (which_div_expanded == floor_div) {
94                 which_div_expanded = null;
95
96                 // notify the server that no floors are expanded
97                 new Ajax.Request(
98                         'set_floordiv_expanded/-1', {
99                                 method: 'post'
100                         }
101                 );
102                 return true;
103         }
104
105         // expand the requested floor
106         $(floor_div).style.display = 'block';
107         which_div_expanded = floor_div;
108
109         // notify the server of which floor is expanded
110         new Ajax.Request(
111                 'set_floordiv_expanded/'+floor_div, {
112                         method: 'post'
113                 }
114         );
115 }
116
117 function switch_to_menu_buttons() {
118         which_div_expanded = null;
119         num_drop_targets = 0;
120         CtdlLoadScreen('iconbar');
121         new Ajax.Updater('iconbar', 'iconbar_ajax_menu', { method: 'get' } );
122 }
123
124
125 // Static variables for mailbox view...
126 //
127 var CtdlNumMsgsSelected = 0;
128 var CtdlMsgsSelected = new Array();
129 var CtdlLastMsgnumSelected = 0;
130
131 // This gets called when you single click on a message in the mailbox view.
132 // We know that the element id of the table row will be the letter 'm' plus the message number.
133 //
134 function CtdlSingleClickMsg(evt, msgnum) {
135
136         // Clear the preview pane until we load the new message
137         $('preview_pane').innerHTML = '';
138
139         // De-select any messages that were already selected, *unless* the Ctrl or
140         // Shift key is being pressed, in which case the user wants multi select
141         // or group select.
142         if ( (!evt.ctrlKey) && (!evt.shiftKey) ) {
143                 if (CtdlNumMsgsSelected > 0) {
144                         for (i=0; i<CtdlNumMsgsSelected; ++i) {
145                                 $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
146                                 $('m'+CtdlMsgsSelected[i]).style.color = '#000';
147                         }
148                         CtdlNumMsgsSelected = 0;
149                 }
150         }
151
152         // For multi select ... is the message being clicked already selected?
153         already_selected = 0;
154         if ( (evt.ctrlKey) && (CtdlNumMsgsSelected > 0) ) {
155                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
156                         if (CtdlMsgsSelected[i] == msgnum) {
157                                 already_selected = 1;
158                                 already_selected_pos = i;
159                         }
160                 }
161         }
162
163         // Now select (or de-select) the message
164         if ( (evt.ctrlKey) && (already_selected == 1) ) {
165
166                 // Deselect: first un-highlight it...
167                 $('m'+msgnum).style.backgroundColor = '#fff';
168                 $('m'+msgnum).style.color = '#000';
169
170                 // Then remove it from the selected messages list.
171                 for (i=already_selected_pos; i<(CtdlNumMsgsSelected-1); ++i) {
172                         CtdlMsgsSelected[i] = CtdlMsgsSelected[i+1];
173                 }
174                 CtdlNumMsgsSelected = CtdlNumMsgsSelected - 1;
175                 
176         }
177
178         else if (evt.shiftKey) {
179                 
180                 // Group select: first clear everything out...
181                 if (CtdlNumMsgsSelected > 0) {
182                         for (i=0; i<CtdlNumMsgsSelected; ++i) {
183                                 $('m'+CtdlMsgsSelected[i]).style.backgroundColor = '#fff';
184                                 $('m'+CtdlMsgsSelected[i]).style.color = '#000';
185                         }
186                 }
187                 CtdlNumMsgsSelected = 0;
188
189                 // Then highlight and select the group.
190                 // Traverse the table looking for a row whose ID contains the desired msgnum
191
192                 var in_the_group = 0;
193                 var is_edge = 0;
194                 var table = $('summary_headers');
195                 if (table) {
196                         for (var r = 0; r < table.rows.length; r++) {
197                                 var thename = table.rows[r].id;
198                                 if ( (thename.substr(1) == msgnum) || (thename.substr(1) == CtdlLastMsgnumSelected) ) {
199                                         in_the_group = 1 - in_the_group;
200                                         is_edge = 1;
201                                 }
202                                 else {
203                                         is_edge = 0;
204                                 }
205                                 if ( (in_the_group == 1) || (is_edge == 1) ) {
206                                         // Highlight it...
207                                         table.rows[r].style.backgroundColor='#69aaff';
208                                         table.rows[r].style.color='#fff';
209
210                                         // And add it to the selected messages list.
211                                         CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
212                                         CtdlMsgsSelected[CtdlNumMsgsSelected-1] = thename.substr(1);
213                                 }
214                         }
215                 }
216         }
217
218         else {
219                 // Select: first highlight it...
220                 $('m'+msgnum).style.backgroundColor='#69aaff';
221                 $('m'+msgnum).style.color='#fff';
222
223                 // Then add it to the selected messages list.
224                 CtdlNumMsgsSelected = CtdlNumMsgsSelected + 1;
225                 CtdlMsgsSelected[CtdlNumMsgsSelected-1] = msgnum;
226
227                 // Gradient
228                 CtdlLoadScreen('preview_pane');
229                 // Update the preview pane
230                 new Ajax.Updater('preview_pane', 'msg/'+msgnum, { method: 'get' } );
231         
232                 // Mark the message as read
233                 new Ajax.Request(
234                         'ajax_servcmd', {
235                                 method: 'post',
236                                 parameters: 'g_cmd=SEEN '+msgnum+'|1',
237                                 onComplete: CtdlRemoveTheUnseenBold(msgnum)
238                         }
239                 );
240         }
241         
242         // Save the selected position in case the user does a group select next time.
243         CtdlLastMsgnumSelected = msgnum;
244
245         return false;           // try to defeat the default click behavior
246 }
247
248 // Delete selected messages.
249 function CtdlDeleteSelectedMessages(evt) {
250         
251         if (CtdlNumMsgsSelected < 1) {
252                 // Nothing to delete, so exit silently.
253                 return false;
254         }
255         for (i=0; i<CtdlNumMsgsSelected; ++i) {
256                 if (parseInt(room_is_trash) > 0) {
257                         new Ajax.Request(
258                                 'ajax_servcmd', {
259                                         method: 'post',
260                                         parameters: 'g_cmd=DELE ' + CtdlMsgsSelected[i],
261                                         onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
262                                 }
263                         );
264                 }
265                 else {
266                         new Ajax.Request(
267                                 'ajax_servcmd', {
268                                         method: 'post',
269                                         parameters: 'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|_TRASH_|0',
270                                         onComplete: CtdlClearDeletedMsg(CtdlMsgsSelected[i])
271                                 }
272                         );
273                 }
274         }
275         CtdlNumMsgsSelected = 0;
276
277         // Clear the preview pane too.
278         $('preview_pane').innerHTML = '';
279 }
280
281
282 // Move selected messages.
283 function CtdlMoveSelectedMessages(evt, target_roomname) {
284         
285         if (CtdlNumMsgsSelected < 1) {
286                 // Nothing to delete, so exit silently.
287                 return false;
288         }
289         for (i=0; i<CtdlNumMsgsSelected; ++i) {
290                 new Ajax.Request(
291                         'ajax_servcmd', {
292                                 method:'post',
293                                 parameters:'g_cmd=MOVE ' + CtdlMsgsSelected[i] + '|' + target_roomname + '|0',
294                                 onComplete:CtdlClearDeletedMsg(CtdlMsgsSelected[i])
295                         }
296                 );
297         }
298         CtdlNumMsgsSelected = 0;
299
300         // Clear the preview pane too.
301         $('preview_pane').innerHTML = '';
302 }
303
304
305
306 // This gets called when the user touches the keyboard after selecting messages...
307 function CtdlMsgListKeyPress(evt) {
308         if(document.all) {                              // aIEeee
309                 var whichKey = window.event.keyCode;
310         }
311         else {                                          // non-sux0r browsers
312                 var whichKey = evt.which;
313         }
314         if (whichKey == 46) {                           // DELETE key
315                 CtdlDeleteSelectedMessages(evt);
316         }
317         return true;
318 }
319
320 // Take the boldface away from a message to indicate that it has been seen.
321 function CtdlRemoveTheUnseenBold(msgnum) {
322         $('m'+msgnum).style.fontWeight='normal';
323 }
324
325 // A message has been deleted, so yank it from the list.
326 function CtdlClearDeletedMsg(msgnum) {
327
328
329         // Traverse the table looking for a row whose ID contains the desired msgnum
330         var table = $('summary_headers');
331         if (table) {
332                 for (var r = 0; r < table.rows.length; r++) {
333                         var thename = table.rows[r].id;
334                         if (thename.substr(1) == msgnum) {
335                                 try {
336                                         table.deleteRow(r);
337                                 }
338                                 catch(e) {
339                                         alert('error: browser failed to clear row ' + r);
340                                 }
341                         }
342                 }
343         }
344         else {                                          // if we can't delete it,
345                 new Effect.Squish('m'+msgnum);          // just hide it.
346         }
347
348
349 }
350
351 // These functions called when the user down-clicks on the message list resizer bar
352
353 var saved_x = 0;
354 var saved_y = 0;
355
356 function CtdlResizeMsgListMouseUp(evt) {
357         document.onmouseup = null;
358         document.onmousemove = null;
359         if (document.layers) {
360                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
361         }
362         return true;
363 }
364
365 function CtdlResizeMsgListMouseMove(evt) {
366         y = (ns6 ? evt.clientY : event.clientY);
367         increment = y - saved_y;
368
369         // First move the bottom of the message list...
370         d = $('message_list');
371         if (d.offsetHeight){
372                 divHeight = d.offsetHeight;
373         }
374         else if (d.style.pixelHeight) {
375                 divHeight = d.style.pixelHeight;
376         }
377         d.style.height = (divHeight + increment) + 'px';
378
379         // Then move the top of the preview pane...
380         d = $('preview_pane');
381         if (d.offsetTop){
382                 divTop = d.offsetTop;
383         }
384         else if (d.style.pixelTop) {
385                 divTop = d.style.pixelTop;
386         }
387         d.style.top = (divTop + increment) + 'px';
388
389         // Resize the bottom of the preview pane...
390         d = $('preview_pane');
391         if (d.offsetHeight){
392                 divHeight = d.offsetHeight;
393         }
394         else if (d.style.pixelHeight) {
395                 divHeight = d.style.pixelHeight;
396         }
397         d.style.height = (divHeight - increment) + 'px';
398
399         // Then move the top of the slider bar.
400         d = $('resize_msglist');
401         if (d.offsetTop){
402                 divTop = d.offsetTop;
403         }
404         else if (d.style.pixelTop) {
405                 divTop = d.style.pixelTop;
406         }
407         d.style.top = (divTop + increment) + 'px';
408
409         saved_y = y;
410         return true;
411 }
412
413 function CtdlResizeMsgListMouseDown(evt) {
414         saved_y = (ns6 ? evt.clientY : event.clientY);
415         document.onmouseup = CtdlResizeMsgListMouseUp;
416         document.onmousemove = CtdlResizeMsgListMouseMove;
417         if (document.layers) {
418                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
419         }
420         return false;           // disable the default action
421 }
422
423
424
425
426
427 // These functions handle moving sticky notes around the screen by dragging them
428
429 var uid_of_note_being_dragged = 0;
430 var saved_cursor_style = 'default';
431 var note_was_dragged = 0;
432
433 function NotesDragMouseUp(evt) {
434         document.onmouseup = null;
435         document.onmousemove = null;
436         if (document.layers) {
437                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
438         }
439
440         d = $('note-' + uid_of_note_being_dragged);
441         d.style.cursor = saved_cursor_style;
442
443         // If any motion actually occurred, submit an ajax http call to record it to the server
444         if (note_was_dragged > 0) {
445                 p = 'note_uid=' + uid_of_note_being_dragged
446                         + '&left=' + d.style.left
447                         + '&top=' + d.style.top
448                         + '&r=' + CtdlRandomString();
449                 new Ajax.Request(
450                         'ajax_update_note',
451                         {
452                                 method: 'post',
453                                 parameters: p
454                         }
455                 );
456         }
457
458         uid_of_note_being_dragged = '';
459         return true;
460 }
461
462 function NotesDragMouseMove(evt) {
463         x = (ns6 ? evt.clientX : event.clientX);
464         x_increment = x - saved_x;
465         y = (ns6 ? evt.clientY : event.clientY);
466         y_increment = y - saved_y;
467
468         // Move the div
469         d = $('note-' + uid_of_note_being_dragged);
470
471         divTop = parseInt(d.style.top);
472         divLeft = parseInt(d.style.left);
473
474         d.style.top = (divTop + y_increment) + 'px';
475         d.style.left = (divLeft + x_increment) + 'px';
476
477         saved_x = x;
478         saved_y = y;
479         note_was_dragged = 1;
480         return true;
481 }
482
483
484 function NotesDragMouseDown(evt, uid) {
485         saved_x = (ns6 ? evt.clientX : event.clientX);
486         saved_y = (ns6 ? evt.clientY : event.clientY);
487         document.onmouseup = NotesDragMouseUp;
488         document.onmousemove = NotesDragMouseMove;
489         if (document.layers) {
490                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
491         }
492         uid_of_note_being_dragged = uid;
493         d = $('note-' + uid_of_note_being_dragged);
494         saved_cursor_style = d.style.cursor;
495         d.style.cursor = 'move';
496         return false;           // disable the default action
497 }
498
499
500 // Called when the user clicks on the palette icon of a sticky note to change its color.
501 // It toggles the color selector visible or invisible.
502
503 function NotesClickPalette(evt, uid) {
504         uid_of_note_being_colored = uid;
505         d = $('palette-' + uid_of_note_being_colored);
506
507         if (d.style.display) {
508                 if (d.style.display == 'none') {
509                         d.style.display = 'block';
510                 }
511                 else {
512                         d.style.display = 'none';
513                 }
514         }
515         else {
516                 d.style.display = 'block';
517         }
518
519         return true;
520 }
521
522
523 // Called when the user clicks on one of the colors in an open color selector.
524 // Sets the desired color and then closes the color selector.
525
526 function NotesClickColor(evt, uid, red, green, blue, notecolor, titlecolor) {
527         uid_of_note_being_colored = uid;
528         palette_button = $('palette-' + uid_of_note_being_colored);
529         note_div = $('note-' + uid_of_note_being_colored);
530         titlebar_div = $('titlebar-' + uid_of_note_being_colored);
531
532         // alert('FIXME red=' + red + ' green=' + green + ' blue=' + blue);
533
534         note_div.style.backgroundColor = notecolor;
535         titlebar_div.style.backgroundColor = titlecolor;
536         palette_button.style.display = 'none';
537
538         // submit an ajax http call to record it to the server
539         p = 'note_uid=' + uid_of_note_being_colored
540                 + '&red=' + red
541                 + '&green=' + green
542                 + '&blue=' + blue
543                 + '&r=' + CtdlRandomString();
544         new Ajax.Request(
545                 'ajax_update_note',
546                 {
547                         method: 'post',
548                         parameters: p
549                 }
550         );
551 }
552
553
554
555
556 // These functions handle resizing sticky notes by dragging the resize handle
557
558 var uid_of_note_being_resized = 0;
559 var saved_cursor_style = 'default';
560 var note_was_resized = 0;
561
562 function NotesResizeMouseUp(evt) {
563         document.onmouseup = null;
564         document.onmousemove = null;
565         if (document.layers) {
566                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
567         }
568
569         d = $('note-' + uid_of_note_being_resized);
570         d.style.cursor = saved_cursor_style;
571
572         // If any motion actually occurred, submit an ajax http call to record it to the server
573         if (note_was_resized > 0) {
574                 p = 'note_uid=' + uid_of_note_being_resized
575                         + '&width=' + d.style.width
576                         + '&height=' + d.style.height
577                         + '&r=' + CtdlRandomString();
578                 new Ajax.Request(
579                         'ajax_update_note',
580                         {
581                                 method: 'post',
582                                 parameters: p
583                         }
584                 );
585         }
586
587         uid_of_note_being_resized = '';
588         return false;           // disable the default action
589 }
590
591 function NotesResizeMouseMove(evt) {
592         x = (ns6 ? evt.clientX : event.clientX);
593         x_increment = x - saved_x;
594         y = (ns6 ? evt.clientY : event.clientY);
595         y_increment = y - saved_y;
596
597         // Move the div
598         d = $('note-' + uid_of_note_being_resized);
599
600         divTop = parseInt(d.style.height);
601         divLeft = parseInt(d.style.width);
602
603         d.style.height = (divTop + y_increment) + 'px';
604         d.style.width = (divLeft + x_increment) + 'px';
605
606         saved_x = x;
607         saved_y = y;
608         note_was_resized = 1;
609         return false;           // disable the default action
610 }
611
612
613 function NotesResizeMouseDown(evt, uid) {
614         saved_x = (ns6 ? evt.clientX : event.clientX);
615         saved_y = (ns6 ? evt.clientY : event.clientY);
616         document.onmouseup = NotesResizeMouseUp;
617         document.onmousemove = NotesResizeMouseMove;
618         if (document.layers) {
619                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
620         }
621         uid_of_note_being_resized = uid;
622         d = $('note-' + uid_of_note_being_resized);
623         saved_cursor_style = d.style.cursor;
624         d.style.cursor = 'move';
625         return false;           // disable the default action
626 }
627
628
629 function DeleteStickyNote(evt, uid, confirmation_prompt) {
630         uid_of_note_being_deleted = uid;
631         d = $('note-' + uid_of_note_being_deleted);
632
633         if (confirm(confirmation_prompt)) {
634                 new Effect.Puff(d);
635
636                 // submit an ajax http call to delete it on the server
637                 p = 'note_uid=' + uid_of_note_being_deleted
638                         + '&deletenote=yes'
639                         + '&r=' + CtdlRandomString();
640                 new Ajax.Request(
641                         'ajax_update_note',
642                         {
643                                 method: 'post',
644                                 parameters: p
645                         }
646                 );
647         }
648 }
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664 // These functions handle drag and drop message moving
665
666 var mm_div = null;
667
668 function CtdlMoveMsgMouseDown(evt, msgnum) {
669
670         // do the highlight first
671         CtdlSingleClickMsg(evt, msgnum);
672
673         // Now handle the possibility of dragging
674         saved_x = (ns6 ? evt.clientX : event.clientX);
675         saved_y = (ns6 ? evt.clientY : event.clientY);
676         document.onmouseup = CtdlMoveMsgMouseUp;
677         document.onmousemove = CtdlMoveMsgMouseMove;
678         if (document.layers) {
679                 document.captureEvents(Event.MOUSEUP | Event.MOUSEMOVE);
680         }
681
682         return false;
683 }
684
685 function CtdlMoveMsgMouseMove(evt) {
686         x = (ns6 ? evt.clientX : event.clientX);
687         y = (ns6 ? evt.clientY : event.clientY);
688
689         if ( (x == saved_x) && (y == saved_y) ) {
690                 return true;
691         }
692
693         if (CtdlNumMsgsSelected < 1) { 
694                 return true;
695         }
696
697         if (!mm_div) {
698
699
700                 drag_o_text = "<div style=\"overflow:none; background-color:#fff; color:#000; border: 1px solid black; filter:alpha(opacity=75); -moz-opacity:.75; opacity:.75;\"><tr><td>";
701                 for (i=0; i<CtdlNumMsgsSelected; ++i) {
702                         drag_o_text = drag_o_text + 
703                                 ctdl_ts_getInnerText(
704                                         $('m'+CtdlMsgsSelected[i]).cells[0]
705                                 ) + '<br>';
706                 }
707                 drag_o_text = drag_o_text + "<div>";
708
709                 mm_div = document.createElement("DIV");
710                 mm_div.style.position='absolute';
711                 mm_div.style.top = y + 'px';
712                 mm_div.style.left = x + 'px';
713                 mm_div.style.pixelHeight = '300';
714                 mm_div.style.pixelWidth = '300';
715                 mm_div.innerHTML = drag_o_text;
716                 document.body.appendChild(mm_div);
717         }
718         else {
719                 mm_div.style.top = y + 'px';
720                 mm_div.style.left = x + 'px';
721         }
722
723         return false;   // prevent the default mouse action from happening?
724 }
725
726 function CtdlMoveMsgMouseUp(evt) {
727         document.onmouseup = null;
728         document.onmousemove = null;
729         if (document.layers) {
730                 document.releaseEvents(Event.MOUSEUP | Event.MOUSEMOVE);
731         }
732
733         if (mm_div) {
734                 document.body.removeChild(mm_div);      
735                 mm_div = null;
736         }
737
738         if (num_drop_targets < 1) {     // nowhere to drop
739                 return true;
740         }
741
742         // Did we release the mouse button while hovering over a drop target?
743         // NOTE: this only works cross-browser because the iconbar div is always
744         //      positioned at 0,0.  Browsers differ in whether the 'offset'
745         //      functions return pos relative to the document or parent.
746
747         for (i=0; i<num_drop_targets; ++i) {
748
749                 x = (ns6 ? evt.clientX : event.clientX);
750                 y = (ns6 ? evt.clientY : event.clientY);
751
752                 l = parseInt(drop_targets_elements[i].offsetLeft);
753                 t = parseInt(drop_targets_elements[i].offsetTop);
754                 r = parseInt(drop_targets_elements[i].offsetLeft)
755                   + parseInt(drop_targets_elements[i].offsetWidth);
756                 b = parseInt(drop_targets_elements[i].offsetTop)
757                   + parseInt(drop_targets_elements[i].offsetHeight);
758
759                 /* alert('Offsets are: ' + l + ' ' + t + ' ' + r + ' ' + b + '.'); */
760         
761                 if ( (x >= l) && (x <= r) && (y >= t) && (y <= b) ) {
762                         // Yes, we dropped it on a hotspot.
763                         CtdlMoveSelectedMessages(evt, drop_targets_roomnames[i]);
764                         return true;
765                 }
766         }
767
768         return true;
769 }
770
771
772 function ctdl_ts_getInnerText(el) {
773         if (typeof el == "string") return el;
774         if (typeof el == "undefined") { return el };
775         if (el.innerText) return el.innerText;  //Not needed but it is faster
776         var str = "";
777         
778         var cs = el.childNodes;
779         var l = cs.length;
780         for (var i = 0; i < l; i++) {
781                 switch (cs[i].nodeType) {
782                         case 1: //ELEMENT_NODE
783                                 str += ts_getInnerText(cs[i]);
784                                 break;
785                         case 3: //TEXT_NODE
786                                 str += cs[i].nodeValue;
787                                 break;
788                 }
789         }
790         return str;
791 }
792
793
794 function CtdlShowRaw(msgnum) {
795 var customnav = document.createElement("span");
796 var mode_citadel = document.createElement("a");
797 mode_citadel.appendChild(document.createTextNode("Citadel Source"));
798 var mode_rfc822 = document.createElement("a");
799 mode_rfc822.appendChild(document.createTextNode(" RFC822 Source"));
800 mode_citadel.setAttribute("href","#");
801 mode_rfc822.setAttribute("href","#");
802 mode_rfc822.setAttribute("onclick","rawSwitch822('" + msgnum + "');");
803 mode_citadel.setAttribute("onclick","rawSwitchCitadel('" + msgnum + "');");
804 customnav.appendChild(mode_citadel);
805 customnav.appendChild(mode_rfc822);
806 customnav.setAttribute("class","floatcustomnav");
807 floatwindow("headerscreen","pre",customnav);
808 rawSwitch822(msgnum);
809 }
810
811 function rawSwitch822(msgnum) {
812 CtdlLoadScreen("headerscreen");
813 new Ajax.Updater("headerscreen", 
814 'ajax_servcmd_esc',
815  { method: 'post',parameters: 'g_cmd=MSG2 ' +msgnum  } );
816
817 }
818
819 function rawSwitchCitadel(msgnum) {
820 CtdlLoadScreen("headerscreen");
821 new Ajax.Updater("headerscreen", 
822 'ajax_servcmd_esc',
823  { method: 'post',parameters: 'g_cmd=MSG0 ' +msgnum  } );
824
825 }
826
827 function floatwindow(newdivid,contentelementtype,customnav) {
828 var windiv = document.createElement("div");
829 windiv.setAttribute("class","floatwindow");
830 var winid = newdivid+"_window";
831 windiv.setAttribute("id",winid);
832 var nav = document.createElement("div");
833 if (customnav != null) {
834 nav.appendChild(customnav);
835 }
836 var minimizeA = document.createElement("a");
837 var minimizeButton = document.createTextNode("Close");
838 minimizeA.appendChild(minimizeButton);
839 minimizeA.setAttribute("onclick","killFloatWindow(this);");
840 minimizeA.setAttribute("href","#");
841 nav.appendChild(minimizeA);
842 nav.setAttribute("class","floatnav");
843 windiv.appendChild(nav);
844 var contentarea = document.createElement("pre");
845 contentarea.setAttribute("class","floatcontent");
846 contentarea.setAttribute("id",newdivid);
847 windiv.appendChild(contentarea);
848 document.body.appendChild(windiv);
849 }
850 function killFloatWindow(caller) {
851 var span = caller.parentNode;
852 var fwindow = span.parentNode;
853 fwindow.parentNode.removeChild(fwindow);
854 }
855 // Place a gradient loadscreen on an element, e.g to use before Ajax.updater
856 function CtdlLoadScreen(elementid) {
857 var elem = document.getElementById(elementid);
858 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>";
859 }
860
861
862
863 // Pop open the address book (target_input is the INPUT field to populate)
864
865 function PopOpenAddressBook(target_input) {
866         $('address_book_popup').style.display = 'block';
867         p = 'target_input=' + target_input + '&r=' + CtdlRandomString();
868         new Ajax.Updater(
869                 'address_book_popup_middle_div',
870                 'display_address_book_middle_div',
871                 {
872                         method: 'get',
873                         parameters: p,
874                         evalScripts: true
875                 }
876         );
877 }
878
879 function PopulateAddressBookInnerDiv(which_addr_book, target_input) {
880         $('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>";
881         p = 'which_addr_book=' + which_addr_book
882           + '&target_input=' + target_input
883           + '&r=' + CtdlRandomString();
884         new Ajax.Updater(
885                 'address_book_inner_div',
886                 'display_address_book_inner_div',
887                 {
888                         method: 'get',
889                         parameters: p
890                 }
891         );
892 }
893
894 // What happens when a contact is selected from the address book popup
895 // (populate the specified target)
896
897 function AddContactsToTarget(target, whichaddr) {
898         while (whichaddr.selectedIndex != -1) {
899                 if (target.value.length > 0) {
900                         target.value = target.value + ', ';
901                 }
902                 target.value = target.value + whichaddr.value;
903                 whichaddr.options[whichaddr.selectedIndex].selected = false;
904         }
905 }
906
907 // Respond to a meeting invitation
908 function RespondToInvitation(question_divname, title_divname, msgnum, cal_partnum, sc) {
909         p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
910         new Ajax.Updater(title_divname, 'respond_to_request', { method: 'post', parameters: p } );
911         Effect.Fade(question_divname, { duration: 0.5 });
912 }
913
914 // Handle a received RSVP
915 function HandleRSVP(question_divname, title_divname, msgnum, cal_partnum, sc) {
916         p = 'msgnum=' + msgnum + '&cal_partnum=' + cal_partnum + '&sc=' + sc ;
917         new Ajax.Updater(title_divname, 'handle_rsvp', { method: 'post', parameters: p } );
918         Effect.Fade(question_divname, { duration: 0.5 });
919 }
920 var fakeMouse = document.createEvent("MouseEvents");
921 fakeMouse.initMouseEvent("click", true, true, window, 
922         0,0,0,0,0, false, false, false, false, 0, null);
923 // TODO: Collapse into one function
924 function toggleTaskDtStart(event) {
925         var checkBox = $('nodtstart');
926         dtStart = document.getElementById("dtstart");
927         if (checkBox.checked) {
928                 dtStart.disabled = true;
929                 dtStart.style.textDecoration = "line-through";
930         } else {
931                 dtStart.disabled = false;
932                 dtStart.style.textDecoration = "";
933                 if (dtStart.value.length == 0)
934                         dtStart.dpck._initCurrentDate();
935         }
936 }
937 function toggleTaskDue(event) {
938         var checkBox = $('nodue');
939         dueField = document.getElementById("due");
940         if (checkBox.checked) {
941                 dueField.disabled = true;
942                 dueField.style.textDecoration = "line-through";
943         } else {
944                 dueField.disabled = false;
945                 dueField.style.textDecoration = "";
946                 if (dueField.value.length == 0)
947                         dueField.dpck._initCurrentDate();
948         }
949 }
950 function ToggleTaskDateOrNoDateActivate(event) {
951         var dtstart = document.getElementById("nodtstart");
952         if (dtstart != null) {
953                 toggleTaskDtStart(null);
954                 toggleTaskDue(null);
955                 $('nodtstart').observe('click', toggleTaskDtStart);
956                 $('nodue').observe('click', toggleTaskDue);
957         } 
958 }
959 function TaskViewGatherCategoriesFromTable() {
960         var table = $('taskview');
961         
962 }
963 function attachDatePicker(relative, wclang) {
964         var dpck = new DatePicker({
965         relative: relative,
966         language: wclang.substr(0,2),
967         disableFutureDate: false,
968         dateFormat: [ ["yyyy", "mm", "dd"], "-"],
969         showDuration: 0.2,
970         closeEffectDuration: 0.2
971         });
972         document.getElementById(relative).dpck = dpck; // attach a ref to it
973 }
974
975 function eventEditAllDay() {
976         var allDayCheck = $('alldayevent');
977         var dtend = $('dtendcell');
978
979         if (allDayCheck.checked) {
980                 //dtend.disabled = true;
981                 dtend.style.textDecoration = "line-through";
982         } else {
983                 //dtend_day.disabled = false;
984                 dtend.style.textDecoration = "";
985         }
986 }
987
988
989
990
991 // Functions which handle show/hide of various elements in the recurrence editor
992
993 function RecurrenceShowHide() {
994
995         if ($('is_recur').checked) {
996                 $('rrule_div').style.display = 'block';
997         }
998         else {
999                 $('rrule_div').style.display = 'none';
1000         }
1001
1002         if ($('freq_selector').selectedIndex == 4) {
1003                 $('weekday_selector').style.display = 'block';
1004         }
1005         else {
1006                 $('weekday_selector').style.display = 'none';
1007         }
1008
1009         if ($('rrend_count').checked) {
1010                 $('rrcount').disabled = false;
1011         }
1012         else {
1013                 $('rrcount').disabled = true;
1014         }
1015
1016         if ($('rrend_until').checked) {
1017                 $('rruntil').disabled = false;
1018         }
1019         else {
1020                 $('rruntil').disabled = true;
1021         }
1022
1023 }