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