thomascube
2005-11-18 fbf77b4493f1b77c99751d8a86365c712ae3fb1b
commit | author | age
e0ed97 1 /*
4e17e6 2  +-----------------------------------------------------------------------+
T 3  | RoundCube Webmail Client Script                                       |
4  |                                                                       |
5  | This file is part of the RoundCube Webmail client                     |
6  | Copyright (C) 2005, RoundCube Dev, - Switzerland                      |
30233b 7  | Licensed under the GNU GPL                                            |
4e17e6 8  |                                                                       |
ecf759 9  | Modified: 2005/11/13 (roundcube)                                      |
4e17e6 10  |                                                                       |
T 11  +-----------------------------------------------------------------------+
12  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
13  +-----------------------------------------------------------------------+
14 */
15
16
17 var rcube_webmail_client;
18
19 function rcube_webmail()
20   {
21   this.env = new Object();
10a699 22   this.labels = new Object();
4e17e6 23   this.buttons = new Object();
T 24   this.gui_objects = new Object();
25   this.commands = new Object();
26   this.selection = new Array();
27
28   // create public reference to myself
29   rcube_webmail_client = this;
30   this.ref = 'rcube_webmail_client';
31  
32   // webmail client settings
33   this.dblclick_time = 600;
34   this.message_time = 5000;
e66f5b 35   this.request_timeout = 180000;
ecf759 36   this.kepp_alive_interval = 60000;
4e17e6 37   this.mbox_expression = new RegExp('[^0-9a-z\-_]', 'gi');
T 38   this.env.blank_img = 'skins/default/images/blank.gif';
39   
40   // mimetypes supported by the browser (default settings)
41   this.mimetypes = new Array('text/plain', 'text/html', 'text/xml',
42                              'image/jpeg', 'image/gif', 'image/png',
43                              'application/x-javascript', 'application/pdf',
44                              'application/x-shockwave-flash');
45
46
47   // set environment variable
48   this.set_env = function(name, value)
49     {
50     //if (!this.busy)
51       this.env[name] = value;    
52     };
10a699 53
T 54
55   // add a localized label to the client environment
56   this.add_label = function(key, value)
57     {
58     this.labels[key] = value;
59     };
60
4e17e6 61
T 62   // add a button to the button list
63   this.register_button = function(command, id, type, act, sel, over)
64     {
65     if (!this.buttons[command])
66       this.buttons[command] = new Array();
67       
68     var button_prop = {id:id, type:type};
69     if (act) button_prop.act = act;
70     if (sel) button_prop.sel = sel;
71     if (over) button_prop.over = over;
72
73     this.buttons[command][this.buttons[command].length] = button_prop;    
74     };
75
76
77   // register a specific gui object
78   this.gui_object = function(name, id)
79     {
80     this.gui_objects[name] = id;
81     };
82
83
84   // initialize webmail client
85   this.init = function()
86     {
87     this.task = this.env.task;
88     
89     // check browser
a95e0e 90     if (!bw.dom || !bw.xmlhttp_test())
4e17e6 91       {
T 92       location.href = this.env.comm_path+'&_action=error&_code=0x199';
93       return;
94       }
95     
96     // find all registered gui objects
97     for (var n in this.gui_objects)
98       this.gui_objects[n] = rcube_find_object(this.gui_objects[n]);
99       
100     // tell parent window that this frame is loaded
101     if (this.env.framed && parent.rcmail && parent.rcmail.set_busy)
102       parent.rcmail.set_busy(false);
103
104     // enable general commands
105     this.enable_command('logout', 'mail', 'addressbook', 'settings', true);
106     
107     switch (this.task)
108       {
109       case 'mail':
110         var msg_list = this.gui_objects.messagelist;
111         if (msg_list)
112           {
113           this.init_messagelist(msg_list);
114           this.enable_command('markread', true);
115           }
116
117         // enable mail commands
118         this.enable_command('list', 'compose', 'add-contact', true);
119         
120         if (this.env.action=='show')
121           {
583f1c 122           this.enable_command('show', 'reply', 'reply-all', 'forward', 'moveto', 'delete', 'viewsource', 'print', 'load-attachment', true);
4e17e6 123           if (this.env.next_uid)
T 124             this.enable_command('nextmessage', true);
125           if (this.env.prev_uid)
126             this.enable_command('previousmessage', true);
127           }
128
129         if (this.env.action=='show' && this.env.blockedobjects)
130           {
131           if (this.gui_objects.remoteobjectsmsg)
132             this.gui_objects.remoteobjectsmsg.style.display = 'block';
133           this.enable_command('load-images', true);
134           }  
135
136         if (this.env.action=='compose')
137           this.enable_command('add-attachment', 'send-attachment', 'send', true);
138           
139         if (this.env.messagecount)
f3b659 140           this.enable_command('select-all', 'select-none', 'sort', true);
4e17e6 141
T 142         this.set_page_buttons();
143
144         // focus this window
145         window.focus();
146
147         // init message compose form
148         if (this.env.action=='compose')
149           this.init_messageform();
150
151         // show printing dialog
152         if (this.env.action=='print')
153           window.print();
154
155         break;
156
157
158       case 'addressbook':
159         var contacts_list = this.gui_objects.contactslist;
160         if (contacts_list)
161           this.init_contactslist(contacts_list);
162       
163         this.set_page_buttons();
164           
165         if (this.env.cid)
166           this.enable_command('show', 'edit', true);
167
168         if ((this.env.action=='add' || this.env.action=='edit') && this.gui_objects.editform)
169           this.enable_command('save', true);
170       
171         this.enable_command('list', 'add', true);
172         break;
173
174
175       case 'settings':
176         this.enable_command('preferences', 'identities', 'save', 'folders', true);
177         
178         if (this.env.action=='identities' || this.env.action=='edit-identity' || this.env.action=='add-identity')
179           this.enable_command('edit', 'add', 'delete', true);
180
181         if (this.env.action=='edit-identity' || this.env.action=='add-identity')
182           this.enable_command('save', true);
183           
184         if (this.env.action=='folders')
185           this.enable_command('subscribe', 'unsubscribe', 'create-folder', 'delete-folder', true);
186           
187         var identities_list = this.gui_objects.identitieslist;
188         if (identities_list)
189           this.init_identitieslist(identities_list);
190
191         break;
192
193       case 'login':
194         var input_user = rcube_find_object('_user');
195         var input_pass = rcube_find_object('_pass');
196         if (input_user && input_user.value=='')
197           input_user.focus();
198         else if (input_pass)
199           input_pass.focus();
200           
201         this.enable_command('login', true);
202         break;
203       
204       default:
205         break;
206       }
207
208
209     // enable basic commands
210     this.enable_command('logout', true);
211
212     // disable browser's contextmenus
213     //document.oncontextmenu = function(){ return false; }
214
215     // flag object as complete
216     this.loaded = true;
7902df 217           
4e17e6 218     // show message
T 219     if (this.pending_message)
220       this.display_message(this.pending_message[0], this.pending_message[1]);
ecf759 221       
T 222     // start interval for keep-alive siganl
223     if (this.kepp_alive_interval)
224       this.kepp_alive_int = setInterval(this.ref+'.send_keep_alive()', this.kepp_alive_interval);
4e17e6 225     };
T 226
227
228   // get all message rows from HTML table and init each row
229   this.init_messagelist = function(msg_list)
230     {
231     if (msg_list && msg_list.tBodies[0])
232       {
233       this.message_rows = new Array();
234
235       var row;
236       for(var r=0; r<msg_list.tBodies[0].childNodes.length; r++)
237         {
238         row = msg_list.tBodies[0].childNodes[r];
239         //row = msg_list.tBodies[0].rows[r];
240         this.init_message_row(row);
241         }
242       }
243       
244     // alias to common rows array
245     this.list_rows = this.message_rows;
246     };
247     
248     
249   // make references in internal array and set event handlers
250   this.init_message_row = function(row)
251     {
252     var uid, msg_icon;
253     
254     if (String(row.id).match(/rcmrow([0-9]+)/))
255       {
256       uid = RegExp.$1;
257       row.uid = uid;
258               
259       this.message_rows[uid] = {id:row.id, obj:row,
260                                 classname:row.className,
261                                 unread:this.env.messages[uid] ? this.env.messages[uid].unread : null,
262                                 replied:this.env.messages[uid] ? this.env.messages[uid].replied : null};
263               
264       // set eventhandlers to table row
265       row.onmousedown = function(e){ return rcube_webmail_client.drag_row(e, this.uid); };
266       row.onmouseup = function(e){ return rcube_webmail_client.click_row(e, this.uid); };
267               
268       // set eventhandler to message icon
269       if ((msg_icon = row.cells[0].childNodes[0]) && row.cells[0].childNodes[0].nodeName=='IMG')
270         {                
271         msg_icon.id = 'msgicn_'+uid;
272         msg_icon._row = row;
273         msg_icon.onmousedown = function(e) { rcube_webmail_client.command('markread', this); };
274                 
275         // get message icon and save original icon src
276         this.message_rows[uid].icon = msg_icon;
277         }
278       }
279     };
280
281
282   // init message compose form: set focus and eventhandlers
283   this.init_messageform = function()
284     {
285     if (!this.gui_objects.messageform)
286       return false;
287     
288     //this.messageform = this.gui_objects.messageform;
289     var input_to = rcube_find_object('_to');
290     var input_cc = rcube_find_object('_cc');
291     var input_bcc = rcube_find_object('_bcc');
292     var input_replyto = rcube_find_object('_replyto');
293     var input_subject = rcube_find_object('_subject');
294     var input_message = rcube_find_object('_message');
295     
296     // init live search events
297     if (input_to)
298       this.init_address_input_events(input_to);
299     if (input_cc)
300       this.init_address_input_events(input_cc);
301     if (input_bcc)
302       this.init_address_input_events(input_bcc);
303
304     if (input_to && input_to.value=='')
305       input_to.focus();
306     else if (input_subject && input_subject.value=='')
307       input_subject.focus();
308     else if (input_message)
309       this.set_caret2start(input_message); // input_message.focus();
310     };
311
312
313   this.init_address_input_events = function(obj)
314     {
315     var handler = function(e){ return rcube_webmail_client.ksearch_keypress(e,this); };
316     var handler2 = function(e){ return rcube_webmail_client.ksearch_blur(e,this); };
317     
318     if (bw.safari)
319       {
320       obj.addEventListener('keydown', handler, false);
321       // obj.addEventListener('blur', handler2, false);
322       }
323     else if (bw.mz)
324       {
325       obj.addEventListener('keypress', handler, false);
326       obj.addEventListener('blur', handler2, false);
327       }
328     else if (bw.ie)
329       {
330       obj.onkeydown = handler;
331       //obj.attachEvent('onkeydown', handler);
332       // obj.attachEvent('onblur', handler2, false);
333       }
334     
335     obj.setAttribute('autocomplete', 'off');       
336     };
337
338
339
340   // get all contact rows from HTML table and init each row
341   this.init_contactslist = function(contacts_list)
342     {
343     if (contacts_list && contacts_list.tBodies[0])
344       {
345       this.contact_rows = new Array();
346
347       var row;
348       for(var r=0; r<contacts_list.tBodies[0].childNodes.length; r++)
349         {
350         row = contacts_list.tBodies[0].childNodes[r];
351         this.init_table_row(row, 'contact_rows');
352         }
353       }
354
355     // alias to common rows array
356     this.list_rows = this.contact_rows;
357     
358     if (this.env.cid)
359       this.select(this.env.cid);
360     };
361
362
363   // make references in internal array and set event handlers
364   this.init_table_row = function(row, array_name)
365     {
366     var cid;
367     
368     if (String(row.id).match(/rcmrow([0-9]+)/))
369       {
370       cid = RegExp.$1;
371       row.cid = cid;
372
373       this[array_name][cid] = {id:row.id,
374                                obj:row,
375                                classname:row.className};
376
377       // set eventhandlers to table row
378       row.onmousedown = function(e) { rcube_webmail_client.in_selection_before=this.cid; return false; };  // fake for drag handler
379       row.onmouseup = function(e){ return rcube_webmail_client.click_row(e, this.cid); };
380       }
381     };
382
383
384   // get all contact rows from HTML table and init each row
385   this.init_identitieslist = function(identities_list)
386     {
387     if (identities_list && identities_list.tBodies[0])
388       {
389       this.identity_rows = new Array();
390
391       var row;
392       for(var r=0; r<identities_list.tBodies[0].childNodes.length; r++)
393         {
394         row = identities_list.tBodies[0].childNodes[r];
395         this.init_table_row(row, 'identity_rows');
396         }
397       }
398
399     // alias to common rows array
400     this.list_rows = this.identity_rows;
401     
402     if (this.env.iid)
403       this.select(this.env.iid);    
404     };
405     
406
407
408   /*********************************************************/
409   /*********       client command interface        *********/
410   /*********************************************************/
411
412
413   // execute a specific command on the web client
414   this.command = function(command, props, obj)
415     {
416     if (obj && obj.blur)
417       obj.blur();
418
419     if (this.busy)
420       return false;
421
422     // command not supported or allowed
423     if (!this.commands[command])
424       {
425       // pass command to parent window
426       if (this.env.framed && parent.rcmail && parent.rcmail.command)
427         parent.rcmail.command(command, props);
428
429       return false;
430       }
431
432     // process command
433     switch (command)
434       {
435       case 'login':
436         if (this.gui_objects.loginform)
437           this.gui_objects.loginform.submit();
438         break;
439
440       case 'logout':
441         location.href = this.env.comm_path+'&_action=logout';
442         break;      
443
444       // commands to switch task
445       case 'mail':
446       case 'addressbook':
447       case 'settings':
448         this.switch_task(command);
449         break;
450
451
452       // misc list commands
453       case 'list':
454         if (this.task=='mail')
455           this.list_mailbox(props);
456         else if (this.task=='addressbook')
457           this.list_contacts();
f3b659 458         break;
T 459
460       case 'sort':
461         // get the type of sorting
b076a4 462         var a_sort = props.split('_');
T 463         var sort_col = a_sort[0];
464         var sort_order = a_sort[1].toUpperCase();
465         var header;
466         
467         if (this.env.sort_col==sort_col && this.env.sort_order==sort_order)
468           break;
469
470         // set table header class
471         if (header = document.getElementById('rcmHead'+this.env.sort_col))
472           this.set_classname(header, 'sorted'+(this.env.sort_order.toUpperCase()), false);
473         if (header = document.getElementById('rcmHead'+sort_col))
474           this.set_classname(header, 'sorted'+sort_order, true);
475
476         // save new sort properties
477         this.env.sort_col = sort_col;
478         this.env.sort_order = sort_order;
479
480         // reload message list
f3b659 481         this.list_mailbox('', '', props);
4e17e6 482         break;
T 483
484       case 'nextpage':
485         this.list_page('next');
486         break;
487
488       case 'previouspage':
489         this.list_page('prev');
490         break;
491
492
493       // common commands used in multiple tasks
494       case 'show':
495         if (this.task=='mail')
496           {
497           var uid = this.get_single_uid();
498           if (uid && (!this.env.uid || uid != this.env.uid))
499             this.show_message(uid);
500           }
501         else if (this.task=='addressbook')
502           {
503           var cid = props ? props : this.get_single_cid();
504           if (cid && !(this.env.action=='show' && cid==this.env.cid))
505             this.load_contact(cid, 'show');
506           }
507         break;
508
509       case 'add':
510         if (this.task=='addressbook')
511           this.load_contact(0, 'add');
512         else if (this.task=='settings')
513           {
514           this.clear_selection();
515           this.load_identity(0, 'add-identity');
516           }
517         break;
518
519       case 'edit':
520         var cid;
521         if (this.task=='addressbook' && (cid = this.get_single_cid()))
522           this.load_contact(cid, 'edit');
523         else if (this.task=='settings' && props)
524           this.load_identity(props, 'edit-identity');
525         break;
526
527       case 'save-identity':
528       case 'save':
529         if (this.gui_objects.editform)
10a699 530           {
T 531           var input_pagesize = rcube_find_object('_pagesize');
532           var input_name  = rcube_find_object('_name');
533           var input_email = rcube_find_object('_email');
534
535           // user prefs
e66f5b 536           if (input_pagesize && isNaN(input_pagesize.value))
10a699 537             {
T 538             alert(this.get_label('nopagesizewarning'));
539             input_pagesize.focus();
540             break;
541             }
542           // contacts/identities
543           else
544             {
545             if (input_name && input_name.value == '')
546               {
547               alert(this.get_label('nonamewarning'));
548               input_name.focus();
549               break;
550               }
551             else if (input_email && !rcube_check_email(input_email.value))
552               {
553               alert(this.get_label('noemailwarning'));
554               input_email.focus();
555               break;
556               }
557             }
558
4e17e6 559           this.gui_objects.editform.submit();
10a699 560           }
4e17e6 561         break;
T 562
563       case 'delete':
564         // mail task
565         if (this.task=='mail' && this.env.trash_mailbox && String(this.env.mailbox).toLowerCase()!=String(this.env.trash_mailbox).toLowerCase())
566           this.move_messages(this.env.trash_mailbox);
567         else if (this.task=='mail')
568           this.delete_messages();
569         // addressbook task
570         else if (this.task=='addressbook')
571           this.delete_contacts();
572         // user settings task
573         else if (this.task=='settings')
574           this.delete_identity();
575         break;
576
577
578       // mail task commands
579       case 'move':
580       case 'moveto':
581         this.move_messages(props);
582         break;
583         
584       case 'markread':
585         if (props && !props._row)
586           break;
587         
588         var uid;
589         var flag = 'read';
590         
591         if (props._row.uid)
592           {
593           uid = props._row.uid;
594           this.dont_select = true;
595           
596           // toggle read/unread
597           if (!this.message_rows[uid].unread)
598             flag = 'unread';
599           }
600           
601         this.mark_message(flag, uid);
602         break;
603         
604       case 'load-images':
605         if (this.env.uid)
606           this.show_message(this.env.uid, true);
607         break;
608
609       case 'load-attachment':
610         var url = this.env.comm_path+'&_action=get&_mbox='+this.env.mailbox+'&_uid='+this.env.uid+'&_part='+props.part;
611         
612         // open attachment in frame if it's of a supported mimetype
613         if (this.env.uid && props.mimetype && find_in_array(props.mimetype, this.mimetypes)>=0)
614           {
615           this.attachment_win = window.open(url+'&_frame=1', 'rcubemailattachment');
616           if (this.attachment_win)
617             {
618             setTimeout(this.ref+'.attachment_win.focus()', 10);
619             break;
620             }
621           }
622
623         location.href = url;
624         break;
625         
626       case 'select-all':
627         this.select_all(props);
628         break;
629
630       case 'select-none':
631         this.clear_selection();
632         break;
633
634       case 'nextmessage':
635         if (this.env.next_uid)
09941e 636           this.show_message(this.env.next_uid);
T 637           //location.href = this.env.comm_path+'&_action=show&_uid='+this.env.next_uid+'&_mbox='+this.env.mailbox;
4e17e6 638         break;
T 639
640       case 'previousmessage':
641         if (this.env.prev_uid)
09941e 642           this.show_message(this.env.prev_uid);
T 643           //location.href = this.env.comm_path+'&_action=show&_uid='+this.env.prev_uid+'&_mbox='+this.env.mailbox;
4e17e6 644         break;
T 645
646       case 'compose':
647         var url = this.env.comm_path+'&_action=compose';
648         
649         // modify url if we're in addressbook
650         if (this.task=='addressbook')
651           {
652           url = this.get_task_url('mail', url);            
653           var a_cids = new Array();
654           
655           // use contact_id passed as command parameter
656           if (props)
657             a_cids[a_cids.length] = props;
658             
659           // get selected contacts
660           else
661             {
662             for (var n=0; n<this.selection.length; n++)
663               a_cids[a_cids.length] = this.selection[n];
664             }
665
666           if (a_cids.length)
667             url += '&_to='+a_cids.join(',');
668           else
669             break;
670           }
671         else if (props)
672            url += '&_to='+props;
673
674         this.set_busy(true);
675         location.href = url;
676         break;      
677
678       case 'send':
679         if (!this.gui_objects.messageform)
680           break;
681           
682         // check input fields
683         var input_to = rcube_find_object('_to');
684         var input_subject = rcube_find_object('_subject');
685         var input_message = rcube_find_object('_message');
10a699 686
T 687         // check for empty recipient
688         if (input_to && !rcube_check_email(input_to.value, true))
4e17e6 689           {
10a699 690           alert(this.get_label('norecipientwarning'));
T 691           input_to.focus();
692           break;
4e17e6 693           }
10a699 694
T 695         // display localized warning for missing subject
696         if (input_subject && input_subject.value == '')
697           {
698           var subject = prompt(this.get_label('nosubjectwarning'), this.get_label('nosubject'));
699
700           // user hit cancel, so don't send
701           if (!subject && subject !== '')
702             {
703             input_subject.focus();
704             break;
705             }
706           else
707             {
708             input_subject.value = subject ? subject : this.get_label('nosubject');            
709             }
710           }
711
712         // check for empty body
713         if (input_message.value=='')
714           {
715           if (!confirm(this.get_label('nobodywarning')))
716             {
717             input_message.focus();
718             break;
719             }
720           }
721
722         // all checks passed, send message
723         this.set_busy(true, 'sendingmessage');
724         var form = this.gui_objects.messageform;
725         form.submit();
4e17e6 726         break;
T 727
728       case 'add-attachment':
729         this.show_attachment_form(true);
730         
731       case 'send-attachment':
732         this.upload_file(props)      
733         break;
734
583f1c 735       case 'reply-all':
4e17e6 736       case 'reply':
T 737         var uid;
738         if (uid = this.get_single_uid())
739           {
740           this.set_busy(true);
583f1c 741           location.href = this.env.comm_path+'&_action=compose&_reply_uid='+uid+'&_mbox='+escape(this.env.mailbox)+(command=='reply-all' ? '&_all=1' : '');
4e17e6 742           }
T 743         break;      
744
745       case 'forward':
746         var uid;
747         if (uid = this.get_single_uid())
748           {
749           this.set_busy(true);
750           location.href = this.env.comm_path+'&_action=compose&_forward_uid='+uid+'&_mbox='+escape(this.env.mailbox);
751           }
752         break;
753         
754       case 'print':
755         var uid;
756         if (uid = this.get_single_uid())
757           {
758           this.printwin = window.open(this.env.comm_path+'&_action=print&_uid='+uid+'&_mbox='+escape(this.env.mailbox)+(this.env.safemode ? '&_safe=1' : ''));
759           if (this.printwin)
760             setTimeout(this.ref+'.printwin.focus()', 20);
761           }
762         break;
763
764       case 'viewsource':
765         var uid;
766         if (uid = this.get_single_uid())
767           {          
768           this.sourcewin = window.open(this.env.comm_path+'&_action=viewsource&_uid='+this.env.uid+'&_mbox='+escape(this.env.mailbox));
769           if (this.sourcewin)
770             setTimeout(this.ref+'.sourcewin.focus()', 20);
771           }
772         break;
773
774       case 'add-contact':
775         this.add_contact(props);
776         break;
777
778
779       // user settings commands
780       case 'preferences':
781         location.href = this.env.comm_path;
782         break;
783
784       case 'identities':
785         location.href = this.env.comm_path+'&_action=identities';
786         break;
787           
788       case 'delete-identity':
789         this.delete_identity();
790         
791       case 'folders':
792         location.href = this.env.comm_path+'&_action=folders';
793         break;
794
795       case 'subscribe':
796         this.subscribe_folder(props);
797         break;
798
799       case 'unsubscribe':
800         this.unsubscribe_folder(props);
801         break;
802         
803       case 'create-folder':
804         this.create_folder(props);
805         break;
806
807       case 'delete-folder':
808         if (confirm('Do you really want to delete this folder?'))
809           this.delete_folder(props);
810         break;
811
812       }
813
814     return obj ? false : true;
815     };
816
817
818   // set command enabled or disabled
819   this.enable_command = function()
820     {
821     var args = this.enable_command.arguments;
822     if(!args.length) return -1;
823
824     var command;
825     var enable = args[args.length-1];
826     
827     for(var n=0; n<args.length-1; n++)
828       {
829       command = args[n];
830       this.commands[command] = enable;
831       this.set_button(command, (enable ? 'act' : 'pas'));
832       }
833     };
834
835
a95e0e 836   // lock/unlock interface
4e17e6 837   this.set_busy = function(a, message)
T 838     {
839     if (a && message)
10a699 840       {
T 841       var msg = this.get_label(message);
842       if (msg==message)        
843         msg = 'Loading...';
844
845       this.display_message(msg, 'loading', true);
846       }
4e17e6 847     else if (!a && this.busy)
T 848       this.hide_message();
849
850     this.busy = a;
851     //document.body.style.cursor = a ? 'wait' : 'default';
852     
853     if (this.gui_objects.editform)
854       this.lock_form(this.gui_objects.editform, a);
a95e0e 855       
T 856     // clear pending timer
857     if (this.request_timer)
858       clearTimeout(this.request_timer);
859
860     // set timer for requests
861     if (a && this.request_timeout)
862       this.request_timer = setTimeout(this.ref+'.request_timed_out()', this.request_timeout);
4e17e6 863     };
T 864
865
10a699 866   // return a localized string
T 867   this.get_label = function(name)
868     {
869     if (this.labels[name])
870       return this.labels[name];
871     else
872       return name;
873     };
874
875
876   // switch to another application task
4e17e6 877   this.switch_task = function(task)
T 878     {
01bb03 879     if (this.task===task && task!='mail')
4e17e6 880       return;
T 881
01bb03 882     var url = this.get_task_url(task);
T 883     if (task=='mail')
884       url += '&_mbox=INBOX';
885
4e17e6 886     this.set_busy(true);
01bb03 887     location.href = url;
4e17e6 888     };
T 889
890
891   this.get_task_url = function(task, url)
892     {
893     if (!url)
894       url = this.env.comm_path;
895
896     return url.replace(/_task=[a-z]+/, '_task='+task);
a95e0e 897     };
T 898     
899   
900   // called when a request timed out
901   this.request_timed_out = function()
902     {
903     this.set_busy(false);
904     this.display_message('Request timed out!', 'error');
4e17e6 905     };
T 906
907
908   /*********************************************************/
909   /*********        event handling methods         *********/
910   /*********************************************************/
911
912
913   // onmouseup handler for mailboxlist item
914   this.mbox_mouse_up = function(mbox)
915     {
916     if (this.drag_active)
917       this.command('moveto', mbox);
918     else
919       this.command('list', mbox);
920       
921     return false;
922     };
923
924
925   // onmousedown-handler of message list row
926   this.drag_row = function(e, id)
927     {
928     this.in_selection_before = this.in_selection(id) ? id : false;
929
930     // don't do anything (another action processed before)
931     if (this.dont_select)
932       return false;
933
934     if (!this.in_selection_before)
935       {
936       var shift = this.check_shiftkey(e);
937       this.select(id, shift);
938       }
939     
940     if (this.selection.length)
941       {
942       this.drag_start = true;
943       document.onmousemove = function(e){ return rcube_webmail_client.drag_mouse_move(e); };
944       document.onmouseup = function(e){ return rcube_webmail_client.drag_mouse_up(e); };
945       }
946
947     return false;
948     };
949
950
951   // onmouseup-handler of message list row
952   this.click_row = function(e, id)
953     {
954     var shift = this.check_shiftkey(e);
955     
956     // don't do anything (another action processed before)
957     if (this.dont_select)
958       {
959       this.dont_select = false;
960       return false;
961       }
962     
963     if (!this.drag_active && this.in_selection_before==id)
964       this.select(id, (shift && this.task!='settings'));
965     
966     this.drag_start = false;
967     this.in_selection_before = false;
968         
969     // row was double clicked
970     if (this.task=='mail' && this.list_rows && this.list_rows[id].clicked && !shift)
971       {
972       this.show_message(id);
973       return false;
974       }
975     else if (this.task=='addressbook')
976       {
977       if (this.selection.length==1 && this.env.contentframe)
978         this.load_contact(this.selection[0], 'show', true);
979       else if (this.task=='addressbook' && this.list_rows && this.list_rows[id].clicked)
980         {
981         this.load_contact(id, 'show');
982         return false;
983         }
984       else if (this.env.contentframe)
985         {
986         var elm = document.getElementById(this.env.contentframe);
987         elm.style.visibility = 'hidden';
988         }
989       }
990     else if (this.task=='settings')
991       {
992       if (this.selection.length==1)
993         this.command('edit', this.selection[0]);
994       }
995
996     this.list_rows[id].clicked = true;
997     setTimeout(this.ref+'.list_rows['+id+'].clicked=false;', this.dblclick_time);
998       
999     return false;
1000     };
1001
1002
1003
1004
1005   /*********************************************************/
1006   /*********     (message) list functionality      *********/
1007   /*********************************************************/
1008
1009
1010   // select/unselect a row
1011   this.select = function(id, multiple)
1012     {
1013     var selected = false
1014     
1015     if (this.list_rows[id] && !multiple)
1016       {
1017       this.clear_selection();
1018       this.selection[0] = id;
1019       this.list_rows[id].obj.className += ' selected';
1020       selected = true;
1021       }
1022     
1023     else if (this.list_rows[id])
1024       {
1025       if (!this.in_selection(id))  // select row
1026         {
1027         this.selection[this.selection.length] = id;
1028         this.set_classname(this.list_rows[id].obj, 'selected', true);        
1029         }
1030       else  // unselect row
1031         {
1032         var p = find_in_array(id, this.selection);
1033         var a_pre = this.selection.slice(0, p);
1034         var a_post = this.selection.slice(p+1, this.selection.length);
1035         this.selection = a_pre.concat(a_post);
1036         this.set_classname(this.list_rows[id].obj, 'selected', false);
1037         }
1038         
1039       selected = (this.selection.length==1);
1040       }
1041
1042     // enable/disable commands for message
1043     if (this.task=='mail')
1044       {
583f1c 1045       this.enable_command('show', 'reply', 'reply-all', 'forward', 'print', selected);
4e17e6 1046       this.enable_command('delete', 'moveto', this.selection.length>0 ? true : false);
T 1047       }
1048     else if (this.task=='addressbook')
1049       {
1050       this.enable_command('edit', /*'print',*/ selected);
1051       this.enable_command('delete', 'compose', this.selection.length>0 ? true : false);
1052       }
1053     };
1054
1055
1056   this.clear_selection = function()
1057     {
1058     for(var n=0; n<this.selection.length; n++)
1059       if (this.list_rows[this.selection[n]])
1060         this.set_classname(this.list_rows[this.selection[n]].obj, 'selected', false);
1061
1062     this.selection = new Array();    
1063     };
1064
1065
1066   // check if given id is part of the current selection
1067   this.in_selection = function(id)
1068     {
1069     for(var n in this.selection)
1070       if (this.selection[n]==id)
1071         return true;
1072
1073     return false;    
1074     };
1075
1076
1077   // select each row in list
1078   this.select_all = function(filter)
1079     {
1080     if (!this.list_rows || !this.list_rows.length)
1081       return false;
1082       
1083     // reset selection first
1084     this.clear_selection();
1085     
1086     for (var n in this.list_rows)
1087       if (!filter || this.list_rows[n][filter]==true)
1088       this.select(n, true);
1089     };
1090     
1091
1092   // when user doble-clicks on a row
1093   this.show_message = function(id, safe)
1094     {
1095     var add_url = '';
1096     var target = window;
1097     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1098       {
1099       target = window.frames[this.env.contentframe];
1100       add_url = '&_framed=1';
1101       }
1102       
1103     if (safe)
1104       add_url = '&_safe=1';
1105
1106     if (id)
1107       {
09941e 1108       this.set_busy(true, 'loading');
4e17e6 1109       target.location.href = this.env.comm_path+'&_action=show&_uid='+id+'&_mbox='+escape(this.env.mailbox)+add_url;
T 1110       }
1111     };
1112
1113
1114
1115   // list a specific page
1116   this.list_page = function(page)
1117     {
1118     if (page=='next')
1119       page = this.env.current_page+1;
1120     if (page=='prev' && this.env.current_page>1)
1121       page = this.env.current_page-1;
1122       
1123     if (page > 0 && page <= this.env.pagecount)
1124       {
1125       this.env.current_page = page;
1126       
1127       if (this.task=='mail')
1128         this.list_mailbox(this.env.mailbox, page);
1129       else if (this.task=='addressbook')
1130         this.list_contacts(page);
1131       }
1132     };
1133
1134
1135   // list messages of a specific mailbox
f3b659 1136   this.list_mailbox = function(mbox, page, sort)
4e17e6 1137     {
T 1138     var add_url = '';
1139     var target = window;
1140
1141     if (!mbox)
1142       mbox = this.env.mailbox;
1143
f3b659 1144     // add sort to url if set
T 1145     if (sort)
1146       add_url += '&_sort=' + sort;
1147       
4e17e6 1148     // set page=1 if changeing to another mailbox
T 1149     if (!page && mbox != this.env.mailbox)
1150       {
1151       page = 1;
9fee0e 1152       add_url += '&_refresh=1';
4e17e6 1153       this.env.current_page = page;
T 1154       this.clear_selection();
1155       }
1156       
1157     if (this.env.mailbox!=mbox)
1158       this.select_mailbox(mbox);
1159
1160     // load message list remotely
1161     if (this.gui_objects.messagelist)
1162       {
9fee0e 1163       this.list_mailbox_remote(mbox, page, add_url);
4e17e6 1164       return;
T 1165       }
1166     
1167     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1168       {
1169       target = window.frames[this.env.contentframe];
9fee0e 1170       add_url += '&_framed=1';
4e17e6 1171       }
T 1172
1173     // load message list to target frame/window
1174     if (mbox)
1175       {
1176       this.set_busy(true, 'loading');
1177       target.location.href = this.env.comm_path+'&_mbox='+escape(mbox)+(page ? '&_page='+page : '')+add_url;
1178       }
1179     };
1180
1181
1182   // send remote request to load message list
9fee0e 1183   this.list_mailbox_remote = function(mbox, page, add_url)
4e17e6 1184     {
T 1185     // clear message list
1186     var table = this.gui_objects.messagelist;
1187     var tbody = document.createElement('TBODY');
1188     table.insertBefore(tbody, table.tBodies[0]);
1189     table.removeChild(table.tBodies[1]);
1190     
1191     this.message_rows = new Array();
1192     this.list_rows = this.message_rows;
1193
1194     // send request to server
1195     var url = '_mbox='+escape(mbox)+(page ? '&_page='+page : '');
1196     this.set_busy(true, 'loading');
ecf759 1197     this.http_request('list', url+add_url, true);
4e17e6 1198     };
T 1199
1200
1201   // move selected messages to the specified mailbox
1202   this.move_messages = function(mbox)
1203     {
1204     // exit if no mailbox specified or if selection is empty
1205     if (!mbox || !(this.selection.length || this.env.uid) || mbox==this.env.mailbox)
1206       return;
1207     
1208     var a_uids = new Array();
1209
1210     if (this.env.uid)
1211       a_uids[a_uids.length] = this.env.uid;
1212     else
1213       {
1214       var id;
1215       for (var n=0; n<this.selection.length; n++)
1216         {
1217         id = this.selection[n];
1218         a_uids[a_uids.length] = id;
1219       
1220         // 'remove' message row from list (just hide it)
1221         if (this.message_rows[id].obj)
1222           this.message_rows[id].obj.style.display = 'none';
1223         }
1224       }
ecf759 1225       
T 1226     var lock = false;
4e17e6 1227
T 1228     // show wait message
1229     if (this.env.action=='show')
ecf759 1230       {
T 1231       lock = true;
4e17e6 1232       this.set_busy(true, 'movingmessage');
ecf759 1233       }
4e17e6 1234
T 1235     // send request to server
ecf759 1236     this.http_request('moveto', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_target_mbox='+escape(mbox)+'&_from='+(this.env.action ? this.env.action : ''), lock);
4e17e6 1237     };
T 1238
1239
1240   // delete selected messages from the current mailbox
1241   this.delete_messages = function()
1242     {
1243     // exit if no mailbox specified or if selection is empty
1244     if (!(this.selection.length || this.env.uid))
1245       return;
1246     
1247     var a_uids = new Array();
1248
1249     if (this.env.uid)
1250       a_uids[a_uids.length] = this.env.uid;
1251     else
1252       {
1253       var id;
1254       for (var n=0; n<this.selection.length; n++)
1255         {
1256         id = this.selection[n];
1257         a_uids[a_uids.length] = id;
1258       
1259         // 'remove' message row from list (just hide it)
1260         if (this.message_rows[id].obj)
1261           this.message_rows[id].obj.style.display = 'none';
1262         }
1263       }
1264
1265     // send request to server
1266     this.http_request('delete', '_uid='+a_uids.join(',')+'&_mbox='+escape(this.env.mailbox)+'&_from='+(this.env.action ? this.env.action : ''));
1267     };
1268
1269
1270   // set a specific flag to one or more messages
1271   this.mark_message = function(flag, uid)
1272     {
1273     var a_uids = new Array();
1274     
1275     if (uid)
1276       a_uids[0] = uid;
1277     else if (this.env.uid)
1278       a_uids[0] = this.env.uid;
1279     else
1280       {
1281       var id;
1282       for (var n=0; n<this.selection.length; n++)
1283         {
1284         id = this.selection[n];
1285         a_uids[a_uids.length] = id;
1286       
1287         // 'remove' message row from list (just hide it)
1288         if (this.message_rows[id].obj)
1289           this.message_rows[id].obj.style.display = 'none';
1290         }
1291       }
1292
1293     // mark all message rows as read/unread
1294     var icn_src;
1295     for (var i=0; i<a_uids.length; i++)
1296       {
1297       uid = a_uids[i];
1298       if (this.message_rows[uid])
1299         {
1300         this.message_rows[uid].unread = (flag=='unread' ? true : false);
1301         
1302         if (this.message_rows[uid].classname.indexOf('unread')<0 && this.message_rows[uid].unread)
1303           {
1304           this.message_rows[uid].classname += ' unread';
fd660a 1305           this.set_classname(this.message_rows[uid].obj, 'unread', true);
T 1306
4e17e6 1307           if (this.env.unreadicon)
T 1308             icn_src = this.env.unreadicon;
1309           }
1310         else if (!this.message_rows[uid].unread)
1311           {
1312           this.message_rows[uid].classname = this.message_rows[uid].classname.replace(/\s*unread/, '');
fd660a 1313           this.set_classname(this.message_rows[uid].obj, 'unread', false);
4e17e6 1314
T 1315           if (this.message_rows[uid].replied && this.env.repliedicon)
1316             icn_src = this.env.repliedicon;
1317           else if (this.env.messageicon)
1318             icn_src = this.env.messageicon;
1319           }
1320
1321         if (this.message_rows[uid].icon && icn_src)
1322           this.message_rows[uid].icon.src = icn_src;
1323         }
1324       }
1325
1326     // send request to server
1327     this.http_request('mark', '_uid='+a_uids.join(',')+'&_flag='+flag);
1328     };
1329
1330
1331
1332   /*********************************************************/
1333   /*********        message compose methods        *********/
1334   /*********************************************************/
1335
1336
1337   this.show_attachment_form = function(a)
1338     {
1339     if (!this.gui_objects.uploadbox)
1340       return false;
1341       
1342     var elm, list;
1343     if (elm = this.gui_objects.uploadbox)
1344       {
1345       if (a &&  (list = this.gui_objects.attachmentlist))
1346         {
1347         var pos = rcube_get_object_pos(list);
1348         var left = pos.x;
1349         var top = pos.y + list.offsetHeight + 10;
1350       
1351         elm.style.top = top+'px';
1352         elm.style.left = left+'px';
1353         }
1354       
1355       elm.style.visibility = a ? 'visible' : 'hidden';
1356       }
1357       
1358     // clear upload form
1359     if (!a && this.gui_objects.attachmentform && this.gui_objects.attachmentform!=this.gui_objects.messageform)
1360       this.gui_objects.attachmentform.reset();
1361     };
1362
1363
1364   // upload attachment file
1365   this.upload_file = function(form)
1366     {
1367     if (!form)
1368       return false;
1369       
1370     // get file input fields
1371     var send = false;
1372     for (var n=0; n<form.elements.length; n++)
1373       if (form.elements[n].type=='file' && form.elements[n].value)
1374         {
1375         send = true;
1376         break;
1377         }
1378     
1379     // create hidden iframe and post upload form
1380     if (send)
1381       {
1382       var ts = new Date().getTime();
1383       var frame_name = 'rcmupload'+ts;
1384
1385       // have to do it this way for IE
1386       // otherwise the form will be posted to a new window
1387       if(document.all && !window.opera)
1388         {
1389         var html = '<iframe name="'+frame_name+'" src="program/blank.gif" style="width:0;height:0;visibility:hidden;"></iframe>';
1390         document.body.insertAdjacentHTML('BeforeEnd',html);
1391         }
1392       else  // for standards-compilant browsers
1393         {
1394         var frame = document.createElement('IFRAME');
1395         frame.name = frame_name;
1396         frame.width = 10;
1397         frame.height = 10;
1398         frame.style.visibility = 'hidden';
1399         document.body.appendChild(frame);
1400         }
1401
1402       form.target = frame_name;
1403       form.action = this.env.comm_path+'&_action=upload';
1404       form.setAttribute('enctype', 'multipart/form-data');
1405       form.submit();
1406       }
1407     
1408     // set reference to the form object
1409     this.gui_objects.attachmentform = form;
1410     };
1411
1412
1413   // add file name to attachment list
1414   // called from upload page
1415   this.add2attachment_list = function(name)
1416     {
1417     if (!this.gui_objects.attachmentlist)
1418       return false;
1419       
1420     var li = document.createElement('LI');
1421     li.innerHTML = name;
1422     this.gui_objects.attachmentlist.appendChild(li);
1423     };
1424
1425
1426   // send remote request to add a new contact
1427   this.add_contact = function(value)
1428     {
1429     if (value)
1430       this.http_request('addcontact', '_address='+value);
1431     };
1432
1433
1434   /*********************************************************/
1435   /*********     keyboard live-search methods      *********/
1436   /*********************************************************/
1437
1438
1439   // handler for keyboard events on address-fields
1440   this.ksearch_keypress = function(e, obj)
1441     {
1442     if (typeof(this.env.contacts)!='object' || !this.env.contacts.length)
1443       return true;
1444
1445     if (this.ksearch_timer)
1446       clearTimeout(this.ksearch_timer);
1447
1448     if (!e)
1449       e = window.event;
1450       
1451     var highlight;
1452     var key = e.keyCode ? e.keyCode : e.which;
1453
1454     switch (key)
1455       {
1456       case 38:  // key up
1457       case 40:  // key down
1458         if (!this.ksearch_pane)
1459           break;
1460           
1461         var dir = key==38 ? 1 : 0;
1462         var next;
1463         
1464         highlight = document.getElementById('rcmksearchSelected');
1465         if (!highlight)
1466           highlight = this.ksearch_pane.ul.firstChild;
1467         
1468         if (highlight && (next = dir ? highlight.previousSibling : highlight.nextSibling))
1469           {
1470           highlight.removeAttribute('id');
1471           //highlight.removeAttribute('class');
1472           this.set_classname(highlight, 'selected', false);
1473           }
1474
1475         if (next)
1476           {
1477           next.setAttribute('id', 'rcmksearchSelected');
1478           this.set_classname(next, 'selected', true);
1479           this.ksearch_selected = next._rcm_id;
1480           }
1481
1482         if (e.preventDefault)
1483           e.preventDefault();
1484         return false;
1485
1486       case 9:  // tab
1487         if(e.shiftKey)
1488           break;
1489
1490       case 13:  // enter     
1491         if (this.ksearch_selected===null || !this.ksearch_input || !this.ksearch_value)
1492           break;
1493
1494         // get cursor pos
1495         var inp_value = this.ksearch_input.value.toLowerCase();
1496         var cpos = this.get_caret_pos(this.ksearch_input);
1497         var p = inp_value.lastIndexOf(this.ksearch_value, cpos);
1498         
1499         // replace search string with full address
1500         var pre = this.ksearch_input.value.substring(0, p);
1501         var end = this.ksearch_input.value.substring(p+this.ksearch_value.length, this.ksearch_input.value.length);
1502         var insert = this.env.contacts[this.ksearch_selected]+', ';
1503         this.ksearch_input.value = pre + insert + end;
1504         
1505         //this.ksearch_input.value = this.ksearch_input.value.substring(0, p)+insert;
1506         
1507         // set caret to insert pos
1508         cpos = p+insert.length;
1509         if (this.ksearch_input.setSelectionRange)
1510           this.ksearch_input.setSelectionRange(cpos, cpos);
1511         
1512         // hide ksearch pane
1513         this.ksearch_hide();
1514       
1515         if (e.preventDefault)
1516           e.preventDefault();
1517         return false;
1518
1519       case 27:  // escape
1520         this.ksearch_hide();
1521         break;
1522
1523       }
1524
1525     // start timer
1526     this.ksearch_timer = setTimeout(this.ref+'.ksearch_get_results()', 200);      
1527     this.ksearch_input = obj;
1528     
1529     return true;
1530     };
1531
1532
1533   // address search processor
1534   this.ksearch_get_results = function()
1535     {
1536     var inp_value = this.ksearch_input ? this.ksearch_input.value : null;
1537     if (inp_value===null)
1538       return;
1539
1540     // get string from current cursor pos to last comma
1541     var cpos = this.get_caret_pos(this.ksearch_input);
1542     var p = inp_value.lastIndexOf(',', cpos-1);
1543     var q = inp_value.substring(p+1, cpos);
1544
1545     // trim query string
1546     q = q.replace(/(^\s+|\s+$)/g, '').toLowerCase();
1547
1548     if (!q.length || q==this.ksearch_value)
1549       {
1550       if (!q.length && this.ksearch_pane && this.ksearch_pane.visible)
1551         this.ksearch_pane.show(0);
1552
1553       return;
1554       }
1555
1556     this.ksearch_value = q;
1557     
1558     // start searching the contact list
1559     var a_results = new Array();
1560     var a_result_ids = new Array();
1561     var c=0;
1562     for (var i=0; i<this.env.contacts.length; i++)
1563       {
1564       if (this.env.contacts[i].toLowerCase().indexOf(q)>=0)
1565         {
1566         a_results[c] = this.env.contacts[i];
1567         a_result_ids[c++] = i;
1568         
1569         if (c==15)  // limit search results
1570           break;
1571         }
1572       }
1573
1574     // display search results
1575     if (c && a_results.length)
1576       {
1577       var p, ul, li;
1578       
1579       // create results pane if not present
1580       if (!this.ksearch_pane)
1581         {
1582         ul = document.createElement('UL');
1583         this.ksearch_pane = new rcube_layer('rcmKSearchpane', {vis:0, zindex:30000});
1584         this.ksearch_pane.elm.appendChild(ul);
1585         this.ksearch_pane.ul = ul;
1586         }
1587       else
1588         ul = this.ksearch_pane.ul;
1589
1590       // remove all search results
1591       ul.innerHTML = '';
1592             
1593       // add each result line to list
1594       for (i=0; i<a_results.length; i++)
1595         {
1596         li = document.createElement('LI');
1597         li.innerHTML = a_results[i].replace(/</, '&lt;').replace(/>/, '&gt;');
1598         li._rcm_id = a_result_ids[i];
1599         ul.appendChild(li);
1600         }
1601
1602       // check if last selected item is still in result list
1603       if (this.ksearch_selected!==null)
1604         {
1605         p = find_in_array(this.ksearch_selected, a_result_ids);
1606         if (p>=0 && ul.childNodes)
1607           {
1608           ul.childNodes[p].setAttribute('id', 'rcmksearchSelected');
1609           this.set_classname(ul.childNodes[p], 'selected', true);
1610           }
1611         else
1612           this.ksearch_selected = null;
1613         }
1614       
1615       // if no item selected, select the first one
1616       if (this.ksearch_selected===null)
1617         {
1618         ul.firstChild.setAttribute('id', 'rcmksearchSelected');
1619         this.set_classname(ul.firstChild, 'selected', true);
1620         this.ksearch_selected = a_result_ids[0];
1621         }
1622
1623       // resize the containing layer to fit the list
1624       //this.ksearch_pane.resize(ul.offsetWidth, ul.offsetHeight);
1625     
1626       // move the results pane right under the input box and make it visible
1627       var pos = rcube_get_object_pos(this.ksearch_input);
1628       this.ksearch_pane.move(pos.x, pos.y+this.ksearch_input.offsetHeight);
1629       this.ksearch_pane.show(1); 
1630       }
1631     // hide results pane
1632     else
1633       this.ksearch_hide();
1634     };
1635
1636
1637   this.ksearch_blur = function(e, obj)
1638     {
1639     if (this.ksearch_timer)
1640       clearTimeout(this.ksearch_timer);
1641
1642     this.ksearch_value = '';      
1643     this.ksearch_input = null;
1644     
1645     this.ksearch_hide();
1646     };
1647
1648
1649   this.ksearch_hide = function()
1650     {
1651     this.ksearch_selected = null;
1652     
1653     if (this.ksearch_pane)
1654       this.ksearch_pane.show(0);    
1655     };
1656
1657
1658
1659   /*********************************************************/
1660   /*********         address book methods          *********/
1661   /*********************************************************/
1662
1663
1664   this.list_contacts = function(page)
1665     {
1666     var add_url = '';
1667     var target = window;
1668     
1669     if (page && this.current_page==page)
1670       return false;
1671
1672     // load contacts remotely
1673     if (this.gui_objects.contactslist)
1674       {
1675       this.list_contacts_remote(page);
1676       return;
1677       }
1678
1679     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1680       {
1681       target = window.frames[this.env.contentframe];
1682       add_url = '&_framed=1';
1683       }
1684
1685     this.set_busy(true, 'loading');
1686     location.href = this.env.comm_path+(page ? '&_page='+page : '')+add_url;
1687     };
1688
1689
1690   // send remote request to load contacts list
1691   this.list_contacts_remote = function(page)
1692     {
1693     // clear list
1694     var table = this.gui_objects.contactslist;
1695     var tbody = document.createElement('TBODY');
1696     table.insertBefore(tbody, table.tBodies[0]);
1697     table.tBodies[1].style.display = 'none';
1698     
1699     this.contact_rows = new Array();
1700     this.list_rows = this.contact_rows;
1701
1702     // send request to server
1703     var url = page ? '&_page='+page : '';
1704     this.set_busy(true, 'loading');
ecf759 1705     this.http_request('list', url, true);
4e17e6 1706     };
T 1707
1708
1709   // load contact record
1710   this.load_contact = function(cid, action, framed)
1711     {
1712     var add_url = '';
1713     var target = window;
1714     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1715       {
1716       add_url = '&_framed=1';
1717       target = window.frames[this.env.contentframe];
1718       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
1719       }
1720     else if (framed)
1721       return false;
1722       
1723     //if (this.env.framed && add_url=='')
1724     //  add_url = '&_framed=1';
1725     
1726     if (action && (cid || action=='add'))
1727       {
1728       this.set_busy(true);
1729       target.location.href = this.env.comm_path+'&_action='+action+'&_cid='+cid+add_url;
1730       }
1731     };
1732
1733
1734   this.delete_contacts = function()
1735     {
1736     // exit if no mailbox specified or if selection is empty
1737     if (!(this.selection.length || this.env.cid))
1738       return;
1739     
1740     var a_cids = new Array();
1741
1742     if (this.env.cid)
1743       a_cids[a_cids.length] = this.env.cid;
1744     else
1745       {
1746       var id;
1747       for (var n=0; n<this.selection.length; n++)
1748         {
1749         id = this.selection[n];
1750         a_cids[a_cids.length] = id;
1751       
1752         // 'remove' row from list (just hide it)
1753         if (this.contact_rows[id].obj)
1754           this.contact_rows[id].obj.style.display = 'none';
1755         }
1756
1757       // hide content frame if we delete the currently displayed contact
1758       if (this.selection.length==1 && this.env.contentframe)
1759         {
1760         var elm = document.getElementById(this.env.contentframe);
1761         elm.style.visibility = 'hidden';
1762         }
1763       }
1764
1765     // send request to server
1766     this.http_request('delete', '_cid='+a_cids.join(',')+'&_from='+(this.env.action ? this.env.action : ''));
1767     };
1768
1769
1770   // update a contact record in the list
1771   this.update_contact_row = function(cid, cols_arr)
1772     {
1773     if (!this.contact_rows[cid] || !this.contact_rows[cid].obj)
1774       return false;
1775       
1776     var row = this.contact_rows[cid].obj;
1777     for (var c=0; c<cols_arr.length; c++)
1778       if (row.cells[c])
1779         row.cells[c].innerHTML = cols_arr[c];
1780
1781     };
1782   
1783
1784
1785   /*********************************************************/
1786   /*********        user settings methods          *********/
1787   /*********************************************************/
1788
1789
1790   // load contact record
1791   this.load_identity = function(id, action)
1792     {
1793     if (action=='edit-identity' && (!id || id==this.env.iid))
1794       return;
1795
1796     var add_url = '';
1797     var target = window;
1798     if (this.env.contentframe && window.frames && window.frames[this.env.contentframe])
1799       {
1800       add_url = '&_framed=1';
1801       target = window.frames[this.env.contentframe];
1802       document.getElementById(this.env.contentframe).style.visibility = 'inherit';
1803       }
1804
1805     if (action && (id || action=='add-identity'))
1806       {
1807       this.set_busy(true);
1808       target.location.href = this.env.comm_path+'&_action='+action+'&_iid='+id+add_url;
1809       }
1810     };
1811
1812
1813
1814   this.delete_identity = function(id)
1815     {
1816     // exit if no mailbox specified or if selection is empty
1817     if (!(this.selection.length || this.env.iid))
1818       return;
1819     
1820     if (!id)
1821       id = this.env.iid ? this.env.iid : this.selection[0];
1822
1823 /*
1824     // 'remove' row from list (just hide it)
1825     if (this.identity_rows && this.identity_rows[id].obj)
1826       {
1827       this.clear_selection();
1828       this.identity_rows[id].obj.style.display = 'none';
1829       }
1830 */
1831
1832     // if (this.env.framed && id)
1833       this.set_busy(true);
1834       location.href = this.env.comm_path+'&_action=delete-identity&_iid='+id;
1835     // else if (id)
1836     //  this.http_request('delete-identity', '_iid='+id);
1837     };
1838
1839
1840   this.create_folder = function(name)
1841     {
1842     var form;
1843     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
1844       name = form.elements['_folder_name'].value;
1845
1846     if (name)
ecf759 1847       this.http_request('create-folder', '_name='+escape(name), true);
4e17e6 1848     else if (form.elements['_folder_name'])
T 1849       form.elements['_folder_name'].focus();
1850     };
1851
1852
1853   this.delete_folder = function(folder)
1854     {
1855     if (folder)
1856       {
1857       for (var id in this.env.subscriptionrows)
1858         if (this.env.subscriptionrows[id]==folder)
1859           break;
1860           
1861       var row;
1862       if (id && (row = document.getElementById(id)))
1863         row.style.display = 'none';
1864
1865       this.http_request('delete-folder', '_mboxes='+escape(folder));
1866       }
1867     };
1868
1869
1870   this.subscribe_folder = function(folder)
1871     {
1872     var form;
1873     if ((form = this.gui_objects.editform) && form.elements['_unsubscribed'])
1874       this.change_subscription('_unsubscribed', '_subscribed', 'subscribe');
1875     else if (folder)
1876       this.http_request('subscribe', '_mboxes='+escape(folder));
1877     };
1878
1879
1880   this.unsubscribe_folder = function(folder)
1881     {
1882     var form;
1883     if ((form = this.gui_objects.editform) && form.elements['_subscribed'])
1884       this.change_subscription('_subscribed', '_unsubscribed', 'unsubscribe');
1885     else if (folder)
1886       this.http_request('unsubscribe', '_mboxes='+escape(folder));
1887     };
1888     
1889
1890   this.change_subscription = function(from, to, action)
1891     {
1892     var form;
1893     if (form = this.gui_objects.editform)
1894       {
1895       var a_folders = new Array();
1896       var list_from = form.elements[from];
1897
1898       for (var i=0; list_from && i<list_from.options.length; i++)
1899         {
1900         if (list_from.options[i] && list_from.options[i].selected)
1901           {
1902           a_folders[a_folders.length] = list_from.options[i].value;
1903           list_from[i] = null;
1904           i--;
1905           }
1906         }
1907
1908       // yes, we have some folders selected
1909       if (a_folders.length)
1910         {
1911         var list_to = form.elements[to];
1912         var index;
1913         
1914         for (var n=0; n<a_folders.length; n++)
1915           {
1916           index = list_to.options.length;
1917           list_to[index] = new Option(a_folders[n]);
1918           }
1919           
1920         this.http_request(action, '_mboxes='+escape(a_folders.join(',')));
1921         }
1922       }
1923       
1924     };
1925
1926
1927    // add a new folder to the subscription list by cloning a folder row
1928    this.add_folder_row = function(name)
1929      {
1930      if (!this.gui_objects.subscriptionlist)
1931        return false;
1932
1933      var tbody = this.gui_objects.subscriptionlist.tBodies[0];
1934      var id = tbody.childNodes.length+1;
1935      
1936      // clone a table row
1937      var row = this.clone_table_row(tbody.rows[0]);
1938      row.id = 'rcmrow'+id;
1939      tbody.appendChild(row);
1940
1941      // add to folder/row-ID map
1942      this.env.subscriptionrows[row.id] = name;
1943
1944      // set folder name
1945      row.cells[0].innerHTML = name;
1946      if (row.cells[1].firstChild.tagName=='INPUT')
1947        {
1948        row.cells[1].firstChild.value = name;
1949        row.cells[1].firstChild.checked = true;
1950        }
1951      if (row.cells[2].firstChild.tagName=='A')
1952        row.cells[2].firstChild.onclick = new Function(this.ref+".command('delete-folder','"+name+"')");
14eafe 1953
T 1954     var form;
1955     if ((form = this.gui_objects.editform) && form.elements['_folder_name'])
1956       form.elements['_folder_name'].value = '';
4e17e6 1957      };
T 1958
1959
1960   // duplicate a specific table row
1961   this.clone_table_row = function(row)
1962     {
1963     var cell, td;
1964     var new_row = document.createElement('TR');
1965     for(var n=0; n<row.childNodes.length; n++)
1966       {
1967       cell = row.childNodes[n];
1968       td = document.createElement('TD');
1969
1970       if (cell.className)
1971         td.className = cell.className;
1972       if (cell.align)
1973         td.setAttribute('align', cell.align);
1974         
1975       td.innerHTML = cell.innerHTML;
1976       new_row.appendChild(td);
1977       }
1978     
1979     return new_row;
1980     };
1981
1982
1983   /*********************************************************/
1984   /*********           GUI functionality           *********/
1985   /*********************************************************/
1986
1987
1988   // eable/disable buttons for page shifting
1989   this.set_page_buttons = function()
1990     {
1991     this.enable_command('nextpage', (this.env.pagecount > this.env.current_page));
1992     this.enable_command('previouspage', (this.env.current_page > 1));
1993     }
1994
1995
1996   // set button to a specific state
1997   this.set_button = function(command, state)
1998     {
1999     var a_buttons = this.buttons[command];
2000     var button, obj;
2001
2002     if(!a_buttons || !a_buttons.length)
2003       return;
2004
2005     for(var n=0; n<a_buttons.length; n++)
2006       {
2007       button = a_buttons[n];
2008       obj = document.getElementById(button.id);
2009
2010       // get default/passive setting of the button
2011       if (obj && button.type=='image' && !button.status)
2012         button.pas = obj._original_src ? obj._original_src : obj.src;
2013       else if (obj && !button.status)
2014         button.pas = String(obj.className);
2015
2016       // set image according to button state
2017       if (obj && button.type=='image' && button[state])
2018         {
2019         button.status = state;        
2020         obj.src = button[state];
2021         }
2022       // set class name according to button state
2023       else if (obj && typeof(button[state])!='undefined')
2024         {
2025         button.status = state;        
2026         obj.className = button[state];        
2027         }
2028       // disable/enable input buttons
2029       if (obj && button.type=='input')
2030         {
2031         button.status = state;
2032         obj.disabled = !state;
2033         }
2034       }
2035     };
2036
2037
2038   // mouse over button
2039   this.button_over = function(command, id)
2040     {
2041     var a_buttons = this.buttons[command];
2042     var button, img;
2043
2044     if(!a_buttons || !a_buttons.length)
2045       return;
2046
2047     for(var n=0; n<a_buttons.length; n++)
2048       {
2049       button = a_buttons[n];
2050       if(button.id==id && button.status=='act')
2051         {
2052         img = document.getElementById(button.id);
2053         if (img && button.over)
2054           img.src = button.over;
2055         }
2056       }
2057     };
2058
2059
2060   // mouse out of button
2061   this.button_out = function(command, id)
2062     {
2063     var a_buttons = this.buttons[command];
2064     var button, img;
2065
2066     if(!a_buttons || !a_buttons.length)
2067       return;
2068
2069     for(var n=0; n<a_buttons.length; n++)
2070       {
2071       button = a_buttons[n];
2072       if(button.id==id && button.status=='act')
2073         {
2074         img = document.getElementById(button.id);
2075         if (img && button.act)
2076           img.src = button.act;
2077         }
2078       }
2079     };
2080
2081
2082   // set/unset a specific class name
2083   this.set_classname = function(obj, classname, set)
2084     {
2085     var reg = new RegExp('\s*'+classname, 'i');
2086     if (!set && obj.className.match(reg))
2087       obj.className = obj.className.replace(reg, '');
2088     else if (set && !obj.className.match(reg))
2089       obj.className += ' '+classname;
2090     };
2091
2092
2093   // display a specific alttext
2094   this.alttext = function(text)
2095     {
2096     
2097     };
2098
2099
2100   // display a system message
2101   this.display_message = function(msg, type, hold)
2102     {
2103     if (!this.loaded)  // save message in order to display after page loaded
2104       {
2105       this.pending_message = new Array(msg, type);
2106       return true;
2107       }
2108     
2109     if (!this.gui_objects.message)
2110       return false;
2111       
2112     if (this.message_timer)
2113       clearTimeout(this.message_timer);
2114     
2115     var cont = msg;
2116     if (type)
2117       cont = '<div class="'+type+'">'+cont+'</div>';
a95e0e 2118
T 2119     this.gui_objects.message._rcube = this;
4e17e6 2120     this.gui_objects.message.innerHTML = cont;
T 2121     this.gui_objects.message.style.display = 'block';
a95e0e 2122     
T 2123     if (type!='loading')
2124       this.gui_objects.message.onmousedown = function(){ this._rcube.hide_message(); return true; };
4e17e6 2125     
T 2126     if (!hold)
2127       this.message_timer = setTimeout(this.ref+'.hide_message()', this.message_time);
2128     };
2129
2130
2131   // make a message row disapear
2132   this.hide_message = function()
2133     {
2134     if (this.gui_objects.message)
a95e0e 2135       {
4e17e6 2136       this.gui_objects.message.style.display = 'none';
a95e0e 2137       this.gui_objects.message.onmousedown = null;
T 2138       }
4e17e6 2139     };
T 2140
2141
2142   // mark a mailbox as selected and set environment variable
2143   this.select_mailbox = function(mbox)
2144     {
2145     if (this.gui_objects.mailboxlist)
2146       {
2147       var item, reg, text_obj;
6a35c8 2148       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
4e17e6 2149       var s_mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
T 2150       var s_current = this.env.mailbox.toLowerCase().replace(this.mbox_expression, '');
597170 2151       
6a35c8 2152       var current_li = document.getElementById('rcmbx'+s_current);
T 2153       var mbox_li = document.getElementById('rcmbx'+s_mbox);
2154       
2155       if (current_li)
2156         this.set_classname(current_li, 'selected', false);
2157       if (mbox_li)
2158         this.set_classname(mbox_li, 'selected', true);
4e17e6 2159       }
T 2160     
2161     this.env.mailbox = mbox;
2162     };
2163
2164
2165   // create a table row in the message list
2166   this.add_message_row = function(uid, cols, flags, attachment)
2167     {
2168     if (!this.gui_objects.messagelist || !this.gui_objects.messagelist.tBodies[0])
2169       return false;
2170     
2171     var tbody = this.gui_objects.messagelist.tBodies[0];
2172     var rowcount = tbody.rows.length;
2173     var even = rowcount%2;
2174     
2175     this.env.messages[uid] = {replied:flags.replied?1:0,
2176                               unread:flags.unread?1:0};
2177     
2178     var row = document.createElement('TR');
2179     row.id = 'rcmrow'+uid;
2180     row.className = 'message '+(even ? 'even' : 'odd')+(flags.unread ? ' unread' : '');
2181
2182     if (this.in_selection(uid))
2183       row.className += ' selected';
2184
2185     var icon = flags.unread && this.env.unreadicon ? this.env.unreadicon :
2186                (flags.replied && this.env.repliedicon ? this.env.repliedicon : this.env.messageicon);
2187
2188     var col = document.createElement('TD');
2189     col.className = 'icon';
2190     col.innerHTML = icon ? '<img src="'+icon+'" alt="" border="0" />' : '';
2191     row.appendChild(col);
2192
2193     // add each submitted col
2194     for (var c in cols)
2195       {
2196       col = document.createElement('TD');
2197       col.className = String(c).toLowerCase();
2198       col.innerHTML = cols[c];
2199       row.appendChild(col);
2200       }
2201
2202     col = document.createElement('TD');
2203     col.className = 'icon';
2204     col.innerHTML = attachment && this.env.attachmenticon ? '<img src="'+this.env.attachmenticon+'" alt="" border="0" />' : '';
2205     row.appendChild(col);
2206     
2207     tbody.appendChild(row);
2208     this.init_message_row(row);
2209     };
2210
2211
2212   // replace content of row count display
2213   this.set_rowcount = function(text)
2214     {
2215     if (this.gui_objects.countdisplay)
2216       this.gui_objects.countdisplay.innerHTML = text;
2217
2218     // update page navigation buttons
2219     this.set_page_buttons();
2220     };
2221
2222
2223   // update the mailboxlist
2224   this.set_unread_count = function(mbox, count)
2225     {
2226     if (!this.gui_objects.mailboxlist)
2227       return false;
2228       
2229     mbox = String(mbox).toLowerCase().replace(this.mbox_expression, '');
2230     
2231     var item, reg, text_obj;
2232     for (var n=0; n<this.gui_objects.mailboxlist.childNodes.length; n++)
2233       {
2234       item = this.gui_objects.mailboxlist.childNodes[n];
2235
2236       if (item.className && item.className.indexOf('mailbox '+mbox)>=0)
2237         {
2238         // set new text
2239         text_obj = item.firstChild;
2240         reg = /\s+\([0-9]+\)$/i;
2241
2242         if (count && text_obj.innerHTML.match(reg))
2243           text_obj.innerHTML = text_obj.innerHTML.replace(reg, ' ('+count+')');
2244         else if (count)
2245           text_obj.innerHTML += ' ('+count+')';
2246         else
2247           text_obj.innerHTML = text_obj.innerHTML.replace(reg, '');
2248           
2249         // set the right classes
2250         this.set_classname(item, 'unread', count>0 ? true : false);        
2251         break;
2252         }
2253       }
2254     };
2255
2256
2257   // add row to contacts list
2258   this.add_contact_row = function(cid, cols)
2259     {
2260     if (!this.gui_objects.contactslist || !this.gui_objects.contactslist.tBodies[0])
2261       return false;
2262     
2263     var tbody = this.gui_objects.contactslist.tBodies[0];
2264     var rowcount = tbody.rows.length;
2265     var even = rowcount%2;
2266     
2267     var row = document.createElement('TR');
2268     row.id = 'rcmrow'+cid;
2269     row.className = 'contact '+(even ? 'even' : 'odd');
2270     
2271     if (this.in_selection(cid))
2272       row.className += ' selected';
2273
2274     // add each submitted col
2275     for (var c in cols)
2276       {
2277       col = document.createElement('TD');
2278       col.className = String(c).toLowerCase();
2279       col.innerHTML = cols[c];
2280       row.appendChild(col);
2281       }
2282     
2283     tbody.appendChild(row);
2284     this.init_table_row(row, 'contact_rows');
2285     };
2286
2287
2288
2289   /********************************************************/
2290   /*********          drag & drop methods         *********/
2291   /********************************************************/
2292
2293
2294   this.drag_mouse_move = function(e)
2295     {
2296     if (this.drag_start)
2297       {
2298       if (!this.draglayer)
2299         this.draglayer = new rcube_layer('rcmdraglayer', {x:0, y:0, width:300, vis:0, zindex:2000});
2300       
2301       // get subjects of selectedd messages
2302       var names = '';
2303       var c, subject, obj;
2304       for(var n=0; n<this.selection.length; n++)
2305         {
2306         if (n>12)  // only show 12 lines
2307           {
2308           names += '...';
2309           break;
2310           }
2311
2312         if (this.message_rows[this.selection[n]].obj)
2313           {
2314           obj = this.message_rows[this.selection[n]].obj;
2315           subject = '';
2316
2317           for(c=0; c<obj.childNodes.length; c++)
2318             if (!subject && obj.childNodes[c].nodeName=='TD' && obj.childNodes[c].firstChild && obj.childNodes[c].firstChild.nodeType==3)
2319               {
2320               subject = obj.childNodes[c].firstChild.data;
2321               names += (subject.length > 50 ? subject.substring(0, 50)+'...' : subject) + '<br />';
2322               }
2323           }
2324         }
2325         
2326       this.draglayer.write(names);
2327       this.draglayer.show(1);
2328       }
2329
2330     var pos = this.get_mouse_pos(e);
2331     this.draglayer.move(pos.x+20, pos.y-5);
2332     
2333     this.drag_start = false;
2334     this.drag_active = true;
2335     
2336     return false;
2337     };
2338
2339
2340   this.drag_mouse_up = function()
2341     {
2342     document.onmousemove = null;
2343     
2344     if (this.draglayer && this.draglayer.visible)
2345       this.draglayer.show(0);
2346       
2347     this.drag_active = false;
2348     
2349     return false;
2350     };
2351
2352
2353
2354   /********************************************************/
2355   /*********        remote request methods        *********/
2356   /********************************************************/
2357
2358
ecf759 2359   this.http_sockets = new Array();
T 2360   
2361   // find a non-busy socket or create a new one
2362   this.get_request_obj = function()
4e17e6 2363     {
ecf759 2364     for (var n=0; n<this.http_sockets.length; n++)
4e17e6 2365       {
ecf759 2366       if (!this.http_sockets[n].busy)
T 2367         return this.http_sockets[n];
4e17e6 2368       }
ecf759 2369     
T 2370     // create a new XMLHTTP object
2371     var i = this.http_sockets.length;
2372     this.http_sockets[i] = new rcube_http_request();
4e17e6 2373
ecf759 2374     return this.http_sockets[i];
T 2375     };
2376   
2377
2378   // send a http request to the server
2379   this.http_request = function(action, querystring, lock)
2380     {
2381     var request_obj = this.get_request_obj();
4e17e6 2382     querystring += '&_remote=1';
T 2383     
2384     // add timestamp to request url to avoid cacheing problems in Safari
2385     if (bw.safari)
2386       querystring += '&_ts='+(new Date().getTime());
2387
2388     // send request
ecf759 2389     if (request_obj)
4e17e6 2390       {
T 2391       // prompt('request', this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
2392       console('HTTP request: '+this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
ecf759 2393
T 2394       if (lock)
2395         this.set_busy(true);
2396
2397       request_obj.__lock = lock ? true : false;
2398       request_obj.__action = action;
2399       request_obj.onerror = function(o){ rcube_webmail_client.http_error(o); };
2400       request_obj.oncomplete = function(o){ rcube_webmail_client.http_response(o); };
2401       request_obj.GET(this.env.comm_path+'&_action='+escape(action)+'&'+querystring);
4e17e6 2402       }
T 2403     };
2404
2405
ecf759 2406   // handle HTTP response
T 2407   this.http_response = function(request_obj)
4e17e6 2408     {
ecf759 2409     var ctype = request_obj.get_header('Content-Type');
T 2410     if (ctype)
2411       ctype = String(ctype).toLowerCase();
4e17e6 2412
ecf759 2413     if (request_obj.__lock)
4e17e6 2414       this.set_busy(false);
T 2415
ecf759 2416   console(request_obj.responseText);
4e17e6 2417
ecf759 2418     // if we get javascript code from server -> execute it
T 2419     if (request_obj.responseText && (ctype=='text/javascript' || ctype=='application/x-javascript'))
2420       eval(request_obj.responseText);
4e17e6 2421
ecf759 2422     // process the response data according to the sent action
T 2423     switch (request_obj.__action)
2424       {
2425       case 'delete':
2426       case 'moveto':
2427         if (this.env.action=='show')
2428           this.command('list');
2429         break;
4e17e6 2430           
ecf759 2431       case 'list':
T 2432         this.enable_command('select-all', 'select-none', this.env.messagecount ? true : false);
2433         break;
4e17e6 2434       }
ecf759 2435
T 2436     request_obj.reset();
4e17e6 2437     };
ecf759 2438
T 2439
2440   // handle HTTP request errors
2441   this.http_error = function(request_obj)
2442     {
2443     alert('Error sending request: '+request_obj.url);
2444
2445     if (request_obj.__lock)
2446       this.set_busy(false);
2447
2448     request_obj.reset();
2449     request_obj.__lock = false;
2450     };
2451
2452
2453   // use an image to send a keep-alive siganl to the server
2454   this.send_keep_alive = function()
2455     {
2456     var d = new Date();
2457     this.http_request('keep-alive', '_t='+d.getTime());
2458     };
2459     
4e17e6 2460
T 2461
2462   /********************************************************/
2463   /*********            helper methods            *********/
2464   /********************************************************/
2465   
2466   // check if we're in show mode or if we have a unique selection
2467   // and return the message uid
2468   this.get_single_uid = function()
2469     {
2470     return this.env.uid ? this.env.uid : (this.selection.length==1 ? this.selection[0] : null);
2471     };
2472
2473   // same as above but for contacts
2474   this.get_single_cid = function()
2475     {
2476     return this.env.cid ? this.env.cid : (this.selection.length==1 ? this.selection[0] : null);
2477     };
2478
2479
2480   // check if Shift-key is pressed on event
2481   this.check_shiftkey = function(e)
2482     {
2483     if(!e && window.event)
2484       e = window.event;
2485
2486     if(bw.linux && bw.ns4 && e.modifiers)
2487       return true;
2488     else if((bw.ns4 && e.modifiers & Event.SHIFT_MASK) || (e && e.shiftKey))
2489       return true;
2490     else
2491       return false;
2492     }
2493
2494
2495   this.get_mouse_pos = function(e)
2496     {
2497     if(!e) e = window.event;
2498     var mX = (e.pageX) ? e.pageX : e.clientX;
2499     var mY = (e.pageY) ? e.pageY : e.clientY;
2500
2501     if(document.body && document.all)
2502       {
2503       mX += document.body.scrollLeft;
2504       mY += document.body.scrollTop;
2505       }
2506
2507     return { x:mX, y:mY };
2508     };
2509     
2510   
2511   this.get_caret_pos = function(obj)
2512     {
2513     if (typeof(obj.selectionEnd)!='undefined')
2514       return obj.selectionEnd;
2515
2516     else if (document.selection && document.selection.createRange)
2517       {
2518       var range = document.selection.createRange();
2519       if (range.parentElement()!=obj)
2520         return 0;
2521
2522       var gm = range.duplicate();
2523       if (obj.tagName=='TEXTAREA')
2524         gm.moveToElementText(obj);
2525       else
2526         gm.expand('textedit');
2527       
2528       gm.setEndPoint('EndToStart', range);
2529       var p = gm.text.length;
2530
2531       return p<=obj.value.length ? p : -1;
2532       }
2533
2534     else
2535       return obj.value.length;
2536     };
2537
2538
2539   this.set_caret2start = function(obj)
2540     {
2541     if (obj.createTextRange)
2542       {
2543       var range = obj.createTextRange();
2544       range.collapse(true);
2545       range.select();
2546       }
2547     else if (obj.setSelectionRange)
2548       obj.setSelectionRange(0,0);
2549
2550     obj.focus();
2551     };
2552
2553
2554   // set all fields of a form disabled
2555   this.lock_form = function(form, lock)
2556     {
2557     if (!form || !form.elements)
2558       return;
2559     
2560     var type;
2561     for (var n=0; n<form.elements.length; n++)
2562       {
2563       type = form.elements[n];
2564       if (type=='hidden')
2565         continue;
2566         
2567       form.elements[n].disabled = lock;
2568       }
2569     };
2570     
2571   }  // end object rcube_webmail
2572
2573
2574
ecf759 2575 // class for HTTP requests
T 2576 function rcube_http_request()
2577   {
2578   this.url = '';
2579   this.busy = false;
2580   this.xmlhttp = null;
2581
2582
2583   // reset object properties
2584   this.reset = function()
2585     {
2586     // set unassigned event handlers
2587     this.onloading = function(){ };
2588     this.onloaded = function(){ };
2589     this.oninteractive = function(){ };
2590     this.oncomplete = function(){ };
2591     this.onabort = function(){ };
2592     this.onerror = function(){ };
2593     
2594     this.url = '';
2595     this.busy = false;
2596     this.xmlhttp = null;
2597     }
2598
2599
2600   // create HTMLHTTP object
2601   this.build = function()
2602     {
2603     if (window.XMLHttpRequest)
2604       this.xmlhttp = new XMLHttpRequest();
2605     else if (window.ActiveXObject)
2606       this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
2607     else
2608       {
2609       
2610       }
2611     }
2612
2613   // sedn GET request
2614   this.GET = function(url)
2615     {
2616     this.build();
2617
2618     if (!this.xmlhttp)
2619       {
2620       this.onerror(this);
2621       return false;
2622       }
2623
2624     var ref = this;
2625     this.url = url;
2626     this.busy = true;
2627
2628     this.xmlhttp.onreadystatechange = function(){ ref.xmlhttp_onreadystatechange(); };
2629     this.xmlhttp.open('GET', url);
2630     this.xmlhttp.send(null);
2631     };
2632
2633
2634   this.POST = function(url, a_param)
2635     {
2636     // not implemented yet
2637     };
2638
2639
2640   // handle onreadystatechange event
2641   this.xmlhttp_onreadystatechange = function()
2642     {
2643     if(this.xmlhttp.readyState == 1)
2644       this.onloading(this);
2645
2646     else if(this.xmlhttp.readyState == 2)
2647       this.onloaded(this);
2648
2649     else if(this.xmlhttp.readyState == 3)
2650       this.oninteractive(this);
2651
2652     else if(this.xmlhttp.readyState == 4)
2653       {
2654       this.responseText = this.xmlhttp.responseText;
2655       this.responseXML = this.xmlhttp.responseXML;
2656       
2657       if(this.xmlhttp.status == 0)
2658         this.onabort(this);
2659       else if(this.xmlhttp.status == 200)
2660         this.oncomplete(this);
2661       else
2662         this.onerror(this);
2663         
2664       this.busy = false;
2665       }
2666     }
2667
2668   // getter method for HTTP headers
2669   this.get_header = function(name)
2670     {
2671     return this.xmlhttp.getResponseHeader(name);
2672     };
2673
2674
2675   this.reset();
2676   
2677   }  // end class rcube_http_request
2678
2679
4e17e6 2680
T 2681 function console(str)
2682   {
2683   if (document.debugform && document.debugform.console)
2684     document.debugform.console.value += str+'\n--------------------------------------\n';
2685   }
2686
2687
2688 // set onload handler
2689 window.onload = function(e)
2690   {
2691   if (window.rcube_webmail_client)
2692     rcube_webmail_client.init();
2693   };