alecpl
2008-10-09 e70b3b24fc8ac7b54a820ae87ce8f4af4043125e
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;
132aae 1583
A 1584     this.env.messages[uid] = rows[uid];
25c35c 1585     }
A 1586
1587   // set message row status, class and icon
1588   this.set_message = function(uid, flag, status)
1589     {
1590     var rows = this.message_list.rows;
1591
1592     if (!rows[uid]) return false;
1593     
1594     if (flag)
1595       this.set_message_status(uid, flag, status);
1596     
1597     if (rows[uid].unread && rows[uid].classname.indexOf('unread')<0)
1598       {
1599       rows[uid].classname += ' unread';
1600       this.set_classname(rows[uid].obj, 'unread', true);
1601       }
1602     else if (!rows[uid].unread && rows[uid].classname.indexOf('unread')>=0)
1603       {
1604       rows[uid].classname = rows[uid].classname.replace(/\s*unread/, '');
1605       this.set_classname(rows[uid].obj, 'unread', false);
1606       }
1607     
1608     if (rows[uid].deleted && rows[uid].classname.indexOf('deleted')<0)
1609       {
1610       rows[uid].classname += ' deleted';
1611       this.set_classname(rows[uid].obj, 'deleted', true);
1612       }
1613     else if (!rows[uid].deleted && rows[uid].classname.indexOf('deleted')>=0)
1614       {
1615       rows[uid].classname = rows[uid].classname.replace(/\s*deleted/, '');
1616       this.set_classname(rows[uid].obj, 'deleted', false);
1617       }
1618
163a13 1619     if (rows[uid].flagged && rows[uid].classname.indexOf('flagged')<0)
A 1620       {
1621       rows[uid].classname += ' flagged';
1622       this.set_classname(rows[uid].obj, 'flagged', true);
1623       }
1624     else if (!rows[uid].flagged && rows[uid].classname.indexOf('flagged')>=0)
1625       {
1626       rows[uid].classname = rows[uid].classname.replace(/\s*flagged/, '');
1627       this.set_classname(rows[uid].obj, 'flagged', false);
1628       }
1629
25c35c 1630     this.set_message_icon(uid);
A 1631     }
0dbac3 1632
4e17e6 1633   // move selected messages to the specified mailbox
T 1634   this.move_messages = function(mbox)
1635     {
e4bbb2 1636     // exit if current or no mailbox specified or if selection is empty
faebf4 1637     if (!mbox || mbox == this.env.mailbox || (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length)))
aa9836 1638       return;
e4bbb2 1639
ecf759 1640     var lock = false;
cfdf04 1641     var add_url = '&_target_mbox='+urlencode(mbox)+'&_from='+(this.env.action ? this.env.action : '');
4e17e6 1642
T 1643     // show wait message
1644     if (this.env.action=='show')
ecf759 1645       {
T 1646       lock = true;
4e17e6 1647       this.set_busy(true, 'movingmessage');
ecf759 1648       }
d87fc2 1649     else if (!this.env.flag_for_deletion)
f11541 1650       this.show_contentframe(false);
6b47de 1651
faebf4 1652     // Hide message command buttons until a message is selected
T 1653     this.enable_command('reply', 'reply-all', 'forward', 'delete', 'mark', 'print', false);
1654
d87fc2 1655     this._with_selected_messages('moveto', lock, add_url, (this.env.flag_for_deletion ? false : true));
4e17e6 1656     };
T 1657
857a38 1658   // delete selected messages from the current mailbox
S 1659   this.delete_messages = function()
1660     {
1c7b97 1661     var selection = this.message_list ? this.message_list.get_selection() : new Array();
3d3531 1662
857a38 1663     // exit if no mailbox specified or if selection is empty
1c7b97 1664     if (!this.env.uid && !selection.length)
e4bbb2 1665         return;
6b47de 1666
857a38 1667     // if there is a trash mailbox defined and we're not currently in it:
1c7b97 1668     if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() != String(this.env.trash_mailbox).toLowerCase())
cfdf04 1669       {
31c171 1670       // if shift was pressed delete it immediately
086377 1671       if (this.message_list && this.message_list.shiftkey)
31c171 1672         {
S 1673         if (confirm(this.get_label('deletemessagesconfirm')))
1674           this.permanently_remove_messages();
1675         }
1676       else
1677         this.move_messages(this.env.trash_mailbox);
cfdf04 1678       }
857a38 1679     // if there is a trash mailbox defined but we *are* in it:
S 1680     else if (this.env.trash_mailbox && String(this.env.mailbox).toLowerCase() == String(this.env.trash_mailbox).toLowerCase())
1681       this.permanently_remove_messages();
1682     // if there isn't a defined trash mailbox and the config is set to flag for deletion
6b47de 1683     else if (!this.env.trash_mailbox && this.env.flag_for_deletion)
T 1684       {
1c7b97 1685       this.mark_message('delete');
6b47de 1686       if(this.env.action=="show")
dab065 1687         this.command('nextmessage','',this);
6b47de 1688       else if (selection.length == 1)
T 1689         this.message_list.select_next();
dab065 1690       }
857a38 1691     // if there isn't a defined trash mailbox and the config is set NOT to flag for deletion
6b47de 1692     else if (!this.env.trash_mailbox) 
857a38 1693       this.permanently_remove_messages();
S 1694   };
cfdf04 1695
T 1696   // delete the selected messages permanently
1697   this.permanently_remove_messages = function()
1698     {
1699     // exit if no mailbox specified or if selection is empty
1700     if (!this.env.uid && (!this.message_list || !this.message_list.get_selection().length))
1701       return;
1c7b97 1702       
f11541 1703     this.show_contentframe(false);
d87fc2 1704     this._with_selected_messages('delete', false, '&_from='+(this.env.action ? this.env.action : ''), true);
cfdf04 1705     };
T 1706
1707   // Send a specifc request with UIDs of all selected messages
1708   // @private
d87fc2 1709   this._with_selected_messages = function(action, lock, add_url, remove)
d22455 1710   {
cfdf04 1711     var a_uids = new Array();
3d3531 1712
cfdf04 1713     if (this.env.uid)
3d3531 1714       a_uids[0] = this.env.uid;
cfdf04 1715     else
d22455 1716     {
cfdf04 1717       var selection = this.message_list.get_selection();
d87fc2 1718       var rows = this.message_list.rows;
cfdf04 1719       var id;
T 1720       for (var n=0; n<selection.length; n++)
1721         {
1722         id = selection[n];
1723         a_uids[a_uids.length] = id;
3d3531 1724
d22455 1725         if (remove)
d87fc2 1726           this.message_list.remove_row(id, (n == selection.length-1));
A 1727         else
d22455 1728         {
132aae 1729           this.set_message_status(id, 'deleted', true);
d22455 1730           if (this.env.read_when_deleted)
132aae 1731             this.set_message_status(id, 'unread', false);
25c35c 1732       this.set_message(id);
cfdf04 1733         }
T 1734       }
d22455 1735     }
132aae 1736
f11541 1737     // also send search request to get the right messages 
T 1738     if (this.env.search_request) 
cfdf04 1739       add_url += '&_search='+this.env.search_request;
T 1740
1741     // send request to server
8d0758 1742     this.http_post(action, '_uid='+a_uids.join(',')+'&_mbox='+urlencode(this.env.mailbox)+add_url, lock);
d22455 1743   };
4e17e6 1744
T 1745   // set a specific flag to one or more messages
1746   this.mark_message = function(flag, uid)
1747     {
1748     var a_uids = new Array();
5d97ac 1749     var r_uids = new Array();
b19097 1750     var selection = this.message_list ? this.message_list.get_selection() : new Array();
3d3531 1751
4e17e6 1752     if (uid)
T 1753       a_uids[0] = uid;
1754     else if (this.env.uid)
1755       a_uids[0] = this.env.uid;
1c7b97 1756     else if (this.message_list)
4e17e6 1757       {
3d3531 1758       for (var n=0; n<selection.length; n++)
4e17e6 1759         {
e189a6 1760           a_uids[a_uids.length] = selection[n];
4e17e6 1761         }
5d97ac 1762       }
A 1763
3d3531 1764     if (!this.message_list)
A 1765       r_uids = a_uids;
1766     else
1767       for (var id, n=0; n<a_uids.length; n++)
5d97ac 1768       {
A 1769         id = a_uids[n];
1770         if ((flag=='read' && this.message_list.rows[id].unread) 
d22455 1771             || (flag=='unread' && !this.message_list.rows[id].unread)
T 1772             || (flag=='delete' && !this.message_list.rows[id].deleted)
1773             || (flag=='undelete' && this.message_list.rows[id].deleted)
1774             || (flag=='flagged' && !this.message_list.rows[id].flagged)
1775             || (flag=='unflagged' && this.message_list.rows[id].flagged))
1776         {
1777           r_uids[r_uids.length] = id;
1778         }
4e17e6 1779       }
3d3531 1780
1a98a6 1781     // nothing to do
5d97ac 1782     if (!r_uids.length)
1a98a6 1783       return;
3d3531 1784
6b47de 1785     switch (flag)
T 1786       {
857a38 1787         case 'read':
S 1788         case 'unread':
5d97ac 1789           this.toggle_read_status(flag, r_uids);
857a38 1790           break;
S 1791         case 'delete':
1792         case 'undelete':
5d97ac 1793           this.toggle_delete_status(r_uids);
e189a6 1794           break;
A 1795         case 'flagged':
1796         case 'unflagged':
1797           this.toggle_flagged_status(flag, a_uids);
857a38 1798           break;
S 1799       }
1800     };
4e17e6 1801
857a38 1802   // set class to read/unread
6b47de 1803   this.toggle_read_status = function(flag, a_uids)
T 1804   {
4e17e6 1805     // mark all message rows as read/unread
T 1806     for (var i=0; i<a_uids.length; i++)
25c35c 1807       this.set_message(a_uids[i], 'unread', (flag=='unread' ? true : false));
4bca67 1808
5d97ac 1809     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
6b47de 1810   };
6d2714 1811
e189a6 1812   // set image to flagged or unflagged
A 1813   this.toggle_flagged_status = function(flag, a_uids)
1814   {
1815     // mark all message rows as flagged/unflagged
1816     for (var i=0; i<a_uids.length; i++)
25c35c 1817       this.set_message(a_uids[i], 'flagged', (flag=='flagged' ? true : false));
e189a6 1818
A 1819     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1820   };
857a38 1821   
S 1822   // mark all message rows as deleted/undeleted
6b47de 1823   this.toggle_delete_status = function(a_uids)
T 1824   {
3d3531 1825     var rows = this.message_list ? this.message_list.rows : new Array();
A 1826     
1c7b97 1827     if (a_uids.length==1)
T 1828     {
25c35c 1829       if (!rows.length || (rows[a_uids[0]] && !rows[a_uids[0]].deleted))
6b47de 1830         this.flag_as_deleted(a_uids);
T 1831       else
1832         this.flag_as_undeleted(a_uids);
1833
1c5853 1834       return true;
S 1835     }
1836     
1837     var all_deleted = true;
1c7b97 1838     for (var i=0; i<a_uids.length; i++)
T 1839     {
857a38 1840       uid = a_uids[i];
6b47de 1841       if (rows[uid]) {
25c35c 1842         if (!rows[uid].deleted)
1c7b97 1843         {
1c5853 1844           all_deleted = false;
S 1845           break;
857a38 1846         }
S 1847       }
1c5853 1848     }
S 1849     
1850     if (all_deleted)
1851       this.flag_as_undeleted(a_uids);
1852     else
1853       this.flag_as_deleted(a_uids);
1854     
1855     return true;
6b47de 1856   };
4e17e6 1857
6b47de 1858   this.flag_as_undeleted = function(a_uids)
T 1859   {
1c7b97 1860     for (var i=0; i<a_uids.length; i++)
25c35c 1861       this.set_message(a_uids[i], 'deleted', false);
6b47de 1862
8d0758 1863     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=undelete');
1c5853 1864     return true;
6b47de 1865   };
T 1866
1867   this.flag_as_deleted = function(a_uids)
1868   {
3d3531 1869     var add_url = '';
A 1870     var r_uids = new Array();
1871     var rows = this.message_list ? this.message_list.rows : new Array();
1872     
1c7b97 1873     for (var i=0; i<a_uids.length; i++)
3d3531 1874       {
1c5853 1875       uid = a_uids[i];
3d3531 1876       if (rows[uid])
A 1877         {
25c35c 1878     this.set_message(uid, 'deleted', true);
d22455 1879         if (rows[uid].unread)
T 1880           r_uids[r_uids.length] = uid;
3d3531 1881         }
A 1882       }
1883
1884     if (r_uids.length)
1885       add_url = '&_ruid='+r_uids.join(',');
1886
1887     this.http_post('mark', '_uid='+a_uids.join(',')+'&_flag=delete'+add_url);
1c5853 1888     return true;  
6b47de 1889   };
3d3531 1890
A 1891   // flag as read without mark request (called from backend)
1892   // argument should be a coma-separated list of uids
1893   this.flag_deleted_as_read = function(uids)
1894   {
1895     var icn_src;
1896     var rows = this.message_list ? this.message_list.rows : new Array();
1897     var str = String(uids);
1898     var a_uids = new Array();
1899
1900     a_uids = str.split(',');
1901
1902     for (var uid, i=0; i<a_uids.length; i++)
1903       {
1904       uid = a_uids[i];
1905       if (rows[uid])
132aae 1906         this.set_message(uid, 'unread', false);
3d3531 1907       }
A 1908   };
0dbac3 1909   
T 1910   
b566ff 1911   /*********************************************************/
S 1912   /*********           login form methods          *********/
1913   /*********************************************************/
1914
1915   // handler for keyboard events on the _user field
65444b 1916   this.login_user_keyup = function(e)
b566ff 1917   {
9bbb17 1918     var key = rcube_event.get_keycode(e);
T 1919     var elm;
b566ff 1920
S 1921     // enter
9bbb17 1922     if ((key==13) && (elm = rcube_find_object('_pass')))
b566ff 1923     {
9bbb17 1924       elm.focus();
b566ff 1925       return false;
S 1926     }
1927   };
f11541 1928
4e17e6 1929
T 1930   /*********************************************************/
1931   /*********        message compose methods        *********/
1932   /*********************************************************/
1cded8 1933   
977a29 1934   // checks the input fields before sending a message
T 1935   this.check_compose_input = function()
1936     {
1937     // check input fields
1938     var input_to = rcube_find_object('_to');
e8f8fe 1939     var input_cc = rcube_find_object('_cc');
T 1940     var input_bcc = rcube_find_object('_bcc');
fd51e0 1941     var input_from = rcube_find_object('_from');
977a29 1942     var input_subject = rcube_find_object('_subject');
T 1943     var input_message = rcube_find_object('_message');
1944
fd51e0 1945     // check sender (if have no identities)
A 1946     if (input_from.type == 'text' && !rcube_check_email(input_from.value, true))
1947       {
1948       alert(this.get_label('nosenderwarning'));
1949       input_from.focus();
1950       return false;
1951       }
1952
977a29 1953     // check for empty recipient
e8f8fe 1954     var recipients = input_to.value ? input_to.value : (input_cc.value ? input_cc.value : input_bcc.value);
T 1955     if (!rcube_check_email(recipients.replace(/^\s+/, '').replace(/[\s,;]+$/, ''), true))
977a29 1956       {
T 1957       alert(this.get_label('norecipientwarning'));
1958       input_to.focus();
1959       return false;
1960       }
1961
1962     // display localized warning for missing subject
1963     if (input_subject && input_subject.value == '')
1964       {
1965       var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
1966
1967       // user hit cancel, so don't send
1968       if (!subject && subject !== '')
1969         {
1970         input_subject.focus();
1971         return false;
1972         }
1973       else
1974         {
1975         input_subject.value = subject ? subject : this.get_label('nosubject');            
1976         }
1977       }
1978
1979     // check for empty body
4ca10b 1980     if ((!window.tinyMCE || !tinyMCE.get('compose-body')) && input_message.value == '' && !confirm(this.get_label('nobodywarning')))
977a29 1981       {
31d9ef 1982       input_message.focus();
T 1983       return false;
977a29 1984       }
4ca10b 1985     else if (window.tinyMCE && tinyMCE.get('compose-body') && !tinyMCE.get('compose-body').getContent() && !confirm(this.get_label('nobodywarning')))
5f0724 1986       {
A 1987       tinyMCE.get('compose-body').focus();
1988       return false;
1989       }
977a29 1990
T 1991     return true;
1992     };
41fa0b 1993
4ca10b 1994   this.display_spellcheck_controls = function(vis)
T 1995   {
1996     if (this.env.spellcheck) {
f4b868 1997       // stop spellchecking process
A 1998       if (!vis && !this.spellcheck_ready) 
1999         {
2000     exec_event(this.env.spellcheck.check_link, 'click');
2001     this.set_spellcheck_state('ready');
2002     }
2003                   
4ca10b 2004       this.env.spellcheck.check_link.style.visibility = vis ? 'visible' : 'hidden';
T 2005       this.env.spellcheck.switch_lan_pic.style.visibility = vis ? 'visible' : 'hidden';
2006     }
2007   };
41fa0b 2008
e170b4 2009   this.set_spellcheck_state = function(s)
T 2010     {
86958f 2011     this.spellcheck_ready = (s=='check_spelling' || s=='ready');
e170b4 2012     this.enable_command('spellcheck', this.spellcheck_ready);
86958f 2013     };
e170b4 2014
f11541 2015   this.set_draft_id = function(id)
T 2016     {
2017     var f;
2018     if (f = rcube_find_object('_draft_saveid'))
2019       f.value = id;
2020     };
2021
f0f98f 2022   this.auto_save_start = function()
S 2023     {
9a5261 2024     if (this.env.draft_autosave)
cfdf04 2025       this.save_timer = self.setTimeout(function(){ ref.command("savedraft"); }, this.env.draft_autosave * 1000);
4b9efb 2026
S 2027     // Unlock interface now that saving is complete
2028     this.busy = false;
41fa0b 2029     };
T 2030
f11541 2031   this.compose_field_hash = function(save)
977a29 2032     {
T 2033     // check input fields
2034     var input_to = rcube_find_object('_to');
c17991 2035     var input_cc = rcube_find_object('_cc');
S 2036     var input_bcc = rcube_find_object('_bcc');
977a29 2037     var input_subject = rcube_find_object('_subject');
c5fb69 2038     var editor, input_message;
977a29 2039     var str = '';
c5fb69 2040     
977a29 2041     if (input_to && input_to.value)
T 2042       str += input_to.value+':';
2043     if (input_cc && input_cc.value)
2044       str += input_cc.value+':';
2045     if (input_bcc && input_bcc.value)
2046       str += input_bcc.value+':';
2047     if (input_subject && input_subject.value)
2048       str += input_subject.value+':';
c5fb69 2049     
A 2050     if (editor = tinyMCE.get('compose-body'))
2051       str += editor.getContent();
2052     else
2053       {
2054       input_message = rcube_find_object('_message');
977a29 2055       str += input_message.value;
c5fb69 2056       }
f11541 2057     
T 2058     if (save)
2059       this.cmp_hash = str;
2060     
977a29 2061     return str;
T 2062     };
2063     
1cded8 2064   this.change_identity = function(obj)
T 2065     {
2066     if (!obj || !obj.options)
2067       return false;
2068
2069     var id = obj.options[obj.selectedIndex].value;
2070     var input_message = rcube_find_object('_message');
2071     var message = input_message ? input_message.value : '';
a0109c 2072     var is_html = (rcube_find_object('_is_html').value == '1');
749b07 2073     var sig, p;
1cded8 2074
1966c5 2075     if (!this.env.identity)
S 2076       this.env.identity = id
a0109c 2077   
S 2078     if (!is_html)
1cded8 2079       {
a0109c 2080       // remove the 'old' signature
S 2081       if (this.env.identity && this.env.signatures && this.env.signatures[this.env.identity])
2082         {
53bd8f 2083         if (this.env.signatures[this.env.identity]['is_html'])
A 2084           sig = this.env.signatures[this.env.identity]['plain_text'];
2085         else
2086       sig = this.env.signatures[this.env.identity]['text'];
2087         
2088     if (sig.indexOf('-- ')!=0)
dd792e 2089           sig = '-- \n'+sig;
S 2090
a0109c 2091         p = message.lastIndexOf(sig);
S 2092         if (p>=0)
2093           message = message.substring(0, p-1) + message.substring(p+sig.length, message.length);
2094         }
dd792e 2095
a0109c 2096       // add the new signature string
S 2097       if (this.env.signatures && this.env.signatures[id])
2098         {
2099         sig = this.env.signatures[id]['text'];
dd792e 2100         if (this.env.signatures[id]['is_html'])
S 2101           {
2102           sig = this.env.signatures[id]['plain_text'];
2103           }
2104         if (sig.indexOf('-- ')!=0)
2105           sig = '-- \n'+sig;
a0109c 2106         message += '\n'+sig;
S 2107         }
1cded8 2108       }
a0109c 2109     else
1cded8 2110       {
d9344f 2111       var editor = tinyMCE.get('compose-body');
a0109c 2112
910d07 2113       if (this.env.signatures)
dd792e 2114         {
910d07 2115         // Append the signature as a div within the body
d9344f 2116         var sigElem = editor.dom.get("_rc_sig");
910d07 2117     var newsig = '';
A 2118     var htmlsig = true;
2119     
dd792e 2120         if (!sigElem)
a0109c 2121           {
910d07 2122           sigElem = editor.getDoc().createElement("div");
dd792e 2123           sigElem.setAttribute("id", "_rc_sig");
d9344f 2124           editor.getBody().appendChild(sigElem);
a0109c 2125           }
910d07 2126
A 2127     if (this.env.signatures[id])
2128       {
0e109c 2129       newsig = this.env.signatures[id]['text'];
910d07 2130       htmlsig = this.env.signatures[id]['is_html'];
A 2131       }
2132
2133         if (htmlsig)
2134           sigElem.innerHTML = newsig;
dd792e 2135         else
910d07 2136           sigElem.innerHTML = '<pre>' + newsig + '</pre>';
dd792e 2137         }
1cded8 2138       }
T 2139
749b07 2140     if (input_message)
1cded8 2141       input_message.value = message;
a0109c 2142
1cded8 2143     this.env.identity = id;
1c5853 2144     return true;
1cded8 2145     };
4e17e6 2146
T 2147   this.show_attachment_form = function(a)
2148     {
2149     if (!this.gui_objects.uploadbox)
2150       return false;
2151       
2152     var elm, list;
2153     if (elm = this.gui_objects.uploadbox)
2154       {
2155       if (a &&  (list = this.gui_objects.attachmentlist))
2156         {
2157         var pos = rcube_get_object_pos(list);
2158         var left = pos.x;
2159         var top = pos.y + list.offsetHeight + 10;
2160       
2161         elm.style.top = top+'px';
2162         elm.style.left = left+'px';
2163         }
2164       
2165       elm.style.visibility = a ? 'visible' : 'hidden';
2166       }
2167       
2168     // clear upload form
480f5d 2169     try {
T 2170       if (!a && this.gui_objects.attachmentform != this.gui_objects.messageform)
2171           this.gui_objects.attachmentform.reset();
2172     }
2173     catch(e){}  // ignore errors
1c5853 2174     
S 2175     return true;  
4e17e6 2176     };
T 2177
2178   // upload attachment file
2179   this.upload_file = function(form)
2180     {
2181     if (!form)
2182       return false;
2183       
2184     // get file input fields
2185     var send = false;
2186     for (var n=0; n<form.elements.length; n++)
2187       if (form.elements[n].type=='file' && form.elements[n].value)
2188         {
2189         send = true;
2190         break;
2191         }
2192     
2193     // create hidden iframe and post upload form
2194     if (send)
2195       {
2196       var ts = new Date().getTime();
2197       var frame_name = 'rcmupload'+ts;
2198
2199       // have to do it this way for IE
2200       // otherwise the form will be posted to a new window
ee0da5 2201       if(document.all)
4e17e6 2202         {
T 2203         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
2204         document.body.insertAdjacentHTML('BeforeEnd',html);
2205         }
2206       else  // for standards-compilant browsers
2207         {
2208         var frame = document.createElement('IFRAME');
2209         frame.name = frame_name;
ee0da5 2210         frame.style.border = 'none';
A 2211         frame.style.width = 0;
2212         frame.style.height = 0;
4e17e6 2213         frame.style.visibility = 'hidden';
T 2214         document.body.appendChild(frame);
2215         }
2216
2217       form.target = frame_name;
2218       form.action = this.env.comm_path+'&_action=upload';
2219       form.setAttribute('enctype', 'multipart/form-data');
2220       form.submit();
2221       }
2222     
2223     // set reference to the form object
2224     this.gui_objects.attachmentform = form;
1c5853 2225     return true;
4e17e6 2226     };
T 2227
2228   // add file name to attachment list
2229   // called from upload page
9a5261 2230   this.add2attachment_list = function(name, content)
4e17e6 2231     {
T 2232     if (!this.gui_objects.attachmentlist)
2233       return false;
aade7b 2234       
4e17e6 2235     var li = document.createElement('LI');
a894ba 2236     li.id = name;
S 2237     li.innerHTML = content;
4e17e6 2238     this.gui_objects.attachmentlist.appendChild(li);
1c5853 2239     return true;
4e17e6 2240     };
T 2241
a894ba 2242   this.remove_from_attachment_list = function(name)
S 2243     {
2244     if (!this.gui_objects.attachmentlist)
2245       return false;
2246
2247     var list = this.gui_objects.attachmentlist.getElementsByTagName("li");
2248     for (i=0;i<list.length;i++)
2249       if (list[i].id == name)
9a5261 2250         this.gui_objects.attachmentlist.removeChild(list[i]);
41fa0b 2251     };
a894ba 2252
S 2253   this.remove_attachment = function(name)
2254     {
2255     if (name)
8d0758 2256       this.http_post('remove-attachment', '_file='+urlencode(name));
a894ba 2257
S 2258     return true;
41fa0b 2259     };
4e17e6 2260
T 2261   // send remote request to add a new contact
2262   this.add_contact = function(value)
2263     {
2264     if (value)
f11541 2265       this.http_post('addcontact', '_address='+value);
1c5853 2266     
S 2267     return true;
4e17e6 2268     };
T 2269
f11541 2270   // send remote request to search mail or contacts
T 2271   this.qsearch = function(value)
4647e1 2272     {
f11541 2273     if (value != '')
4647e1 2274       {
f11541 2275       if (this.message_list)
T 2276         this.message_list.clear();
2277       else if (this.contact_list) {
2278         this.contact_list.clear(true);
2279         this.show_contentframe(false);
2280       }
2281
2282       // reset vars
2283       this.env.current_page = 1;
4647e1 2284       this.set_busy(true, 'searching');
c5418b 2285       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 2286       }
1c5853 2287     return true;
4647e1 2288     };
T 2289
2290   // reset quick-search form
2291   this.reset_qsearch = function()
2292     {
2293     if (this.gui_objects.qsearchbox)
2294       this.gui_objects.qsearchbox.value = '';
2295       
2296     this.env.search_request = null;
1c5853 2297     return true;
4647e1 2298     };
41fa0b 2299
9a5762 2300   this.sent_successfully = function(type, msg)
41fa0b 2301     {
T 2302     this.list_mailbox();
9a5762 2303     this.display_message(msg, type, true);
41fa0b 2304     }
T 2305
4e17e6 2306
T 2307   /*********************************************************/
2308   /*********     keyboard live-search methods      *********/
2309   /*********************************************************/
2310
2311   // handler for keyboard events on address-fields
2312   this.ksearch_keypress = function(e, obj)
2313     {
2314     if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
2315       return true;
2316
2317     if (this.ksearch_timer)
2318       clearTimeout(this.ksearch_timer);
2319
2320     var highlight;
9bbb17 2321     var key = rcube_event.get_keycode(e);
T 2322     var mod = rcube_event.get_modifier(e);
4e17e6 2323
T 2324     switch (key)
2325       {
2326       case 38:  // key up
2327       case 40:  // key down
2328         if (!this.ksearch_pane)
2329           break;
2330           
2331         var dir = key==38 ? 1 : 0;
2332         var next;
2333         
2334         highlight = document.getElementById('rcmksearchSelected');
2335         if (!highlight)
2336           highlight = this.ksearch_pane.ul.firstChild;
2337         
2338         if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
2339           {
2340           highlight.removeAttribute('id');
2341           this.set_classname(highlight, 'selected', false);
2342           }
2343
2344         if (next)
2345           {
2346           next.setAttribute('id', 'rcmksearchSelected');
2347           this.set_classname(next, 'selected', true);
2348           this.ksearch_selected = next._rcm_id;
2349           }
2350
86958f 2351         return rcube_event.cancel(e);
4e17e6 2352
T 2353       case 9:  // tab
9bbb17 2354         if(mod == SHIFT_KEY)
4e17e6 2355           break;
T 2356
2357       case 13:  // enter     
2358         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
2359           break;
2360
86958f 2361         // insert selected address and hide ksearch pane
T 2362         this.insert_recipient(this.ksearch_selected);
4e17e6 2363         this.ksearch_hide();
86958f 2364
T 2365         return rcube_event.cancel(e);
4e17e6 2366
T 2367       case 27:  // escape
2368         this.ksearch_hide();
2369         break;
2370
2371       }
2372
2373     // start timer
dbbd1f 2374     this.ksearch_timer = window.setTimeout(function(){ ref.ksearch_get_results(); }, 200);
4e17e6 2375     this.ksearch_input = obj;
T 2376     
2377     return true;
2378     };
86958f 2379
T 2380   this.insert_recipient = function(id)
2381   {
2382     if (!this.env.contacts[id] || !this.ksearch_input)
2383       return;
2384     
2385     // get cursor pos
2386     var inp_value = this.ksearch_input.value.toLowerCase();
2387     var cpos = this.get_caret_pos(this.ksearch_input);
2388     var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
2389     
2390     // replace search string with full address
2391     var pre = this.ksearch_input.value.substring(0, p);
2392     var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
2393     var insert  = this.env.contacts[id]+', ';
2394     this.ksearch_input.value = pre + insert + end;
2395     
2396     // set caret to insert pos
2397     cpos = p+insert.length;
2398     if (this.ksearch_input.setSelectionRange)
2399       this.ksearch_input.setSelectionRange(cpos, cpos);
2400   };
4e17e6 2401
T 2402   // address search processor
2403   this.ksearch_get_results = function()
2404     {
2405     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
2406     if (inp_value===null)
2407       return;
2408
2409     // get string from current cursor pos to last comma
2410     var cpos = this.get_caret_pos(this.ksearch_input);
2411     var p = inp_value.lastIndexOf(',', cpos-1);
2412     var q = inp_value.substring(p+1, cpos);
2413
2414     // trim query string
2415     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
2416
2417     if (!q.length || q==this.ksearch_value)
2418       {
2419       if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
2420         this.ksearch_pane.show(0);
2421
2422       return;
2423       }
2424
2425     this.ksearch_value = q;
2426     
2427     // start searching the contact list
2428     var a_results = new Array();
2429     var a_result_ids = new Array();
2430     var c=0;
2431     for (var i=0; i<this.env.contacts.length; i++)
2432       {
2433       if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
2434         {
2435         a_results[c] = this.env.contacts[i];
2436         a_result_ids[c++] = i;
2437         
2438         if (c==15)  // limit search results
2439           break;
2440         }
2441       }
2442
2443     // display search results
2444     if (c && a_results.length)
2445       {
2446       var p, ul, li;
2447       
2448       // create results pane if not present
2449       if (!this.ksearch_pane)
2450         {
2451         ul = document.createElement('UL');
2452         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
2453         this.ksearch_pane.elm.appendChild(ul);
2454         this.ksearch_pane.ul = ul;
2455         }
2456       else
2457         ul = this.ksearch_pane.ul;
2458
2459       // remove all search results
2460       ul.innerHTML = '';
2461             
2462       // add each result line to list
2463       for (i=0; i<a_results.length; i++)
2464         {
2465         li = document.createElement('LI');
2466         li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
2467         li._rcm_id = a_result_ids[i];
2468         ul.appendChild(li);
2469         }
2470
2471       // check if last selected item is still in result list
2472       if (this.ksearch_selected!==null)
2473         {
2474         p = find_in_array(this.ksearch_selected, a_result_ids);
2475         if (p>=0 && ul.childNodes)
2476           {
2477           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
2478           this.set_classname(ul.childNodes[p], 'selected', true);
2479           }
2480         else
2481           this.ksearch_selected = null;
2482         }
2483       
2484       // if no item selected, select the first one
2485       if (this.ksearch_selected===null)
2486         {
2487         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
2488         this.set_classname(ul.firstChild, 'selected', true);
2489         this.ksearch_selected = a_result_ids[0];
2490         }
2491
2492       // move the results pane right under the input box and make it visible
2493       var pos = rcube_get_object_pos(this.ksearch_input);
2494       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
2495       this.ksearch_pane.show(1); 
2496       }
2497     // hide results pane
2498     else
2499       this.ksearch_hide();
2500     };
2501
2502   this.ksearch_blur = function(e, obj)
2503     {
2504     if (this.ksearch_timer)
2505       clearTimeout(this.ksearch_timer);
2506
2507     this.ksearch_value = '';      
2508     this.ksearch_input = null;
2509     
2510     this.ksearch_hide();
2511     };
2512
2513
2514   this.ksearch_hide = function()
2515     {
2516     this.ksearch_selected = null;
2517     
2518     if (this.ksearch_pane)
2519       this.ksearch_pane.show(0);    
2520     };
2521
2522
2523   /*********************************************************/
2524   /*********         address book methods          *********/
2525   /*********************************************************/
2526
6b47de 2527   this.contactlist_keypress = function(list)
T 2528     {
2529       if (list.key_pressed == list.DELETE_KEY)
2530         this.command('delete');
2531     };
2532
2533   this.contactlist_select = function(list)
2534     {
f11541 2535       if (this.preview_timer)
T 2536         clearTimeout(this.preview_timer);
2537
2538       var id, frame, ref = this;
6b47de 2539       if (id = list.get_single_selection())
26f5b0 2540         this.preview_timer = window.setTimeout(function(){ ref.load_contact(id, 'show'); }, 200);
f11541 2541       else if (this.env.contentframe)
T 2542         this.show_contentframe(false);
6b47de 2543
f11541 2544       this.enable_command('compose', list.selection.length > 0);
f74b28 2545       this.enable_command('edit', (id && this.env.address_sources && !this.env.address_sources[this.env.source].readonly) ? true : false);
f11541 2546       this.enable_command('delete', list.selection.length && this.env.address_sources && !this.env.address_sources[this.env.source].readonly);
6b47de 2547
f11541 2548       return false;
6b47de 2549     };
T 2550
f11541 2551   this.list_contacts = function(src, page)
4e17e6 2552     {
T 2553     var add_url = '';
2554     var target = window;
2555     
f11541 2556     if (!src)
T 2557       src = this.env.source;
2558     
2559     if (page && this.current_page==page && src == this.env.source)
4e17e6 2560       return false;
f11541 2561       
T 2562     if (src != this.env.source)
2563       {
2564       page = 1;
2565       this.env.current_page = page;
6b603d 2566       this.reset_qsearch();
f11541 2567       }
T 2568
2569     this.select_folder(src, this.env.source);
2570     this.env.source = src;
4e17e6 2571
T 2572     // load contacts remotely
2573     if (this.gui_objects.contactslist)
2574       {
f11541 2575       this.list_contacts_remote(src, page);
4e17e6 2576       return;
T 2577       }
2578
2579     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2580       {
2581       target = window.frames[this.env.contentframe];
2582       add_url = '&_framed=1';
2583       }
2584
f11541 2585     // also send search request to get the correct listing
T 2586     if (this.env.search_request)
2587       add_url += '&_search='+this.env.search_request;
2588
4e17e6 2589     this.set_busy(true, 'loading');
f11541 2590     target.location.href = this.env.comm_path+(src ? '&_source='+urlencode(src) : '')+(page ? '&_page='+page : '')+add_url;
4e17e6 2591     };
T 2592
2593   // send remote request to load contacts list
f11541 2594   this.list_contacts_remote = function(src, page)
4e17e6 2595     {
6b47de 2596     // clear message list first
f11541 2597     this.contact_list.clear(true);
T 2598     this.show_contentframe(false);
2599     this.enable_command('delete', 'compose', false);
4e17e6 2600
T 2601     // send request to server
fc7374 2602     var url = (src ? '_source='+urlencode(src) : '') + (page ? (src?'&':'') + '_page='+page : '');
f11541 2603     this.env.source = src;
T 2604     
2605     // also send search request to get the right messages 
2606     if (this.env.search_request) 
2607       url += '&_search='+this.env.search_request;
2608
4e17e6 2609     this.set_busy(true, 'loading');
ecf759 2610     this.http_request('list', url, true);
4e17e6 2611     };
T 2612
2613   // load contact record
2614   this.load_contact = function(cid, action, framed)
2615     {
2616     var add_url = '';
2617     var target = window;
2618     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2619       {
2620       add_url = '&_framed=1';
2621       target = window.frames[this.env.contentframe];
f11541 2622       this.show_contentframe(true);
4e17e6 2623       }
T 2624     else if (framed)
2625       return false;
f11541 2626       
T 2627     if (action && (cid || action=='add') && !this.drag_active)
4e17e6 2628       {
T 2629       this.set_busy(true);
f11541 2630       target.location.href = this.env.comm_path+'&_action='+action+'&_source='+urlencode(this.env.source)+'&_cid='+urlencode(cid) + add_url;
4e17e6 2631       }
1c5853 2632     return true;
4e17e6 2633     };
f11541 2634
T 2635   // copy a contact to the specified target (group or directory)
2636   this.copy_contact = function(cid, to)
bf2f39 2637     {
f11541 2638     if (!cid)
T 2639       cid = this.contact_list.get_selection().join(',');
2640
2641     if (to != this.env.source && cid && this.env.address_sources[to] && !this.env.address_sources[to].readonly)
2642       this.http_post('copy', '_cid='+urlencode(cid)+'&_source='+urlencode(this.env.source)+'&_to='+urlencode(to));
bf2f39 2643     };
4e17e6 2644
T 2645
2646   this.delete_contacts = function()
2647     {
2648     // exit if no mailbox specified or if selection is empty
6b47de 2649     var selection = this.contact_list.get_selection();
T 2650     if (!(selection.length || this.env.cid) || !confirm(this.get_label('deletecontactconfirm')))
4e17e6 2651       return;
5e3512 2652       
4e17e6 2653     var a_cids = new Array();
b15568 2654     var qs = '';
4e17e6 2655
T 2656     if (this.env.cid)
2657       a_cids[a_cids.length] = this.env.cid;
2658     else
2659       {
2660       var id;
6b47de 2661       for (var n=0; n<selection.length; n++)
4e17e6 2662         {
6b47de 2663         id = selection[n];
4e17e6 2664         a_cids[a_cids.length] = id;
f4f8c6 2665         this.contact_list.remove_row(id, (n == selection.length-1));
4e17e6 2666         }
T 2667
2668       // hide content frame if we delete the currently displayed contact
f11541 2669       if (selection.length == 1)
T 2670         this.show_contentframe(false);
4e17e6 2671       }
T 2672
b15568 2673     // also send search request to get the right records from the next page
T 2674     if (this.env.search_request) 
2675       qs += '&_search='+this.env.search_request;
2676
4e17e6 2677     // send request to server
4f9c83 2678     this.http_post('delete', '_cid='+urlencode(a_cids.join(','))+'&_source='+urlencode(this.env.source)+'&_from='+(this.env.action ? this.env.action : '')+qs);
1c5853 2679     return true;
4e17e6 2680     };
T 2681
2682   // update a contact record in the list
2683   this.update_contact_row = function(cid, cols_arr)
2684     {
6b47de 2685     var row;
T 2686     if (this.contact_list.rows[cid] && (row = this.contact_list.rows[cid].obj))
2687       {
2688       for (var c=0; c<cols_arr.length; c++)
2689         if (row.cells[c])
2690           row.cells[c].innerHTML = cols_arr[c];
2691
2692       return true;
2693       }
2694
2695     return false;
4e17e6 2696     };
T 2697
2698
2699   /*********************************************************/
2700   /*********        user settings methods          *********/
2701   /*********************************************************/
2702
b0dbf3 2703   this.init_subscription_list = function()
S 2704     {
2705     var p = this;
68b6a9 2706     this.subscription_list = new rcube_list_widget(this.gui_objects.subscriptionlist, {multiselect:false, draggable:true, keyboard:false, toggleselect:true});
b0dbf3 2707     this.subscription_list.addEventListener('select', function(o){ p.subscription_select(o); });
S 2708     this.subscription_list.addEventListener('dragstart', function(o){ p.drag_active = true; });
2709     this.subscription_list.addEventListener('dragend', function(o){ p.subscription_move_folder(o); });
68b6a9 2710     this.subscription_list.row_init = function (row)
S 2711       {
2712       var anchors = row.obj.getElementsByTagName('A');
2713       if (anchors[0])
0aa430 2714         anchors[0].onclick = function() { p.rename_folder(row.id); return false; };
68b6a9 2715       if (anchors[1])
0aa430 2716         anchors[1].onclick = function() { p.delete_folder(row.id); return false; };
68b6a9 2717       row.obj.onmouseover = function() { p.focus_subscription(row.id); };
S 2718       row.obj.onmouseout = function() { p.unfocus_subscription(row.id); };
2719       }
b0dbf3 2720     this.subscription_list.init();
S 2721     }
2722
6b47de 2723   this.identity_select = function(list)
T 2724     {
2725     var id;
2726     if (id = list.get_single_selection())
2727       this.load_identity(id, 'edit-identity');
2728     };
4e17e6 2729
T 2730   // load contact record
2731   this.load_identity = function(id, action)
2732     {
2733     if (action=='edit-identity' && (!id || id==this.env.iid))
1c5853 2734       return false;
4e17e6 2735
T 2736     var add_url = '';
2737     var target = window;
2738     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
2739       {
2740       add_url = '&_framed=1';
2741       target = window.frames[this.env.contentframe];
2742       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
2743       }
2744
2745     if (action && (id || action=='add-identity'))
2746       {
2747       this.set_busy(true);
2748       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
2749       }
1c5853 2750     return true;
4e17e6 2751     };
T 2752
2753   this.delete_identity = function(id)
2754     {
2755     // exit if no mailbox specified or if selection is empty
6b47de 2756     var selection = this.identity_list.get_selection();
T 2757     if (!(selection.length || this.env.iid))
4e17e6 2758       return;
T 2759     
2760     if (!id)
6b47de 2761       id = this.env.iid ? this.env.iid : selection[0];
4e17e6 2762
T 2763     // if (this.env.framed && id)
6b47de 2764     this.goto_url('delete-identity', '_iid='+id, true);
1c5853 2765     return true;
b0dbf3 2766     };
S 2767
2768   this.focus_subscription = function(id)
2769     {
68b6a9 2770     var row, folder;
b0dbf3 2771     var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
3e71ab 2772
681a59 2773     if (this.drag_active && this.env.folder && (row = document.getElementById(id)))
3e71ab 2774       if (this.env.subscriptionrows[id] &&
S 2775           (folder = this.env.subscriptionrows[id][0]))
b0dbf3 2776         {
3e71ab 2777         if (this.check_droptarget(folder) &&
681a59 2778             !this.env.subscriptionrows[this.get_folder_row_id(this.env.folder)][2] &&    
A 2779         (folder != this.env.folder.replace(reg, '')) &&
3e71ab 2780             (!folder.match(new RegExp('^'+RegExp.escape(this.env.folder+this.env.delimiter)))))
b0dbf3 2781           {
3e71ab 2782           this.set_env('dstfolder', folder);
S 2783           this.set_classname(row, 'droptarget', true);
b0dbf3 2784           }
S 2785         }
3e71ab 2786       else if (this.env.folder.match(new RegExp(RegExp.escape(this.env.delimiter))))
b0dbf3 2787         {
3e71ab 2788         this.set_env('dstfolder', this.env.delimiter);
S 2789         this.set_classname(this.subscription_list.frame, 'droptarget', true);
b0dbf3 2790         }
S 2791     }
2792
2793   this.unfocus_subscription = function(id)
2794     {
3e71ab 2795       var row;
b0dbf3 2796       this.set_env('dstfolder', null);
3e71ab 2797       if (this.env.subscriptionrows[id] &&
S 2798           (row = document.getElementById(id)))
b0dbf3 2799         this.set_classname(row, 'droptarget', false);
3e71ab 2800       else
S 2801         this.set_classname(this.subscription_list.frame, 'droptarget', false);
b0dbf3 2802     }
S 2803
2804   this.subscription_select = function(list)
2805     {
68b6a9 2806     var id, folder;
S 2807     if ((id = list.get_single_selection()) &&
2808         this.env.subscriptionrows['rcmrow'+id] &&
681a59 2809         (folder = this.env.subscriptionrows['rcmrow'+id][0]))
68b6a9 2810       this.set_env('folder', folder);
S 2811     else
2812       this.set_env('folder', null);
a8d23d 2813       
T 2814     if (this.gui_objects.createfolderhint)
2815       this.gui_objects.createfolderhint.innerHTML = this.env.folder ? this.get_label('addsubfolderhint') : '';
b0dbf3 2816     };
S 2817
2818   this.subscription_move_folder = function(list)
2819     {
2820     var reg = RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
2821     if (this.env.folder && this.env.dstfolder && (this.env.dstfolder != this.env.folder) &&
2822         (this.env.dstfolder != this.env.folder.replace(reg, '')))
2823       {
2824       var reg = new RegExp('[^'+RegExp.escape(this.env.delimiter)+']*['+RegExp.escape(this.env.delimiter)+']', 'g');
2825       var basename = this.env.folder.replace(reg, '');
2826       var newname = this.env.dstfolder==this.env.delimiter ? basename : this.env.dstfolder+this.env.delimiter+basename;
2827       this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.folder)+'&_folder_newname='+urlencode(newname));
2828       }
2829     this.drag_active = false;
68b6a9 2830     this.unfocus_subscription(this.get_folder_row_id(this.env.dstfolder));
4e17e6 2831     };
T 2832
24053e 2833   // tell server to create and subscribe a new mailbox
4e17e6 2834   this.create_folder = function(name)
T 2835     {
a8d23d 2836     if (this.edit_folder)
T 2837       this.reset_folder_rename();
24053e 2838
4e17e6 2839     var form;
T 2840     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
6eaac2 2841       {
4e17e6 2842       name = form.elements['_folder_name'].value;
T 2843
6eaac2 2844       if (name.indexOf(this.env.delimiter)>=0)
A 2845         {
134eaf 2846         alert(this.get_label('forbiddencharacter')+' ('+this.env.delimiter+')');
6eaac2 2847         return false;
A 2848         }
2849
2850       if (this.env.folder && name != '')
2851         name = this.env.folder+this.env.delimiter+name;
2852
d93fc9 2853       this.set_busy(true, 'foldercreating');
8d0758 2854       this.http_post('create-folder', '_name='+urlencode(name), true);
6eaac2 2855       }
4e17e6 2856     else if (form.elements['_folder_name'])
T 2857       form.elements['_folder_name'].focus();
2858     };
24053e 2859
d19426 2860   // start renaming the mailbox name.
24053e 2861   // this will replace the name string with an input field
68b6a9 2862   this.rename_folder = function(id)
4e17e6 2863     {
24053e 2864     var temp, row, form;
T 2865
2866     // reset current renaming
681a59 2867     if (temp = this.edit_folder)
A 2868       {
2869       this.reset_folder_rename();
2870       if (temp == id)
2871         return;
2872       }
24053e 2873
0aa430 2874     if (id && this.env.subscriptionrows[id] && (row = document.getElementById(id)))
4e17e6 2875       {
b0dbf3 2876       var reg = new RegExp('.*['+RegExp.escape(this.env.delimiter)+']');
24053e 2877       this.name_input = document.createElement('INPUT');
681a59 2878       this.name_input.value = this.env.subscriptionrows[id][0].replace(reg, '');
24053e 2879       this.name_input.style.width = '100%';
681a59 2880
b0dbf3 2881       reg = new RegExp('['+RegExp.escape(this.env.delimiter)+']?[^'+RegExp.escape(this.env.delimiter)+']+$');
0aa430 2882       this.name_input.__parent = this.env.subscriptionrows[id][0].replace(reg, '');
24053e 2883       this.name_input.onkeypress = function(e){ rcmail.name_input_keypress(e); };
T 2884       
2885       row.cells[0].replaceChild(this.name_input, row.cells[0].firstChild);
2886       this.edit_folder = id;
2887       this.name_input.select();
2888       
2889       if (form = this.gui_objects.editform)
2890         form.onsubmit = function(){ return false; };
4e17e6 2891       }
1cded8 2892     };
T 2893
24053e 2894   // remove the input field and write the current mailbox name to the table cell
T 2895   this.reset_folder_rename = function()
1cded8 2896     {
24053e 2897     var cell = this.name_input ? this.name_input.parentNode : null;
681a59 2898
e170b4 2899     if (cell && this.edit_folder && this.env.subscriptionrows[this.edit_folder])
681a59 2900       cell.innerHTML = this.env.subscriptionrows[this.edit_folder][1];
24053e 2901       
T 2902     this.edit_folder = null;
2903     };
2904
2905   // handler for keyboard events on the input field
2906   this.name_input_keypress = function(e)
2907     {
9bbb17 2908     var key = rcube_event.get_keycode(e);
24053e 2909
T 2910     // enter
2911     if (key==13)
2912       {
2913       var newname = this.name_input ? this.name_input.value : null;
2914       if (this.edit_folder && newname)
b0dbf3 2915         {
134eaf 2916         if (newname.indexOf(this.env.delimiter)>=0)
6eaac2 2917           {
134eaf 2918           alert(this.get_label('forbiddencharacter')+' ('+this.env.delimiter+')');
6eaac2 2919           return false;
A 2920           }
2921
0aa430 2922         if (this.name_input.__parent)
T 2923           newname = this.name_input.__parent + this.env.delimiter + newname;
134eaf 2924
d93fc9 2925         this.set_busy(true, 'folderrenaming');
4e59f6 2926         this.http_post('rename-folder', '_folder_oldname='+urlencode(this.env.subscriptionrows[this.edit_folder][0])+'&_folder_newname='+urlencode(newname), true);
b0dbf3 2927         }
24053e 2928       }
T 2929     // escape
2930     else if (key==27)
2931       this.reset_folder_rename();
2932     };
2933
2934   // delete a specific mailbox with all its messages
68b6a9 2935   this.delete_folder = function(id)
24053e 2936     {
68b6a9 2937     var folder = this.env.subscriptionrows[id][0];
S 2938
41841b 2939     if (this.edit_folder)
S 2940       this.reset_folder_rename();
fdbb19 2941
68b6a9 2942     if (folder && confirm(this.get_label('deletefolderconfirm')))
S 2943       {
d93fc9 2944       this.set_busy(true, 'folderdeleting');
A 2945       this.http_post('delete-folder', '_mboxes='+urlencode(folder), true);
68b6a9 2946       this.set_env('folder', null);
0ed265 2947
T 2948       if (this.gui_objects.createfolderhint)
2949         this.gui_objects.createfolderhint.innerHTML = '';
68b6a9 2950       }
24053e 2951     };
T 2952
2953   // add a new folder to the subscription list by cloning a folder row
681a59 2954   this.add_folder_row = function(name, display_name, replace, before)
24053e 2955     {
T 2956     if (!this.gui_objects.subscriptionlist)
2957       return false;
2958
681a59 2959     // find not protected folder    
24053e 2960     for (var refid in this.env.subscriptionrows)
681a59 2961       if (this.env.subscriptionrows[refid]!=null && !this.env.subscriptionrows[refid][2])
1cded8 2962         break;
T 2963
24053e 2964     var refrow, form;
T 2965     var tbody = this.gui_objects.subscriptionlist.tBodies[0];
e8a89e 2966     var id = 'rcmrow'+(tbody.childNodes.length+1);
1f064b 2967     var selection = this.subscription_list.get_single_selection();
e8a89e 2968     
T 2969     if (replace && replace.id)
2970     {
2971       id = replace.id;
2972       refid = replace.id;
2973     }
24053e 2974
T 2975     if (!id || !(refrow = document.getElementById(refid)))
2976       {
2977       // Refresh page if we don't have a table row to clone
6b47de 2978       this.goto_url('folders');
24053e 2979       }
T 2980     else
2981       {
2982       // clone a table row if there are existing rows
2983       var row = this.clone_table_row(refrow);
68b6a9 2984       row.id = id;
681a59 2985
A 2986       if (before && (before = this.get_folder_row_id(before)))
f89f03 2987         tbody.insertBefore(row, document.getElementById(before));
24053e 2988       else
f89f03 2989         tbody.appendChild(row);
681a59 2990       
A 2991       if (replace)
f89f03 2992         tbody.removeChild(replace);
24053e 2993       }
681a59 2994
24053e 2995     // add to folder/row-ID map
681a59 2996     this.env.subscriptionrows[row.id] = [name, display_name, 0];
24053e 2997
T 2998     // set folder name
4d4264 2999     row.cells[0].innerHTML = display_name;
e8a89e 3000     
T 3001     // set messages count to zero
3002     if (!replace)
3003       row.cells[1].innerHTML = '*';
3004     
3005     if (!replace && row.cells[2] && row.cells[2].firstChild.tagName=='INPUT')
24053e 3006       {
e8a89e 3007       row.cells[2].firstChild.value = name;
T 3008       row.cells[2].firstChild.checked = true;
24053e 3009       }
e8a89e 3010     
24053e 3011     // add new folder to rename-folder list and clear input field
f9c107 3012     if (!replace && (form = this.gui_objects.editform))
24053e 3013       {
f9c107 3014       if (form.elements['_folder_oldname'])
T 3015         form.elements['_folder_oldname'].options[form.elements['_folder_oldname'].options.length] = new Option(name,name);
3016       if (form.elements['_folder_name'])
3017         form.elements['_folder_name'].value = ''; 
24053e 3018       }
T 3019
b0dbf3 3020     this.init_subscription_list();
b119e2 3021     if (selection && document.getElementById('rcmrow'+selection))
1f064b 3022       this.subscription_list.select_row(selection);
b0dbf3 3023
68b6a9 3024     if (document.getElementById(id).scrollIntoView)
S 3025       document.getElementById(id).scrollIntoView();
24053e 3026     };
T 3027
3028   // replace an existing table row with a new folder line
681a59 3029   this.replace_folder_row = function(oldfolder, newfolder, display_name, before)
24053e 3030     {
T 3031     var id = this.get_folder_row_id(oldfolder);
3032     var row = document.getElementById(id);
3033     
3034     // replace an existing table row (if found)
681a59 3035     this.add_folder_row(newfolder, display_name, row, before);
24053e 3036     
T 3037     // rename folder in rename-folder dropdown
3038     var form, elm;
3039     if ((form = this.gui_objects.editform) && (elm = form.elements['_folder_oldname']))
3040       {
3041       for (var i=0;i<elm.options.length;i++)
3042         {
3043         if (elm.options[i].value == oldfolder)
3044           {
4d4264 3045           elm.options[i].text = display_name;
24053e 3046           elm.options[i].value = newfolder;
T 3047           break;
3048           }
3049         }
3050
3051       form.elements['_folder_newname'].value = '';
3052       }
3053     };
3054
3055   // remove the table row of a specific mailbox from the table
3056   // (the row will not be removed, just hidden)
3057   this.remove_folder_row = function(folder)
3058     {
1cded8 3059     var row;
24053e 3060     var id = this.get_folder_row_id(folder);
1cded8 3061     if (id && (row = document.getElementById(id)))
681a59 3062       row.style.display = 'none';
c8c1e0 3063
S 3064     // remove folder from rename-folder list
3065     var form;
3066     if ((form = this.gui_objects.editform) && form.elements['_folder_oldname'])
3067       {
3068       for (var i=0;i<form.elements['_folder_oldname'].options.length;i++)
3069         {
3070         if (form.elements['_folder_oldname'].options[i].value == folder) 
24053e 3071           {
c8c1e0 3072           form.elements['_folder_oldname'].options[i] = null;
24053e 3073           break;
c8c1e0 3074           }
S 3075         }
3076       }
24053e 3077     
f9c107 3078     if (form && form.elements['_folder_newname'])
T 3079       form.elements['_folder_newname'].value = '';
4e17e6 3080     };
T 3081
3082   this.subscribe_folder = function(folder)
3083     {
c5097c 3084     if (folder)
A 3085       this.http_post('subscribe', '_mbox='+urlencode(folder));
4e17e6 3086     };
T 3087
3088   this.unsubscribe_folder = function(folder)
3089     {
c5097c 3090     if (folder)
A 3091       this.http_post('unsubscribe', '_mbox='+urlencode(folder));
4e17e6 3092     };
T 3093     
24053e 3094   // helper method to find a specific mailbox row ID
T 3095   this.get_folder_row_id = function(folder)
3096     {
3097     for (var id in this.env.subscriptionrows)
4d4264 3098       if (this.env.subscriptionrows[id] && this.env.subscriptionrows[id][0] == folder)
24053e 3099         break;
T 3100         
3101     return id;
3102     };
4e17e6 3103
T 3104   // duplicate a specific table row
3105   this.clone_table_row = function(row)
3106     {
3107     var cell, td;
3108     var new_row = document.createElement('TR');
f89f03 3109     for(var n=0; n<row.cells.length; n++)
4e17e6 3110       {
f89f03 3111       cell = row.cells[n];
4e17e6 3112       td = document.createElement('TD');
T 3113
3114       if (cell.className)
3115         td.className = cell.className;
3116       if (cell.align)
3117         td.setAttribute('align', cell.align);
3118         
3119       td.innerHTML = cell.innerHTML;
3120       new_row.appendChild(td);
3121       }
3122     
3123     return new_row;
9bebdf 3124     };
S 3125
4e17e6 3126
T 3127   /*********************************************************/
3128   /*********           GUI functionality           *********/
3129   /*********************************************************/
3130
3131   // eable/disable buttons for page shifting
3132   this.set_page_buttons = function()
3133     {
3134     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
d17008 3135     this.enable_command('lastpage', (this.env.pagecount > this.env.current_page));
4e17e6 3136     this.enable_command('previouspage', (this.env.current_page > 1));
d17008 3137     this.enable_command('firstpage', (this.env.current_page > 1));
4e17e6 3138     }
T 3139
3140   // set button to a specific state
3141   this.set_button = function(command, state)
3142     {
3143     var a_buttons = this.buttons[command];
3144     var button, obj;
3145
3146     if(!a_buttons || !a_buttons.length)
4da1d7 3147       return false;
4e17e6 3148
T 3149     for(var n=0; n<a_buttons.length; n++)
3150       {
3151       button = a_buttons[n];
3152       obj = document.getElementById(button.id);
3153
3154       // get default/passive setting of the button
104ee3 3155       if (obj && button.type=='image' && !button.status) {
4e17e6 3156         button.pas = obj._original_src ? obj._original_src : obj.src;
104ee3 3157         // respect PNG fix on IE browsers
T 3158         if (obj.runtimeStyle && obj.runtimeStyle.filter && obj.runtimeStyle.filter.match(/src=['"]([^'"]+)['"]/))
3159           button.pas = RegExp.$1;
3160       }
4e17e6 3161       else if (obj && !button.status)
T 3162         button.pas = String(obj.className);
3163
3164       // set image according to button state
3165       if (obj && button.type=='image' && button[state])
3166         {
3167         button.status = state;        
3168         obj.src = button[state];
3169         }
3170       // set class name according to button state
3171       else if (obj && typeof(button[state])!='undefined')
3172         {
3173         button.status = state;        
3174         obj.className = button[state];        
3175         }
3176       // disable/enable input buttons
3177       if (obj && button.type=='input')
3178         {
3179         button.status = state;
3180         obj.disabled = !state;
3181         }
3182       }
3183     };
3184
eb6842 3185   // display a specific alttext
T 3186   this.set_alttext = function(command, label)
3187     {
3188       if (!this.buttons[command] || !this.buttons[command].length)
3189         return;
3190       
3191       var button, obj, link;
3192       for (var n=0; n<this.buttons[command].length; n++)
3193       {
3194         button = this.buttons[command][n];
3195         obj = document.getElementById(button.id);
3196         
3197         if (button.type=='image' && obj)
3198         {
3199           obj.setAttribute('alt', this.get_label(label));
3200           if ((link = obj.parentNode) && link.tagName == 'A')
3201             link.setAttribute('title', this.get_label(label));
3202         }
3203         else if (obj)
3204           obj.setAttribute('title', this.get_label(label));
3205       }
3206     };
4e17e6 3207
T 3208   // mouse over button
3209   this.button_over = function(command, id)
3210     {
3211     var a_buttons = this.buttons[command];
3212     var button, img;
3213
3214     if(!a_buttons || !a_buttons.length)
4da1d7 3215       return false;
4e17e6 3216
T 3217     for(var n=0; n<a_buttons.length; n++)
3218       {
3219       button = a_buttons[n];
3220       if(button.id==id && button.status=='act')
3221         {
3222         img = document.getElementById(button.id);
3223         if (img && button.over)
3224           img.src = button.over;
3225         }
3226       }
4da1d7 3227       
4e17e6 3228     };
T 3229
c8c1e0 3230   // mouse down on button
S 3231   this.button_sel = function(command, id)
3232     {
3233     var a_buttons = this.buttons[command];
3234     var button, img;
3235
3236     if(!a_buttons || !a_buttons.length)
3237       return;
3238
3239     for(var n=0; n<a_buttons.length; n++)
3240       {
3241       button = a_buttons[n];
3242       if(button.id==id && button.status=='act')
3243         {
3244         img = document.getElementById(button.id);
3245         if (img && button.sel)
3246           img.src = button.sel;
3247         }
3248       }
3249     };
4e17e6 3250
T 3251   // mouse out of button
3252   this.button_out = function(command, id)
3253     {
3254     var a_buttons = this.buttons[command];
3255     var button, img;
3256
3257     if(!a_buttons || !a_buttons.length)
3258       return;
3259
3260     for(var n=0; n<a_buttons.length; n++)
3261       {
3262       button = a_buttons[n];
3263       if(button.id==id && button.status=='act')
3264         {
3265         img = document.getElementById(button.id);
3266         if (img && button.act)
3267           img.src = button.act;
3268         }
3269       }
3270     };
3271
3272   // set/unset a specific class name
3273   this.set_classname = function(obj, classname, set)
3274     {
3275     var reg = new RegExp('\s*'+classname, 'i');
3276     if (!set && obj.className.match(reg))
3277       obj.className = obj.className.replace(reg, '');
3278     else if (set && !obj.className.match(reg))
3279       obj.className += ' '+classname;
3280     };
3281
5eee00 3282   // write to the document/window title
T 3283   this.set_pagetitle = function(title)
3284   {
3285     if (title && document.title)
3286       document.title = title;
3287   }
3288
4e17e6 3289   // display a system message
T 3290   this.display_message = function(msg, type, hold)
3291     {
3292     if (!this.loaded)  // save message in order to display after page loaded
3293       {
3294       this.pending_message = new Array(msg, type);
3295       return true;
3296       }
b716bd 3297
T 3298     // pass command to parent window
3299     if (this.env.framed && parent.rcmail)
3300       return parent.rcmail.display_message(msg, type, hold);
3301
4e17e6 3302     if (!this.gui_objects.message)
T 3303       return false;
f9c107 3304
4e17e6 3305     if (this.message_timer)
T 3306       clearTimeout(this.message_timer);
3307     
3308     var cont = msg;
3309     if (type)
3310       cont = '<div class="'+type+'">'+cont+'</div>';
a95e0e 3311
b716bd 3312     var _rcube = this;
4e17e6 3313     this.gui_objects.message.innerHTML = cont;
T 3314     this.gui_objects.message.style.display = 'block';
b716bd 3315     
a95e0e 3316     if (type!='loading')
b716bd 3317       this.gui_objects.message.onmousedown = function(){ _rcube.hide_message(); return true; };
4e17e6 3318     
T 3319     if (!hold)
dbbd1f 3320       this.message_timer = window.setTimeout(function(){ ref.hide_message(); }, this.message_time);
4e17e6 3321     };
T 3322
3323   // make a message row disapear
3324   this.hide_message = function()
3325     {
3326     if (this.gui_objects.message)
a95e0e 3327       {
4e17e6 3328       this.gui_objects.message.style.display = 'none';
a95e0e 3329       this.gui_objects.message.onmousedown = null;
T 3330       }
4e17e6 3331     };
T 3332
3333   // mark a mailbox as selected and set environment variable
f11541 3334   this.select_folder = function(name, old)
T 3335   {
3336     if (this.gui_objects.folderlist)
4e17e6 3337     {
f11541 3338       var current_li, target_li;
597170 3339       
f11541 3340       if ((current_li = this.get_folder_li(old)))
T 3341       {
6a35c8 3342         this.set_classname(current_li, 'selected', false);
952860 3343         this.set_classname(current_li, 'unfocused', false);
4e17e6 3344       }
6b47de 3345
f11541 3346       if ((target_li = this.get_folder_li(name)))
fa4cd2 3347       {
f11541 3348         this.set_classname(target_li, 'unfocused', false);
T 3349         this.set_classname(target_li, 'selected', true);
fa4cd2 3350       }
f11541 3351     }
T 3352   };
3353
3354   // helper method to find a folder list item
3355   this.get_folder_li = function(name)
3356   {
3357     if (this.gui_objects.folderlist)
3358     {
3359       name = String(name).replace(this.identifier_expr, '');
3360       return document.getElementById('rcmli'+name);
3361     }
3362
3363     return null;
3364   };
24053e 3365
25d8ba 3366   // for reordering column array, Konqueror workaround
S 3367   this.set_message_coltypes = function(coltypes) 
3368   { 
24053e 3369     this.coltypes = coltypes;
T 3370     
3371     // set correct list titles
3372     var cell, col;
3373     var thead = this.gui_objects.messagelist ? this.gui_objects.messagelist.tHead : null;
3374     for (var n=0; thead && n<this.coltypes.length; n++) 
3375       {
3376       col = this.coltypes[n];
3377       if ((cell = thead.rows[0].cells[n+1]) && (col=='from' || col=='to'))
3378         {
3379         // if we have links for sorting, it's a bit more complicated...
3380         if (cell.firstChild && cell.firstChild.tagName=='A')
3381           {
3382           cell.firstChild.innerHTML = this.get_label(this.coltypes[n]);
3383           cell.firstChild.onclick = function(){ return rcmail.command('sort', this.__col, this); };
3384           cell.firstChild.__col = col;
3385           }
3386         else
3387           cell.innerHTML = this.get_label(this.coltypes[n]);
3388
3389         cell.id = 'rcmHead'+col;
3390         }
e189a6 3391       else if (col == 'subject' && this.message_list)
d24d20 3392         this.message_list.subject_col = n+1;
e189a6 3393       else if (col == 'flag' && this.env.unflaggedicon)
A 3394         {
3395       cell.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
3396     }
24053e 3397       }
T 3398   };
4e17e6 3399
T 3400   // create a table row in the message list
15a9d1 3401   this.add_message_row = function(uid, cols, flags, attachment, attop)
4e17e6 3402     {
6b47de 3403     if (!this.gui_objects.messagelist || !this.message_list)
4e17e6 3404       return false;
6b47de 3405
4e17e6 3406     var tbody = this.gui_objects.messagelist.tBodies[0];
T 3407     var rowcount = tbody.rows.length;
3408     var even = rowcount%2;
3409     
857a38 3410     this.env.messages[uid] = {deleted:flags.deleted?1:0,
S 3411                               replied:flags.replied?1:0,
e189a6 3412                               unread:flags.unread?1:0,
d73404 3413                   forwarded:flags.forwarded?1:0,
e189a6 3414                               flagged:flags.flagged?1:0};
4e17e6 3415     
T 3416     var row = document.createElement('TR');
3417     row.id = 'rcmrow'+uid;
a164a2 3418     row.className = 'message'
A 3419     + (even ? ' even' : ' odd')
3420         + (flags.unread ? ' unread' : '')
3421     + (flags.deleted ? ' deleted' : '')
3422     + (flags.flagged ? ' flagged' : '');            
6b47de 3423
T 3424     if (this.message_list.in_selection(uid))
4e17e6 3425       row.className += ' selected';
T 3426
d73404 3427     var icon = this.env.messageicon;
A 3428     if (flags.deleted && this.env.deletedicon)
3429       icon = this.env.deletedicon;
3430     else if (flags.replied && this.env.repliedicon)
3431       {
3432       if (flags.forwarded && this.env.forwardedrepliedicon)
3433         icon = this.env.forwardedrepliedicon;
3434       else
3435         icon = this.env.repliedicon;
3436       }
3437     else if (flags.forwarded && this.env.forwardedicon)
3438       icon = this.env.forwardedicon;
25c35c 3439     else if(flags.unread && this.env.unreadicon)
A 3440       icon = this.env.unreadicon;
d73404 3441     
4e17e6 3442     var col = document.createElement('TD');
T 3443     col.className = 'icon';
e189a6 3444     col.innerHTML = icon ? '<img src="'+icon+'" alt="" />' : '';
4e17e6 3445     row.appendChild(col);
T 3446
3447     // add each submitted col
25d8ba 3448     for (var n = 0; n < this.coltypes.length; n++) 
S 3449       { 
3450       var c = this.coltypes[n];
4e17e6 3451       col = document.createElement('TD');
T 3452       col.className = String(c).toLowerCase();
e189a6 3453       
A 3454       if (c=='flag')
3455         {
3456         if (flags.flagged && this.env.flaggedicon)
3457           col.innerHTML = '<img src="'+this.env.flaggedicon+'" alt="" />';
3458         else if(this.env.unflaggedicon)
3459           col.innerHTML = '<img src="'+this.env.unflaggedicon+'" alt="" />';
3460     }
3461       else
3462         col.innerHTML = cols[c];
3463
4e17e6 3464       row.appendChild(col);
T 3465       }
3466
3467     col = document.createElement('TD');
3468     col.className = 'icon';
e189a6 3469     col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" />' : '';
4e17e6 3470     row.appendChild(col);
6b47de 3471
T 3472     this.message_list.insert_row(row, attop);
095d05 3473
A 3474     // remove 'old' row
3475     if (attop && this.env.pagesize && this.message_list.rowcount > this.env.pagesize)
3476       {
3477     var uid = this.message_list.get_last_row();
3478         this.message_list.remove_row(uid);
3479     this.message_list.clear_selection(uid);
3480       }
4e17e6 3481     };
T 3482
3483   // replace content of row count display
3484   this.set_rowcount = function(text)
3485     {
3486     if (this.gui_objects.countdisplay)
3487       this.gui_objects.countdisplay.innerHTML = text;
3488
3489     // update page navigation buttons
3490     this.set_page_buttons();
3491     };
6d2714 3492
ac5d15 3493   // replace content of mailboxname display
T 3494   this.set_mailboxname = function(content)
3495     {
3496     if (this.gui_objects.mailboxname && content)
3497       this.gui_objects.mailboxname.innerHTML = content;
3498     };
3499
58e360 3500   // replace content of quota display
6d2714 3501   this.set_quota = function(content)
f11541 3502     {
6d2714 3503     if (this.gui_objects.quotadisplay && content)
A 3504       this.gui_objects.quotadisplay.innerHTML = content;
3505     };
6b47de 3506
4e17e6 3507   // update the mailboxlist
15a9d1 3508   this.set_unread_count = function(mbox, count, set_title)
4e17e6 3509     {
T 3510     if (!this.gui_objects.mailboxlist)
3511       return false;
25d8ba 3512
85360d 3513     this.env.unread_counts[mbox] = count;
T 3514     this.set_unread_count_display(mbox, set_title);
7f9d71 3515     }
S 3516
3517   // update the mailbox count display
3518   this.set_unread_count_display = function(mbox, set_title)
3519     {
85360d 3520     var reg, text_obj, item, mycount, childcount, div;
7f9d71 3521     if (item = this.get_folder_li(mbox))
S 3522       {
07d367 3523       mycount = this.env.unread_counts[mbox] ? this.env.unread_counts[mbox] : 0;
acbc48 3524       text_obj = item.getElementsByTagName('a')[0];
15a9d1 3525       reg = /\s+\([0-9]+\)$/i;
7f9d71 3526
835a0c 3527       childcount = 0;
S 3528       if ((div = item.getElementsByTagName('div')[0]) &&
3529           div.className.match(/collapsed/))
7f9d71 3530         {
S 3531         // add children's counters
07d367 3532         for (var k in this.env.unread_counts) 
df0dd4 3533           if (k.indexOf(mbox + this.env.delimiter) == 0) {
85360d 3534             childcount += this.env.unread_counts[k];
07d367 3535       }
7f9d71 3536         }
4e17e6 3537
cca626 3538       if (mycount && text_obj.innerHTML.match(reg))
S 3539         text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+mycount+')');
3540       else if (mycount)
3541         text_obj.innerHTML += ' ('+mycount+')';
15a9d1 3542       else
T 3543         text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
25d8ba 3544
7f9d71 3545       // set parent's display
07d367 3546       reg = new RegExp(RegExp.escape(this.env.delimiter) + '[^' + RegExp.escape(this.env.delimiter) + ']+$');
7f9d71 3547       if (mbox.match(reg))
S 3548         this.set_unread_count_display(mbox.replace(reg, ''), false);
3549
15a9d1 3550       // set the right classes
cca626 3551       this.set_classname(item, 'unread', (mycount+childcount)>0 ? true : false);
15a9d1 3552       }
T 3553
3554     // set unread count to window title
01c86f 3555     reg = /^\([0-9]+\)\s+/i;
25d8ba 3556     if (set_title && document.title)
15a9d1 3557       {
T 3558       var doc_title = String(document.title);
5eee00 3559       var new_title = "";
15a9d1 3560
85360d 3561       if (mycount && doc_title.match(reg))
T 3562         new_title = doc_title.replace(reg, '('+mycount+') ');
3563       else if (mycount)
3564         new_title = '('+mycount+') '+doc_title;
15a9d1 3565       else
5eee00 3566         new_title = doc_title.replace(reg, '');
T 3567         
3568       this.set_pagetitle(new_title);
01c86f 3569       }
4e17e6 3570     };
T 3571
3572   // add row to contacts list
6b47de 3573   this.add_contact_row = function(cid, cols, select)
4e17e6 3574     {
T 3575     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
3576       return false;
3577     
3578     var tbody = this.gui_objects.contactslist.tBodies[0];
3579     var rowcount = tbody.rows.length;
3580     var even = rowcount%2;
3581     
3582     var row = document.createElement('TR');
3583     row.id = 'rcmrow'+cid;
3584     row.className = 'contact '+(even ? 'even' : 'odd');
3585     
6b47de 3586     if (this.contact_list.in_selection(cid))
4e17e6 3587       row.className += ' selected';
T 3588
3589     // add each submitted col
3590     for (var c in cols)
3591       {
3592       col = document.createElement('TD');
3593       col.className = String(c).toLowerCase();
3594       col.innerHTML = cols[c];
3595       row.appendChild(col);
3596       }
3597     
6b47de 3598     this.contact_list.insert_row(row);
0dbac3 3599     this.enable_command('export', (this.contact_list.rowcount > 0));
4e17e6 3600     };
T 3601
d9344f 3602   this.toggle_editor = function(checkbox, textAreaId)
a0109c 3603     {
S 3604     var ischecked = checkbox.checked;
3bd94b 3605     var composeElement = document.getElementById(textAreaId);
A 3606     
a0109c 3607     if (ischecked)
S 3608       {
3bd94b 3609       var existingPlainText = composeElement.value;
A 3610       var htmlText = "<pre>" + existingPlainText + "</pre>";
3611       composeElement.value = htmlText;
3612       tinyMCE.execCommand('mceAddControl', true, textAreaId);
a0109c 3613       }
S 3614     else
3615       {
3bd94b 3616       var thisMCE = tinyMCE.get(textAreaId);
A 3617       var existingHtml = thisMCE.getContent();
3618       this.html2plain(existingHtml, textAreaId);
3619       tinyMCE.execCommand('mceRemoveControl', true, textAreaId);
a0109c 3620       }
4e17e6 3621     };
T 3622
712b30 3623   this.toggle_prefer_html = function(checkbox)
A 3624     {
3625     var addrbook_show_images;
3626     if (addrbook_show_images = document.getElementById('rcmfd_addrbook_show_images'))
3627       addrbook_show_images.disabled = !checkbox.checked;
3628     }
3629
e5686f 3630   // display fetched raw headers
A 3631   this.set_headers = function(content)
3632     {
3633     if (this.gui_objects.all_headers_row && this.gui_objects.all_headers_box && content)
3634       {
3635       var box = this.gui_objects.all_headers_box;
3636       box.innerHTML = content;
3637       box.style.display = 'block';
3638
3639       if (this.env.framed && parent.rcmail)
3640     parent.rcmail.set_busy(false);
3641       else
3642         this.set_busy(false);
3643       }
3644     };
a980cb 3645
e5686f 3646   // display all-headers row and fetch raw message headers
A 3647   this.load_headers = function(elem)
3648     {
3649     if (!this.gui_objects.all_headers_row || !this.gui_objects.all_headers_box || !this.env.uid)
3650       return;
3651     
3652     this.set_classname(elem, 'show-headers', false);
3653     this.set_classname(elem, 'hide-headers', true);
3654     this.gui_objects.all_headers_row.style.display = bw.ie ? 'block' : 'table-row';
3655     elem.onclick = function() { rcmail.hide_headers(elem); }
3656
3657     // fetch headers only once
3658     if (!this.gui_objects.all_headers_box.innerHTML)
3659       {
a980cb 3660       this.display_message(this.get_label('loading'), 'loading', true); 
e5686f 3661       this.http_post('headers', '_uid='+this.env.uid);
A 3662       }
3663     }
3664
3665   // hide all-headers row
3666   this.hide_headers = function(elem)
3667     {
3668     if (!this.gui_objects.all_headers_row || !this.gui_objects.all_headers_box)
3669       return;
3670
3671     this.set_classname(elem, 'hide-headers', false);
3672     this.set_classname(elem, 'show-headers', true);
3673     this.gui_objects.all_headers_row.style.display = 'none';
3674     elem.onclick = function() { rcmail.load_headers(elem); }
3675     }
3676
4e17e6 3677
T 3678   /********************************************************/
3bd94b 3679   /*********  html to text conversion functions   *********/
A 3680   /********************************************************/
3681
3682   this.html2plain = function(htmlText, id)
3683     {
3684     var http_request = new rcube_http_request();
3685     var url = this.env.bin_path+'html2text.php';
3686     var rcmail = this;
3687
3688     this.set_busy(true, 'converting');
3689     console.log('HTTP POST: '+url);
3690
3691     http_request.onerror = function(o) { rcmail.http_error(o); };
3692     http_request.oncomplete = function(o) { rcmail.set_text_value(o, id); };
3693     http_request.POST(url, htmlText, 'application/octet-stream');
3694     }
3695
3696   this.set_text_value = function(httpRequest, id)
3697     {
3698     this.set_busy(false);
3699     document.getElementById(id).value = httpRequest.get_text();
3700     console.log(httpRequest.get_text());
3701     }
3702
3703
3704   /********************************************************/
4e17e6 3705   /*********        remote request methods        *********/
T 3706   /********************************************************/
6b47de 3707
4b9efb 3708   this.redirect = function(url, lock)
f11541 3709     {
719a25 3710     if (lock || lock === null)
4b9efb 3711       this.set_busy(true);
S 3712
f11541 3713     if (this.env.framed && window.parent)
T 3714       parent.location.href = url;
3715     else  
3716       location.href = url;
3717     };
6b47de 3718
T 3719   this.goto_url = function(action, query, lock)
3720     {
3721     var querystring = query ? '&'+query : '';
4b9efb 3722     this.redirect(this.env.comm_path+'&_action='+action+querystring, lock);
6b47de 3723     };
4e17e6 3724
ecf759 3725   this.http_sockets = new Array();
T 3726   
3727   // find a non-busy socket or create a new one
3728   this.get_request_obj = function()
4e17e6 3729     {
ecf759 3730     for (var n=0; n<this.http_sockets.length; n++)
4e17e6 3731       {
ecf759 3732       if (!this.http_sockets[n].busy)
T 3733         return this.http_sockets[n];
4e17e6 3734       }
ecf759 3735     
T 3736     // create a new XMLHTTP object
3737     var i = this.http_sockets.length;
3738     this.http_sockets[i] = new rcube_http_request();
4e17e6 3739
ecf759 3740     return this.http_sockets[i];
T 3741     };
3742   
3743   // send a http request to the server
3744   this.http_request = function(action, querystring, lock)
3745     {
3746     var request_obj = this.get_request_obj();
b19097 3747     querystring += (querystring ? '&' : '') + '_remote=1';
4e17e6 3748     
T 3749     // add timestamp to request url to avoid cacheing problems in Safari
3750     if (bw.safari)
3751       querystring += '&_ts='+(new Date().getTime());
3752
3753     // send request
ecf759 3754     if (request_obj)
4e17e6 3755       {
f11541 3756       console.log('HTTP request: '+this.env.comm_path+'&_action='+action+'&'+querystring);
ecf759 3757
T 3758       if (lock)
3759         this.set_busy(true);
3760
f11541 3761       var rcm = this;
ecf759 3762       request_obj.__lock = lock ? true : false;
T 3763       request_obj.__action = action;
86958f 3764       request_obj.onerror = function(o){ ref.http_error(o); };
T 3765       request_obj.oncomplete = function(o){ ref.http_response(o); };
4d4264 3766       request_obj.GET(this.env.comm_path+'&_action='+action+'&'+querystring);
4e17e6 3767       }
T 3768     };
3769
f11541 3770     // send a http POST request to the server
T 3771     this.http_post = function(action, postdata, lock)
3772       {
3773       var request_obj;
3774       if (postdata && typeof(postdata) == 'object')
3775         postdata._remote = 1;
3776       else
3777         postdata += (postdata ? '&' : '') + '_remote=1';
3778
3779       // send request
3780       if (request_obj = this.get_request_obj())
3781         {
3782         console.log('HTTP POST: '+this.env.comm_path+'&_action='+action);
3783
3784         if (lock)
3785           this.set_busy(true);
3786
3787         var rcm = this;
3788         request_obj.__lock = lock ? true : false;
3789         request_obj.__action = action;
3790         request_obj.onerror = function(o){ rcm.http_error(o); };
3791         request_obj.oncomplete = function(o){ rcm.http_response(o); };
3792         request_obj.POST(this.env.comm_path+'&_action='+action, postdata);
3793         }
3794       };
4e17e6 3795
ecf759 3796   // handle HTTP response
T 3797   this.http_response = function(request_obj)
4e17e6 3798     {
ecf759 3799     var ctype = request_obj.get_header('Content-Type');
bf2f39 3800     if (ctype)
A 3801       {
ecf759 3802       ctype = String(ctype).toLowerCase();
e1cf7c 3803       var ctype_array=ctype.split(";");
S 3804       ctype = ctype_array[0];
bf2f39 3805       }
4e17e6 3806
5eee00 3807     if (request_obj.__lock)
e5686f 3808       this.set_busy(false);
4e17e6 3809
f11541 3810     console.log(request_obj.get_text());
4e17e6 3811
ecf759 3812     // if we get javascript code from server -> execute it
c03095 3813     if (request_obj.get_text() && (ctype=='text/javascript' || ctype=='application/x-javascript'))
T 3814       eval(request_obj.get_text());
4e17e6 3815
ecf759 3816     // process the response data according to the sent action
0dbac3 3817     switch (request_obj.__action) {
ecf759 3818       case 'delete':
0dbac3 3819         if (this.task == 'addressbook') {
T 3820           var uid = this.contact_list.get_selection();
3821           this.enable_command('compose', (uid && this.contact_list.rows[uid]));
3822           this.enable_command('delete', 'edit', (uid && this.contact_list.rows[uid] && this.env.address_sources && !this.env.address_sources[this.env.source].readonly));
3823           this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
3824         }
3825       
ecf759 3826       case 'moveto':
0dbac3 3827         if (this.env.action == 'show')
ecf759 3828           this.command('list');
b19097 3829         else if (this.message_list)
T 3830           this.message_list.init();
0dbac3 3831         break;
T 3832         
2eb032 3833       case 'purge':
3d3531 3834       case 'expunge':      
0dbac3 3835         if (!this.env.messagecount && this.task == 'mail') {
T 3836           // clear preview pane content
3837           if (this.env.contentframe)
3838             this.show_contentframe(false);
3839           // disable commands useless when mailbox is empty
3840           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'mark', 'viewsource',
3841             'print', 'load-attachment', 'purge', 'expunge', 'select-all', 'select-none', 'sort', false);
3842         }
3843         break;
fdccdb 3844
d41d67 3845       case 'check-recent':
fdccdb 3846       case 'getunread':
0dbac3 3847       case 'list':
T 3848         if (this.task == 'mail') {
2c832e 3849           if (this.message_list && request_obj.__action == 'list')
835ae8 3850             this.msglist_select(this.message_list);
0dbac3 3851           this.enable_command('show', 'expunge', 'select-all', 'select-none', 'sort', (this.env.messagecount > 0));
T 3852           this.enable_command('purge', this.purge_mailbox_test());
3853         }
3854         else if (this.task == 'addressbook')
3855           this.enable_command('export', (this.contact_list && this.contact_list.rowcount > 0));
2c832e 3856
0dbac3 3857         break;
bf2f39 3858       }
ecf759 3859
T 3860     request_obj.reset();
4e17e6 3861     };
ecf759 3862
T 3863   // handle HTTP request errors
3864   this.http_error = function(request_obj)
3865     {
835ae8 3866     //alert('Error sending request: '+request_obj.url+' => HTTP '+request_obj.xmlhttp.status);
ecf759 3867     if (request_obj.__lock)
T 3868       this.set_busy(false);
3869
3870     request_obj.reset();
3871     request_obj.__lock = false;
835ae8 3872     this.display_message('Unknown Server Error!', 'error');
ecf759 3873     };
T 3874
3875   // use an image to send a keep-alive siganl to the server
3876   this.send_keep_alive = function()
3877     {
3878     var d = new Date();
3879     this.http_request('keep-alive', '_t='+d.getTime());
3880     };
15a9d1 3881
T 3882   // send periodic request to check for recent messages
3883   this.check_for_recent = function()
3884     {
aade7b 3885     if (this.busy)
T 3886       return;
3887
c8c1e0 3888     this.set_busy(true, 'checkingmail');
110748 3889     this.http_request('check-recent', (this.env.search_request ? '_search='+this.env.search_request+'&' : '') + '_t='+(new Date().getTime()), true);
15a9d1 3890     };
4e17e6 3891
T 3892
3893   /********************************************************/
3894   /*********            helper methods            *********/
3895   /********************************************************/
3896   
3897   // check if we're in show mode or if we have a unique selection
3898   // and return the message uid
3899   this.get_single_uid = function()
3900     {
6b47de 3901     return this.env.uid ? this.env.uid : (this.message_list ? this.message_list.get_single_selection() : null);
4e17e6 3902     };
T 3903
3904   // same as above but for contacts
3905   this.get_single_cid = function()
3906     {
6b47de 3907     return this.env.cid ? this.env.cid : (this.contact_list ? this.contact_list.get_single_selection() : null);
4e17e6 3908     };
T 3909
3910
3911   this.get_caret_pos = function(obj)
3912     {
3913     if (typeof(obj.selectionEnd)!='undefined')
3914       return obj.selectionEnd;
3915
3916     else if (document.selection && document.selection.createRange)
3917       {
3918       var range = document.selection.createRange();
3919       if (range.parentElement()!=obj)
3920         return 0;
3921
3922       var gm = range.duplicate();
3923       if (obj.tagName=='TEXTAREA')
3924         gm.moveToElementText(obj);
3925       else
3926         gm.expand('textedit');
3927       
3928       gm.setEndPoint('EndToStart', range);
3929       var p = gm.text.length;
3930
3931       return p<=obj.value.length ? p : -1;
3932       }
3933
3934     else
3935       return obj.value.length;
3936     };
3937
3938   this.set_caret2start = function(obj)
3939     {
3940     if (obj.createTextRange)
3941       {
3942       var range = obj.createTextRange();
3943       range.collapse(true);
3944       range.select();
3945       }
3946     else if (obj.setSelectionRange)
3947       obj.setSelectionRange(0,0);
3948
3949     obj.focus();
3950     };
3951
3952   // set all fields of a form disabled
3953   this.lock_form = function(form, lock)
3954     {
3955     if (!form || !form.elements)
3956       return;
3957     
3958     var type;
3959     for (var n=0; n<form.elements.length; n++)
3960       {
3961       type = form.elements[n];
3962       if (type=='hidden')
3963         continue;
3964         
3965       form.elements[n].disabled = lock;
3966       }
3967     };
3968     
3969   }  // end object rcube_webmail
3970
3971
6b47de 3972 /**
T 3973  * Class for sending HTTP requests
3974  * @constructor
3975  */
ecf759 3976 function rcube_http_request()
T 3977   {
3978   this.url = '';
3979   this.busy = false;
3980   this.xmlhttp = null;
3981
3982   // reset object properties
3983   this.reset = function()
3984     {
3985     // set unassigned event handlers
3986     this.onloading = function(){ };
3987     this.onloaded = function(){ };
3988     this.oninteractive = function(){ };
3989     this.oncomplete = function(){ };
3990     this.onabort = function(){ };
3991     this.onerror = function(){ };
3992     
3993     this.url = '';
3994     this.busy = false;
3995     this.xmlhttp = null;
3996     }
3997
3998   // create HTMLHTTP object
3999   this.build = function()
4000     {
4001     if (window.XMLHttpRequest)
4002       this.xmlhttp = new XMLHttpRequest();
4003     else if (window.ActiveXObject)
f11541 4004       {
T 4005       try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
4006       catch(e) { this.xmlhttp = null; }
4007       }
ecf759 4008     else
T 4009       {
4010       
4011       }
4012     }
4013
a0109c 4014   // send GET request
ecf759 4015   this.GET = function(url)
T 4016     {
4017     this.build();
4018
4019     if (!this.xmlhttp)
4020       {
4021       this.onerror(this);
4022       return false;
4023       }
4024
719a25 4025     var _ref = this;
ecf759 4026     this.url = url;
T 4027     this.busy = true;
4028
719a25 4029     this.xmlhttp.onreadystatechange = function(){ _ref.xmlhttp_onreadystatechange(); };
f93650 4030     this.xmlhttp.open('GET', url, true);
234c0d 4031     this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
ecf759 4032     this.xmlhttp.send(null);
T 4033     };
4034
a0109c 4035   this.POST = function(url, body, contentType)
ecf759 4036     {
a0109c 4037     // default value for contentType if not provided
f11541 4038     if (typeof(contentType) == 'undefined')
T 4039       contentType = 'application/x-www-form-urlencoded';
a0109c 4040
S 4041     this.build();
4042     
4043     if (!this.xmlhttp)
4044     {
4045        this.onerror(this);
4046        return false;
4047     }
f11541 4048     
T 4049     var req_body = body;
4050     if (typeof(body) == 'object')
4051     {
4052       req_body = '';
4053       for (var p in body)
4054         req_body += (req_body ? '&' : '') + p+'='+urlencode(body[p]);
4055     }
a0109c 4056
f11541 4057     var ref = this;
a0109c 4058     this.url = url;
S 4059     this.busy = true;
4060     
4061     this.xmlhttp.onreadystatechange = function() { ref.xmlhttp_onreadystatechange(); };
4062     this.xmlhttp.open('POST', url, true);
4063     this.xmlhttp.setRequestHeader('Content-Type', contentType);
234c0d 4064     this.xmlhttp.setRequestHeader('X-RoundCube-Referer', bw.get_cookie('roundcube_sessid'));
f11541 4065     this.xmlhttp.send(req_body);
ecf759 4066     };
T 4067
4068   // handle onreadystatechange event
4069   this.xmlhttp_onreadystatechange = function()
4070     {
4071     if(this.xmlhttp.readyState == 1)
4072       this.onloading(this);
4073
4074     else if(this.xmlhttp.readyState == 2)
4075       this.onloaded(this);
4076
4077     else if(this.xmlhttp.readyState == 3)
4078       this.oninteractive(this);
4079
4080     else if(this.xmlhttp.readyState == 4)
4081       {
9a5261 4082       try {
T 4083         if (this.xmlhttp.status == 0)
4084           this.onabort(this);
4085         else if(this.xmlhttp.status == 200)
4086           this.oncomplete(this);
4087         else
4088           this.onerror(this);
4089
4090         this.busy = false;
4091         }
4092       catch(err)
4093         {
ecf759 4094         this.onerror(this);
9a5261 4095         this.busy = false;
T 4096         }
ecf759 4097       }
T 4098     }
4099
4100   // getter method for HTTP headers
4101   this.get_header = function(name)
4102     {
4103     return this.xmlhttp.getResponseHeader(name);
4104     };
4105
c03095 4106   this.get_text = function()
T 4107     {
4108     return this.xmlhttp.responseText;
4109     };
4110
4111   this.get_xml = function()
4112     {
4113     return this.xmlhttp.responseXML;
4114     };
ecf759 4115
T 4116   this.reset();
4117   
4118   }  // end class rcube_http_request
4119
4120
e170b4 4121 // helper function to call the init method with a delay
T 4122 function call_init(o)
4123   {
dbbd1f 4124     window.setTimeout('if (window[\''+o+'\'] && window[\''+o+'\'].init) { '+o+'.init(); }',
S 4125                       bw.win ? 500 : 200);
4e17e6 4126   }