]> git.donarmstrong.com Git - roundcube.git/blob - program/js/app.js
Imported Upstream version 0.3
[roundcube.git] / program / js / app.js
1 /*
2  +-----------------------------------------------------------------------+
3  | RoundCube Webmail Client Script                                       |
4  |                                                                       |
5  | This file is part of the RoundCube Webmail client                     |
6  | Copyright (C) 2005-2009, RoundCube Dev, - Switzerland                 |
7  | Licensed under the GNU GPL                                            |
8  |                                                                       |
9  +-----------------------------------------------------------------------+
10  | Authors: Thomas Bruederli <roundcube@gmail.com>                       |
11  |          Charles McNulty <charles@charlesmcnulty.com>                 |
12  +-----------------------------------------------------------------------+
13  | Requires: jquery.js, common.js, list.js                               |
14  +-----------------------------------------------------------------------+
15
16   $Id: app.js 2889 2009-08-29 18:41:17Z alec $
17 */
18
19
20 function rcube_webmail()
21 {
22   this.env = new Object();
23   this.labels = new Object();
24   this.buttons = new Object();
25   this.buttons_sel = new Object();
26   this.gui_objects = new Object();
27   this.gui_containers = new Object();
28   this.commands = new Object();
29   this.command_handlers = new Object();
30   this.onloads = new Array();
31
32   // create protected reference to myself
33   this.ref = 'rcmail';
34   var ref = this;
35  
36   // webmail client settings
37   this.dblclick_time = 500;
38   this.message_time = 3000;
39   
40   this.identifier_expr = new RegExp('[^0-9a-z\-_]', 'gi');
41   
42   // mimetypes supported by the browser (default settings)
43   this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
44                              'image/jpeg', 'image/gif', 'image/png',
45                              'application/x-javascript', 'application/pdf',
46                              'application/x-shockwave-flash');
47
48   // default environment vars
49   this.env.keep_alive = 60;        // seconds
50   this.env.request_timeout = 180;  // seconds
51   this.env.draft_autosave = 0;     // seconds
52   this.env.comm_path = './';
53   this.env.bin_path = './bin/';
54   this.env.blankpage = 'program/blank.gif';
55
56   // set jQuery ajax options
57   jQuery.ajaxSetup({ cache:false,
58     error:function(request, status, err){ ref.http_error(request, status, err); },
59     beforeSend:function(xmlhttp){ xmlhttp.setRequestHeader('X-RoundCube-Request', ref.env.request_token); }
60   });
61
62   // set environment variable(s)
63   this.set_env = function(p, value)
64     {
65     if (p != null && typeof(p) == 'object' && !value)
66       for (var n in p)
67         this.env[n] = p[n];
68     else
69       this.env[p] = value;
70     };
71
72   // add a localized label to the client environment
73   this.add_label = function(key, value)
74     {
75     this.labels[key] = value;
76     };
77
78   // add a button to the button list
79   this.register_button = function(command, id, type, act, sel, over)
80     {
81     if (!this.buttons[command])
82       this.buttons[command] = new Array();
83       
84     var button_prop = {id:id, type:type};
85     if (act) button_prop.act = act;
86     if (sel) button_prop.sel = sel;
87     if (over) button_prop.over = over;
88
89     this.buttons[command][this.buttons[command].length] = button_prop;    
90     };
91
92   // register a specific gui object
93   this.gui_object = function(name, id)
94     {
95     this.gui_objects[name] = id;
96     };
97   
98   // register a container object
99   this.gui_container = function(name, id)
100   {
101     this.gui_containers[name] = id;
102   };
103   
104   // add a GUI element (html node) to a specified container
105   this.add_element = function(elm, container)
106   {
107     if (this.gui_containers[container] && this.gui_containers[container].jquery)
108       this.gui_containers[container].append(elm);
109   };
110
111   // register an external handler for a certain command
112   this.register_command = function(command, callback, enable)
113   {
114     this.command_handlers[command] = callback;
115     
116     if (enable)
117       this.enable_command(command, true);
118   };
119   
120   // execute the given script on load
121   this.add_onload = function(f)
122   {
123     this.onloads[this.onloads.length] = f;
124   };
125
126   // initialize webmail client
127   this.init = function()
128     {
129     var p = this;
130     this.task = this.env.task;
131     
132     // check browser
133     if (!bw.dom || !bw.xmlhttp_test())
134       {
135       this.goto_url('error', '_code=0x199');
136       return;
137       }
138
139     // find all registered gui containers
140     for (var n in this.gui_containers)
141       this.gui_containers[n] = $('#'+this.gui_containers[n]);
142
143     // find all registered gui objects
144     for (var n in this.gui_objects)
145       this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
146       
147     // init registered buttons
148     this.init_buttons();
149
150     // tell parent window that this frame is loaded
151     if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
152       parent.rcmail.set_busy(false);
153
154     // enable general commands
155     this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
156     
157     if (this.env.permaurl)
158       this.enable_command('permaurl', true);
159
160     switch (this.task)
161       {
162       case 'mail':
163         if (this.gui_objects.messagelist)
164           {
165           this.message_list = new rcube_list_widget(this.gui_objects.messagelist, {multiselect:true, draggable:true, keyboard:true, dblclick_time:this.dblclick_time});
166           this.message_list.row_init = function(o){ p.init_message_row(o); };
167           this.message_list.addEventListener('dblclick', function(o){ p.msglist_dbl_click(o); });
168           this.message_list.addEventListener('keypress', function(o){ p.msglist_keypress(o); });
169           this.message_list.addEventListener('select', function(o){ p.msglist_select(o); });
170           this.message_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
171           this.message_list.addEventListener('dragmove', function(e){ p.drag_move(e); });
172           this.message_list.addEventListener('dragend', function(e){ p.drag_end(e); });
173           document.onmouseup = function(e){ return p.doc_mouse_up(e); };
174
175           this.message_list.init();
176           this.enable_command('toggle_status', 'toggle_flag', true);
177           
178           if (this.gui_objects.mailcontframe)
179             this.gui_objects.mailcontframe.onmousedown = function(e){ return p.click_on_list(e); };
180           else
181             this.message_list.focus();
182           }
183           
184         if (this.env.coltypes)
185           this.set_message_coltypes(this.env.coltypes);
186
187         // enable mail commands
188         this.enable_command('list', 'checkmail', 'compose', 'add-contact', 'search', 'reset-search', 'collapse-folder', true);
189
190         if (this.env.search_text != null && document.getElementById('quicksearchbox') != null)
191           document.getElementById('quicksearchbox').value = this.env.search_text;
192         
193         if (this.env.action=='show' || this.env.action=='preview')
194           {
195           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete',
196             'open', 'mark', 'edit', 'viewsource', 'download', 'print', 'load-attachment', 'load-headers', true);
197
198           if (this.env.next_uid)
199             {
200             this.enable_command('nextmessage', true);
201             this.enable_command('lastmessage', true);
202             }
203           if (this.env.prev_uid)
204             {
205             this.enable_command('previousmessage', true);
206             this.enable_command('firstmessage', true);
207             }
208         
209           if (this.env.blockedobjects)
210             {
211             if (this.gui_objects.remoteobjectsmsg)
212               this.gui_objects.remoteobjectsmsg.style.display = 'block';
213             this.enable_command('load-images', 'always-load', true);
214             }
215           }
216
217         if (this.env.trash_mailbox && this.env.mailbox != this.env.trash_mailbox)
218           this.set_alttext('delete', 'movemessagetotrash');
219         
220         // make preview/message frame visible
221         if (this.env.action == 'preview' && this.env.framed && parent.rcmail)
222           {
223           this.enable_command('compose', 'add-contact', false);
224           parent.rcmail.show_contentframe(true);
225           }
226
227         if (this.env.action=='compose')
228           {
229           this.enable_command('add-attachment', 'send-attachment', 'remove-attachment', 'send', true);
230           if (this.env.spellcheck)
231             {
232             this.env.spellcheck.spelling_state_observer = function(s){ ref.set_spellcheck_state(s); };
233             this.set_spellcheck_state('ready');
234             if ($("input[name='_is_html']").val() == '1')
235               this.display_spellcheck_controls(false);
236             }
237           if (this.env.drafts_mailbox)
238             this.enable_command('savedraft', true);
239             
240           document.onmouseup = function(e){ return p.doc_mouse_up(e); };
241
242           // init message compose form
243           this.init_messageform();
244           }
245
246         if (this.env.messagecount)
247           this.enable_command('select-all', 'select-none', 'expunge', true);
248
249         if (this.purge_mailbox_test())
250           this.enable_command('purge', true);
251
252         this.set_page_buttons();
253
254         // show printing dialog
255         if (this.env.action=='print')
256           window.print();
257
258         // get unread count for each mailbox
259         if (this.gui_objects.mailboxlist)
260         {
261           this.env.unread_counts = {};
262           this.gui_objects.folderlist = this.gui_objects.mailboxlist;
263           this.http_request('getunread', '');
264         }
265         
266         // ask user to send MDN
267         if (this.env.mdn_request && this.env.uid)
268         {
269           var mdnurl = '_uid='+this.env.uid+'&_mbox='+urlencode(this.env.mailbox);
270           if (confirm(this.get_label('mdnrequest')))
271             this.http_post('sendmdn', mdnurl);
272           else
273             this.http_post('mark', mdnurl+'&_flag=mdnsent');
274         }
275
276         break;
277
278
279       case 'addressbook':
280         if (this.gui_objects.contactslist)
281           {
282           this.contact_list = new rcube_list_widget(this.gui_objects.contactslist, {multiselect:true, draggable:true, keyboard:true});
283           this.contact_list.row_init = function(row){ p.triggerEvent('insertrow', { cid:row.uid, row:row }); };
284           this.contact_list.addEventListener('keypress', function(o){ p.contactlist_keypress(o); });
285           this.contact_list.addEventListener('select', function(o){ p.contactlist_select(o); });
286           this.contact_list.addEventListener('dragstart', function(o){ p.drag_start(o); });
287           this.contact_list.addEventListener('dragmove', function(e){ p.drag_move(e); });
288           this.contact_list.addEventListener('dragend', function(e){ p.drag_end(e); });
289           this.contact_list.init();
290
291           if (this.env.cid)
292             this.contact_list.highlight_row(this.env.cid);
293
294           if (this.gui_objects.contactslist.parentNode)
295             {
296             this.gui_objects.contactslist.parentNode.onmousedown = function(e){ return p.click_on_list(e); };
297             document.onmouseup = function(e){ return p.doc_mouse_up(e); };
298             }
299           else
300             this.contact_list.focus();
301             
302           this.gui_objects.folderlist = this.gui_objects.contactslist;
303           }
304
305         this.set_page_buttons();
306         
307         if (this.env.address_sources && this.env.address_sources[this.env.source] && !this.env.address_sources[this.env.source].readonly)
308           this.enable_command('add', true);
309         
310         if (this.env.cid)
311           this.enable_command('show', 'edit', true);
312
313         if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
314           this.enable_command('save', true);
315         else
316           this.enable_command('search', 'reset-search', 'moveto', 'import', true);
317           
318         if (this.contact_list && this.contact_list.rowcount > 0)
319           this.enable_command('export', true);
320
321         this.enable_command('list', true);
322         break;
323
324
325       case 'settings':
326         this.enable_command('preferences', 'identities', 'save', 'folders', true);
327         
328         if (this.env.action=='identities') {
329           this.enable_command('add', this.env.identities_level < 2);
330         }
331         else if (this.env.action=='edit-identity' || this.env.action=='add-identity') {
332           this.enable_command('add', this.env.identities_level < 2);
333           this.enable_command('save', 'delete', 'edit', true);
334         }
335         else if (this.env.action=='folders')
336           this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'rename-folder', 'delete-folder', true);
337
338         if (this.gui_objects.identitieslist)
339           {
340           this.identity_list = new rcube_list_widget(this.gui_objects.identitieslist, {multiselect:false, draggable:false, keyboard:false});
341           this.identity_list.addEventListener('select', function(o){ p.identity_select(o); });
342           this.identity_list.init();
343           this.identity_list.focus();
344
345           if (this.env.iid)
346             this.identity_list.highlight_row(this.env.iid);
347           }
348         else if (this.gui_objects.sectionslist)
349           {
350           this.sections_list = new rcube_list_widget(this.gui_objects.sectionslist, {multiselect:false, draggable:false, keyboard:false});
351           this.sections_list.addEventListener('select', function(o){ p.section_select(o); });
352           this.sections_list.init();
353           this.sections_list.focus();
354           this.sections_list.select('general');  // open first section by default
355         }
356         else if (this.gui_objects.subscriptionlist)
357           this.init_subscription_list();
358
359         break;
360
361       case 'login':
362         var input_user = $('#rcmloginuser');
363         input_user.bind('keyup', function(e){ return rcmail.login_user_keyup(e); });
364         
365         if (input_user.val() == '')
366           input_user.focus();
367         else
368           $('#rcmloginpwd').focus();
369
370         // detect client timezone
371         $('#rcmlogintz').val(new Date().getTimezoneOffset() / -60);
372
373         this.enable_command('login', true);
374         break;
375       
376       default:
377         break;
378       }
379
380     // flag object as complete
381     this.loaded = true;
382
383     // show message
384     if (this.pending_message)
385       this.display_message(this.pending_message[0], this.pending_message[1]);
386       
387     // map implicit containers
388     if (this.gui_objects.folderlist)
389       this.gui_containers.foldertray = $(this.gui_objects.folderlist);
390
391     // trigger init event hook
392     this.triggerEvent('init', { task:this.task, action:this.env.action });
393     
394     // execute all foreign onload scripts
395     // @deprecated
396     for (var i=0; i<this.onloads.length; i++)
397       {
398       if (typeof(this.onloads[i]) == 'string')
399         eval(this.onloads[i]);
400       else if (typeof(this.onloads[i]) == 'function')
401         this.onloads[i]();
402       }
403
404     // start keep-alive interval
405     this.start_keepalive();
406   };
407
408   // start interval for keep-alive/recent_check signal
409   this.start_keepalive = function()
410     {
411     if (this.env.keep_alive && !this.env.framed && this.task=='mail' && this.gui_objects.mailboxlist)
412       this._int = setInterval(function(){ ref.check_for_recent(false); }, this.env.keep_alive * 1000);
413     else if (this.env.keep_alive && !this.env.framed && this.task!='login')
414       this._int = setInterval(function(){ ref.send_keep_alive(); }, this.env.keep_alive * 1000);
415     }
416
417   this.init_message_row = function(row)
418   {
419     var uid = row.uid;
420     if (uid && this.env.messages[uid])
421       {
422       row.deleted = this.env.messages[uid].deleted ? true : false;
423       row.unread = this.env.messages[uid].unread ? true : false;
424       row.replied = this.env.messages[uid].replied ? true : false;
425       row.flagged = this.env.messages[uid].flagged ? true : false;
426       row.forwarded = this.env.messages[uid].forwarded ? true : false;
427       }
428
429     // set eventhandler to message icon
430     if (row.icon = row.obj.getElementsByTagName('td')[0].getElementsByTagName('img')[0])
431       {
432       var p = this;
433       row.icon.id = 'msgicn_'+row.uid;
434       row.icon._row = row.obj;
435       row.icon.onmousedown = function(e) { p.command('toggle_status', this); };
436       }
437
438     // global variable 'flagged_col' may be not defined yet
439     if (!this.env.flagged_col && this.env.coltypes)
440       {
441       var found;
442       if((found = find_in_array('flag', this.env.coltypes)) >= 0)
443         this.set_env('flagged_col', found+1);
444       }
445
446     // set eventhandler to flag icon, if icon found
447     if (this.env.flagged_col && (row.flagged_icon = row.obj.getElementsByTagName('td')[this.env.flagged_col].getElementsByTagName('img')[0]))
448       {
449       var p = this;
450       row.flagged_icon.id = 'flaggedicn_'+row.uid;
451       row.flagged_icon._row = row.obj;
452       row.flagged_icon.onmousedown = function(e) { p.command('toggle_flag', this); };
453       }
454       
455     this.triggerEvent('insertrow', { uid:uid, row:row });
456   };
457
458   // init message compose form: set focus and eventhandlers
459   this.init_messageform = function()
460     {
461     if (!this.gui_objects.messageform)
462       return false;
463     
464     //this.messageform = this.gui_objects.messageform;
465     var input_from = $("[name='_from']");
466     var input_to = $("[name='_to']");
467     var input_subject = $("input[name='_subject']");
468     var input_message = $("[name='_message']").get(0);
469
470     // init live search events
471     this.init_address_input_events(input_to);
472     this.init_address_input_events($("[name='_cc']"));
473     this.init_address_input_events($("[name='_bcc']"));
474
475     // add signature according to selected identity
476     if (input_from.attr('type') == 'select-one' && $("input[name='_draft_saveid']").val() == ''
477         && $("input[name='_is_html']").val() != '1') {  // if we have HTML editor, signature is added in callback
478       this.change_identity(input_from[0]);
479     }
480
481     if (input_to.val() == '')
482       input_to.focus();
483     else if (input_subject.val() == '')
484       input_subject.focus();
485     else if (input_message)
486       input_message.focus();
487
488     // get summary of all field values
489     this.compose_field_hash(true);
490  
491     // start the auto-save timer
492     this.auto_save_start();
493     };
494
495   this.init_address_input_events = function(obj)
496     {
497     var handler = function(e){ return ref.ksearch_keypress(e,this); };
498     obj.bind((bw.safari || bw.ie ? 'keydown' : 'keypress'), handler);
499     obj.attr('autocomplete', 'off');
500     };
501
502
503   /*********************************************************/
504   /*********       client command interface        *********/
505   /*********************************************************/
506
507   // execute a specific command on the web client
508   this.command = function(command, props, obj)
509     {
510     if (obj && obj.blur)
511       obj.blur();
512
513     if (this.busy)
514       return false;
515
516     // command not supported or allowed
517     if (!this.commands[command])
518       {
519       // pass command to parent window
520       if (this.env.framed && parent.rcmail && parent.rcmail.command)
521         parent.rcmail.command(command, props);
522
523       return false;
524       }
525       
526    // check input before leaving compose step
527    if (this.task=='mail' && this.env.action=='compose' && (command=='list' || command=='mail' || command=='addressbook' || command=='settings'))
528      {
529      if (this.cmp_hash != this.compose_field_hash() && !confirm(this.get_label('notsentwarning')))
530         return false;
531      }
532
533     // process external commands
534     if (typeof this.command_handlers[command] == 'function')
535     {
536       var ret = this.command_handlers[command](props, obj);
537       return ret !== null ? ret : (obj ? false : true);
538     }
539     else if (typeof this.command_handlers[command] == 'string')
540     {
541       var ret = window[this.command_handlers[command]](props, obj);
542       return ret !== null ? ret : (obj ? false : true);
543     }
544     
545     // trigger plugin hook
546     var event_ret = this.triggerEvent('before'+command, props);
547     if (typeof event_ret != 'undefined') {
548       // abort if one the handlers returned false
549       if (event_ret === false)
550         return false;
551       else
552         props = event_ret;
553     }
554
555     // process internal command
556     switch (command)
557       {
558       case 'login':
559         if (this.gui_objects.loginform)
560           this.gui_objects.loginform.submit();
561         break;
562
563       // commands to switch task
564       case 'mail':
565       case 'addressbook':
566       case 'settings':
567       case 'logout':
568         this.switch_task(command);
569         break;
570
571       case 'permaurl':
572         if (obj && obj.href && obj.target)
573           return true;
574         else if (this.env.permaurl)
575           parent.location.href = this.env.permaurl;
576         break;
577
578       case 'open':
579         var uid;
580         if (uid = this.get_single_uid())
581         {
582           obj.href = '?_task='+this.env.task+'&_action=show&_mbox='+urlencode(this.env.mailbox)+'&_uid='+uid;
583           return true;
584         }
585         break;
586
587       // misc list commands
588       case 'list':
589         if (this.task=='mail')
590           {
591           if (this.env.search_request<0 || (props != '' && (this.env.search_request && props != this.env.mailbox)))
592             this.reset_qsearch();
593
594           this.list_mailbox(props);
595
596           if (this.env.trash_mailbox)
597             this.set_alttext('delete', this.env.mailbox != this.env.trash_mailbox ? 'movemessagetotrash' : 'deletemessage');
598           }
599         else if (this.task=='addressbook')
600           {
601           if (this.env.search_request<0 || (this.env.search_request && props != this.env.source))
602             this.reset_qsearch();
603
604           this.list_contacts(props);
605           this.enable_command('add', (this.env.address_sources && !this.env.address_sources[props].readonly));
606           }
607         break;
608
609
610       case 'load-headers':
611         this.load_headers(obj);
612         break;
613
614
615       case 'sort':
616         var sort_order, sort_col = props;
617
618         if (this.env.sort_col==sort_col)
619           sort_order = this.env.sort_order=='ASC' ? 'DESC' : 'ASC';
620         else
621           sort_order = 'ASC';
622         
623         // set table header class
624         $('#rcm'+this.env.sort_col).removeClass('sorted'+(this.env.sort_order.toUpperCase()));
625         $('#rcm'+sort_col).addClass('sorted'+sort_order);
626
627         // save new sort properties
628         this.env.sort_col = sort_col;
629         this.env.sort_order = sort_order;
630
631         // reload message list
632         this.list_mailbox('', '', sort_col+'_'+sort_order);
633         break;
634
635       case 'nextpage':
636         this.list_page('next');
637         break;
638
639       case 'lastpage':
640         this.list_page('last');
641         break;
642
643       case 'previouspage':
644         this.list_page('prev');
645         break;
646
647       case 'firstpage':
648         this.list_page('first');
649         break;
650
651       case 'expunge':
652         if (this.env.messagecount)
653           this.expunge_mailbox(this.env.mailbox);
654         break;
655
656       case 'purge':
657       case 'empty-mailbox':
658         if (this.env.messagecount)
659           this.purge_mailbox(this.env.mailbox);
660         break;
661
662
663       // common commands used in multiple tasks
664       case 'show':
665         if (this.task=='mail')
666           {
667           var uid = this.get_single_uid();
668           if (uid && (!this.env.uid || uid != this.env.uid))
669             {
670             if (this.env.mailbox == this.env.drafts_mailbox)
671               this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
672             else
673               this.show_message(uid);
674             }
675           }
676         else if (this.task=='addressbook')
677           {
678           var cid = props ? props : this.get_single_cid();
679           if (cid && !(this.env.action=='show' && cid==this.env.cid))
680             this.load_contact(cid, 'show');
681           }
682         break;
683
684       case 'add':
685         if (this.task=='addressbook')
686           this.load_contact(0, 'add');
687         else if (this.task=='settings')
688           {
689           this.identity_list.clear_selection();
690           this.load_identity(0, 'add-identity');
691           }
692         break;
693
694       case 'edit':
695         var cid;
696         if (this.task=='addressbook' && (cid = this.get_single_cid()))
697           this.load_contact(cid, 'edit');
698         else if (this.task=='settings' && props)
699           this.load_identity(props, 'edit-identity');
700         else if (this.task=='mail' && (cid = this.get_single_uid())) {
701           var url = (this.env.mailbox == this.env.drafts_mailbox) ? '_draft_uid=' : '_uid=';
702           this.goto_url('compose', url+cid+'&_mbox='+urlencode(this.env.mailbox), true);
703         }
704         break;
705
706       case 'save-identity':
707       case 'save':
708         if (this.gui_objects.editform)
709           {
710           var input_pagesize = $("input[name='_pagesize']");
711           var input_name  = $("input[name='_name']");
712           var input_email = $("input[name='_email']");
713
714           // user prefs
715           if (input_pagesize.length && isNaN(parseInt(input_pagesize.val())))
716             {
717             alert(this.get_label('nopagesizewarning'));
718             input_pagesize.focus();
719             break;
720             }
721           // contacts/identities
722           else
723             {
724             if (input_name.length && input_name.val() == '')
725               {
726               alert(this.get_label('nonamewarning'));
727               input_name.focus();
728               break;
729               }
730             else if (input_email.length && !rcube_check_email(input_email.val()))
731               {
732               alert(this.get_label('noemailwarning'));
733               input_email.focus();
734               break;
735               }
736             }
737
738           this.gui_objects.editform.submit();
739           }
740         break;
741
742       case 'delete':
743         // mail task
744         if (this.task=='mail')
745           this.delete_messages();
746         // addressbook task
747         else if (this.task=='addressbook')
748           this.delete_contacts();
749         // user settings task
750         else if (this.task=='settings')
751           this.delete_identity();
752         break;
753
754
755       // mail task commands
756       case 'move':
757       case 'moveto':
758         if (this.task == 'mail')
759           this.move_messages(props);
760         else if (this.task == 'addressbook' && this.drag_active)
761           this.copy_contact(null, props);
762         break;
763
764       case 'mark':
765         if (props)
766           this.mark_message(props);
767         break;
768       
769       case 'toggle_status':
770         if (props && !props._row)
771           break;
772         
773         var uid;
774         var flag = 'read';
775         
776         if (props._row.uid)
777           {
778           uid = props._row.uid;
779           
780           // toggle read/unread
781           if (this.message_list.rows[uid].deleted) {
782             flag = 'undelete';
783           } else if (!this.message_list.rows[uid].unread)
784             flag = 'unread';
785           }
786           
787         this.mark_message(flag, uid);
788         break;
789         
790       case 'toggle_flag':
791         if (props && !props._row)
792           break;
793
794         var uid;
795         var flag = 'flagged';
796
797         if (props._row.uid)
798           {
799           uid = props._row.uid;
800           // toggle flagged/unflagged
801           if (this.message_list.rows[uid].flagged)
802             flag = 'unflagged';
803           }
804         this.mark_message(flag, uid);
805         break;
806
807       case 'always-load':
808         if (this.env.uid && this.env.sender) {
809           this.add_contact(urlencode(this.env.sender));
810           window.setTimeout(function(){ ref.command('load-images'); }, 300);
811           break;
812         }
813         
814       case 'load-images':
815         if (this.env.uid)
816           this.show_message(this.env.uid, true, this.env.action=='preview');
817         break;
818
819       case 'load-attachment':
820         var qstring = '_mbox='+urlencode(this.env.mailbox)+'&_uid='+this.env.uid+'&_part='+props.part;
821         
822         // open attachment in frame if it's of a supported mimetype
823         if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
824           {
825           if (props.mimetype == 'text/html')
826             qstring += '&_safe=1';
827           this.attachment_win = window.open(this.env.comm_path+'&_action=get&'+qstring+'&_frame=1', 'rcubemailattachment');
828           if (this.attachment_win)
829             {
830             window.setTimeout(function(){ ref.attachment_win.focus(); }, 10);
831             break;
832             }
833           }
834
835         this.goto_url('get', qstring+'&_download=1', false);
836         break;
837         
838       case 'select-all':
839         if (props == 'invert')
840           this.message_list.invert_selection();
841         else
842           this.message_list.select_all(props);
843         break;
844
845       case 'select-none':
846         this.message_list.clear_selection();
847         break;
848
849       case 'nextmessage':
850         if (this.env.next_uid)
851           this.show_message(this.env.next_uid, false, this.env.action=='preview');
852         break;
853
854       case 'lastmessage':
855         if (this.env.last_uid)
856           this.show_message(this.env.last_uid);
857         break;
858
859       case 'previousmessage':
860         if (this.env.prev_uid)
861           this.show_message(this.env.prev_uid, false, this.env.action=='preview');
862         break;
863
864       case 'firstmessage':
865         if (this.env.first_uid)
866           this.show_message(this.env.first_uid);
867         break;
868       
869       case 'checkmail':
870         this.check_for_recent(true);
871         break;
872       
873       case 'compose':
874         var url = this.env.comm_path+'&_action=compose';
875        
876         if (this.task=='mail')
877         {
878           url += '&_mbox='+urlencode(this.env.mailbox);
879           
880           if (this.env.mailbox==this.env.drafts_mailbox)
881           {
882             var uid;
883             if (uid = this.get_single_uid())
884               url += '&_draft_uid='+uid;
885           }
886           else if (props)
887              url += '&_to='+urlencode(props);
888         }
889         // modify url if we're in addressbook
890         else if (this.task=='addressbook')
891           {
892           // switch to mail compose step directly
893           if (props && props.indexOf('@') > 0)
894             {
895             url = this.get_task_url('mail', url);
896             this.redirect(url + '&_to='+urlencode(props));
897             break;
898             }
899           
900           // use contact_id passed as command parameter
901           var a_cids = new Array();
902           if (props)
903             a_cids[a_cids.length] = props;
904           // get selected contacts
905           else if (this.contact_list)
906             {
907             var selection = this.contact_list.get_selection();
908             for (var n=0; n<selection.length; n++)
909               a_cids[a_cids.length] = selection[n];
910             }
911             
912           if (a_cids.length)
913             this.http_request('mailto', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source), true);
914
915           break;
916           }
917
918         // don't know if this is necessary...
919         url = url.replace(/&_framed=1/, "");
920
921         this.redirect(url);
922         break;
923         
924       case 'spellcheck':
925         if (window.tinyMCE && tinyMCE.get(this.env.composebody)) {
926           tinyMCE.execCommand('mceSpellCheck', true);
927         }
928         else if (this.env.spellcheck && this.env.spellcheck.spellCheck && this.spellcheck_ready) {
929           this.env.spellcheck.spellCheck();
930           this.set_spellcheck_state('checking');
931         }
932         break;
933
934       case 'savedraft':
935         // Reset the auto-save timer
936         self.clearTimeout(this.save_timer);
937
938         if (!this.gui_objects.messageform)
939           break;
940
941         // if saving Drafts is disabled in main.inc.php
942         // or if compose form did not change
943         if (!this.env.drafts_mailbox || this.cmp_hash == this.compose_field_hash())
944           break;
945
946         this.set_busy(true, 'savingmessage');
947         var form = this.gui_objects.messageform;
948         form.target = "savetarget";
949         form._draft.value = '1';
950         form.submit();
951         break;
952
953       case 'send':
954         if (!this.gui_objects.messageform)
955           break;
956
957         if (!this.check_compose_input())
958           break;
959
960         // Reset the auto-save timer
961         self.clearTimeout(this.save_timer);
962
963         // all checks passed, send message
964         this.set_busy(true, 'sendingmessage');
965         var form = this.gui_objects.messageform;
966         form.target = "savetarget";     
967         form._draft.value = '';
968         form.submit();
969         
970         // clear timeout (sending could take longer)
971         clearTimeout(this.request_timer);
972         break;
973
974       case 'add-attachment':
975         this.show_attachment_form(true);
976         
977       case 'send-attachment':
978         // Reset the auto-save timer
979         self.clearTimeout(this.save_timer);
980
981         this.upload_file(props)      
982         break;
983       
984       case 'remove-attachment':
985         this.remove_attachment(props);
986         break;
987
988       case 'reply-all':
989       case 'reply':
990         var uid;
991         if (uid = this.get_single_uid())
992           this.goto_url('compose', '_reply_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : ''), true);
993         break;      
994
995       case 'forward':
996         var uid;
997         if (uid = this.get_single_uid())
998           this.goto_url('compose', '_forward_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
999         break;
1000         
1001       case 'print':
1002         var uid;
1003         if (uid = this.get_single_uid())
1004         {
1005           ref.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
1006           if (this.printwin)
1007           {
1008             window.setTimeout(function(){ ref.printwin.focus(); }, 20);
1009             if (this.env.action != 'show')
1010               this.mark_message('read', uid);
1011           }
1012         }
1013         break;
1014
1015       case 'viewsource':
1016         var uid;
1017         if (uid = this.get_single_uid())
1018           {
1019           ref.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox));
1020           if (this.sourcewin)
1021             window.setTimeout(function(){ ref.sourcewin.focus(); }, 20);
1022           }
1023         break;
1024
1025       case 'download':
1026         var uid;
1027         if (uid = this.get_single_uid())
1028           this.goto_url('viewsource', '&_uid='+uid+'&_mbox='+urlencode(this.env.mailbox)+'&_save=1');
1029         break;
1030
1031       case 'add-contact':
1032         this.add_contact(props);
1033         break;
1034       
1035       // quicksearch
1036       case 'search':
1037         if (!props && this.gui_objects.qsearchbox)
1038           props = this.gui_objects.qsearchbox.value;
1039         if (props)
1040         {
1041           this.qsearch(props);
1042           break;
1043         }
1044
1045       // reset quicksearch
1046       case 'reset-search':
1047         var s = this.env.search_request;
1048         this.reset_qsearch();
1049         
1050         if (s && this.env.mailbox)
1051           this.list_mailbox(this.env.mailbox);
1052         else if (s && this.task == 'addressbook')
1053           this.list_contacts(this.env.source);
1054         break;
1055
1056       case 'import':
1057         if (this.env.action == 'import' && this.gui_objects.importform) {
1058           var file = document.getElementById('rcmimportfile');
1059           if (file && !file.value) {
1060             alert(this.get_label('selectimportfile'));
1061             break;
1062           }
1063           this.gui_objects.importform.submit();
1064           this.set_busy(true, 'importwait');
1065           this.lock_form(this.gui_objects.importform, true);
1066         }
1067         else
1068           this.goto_url('import');
1069         break;
1070         
1071       case 'export':
1072         if (this.contact_list.rowcount > 0) {
1073           var add_url = (this.env.source ? '_source='+urlencode(this.env.source)+'&' : '');
1074           if (this.env.search_request)
1075             add_url += '_search='+this.env.search_request;
1076         
1077           this.goto_url('export', add_url);
1078         }
1079         break;
1080
1081       // collapse/expand folder
1082       case 'collapse-folder':
1083         if (props)
1084           this.collapse_folder(props);
1085         break;
1086
1087       // user settings commands
1088       case 'preferences':
1089         this.goto_url('');
1090         break;
1091
1092       case 'identities':
1093         this.goto_url('identities');
1094         break;
1095           
1096       case 'delete-identity':
1097         this.delete_identity();
1098         
1099       case 'folders':
1100         this.goto_url('folders');
1101         break;
1102
1103       case 'subscribe':
1104         this.subscribe_folder(props);
1105         break;
1106
1107       case 'unsubscribe':
1108         this.unsubscribe_folder(props);
1109         break;
1110         
1111       case 'create-folder':
1112         this.create_folder(props);
1113         break;
1114
1115       case 'rename-folder':
1116         this.rename_folder(props);
1117         break;
1118
1119       case 'delete-folder':
1120         this.delete_folder(props);
1121         break;
1122
1123       }
1124       
1125     this.triggerEvent('after'+command, props);
1126
1127     return obj ? false : true;
1128     };
1129
1130   // set command enabled or disabled
1131   this.enable_command = function()
1132     {
1133     var args = arguments;
1134     if(!args.length) return -1;
1135
1136     var command;
1137     var enable = args[args.length-1];
1138     
1139     for(var n=0; n<args.length-1; n++)
1140       {
1141       command = args[n];
1142       this.commands[command] = enable;
1143       this.set_button(command, (enable ? 'act' : 'pas'));
1144       }
1145       return true;
1146     };
1147
1148   // lock/unlock interface
1149   this.set_busy = function(a, message)
1150     {
1151     if (a && message)
1152       {
1153       var msg = this.get_label(message);
1154       if (msg==message)        
1155         msg = 'Loading...';
1156
1157       this.display_message(msg, 'loading', true);
1158       }
1159     else if (!a)
1160       this.hide_message();
1161
1162     this.busy = a;
1163     //document.body.style.cursor = a ? 'wait' : 'default';
1164     
1165     if (this.gui_objects.editform)
1166       this.lock_form(this.gui_objects.editform, a);
1167       
1168     // clear pending timer
1169     if (this.request_timer)
1170       clearTimeout(this.request_timer);
1171
1172     // set timer for requests
1173     if (a && this.env.request_timeout)
1174       this.request_timer = window.setTimeout(function(){ ref.request_timed_out(); }, this.env.request_timeout * 1000);
1175     };
1176
1177   // return a localized string
1178   this.get_label = function(name, domain)
1179     {
1180     if (domain && this.labels[domain+'.'+name])
1181       return this.labels[domain+'.'+name];
1182     else if (this.labels[name])
1183       return this.labels[name];
1184     else
1185       return name;
1186     };
1187   
1188   // alias for convenience reasons
1189   this.gettext = this.get_label;
1190
1191   // switch to another application task
1192   this.switch_task = function(task)
1193     {
1194     if (this.task===task && task!='mail')
1195       return;
1196
1197     var url = this.get_task_url(task);
1198     if (task=='mail')
1199       url += '&_mbox=INBOX';
1200
1201     this.redirect(url);
1202     };
1203
1204   this.get_task_url = function(task, url)
1205     {
1206     if (!url)
1207       url = this.env.comm_path;
1208
1209     return url.replace(/_task=[a-z]+/, '_task='+task);
1210     };
1211     
1212   // called when a request timed out
1213   this.request_timed_out = function()
1214     {
1215     this.set_busy(false);
1216     this.display_message('Request timed out!', 'error');
1217     };
1218   
1219   this.reload = function(delay)
1220   {
1221     if (this.env.framed && parent.rcmail)
1222       parent.rcmail.reload(delay);
1223     else if (delay)
1224       window.setTimeout(function(){ rcmail.reload(); }, delay);
1225     else if (window.location)
1226       location.href = this.env.comm_path;
1227   };
1228
1229
1230   /*********************************************************/
1231   /*********        event handling methods         *********/
1232   /*********************************************************/
1233
1234   this.doc_mouse_up = function(e)
1235   {
1236     var model, list, li;
1237
1238     if (this.message_list) {
1239       if (!rcube_mouse_is_over(e, this.message_list.list))
1240         this.message_list.blur();
1241       list = this.message_list;
1242       model = this.env.mailboxes;
1243     }
1244     else if (this.contact_list) {
1245       if (!rcube_mouse_is_over(e, this.contact_list.list))
1246         this.contact_list.blur();
1247       list = this.contact_list;
1248       model = this.env.address_sources;
1249     }
1250     else if (this.ksearch_value) {
1251       this.ksearch_blur();
1252     }
1253
1254     // handle mouse release when dragging
1255     if (this.drag_active && model && this.env.last_folder_target) {
1256       $(this.get_folder_li(this.env.last_folder_target)).removeClass('droptarget');
1257       this.command('moveto', model[this.env.last_folder_target].id);
1258       this.env.last_folder_target = null;
1259       list.draglayer.hide();
1260     }
1261     
1262     // reset 'pressed' buttons
1263     if (this.buttons_sel) {
1264       for (var id in this.buttons_sel)
1265         if (typeof id != 'function')
1266           this.button_out(this.buttons_sel[id], id);
1267       this.buttons_sel = {};
1268     }
1269   };
1270
1271   this.drag_start = function(list)
1272   {
1273     var model = this.task == 'mail' ? this.env.mailboxes : this.env.address_sources;
1274
1275     this.drag_active = true;
1276     if (this.preview_timer)
1277       clearTimeout(this.preview_timer);
1278     
1279     // save folderlist and folders location/sizes for droptarget calculation in drag_move()
1280     if (this.gui_objects.folderlist && model)
1281       {
1282       this.initialBodyScrollTop = bw.ie ? 0 : window.pageYOffset;
1283       this.initialListScrollTop = this.gui_objects.folderlist.parentNode.scrollTop;
1284
1285       var li, pos, list, height;
1286       list = $(this.gui_objects.folderlist);
1287       pos = list.offset();
1288       this.env.folderlist_coords = { x1:pos.left, y1:pos.top, x2:pos.left + list.width(), y2:pos.top + list.height() };
1289
1290       this.env.folder_coords = new Array();
1291       for (var k in model) {
1292         if (li = this.get_folder_li(k)) {
1293           // only visible folders
1294           if (height = li.firstChild.offsetHeight) {
1295             pos = $(li.firstChild).offset();
1296             this.env.folder_coords[k] = { x1:pos.left, y1:pos.top,
1297               x2:pos.left + li.firstChild.offsetWidth, y2:pos.top + height, on:0 };
1298           }
1299         }
1300       }
1301     }
1302   };
1303
1304   this.drag_end = function(e)
1305   {
1306     this.drag_active = false;
1307     this.env.last_folder_target = null;
1308     
1309     if (this.folder_auto_timer) {
1310       window.clearTimeout(this.folder_auto_timer);
1311       this.folder_auto_timer = null;
1312       this.folder_auto_expand = null;
1313     }
1314
1315     // over the folders
1316     if (this.gui_objects.folderlist && this.env.folder_coords) {
1317       for (var k in this.env.folder_coords) {
1318         if (this.env.folder_coords[k].on)
1319           $(this.get_folder_li(k)).removeClass('droptarget');
1320       }
1321     }
1322   };
1323   
1324   this.drag_move = function(e)
1325   {
1326     if (this.gui_objects.folderlist && this.env.folder_coords) {
1327       // offsets to compensate for scrolling while dragging a message
1328       var boffset = bw.ie ? -document.documentElement.scrollTop : this.initialBodyScrollTop;
1329       var moffset = this.initialListScrollTop-this.gui_objects.folderlist.parentNode.scrollTop;
1330       var toffset = -moffset-boffset;
1331
1332       var li, div, pos, mouse;
1333       mouse = rcube_event.get_mouse_pos(e);
1334       pos = this.env.folderlist_coords;
1335       mouse.y += toffset;
1336
1337       // if mouse pointer is outside of folderlist
1338       if (mouse.x < pos.x1 || mouse.x >= pos.x2 || mouse.y < pos.y1 || mouse.y >= pos.y2) {
1339         if (this.env.last_folder_target) {
1340           $(this.get_folder_li(this.env.last_folder_target)).removeClass('droptarget');
1341           this.env.folder_coords[this.env.last_folder_target].on = 0;
1342           this.env.last_folder_target = null;
1343         }
1344         return;
1345       }
1346     
1347       // over the folders
1348       for (var k in this.env.folder_coords) {
1349         pos = this.env.folder_coords[k];
1350         if (mouse.x >= pos.x1 && mouse.x < pos.x2 && mouse.y >= pos.y1 && mouse.y < pos.y2
1351             && this.check_droptarget(k)) {
1352
1353           li = this.get_folder_li(k);
1354           div = $(li.getElementsByTagName("div")[0]);
1355
1356           // if the folder is collapsed, expand it after 1sec and restart the drag & drop process.
1357           if (div.hasClass('collapsed')) {
1358             if (this.folder_auto_timer)
1359               window.clearTimeout(this.folder_auto_timer);
1360             
1361             this.folder_auto_expand = k;
1362             this.folder_auto_timer = window.setTimeout(function() {
1363                 rcmail.command("collapse-folder", rcmail.folder_auto_expand);
1364                 rcmail.drag_start(null);
1365               }, 1000);
1366           } else if (this.folder_auto_timer) {
1367             window.clearTimeout(this.folder_auto_timer);
1368             this.folder_auto_timer = null;
1369             this.folder_auto_expand = null;
1370           }
1371           
1372           $(li).addClass('droptarget');
1373           this.env.last_folder_target = k;
1374           this.env.folder_coords[k].on = 1;
1375         }
1376         else if (pos.on) {
1377           $(this.get_folder_li(k)).removeClass('droptarget');
1378           this.env.folder_coords[k].on = 0;
1379         }
1380       }
1381     }
1382   };
1383
1384   this.collapse_folder = function(id)
1385     {
1386     var div;
1387     if ((li = this.get_folder_li(id)) &&
1388         (div = $(li.getElementsByTagName("div")[0])) &&
1389         (div.hasClass('collapsed') || div.hasClass('expanded')))
1390       {
1391       var ul = $(li.getElementsByTagName("ul")[0]);
1392       if (div.hasClass('collapsed'))
1393         {
1394         ul.show();
1395         div.removeClass('collapsed').addClass('expanded');
1396         var reg = new RegExp('&'+urlencode(id)+'&');
1397         this.set_env('collapsed_folders', this.env.collapsed_folders.replace(reg, ''));
1398         }
1399       else
1400         {
1401         ul.hide();
1402         div.removeClass('expanded').addClass('collapsed');
1403         this.set_env('collapsed_folders', this.env.collapsed_folders+'&'+urlencode(id)+'&');
1404
1405         // select parent folder if one of its childs is currently selected
1406         if (this.env.mailbox.indexOf(id + this.env.delimiter) == 0)
1407           this.command('list', id);
1408         }
1409
1410       // Work around a bug in IE6 and IE7, see #1485309
1411       if ((bw.ie6 || bw.ie7) &&
1412           li.nextSibling &&
1413           (li.nextSibling.getElementsByTagName("ul").length>0) &&
1414           li.nextSibling.getElementsByTagName("ul")[0].style &&
1415           (li.nextSibling.getElementsByTagName("ul")[0].style.display!='none'))
1416         {
1417           li.nextSibling.getElementsByTagName("ul")[0].style.display = 'none';
1418           li.nextSibling.getElementsByTagName("ul")[0].style.display = '';
1419         }
1420
1421       this.http_post('save-pref', '_name=collapsed_folders&_value='+urlencode(this.env.collapsed_folders));
1422       this.set_unread_count_display(id, false);
1423       }
1424     }
1425
1426   this.click_on_list = function(e)
1427     {
1428     if (this.gui_objects.qsearchbox)
1429       this.gui_objects.qsearchbox.blur();
1430
1431     if (this.message_list)
1432       this.message_list.focus();
1433     else if (this.contact_list)
1434       this.contact_list.focus();
1435
1436     return rcube_event.get_button(e) == 2 ? true : rcube_event.cancel(e);
1437     };
1438
1439   this.msglist_select = function(list)
1440     {
1441     if (this.preview_timer)
1442       clearTimeout(this.preview_timer);
1443
1444     var selected = list.selection.length==1;
1445
1446     // Hide certain command buttons when Drafts folder is selected
1447     if (this.env.mailbox == this.env.drafts_mailbox)
1448       {
1449       this.enable_command('reply', 'reply-all', 'forward', false);
1450       this.enable_command('show', 'print', 'open', 'edit', 'download', 'viewsource', selected);
1451       this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
1452       }
1453     else
1454       {
1455       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', 'edit', 'open', 'download', 'viewsource', selected);
1456       this.enable_command('delete', 'moveto', 'mark', (list.selection.length > 0 ? true : false));
1457       }
1458
1459     // start timer for message preview (wait for double click)
1460     if (selected && this.env.contentframe && !list.multi_selecting)
1461       this.preview_timer = window.setTimeout(function(){ ref.msglist_get_preview(); }, 200);
1462     else if (this.env.contentframe)
1463       this.show_contentframe(false);
1464     };
1465
1466   this.msglist_dbl_click = function(list)
1467     {
1468       if (this.preview_timer)
1469         clearTimeout(this.preview_timer);
1470
1471     var uid = list.get_single_selection();
1472     if (uid && this.env.mailbox == this.env.drafts_mailbox)
1473       this.goto_url('compose', '_draft_uid='+uid+'&_mbox='+urlencode(this.env.mailbox), true);
1474     else if (uid)
1475       this.show_message(uid, false, false);
1476     };
1477
1478   this.msglist_keypress = function(list)
1479     {
1480     if (list.key_pressed == list.ENTER_KEY)
1481       this.command('show');
1482     else if (list.key_pressed == list.DELETE_KEY)
1483       this.command('delete');
1484     else if (list.key_pressed == list.BACKSPACE_KEY)
1485       this.command('delete');
1486     else
1487       list.shiftkey = false;
1488     };
1489
1490   this.msglist_get_preview = function()
1491   {
1492     var uid = this.get_single_uid();
1493     if (uid && this.env.contentframe && !this.drag_active)
1494       this.show_message(uid, false, true);
1495     else if (this.env.contentframe)
1496       this.show_contentframe(false);
1497   };
1498   
1499   this.check_droptarget = function(id)
1500   {
1501     if (this.task == 'mail')
1502       return (this.env.mailboxes[id] && this.env.mailboxes[id].id != this.env.mailbox && !this.env.mailboxes[id].virtual);
1503     else if (this.task == 'addressbook')
1504       return (id != this.env.source && this.env.address_sources[id] && !this.env.address_sources[id].readonly);
1505     else if (this.task == 'settings')
1506       return (id != this.env.folder);
1507   };
1508
1509
1510   /*********************************************************/
1511   /*********     (message) list functionality      *********/
1512   /*********************************************************/
1513
1514   // when user doble-clicks on a row
1515   this.show_message = function(id, safe, preview)
1516     {
1517     if (!id) return;
1518     
1519     var add_url = '';
1520     var action = preview ? 'preview': 'show';
1521     var target = window;
1522     
1523     if (preview && this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1524       {
1525       target = window.frames[this.env.contentframe];
1526       add_url = '&_framed=1';
1527       }
1528
1529     if (safe)
1530       add_url = '&_safe=1';
1531
1532     // also send search request to get the right messages
1533     if (this.env.search_request)
1534       add_url += '&_search='+this.env.search_request;
1535
1536     var url = '&_action='+action+'&_uid='+id+'&_mbox='+urlencode(this.env.mailbox)+add_url;
1537     if (action == 'preview' && String(target.location.href).indexOf(url) >= 0)
1538       this.show_contentframe(true);
1539     else
1540       {
1541       this.set_busy(true, 'loading');
1542       target.location.href = this.env.comm_path+url;
1543
1544       // mark as read and change mbox unread counter
1545       if (action == 'preview' && this.message_list && this.message_list.rows[id] && this.message_list.rows[id].unread)
1546         {
1547         this.set_message(id, 'unread', false);
1548         if (this.env.unread_counts[this.env.mailbox])
1549           {
1550           this.env.unread_counts[this.env.mailbox] -= 1;
1551           this.set_unread_count(this.env.mailbox, this.env.unread_counts[this.env.mailbox], this.env.mailbox == 'INBOX');
1552           }
1553         }
1554       }
1555     };
1556
1557   this.show_contentframe = function(show)
1558     {
1559     var frm;
1560     if (this.env.contentframe && (frm = $('#'+this.env.contentframe)) && frm.length)
1561       {
1562       if (!show && window.frames[this.env.contentframe])
1563         {
1564         if (window.frames[this.env.contentframe].location.href.indexOf(this.env.blankpage)<0)
1565           window.frames[this.env.contentframe].location.href = this.env.blankpage;
1566         }
1567       else if (!bw.safari && !bw.konq)
1568         frm[show ? 'show' : 'hide']();
1569       }
1570
1571     if (!show && this.busy)
1572       this.set_busy(false);
1573     };
1574
1575   // list a specific page
1576   this.list_page = function(page)
1577     {
1578     if (page=='next')
1579       page = this.env.current_page+1;
1580     if (page=='last')
1581       page = this.env.pagecount;
1582     if (page=='prev' && this.env.current_page>1)
1583       page = this.env.current_page-1;
1584     if (page=='first' && this.env.current_page>1)
1585       page = 1;
1586       
1587     if (page > 0 && page <= this.env.pagecount)
1588       {
1589       this.env.current_page = page;
1590       
1591       if (this.task=='mail')
1592         this.list_mailbox(this.env.mailbox, page);
1593       else if (this.task=='addressbook')
1594         this.list_contacts(this.env.source, page);
1595       }
1596     };
1597
1598   // list messages of a specific mailbox using filter
1599   this.filter_mailbox = function(filter)
1600     {
1601       var search;
1602       if (this.gui_objects.qsearchbox)
1603         search = this.gui_objects.qsearchbox.value;
1604       
1605       this.message_list.clear();
1606
1607       // reset vars
1608       this.env.current_page = 1;
1609       this.set_busy(true, 'searching');
1610       this.http_request('search', '_filter='+filter
1611             + (search ? '&_q='+urlencode(search) : '')
1612             + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : ''), true);
1613     }
1614
1615
1616   // list messages of a specific mailbox
1617   this.list_mailbox = function(mbox, page, sort)
1618     {
1619     var add_url = '';
1620     var target = window;
1621
1622     if (!mbox)
1623       mbox = this.env.mailbox;
1624
1625     // add sort to url if set
1626     if (sort)
1627       add_url += '&_sort=' + sort;
1628
1629     // also send search request to get the right messages
1630     if (this.env.search_request)
1631       add_url += '&_search='+this.env.search_request;
1632       
1633     // set page=1 if changeing to another mailbox
1634     if (!page)
1635       {
1636       page = 1;
1637       this.env.current_page = page;
1638       this.show_contentframe(false);
1639       }
1640     
1641     if (mbox != this.env.mailbox || (mbox == this.env.mailbox && !page && !sort))
1642       add_url += '&_refresh=1';
1643
1644     // unselect selected messages
1645     this.last_selected = 0;
1646     if (this.message_list)
1647       this.message_list.clear_selection();
1648     
1649     this.select_folder(mbox, this.env.mailbox);
1650     this.env.mailbox = mbox;
1651
1652     // load message list remotely
1653     if (this.gui_objects.messagelist)
1654       {
1655       this.list_mailbox_remote(mbox, page, add_url);
1656       return;
1657       }
1658     
1659     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1660       {
1661       target = window.frames[this.env.contentframe];
1662       add_url += '&_framed=1';
1663       }
1664
1665     // load message list to target frame/window
1666     if (mbox)
1667       {
1668       this.set_busy(true, 'loading');
1669       target.location.href = this.env.comm_path+'&_mbox='+urlencode(mbox)+(page ? '&_page='+page : '')+add_url;
1670       }
1671     };
1672
1673   // send remote request to load message list
1674   this.list_mailbox_remote = function(mbox, page, add_url)
1675     {
1676     // clear message list first
1677     this.message_list.clear();
1678
1679     // send request to server
1680     var url = '_mbox='+urlencode(mbox)+(page ? '&_page='+page : '');
1681     this.set_busy(true, 'loading');
1682     this.http_request('list', url+add_url, true);
1683     };
1684
1685   this.expunge_mailbox = function(mbox)
1686     {
1687     var lock = false;
1688     var add_url = '';
1689     
1690     // lock interface if it's the active mailbox
1691     if (mbox == this.env.mailbox)
1692        {
1693        lock = true;
1694        this.set_busy(true, 'loading');
1695        add_url = '&_reload=1';
1696        }
1697
1698     // send request to server
1699     var url = '_mbox='+urlencode(mbox);
1700     this.http_post('expunge', url+add_url, lock);
1701     };
1702
1703   this.purge_mailbox = function(mbox)
1704     {
1705     var lock = false;
1706     var add_url = '';
1707     
1708     if (!confirm(this.get_label('purgefolderconfirm')))
1709       return false;
1710     
1711     // lock interface if it's the active mailbox
1712     if (mbox == this.env.mailbox)
1713        {
1714        lock = true;
1715        this.set_busy(true, 'loading');
1716        add_url = '&_reload=1';
1717        }
1718
1719     // send request to server
1720     var url = '_mbox='+urlencode(mbox);
1721     this.http_post('purge', url+add_url, lock);
1722     return true;
1723     };
1724
1725   // test if purge command is allowed
1726   this.purge_mailbox_test = function()
1727   {
1728     return (this.env.messagecount && (this.env.mailbox == this.env.trash_mailbox || this.env.mailbox == this.env.junk_mailbox 
1729       || this.env.mailbox.match('^' + RegExp.escape(this.env.trash_mailbox) + RegExp.escape(this.env.delimiter)) 
1730       || this.env.mailbox.match('^' + RegExp.escape(this.env.junk_mailbox) + RegExp.escape(this.env.delimiter))));
1731   };
1732
1733   // set message icon
1734   this.set_message_icon = function(uid)
1735   {
1736     var icn_src;
1737     var rows = this.message_list.rows;
1738
1739     if (!rows[uid])
1740       return false;
1741
1742     if (rows[uid].deleted && this.env.deletedicon)
1743       icn_src = this.env.deletedicon;
1744     else if (rows[uid].replied && this.env.repliedicon)
1745       {
1746       if (rows[uid].forwarded && this.env.forwardedrepliedicon)
1747         icn_src = this.env.forwardedrepliedicon;
1748       else
1749         icn_src = this.env.repliedicon;
1750       }
1751     else if (rows[uid].forwarded && this.env.forwardedicon)
1752       icn_src = this.env.forwardedicon;
1753     else if (rows[uid].unread && this.env.unreadicon)
1754       icn_src = this.env.unreadicon;
1755     else if (this.env.messageicon)
1756       icn_src = this.env.messageicon;
1757       
1758     if (icn_src && rows[uid].icon)
1759       rows[uid].icon.src = icn_src;
1760
1761     icn_src = '';
1762     
1763     if (rows[uid].flagged && this.env.flaggedicon)
1764       icn_src = this.env.flaggedicon;
1765     else if (!rows[uid].flagged && this.env.unflaggedicon)
1766       icn_src = this.env.unflaggedicon;
1767
1768     if (rows[uid].flagged_icon && icn_src)
1769       rows[uid].flagged_icon.src = icn_src;
1770   }
1771
1772   // set message status
1773   this.set_message_status = function(uid, flag, status)
1774     {
1775     var rows = this.message_list.rows;
1776
1777     if (!rows[uid]) return false;
1778
1779     if (flag == 'unread')
1780       rows[uid].unread = status;
1781     else if(flag == 'deleted')
1782       rows[uid].deleted = status;
1783     else if (flag == 'replied')
1784       rows[uid].replied = status;
1785     else if (flag == 'forwarded')
1786       rows[uid].forwarded = status;
1787     else if (flag == 'flagged')
1788       rows[uid].flagged = status;
1789
1790     this.env.messages[uid] = rows[uid];
1791     }
1792
1793   // set message row status, class and icon
1794   this.set_message = function(uid, flag, status)
1795     {
1796     var rows = this.message_list.rows;
1797
1798     if (!rows[uid]) return false;
1799     
1800     if (flag)
1801       this.set_message_status(uid, flag, status);
1802     
1803     var rowobj = $(rows[uid].obj);
1804     if (rows[uid].unread && rows[uid].classname.indexOf('unread')<0)
1805       {
1806       rows[uid].classname += ' unread';
1807       rowobj.addClass('unread');
1808       }
1809     else if (!rows[uid].unread && rows[uid].classname.indexOf('unread')>=0)
1810       {
1811       rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
1812       rowobj.removeClass('unread');
1813       }
1814     
1815     if (rows[uid].deleted && rows[uid].classname.indexOf('deleted')<0)
1816       {
1817       rows[uid].classname += ' deleted';
1818       rowobj.addClass('deleted');
1819       }
1820     else if (!rows[uid].deleted && rows[uid].classname.indexOf('deleted')>=0)
1821       {
1822       rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
1823       rowobj.removeClass('deleted');
1824       }
1825
1826     if (rows[uid].flagged && rows[uid].classname.indexOf('flagged')<0)
1827       {
1828       rows[uid].classname += ' flagged';
1829       rowobj.addClass('flagged');
1830       }
1831     else if (!rows[uid].flagged && rows[uid].classname.indexOf('flagged')>=0)
1832       {
1833       rows[uid].classname = rows[uid].classname.replace(/\s*flagged/, '');
1834       rowobj.removeClass('flagged');
1835       }
1836
1837     this.set_message_icon(uid);
1838     }
1839
1840   // move selected messages to the specified mailbox
1841   this.move_messages = function(mbox)
1842     {
1843     // exit if current or no mailbox specified or if selection is empty
1844     if (!mbox || mbox == this.env.mailbox || (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length)))
1845       return;
1846
1847     var lock = false;
1848     var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
1849
1850     // show wait message
1851     if (this.env.action=='show')
1852       {
1853       lock = true;
1854       this.set_busy(true, 'movingmessage');
1855       }
1856     else
1857       this.show_contentframe(false);
1858
1859     // Hide message command buttons until a message is selected
1860     this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', 'open', 'edit', 'viewsource', 'download', false);
1861
1862     this._with_selected_messages('moveto', lock, add_url);
1863     };
1864
1865   // delete selected messages from the current mailbox
1866   this.delete_messages = function()
1867     {
1868     var selection = this.message_list ? this.message_list.get_selection() : new Array();
1869
1870     // exit if no mailbox specified or if selection is empty
1871     if (!this.env.uid && !selection.length)
1872       return;
1873
1874     // if config is set to flag for deletion
1875     if (this.env.flag_for_deletion)
1876       this.mark_message('delete');
1877     // if there isn't a defined trash mailbox or we are in it
1878     else if (!this.env.trash_mailbox || String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase()) 
1879       this.permanently_remove_messages();
1880     // if there is a trash mailbox defined and we're not currently in it
1881     else {
1882       // if shift was pressed delete it immediately
1883       if (this.message_list && this.message_list.shiftkey)
1884         {
1885         if (confirm(this.get_label('deletemessagesconfirm')))
1886           this.permanently_remove_messages();
1887         }
1888       else
1889         this.move_messages(this.env.trash_mailbox);
1890       }
1891   };
1892
1893   // delete the selected messages permanently
1894   this.permanently_remove_messages = function()
1895     {
1896     // exit if no mailbox specified or if selection is empty
1897     if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1898       return;
1899       
1900     this.show_contentframe(false);
1901     this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''));
1902     };
1903
1904   // Send a specifc request with UIDs of all selected messages
1905   // @private
1906   this._with_selected_messages = function(action, lock, add_url, remove)
1907   {
1908     var a_uids = new Array();
1909
1910     if (this.env.uid)
1911       a_uids[0] = this.env.uid;
1912     else
1913     {
1914       var selection = this.message_list.get_selection();
1915       var rows = this.message_list.rows;
1916       var id;
1917       for (var n=0; n<selection.length; n++) {
1918         id = selection[n];
1919         a_uids[a_uids.length] = id;
1920         this.message_list.remove_row(id, (this.env.display_next && n == selection.length-1));
1921       }
1922       // make sure there are no selected rows
1923       if (!this.env.display_next)
1924         this.message_list.clear_selection();
1925     }
1926
1927     // also send search request to get the right messages 
1928     if (this.env.search_request) 
1929       add_url += '&_search='+this.env.search_request;
1930
1931     if (this.env.display_next && this.env.next_uid)
1932       add_url += '&_next_uid='+this.env.next_uid;
1933
1934     // send request to server
1935     this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
1936   };
1937
1938   // set a specific flag to one or more messages
1939   this.mark_message = function(flag, uid)
1940     {
1941     var a_uids = new Array();
1942     var r_uids = new Array();
1943     var selection = this.message_list ? this.message_list.get_selection() : new Array();
1944
1945     if (uid)
1946       a_uids[0] = uid;
1947     else if (this.env.uid)
1948       a_uids[0] = this.env.uid;
1949     else if (this.message_list)
1950       {
1951       for (var n=0; n<selection.length; n++)
1952         {
1953           a_uids[a_uids.length] = selection[n];
1954         }
1955       }
1956
1957     if (!this.message_list)
1958       r_uids = a_uids;
1959     else
1960       for (var id, n=0; n<a_uids.length; n++)
1961       {
1962         id = a_uids[n];
1963         if ((flag=='read' && this.message_list.rows[id].unread) 
1964             || (flag=='unread' && !this.message_list.rows[id].unread)
1965             || (flag=='delete' && !this.message_list.rows[id].deleted)
1966             || (flag=='undelete' && this.message_list.rows[id].deleted)
1967             || (flag=='flagged' && !this.message_list.rows[id].flagged)
1968             || (flag=='unflagged' && this.message_list.rows[id].flagged))
1969         {
1970           r_uids[r_uids.length] = id;
1971         }
1972       }
1973
1974     // nothing to do
1975     if (!r_uids.length)
1976       return;
1977
1978     switch (flag)
1979       {
1980         case 'read':
1981         case 'unread':
1982           this.toggle_read_status(flag, r_uids);
1983           break;
1984         case 'delete':
1985         case 'undelete':
1986           this.toggle_delete_status(r_uids);
1987           break;
1988         case 'flagged':
1989         case 'unflagged':
1990           this.toggle_flagged_status(flag, a_uids);
1991           break;
1992       }
1993     };
1994
1995   // set class to read/unread
1996   this.toggle_read_status = function(flag, a_uids)
1997   {
1998     // mark all message rows as read/unread
1999     for (var i=0; i<a_uids.length; i++)
2000       this.set_message(a_uids[i], 'unread', (flag=='unread' ? true : false));
2001
2002     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
2003   };
2004
2005   // set image to flagged or unflagged
2006   this.toggle_flagged_status = function(flag, a_uids)
2007   {
2008     // mark all message rows as flagged/unflagged
2009     for (var i=0; i<a_uids.length; i++)
2010       this.set_message(a_uids[i], 'flagged', (flag=='flagged' ? true : false));
2011
2012     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
2013   };
2014   
2015   // mark all message rows as deleted/undeleted
2016   this.toggle_delete_status = function(a_uids)
2017   {
2018     var rows = this.message_list ? this.message_list.rows : new Array();
2019     
2020     if (a_uids.length==1)
2021     {
2022       if (!rows.length || (rows[a_uids[0]] && !rows[a_uids[0]].deleted))
2023         this.flag_as_deleted(a_uids);
2024       else
2025         this.flag_as_undeleted(a_uids);
2026
2027       return true;
2028     }
2029     
2030     var all_deleted = true;
2031     for (var i=0; i<a_uids.length; i++)
2032     {
2033       uid = a_uids[i];
2034       if (rows[uid]) {
2035         if (!rows[uid].deleted)
2036         {
2037           all_deleted = false;
2038           break;
2039         }
2040       }
2041     }
2042     
2043     if (all_deleted)
2044       this.flag_as_undeleted(a_uids);
2045     else
2046       this.flag_as_deleted(a_uids);
2047     
2048     return true;
2049   };
2050
2051   this.flag_as_undeleted = function(a_uids)
2052   {
2053     for (var i=0; i<a_uids.length; i++)
2054       this.set_message(a_uids[i], 'deleted', false);
2055
2056     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
2057     return true;
2058   };
2059
2060   this.flag_as_deleted = function(a_uids)
2061   {
2062     var add_url = '';
2063     var r_uids = new Array();
2064     var rows = this.message_list ? this.message_list.rows : new Array();
2065     
2066     for (var i=0; i<a_uids.length; i++)
2067       {
2068       uid = a_uids[i];
2069       if (rows[uid])
2070         {
2071         if (rows[uid].unread)
2072           r_uids[r_uids.length] = uid;
2073
2074         if (this.env.skip_deleted)
2075           this.message_list.remove_row(uid, (this.env.display_next && i == this.message_list.selection.length-1));
2076         else
2077           this.set_message(uid, 'deleted', true);
2078         }
2079       }
2080
2081     // make sure there are no selected rows
2082     if (this.env.skip_deleted && !this.env.display_next && this.message_list)
2083       this.message_list.clear_selection();
2084
2085     add_url = '&_from='+(this.env.action ? this.env.action : '');
2086     
2087     if (r_uids.length)
2088       add_url += '&_ruid='+r_uids.join(',');
2089
2090     if (this.env.skip_deleted) {
2091       // also send search request to get the right messages 
2092       if (this.env.search_request) 
2093         add_url += '&_search='+this.env.search_request;
2094       if (this.env.display_next && this.env.next_uid)
2095         add_url += '&_next_uid='+this.env.next_uid;
2096     }
2097     
2098     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete'+add_url);
2099     return true;  
2100   };
2101
2102   // flag as read without mark request (called from backend)
2103   // argument should be a coma-separated list of uids
2104   this.flag_deleted_as_read = function(uids)
2105   {
2106     var icn_src;
2107     var rows = this.message_list ? this.message_list.rows : new Array();
2108     var str = String(uids);
2109     var a_uids = new Array();
2110
2111     a_uids = str.split(',');
2112
2113     for (var uid, i=0; i<a_uids.length; i++)
2114       {
2115       uid = a_uids[i];
2116       if (rows[uid])
2117         this.set_message(uid, 'unread', false);
2118       }
2119   };
2120   
2121   
2122   /*********************************************************/
2123   /*********           login form methods          *********/
2124   /*********************************************************/
2125
2126   // handler for keyboard events on the _user field
2127   this.login_user_keyup = function(e)
2128   {
2129     var key = rcube_event.get_keycode(e);
2130     var passwd = $('#rcmloginpwd');
2131
2132     // enter
2133     if (key == 13 && passwd.length && !passwd.val()) {
2134       passwd.focus();
2135       return rcube_event.cancel(e);
2136     }
2137     
2138     return true;
2139   };
2140
2141
2142   /*********************************************************/
2143   /*********        message compose methods        *********/
2144   /*********************************************************/
2145   
2146   // checks the input fields before sending a message
2147   this.check_compose_input = function()
2148     {
2149     // check input fields
2150     var input_to = $("[name='_to']");
2151     var input_cc = $("[name='_cc']");
2152     var input_bcc = $("[name='_bcc']");
2153     var input_from = $("[name='_from']");
2154     var input_subject = $("[name='_subject']");
2155     var input_message = $("[name='_message']");
2156
2157     // check sender (if have no identities)
2158     if (input_from.attr('type') == 'text' && !rcube_check_email(input_from.val(), true))
2159       {
2160       alert(this.get_label('nosenderwarning'));
2161       input_from.focus();
2162       return false;
2163       }
2164
2165     // check for empty recipient
2166     var recipients = input_to.val() ? input_to.val() : (input_cc.val() ? input_cc.val() : input_bcc.val());
2167     if (!rcube_check_email(recipients.replace(/^\s+/, '').replace(/[\s,;]+$/, ''), true))
2168       {
2169       alert(this.get_label('norecipientwarning'));
2170       input_to.focus();
2171       return false;
2172       }
2173
2174     // display localized warning for missing subject
2175     if (input_subject.val() == '')
2176       {
2177       var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
2178
2179       // user hit cancel, so don't send
2180       if (!subject && subject !== '')
2181         {
2182         input_subject.focus();
2183         return false;
2184         }
2185       else
2186         {
2187         input_subject.val((subject ? subject : this.get_label('nosubject')));
2188         }
2189       }
2190
2191     // check for empty body
2192     if ((!window.tinyMCE || !tinyMCE.get(this.env.composebody))
2193         && input_message.val() == '' && !confirm(this.get_label('nobodywarning')))
2194       {
2195       input_message.focus();
2196       return false;
2197       }
2198     else if (window.tinyMCE && tinyMCE.get(this.env.composebody)
2199         && !tinyMCE.get(this.env.composebody).getContent()
2200         && !confirm(this.get_label('nobodywarning')))
2201       {
2202       tinyMCE.get(this.env.composebody).focus();
2203       return false;
2204       }
2205
2206     // Apply spellcheck changes if spell checker is active
2207     this.stop_spellchecking();
2208
2209     // move body from html editor to textarea (just to be sure, #1485860)
2210     if (window.tinyMCE && tinyMCE.get(this.env.composebody))
2211       tinyMCE.triggerSave();
2212
2213     return true;
2214     };
2215
2216   this.stop_spellchecking = function()
2217     {
2218     if (this.env.spellcheck && !this.spellcheck_ready) {
2219       $(this.env.spellcheck.spell_span).trigger('click');
2220       this.set_spellcheck_state('ready');
2221       }
2222     };
2223
2224   this.display_spellcheck_controls = function(vis)
2225     {
2226     if (this.env.spellcheck) {
2227       // stop spellchecking process
2228       if (!vis)
2229         this.stop_spellchecking();
2230
2231       $(this.env.spellcheck.spell_container).css('visibility', vis ? 'visible' : 'hidden');
2232       }
2233     };
2234
2235   this.set_spellcheck_state = function(s)
2236     {
2237     this.spellcheck_ready = (s == 'ready' || s == 'no_error_found');
2238     this.enable_command('spellcheck', this.spellcheck_ready);
2239     };
2240
2241   this.set_draft_id = function(id)
2242     {
2243     $("input[name='_draft_saveid']").val(id);
2244     };
2245
2246   this.auto_save_start = function()
2247     {
2248     if (this.env.draft_autosave)
2249       this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
2250
2251     // Unlock interface now that saving is complete
2252     this.busy = false;
2253     };
2254
2255   this.compose_field_hash = function(save)
2256     {
2257     // check input fields
2258     var value_to = $("[name='_to']").val();
2259     var value_cc = $("[name='_cc']").val();
2260     var value_bcc = $("[name='_bcc']").val();
2261     var value_subject = $("[name='_subject']").val();
2262     var str = '';
2263     
2264     if (value_to)
2265       str += value_to+':';
2266     if (value_cc)
2267       str += value_cc+':';
2268     if (value_bcc)
2269       str += value_bcc+':';
2270     if (value_subject)
2271       str += value_subject+':';
2272     
2273     var editor = tinyMCE.get(this.env.composebody);
2274     if (editor)
2275       str += editor.getContent();
2276     else
2277       str += $("[name='_message']").val();
2278     
2279     if (save)
2280       this.cmp_hash = str;
2281     
2282     return str;
2283     };
2284     
2285   this.change_identity = function(obj)
2286     {
2287     if (!obj || !obj.options)
2288       return false;
2289
2290     var id = obj.options[obj.selectedIndex].value;
2291     var input_message = $("[name='_message']");
2292     var message = input_message.val();
2293     var is_html = ($("input[name='_is_html']").val() == '1');
2294     var sig, p, len;
2295
2296     if (!this.env.identity)
2297       this.env.identity = id
2298   
2299     if (!is_html)
2300       {
2301       // remove the 'old' signature
2302       if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
2303         {
2304         if (this.env.signatures[this.env.identity]['is_html'])
2305           sig = this.env.signatures[this.env.identity]['plain_text'];
2306         else
2307           sig = this.env.signatures[this.env.identity]['text'];
2308         
2309         if (sig.indexOf('-- ')!=0)
2310           sig = '-- \n'+sig;
2311
2312         p = message.lastIndexOf(sig);
2313         if (p>=0)
2314           message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
2315         }
2316
2317       message = message.replace(/[\r\n]+$/, '');
2318       len = message.length;
2319
2320       // add the new signature string
2321       if (this.env.signatures && this.env.signatures[id])
2322         {
2323         sig = this.env.signatures[id]['text'];
2324         if (this.env.signatures[id]['is_html'])
2325           {
2326           sig = this.env.signatures[id]['plain_text'];
2327           }
2328         if (sig.indexOf('-- ')!=0)
2329           sig = '-- \n'+sig;
2330         message += '\n\n'+sig;
2331         if (len) len += 1;
2332         }
2333       }
2334     else
2335       {
2336       var editor = tinyMCE.get(this.env.composebody);
2337
2338       if (this.env.signatures)
2339         {
2340         // Append the signature as a div within the body
2341         var sigElem = editor.dom.get('_rc_sig');
2342         var newsig = '';
2343         var htmlsig = true;
2344
2345         if (!sigElem)
2346           {
2347           // add empty line before signature on IE
2348           if (bw.ie)
2349             editor.getBody().appendChild(editor.getDoc().createElement('br'));
2350
2351           sigElem = editor.getDoc().createElement('div');
2352           sigElem.setAttribute('id', '_rc_sig');
2353           editor.getBody().appendChild(sigElem);
2354           }
2355
2356         if (this.env.signatures[id])
2357         {
2358           newsig = this.env.signatures[id]['text'];
2359           htmlsig = this.env.signatures[id]['is_html'];
2360
2361           if (newsig) {
2362             if (htmlsig && this.env.signatures[id]['plain_text'].indexOf('-- ')!=0)
2363               newsig = '<p>-- </p>' + newsig;
2364             else if (!htmlsig && newsig.indexOf('-- ')!=0)
2365               newsig = '-- \n' + newsig;
2366           }
2367         }
2368
2369         if (htmlsig)
2370           sigElem.innerHTML = newsig;
2371         else
2372           sigElem.innerHTML = '<pre>' + newsig + '</pre>';
2373         }
2374       }
2375
2376     input_message.val(message);
2377
2378     // move cursor before the signature
2379     if (!is_html)
2380       this.set_caret_pos(input_message.get(0), len);
2381
2382     this.env.identity = id;
2383     return true;
2384     };
2385
2386   this.show_attachment_form = function(a)
2387     {
2388     if (!this.gui_objects.uploadbox)
2389       return false;
2390       
2391     var elm, list;
2392     if (elm = this.gui_objects.uploadbox)
2393       {
2394       if (a && (list = this.gui_objects.attachmentlist))
2395         {
2396         var pos = $(list).offset();
2397         elm.style.top = (pos.top + list.offsetHeight + 10) + 'px';
2398         elm.style.left = pos.left + 'px';
2399         }
2400       
2401       elm.style.visibility = a ? 'visible' : 'hidden';
2402       }
2403       
2404     // clear upload form
2405     try {
2406       if (!a && this.gui_objects.attachmentform != this.gui_objects.messageform)
2407         this.gui_objects.attachmentform.reset();
2408     }
2409     catch(e){}  // ignore errors
2410     
2411     return true;
2412     };
2413
2414   // upload attachment file
2415   this.upload_file = function(form)
2416     {
2417     if (!form)
2418       return false;
2419       
2420     // get file input fields
2421     var send = false;
2422     for (var n=0; n<form.elements.length; n++)
2423       if (form.elements[n].type=='file' && form.elements[n].value)
2424         {
2425         send = true;
2426         break;
2427         }
2428     
2429     // create hidden iframe and post upload form
2430     if (send)
2431       {
2432       var ts = new Date().getTime();
2433       var frame_name = 'rcmupload'+ts;
2434
2435       // have to do it this way for IE
2436       // otherwise the form will be posted to a new window
2437       if(document.all)
2438         {
2439         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
2440         document.body.insertAdjacentHTML('BeforeEnd',html);
2441         }
2442       else  // for standards-compilant browsers
2443         {
2444         var frame = document.createElement('iframe');
2445         frame.name = frame_name;
2446         frame.style.border = 'none';
2447         frame.style.width = 0;
2448         frame.style.height = 0;
2449         frame.style.visibility = 'hidden';
2450         document.body.appendChild(frame);
2451         }
2452
2453       form.target = frame_name;
2454       form.action = this.env.comm_path+'&_action=upload';
2455       form.setAttribute('enctype', 'multipart/form-data');
2456       form.submit();
2457       }
2458     
2459     // set reference to the form object
2460     this.gui_objects.attachmentform = form;
2461     return true;
2462     };
2463
2464   // add file name to attachment list
2465   // called from upload page
2466   this.add2attachment_list = function(name, content)
2467     {
2468     if (!this.gui_objects.attachmentlist)
2469       return false;
2470       
2471     $('<li>').attr('id', name).html(content).appendTo(this.gui_objects.attachmentlist);
2472     return true;
2473     };
2474
2475   this.remove_from_attachment_list = function(name)
2476     {
2477     if (!this.gui_objects.attachmentlist)
2478       return false;
2479
2480     var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
2481     for (i=0;i<list.length;i++)
2482       if (list[i].id == name)
2483         this.gui_objects.attachmentlist.removeChild(list[i]);
2484     };
2485
2486   this.remove_attachment = function(name)
2487     {
2488     if (name)
2489       this.http_post('remove-attachment', '_file='+urlencode(name));
2490
2491     return true;
2492     };
2493
2494   // send remote request to add a new contact
2495   this.add_contact = function(value)
2496     {
2497     if (value)
2498       this.http_post('addcontact', '_address='+value);
2499     
2500     return true;
2501     };
2502
2503   // send remote request to search mail or contacts
2504   this.qsearch = function(value)
2505     {
2506     if (value != '')
2507       {
2508       var addurl = '';
2509       if (this.message_list) {
2510         this.message_list.clear();
2511         if (this.env.search_mods) {
2512           var head_arr = new Array();
2513           for (var n in this.env.search_mods)
2514             head_arr.push(n);
2515           addurl += '&_headers='+head_arr.join(',');
2516           }
2517         } else if (this.contact_list) {
2518         this.contact_list.clear(true);
2519         this.show_contentframe(false);
2520         }
2521
2522       if (this.gui_objects.search_filter)
2523         addurl += '&_filter=' + this.gui_objects.search_filter.value;
2524
2525       // reset vars
2526       this.env.current_page = 1;
2527       this.set_busy(true, 'searching');
2528       this.http_request('search', '_q='+urlencode(value)
2529         + (this.env.mailbox ? '&_mbox='+urlencode(this.env.mailbox) : '')
2530         + (this.env.source ? '&_source='+urlencode(this.env.source) : '')
2531         + (addurl ? addurl : ''), true);
2532       }
2533     return true;
2534     };
2535
2536   // reset quick-search form
2537   this.reset_qsearch = function()
2538     {
2539     if (this.gui_objects.qsearchbox)
2540       this.gui_objects.qsearchbox.value = '';
2541       
2542     this.env.search_request = null;
2543     return true;
2544     };
2545
2546   this.sent_successfully = function(type, msg)
2547     {
2548     this.list_mailbox();
2549     this.display_message(msg, type, true);
2550     }
2551
2552
2553   /*********************************************************/
2554   /*********     keyboard live-search methods      *********/
2555   /*********************************************************/
2556
2557   // handler for keyboard events on address-fields
2558   this.ksearch_keypress = function(e, obj)
2559   {
2560     if (this.ksearch_timer)
2561       clearTimeout(this.ksearch_timer);
2562
2563     var highlight;
2564     var key = rcube_event.get_keycode(e);
2565     var mod = rcube_event.get_modifier(e);
2566
2567     switch (key)
2568       {
2569       case 38:  // key up
2570       case 40:  // key down
2571         if (!this.ksearch_pane)
2572           break;
2573           
2574         var dir = key==38 ? 1 : 0;
2575         
2576         highlight = document.getElementById('rcmksearchSelected');
2577         if (!highlight)
2578           highlight = this.ksearch_pane.__ul.firstChild;
2579         
2580         if (highlight)
2581           this.ksearch_select(dir ? highlight.previousSibling : highlight.nextSibling);
2582
2583         return rcube_event.cancel(e);
2584
2585       case 9:  // tab
2586         if(mod == SHIFT_KEY)
2587           break;
2588
2589       case 13:  // enter
2590         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2591           break;
2592
2593         // insert selected address and hide ksearch pane
2594         this.insert_recipient(this.ksearch_selected);
2595         this.ksearch_hide();
2596
2597         return rcube_event.cancel(e);
2598
2599       case 27:  // escape
2600         this.ksearch_hide();
2601         break;
2602       
2603       case 37:  // left
2604       case 39:  // right
2605         if (mod != SHIFT_KEY)
2606           return;
2607       }
2608
2609     // start timer
2610     this.ksearch_timer = window.setTimeout(function(){ ref.ksearch_get_results(); }, 200);
2611     this.ksearch_input = obj;
2612     
2613     return true;
2614   };
2615   
2616   this.ksearch_select = function(node)
2617   {
2618     var current = $('#rcmksearchSelected');
2619     if (current[0] && node) {
2620       current.removeAttr('id').removeClass('selected');
2621     }
2622
2623     if (node) {
2624       $(node).attr('id', 'rcmksearchSelected').addClass('selected');
2625       this.ksearch_selected = node._rcm_id;
2626     }
2627   };
2628
2629   this.insert_recipient = function(id)
2630   {
2631     if (!this.env.contacts[id] || !this.ksearch_input)
2632       return;
2633     
2634     // get cursor pos
2635     var inp_value = this.ksearch_input.value.toLowerCase();
2636     var cpos = this.get_caret_pos(this.ksearch_input);
2637     var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2638
2639     // replace search string with full address
2640     var pre = this.ksearch_input.value.substring(0, p);
2641     var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2642     var insert  = this.env.contacts[id]+', ';
2643     this.ksearch_input.value = pre + insert + end;
2644
2645     // set caret to insert pos
2646     cpos = p+insert.length;
2647     if (this.ksearch_input.setSelectionRange)
2648       this.ksearch_input.setSelectionRange(cpos, cpos);
2649   };
2650
2651   // address search processor
2652   this.ksearch_get_results = function()
2653   {
2654     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2655     if (inp_value === null)
2656       return;
2657       
2658     if (this.ksearch_pane && this.ksearch_pane.is(":visible"))
2659       this.ksearch_pane.hide();
2660
2661     // get string from current cursor pos to last comma
2662     var cpos = this.get_caret_pos(this.ksearch_input);
2663     var p = inp_value.lastIndexOf(',', cpos-1);
2664     var q = inp_value.substring(p+1, cpos);
2665
2666     // trim query string
2667     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2668
2669     // Don't (re-)search if the last results are still active
2670     if (q == this.ksearch_value)
2671       return;
2672     
2673     var old_value = this.ksearch_value;
2674     this.ksearch_value = q;
2675     
2676     // ...string is empty
2677     if (!q.length)
2678       return;
2679
2680     // ...new search value contains old one and previous search result was empty
2681     if (old_value && old_value.length && this.env.contacts && !this.env.contacts.length && q.indexOf(old_value) == 0)
2682       return;
2683     
2684     this.display_message(this.get_label('searching'), 'loading', true);
2685     this.http_post('autocomplete', '_search='+urlencode(q));
2686   };
2687
2688   this.ksearch_query_results = function(results, search)
2689   {
2690     // ignore this outdated search response
2691     if (this.ksearch_value && search != this.ksearch_value)
2692       return;
2693       
2694     this.hide_message();
2695     this.env.contacts = results ? results : [];
2696     this.ksearch_display_results(this.env.contacts);
2697   };
2698
2699   this.ksearch_display_results = function (a_results)
2700   {
2701     // display search results
2702     if (a_results.length && this.ksearch_input) {
2703       var p, ul, li;
2704       
2705       // create results pane if not present
2706       if (!this.ksearch_pane) {
2707         ul = $('<ul>');
2708         this.ksearch_pane = $('<div>').attr('id', 'rcmKSearchpane').css({ position:'absolute', 'z-index':30000 }).append(ul).appendTo(document.body);
2709         this.ksearch_pane.__ul = ul[0];
2710       }
2711
2712       // remove all search results
2713       ul = this.ksearch_pane.__ul;
2714       ul.innerHTML = '';
2715             
2716       // add each result line to list
2717       for (i=0; i<a_results.length; i++) {
2718         li = document.createElement('LI');
2719         li.innerHTML = a_results[i].replace(new RegExp('('+this.ksearch_value+')', 'ig'), '##$1%%').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/##([^%]+)%%/g, '<b>$1</b>');
2720         li.onmouseover = function(){ ref.ksearch_select(this); };
2721         li.onmouseup = function(){ ref.ksearch_click(this) };
2722         li._rcm_id = i;
2723         ul.appendChild(li);
2724       }
2725
2726       // select the first
2727       $(ul.firstChild).attr('id', 'rcmksearchSelected').addClass('selected');
2728       this.ksearch_selected = 0;
2729
2730       // move the results pane right under the input box and make it visible
2731       var pos = $(this.ksearch_input).offset();
2732       this.ksearch_pane.css({ left:pos.left+'px', top:(pos.top + this.ksearch_input.offsetHeight)+'px' }).show();
2733     }
2734     // hide results pane
2735     else
2736       this.ksearch_hide();
2737   };
2738   
2739   this.ksearch_click = function(node)
2740   {
2741     if (this.ksearch_input)
2742       this.ksearch_input.focus();
2743
2744     this.insert_recipient(node._rcm_id);
2745     this.ksearch_hide();
2746   };
2747
2748   this.ksearch_blur = function()
2749     {
2750     if (this.ksearch_timer)
2751       clearTimeout(this.ksearch_timer);
2752
2753     this.ksearch_value = '';
2754     this.ksearch_input = null;
2755     
2756     this.ksearch_hide();
2757     };
2758
2759
2760   this.ksearch_hide = function()
2761     {
2762     this.ksearch_selected = null;
2763     
2764     if (this.ksearch_pane)
2765       this.ksearch_pane.hide();
2766     };
2767
2768
2769   /*********************************************************/
2770   /*********         address book methods          *********/
2771   /*********************************************************/
2772
2773   this.contactlist_keypress = function(list)
2774     {
2775       if (list.key_pressed == list.DELETE_KEY)
2776         this.command('delete');
2777     };
2778
2779   this.contactlist_select = function(list)
2780     {
2781       if (this.preview_timer)
2782         clearTimeout(this.preview_timer);
2783
2784       var id, frame, ref = this;
2785       if (id = list.get_single_selection())
2786         this.preview_timer = window.setTimeout(function(){ ref.load_contact(id, 'show'); }, 200);
2787       else if (this.env.contentframe)
2788         this.show_contentframe(false);
2789
2790       this.enable_command('compose', list.selection.length > 0);
2791       this.enable_command('edit', (id && this.env.address_sources && !this.env.address_sources[this.env.source].readonly) ? true : false);
2792       this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
2793
2794       return false;
2795     };
2796
2797   this.list_contacts = function(src, page)
2798     {
2799     var add_url = '';
2800     var target = window;
2801     
2802     if (!src)
2803       src = this.env.source;
2804     
2805     if (page && this.current_page==page && src == this.env.source)
2806       return false;
2807       
2808     if (src != this.env.source)
2809       {
2810       page = 1;
2811       this.env.current_page = page;
2812       this.reset_qsearch();
2813       }
2814
2815     this.select_folder(src, this.env.source);
2816     this.env.source = src;
2817
2818     // load contacts remotely
2819     if (this.gui_objects.contactslist)
2820       {
2821       this.list_contacts_remote(src, page);
2822       return;
2823       }
2824
2825     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2826       {
2827       target = window.frames[this.env.contentframe];
2828       add_url = '&_framed=1';
2829       }
2830
2831     // also send search request to get the correct listing
2832     if (this.env.search_request)
2833       add_url += '&_search='+this.env.search_request;
2834
2835     this.set_busy(true, 'loading');
2836     target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
2837     };
2838
2839   // send remote request to load contacts list
2840   this.list_contacts_remote = function(src, page)
2841     {
2842     // clear message list first
2843     this.contact_list.clear(true);
2844     this.show_contentframe(false);
2845     this.enable_command('delete', 'compose', false);
2846
2847     // send request to server
2848     var url = (src ? '_source='+urlencode(src) : '') + (page ? (src?'&':'') + '_page='+page : '');
2849     this.env.source = src;
2850     
2851     // also send search request to get the right messages 
2852     if (this.env.search_request) 
2853       url += '&_search='+this.env.search_request;
2854
2855     this.set_busy(true, 'loading');
2856     this.http_request('list', url, true);
2857     };
2858
2859   // load contact record
2860   this.load_contact = function(cid, action, framed)
2861     {
2862     var add_url = '';
2863     var target = window;
2864     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2865       {
2866       add_url = '&_framed=1';
2867       target = window.frames[this.env.contentframe];
2868       this.show_contentframe(true);
2869       }
2870     else if (framed)
2871       return false;
2872       
2873     if (action && (cid || action=='add') && !this.drag_active)
2874       {
2875       this.set_busy(true);
2876       target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
2877       }
2878     return true;
2879     };
2880
2881   // copy a contact to the specified target (group or directory)
2882   this.copy_contact = function(cid, to)
2883     {
2884     if (!cid)
2885       cid = this.contact_list.get_selection().join(',');
2886
2887     if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2888       this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
2889     };
2890
2891
2892   this.delete_contacts = function()
2893     {
2894     // exit if no mailbox specified or if selection is empty
2895     var selection = this.contact_list.get_selection();
2896     if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
2897       return;
2898       
2899     var a_cids = new Array();
2900     var qs = '';
2901
2902     if (this.env.cid)
2903       a_cids[a_cids.length] = this.env.cid;
2904     else
2905       {
2906       var id;
2907       for (var n=0; n<selection.length; n++)
2908         {
2909         id = selection[n];
2910         a_cids[a_cids.length] = id;
2911         this.contact_list.remove_row(id, (n == selection.length-1));
2912         }
2913
2914       // hide content frame if we delete the currently displayed contact
2915       if (selection.length == 1)
2916         this.show_contentframe(false);
2917       }
2918
2919     // also send search request to get the right records from the next page
2920     if (this.env.search_request) 
2921       qs += '&_search='+this.env.search_request;
2922
2923     // send request to server
2924     this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source)+'&_from='+(this.env.action ? this.env.action : '')+qs);
2925     return true;
2926     };
2927
2928   // update a contact record in the list
2929   this.update_contact_row = function(cid, cols_arr, newcid)
2930   {
2931     var row;
2932     if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj)) {
2933       for (var c=0; c<cols_arr.length; c++)
2934         if (row.cells[c])
2935           $(row.cells[c]).html(cols_arr[c]);
2936
2937       // cid change
2938       if (newcid) {
2939         row.id = 'rcmrow' + newcid;
2940         this.contact_list.remove_row(cid);
2941         this.contact_list.init_row(row);
2942         this.contact_list.selection[0] = newcid;
2943         row.style.display = '';
2944       }
2945
2946       return true;
2947     }
2948
2949     return false;
2950   };
2951
2952   // add row to contacts list
2953   this.add_contact_row = function(cid, cols, select)
2954     {
2955     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
2956       return false;
2957     
2958     var tbody = this.gui_objects.contactslist.tBodies[0];
2959     var rowcount = tbody.rows.length;
2960     var even = rowcount%2;
2961     
2962     var row = document.createElement('tr');
2963     row.id = 'rcmrow'+cid;
2964     row.className = 'contact '+(even ? 'even' : 'odd');
2965             
2966     if (this.contact_list.in_selection(cid))
2967       row.className += ' selected';
2968
2969     // add each submitted col
2970     for (var c in cols) {
2971       col = document.createElement('td');
2972       col.className = String(c).toLowerCase();
2973       col.innerHTML = cols[c];
2974       row.appendChild(col);
2975     }
2976     
2977     this.contact_list.insert_row(row);
2978     
2979     this.enable_command('export', (this.contact_list.rowcount > 0));
2980     };
2981
2982
2983   /*********************************************************/
2984   /*********        user settings methods          *********/
2985   /*********************************************************/
2986
2987   this.init_subscription_list = function()
2988     {
2989     var p = this;
2990     this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist, {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
2991     this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
2992     this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
2993     this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
2994     this.subscription_list.row_init = function (row)
2995       {
2996       var anchors = row.obj.getElementsByTagName('a');
2997       if (anchors[0])
2998         anchors[0].onclick = function() { p.rename_folder(row.id); return false; };
2999       if (anchors[1])
3000         anchors[1].onclick = function() { p.delete_folder(row.id); return false; };
3001       row.obj.onmouseover = function() { p.focus_subscription(row.id); };
3002       row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
3003       }
3004     this.subscription_list.init();
3005     }
3006
3007   // preferences section select and load options frame
3008   this.section_select = function(list)
3009     {
3010     var id = list.get_single_selection();
3011     
3012     if (id) {
3013       var add_url = '';
3014       var target = window;
3015       this.set_busy(true);
3016
3017       if (this.env.contentframe && window.frames && window.frames[this.env.contentframe]) {
3018         add_url = '&_framed=1';
3019         target = window.frames[this.env.contentframe];
3020         }
3021
3022       target.location.href = this.env.comm_path+'&_action=edit-prefs&_section='+id+add_url;
3023       }
3024
3025     return true;
3026     };
3027
3028   this.identity_select = function(list)
3029     {
3030     var id;
3031     if (id = list.get_single_selection())
3032       this.load_identity(id, 'edit-identity');
3033     };
3034
3035   // load identity record
3036   this.load_identity = function(id, action)
3037     {
3038     if (action=='edit-identity' && (!id || id==this.env.iid))
3039       return false;
3040
3041     var add_url = '';
3042     var target = window;
3043     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
3044       {
3045       add_url = '&_framed=1';
3046       target = window.frames[this.env.contentframe];
3047       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
3048       }
3049
3050     if (action && (id || action=='add-identity'))
3051       {
3052       this.set_busy(true);
3053       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
3054       }
3055     return true;
3056     };
3057
3058   this.delete_identity = function(id)
3059     {
3060     // exit if no mailbox specified or if selection is empty
3061     var selection = this.identity_list.get_selection();
3062     if (!(selection.length || this.env.iid))
3063       return;
3064     
3065     if (!id)
3066       id = this.env.iid ? this.env.iid : selection[0];
3067
3068     // append token to request
3069     this.goto_url('delete-identity', '_iid='+id+'&_token='+this.env.request_token, true);
3070     
3071     return true;
3072     };
3073
3074   this.focus_subscription = function(id)
3075     {
3076     var row, folder;
3077     var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3078
3079     if (this.drag_active && this.env.folder && (row = document.getElementById(id)))
3080       if (this.env.subscriptionrows[id] &&
3081           (folder = this.env.subscriptionrows[id][0]))
3082         {
3083         if (this.check_droptarget(folder) &&
3084             !this.env.subscriptionrows[this.get_folder_row_id(this.env.folder)][2] &&
3085             (folder != this.env.folder.replace(reg, '')) &&
3086             (!folder.match(new RegExp('^'+RegExp.escape(this.env.folder+this.env.delimiter)))))
3087           {
3088           this.set_env('dstfolder', folder);
3089           $(row).addClass('droptarget');
3090           }
3091         }
3092       else if (this.env.folder.match(new RegExp(RegExp.escape(this.env.delimiter))))
3093         {
3094         this.set_env('dstfolder', this.env.delimiter);
3095         $(this.subscription_list.frame).addClass('droptarget');
3096         }
3097     }
3098
3099   this.unfocus_subscription = function(id)
3100     {
3101       var row = $('#'+id);
3102       this.set_env('dstfolder', null);
3103       if (this.env.subscriptionrows[id] && row[0])
3104         row.removeClass('droptarget');
3105       else
3106         $(this.subscription_list.frame).removeClass('droptarget');
3107     }
3108
3109   this.subscription_select = function(list)
3110     {
3111     var id, folder;
3112     if ((id = list.get_single_selection()) &&
3113         this.env.subscriptionrows['rcmrow'+id] &&
3114         (folder = this.env.subscriptionrows['rcmrow'+id][0]))
3115       this.set_env('folder', folder);
3116     else
3117       this.set_env('folder', null);
3118       
3119     if (this.gui_objects.createfolderhint)
3120       $(this.gui_objects.createfolderhint).html(this.env.folder ? this.get_label('addsubfolderhint') : '');
3121     };
3122
3123   this.subscription_move_folder = function(list)
3124     {
3125     var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3126     if (this.env.folder && this.env.dstfolder && (this.env.dstfolder != this.env.folder) &&
3127         (this.env.dstfolder != this.env.folder.replace(reg, '')))
3128       {
3129       var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
3130       var basename = this.env.folder.replace(reg, '');
3131       var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
3132
3133       this.set_busy(true, 'foldermoving');
3134       this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname), true);
3135       }
3136     this.drag_active = false;
3137     this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
3138     };
3139
3140   // tell server to create and subscribe a new mailbox
3141   this.create_folder = function(name)
3142     {
3143     if (this.edit_folder)
3144       this.reset_folder_rename();
3145
3146     var form;
3147     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
3148       {
3149       name = form.elements['_folder_name'].value;
3150
3151       if (name.indexOf(this.env.delimiter)>=0)
3152         {
3153         alert(this.get_label('forbiddencharacter')+' ('+this.env.delimiter+')');
3154         return false;
3155         }
3156
3157       if (this.env.folder && name != '')
3158         name = this.env.folder+this.env.delimiter+name;
3159
3160       this.set_busy(true, 'foldercreating');
3161       this.http_post('create-folder', '_name='+urlencode(name), true);
3162       }
3163     else if (form.elements['_folder_name'])
3164       form.elements['_folder_name'].focus();
3165     };
3166
3167   // start renaming the mailbox name.
3168   // this will replace the name string with an input field
3169   this.rename_folder = function(id)
3170     {
3171     var temp, row, form;
3172
3173     // reset current renaming
3174     if (temp = this.edit_folder)
3175       {
3176       this.reset_folder_rename();
3177       if (temp == id)
3178         return;
3179       }
3180
3181     if (id && this.env.subscriptionrows[id] && (row = document.getElementById(id)))
3182       {
3183       var reg = new RegExp('.*['+RegExp.escape(this.env.delimiter)+']');
3184       this.name_input = document.createElement('input');
3185       this.name_input.type = 'text';
3186       this.name_input.value = this.env.subscriptionrows[id][0].replace(reg, '');
3187
3188       reg = new RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3189       this.name_input.__parent = this.env.subscriptionrows[id][0].replace(reg, '');
3190       this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
3191       
3192       row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
3193       this.edit_folder = id;
3194       this.name_input.select();
3195       
3196       if (form = this.gui_objects.editform)
3197         form.onsubmit = function(){ return false; };
3198       }
3199     };
3200
3201   // remove the input field and write the current mailbox name to the table cell
3202   this.reset_folder_rename = function()
3203     {
3204     var cell = this.name_input ? this.name_input.parentNode : null;
3205
3206     if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
3207       $(cell).html(this.env.subscriptionrows[this.edit_folder][1]);
3208       
3209     this.edit_folder = null;
3210     };
3211
3212   // handler for keyboard events on the input field
3213   this.name_input_keypress = function(e)
3214     {
3215     var key = rcube_event.get_keycode(e);
3216
3217     // enter
3218     if (key==13)
3219       {
3220       var newname = this.name_input ? this.name_input.value : null;
3221       if (this.edit_folder && newname)
3222         {
3223         if (newname.indexOf(this.env.delimiter)>=0)
3224           {
3225           alert(this.get_label('forbiddencharacter')+' ('+this.env.delimiter+')');
3226           return false;
3227           }
3228
3229         if (this.name_input.__parent)
3230           newname = this.name_input.__parent + this.env.delimiter + newname;
3231
3232         this.set_busy(true, 'folderrenaming');
3233         this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname), true);
3234         }
3235       }
3236     // escape
3237     else if (key==27)
3238       this.reset_folder_rename();
3239     };
3240
3241   // delete a specific mailbox with all its messages
3242   this.delete_folder = function(id)
3243     {
3244     var folder = this.env.subscriptionrows[id][0];
3245
3246     if (this.edit_folder)
3247       this.reset_folder_rename();
3248
3249     if (folder && confirm(this.get_label('deletefolderconfirm')))
3250       {
3251       this.set_busy(true, 'folderdeleting');
3252       this.http_post('delete-folder', '_mboxes='+urlencode(folder), true);
3253       this.set_env('folder', null);
3254
3255       $(this.gui_objects.createfolderhint).html('');
3256       }
3257     };
3258
3259   // add a new folder to the subscription list by cloning a folder row
3260   this.add_folder_row = function(name, display_name, replace, before)
3261     {
3262     if (!this.gui_objects.subscriptionlist)
3263       return false;
3264
3265     // find not protected folder    
3266     for (var refid in this.env.subscriptionrows)
3267       if (this.env.subscriptionrows[refid]!=null && !this.env.subscriptionrows[refid][2])
3268         break;
3269
3270     var refrow, form;
3271     var tbody = this.gui_objects.subscriptionlist.tBodies[0];
3272     var id = 'rcmrow'+(tbody.childNodes.length+1);
3273     var selection = this.subscription_list.get_single_selection();
3274     
3275     if (replace && replace.id)
3276     {
3277       id = replace.id;
3278       refid = replace.id;
3279     }
3280
3281     if (!id || !(refrow = document.getElementById(refid)))
3282       {
3283       // Refresh page if we don't have a table row to clone
3284       this.goto_url('folders');
3285       }
3286     else
3287       {
3288       // clone a table row if there are existing rows
3289       var row = this.clone_table_row(refrow);
3290       row.id = id;
3291
3292       if (before && (before = this.get_folder_row_id(before)))
3293         tbody.insertBefore(row, document.getElementById(before));
3294       else
3295         tbody.appendChild(row);
3296       
3297       if (replace)
3298         tbody.removeChild(replace);
3299       }
3300
3301     // add to folder/row-ID map
3302     this.env.subscriptionrows[row.id] = [name, display_name, 0];
3303
3304     // set folder name
3305     row.cells[0].innerHTML = display_name;
3306     
3307     // set messages count to zero
3308     if (!replace)
3309       row.cells[1].innerHTML = '*';
3310     
3311     if (!replace && row.cells[2] && row.cells[2].firstChild.tagName.toLowerCase()=='input')
3312       {
3313       row.cells[2].firstChild.value = name;
3314       row.cells[2].firstChild.checked = true;
3315       }
3316     
3317     // add new folder to rename-folder list and clear input field
3318     if (!replace && (form = this.gui_objects.editform))
3319       {
3320       if (form.elements['_folder_oldname'])
3321         form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
3322       if (form.elements['_folder_name'])
3323         form.elements['_folder_name'].value = ''; 
3324       }
3325
3326     this.init_subscription_list();
3327     if (selection && document.getElementById('rcmrow'+selection))
3328       this.subscription_list.select_row(selection);
3329
3330     if (document.getElementById(id).scrollIntoView)
3331       document.getElementById(id).scrollIntoView();
3332     };
3333
3334   // replace an existing table row with a new folder line
3335   this.replace_folder_row = function(oldfolder, newfolder, display_name, before)
3336     {
3337     var id = this.get_folder_row_id(oldfolder);
3338     var row = document.getElementById(id);
3339     
3340     // replace an existing table row (if found)
3341     this.add_folder_row(newfolder, display_name, row, before);
3342     
3343     // rename folder in rename-folder dropdown
3344     var form, elm;
3345     if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
3346       {
3347       for (var i=0;i<elm.options.length;i++)
3348         {
3349         if (elm.options[i].value == oldfolder)
3350           {
3351           elm.options[i].text = display_name;
3352           elm.options[i].value = newfolder;
3353           break;
3354           }
3355         }
3356
3357       form.elements['_folder_newname'].value = '';
3358       }
3359     };
3360
3361   // remove the table row of a specific mailbox from the table
3362   // (the row will not be removed, just hidden)
3363   this.remove_folder_row = function(folder)
3364     {
3365     var row;
3366     var id = this.get_folder_row_id(folder);
3367     if (id && (row = document.getElementById(id)))
3368       row.style.display = 'none';
3369
3370     // remove folder from rename-folder list
3371     var form;
3372     if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
3373       {
3374       for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
3375         {
3376         if (form.elements['_folder_oldname'].options[i].value == folder) 
3377           {
3378           form.elements['_folder_oldname'].options[i] = null;
3379           break;
3380           }
3381         }
3382       }
3383     
3384     if (form && form.elements['_folder_newname'])
3385       form.elements['_folder_newname'].value = '';
3386     };
3387
3388   this.subscribe_folder = function(folder)
3389     {
3390     if (folder)
3391       this.http_post('subscribe', '_mbox='+urlencode(folder));
3392     };
3393
3394   this.unsubscribe_folder = function(folder)
3395     {
3396     if (folder)
3397       this.http_post('unsubscribe', '_mbox='+urlencode(folder));
3398     };
3399     
3400   // helper method to find a specific mailbox row ID
3401   this.get_folder_row_id = function(folder)
3402     {
3403     for (var id in this.env.subscriptionrows)
3404       if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
3405         break;
3406         
3407     return id;
3408     };
3409
3410   // duplicate a specific table row
3411   this.clone_table_row = function(row)
3412     {
3413     var cell, td;
3414     var new_row = document.createElement('tr');
3415     for(var n=0; n<row.cells.length; n++)
3416       {
3417       cell = row.cells[n];
3418       td = document.createElement('td');
3419
3420       if (cell.className)
3421         td.className = cell.className;
3422       if (cell.align)
3423         td.setAttribute('align', cell.align);
3424         
3425       td.innerHTML = cell.innerHTML;
3426       new_row.appendChild(td);
3427       }
3428     
3429     return new_row;
3430     };
3431
3432
3433   /*********************************************************/
3434   /*********           GUI functionality           *********/
3435   /*********************************************************/
3436
3437   // eable/disable buttons for page shifting
3438   this.set_page_buttons = function()
3439   {
3440     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
3441     this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
3442     this.enable_command('previouspage', (this.env.current_page > 1));
3443     this.enable_command('firstpage', (this.env.current_page > 1));
3444   };
3445   
3446   // set event handlers on registered buttons
3447   this.init_buttons = function()
3448   {
3449     for (var cmd in this.buttons) {
3450       if (typeof cmd != 'string')
3451         continue;
3452       
3453       for (var i=0; i< this.buttons[cmd].length; i++) {
3454         var prop = this.buttons[cmd][i];
3455         var elm = document.getElementById(prop.id);
3456         if (!elm)
3457           continue;
3458
3459         var preload = false;
3460         if (prop.type == 'image') {
3461           elm = elm.parentNode;
3462           preload = true;
3463         }
3464         
3465         elm._command = cmd;
3466         elm._id = prop.id;
3467         if (prop.sel) {
3468           elm.onmousedown = function(e){ return rcmail.button_sel(this._command, this._id); };
3469           elm.onmouseup = function(e){ return rcmail.button_out(this._command, this._id); };
3470           if (preload)
3471             new Image().src = prop.sel;
3472         }
3473         if (prop.over) {
3474           elm.onmouseover = function(e){ return rcmail.button_over(this._command, this._id); };
3475           elm.onmouseout = function(e){ return rcmail.button_out(this._command, this._id); };
3476           if (preload)
3477             new Image().src = prop.over;
3478         }
3479       }
3480     }
3481   };
3482
3483   // set button to a specific state
3484   this.set_button = function(command, state)
3485     {
3486     var a_buttons = this.buttons[command];
3487     var button, obj;
3488
3489     if(!a_buttons || !a_buttons.length)
3490       return false;
3491
3492     for(var n=0; n<a_buttons.length; n++)
3493       {
3494       button = a_buttons[n];
3495       obj = document.getElementById(button.id);
3496
3497       // get default/passive setting of the button
3498       if (obj && button.type=='image' && !button.status) {
3499         button.pas = obj._original_src ? obj._original_src : obj.src;
3500         // respect PNG fix on IE browsers
3501         if (obj.runtimeStyle && obj.runtimeStyle.filter && obj.runtimeStyle.filter.match(/src=['"]([^'"]+)['"]/))
3502           button.pas = RegExp.$1;
3503       }
3504       else if (obj && !button.status)
3505         button.pas = String(obj.className);
3506
3507       // set image according to button state
3508       if (obj && button.type=='image' && button[state])
3509         {
3510         button.status = state;        
3511         obj.src = button[state];
3512         }
3513       // set class name according to button state
3514       else if (obj && typeof(button[state])!='undefined')
3515         {
3516         button.status = state;        
3517         obj.className = button[state];        
3518         }
3519       // disable/enable input buttons
3520       if (obj && button.type=='input')
3521         {
3522         button.status = state;
3523         obj.disabled = !state;
3524         }
3525       }
3526     };
3527
3528   // display a specific alttext
3529   this.set_alttext = function(command, label)
3530     {
3531       if (!this.buttons[command] || !this.buttons[command].length)
3532         return;
3533       
3534       var button, obj, link;
3535       for (var n=0; n<this.buttons[command].length; n++)
3536       {
3537         button = this.buttons[command][n];
3538         obj = document.getElementById(button.id);
3539         
3540         if (button.type=='image' && obj)
3541         {
3542           obj.setAttribute('alt', this.get_label(label));
3543           if ((link = obj.parentNode) && link.tagName.toLowerCase() == 'a')
3544             link.setAttribute('title', this.get_label(label));
3545         }
3546         else if (obj)
3547           obj.setAttribute('title', this.get_label(label));
3548       }
3549     };
3550
3551   // mouse over button
3552   this.button_over = function(command, id)
3553   {
3554     var a_buttons = this.buttons[command];
3555     var button, elm;
3556
3557     if(!a_buttons || !a_buttons.length)
3558       return false;
3559
3560     for(var n=0; n<a_buttons.length; n++)
3561     {
3562       button = a_buttons[n];
3563       if(button.id==id && button.status=='act')
3564       {
3565         elm = document.getElementById(button.id);
3566         if (elm && button.over) {
3567           if (button.type == 'image')
3568             elm.src = button.over;
3569           else
3570             elm.className = button.over;
3571         }
3572       }
3573     }
3574   };
3575
3576   // mouse down on button
3577   this.button_sel = function(command, id)
3578   {
3579     var a_buttons = this.buttons[command];
3580     var button, elm;
3581
3582     if(!a_buttons || !a_buttons.length)
3583       return;
3584
3585     for(var n=0; n<a_buttons.length; n++)
3586     {
3587       button = a_buttons[n];
3588       if(button.id==id && button.status=='act')
3589       {
3590         elm = document.getElementById(button.id);
3591         if (elm && button.sel) {
3592           if (button.type == 'image')
3593             elm.src = button.sel;
3594           else
3595             elm.className = button.sel;
3596         }
3597         this.buttons_sel[id] = command;
3598       }
3599     }
3600   };
3601
3602   // mouse out of button
3603   this.button_out = function(command, id)
3604   {
3605     var a_buttons = this.buttons[command];
3606     var button, elm;
3607
3608     if(!a_buttons || !a_buttons.length)
3609       return;
3610
3611     for(var n=0; n<a_buttons.length; n++)
3612     {
3613       button = a_buttons[n];
3614       if(button.id==id && button.status=='act')
3615       {
3616         elm = document.getElementById(button.id);
3617         if (elm && button.act) {
3618           if (button.type == 'image')
3619             elm.src = button.act;
3620           else
3621             elm.className = button.act;
3622         }
3623       }
3624     }
3625   };
3626
3627   // write to the document/window title
3628   this.set_pagetitle = function(title)
3629   {
3630     if (title && document.title)
3631       document.title = title;
3632   }
3633
3634   // display a system message
3635   this.display_message = function(msg, type, hold)
3636     {
3637     if (!this.loaded)  // save message in order to display after page loaded
3638       {
3639       this.pending_message = new Array(msg, type);
3640       return true;
3641       }
3642
3643     // pass command to parent window
3644     if (this.env.framed && parent.rcmail)
3645       return parent.rcmail.display_message(msg, type, hold);
3646
3647     if (!this.gui_objects.message)
3648       return false;
3649
3650     if (this.message_timer)
3651       clearTimeout(this.message_timer);
3652     
3653     var cont = msg;
3654     if (type)
3655       cont = '<div class="'+type+'">'+cont+'</div>';
3656
3657     var obj = $(this.gui_objects.message).html(cont).show();
3658     
3659     if (type!='loading')
3660       obj.bind('mousedown', function(){ ref.hide_message(); return true; });
3661     
3662     if (!hold)
3663       this.message_timer = window.setTimeout(function(){ ref.hide_message(true); }, this.message_time);
3664     };
3665
3666   // make a message row disapear
3667   this.hide_message = function(fade)
3668     {
3669     if (this.gui_objects.message)
3670       $(this.gui_objects.message).unbind()[(fade?'fadeOut':'hide')]();
3671     };
3672
3673   // mark a mailbox as selected and set environment variable
3674   this.select_folder = function(name, old)
3675   {
3676     if (this.gui_objects.folderlist)
3677     {
3678       var current_li, target_li;
3679       
3680       if ((current_li = this.get_folder_li(old))) {
3681         $(current_li).removeClass('selected').removeClass('unfocused');
3682       }
3683       if ((target_li = this.get_folder_li(name))) {
3684         $(target_li).removeClass('unfocused').addClass('selected');
3685       }
3686       
3687       // trigger event hook
3688       this.triggerEvent('selectfolder', { folder:name, old:old });
3689     }
3690   };
3691
3692   // helper method to find a folder list item
3693   this.get_folder_li = function(name)
3694   {
3695     if (this.gui_objects.folderlist)
3696     {
3697       name = String(name).replace(this.identifier_expr, '_');
3698       return document.getElementById('rcmli'+name);
3699     }
3700
3701     return null;
3702   };
3703
3704   // for reordering column array, Konqueror workaround
3705   this.set_message_coltypes = function(coltypes) 
3706   { 
3707     this.coltypes = coltypes;
3708     
3709     // set correct list titles
3710     var cell, col;
3711     var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3712     for (var n=0; thead && n<this.coltypes.length; n++) 
3713       {
3714       col = this.coltypes[n];
3715       if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3716         {
3717         // if we have links for sorting, it's a bit more complicated...
3718         if (cell.firstChild && cell.firstChild.tagName.toLowerCase()=='a')
3719           {
3720           cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3721           cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3722           cell.firstChild.__col = col;
3723           }
3724         else
3725           cell.innerHTML = this.get_label(this.coltypes[n]);
3726
3727         cell.id = 'rcm'+col;
3728         }
3729       else if (col == 'subject' && this.message_list)
3730         this.message_list.subject_col = n+1;
3731       }
3732   };
3733
3734   // create a table row in the message list
3735   this.add_message_row = function(uid, cols, flags, attachment, attop)
3736     {
3737     if (!this.gui_objects.messagelist || !this.message_list)
3738       return false;
3739
3740     if (this.message_list.background)
3741       var tbody = this.message_list.background;
3742     else
3743       var tbody = this.gui_objects.messagelist.tBodies[0];
3744     
3745     var rowcount = tbody.rows.length;
3746     var even = rowcount%2;
3747     
3748     this.env.messages[uid] = {
3749       deleted: flags.deleted?1:0,
3750       replied: flags.replied?1:0,
3751       unread: flags.unread?1:0,
3752       forwarded: flags.forwarded?1:0,
3753       flagged:flags.flagged?1:0
3754     };
3755
3756     var css_class = 'message'
3757         + (even ? ' even' : ' odd')
3758         + (flags.unread ? ' unread' : '')
3759         + (flags.deleted ? ' deleted' : '')
3760         + (flags.flagged ? ' flagged' : '')
3761         + (this.message_list.in_selection(uid) ? ' selected' : '');
3762
3763     // for performance use DOM instead of jQuery here
3764     var row = document.createElement('tr');
3765     row.id = 'rcmrow'+uid;
3766     row.className = css_class;
3767     
3768     var icon = this.env.messageicon;
3769     if (flags.deleted && this.env.deletedicon)
3770       icon = this.env.deletedicon;
3771     else if (flags.replied && this.env.repliedicon)
3772       {
3773       if (flags.forwarded && this.env.forwardedrepliedicon)
3774         icon = this.env.forwardedrepliedicon;
3775       else
3776         icon = this.env.repliedicon;
3777       }
3778     else if (flags.forwarded && this.env.forwardedicon)
3779       icon = this.env.forwardedicon;
3780     else if(flags.unread && this.env.unreadicon)
3781       icon = this.env.unreadicon;
3782     
3783     // add icon col
3784     var col = document.createElement('td');
3785     col.className = 'icon';
3786     col.innerHTML = icon ? '<img src="'+icon+'" alt="" />' : '';
3787     row.appendChild(col);
3788                   
3789     // add each submitted col
3790     for (var n = 0; n < this.coltypes.length; n++) {
3791       var c = this.coltypes[n];
3792       col = document.createElement('td');
3793       col.className = String(c).toLowerCase();
3794             
3795       if (c=='flag') {
3796         if (flags.flagged && this.env.flaggedicon)
3797           col.innerHTML = '<img src="'+this.env.flaggedicon+'" alt="" />';
3798         else if(!flags.flagged && this.env.unflaggedicon)
3799           col.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
3800         }
3801       else if (c=='attachment')
3802         col.innerHTML = (attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '&nbsp;');
3803       else
3804         col.innerHTML = cols[c];
3805
3806       row.appendChild(col);
3807       }
3808
3809     this.message_list.insert_row(row, attop);
3810
3811     // remove 'old' row
3812     if (attop && this.env.pagesize && this.message_list.rowcount > this.env.pagesize) {
3813       var uid = this.message_list.get_last_row();
3814       this.message_list.remove_row(uid);
3815       this.message_list.clear_selection(uid);
3816       }
3817     };
3818
3819   // messages list handling in background (for performance)
3820   this.offline_message_list = function(flag)
3821     {
3822       if (this.message_list)
3823         this.message_list.set_background_mode(flag);
3824     };
3825
3826   // replace content of row count display
3827   this.set_rowcount = function(text)
3828     {
3829     $(this.gui_objects.countdisplay).html(text);
3830
3831     // update page navigation buttons
3832     this.set_page_buttons();
3833     };
3834
3835   // replace content of mailboxname display
3836   this.set_mailboxname = function(content)
3837     {
3838     if (this.gui_objects.mailboxname && content)
3839       this.gui_objects.mailboxname.innerHTML = content;
3840     };
3841
3842   // replace content of quota display
3843   this.set_quota = function(content)
3844     {
3845     if (content && this.gui_objects.quotadisplay)
3846       $(this.gui_objects.quotadisplay).html(content);
3847     };
3848
3849   // update the mailboxlist
3850   this.set_unread_count = function(mbox, count, set_title)
3851     {
3852     if (!this.gui_objects.mailboxlist)
3853       return false;
3854
3855     this.env.unread_counts[mbox] = count;
3856     this.set_unread_count_display(mbox, set_title);
3857     }
3858
3859   // update the mailbox count display
3860   this.set_unread_count_display = function(mbox, set_title)
3861     {
3862     var reg, text_obj, item, mycount, childcount, div;
3863     if (item = this.get_folder_li(mbox))
3864       {
3865       mycount = this.env.unread_counts[mbox] ? this.env.unread_counts[mbox] : 0;
3866       text_obj = item.getElementsByTagName('a')[0];
3867       reg = /\s+\([0-9]+\)$/i;
3868
3869       childcount = 0;
3870       if ((div = item.getElementsByTagName('div')[0]) &&
3871           div.className.match(/collapsed/))
3872         {
3873         // add children's counters
3874         for (var k in this.env.unread_counts) 
3875           if (k.indexOf(mbox + this.env.delimiter) == 0)
3876             childcount += this.env.unread_counts[k];
3877         }
3878
3879       if (mycount && text_obj.innerHTML.match(reg))
3880         text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+mycount+')');
3881       else if (mycount)
3882         text_obj.innerHTML += ' ('+mycount+')';
3883       else
3884         text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
3885
3886       // set parent's display
3887       reg = new RegExp(RegExp.escape(this.env.delimiter) + '[^' + RegExp.escape(this.env.delimiter) + ']+$');
3888       if (mbox.match(reg))
3889         this.set_unread_count_display(mbox.replace(reg, ''), false);
3890
3891       // set the right classes
3892       if ((mycount+childcount)>0)
3893         $(item).addClass('unread');
3894       else
3895         $(item).removeClass('unread');
3896       }
3897
3898     // set unread count to window title
3899     reg = /^\([0-9]+\)\s+/i;
3900     if (set_title && document.title)
3901       {
3902       var doc_title = String(document.title);
3903       var new_title = "";
3904
3905       if (mycount && doc_title.match(reg))
3906         new_title = doc_title.replace(reg, '('+mycount+') ');
3907       else if (mycount)
3908         new_title = '('+mycount+') '+doc_title;
3909       else
3910         new_title = doc_title.replace(reg, '');
3911         
3912       this.set_pagetitle(new_title);
3913       }
3914     };
3915
3916   // notifies that a new message(s) has hit the mailbox
3917   this.new_message_focus = function()
3918     {
3919     // focus main window
3920     if (this.env.framed && window.parent)
3921       window.parent.focus();
3922     else
3923       window.focus();
3924     }
3925
3926   this.toggle_prefer_html = function(checkbox)
3927     {
3928     var addrbook_show_images;
3929     if (addrbook_show_images = document.getElementById('rcmfd_addrbook_show_images'))
3930       addrbook_show_images.disabled = !checkbox.checked;
3931     }
3932
3933   // display fetched raw headers
3934   this.set_headers = function(content)
3935   {
3936     if (this.gui_objects.all_headers_row && this.gui_objects.all_headers_box && content) {
3937       $(this.gui_objects.all_headers_box).html(content).show();
3938
3939       if (this.env.framed && parent.rcmail)
3940         parent.rcmail.set_busy(false);
3941       else
3942         this.set_busy(false);
3943     }
3944   };
3945
3946   // display all-headers row and fetch raw message headers
3947   this.load_headers = function(elem)
3948     {
3949     if (!this.gui_objects.all_headers_row || !this.gui_objects.all_headers_box || !this.env.uid)
3950       return;
3951     
3952     $(elem).removeClass('show-headers').addClass('hide-headers');
3953     $(this.gui_objects.all_headers_row).show();
3954     elem.onclick = function() { rcmail.hide_headers(elem); }
3955
3956     // fetch headers only once
3957     if (!this.gui_objects.all_headers_box.innerHTML)
3958       {
3959       this.display_message(this.get_label('loading'), 'loading', true);
3960       this.http_post('headers', '_uid='+this.env.uid);
3961       }
3962     }
3963
3964   // hide all-headers row
3965   this.hide_headers = function(elem)
3966     {
3967     if (!this.gui_objects.all_headers_row || !this.gui_objects.all_headers_box)
3968       return;
3969
3970     $(elem).removeClass('hide-headers').addClass('show-headers');
3971     $(this.gui_objects.all_headers_row).hide();
3972     elem.onclick = function() { rcmail.load_headers(elem); }
3973     }
3974
3975
3976   /********************************************************/
3977   /*********  html to text conversion functions   *********/
3978   /********************************************************/
3979
3980   this.html2plain = function(htmlText, id)
3981     {
3982     var url = this.env.bin_path+'html2text.php';
3983     var rcmail = this;
3984
3985     this.set_busy(true, 'converting');
3986     console.log('HTTP POST: '+url);
3987
3988     $.ajax({ type: 'POST', url: url, data: htmlText, contentType: 'application/octet-stream',
3989       error: function(o) { rcmail.http_error(o); },
3990       success: function(data) { rcmail.set_busy(false); $(document.getElementById(id)).val(data); console.log(data); }
3991       });
3992     }
3993
3994   this.plain2html = function(plainText, id)
3995     {
3996     this.set_busy(true, 'converting');
3997     $(document.getElementById(id)).val('<pre>'+plainText+'</pre>');
3998     this.set_busy(false);
3999     }
4000
4001
4002   /********************************************************/
4003   /*********        remote request methods        *********/
4004   /********************************************************/
4005
4006   this.redirect = function(url, lock)
4007     {
4008     if (lock || lock === null)
4009       this.set_busy(true);
4010
4011     if (this.env.framed && window.parent)
4012       parent.location.href = url;
4013     else  
4014       location.href = url;
4015     };
4016
4017   this.goto_url = function(action, query, lock)
4018     {
4019     var querystring = query ? '&'+query : '';
4020     this.redirect(this.env.comm_path+'&_action='+action+querystring, lock);
4021     };
4022
4023   // send a http request to the server
4024   this.http_request = function(action, querystring, lock)
4025   {
4026     querystring += (querystring ? '&' : '') + '_remote=1';
4027     var url = this.env.comm_path + '&_action=' + action + '&' + querystring
4028     
4029     // send request
4030     console.log('HTTP POST: ' + url);
4031     jQuery.get(url, { _unlock:(lock?1:0) }, function(data){ ref.http_response(data); }, 'json');
4032   };
4033
4034   // send a http POST request to the server
4035   this.http_post = function(action, postdata, lock)
4036   {
4037     var url = this.env.comm_path+'&_action=' + action;
4038     
4039     if (postdata && typeof(postdata) == 'object') {
4040       postdata._remote = 1;
4041       postdata._unlock = (lock ? 1 : 0);
4042     }
4043     else
4044       postdata += (postdata ? '&' : '') + '_remote=1' + (lock ? '&_unlock=1' : '');
4045
4046     // send request
4047     console.log('HTTP POST: ' + url);
4048     jQuery.post(url, postdata, function(data){ ref.http_response(data); }, 'json');
4049   };
4050
4051   // handle HTTP response
4052   this.http_response = function(response)
4053   {
4054     var console_msg = '';
4055     
4056     if (response.unlock)
4057       this.set_busy(false);
4058
4059     // set env vars
4060     if (response.env)
4061       this.set_env(response.env);
4062
4063     // we have labels to add
4064     if (typeof response.texts == 'object') {
4065       for (var name in response.texts)
4066         if (typeof response.texts[name] == 'string')
4067           this.add_label(name, response.texts[name]);
4068     }
4069
4070     // if we get javascript code from server -> execute it
4071     if (response.exec) {
4072       console.log(response.exec);
4073       eval(response.exec);
4074     }
4075     
4076     // execute callback functions of plugins
4077     if (response.callbacks && response.callbacks.length) {
4078       for (var i=0; i < response.callbacks.length; i++)
4079         this.triggerEvent(response.callbacks[i][0], response.callbacks[i][1]);
4080     }
4081  
4082     // process the response data according to the sent action
4083     switch (response.action) {
4084       case 'delete':
4085         if (this.task == 'addressbook') {
4086           var uid = this.contact_list.get_selection();
4087           this.enable_command('compose', (uid && this.contact_list.rows[uid]));
4088           this.enable_command('delete', 'edit', (uid && this.contact_list.rows[uid] && this.env.address_sources && !this.env.address_sources[this.env.source].readonly));
4089           this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
4090         }
4091       
4092       case 'moveto':
4093         if (this.env.action == 'show') {
4094           // re-enable commands on move/delete error
4095           this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', 'open', 'edit', 'viewsource', 'download', true);
4096         } else if (this.message_list)
4097           this.message_list.init();
4098         break;
4099         
4100       case 'purge':
4101       case 'expunge':
4102         if (!this.env.messagecount && this.task == 'mail') {
4103           // clear preview pane content
4104           if (this.env.contentframe)
4105             this.show_contentframe(false);
4106           // disable commands useless when mailbox is empty
4107           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 
4108             'mark', 'viewsource', 'open', 'edit', 'download', 'print', 'load-attachment', 
4109             'purge', 'expunge', 'select-all', 'select-none', 'sort', false);
4110         }
4111         break;
4112
4113       case 'check-recent':
4114       case 'getunread':
4115       case 'list':
4116         if (this.task == 'mail') {
4117           if (this.message_list && response.action == 'list')
4118             this.msglist_select(this.message_list);
4119           this.enable_command('show', 'expunge', 'select-all', 'select-none', 'sort', (this.env.messagecount > 0));
4120           this.enable_command('purge', this.purge_mailbox_test());
4121           
4122           if (response.action == 'list')
4123             this.triggerEvent('listupdate', { folder:this.env.mailbox, rowcount:this.message_list.rowcount });
4124         }
4125         else if (this.task == 'addressbook') {
4126           this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
4127           
4128           if (response.action == 'list')
4129             this.triggerEvent('listupdate', { folder:this.env.source, rowcount:this.contact_list.rowcount });
4130         }
4131         break;
4132     }
4133   };
4134
4135   // handle HTTP request errors
4136   this.http_error = function(request, status, err)
4137     {
4138     var errmsg = request.statusText;
4139
4140     this.set_busy(false);
4141     request.abort();
4142     
4143     if (errmsg)
4144       this.display_message(this.get_label('servererror') + ' (' + errmsg + ')', 'error');
4145     };
4146
4147   // use an image to send a keep-alive siganl to the server
4148   this.send_keep_alive = function()
4149     {
4150     var d = new Date();
4151     this.http_request('keep-alive', '_t='+d.getTime());
4152     };
4153
4154   // send periodic request to check for recent messages
4155   this.check_for_recent = function(setbusy)
4156     {
4157     if (this.busy)
4158       return;
4159
4160     if (setbusy)
4161       this.set_busy(true, 'checkingmail');
4162
4163     var addurl = '_t=' + (new Date().getTime());
4164
4165     if (this.gui_objects.messagelist)
4166       addurl += '&_list=1';
4167     if (this.gui_objects.quotadisplay)
4168       addurl += '&_quota=1';
4169     if (this.env.search_request)
4170       addurl += '&_search=' + this.env.search_request;
4171
4172     this.http_request('check-recent', addurl, true);
4173     };
4174
4175
4176   /********************************************************/
4177   /*********            helper methods            *********/
4178   /********************************************************/
4179   
4180   // check if we're in show mode or if we have a unique selection
4181   // and return the message uid
4182   this.get_single_uid = function()
4183     {
4184     return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
4185     };
4186
4187   // same as above but for contacts
4188   this.get_single_cid = function()
4189     {
4190     return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
4191     };
4192
4193
4194   this.get_caret_pos = function(obj)
4195     {
4196     if (typeof(obj.selectionEnd)!='undefined')
4197       return obj.selectionEnd;
4198     else if (document.selection && document.selection.createRange)
4199       {
4200       var range = document.selection.createRange();
4201       if (range.parentElement()!=obj)
4202         return 0;
4203
4204       var gm = range.duplicate();
4205       if (obj.tagName=='TEXTAREA')
4206         gm.moveToElementText(obj);
4207       else
4208         gm.expand('textedit');
4209       
4210       gm.setEndPoint('EndToStart', range);
4211       var p = gm.text.length;
4212
4213       return p<=obj.value.length ? p : -1;
4214       }
4215     else
4216       return obj.value.length;
4217     };
4218
4219   this.set_caret_pos = function(obj, pos)
4220     {
4221     if (obj.setSelectionRange)
4222       obj.setSelectionRange(pos, pos);
4223     else if (obj.createTextRange)
4224       {
4225       var range = obj.createTextRange();
4226       range.collapse(true);
4227       range.moveEnd('character', pos);
4228       range.moveStart('character', pos);
4229       range.select();
4230       }
4231     }
4232
4233   // set all fields of a form disabled
4234   this.lock_form = function(form, lock)
4235     {
4236     if (!form || !form.elements)
4237       return;
4238     
4239     var type;
4240     for (var n=0; n<form.elements.length; n++)
4241       {
4242       type = form.elements[n];
4243       if (type=='hidden')
4244         continue;
4245         
4246       form.elements[n].disabled = lock;
4247       }
4248     };
4249     
4250 }  // end object rcube_webmail
4251
4252
4253 // copy event engine prototype
4254 rcube_webmail.prototype.addEventListener = rcube_event_engine.prototype.addEventListener;
4255 rcube_webmail.prototype.removeEventListener = rcube_event_engine.prototype.removeEventListener;
4256 rcube_webmail.prototype.triggerEvent = rcube_event_engine.prototype.triggerEvent;
4257