svncommit
2006-01-08 c0da982d9e11bb80ec9948e89ccdb55b90bc77e3
commit | author | age
e0ed97 1 /*
4e17e6 2  +-----------------------------------------------------------------------+
T 3  | RoundCube Webmail Client Script                                       |
4  |                                                                       |
5  | This file is part of the RoundCube Webmail client                     |
6  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
30233b 7  | Licensed under the GNU GPL                                            |
4e17e6 8  |                                                                       |
T 9  +-----------------------------------------------------------------------+
10  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
11  +-----------------------------------------------------------------------+
15a9d1 12  
T 13   $Id$
4e17e6 14 */
T 15
16
17 var rcube_webmail_client;
18
19 function rcube_webmail()
20   {
21   this.env = new Object();
10a699 22   this.labels = new Object();
4e17e6 23   this.buttons = new Object();
T 24   this.gui_objects = new Object();
25   this.commands = new Object();
26   this.selection = new Array();
27
28   // create public reference to myself
29   rcube_webmail_client = this;
30   this.ref = 'rcube_webmail_client';
31  
32   // webmail client settings
33   this.dblclick_time = 600;
34   this.message_time = 5000;
e66f5b 35   this.request_timeout = 180000;
ecf759 36   this.kepp_alive_interval = 60000;
4e17e6 37   this.mbox_expression = new RegExp('[^0-9a-z\-_]', 'gi');
T 38   this.env.blank_img = 'skins/default/images/blank.gif';
39   
40   // mimetypes supported by the browser (default settings)
41   this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
42                              'image/jpeg', 'image/gif', 'image/png',
43                              'application/x-javascript', 'application/pdf',
44                              'application/x-shockwave-flash');
45
46
47   // set environment variable
48   this.set_env = function(name, value)
49     {
50     //if (!this.busy)
51       this.env[name] = value;    
52     };
10a699 53
T 54
55   // add a localized label to the client environment
56   this.add_label = function(key, value)
57     {
58     this.labels[key] = value;
59     };
60
4e17e6 61
T 62   // add a button to the button list
63   this.register_button = function(command, id, type, act, sel, over)
64     {
65     if (!this.buttons[command])
66       this.buttons[command] = new Array();
67       
68     var button_prop = {id:id, type:type};
69     if (act) button_prop.act = act;
70     if (sel) button_prop.sel = sel;
71     if (over) button_prop.over = over;
72
73     this.buttons[command][this.buttons[command].length] = button_prop;    
74     };
75
76
77   // register a specific gui object
78   this.gui_object = function(name, id)
79     {
80     this.gui_objects[name] = id;
81     };
82
83
84   // initialize webmail client
85   this.init = function()
86     {
87     this.task = this.env.task;
88     
89     // check browser
a95e0e 90     if (!bw.dom || !bw.xmlhttp_test())
4e17e6 91       {
T 92       location.href = this.env.comm_path+'&_action=error&_code=0x199';
93       return;
94       }
95     
96     // find all registered gui objects
97     for (var n in this.gui_objects)
98       this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
99       
100     // tell parent window that this frame is loaded
101     if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
102       parent.rcmail.set_busy(false);
103
104     // enable general commands
105     this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
106     
107     switch (this.task)
108       {
109       case 'mail':
110         var msg_list = this.gui_objects.messagelist;
111         if (msg_list)
112           {
113           this.init_messagelist(msg_list);
114           this.enable_command('markread', true);
115           }
116
117         // enable mail commands
118         this.enable_command('list', 'compose', 'add-contact', true);
119         
120         if (this.env.action=='show')
121           {
583f1c 122           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'viewsource', 'print', 'load-attachment', true);
4e17e6 123           if (this.env.next_uid)
T 124             this.enable_command('nextmessage', true);
125           if (this.env.prev_uid)
126             this.enable_command('previousmessage', true);
127           }
128
129         if (this.env.action=='show' && this.env.blockedobjects)
130           {
131           if (this.gui_objects.remoteobjectsmsg)
132             this.gui_objects.remoteobjectsmsg.style.display = 'block';
133           this.enable_command('load-images', true);
134           }  
135
136         if (this.env.action=='compose')
137           this.enable_command('add-attachment', 'send-attachment', 'send', true);
138           
139         if (this.env.messagecount)
15a9d1 140           this.enable_command('select-all', 'select-none', 'sort', 'expunge', true);
4e17e6 141
T 142         this.set_page_buttons();
143
144         // focus this window
145         window.focus();
146
147         // init message compose form
148         if (this.env.action=='compose')
149           this.init_messageform();
150
151         // show printing dialog
152         if (this.env.action=='print')
153           window.print();
15a9d1 154           
T 155         // get unread count for each mailbox
156         if (this.gui_objects.mailboxlist)
157           this.http_request('getunread', '');
4e17e6 158
T 159         break;
160
161
162       case 'addressbook':
d1d2c4 163         var contacts_list      = this.gui_objects.contactslist;
S 164         var ldap_contacts_list = this.gui_objects.ldapcontactslist;
165
4e17e6 166         if (contacts_list)
T 167           this.init_contactslist(contacts_list);
168       
d1d2c4 169         if (ldap_contacts_list)
S 170           this.init_ldapsearchlist(ldap_contacts_list);
171
4e17e6 172         this.set_page_buttons();
T 173           
174         if (this.env.cid)
175           this.enable_command('show', 'edit', true);
176
177         if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
178           this.enable_command('save', true);
179       
c0da98 180         this.enable_command('list', 'add', true);
S 181
182         this.enable_command('ldappublicsearch', this.env.ldappublicsearch);
183
4e17e6 184         break;
T 185
186
187       case 'settings':
188         this.enable_command('preferences', 'identities', 'save', 'folders', true);
189         
190         if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
191           this.enable_command('edit', 'add', 'delete', true);
192
193         if (this.env.action=='edit-identity' || this.env.action=='add-identity')
194           this.enable_command('save', true);
195           
196         if (this.env.action=='folders')
197           this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'delete-folder', true);
198           
199         var identities_list = this.gui_objects.identitieslist;
200         if (identities_list)
201           this.init_identitieslist(identities_list);
202
203         break;
204
205       case 'login':
206         var input_user = rcube_find_object('_user');
207         var input_pass = rcube_find_object('_pass');
208         if (input_user && input_user.value=='')
209           input_user.focus();
210         else if (input_pass)
211           input_pass.focus();
212           
213         this.enable_command('login', true);
214         break;
215       
216       default:
217         break;
218       }
219
220
221     // enable basic commands
222     this.enable_command('logout', true);
223
224     // disable browser's contextmenus
1cded8 225     // document.oncontextmenu = function(){ return false; }
4e17e6 226
T 227     // flag object as complete
228     this.loaded = true;
7902df 229           
4e17e6 230     // show message
T 231     if (this.pending_message)
232       this.display_message(this.pending_message[0], this.pending_message[1]);
ecf759 233       
15a9d1 234     // start interval for keep-alive/recent_check signal
ecf759 235     if (this.kepp_alive_interval)
15a9d1 236       this.kepp_alive_int = setInterval(this.ref+'.'+(this.task=='mail'?'check_for_recent()':'send_keep_alive()'), this.kepp_alive_interval);
4e17e6 237     };
T 238
239
240   // get all message rows from HTML table and init each row
241   this.init_messagelist = function(msg_list)
242     {
243     if (msg_list && msg_list.tBodies[0])
244       {
245       this.message_rows = new Array();
246
247       var row;
248       for(var r=0; r<msg_list.tBodies[0].childNodes.length; r++)
249         {
250         row = msg_list.tBodies[0].childNodes[r];
251         //row = msg_list.tBodies[0].rows[r];
252         this.init_message_row(row);
253         }
254       }
255       
256     // alias to common rows array
257     this.list_rows = this.message_rows;
258     };
259     
260     
261   // make references in internal array and set event handlers
262   this.init_message_row = function(row)
263     {
264     var uid, msg_icon;
265     
266     if (String(row.id).match(/rcmrow([0-9]+)/))
267       {
268       uid = RegExp.$1;
269       row.uid = uid;
270               
271       this.message_rows[uid] = {id:row.id, obj:row,
272                                 classname:row.className,
273                                 unread:this.env.messages[uid] ? this.env.messages[uid].unread : null,
274                                 replied:this.env.messages[uid] ? this.env.messages[uid].replied : null};
275               
276       // set eventhandlers to table row
277       row.onmousedown = function(e){ return rcube_webmail_client.drag_row(e, this.uid); };
278       row.onmouseup = function(e){ return rcube_webmail_client.click_row(e, this.uid); };
279               
280       // set eventhandler to message icon
281       if ((msg_icon = row.cells[0].childNodes[0]) && row.cells[0].childNodes[0].nodeName=='IMG')
282         {                
283         msg_icon.id = 'msgicn_'+uid;
284         msg_icon._row = row;
285         msg_icon.onmousedown = function(e) { rcube_webmail_client.command('markread', this); };
286                 
287         // get message icon and save original icon src
288         this.message_rows[uid].icon = msg_icon;
289         }
290       }
291     };
292
293
294   // init message compose form: set focus and eventhandlers
295   this.init_messageform = function()
296     {
297     if (!this.gui_objects.messageform)
298       return false;
299     
300     //this.messageform = this.gui_objects.messageform;
1cded8 301     var input_from = rcube_find_object('_from');
4e17e6 302     var input_to = rcube_find_object('_to');
T 303     var input_cc = rcube_find_object('_cc');
304     var input_bcc = rcube_find_object('_bcc');
305     var input_replyto = rcube_find_object('_replyto');
306     var input_subject = rcube_find_object('_subject');
307     var input_message = rcube_find_object('_message');
308     
309     // init live search events
310     if (input_to)
311       this.init_address_input_events(input_to);
312     if (input_cc)
313       this.init_address_input_events(input_cc);
314     if (input_bcc)
315       this.init_address_input_events(input_bcc);
1cded8 316       
T 317     // add signature according to selected identity
318     if (input_from && input_from.type=='select-one')
319       this.change_identity(input_from);
4e17e6 320
T 321     if (input_to && input_to.value=='')
322       input_to.focus();
323     else if (input_subject && input_subject.value=='')
324       input_subject.focus();
325     else if (input_message)
326       this.set_caret2start(input_message); // input_message.focus();
977a29 327     
T 328     // get summary of all field values
329     this.cmp_hash = this.compose_field_hash();
4e17e6 330     };
T 331
332
333   this.init_address_input_events = function(obj)
334     {
335     var handler = function(e){ return rcube_webmail_client.ksearch_keypress(e,this); };
336     var handler2 = function(e){ return rcube_webmail_client.ksearch_blur(e,this); };
337     
338     if (bw.safari)
339       {
340       obj.addEventListener('keydown', handler, false);
341       // obj.addEventListener('blur', handler2, false);
342       }
343     else if (bw.mz)
344       {
345       obj.addEventListener('keypress', handler, false);
346       obj.addEventListener('blur', handler2, false);
347       }
348     else if (bw.ie)
349       {
350       obj.onkeydown = handler;
351       //obj.attachEvent('onkeydown', handler);
352       // obj.attachEvent('onblur', handler2, false);
353       }
354     
355     obj.setAttribute('autocomplete', 'off');       
356     };
357
358
359
360   // get all contact rows from HTML table and init each row
361   this.init_contactslist = function(contacts_list)
362     {
363     if (contacts_list && contacts_list.tBodies[0])
364       {
365       this.contact_rows = new Array();
366
367       var row;
368       for(var r=0; r<contacts_list.tBodies[0].childNodes.length; r++)
369         {
370         row = contacts_list.tBodies[0].childNodes[r];
371         this.init_table_row(row, 'contact_rows');
372         }
373       }
374
375     // alias to common rows array
376     this.list_rows = this.contact_rows;
377     
378     if (this.env.cid)
379       this.select(this.env.cid);
380     };
381
382
d1d2c4 383   // get all contact rows from HTML table and init each row
S 384   this.init_ldapsearchlist = function(ldap_contacts_list)
385     {
386     if (ldap_contacts_list && ldap_contacts_list.tBodies[0])
387       {
388       this.ldap_contact_rows = new Array();
389
390       var row;
391       for(var r=0; r<ldap_contacts_list.tBodies[0].childNodes.length; r++)
392         {
393         row = ldap_contacts_list.tBodies[0].childNodes[r];
394         this.init_table_row(row, 'ldap_contact_rows');
395         }
396       }
397
398     // alias to common rows array
399     this.list_rows = this.ldap_contact_rows;
400     };
401
402
4e17e6 403   // make references in internal array and set event handlers
T 404   this.init_table_row = function(row, array_name)
405     {
406     var cid;
407     
408     if (String(row.id).match(/rcmrow([0-9]+)/))
409       {
410       cid = RegExp.$1;
411       row.cid = cid;
412
413       this[array_name][cid] = {id:row.id,
414                                obj:row,
415                                classname:row.className};
416
417       // set eventhandlers to table row
418       row.onmousedown = function(e) { rcube_webmail_client.in_selection_before=this.cid; return false; };  // fake for drag handler
419       row.onmouseup = function(e){ return rcube_webmail_client.click_row(e, this.cid); };
420       }
421     };
422
423
424   // get all contact rows from HTML table and init each row
425   this.init_identitieslist = function(identities_list)
426     {
427     if (identities_list && identities_list.tBodies[0])
428       {
429       this.identity_rows = new Array();
430
431       var row;
432       for(var r=0; r<identities_list.tBodies[0].childNodes.length; r++)
433         {
434         row = identities_list.tBodies[0].childNodes[r];
435         this.init_table_row(row, 'identity_rows');
436         }
437       }
438
439     // alias to common rows array
440     this.list_rows = this.identity_rows;
441     
442     if (this.env.iid)
443       this.select(this.env.iid);    
444     };
445     
446
447
448   /*********************************************************/
449   /*********       client command interface        *********/
450   /*********************************************************/
451
452
453   // execute a specific command on the web client
454   this.command = function(command, props, obj)
455     {
456     if (obj && obj.blur)
457       obj.blur();
458
459     if (this.busy)
460       return false;
461
462     // command not supported or allowed
463     if (!this.commands[command])
464       {
465       // pass command to parent window
466       if (this.env.framed && parent.rcmail && parent.rcmail.command)
467         parent.rcmail.command(command, props);
468
469       return false;
470       }
15a9d1 471       
T 472       
473    // check input before leaving compose step
474    if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
475      {
476      if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
477         return false;
478      }
479
4e17e6 480
T 481     // process command
482     switch (command)
483       {
484       case 'login':
485         if (this.gui_objects.loginform)
486           this.gui_objects.loginform.submit();
487         break;
488
489       case 'logout':
490         location.href = this.env.comm_path+'&_action=logout';
491         break;      
492
493       // commands to switch task
494       case 'mail':
495       case 'addressbook':
496       case 'settings':
497         this.switch_task(command);
498         break;
499
500
501       // misc list commands
502       case 'list':
503         if (this.task=='mail')
504           this.list_mailbox(props);
505         else if (this.task=='addressbook')
506           this.list_contacts();
f3b659 507         break;
T 508
509       case 'sort':
510         // get the type of sorting
b076a4 511         var a_sort = props.split('_');
T 512         var sort_col = a_sort[0];
1cded8 513         var sort_order = a_sort[1] ? a_sort[1].toUpperCase() : null;
b076a4 514         var header;
1cded8 515         
T 516         // no sort order specified: toggle
517         if (sort_order==null)
518           {
519           if (this.env.sort_col==sort_col)
520             sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
521           else
522             sort_order = this.env.sort_order;
523           }
b076a4 524         
T 525         if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
526           break;
527
528         // set table header class
529         if (header = document.getElementById('rcmHead'+this.env.sort_col))
530           this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
531         if (header = document.getElementById('rcmHead'+sort_col))
532           this.set_classname(header, 'sorted'+sort_order, true);
533
534         // save new sort properties
535         this.env.sort_col = sort_col;
536         this.env.sort_order = sort_order;
537
538         // reload message list
1cded8 539         this.list_mailbox('', '', sort_col+'_'+sort_order);
4e17e6 540         break;
T 541
542       case 'nextpage':
543         this.list_page('next');
544         break;
545
546       case 'previouspage':
547         this.list_page('prev');
15a9d1 548         break;
T 549
550       case 'expunge':
551         if (this.env.messagecount)
552           this.expunge_mailbox(this.env.mailbox);
553         break;
554
555       case 'clear-mailbox':
556         //if (this.env.messagecount)
557           //this.clear_mailbox(this.env.mailbox);
4e17e6 558         break;
T 559
560
561       // common commands used in multiple tasks
562       case 'show':
563         if (this.task=='mail')
564           {
565           var uid = this.get_single_uid();
566           if (uid && (!this.env.uid || uid != this.env.uid))
567             this.show_message(uid);
568           }
569         else if (this.task=='addressbook')
570           {
571           var cid = props ? props : this.get_single_cid();
572           if (cid && !(this.env.action=='show' && cid==this.env.cid))
573             this.load_contact(cid, 'show');
574           }
575         break;
576
577       case 'add':
578         if (this.task=='addressbook')
d1d2c4 579           if (!window.frames[this.env.contentframe].rcmail)
S 580             this.load_contact(0, 'add');
581           else
582             {
583             if (window.frames[this.env.contentframe].rcmail.selection.length)
584               this.add_ldap_contacts();
585             else
586               this.load_contact(0, 'add');
587             }
4e17e6 588         else if (this.task=='settings')
T 589           {
590           this.clear_selection();
591           this.load_identity(0, 'add-identity');
592           }
593         break;
594
595       case 'edit':
596         var cid;
597         if (this.task=='addressbook' && (cid = this.get_single_cid()))
598           this.load_contact(cid, 'edit');
599         else if (this.task=='settings' && props)
600           this.load_identity(props, 'edit-identity');
601         break;
602
603       case 'save-identity':
604       case 'save':
605         if (this.gui_objects.editform)
10a699 606           {
T 607           var input_pagesize = rcube_find_object('_pagesize');
608           var input_name  = rcube_find_object('_name');
609           var input_email = rcube_find_object('_email');
610
611           // user prefs
e66f5b 612           if (input_pagesize && isNaN(input_pagesize.value))
10a699 613             {
T 614             alert(this.get_label('nopagesizewarning'));
615             input_pagesize.focus();
616             break;
617             }
618           // contacts/identities
619           else
620             {
621             if (input_name && input_name.value == '')
622               {
623               alert(this.get_label('nonamewarning'));
624               input_name.focus();
625               break;
626               }
627             else if (input_email && !rcube_check_email(input_email.value))
628               {
629               alert(this.get_label('noemailwarning'));
630               input_email.focus();
631               break;
632               }
633             }
634
4e17e6 635           this.gui_objects.editform.submit();
10a699 636           }
4e17e6 637         break;
T 638
639       case 'delete':
640         // mail task
641         if (this.task=='mail' && this.env.trash_mailbox && String(this.env.mailbox).toLowerCase()!=String(this.env.trash_mailbox).toLowerCase())
642           this.move_messages(this.env.trash_mailbox);
643         else if (this.task=='mail')
644           this.delete_messages();
645         // addressbook task
646         else if (this.task=='addressbook')
647           this.delete_contacts();
648         // user settings task
649         else if (this.task=='settings')
650           this.delete_identity();
651         break;
652
653
654       // mail task commands
655       case 'move':
656       case 'moveto':
657         this.move_messages(props);
658         break;
659         
660       case 'markread':
661         if (props && !props._row)
662           break;
663         
664         var uid;
665         var flag = 'read';
666         
667         if (props._row.uid)
668           {
669           uid = props._row.uid;
670           this.dont_select = true;
671           
672           // toggle read/unread
673           if (!this.message_rows[uid].unread)
674             flag = 'unread';
675           }
676           
677         this.mark_message(flag, uid);
678         break;
679         
680       case 'load-images':
681         if (this.env.uid)
682           this.show_message(this.env.uid, true);
683         break;
684
685       case 'load-attachment':
686         var url = this.env.comm_path+'&_action=get&_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
687         
688         // open attachment in frame if it's of a supported mimetype
689         if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
690           {
691           this.attachment_win = window.open(url+'&_frame=1', 'rcubemailattachment');
692           if (this.attachment_win)
693             {
694             setTimeout(this.ref+'.attachment_win.focus()', 10);
695             break;
696             }
697           }
698
699         location.href = url;
700         break;
701         
702       case 'select-all':
703         this.select_all(props);
704         break;
705
706       case 'select-none':
707         this.clear_selection();
708         break;
709
710       case 'nextmessage':
711         if (this.env.next_uid)
09941e 712           this.show_message(this.env.next_uid);
T 713           //location.href = this.env.comm_path+'&_action=show&_uid='+this.env.next_uid+'&_mbox='+this.env.mailbox;
4e17e6 714         break;
T 715
716       case 'previousmessage':
717         if (this.env.prev_uid)
09941e 718           this.show_message(this.env.prev_uid);
T 719           //location.href = this.env.comm_path+'&_action=show&_uid='+this.env.prev_uid+'&_mbox='+this.env.mailbox;
4e17e6 720         break;
d1d2c4 721       
S 722       
4e17e6 723       case 'compose':
T 724         var url = this.env.comm_path+'&_action=compose';
725         
726         // modify url if we're in addressbook
727         if (this.task=='addressbook')
728           {
729           url = this.get_task_url('mail', url);            
730           var a_cids = new Array();
731           
732           // use contact_id passed as command parameter
733           if (props)
734             a_cids[a_cids.length] = props;
735             
736           // get selected contacts
737           else
738             {
d1d2c4 739             if (!window.frames[this.env.contentframe].rcmail.selection.length)
S 740               {
741               for (var n=0; n<this.selection.length; n++)
742                 a_cids[a_cids.length] = this.selection[n];
743               }
744             else
745               {
746               var frameRcmail = window.frames[this.env.contentframe].rcmail;
747               // get the email address(es)
748               for (var n=0; n<frameRcmail.selection.length; n++)
749                 a_cids[a_cids.length] = frameRcmail.ldap_contact_rows[frameRcmail.selection[n]].obj.cells[1].innerHTML;
750               }
4e17e6 751             }
T 752           if (a_cids.length)
753             url += '&_to='+a_cids.join(',');
754           else
755             break;
d1d2c4 756             
4e17e6 757           }
T 758         else if (props)
759            url += '&_to='+props;
d1d2c4 760         
S 761         // don't know if this is necessary...
762         url = url.replace(/&_framed=1/, "");
4e17e6 763
T 764         this.set_busy(true);
d1d2c4 765
S 766         // need parent in case we are coming from the contact frame
767         parent.window.location.href = url;
768         break;    
4e17e6 769
T 770       case 'send':
771         if (!this.gui_objects.messageform)
772           break;
773           
977a29 774         if (!this.check_compose_input())
10a699 775           break;
T 776
777         // all checks passed, send message
778         this.set_busy(true, 'sendingmessage');
779         var form = this.gui_objects.messageform;
780         form.submit();
4e17e6 781         break;
T 782
783       case 'add-attachment':
784         this.show_attachment_form(true);
785         
786       case 'send-attachment':
787         this.upload_file(props)      
788         break;
789
583f1c 790       case 'reply-all':
4e17e6 791       case 'reply':
T 792         var uid;
793         if (uid = this.get_single_uid())
794           {
795           this.set_busy(true);
583f1c 796           location.href = this.env.comm_path+'&_action=compose&_reply_uid='+uid+'&_mbox='+escape(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : '');
4e17e6 797           }
T 798         break;      
799
800       case 'forward':
801         var uid;
802         if (uid = this.get_single_uid())
803           {
804           this.set_busy(true);
805           location.href = this.env.comm_path+'&_action=compose&_forward_uid='+uid+'&_mbox='+escape(this.env.mailbox);
806           }
807         break;
808         
809       case 'print':
810         var uid;
811         if (uid = this.get_single_uid())
812           {
813           this.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+escape(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
814           if (this.printwin)
815             setTimeout(this.ref+'.printwin.focus()', 20);
816           }
817         break;
818
819       case 'viewsource':
820         var uid;
821         if (uid = this.get_single_uid())
822           {          
823           this.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+escape(this.env.mailbox));
824           if (this.sourcewin)
825             setTimeout(this.ref+'.sourcewin.focus()', 20);
826           }
827         break;
828
829       case 'add-contact':
830         this.add_contact(props);
831         break;
d1d2c4 832       
S 833
834       // ldap search
835       case 'ldappublicsearch':
836         if (this.gui_objects.ldappublicsearchform) 
837           this.gui_objects.ldappublicsearchform.submit();
838         else 
839           this.ldappublicsearch(command);
840         break; 
4e17e6 841
T 842
843       // user settings commands
844       case 'preferences':
845         location.href = this.env.comm_path;
846         break;
847
848       case 'identities':
849         location.href = this.env.comm_path+'&_action=identities';
850         break;
851           
852       case 'delete-identity':
853         this.delete_identity();
854         
855       case 'folders':
856         location.href = this.env.comm_path+'&_action=folders';
857         break;
858
859       case 'subscribe':
860         this.subscribe_folder(props);
861         break;
862
863       case 'unsubscribe':
864         this.unsubscribe_folder(props);
865         break;
866         
867       case 'create-folder':
868         this.create_folder(props);
869         break;
870
871       case 'delete-folder':
1cded8 872         if (confirm(this.get_label('deletefolderconfirm')))
4e17e6 873           this.delete_folder(props);
T 874         break;
875
876       }
877
878     return obj ? false : true;
879     };
880
881
882   // set command enabled or disabled
883   this.enable_command = function()
884     {
885     var args = this.enable_command.arguments;
886     if(!args.length) return -1;
887
888     var command;
889     var enable = args[args.length-1];
890     
891     for(var n=0; n<args.length-1; n++)
892       {
893       command = args[n];
894       this.commands[command] = enable;
895       this.set_button(command, (enable ? 'act' : 'pas'));
896       }
897     };
898
899
a95e0e 900   // lock/unlock interface
4e17e6 901   this.set_busy = function(a, message)
T 902     {
903     if (a && message)
10a699 904       {
T 905       var msg = this.get_label(message);
906       if (msg==message)        
907         msg = 'Loading...';
908
909       this.display_message(msg, 'loading', true);
910       }
4e17e6 911     else if (!a && this.busy)
T 912       this.hide_message();
913
914     this.busy = a;
915     //document.body.style.cursor = a ? 'wait' : 'default';
916     
917     if (this.gui_objects.editform)
918       this.lock_form(this.gui_objects.editform, a);
a95e0e 919       
T 920     // clear pending timer
921     if (this.request_timer)
922       clearTimeout(this.request_timer);
923
924     // set timer for requests
925     if (a && this.request_timeout)
926       this.request_timer = setTimeout(this.ref+'.request_timed_out()', this.request_timeout);
4e17e6 927     };
T 928
929
10a699 930   // return a localized string
T 931   this.get_label = function(name)
932     {
933     if (this.labels[name])
934       return this.labels[name];
935     else
936       return name;
937     };
938
939
940   // switch to another application task
4e17e6 941   this.switch_task = function(task)
T 942     {
01bb03 943     if (this.task===task && task!='mail')
4e17e6 944       return;
T 945
01bb03 946     var url = this.get_task_url(task);
T 947     if (task=='mail')
948       url += '&_mbox=INBOX';
949
4e17e6 950     this.set_busy(true);
01bb03 951     location.href = url;
4e17e6 952     };
T 953
954
955   this.get_task_url = function(task, url)
956     {
957     if (!url)
958       url = this.env.comm_path;
959
960     return url.replace(/_task=[a-z]+/, '_task='+task);
a95e0e 961     };
T 962     
963   
964   // called when a request timed out
965   this.request_timed_out = function()
966     {
967     this.set_busy(false);
968     this.display_message('Request timed out!', 'error');
4e17e6 969     };
T 970
971
972   /*********************************************************/
973   /*********        event handling methods         *********/
974   /*********************************************************/
975
976
977   // onmouseup handler for mailboxlist item
978   this.mbox_mouse_up = function(mbox)
979     {
980     if (this.drag_active)
981       this.command('moveto', mbox);
982     else
983       this.command('list', mbox);
984       
985     return false;
986     };
987
988
989   // onmousedown-handler of message list row
990   this.drag_row = function(e, id)
991     {
992     this.in_selection_before = this.in_selection(id) ? id : false;
993
994     // don't do anything (another action processed before)
995     if (this.dont_select)
996       return false;
997
998     if (!this.in_selection_before)
999       {
1cded8 1000       var ctrl = this.check_ctrlkey(e);
T 1001       this.select(id, ctrl);
4e17e6 1002       }
T 1003     
1004     if (this.selection.length)
1005       {
1006       this.drag_start = true;
1007       document.onmousemove = function(e){ return rcube_webmail_client.drag_mouse_move(e); };
1008       document.onmouseup = function(e){ return rcube_webmail_client.drag_mouse_up(e); };
1009       }
1010
1011     return false;
1012     };
1013
1014
1015   // onmouseup-handler of message list row
1016   this.click_row = function(e, id)
1017     {
d1d2c4 1018     var shift = this.check_shiftkey(e);
4e17e6 1019     
T 1020     // don't do anything (another action processed before)
1021     if (this.dont_select)
1022       {
1023       this.dont_select = false;
1024       return false;
1025       }
1026     
1027     if (!this.drag_active && this.in_selection_before==id)
d1d2c4 1028       {
S 1029       this.select(id, (shift && this.task!='settings'));
1030       }
4e17e6 1031     
T 1032     this.drag_start = false;
1033     this.in_selection_before = false;
1034         
1035     // row was double clicked
d1d2c4 1036     if (this.task=='mail' && this.list_rows && this.list_rows[id].clicked && !shift)
4e17e6 1037       {
T 1038       this.show_message(id);
1039       return false;
1040       }
1041     else if (this.task=='addressbook')
1042       {
d1d2c4 1043       if (this.contact_rows && this.selection.length==1)
S 1044         {
4e17e6 1045         this.load_contact(this.selection[0], 'show', true);
d1d2c4 1046         // change the text for the add contact button
S 1047         var links = parent.document.getElementById('abooktoolbar').getElementsByTagName('A');
1048         for (i = 0; i < links.length; i++)
1049           {
1050           var onclickstring = new String(links[i].onclick);
1051           if (onclickstring.search('\"add\"') != -1)
1052             links[i].title = this.env.newcontact;
1053           }
1054         }
1055       else if (this.contact_rows && this.contact_rows[id].clicked)
4e17e6 1056         {
T 1057         this.load_contact(id, 'show');
1058         return false;
1059         }
d1d2c4 1060       else if (this.ldap_contact_rows && !this.ldap_contact_rows[id].clicked)
S 1061         {
1062         // clear selection
1063         parent.rcmail.clear_selection();
1064
1065         // disable delete
1066         parent.rcmail.set_button('delete', 'pas');
1067
1068         // change the text for the add contact button
1069         var links = parent.document.getElementById('abooktoolbar').getElementsByTagName('A');
1070         for (i = 0; i < links.length; i++)
1071           {
1072           var onclickstring = new String(links[i].onclick);
1073           if (onclickstring.search('\"add\"') != -1)
1074             links[i].title = this.env.addcontact;
1075           }
1076         }
1077       // handle double click event
1078       else if (this.ldap_contact_rows && this.selection.length==1 && this.ldap_contact_rows[id].clicked)
1079         this.command('compose', this.ldap_contact_rows[id].obj.cells[1].innerHTML);
4e17e6 1080       else if (this.env.contentframe)
T 1081         {
1082         var elm = document.getElementById(this.env.contentframe);
1083         elm.style.visibility = 'hidden';
1084         }
1085       }
1086     else if (this.task=='settings')
1087       {
1088       if (this.selection.length==1)
1089         this.command('edit', this.selection[0]);
1090       }
1091
1092     this.list_rows[id].clicked = true;
1093     setTimeout(this.ref+'.list_rows['+id+'].clicked=false;', this.dblclick_time);
1094       
1095     return false;
1096     };
1097
1098
1099
1100   /*********************************************************/
1101   /*********     (message) list functionality      *********/
1102   /*********************************************************/
1103
1104
1105   // select/unselect a row
1106   this.select = function(id, multiple)
1107     {
1108     var selected = false
1109     
1110     if (this.list_rows[id] && !multiple)
1111       {
1112       this.clear_selection();
1113       this.selection[0] = id;
1114       this.list_rows[id].obj.className += ' selected';
1115       selected = true;
1116       }
1117     
1118     else if (this.list_rows[id])
1119       {
1120       if (!this.in_selection(id))  // select row
1121         {
1122         this.selection[this.selection.length] = id;
1123         this.set_classname(this.list_rows[id].obj, 'selected', true);        
1124         }
1125       else  // unselect row
1126         {
1127         var p = find_in_array(id, this.selection);
1128         var a_pre = this.selection.slice(0, p);
1129         var a_post = this.selection.slice(p+1, this.selection.length);
1130         this.selection = a_pre.concat(a_post);
1131         this.set_classname(this.list_rows[id].obj, 'selected', false);
1132         }
1133         
1134       selected = (this.selection.length==1);
1135       }
1136
1137     // enable/disable commands for message
1138     if (this.task=='mail')
1139       {
583f1c 1140       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
4e17e6 1141       this.enable_command('delete', 'moveto', this.selection.length>0 ? true : false);
T 1142       }
1143     else if (this.task=='addressbook')
1144       {
1145       this.enable_command('edit', /*'print',*/ selected);
1146       this.enable_command('delete', 'compose', this.selection.length>0 ? true : false);
1147       }
1148     };
1149
1150
1151   this.clear_selection = function()
1152     {
1153     for(var n=0; n<this.selection.length; n++)
1154       if (this.list_rows[this.selection[n]])
1155         this.set_classname(this.list_rows[this.selection[n]].obj, 'selected', false);
1156
1157     this.selection = new Array();    
1158     };
1159
1160
1161   // check if given id is part of the current selection
1162   this.in_selection = function(id)
1163     {
1164     for(var n in this.selection)
1165       if (this.selection[n]==id)
1166         return true;
1167
1168     return false;    
1169     };
1170
1171
1172   // select each row in list
1173   this.select_all = function(filter)
1174     {
1175     if (!this.list_rows || !this.list_rows.length)
1176       return false;
1177       
1178     // reset selection first
1179     this.clear_selection();
1180     
1181     for (var n in this.list_rows)
1182       if (!filter || this.list_rows[n][filter]==true)
1183       this.select(n, true);
1184     };
1185     
1186
1187   // when user doble-clicks on a row
1188   this.show_message = function(id, safe)
1189     {
1190     var add_url = '';
1191     var target = window;
1192     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1193       {
1194       target = window.frames[this.env.contentframe];
1195       add_url = '&_framed=1';
1196       }
1197       
1198     if (safe)
1199       add_url = '&_safe=1';
1200
1201     if (id)
1202       {
09941e 1203       this.set_busy(true, 'loading');
4e17e6 1204       target.location.href = this.env.comm_path+'&_action=show&_uid='+id+'&_mbox='+escape(this.env.mailbox)+add_url;
T 1205       }
1206     };
1207
1208
1209
1210   // list a specific page
1211   this.list_page = function(page)
1212     {
1213     if (page=='next')
1214       page = this.env.current_page+1;
1215     if (page=='prev' && this.env.current_page>1)
1216       page = this.env.current_page-1;
1217       
1218     if (page > 0 && page <= this.env.pagecount)
1219       {
1220       this.env.current_page = page;
1221       
1222       if (this.task=='mail')
1223         this.list_mailbox(this.env.mailbox, page);
1224       else if (this.task=='addressbook')
1225         this.list_contacts(page);
1226       }
1227     };
1228
1229
1230   // list messages of a specific mailbox
f3b659 1231   this.list_mailbox = function(mbox, page, sort)
4e17e6 1232     {
T 1233     var add_url = '';
1234     var target = window;
1235
1236     if (!mbox)
1237       mbox = this.env.mailbox;
1238
f3b659 1239     // add sort to url if set
T 1240     if (sort)
1241       add_url += '&_sort=' + sort;
1242       
4e17e6 1243     // set page=1 if changeing to another mailbox
T 1244     if (!page && mbox != this.env.mailbox)
1245       {
1246       page = 1;
9fee0e 1247       add_url += '&_refresh=1';
4e17e6 1248       this.env.current_page = page;
T 1249       this.clear_selection();
1250       }
1251       
1252     if (this.env.mailbox!=mbox)
1253       this.select_mailbox(mbox);
1254
1255     // load message list remotely
1256     if (this.gui_objects.messagelist)
1257       {
9fee0e 1258       this.list_mailbox_remote(mbox, page, add_url);
4e17e6 1259       return;
T 1260       }
1261     
1262     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1263       {
1264       target = window.frames[this.env.contentframe];
9fee0e 1265       add_url += '&_framed=1';
4e17e6 1266       }
T 1267
1268     // load message list to target frame/window
1269     if (mbox)
1270       {
1271       this.set_busy(true, 'loading');
1272       target.location.href = this.env.comm_path+'&_mbox='+escape(mbox)+(page ? '&_page='+page : '')+add_url;
1273       }
1274     };
1275
1276
1277   // send remote request to load message list
9fee0e 1278   this.list_mailbox_remote = function(mbox, page, add_url)
4e17e6 1279     {
15a9d1 1280     // clear message list first
T 1281     this.clear_message_list();
1282
1283     // send request to server
1284     var url = '_mbox='+escape(mbox)+(page ? '&_page='+page : '');
1285     this.set_busy(true, 'loading');
1286     this.http_request('list', url+add_url, true);
1287     };
1288
1289
1290   this.clear_message_list = function()
1291     {
4e17e6 1292     var table = this.gui_objects.messagelist;
T 1293     var tbody = document.createElement('TBODY');
1294     table.insertBefore(tbody, table.tBodies[0]);
1295     table.removeChild(table.tBodies[1]);
1296     
1297     this.message_rows = new Array();
1298     this.list_rows = this.message_rows;
15a9d1 1299     
T 1300     };
1301
1302
1303   this.expunge_mailbox = function(mbox)
1304     {
1305     var lock = false;
1306     var add_url = '';
1307     
1308     // lock interface if it's the active mailbox
1309     if (mbox == this.env.mailbox)
1310        {
1311        lock = true;
1312        this.set_busy(true, 'loading');
1313        add_url = '&_reload=1';
1314        }
4e17e6 1315
T 1316     // send request to server
15a9d1 1317     var url = '_mbox='+escape(mbox);
T 1318     this.http_request('expunge', url+add_url, lock);
4e17e6 1319     };
T 1320
1321
1322   // move selected messages to the specified mailbox
1323   this.move_messages = function(mbox)
1324     {
1325     // exit if no mailbox specified or if selection is empty
1326     if (!mbox || !(this.selection.length || this.env.uid) || mbox==this.env.mailbox)
1327       return;
1328     
1329     var a_uids = new Array();
1330
1331     if (this.env.uid)
1332       a_uids[a_uids.length] = this.env.uid;
1333     else
1334       {
1335       var id;
1336       for (var n=0; n<this.selection.length; n++)
1337         {
1338         id = this.selection[n];
1339         a_uids[a_uids.length] = id;
1340       
1341         // 'remove' message row from list (just hide it)
1342         if (this.message_rows[id].obj)
1343           this.message_rows[id].obj.style.display = 'none';
1344         }
1345       }
ecf759 1346       
T 1347     var lock = false;
4e17e6 1348
T 1349     // show wait message
1350     if (this.env.action=='show')
ecf759 1351       {
T 1352       lock = true;
4e17e6 1353       this.set_busy(true, 'movingmessage');
ecf759 1354       }
4e17e6 1355
T 1356     // send request to server
ecf759 1357     this.http_request('moveto', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_target_mbox='+escape(mbox)+'&_from='+(this.env.action ? this.env.action : ''), lock);
4e17e6 1358     };
T 1359
1360
1361   // delete selected messages from the current mailbox
1362   this.delete_messages = function()
1363     {
1364     // exit if no mailbox specified or if selection is empty
1365     if (!(this.selection.length || this.env.uid))
1366       return;
1367     
1368     var a_uids = new Array();
1369
1370     if (this.env.uid)
1371       a_uids[a_uids.length] = this.env.uid;
1372     else
1373       {
1374       var id;
1375       for (var n=0; n<this.selection.length; n++)
1376         {
1377         id = this.selection[n];
1378         a_uids[a_uids.length] = id;
1379       
1380         // 'remove' message row from list (just hide it)
1381         if (this.message_rows[id].obj)
1382           this.message_rows[id].obj.style.display = 'none';
1383         }
1384       }
1385
1386     // send request to server
1387     this.http_request('delete', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_from='+(this.env.action ? this.env.action : ''));
1388     };
1389
1390
1391   // set a specific flag to one or more messages
1392   this.mark_message = function(flag, uid)
1393     {
1394     var a_uids = new Array();
1395     
1396     if (uid)
1397       a_uids[0] = uid;
1398     else if (this.env.uid)
1399       a_uids[0] = this.env.uid;
1400     else
1401       {
1402       var id;
1403       for (var n=0; n<this.selection.length; n++)
1404         {
1405         id = this.selection[n];
1406         a_uids[a_uids.length] = id;
1407       
1408         // 'remove' message row from list (just hide it)
1409         if (this.message_rows[id].obj)
1410           this.message_rows[id].obj.style.display = 'none';
1411         }
1412       }
1413
1414     // mark all message rows as read/unread
1415     var icn_src;
1416     for (var i=0; i<a_uids.length; i++)
1417       {
1418       uid = a_uids[i];
1419       if (this.message_rows[uid])
1420         {
1421         this.message_rows[uid].unread = (flag=='unread' ? true : false);
1422         
1423         if (this.message_rows[uid].classname.indexOf('unread')<0 && this.message_rows[uid].unread)
1424           {
1425           this.message_rows[uid].classname += ' unread';
fd660a 1426           this.set_classname(this.message_rows[uid].obj, 'unread', true);
T 1427
4e17e6 1428           if (this.env.unreadicon)
T 1429             icn_src = this.env.unreadicon;
1430           }
1431         else if (!this.message_rows[uid].unread)
1432           {
1433           this.message_rows[uid].classname = this.message_rows[uid].classname.replace(/\s*unread/, '');
fd660a 1434           this.set_classname(this.message_rows[uid].obj, 'unread', false);
4e17e6 1435
T 1436           if (this.message_rows[uid].replied && this.env.repliedicon)
1437             icn_src = this.env.repliedicon;
1438           else if (this.env.messageicon)
1439             icn_src = this.env.messageicon;
1440           }
1441
1442         if (this.message_rows[uid].icon && icn_src)
1443           this.message_rows[uid].icon.src = icn_src;
1444         }
1445       }
1446
1447     // send request to server
1448     this.http_request('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1449     };
1450
1451
1452
1453   /*********************************************************/
1454   /*********        message compose methods        *********/
1455   /*********************************************************/
1cded8 1456   
T 1457   
977a29 1458   // checks the input fields before sending a message
T 1459   this.check_compose_input = function()
1460     {
1461     // check input fields
1462     var input_to = rcube_find_object('_to');
1463     var input_subject = rcube_find_object('_subject');
1464     var input_message = rcube_find_object('_message');
1465
1466     // check for empty recipient
1467     if (input_to && !rcube_check_email(input_to.value, true))
1468       {
1469       alert(this.get_label('norecipientwarning'));
1470       input_to.focus();
1471       return false;
1472       }
1473
1474     // display localized warning for missing subject
1475     if (input_subject && input_subject.value == '')
1476       {
1477       var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1478
1479       // user hit cancel, so don't send
1480       if (!subject && subject !== '')
1481         {
1482         input_subject.focus();
1483         return false;
1484         }
1485       else
1486         {
1487         input_subject.value = subject ? subject : this.get_label('nosubject');            
1488         }
1489       }
1490
1491     // check for empty body
1492     if (input_message.value=='')
1493       {
1494       if (!confirm(this.get_label('nobodywarning')))
1495         {
1496         input_message.focus();
1497         return false;
1498         }
1499       }
1500
1501     return true;
1502     };
1503     
1504     
1505   this.compose_field_hash = function()
1506     {
1507     // check input fields
1508     var input_to = rcube_find_object('_to');
1509     var input_cc = rcube_find_object('_to');
1510     var input_bcc = rcube_find_object('_to');
1511     var input_subject = rcube_find_object('_subject');
1512     var input_message = rcube_find_object('_message');
1513     
1514     var str = '';
1515     if (input_to && input_to.value)
1516       str += input_to.value+':';
1517     if (input_cc && input_cc.value)
1518       str += input_cc.value+':';
1519     if (input_bcc && input_bcc.value)
1520       str += input_bcc.value+':';
1521     if (input_subject && input_subject.value)
1522       str += input_subject.value+':';
1523     if (input_message && input_message.value)
1524       str += input_message.value;
1525
1526     return str;
1527     };
1528     
1529   
1cded8 1530   this.change_identity = function(obj)
T 1531     {
1532     if (!obj || !obj.options)
1533       return false;
1534
1535     var id = obj.options[obj.selectedIndex].value;
1536     var input_message = rcube_find_object('_message');
1537     var message = input_message ? input_message.value : '';
749b07 1538     var sig, p;
1cded8 1539
T 1540     // remove the 'old' signature
1541     if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
1542       {
749b07 1543       sig = this.env.signatures[this.env.identity];
T 1544       if (sig.indexOf('-- ')!=0)
1545         sig = '-- \n'+sig;
1546
1547       p = message.lastIndexOf(sig);
1548       if (p>=0)
1cded8 1549         message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
T 1550       }
1551
1552     // add the new signature string
1553     if (this.env.signatures && this.env.signatures[id])
1554       {
749b07 1555       sig = this.env.signatures[id];
T 1556       if (sig.indexOf('-- ')!=0)
1557         sig = '-- \n'+sig;
1cded8 1558       message += '\n'+sig;
T 1559       }
1560
749b07 1561     if (input_message)
1cded8 1562       input_message.value = message;
T 1563       
1564     this.env.identity = id;
1565     };
4e17e6 1566
T 1567
1568   this.show_attachment_form = function(a)
1569     {
1570     if (!this.gui_objects.uploadbox)
1571       return false;
1572       
1573     var elm, list;
1574     if (elm = this.gui_objects.uploadbox)
1575       {
1576       if (a &&  (list = this.gui_objects.attachmentlist))
1577         {
1578         var pos = rcube_get_object_pos(list);
1579         var left = pos.x;
1580         var top = pos.y + list.offsetHeight + 10;
1581       
1582         elm.style.top = top+'px';
1583         elm.style.left = left+'px';
1584         }
1585       
1586       elm.style.visibility = a ? 'visible' : 'hidden';
1587       }
1588       
1589     // clear upload form
1590     if (!a && this.gui_objects.attachmentform && this.gui_objects.attachmentform!=this.gui_objects.messageform)
1591       this.gui_objects.attachmentform.reset();
1592     };
1593
1594
1595   // upload attachment file
1596   this.upload_file = function(form)
1597     {
1598     if (!form)
1599       return false;
1600       
1601     // get file input fields
1602     var send = false;
1603     for (var n=0; n<form.elements.length; n++)
1604       if (form.elements[n].type=='file' && form.elements[n].value)
1605         {
1606         send = true;
1607         break;
1608         }
1609     
1610     // create hidden iframe and post upload form
1611     if (send)
1612       {
1613       var ts = new Date().getTime();
1614       var frame_name = 'rcmupload'+ts;
1615
1616       // have to do it this way for IE
1617       // otherwise the form will be posted to a new window
1618       if(document.all && !window.opera)
1619         {
1620         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1621         document.body.insertAdjacentHTML('BeforeEnd',html);
1622         }
1623       else  // for standards-compilant browsers
1624         {
1625         var frame = document.createElement('IFRAME');
1626         frame.name = frame_name;
1627         frame.width = 10;
1628         frame.height = 10;
1629         frame.style.visibility = 'hidden';
1630         document.body.appendChild(frame);
1631         }
1632
1633       form.target = frame_name;
1634       form.action = this.env.comm_path+'&_action=upload';
1635       form.setAttribute('enctype', 'multipart/form-data');
1636       form.submit();
1637       }
1638     
1639     // set reference to the form object
1640     this.gui_objects.attachmentform = form;
1641     };
1642
1643
1644   // add file name to attachment list
1645   // called from upload page
1646   this.add2attachment_list = function(name)
1647     {
1648     if (!this.gui_objects.attachmentlist)
1649       return false;
1650       
1651     var li = document.createElement('LI');
1652     li.innerHTML = name;
1653     this.gui_objects.attachmentlist.appendChild(li);
1654     };
1655
1656
1657   // send remote request to add a new contact
1658   this.add_contact = function(value)
1659     {
1660     if (value)
1661       this.http_request('addcontact', '_address='+value);
1662     };
1663
1664
1665   /*********************************************************/
1666   /*********     keyboard live-search methods      *********/
1667   /*********************************************************/
1668
1669
1670   // handler for keyboard events on address-fields
1671   this.ksearch_keypress = function(e, obj)
1672     {
1673     if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
1674       return true;
1675
1676     if (this.ksearch_timer)
1677       clearTimeout(this.ksearch_timer);
1678
1679     if (!e)
1680       e = window.event;
1681       
1682     var highlight;
1683     var key = e.keyCode ? e.keyCode : e.which;
1684
1685     switch (key)
1686       {
1687       case 38:  // key up
1688       case 40:  // key down
1689         if (!this.ksearch_pane)
1690           break;
1691           
1692         var dir = key==38 ? 1 : 0;
1693         var next;
1694         
1695         highlight = document.getElementById('rcmksearchSelected');
1696         if (!highlight)
1697           highlight = this.ksearch_pane.ul.firstChild;
1698         
1699         if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
1700           {
1701           highlight.removeAttribute('id');
1702           //highlight.removeAttribute('class');
1703           this.set_classname(highlight, 'selected', false);
1704           }
1705
1706         if (next)
1707           {
1708           next.setAttribute('id', 'rcmksearchSelected');
1709           this.set_classname(next, 'selected', true);
1710           this.ksearch_selected = next._rcm_id;
1711           }
1712
1713         if (e.preventDefault)
1714           e.preventDefault();
1715         return false;
1716
1717       case 9:  // tab
1718         if(e.shiftKey)
1719           break;
1720
1721       case 13:  // enter     
1722         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
1723           break;
1724
1725         // get cursor pos
1726         var inp_value = this.ksearch_input.value.toLowerCase();
1727         var cpos = this.get_caret_pos(this.ksearch_input);
1728         var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
1729         
1730         // replace search string with full address
1731         var pre = this.ksearch_input.value.substring(0, p);
1732         var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
1733         var insert = this.env.contacts[this.ksearch_selected]+', ';
1734         this.ksearch_input.value = pre + insert + end;
1735         
1736         //this.ksearch_input.value = this.ksearch_input.value.substring(0, p)+insert;
1737         
1738         // set caret to insert pos
1739         cpos = p+insert.length;
1740         if (this.ksearch_input.setSelectionRange)
1741           this.ksearch_input.setSelectionRange(cpos, cpos);
1742         
1743         // hide ksearch pane
1744         this.ksearch_hide();
1745       
1746         if (e.preventDefault)
1747           e.preventDefault();
1748         return false;
1749
1750       case 27:  // escape
1751         this.ksearch_hide();
1752         break;
1753
1754       }
1755
1756     // start timer
1757     this.ksearch_timer = setTimeout(this.ref+'.ksearch_get_results()', 200);      
1758     this.ksearch_input = obj;
1759     
1760     return true;
1761     };
1762
1763
1764   // address search processor
1765   this.ksearch_get_results = function()
1766     {
1767     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
1768     if (inp_value===null)
1769       return;
1770
1771     // get string from current cursor pos to last comma
1772     var cpos = this.get_caret_pos(this.ksearch_input);
1773     var p = inp_value.lastIndexOf(',', cpos-1);
1774     var q = inp_value.substring(p+1, cpos);
1775
1776     // trim query string
1777     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
1778
1779     if (!q.length || q==this.ksearch_value)
1780       {
1781       if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
1782         this.ksearch_pane.show(0);
1783
1784       return;
1785       }
1786
1787     this.ksearch_value = q;
1788     
1789     // start searching the contact list
1790     var a_results = new Array();
1791     var a_result_ids = new Array();
1792     var c=0;
1793     for (var i=0; i<this.env.contacts.length; i++)
1794       {
1795       if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
1796         {
1797         a_results[c] = this.env.contacts[i];
1798         a_result_ids[c++] = i;
1799         
1800         if (c==15)  // limit search results
1801           break;
1802         }
1803       }
1804
1805     // display search results
1806     if (c && a_results.length)
1807       {
1808       var p, ul, li;
1809       
1810       // create results pane if not present
1811       if (!this.ksearch_pane)
1812         {
1813         ul = document.createElement('UL');
1814         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
1815         this.ksearch_pane.elm.appendChild(ul);
1816         this.ksearch_pane.ul = ul;
1817         }
1818       else
1819         ul = this.ksearch_pane.ul;
1820
1821       // remove all search results
1822       ul.innerHTML = '';
1823             
1824       // add each result line to list
1825       for (i=0; i<a_results.length; i++)
1826         {
1827         li = document.createElement('LI');
1828         li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
1829         li._rcm_id = a_result_ids[i];
1830         ul.appendChild(li);
1831         }
1832
1833       // check if last selected item is still in result list
1834       if (this.ksearch_selected!==null)
1835         {
1836         p = find_in_array(this.ksearch_selected, a_result_ids);
1837         if (p>=0 && ul.childNodes)
1838           {
1839           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
1840           this.set_classname(ul.childNodes[p], 'selected', true);
1841           }
1842         else
1843           this.ksearch_selected = null;
1844         }
1845       
1846       // if no item selected, select the first one
1847       if (this.ksearch_selected===null)
1848         {
1849         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
1850         this.set_classname(ul.firstChild, 'selected', true);
1851         this.ksearch_selected = a_result_ids[0];
1852         }
1853
1854       // resize the containing layer to fit the list
1855       //this.ksearch_pane.resize(ul.offsetWidth, ul.offsetHeight);
1856     
1857       // move the results pane right under the input box and make it visible
1858       var pos = rcube_get_object_pos(this.ksearch_input);
1859       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
1860       this.ksearch_pane.show(1); 
1861       }
1862     // hide results pane
1863     else
1864       this.ksearch_hide();
1865     };
1866
1867
1868   this.ksearch_blur = function(e, obj)
1869     {
1870     if (this.ksearch_timer)
1871       clearTimeout(this.ksearch_timer);
1872
1873     this.ksearch_value = '';      
1874     this.ksearch_input = null;
1875     
1876     this.ksearch_hide();
1877     };
1878
1879
1880   this.ksearch_hide = function()
1881     {
1882     this.ksearch_selected = null;
1883     
1884     if (this.ksearch_pane)
1885       this.ksearch_pane.show(0);    
1886     };
1887
1888
1889
1890   /*********************************************************/
1891   /*********         address book methods          *********/
1892   /*********************************************************/
1893
1894
1895   this.list_contacts = function(page)
1896     {
1897     var add_url = '';
1898     var target = window;
1899     
1900     if (page && this.current_page==page)
1901       return false;
1902
1903     // load contacts remotely
1904     if (this.gui_objects.contactslist)
1905       {
1906       this.list_contacts_remote(page);
1907       return;
1908       }
1909
1910     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1911       {
1912       target = window.frames[this.env.contentframe];
1913       add_url = '&_framed=1';
1914       }
1915
1916     this.set_busy(true, 'loading');
1917     location.href = this.env.comm_path+(page ? '&_page='+page : '')+add_url;
1918     };
1919
1920
1921   // send remote request to load contacts list
1922   this.list_contacts_remote = function(page)
1923     {
1924     // clear list
1925     var table = this.gui_objects.contactslist;
1926     var tbody = document.createElement('TBODY');
1927     table.insertBefore(tbody, table.tBodies[0]);
1928     table.tBodies[1].style.display = 'none';
1929     
1930     this.contact_rows = new Array();
1931     this.list_rows = this.contact_rows;
1932
1933     // send request to server
1934     var url = page ? '&_page='+page : '';
1935     this.set_busy(true, 'loading');
ecf759 1936     this.http_request('list', url, true);
4e17e6 1937     };
T 1938
1939
1940   // load contact record
1941   this.load_contact = function(cid, action, framed)
1942     {
1943     var add_url = '';
1944     var target = window;
1945     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1946       {
1947       add_url = '&_framed=1';
1948       target = window.frames[this.env.contentframe];
1949       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
1950       }
1951     else if (framed)
1952       return false;
1953       
1954     //if (this.env.framed && add_url=='')
1955     //  add_url = '&_framed=1';
1956     
1957     if (action && (cid || action=='add'))
1958       {
1959       this.set_busy(true);
1960       target.location.href = this.env.comm_path+'&_action='+action+'&_cid='+cid+add_url;
1961       }
1962     };
1963
1964
1965   this.delete_contacts = function()
1966     {
1967     // exit if no mailbox specified or if selection is empty
1968     if (!(this.selection.length || this.env.cid))
1969       return;
1970     
1971     var a_cids = new Array();
1972
1973     if (this.env.cid)
1974       a_cids[a_cids.length] = this.env.cid;
1975     else
1976       {
1977       var id;
1978       for (var n=0; n<this.selection.length; n++)
1979         {
1980         id = this.selection[n];
1981         a_cids[a_cids.length] = id;
1982       
1983         // 'remove' row from list (just hide it)
1984         if (this.contact_rows[id].obj)
1985           this.contact_rows[id].obj.style.display = 'none';
1986         }
1987
1988       // hide content frame if we delete the currently displayed contact
1989       if (this.selection.length==1 && this.env.contentframe)
1990         {
1991         var elm = document.getElementById(this.env.contentframe);
1992         elm.style.visibility = 'hidden';
1993         }
1994       }
1995
1996     // send request to server
1997     this.http_request('delete', '_cid='+a_cids.join(',')+'&_from='+(this.env.action ? this.env.action : ''));
1998     };
1999
2000
2001   // update a contact record in the list
2002   this.update_contact_row = function(cid, cols_arr)
2003     {
2004     if (!this.contact_rows[cid] || !this.contact_rows[cid].obj)
2005       return false;
2006       
2007     var row = this.contact_rows[cid].obj;
2008     for (var c=0; c<cols_arr.length; c++)
2009       if (row.cells[c])
2010         row.cells[c].innerHTML = cols_arr[c];
2011
2012     };
2013   
d1d2c4 2014   
S 2015   // load ldap search form
2016   this.ldappublicsearch = function(action)
2017     {
2018     var add_url = '';
2019     var target = window;
2020     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2021       {
2022       add_url = '&_framed=1';
2023       target = window.frames[this.env.contentframe];
2024       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2025       }
2026     else
2027       return false; 
2028
2029
2030     if (action == 'ldappublicsearch')
2031       target.location.href = this.env.comm_path+'&_action='+action+add_url;
2032     };
2033  
2034   // add ldap contacts to address book
2035   this.add_ldap_contacts = function()
2036     {
2037     if (window.frames[this.env.contentframe].rcmail)
2038       {
2039       var frame = window.frames[this.env.contentframe];
2040
2041       // build the url
2042       var url    = '&_framed=1';
2043       var emails = '&_emails=';
2044       var names  = '&_names=';
2045       var end    = '';
2046       for (var n=0; n<frame.rcmail.selection.length; n++)
2047         {
2048         end = n < frame.rcmail.selection.length - 1 ? ',' : '';
2049         emails += frame.rcmail.ldap_contact_rows[frame.rcmail.selection[n]].obj.cells[1].innerHTML + end;
2050         names  += frame.rcmail.ldap_contact_rows[frame.rcmail.selection[n]].obj.cells[0].innerHTML + end;
2051         }
2052        
2053       frame.location.href = this.env.comm_path + '&_action=save&_framed=1' + emails + names;
2054       }
2055     return false;
2056     }
2057   
4e17e6 2058
T 2059
2060   /*********************************************************/
2061   /*********        user settings methods          *********/
2062   /*********************************************************/
2063
2064
2065   // load contact record
2066   this.load_identity = function(id, action)
2067     {
2068     if (action=='edit-identity' && (!id || id==this.env.iid))
2069       return;
2070
2071     var add_url = '';
2072     var target = window;
2073     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2074       {
2075       add_url = '&_framed=1';
2076       target = window.frames[this.env.contentframe];
2077       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2078       }
2079
2080     if (action && (id || action=='add-identity'))
2081       {
2082       this.set_busy(true);
2083       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2084       }
2085     };
2086
2087
2088
2089   this.delete_identity = function(id)
2090     {
2091     // exit if no mailbox specified or if selection is empty
2092     if (!(this.selection.length || this.env.iid))
2093       return;
2094     
2095     if (!id)
2096       id = this.env.iid ? this.env.iid : this.selection[0];
2097
2098 /*
2099     // 'remove' row from list (just hide it)
2100     if (this.identity_rows && this.identity_rows[id].obj)
2101       {
2102       this.clear_selection();
2103       this.identity_rows[id].obj.style.display = 'none';
2104       }
2105 */
2106
2107     // if (this.env.framed && id)
2108       this.set_busy(true);
2109       location.href = this.env.comm_path+'&_action=delete-identity&_iid='+id;
2110     // else if (id)
2111     //  this.http_request('delete-identity', '_iid='+id);
2112     };
2113
2114
2115   this.create_folder = function(name)
2116     {
2117     var form;
2118     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2119       name = form.elements['_folder_name'].value;
2120
2121     if (name)
ecf759 2122       this.http_request('create-folder', '_name='+escape(name), true);
4e17e6 2123     else if (form.elements['_folder_name'])
T 2124       form.elements['_folder_name'].focus();
2125     };
2126
2127
2128   this.delete_folder = function(folder)
2129     {
2130     if (folder)
2131       {
2132       this.http_request('delete-folder', '_mboxes='+escape(folder));
2133       }
1cded8 2134     };
T 2135
2136
2137   this.remove_folder_row = function(folder)
2138     {
2139     for (var id in this.env.subscriptionrows)
2140       if (this.env.subscriptionrows[id]==folder)
2141         break;
2142
2143     var row;
2144     if (id && (row = document.getElementById(id)))
2145       row.style.display = 'none';    
4e17e6 2146     };
T 2147
2148
2149   this.subscribe_folder = function(folder)
2150     {
2151     var form;
2152     if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
2153       this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
2154     else if (folder)
2155       this.http_request('subscribe', '_mboxes='+escape(folder));
2156     };
2157
2158
2159   this.unsubscribe_folder = function(folder)
2160     {
2161     var form;
2162     if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
2163       this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
2164     else if (folder)
2165       this.http_request('unsubscribe', '_mboxes='+escape(folder));
2166     };
2167     
2168
2169   this.change_subscription = function(from, to, action)
2170     {
2171     var form;
2172     if (form = this.gui_objects.editform)
2173       {
2174       var a_folders = new Array();
2175       var list_from = form.elements[from];
2176
2177       for (var i=0; list_from && i<list_from.options.length; i++)
2178         {
2179         if (list_from.options[i] && list_from.options[i].selected)
2180           {
2181           a_folders[a_folders.length] = list_from.options[i].value;
2182           list_from[i] = null;
2183           i--;
2184           }
2185         }
2186
2187       // yes, we have some folders selected
2188       if (a_folders.length)
2189         {
2190         var list_to = form.elements[to];
2191         var index;
2192         
2193         for (var n=0; n<a_folders.length; n++)
2194           {
2195           index = list_to.options.length;
2196           list_to[index] = new Option(a_folders[n]);
2197           }
2198           
2199         this.http_request(action, '_mboxes='+escape(a_folders.join(',')));
2200         }
2201       }
2202       
2203     };
2204
2205
2206    // add a new folder to the subscription list by cloning a folder row
2207    this.add_folder_row = function(name)
2208      {
2209      if (!this.gui_objects.subscriptionlist)
2210        return false;
2211
2212      var tbody = this.gui_objects.subscriptionlist.tBodies[0];
2213      var id = tbody.childNodes.length+1;
2214      
2215      // clone a table row
2216      var row = this.clone_table_row(tbody.rows[0]);
2217      row.id = 'rcmrow'+id;
2218      tbody.appendChild(row);
2219
2220      // add to folder/row-ID map
2221      this.env.subscriptionrows[row.id] = name;
2222
2223      // set folder name
2224      row.cells[0].innerHTML = name;
2225      if (row.cells[1].firstChild.tagName=='INPUT')
2226        {
2227        row.cells[1].firstChild.value = name;
2228        row.cells[1].firstChild.checked = true;
2229        }
2230      if (row.cells[2].firstChild.tagName=='A')
2231        row.cells[2].firstChild.onclick = new Function(this.ref+".command('delete-folder','"+name+"')");
14eafe 2232
T 2233     var form;
2234     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
2235       form.elements['_folder_name'].value = '';
4e17e6 2236      };
T 2237
2238
2239   // duplicate a specific table row
2240   this.clone_table_row = function(row)
2241     {
2242     var cell, td;
2243     var new_row = document.createElement('TR');
2244     for(var n=0; n<row.childNodes.length; n++)
2245       {
2246       cell = row.childNodes[n];
2247       td = document.createElement('TD');
2248
2249       if (cell.className)
2250         td.className = cell.className;
2251       if (cell.align)
2252         td.setAttribute('align', cell.align);
2253         
2254       td.innerHTML = cell.innerHTML;
2255       new_row.appendChild(td);
2256       }
2257     
2258     return new_row;
2259     };
2260
2261
2262   /*********************************************************/
2263   /*********           GUI functionality           *********/
2264   /*********************************************************/
2265
2266
2267   // eable/disable buttons for page shifting
2268   this.set_page_buttons = function()
2269     {
2270     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
2271     this.enable_command('previouspage', (this.env.current_page > 1));
2272     }
2273
2274
2275   // set button to a specific state
2276   this.set_button = function(command, state)
2277     {
2278     var a_buttons = this.buttons[command];
2279     var button, obj;
2280
2281     if(!a_buttons || !a_buttons.length)
2282       return;
2283
2284     for(var n=0; n<a_buttons.length; n++)
2285       {
2286       button = a_buttons[n];
2287       obj = document.getElementById(button.id);
2288
2289       // get default/passive setting of the button
2290       if (obj && button.type=='image' && !button.status)
2291         button.pas = obj._original_src ? obj._original_src : obj.src;
2292       else if (obj && !button.status)
2293         button.pas = String(obj.className);
2294
2295       // set image according to button state
2296       if (obj && button.type=='image' && button[state])
2297         {
2298         button.status = state;        
2299         obj.src = button[state];
2300         }
2301       // set class name according to button state
2302       else if (obj && typeof(button[state])!='undefined')
2303         {
2304         button.status = state;        
2305         obj.className = button[state];        
2306         }
2307       // disable/enable input buttons
2308       if (obj && button.type=='input')
2309         {
2310         button.status = state;
2311         obj.disabled = !state;
2312         }
2313       }
2314     };
2315
2316
2317   // mouse over button
2318   this.button_over = function(command, id)
2319     {
2320     var a_buttons = this.buttons[command];
2321     var button, img;
2322
2323     if(!a_buttons || !a_buttons.length)
2324       return;
2325
2326     for(var n=0; n<a_buttons.length; n++)
2327       {
2328       button = a_buttons[n];
2329       if(button.id==id && button.status=='act')
2330         {
2331         img = document.getElementById(button.id);
2332         if (img && button.over)
2333           img.src = button.over;
2334         }
2335       }
2336     };
2337
2338
2339   // mouse out of button
2340   this.button_out = function(command, id)
2341     {
2342     var a_buttons = this.buttons[command];
2343     var button, img;
2344
2345     if(!a_buttons || !a_buttons.length)
2346       return;
2347
2348     for(var n=0; n<a_buttons.length; n++)
2349       {
2350       button = a_buttons[n];
2351       if(button.id==id && button.status=='act')
2352         {
2353         img = document.getElementById(button.id);
2354         if (img && button.act)
2355           img.src = button.act;
2356         }
2357       }
2358     };
2359
2360
2361   // set/unset a specific class name
2362   this.set_classname = function(obj, classname, set)
2363     {
2364     var reg = new RegExp('\s*'+classname, 'i');
2365     if (!set && obj.className.match(reg))
2366       obj.className = obj.className.replace(reg, '');
2367     else if (set && !obj.className.match(reg))
2368       obj.className += ' '+classname;
2369     };
2370
2371
2372   // display a specific alttext
2373   this.alttext = function(text)
2374     {
2375     
2376     };
2377
2378
2379   // display a system message
2380   this.display_message = function(msg, type, hold)
2381     {
2382     if (!this.loaded)  // save message in order to display after page loaded
2383       {
2384       this.pending_message = new Array(msg, type);
2385       return true;
2386       }
2387     
2388     if (!this.gui_objects.message)
2389       return false;
2390       
2391     if (this.message_timer)
2392       clearTimeout(this.message_timer);
2393     
2394     var cont = msg;
2395     if (type)
2396       cont = '<div class="'+type+'">'+cont+'</div>';
a95e0e 2397
T 2398     this.gui_objects.message._rcube = this;
4e17e6 2399     this.gui_objects.message.innerHTML = cont;
T 2400     this.gui_objects.message.style.display = 'block';
a95e0e 2401     
T 2402     if (type!='loading')
2403       this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
4e17e6 2404     
T 2405     if (!hold)
2406       this.message_timer = setTimeout(this.ref+'.hide_message()', this.message_time);
2407     };
2408
2409
2410   // make a message row disapear
2411   this.hide_message = function()
2412     {
2413     if (this.gui_objects.message)
a95e0e 2414       {
4e17e6 2415       this.gui_objects.message.style.display = 'none';
a95e0e 2416       this.gui_objects.message.onmousedown = null;
T 2417       }
4e17e6 2418     };
T 2419
2420
2421   // mark a mailbox as selected and set environment variable
2422   this.select_mailbox = function(mbox)
2423     {
2424     if (this.gui_objects.mailboxlist)
2425       {
2426       var item, reg, text_obj;
6a35c8 2427       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
4e17e6 2428       var s_mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2429       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
597170 2430       
6a35c8 2431       var current_li = document.getElementById('rcmbx'+s_current);
T 2432       var mbox_li = document.getElementById('rcmbx'+s_mbox);
2433       
2434       if (current_li)
2435         this.set_classname(current_li, 'selected', false);
2436       if (mbox_li)
2437         this.set_classname(mbox_li, 'selected', true);
4e17e6 2438       }
T 2439     
2440     this.env.mailbox = mbox;
2441     };
2442
2443
2444   // create a table row in the message list
15a9d1 2445   this.add_message_row = function(uid, cols, flags, attachment, attop)
4e17e6 2446     {
T 2447     if (!this.gui_objects.messagelist || !this.gui_objects.messagelist.tBodies[0])
2448       return false;
2449     
2450     var tbody = this.gui_objects.messagelist.tBodies[0];
2451     var rowcount = tbody.rows.length;
2452     var even = rowcount%2;
2453     
2454     this.env.messages[uid] = {replied:flags.replied?1:0,
2455                               unread:flags.unread?1:0};
2456     
2457     var row = document.createElement('TR');
2458     row.id = 'rcmrow'+uid;
15a9d1 2459     row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '')+(flags.deleted ? ' deleted' : '');
T 2460     
4e17e6 2461     if (this.in_selection(uid))
T 2462       row.className += ' selected';
2463
2464     var icon = flags.unread && this.env.unreadicon ? this.env.unreadicon :
2465                (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon);
2466
2467     var col = document.createElement('TD');
2468     col.className = 'icon';
2469     col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
2470     row.appendChild(col);
2471
2472     // add each submitted col
2473     for (var c in cols)
2474       {
2475       col = document.createElement('TD');
2476       col.className = String(c).toLowerCase();
2477       col.innerHTML = cols[c];
2478       row.appendChild(col);
2479       }
2480
2481     col = document.createElement('TD');
2482     col.className = 'icon';
2483     col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
2484     row.appendChild(col);
2485     
15a9d1 2486     if (attop && tbody.rows.length)
T 2487       tbody.insertBefore(row, tbody.firstChild);
2488     else
2489       tbody.appendChild(row);
2490       
4e17e6 2491     this.init_message_row(row);
T 2492     };
2493
2494
2495   // replace content of row count display
2496   this.set_rowcount = function(text)
2497     {
2498     if (this.gui_objects.countdisplay)
2499       this.gui_objects.countdisplay.innerHTML = text;
2500
2501     // update page navigation buttons
2502     this.set_page_buttons();
2503     };
2504
2505
2506   // update the mailboxlist
15a9d1 2507   this.set_unread_count = function(mbox, count, set_title)
4e17e6 2508     {
T 2509     if (!this.gui_objects.mailboxlist)
2510       return false;
2511       
2512     var item, reg, text_obj;
15a9d1 2513     mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2514     item = document.getElementById('rcmbx'+mbox);
2515
2516     if (item && item.className && item.className.indexOf('mailbox '+mbox)>=0)
4e17e6 2517       {
15a9d1 2518       // set new text
T 2519       text_obj = item.firstChild;
2520       reg = /\s+\([0-9]+\)$/i;
4e17e6 2521
15a9d1 2522       if (count && text_obj.innerHTML.match(reg))
T 2523         text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
2524       else if (count)
2525         text_obj.innerHTML += ' ('+count+')';
2526       else
2527         text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
4e17e6 2528           
15a9d1 2529       // set the right classes
T 2530       this.set_classname(item, 'unread', count>0 ? true : false);
2531       }
2532
2533     // set unread count to window title
2534     if (set_title && document.title)
2535       {
2536       var doc_title = String(document.title);
2537       reg = /^\([0-9]+\)\s+/i;
2538
2539       if (count && doc_title.match(reg))
2540         document.title = doc_title.replace(reg, '('+count+') ');
2541       else if (count)
2542         document.title = '('+count+') '+doc_title;
2543       else
2544         document.title = doc_title.replace(reg, '');
4e17e6 2545       }
T 2546     };
2547
2548
2549   // add row to contacts list
2550   this.add_contact_row = function(cid, cols)
2551     {
2552     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
2553       return false;
2554     
2555     var tbody = this.gui_objects.contactslist.tBodies[0];
2556     var rowcount = tbody.rows.length;
2557     var even = rowcount%2;
2558     
2559     var row = document.createElement('TR');
2560     row.id = 'rcmrow'+cid;
2561     row.className = 'contact '+(even ? 'even' : 'odd');
2562     
2563     if (this.in_selection(cid))
2564       row.className += ' selected';
2565
2566     // add each submitted col
2567     for (var c in cols)
2568       {
2569       col = document.createElement('TD');
2570       col.className = String(c).toLowerCase();
2571       col.innerHTML = cols[c];
2572       row.appendChild(col);
2573       }
2574     
2575     tbody.appendChild(row);
2576     this.init_table_row(row, 'contact_rows');
2577     };
2578
2579
2580
2581   /********************************************************/
2582   /*********          drag & drop methods         *********/
2583   /********************************************************/
2584
2585
2586   this.drag_mouse_move = function(e)
2587     {
2588     if (this.drag_start)
2589       {
2590       if (!this.draglayer)
2591         this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
2592       
2593       // get subjects of selectedd messages
2594       var names = '';
2595       var c, subject, obj;
2596       for(var n=0; n<this.selection.length; n++)
2597         {
2598         if (n>12)  // only show 12 lines
2599           {
2600           names += '...';
2601           break;
2602           }
2603
2604         if (this.message_rows[this.selection[n]].obj)
2605           {
2606           obj = this.message_rows[this.selection[n]].obj;
2607           subject = '';
2608
2609           for(c=0; c<obj.childNodes.length; c++)
2610             if (!subject && obj.childNodes[c].nodeName=='TD' && obj.childNodes[c].firstChild && obj.childNodes[c].firstChild.nodeType==3)
2611               {
2612               subject = obj.childNodes[c].firstChild.data;
2613               names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
2614               }
2615           }
2616         }
2617         
2618       this.draglayer.write(names);
2619       this.draglayer.show(1);
2620       }
2621
2622     var pos = this.get_mouse_pos(e);
2623     this.draglayer.move(pos.x+20, pos.y-5);
2624     
2625     this.drag_start = false;
2626     this.drag_active = true;
2627     
2628     return false;
2629     };
2630
2631
2632   this.drag_mouse_up = function()
2633     {
2634     document.onmousemove = null;
2635     
2636     if (this.draglayer && this.draglayer.visible)
2637       this.draglayer.show(0);
2638       
2639     this.drag_active = false;
2640     
2641     return false;
2642     };
2643
2644
2645
2646   /********************************************************/
2647   /*********        remote request methods        *********/
2648   /********************************************************/
2649
2650
ecf759 2651   this.http_sockets = new Array();
T 2652   
2653   // find a non-busy socket or create a new one
2654   this.get_request_obj = function()
4e17e6 2655     {
ecf759 2656     for (var n=0; n<this.http_sockets.length; n++)
4e17e6 2657       {
ecf759 2658       if (!this.http_sockets[n].busy)
T 2659         return this.http_sockets[n];
4e17e6 2660       }
ecf759 2661     
T 2662     // create a new XMLHTTP object
2663     var i = this.http_sockets.length;
2664     this.http_sockets[i] = new rcube_http_request();
4e17e6 2665
ecf759 2666     return this.http_sockets[i];
T 2667     };
2668   
2669
2670   // send a http request to the server
2671   this.http_request = function(action, querystring, lock)
2672     {
2673     var request_obj = this.get_request_obj();
4e17e6 2674     querystring += '&_remote=1';
T 2675     
2676     // add timestamp to request url to avoid cacheing problems in Safari
2677     if (bw.safari)
2678       querystring += '&_ts='+(new Date().getTime());
2679
2680     // send request
ecf759 2681     if (request_obj)
4e17e6 2682       {
T 2683       // prompt('request', this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
2684       console('HTTP request: '+this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
ecf759 2685
T 2686       if (lock)
2687         this.set_busy(true);
2688
2689       request_obj.__lock = lock ? true : false;
2690       request_obj.__action = action;
2691       request_obj.onerror = function(o){ rcube_webmail_client.http_error(o); };
2692       request_obj.oncomplete = function(o){ rcube_webmail_client.http_response(o); };
2693       request_obj.GET(this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
4e17e6 2694       }
T 2695     };
2696
2697
ecf759 2698   // handle HTTP response
T 2699   this.http_response = function(request_obj)
4e17e6 2700     {
ecf759 2701     var ctype = request_obj.get_header('Content-Type');
T 2702     if (ctype)
2703       ctype = String(ctype).toLowerCase();
4e17e6 2704
ecf759 2705     if (request_obj.__lock)
4e17e6 2706       this.set_busy(false);
T 2707
ecf759 2708   console(request_obj.responseText);
4e17e6 2709
ecf759 2710     // if we get javascript code from server -> execute it
T 2711     if (request_obj.responseText && (ctype=='text/javascript' || ctype=='application/x-javascript'))
2712       eval(request_obj.responseText);
4e17e6 2713
ecf759 2714     // process the response data according to the sent action
T 2715     switch (request_obj.__action)
2716       {
2717       case 'delete':
2718       case 'moveto':
2719         if (this.env.action=='show')
2720           this.command('list');
2721         break;
15a9d1 2722
ecf759 2723       case 'list':
15a9d1 2724       case 'expunge':
T 2725         this.enable_command('select-all', 'select-none', 'expunge', this.env.messagecount ? true : false);
ecf759 2726         break;
4e17e6 2727       }
ecf759 2728
T 2729     request_obj.reset();
4e17e6 2730     };
ecf759 2731
T 2732
2733   // handle HTTP request errors
2734   this.http_error = function(request_obj)
2735     {
2736     alert('Error sending request: '+request_obj.url);
2737
2738     if (request_obj.__lock)
2739       this.set_busy(false);
2740
2741     request_obj.reset();
2742     request_obj.__lock = false;
2743     };
2744
2745
2746   // use an image to send a keep-alive siganl to the server
2747   this.send_keep_alive = function()
2748     {
2749     var d = new Date();
2750     this.http_request('keep-alive', '_t='+d.getTime());
2751     };
15a9d1 2752
ecf759 2753     
15a9d1 2754   // send periodic request to check for recent messages
T 2755   this.check_for_recent = function()
2756     {
2757     var d = new Date();
2758     this.http_request('check-recent', '_t='+d.getTime());
2759     };
4e17e6 2760
T 2761
2762   /********************************************************/
2763   /*********            helper methods            *********/
2764   /********************************************************/
2765   
2766   // check if we're in show mode or if we have a unique selection
2767   // and return the message uid
2768   this.get_single_uid = function()
2769     {
2770     return this.env.uid ? this.env.uid : (this.selection.length==1 ? this.selection[0] : null);
2771     };
2772
2773   // same as above but for contacts
2774   this.get_single_cid = function()
2775     {
2776     return this.env.cid ? this.env.cid : (this.selection.length==1 ? this.selection[0] : null);
2777     };
2778
2779
2780   // check if Shift-key is pressed on event
2781   this.check_shiftkey = function(e)
2782     {
2783     if(!e && window.event)
2784       e = window.event;
2785
2786     if(bw.linux && bw.ns4 && e.modifiers)
2787       return true;
2788     else if((bw.ns4 && e.modifiers & Event.SHIFT_MASK) || (e && e.shiftKey))
2789       return true;
2790     else
2791       return false;
2792     }
2793
1cded8 2794   // check if Shift-key is pressed on event
T 2795   this.check_ctrlkey = function(e)
2796     {
2797     if(!e && window.event)
2798       e = window.event;
2799
2800     if(bw.linux && bw.ns4 && e.modifiers)
2801       return true;
2802    else if (bw.mac)
2803        return this.check_shiftkey(e);
2804     else if((bw.ns4 && e.modifiers & Event.CTRL_MASK) || (e && e.ctrlKey))
2805       return true;
2806     else
2807       return false;
2808     }
4e17e6 2809
T 2810   this.get_mouse_pos = function(e)
2811     {
2812     if(!e) e = window.event;
2813     var mX = (e.pageX) ? e.pageX : e.clientX;
2814     var mY = (e.pageY) ? e.pageY : e.clientY;
2815
2816     if(document.body && document.all)
2817       {
2818       mX += document.body.scrollLeft;
2819       mY += document.body.scrollTop;
2820       }
2821
2822     return { x:mX, y:mY };
2823     };
2824     
2825   
2826   this.get_caret_pos = function(obj)
2827     {
2828     if (typeof(obj.selectionEnd)!='undefined')
2829       return obj.selectionEnd;
2830
2831     else if (document.selection && document.selection.createRange)
2832       {
2833       var range = document.selection.createRange();
2834       if (range.parentElement()!=obj)
2835         return 0;
2836
2837       var gm = range.duplicate();
2838       if (obj.tagName=='TEXTAREA')
2839         gm.moveToElementText(obj);
2840       else
2841         gm.expand('textedit');
2842       
2843       gm.setEndPoint('EndToStart', range);
2844       var p = gm.text.length;
2845
2846       return p<=obj.value.length ? p : -1;
2847       }
2848
2849     else
2850       return obj.value.length;
2851     };
2852
2853
2854   this.set_caret2start = function(obj)
2855     {
2856     if (obj.createTextRange)
2857       {
2858       var range = obj.createTextRange();
2859       range.collapse(true);
2860       range.select();
2861       }
2862     else if (obj.setSelectionRange)
2863       obj.setSelectionRange(0,0);
2864
2865     obj.focus();
2866     };
2867
2868
2869   // set all fields of a form disabled
2870   this.lock_form = function(form, lock)
2871     {
2872     if (!form || !form.elements)
2873       return;
2874     
2875     var type;
2876     for (var n=0; n<form.elements.length; n++)
2877       {
2878       type = form.elements[n];
2879       if (type=='hidden')
2880         continue;
2881         
2882       form.elements[n].disabled = lock;
2883       }
2884     };
2885     
2886   }  // end object rcube_webmail
2887
2888
2889
ecf759 2890 // class for HTTP requests
T 2891 function rcube_http_request()
2892   {
2893   this.url = '';
2894   this.busy = false;
2895   this.xmlhttp = null;
2896
2897
2898   // reset object properties
2899   this.reset = function()
2900     {
2901     // set unassigned event handlers
2902     this.onloading = function(){ };
2903     this.onloaded = function(){ };
2904     this.oninteractive = function(){ };
2905     this.oncomplete = function(){ };
2906     this.onabort = function(){ };
2907     this.onerror = function(){ };
2908     
2909     this.url = '';
2910     this.busy = false;
2911     this.xmlhttp = null;
2912     }
2913
2914
2915   // create HTMLHTTP object
2916   this.build = function()
2917     {
2918     if (window.XMLHttpRequest)
2919       this.xmlhttp = new XMLHttpRequest();
2920     else if (window.ActiveXObject)
2921       this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
2922     else
2923       {
2924       
2925       }
2926     }
2927
2928   // sedn GET request
2929   this.GET = function(url)
2930     {
2931     this.build();
2932
2933     if (!this.xmlhttp)
2934       {
2935       this.onerror(this);
2936       return false;
2937       }
2938
2939     var ref = this;
2940     this.url = url;
2941     this.busy = true;
2942
2943     this.xmlhttp.onreadystatechange = function(){ ref.xmlhttp_onreadystatechange(); };
2944     this.xmlhttp.open('GET', url);
2945     this.xmlhttp.send(null);
2946     };
2947
2948
2949   this.POST = function(url, a_param)
2950     {
2951     // not implemented yet
2952     };
2953
2954
2955   // handle onreadystatechange event
2956   this.xmlhttp_onreadystatechange = function()
2957     {
2958     if(this.xmlhttp.readyState == 1)
2959       this.onloading(this);
2960
2961     else if(this.xmlhttp.readyState == 2)
2962       this.onloaded(this);
2963
2964     else if(this.xmlhttp.readyState == 3)
2965       this.oninteractive(this);
2966
2967     else if(this.xmlhttp.readyState == 4)
2968       {
2969       this.responseText = this.xmlhttp.responseText;
2970       this.responseXML = this.xmlhttp.responseXML;
2971       
2972       if(this.xmlhttp.status == 0)
2973         this.onabort(this);
2974       else if(this.xmlhttp.status == 200)
2975         this.oncomplete(this);
2976       else
2977         this.onerror(this);
2978         
2979       this.busy = false;
2980       }
2981     }
2982
2983   // getter method for HTTP headers
2984   this.get_header = function(name)
2985     {
2986     return this.xmlhttp.getResponseHeader(name);
2987     };
2988
2989
2990   this.reset();
2991   
2992   }  // end class rcube_http_request
2993
2994
4e17e6 2995
T 2996 function console(str)
2997   {
2998   if (document.debugform && document.debugform.console)
2999     document.debugform.console.value += str+'\n--------------------------------------\n';
3000   }
3001
3002
3003 // set onload handler
3004 window.onload = function(e)
3005   {
3006   if (window.rcube_webmail_client)
3007     rcube_webmail_client.init();
3008   };