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