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