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