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