]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/ajax.js
Release 6.2.0beta4
[Github/sugarcrm.git] / modules / Emails / javascript / ajax.js
1 /*********************************************************************************
2  * SugarCRM is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36
37 if (typeof console == "undefined")
38         console = { log: function(o) {alert(o)} };
39
40 var AjaxObject = {
41         ret : '',
42         currentRequestObject : null,
43         //timeout : 30000, // 30 second timeout default
44         timeout : 9999999999, // 30 second timeout default
45         forceAbort : false,
46         trail : new Array(),
47
48         /**
49          */
50         _reset : function() {
51                 this.timeout = 30000;
52                 this.forceAbort = false;
53         },
54
55         folderRenameCleanup : function() {
56                 SUGAR.email2.folders.setSugarFolders();
57         },
58
59         fullSyncCleanup : function(o) {
60                 this.folders.checkMailCleanup(o);
61                 SUGAR.email2.settings.settingsDialog.hide();
62         },
63
64         /**
65          */
66         composeCache : function(o) {
67                 var idx = SUGAR.email2.composeLayout.currentInstanceId; // post instance increment
68                 // get email templates and user signatures
69                 var ret = JSON.parse(o.responseText);
70
71                 SUGAR.email2.composeLayout.emailTemplates = ret.emailTemplates;
72                 SUGAR.email2.composeLayout.signatures = ret.signatures;
73                 SUGAR.email2.composeLayout.fromAccounts = ret.fromAccounts;
74
75                 SUGAR.email2.composeLayout.setComposeOptions(idx);
76
77                 //Set the error array so we can notify the user when they try to hit send if any errors
78                 //are present.  We will also notify them now (after hitting compose button).
79                 SUGAR.email2.composeLayout.outboundAccountErrors = ret.errorArray;
80
81
82                 //if error element is returning an array, then check the length to make sure we have error messages
83                 if (typeof(ret.errorArray)=='object' && ret.errorArray instanceof Array && ret.errorArray.length > 0){
84                         //add error messages for display
85                         for(i in ret.errorArray)
86                                 overlay(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorArray[i], 'alert');
87                 }else if (typeof(ret.errorArray)=='object' && ret.errorArray!=null && ret.errorArray!='' ) {
88                         //if error element is returning an object, and the object value is not empty or null, then display error message
89                         for(i in ret.errorArray)
90                                 overlay(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorArray[i], 'alert');
91                 }
92
93                 //YUI bug with IE6 - Wont restore visibility property for nested select elements.
94                 if(SUGAR.isIE) {
95                         var overlayPanel = YAHOO.SUGAR.MessageBox.panel;
96                         if(overlayPanel) {
97                           overlayPanel.subscribe('hide',function() { YAHOO.util.Dom.setStyle('addressFrom' + idx,'visibility','');});
98                         }
99                 }
100         },
101
102
103         handleDeleteSignature : function(o) {
104                 hideOverlay();
105                 var ret = JSON.parse(o.responseText);
106                 SUGAR.email2.composeLayout.signatures = ret.signatures;
107         var field = document.getElementById('signature_id');
108                 SUGAR.email2.util.emptySelectOptions(field);
109
110                 for(var i in ret.signatures) { // iterate through assoc array
111                         var opt = new Option(ret.signatures[i], i);
112                         field.options.add(opt);
113                 }
114                 setSigEditButtonVisibility();
115         },
116
117         /**
118          */
119         handleDeleteReturn : function(o) {
120                 // force refresh ListView
121                 hideOverlay();
122                 if(document.getElementById('focusEmailMbox')) {
123                         YAHOO.namespace('frameFolders').selectednode = SUGAR.email2.folders.getNodeFromMboxPath(document.getElementById('focusEmailMbox').innerHTML);
124                 }
125
126                 // need to display success message before calling next async call?
127                 document.getElementById(this.target).innerHTML = o.responseText;
128         },
129
130         /**
131          */
132     handleFailure : function(o) {
133                 // Failure handler
134                 overlay('Exception occurred...', o.statusText, 'alert');
135                 if(document.getElementById('saveButton')) {
136                         document.getElementById('saveButton').disabled = false;
137                 }
138         },
139
140         handleReplyForward : function(o) {
141                 var a = JSON.parse(o.responseText);
142                 globalA = a;
143                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
144
145                 document.getElementById('email_id' + idx).value = a.uid;
146                 document.getElementById('emailSubject' + idx).value = a.name;
147                 document.getElementById('addressTO' + idx).value = a.from;
148                 document.getElementById('uid' + idx).value = a.uid;
149                 if(a.cc) {
150                         document.getElementById('addressCC' + idx).value = a.cc;
151                         SE.composeLayout.showHiddenAddress('cc', idx);
152                 }
153
154                 if(a.type) {
155                         document.getElementById('type' + idx).value = a.type;
156                 }
157
158                 // apply attachment values
159                 SUGAR.email2.composeLayout.loadAttachments(a.attachments);
160
161                 setTimeout("callbackReplyForward.finish(globalA);", 500);
162         },
163
164         handleReplyForwardForDraft : function(o) {
165                 var a = JSON.parse(o.responseText);
166                 globalA = a;
167                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
168
169                 document.getElementById('email_id' + idx).value = a.uid;
170                 document.getElementById('emailSubject' + idx).value = a.name;
171                 document.getElementById('addressTO' + idx).value = a.to;
172
173                 if(a.cc) {
174                         document.getElementById('addressCC' + idx).value = a.cc;
175                         SUGAR.email2.composeLayout.showHiddenAddress('cc',idx);
176                 }
177
178                 if(a.bcc) {
179                         document.getElementById('addressBCC' + idx).value = a.bcc;
180                         SUGAR.email2.composeLayout.showHiddenAddress('bcc',idx);
181                 }
182
183
184                 if(a.type) {
185                         document.getElementById('type' + idx).value = a.type;
186                 }
187
188
189                 // apply attachment values
190                 SUGAR.email2.composeLayout.loadAttachments(a.attachments);
191
192                 setTimeout("callbackReplyForward.finish(globalA);", 500);
193         },
194
195         /**
196          */
197         handleSuccess : function(o) {
198                 document.getElementById(this.target).innerHTML = o.responseText;
199                 hideOverlay();
200         },
201
202         /**
203          */
204         ieDeleteSuccess : function(o) {
205                 hideOverlay();
206
207                 var ret = JSON.parse(o.responseText);
208
209                 SUGAR.email2.accounts.refreshInboundAccountTable();
210                 alert(app_strings.LBL_EMAIL_IE_DELETE_SUCCESSFUL);
211                 SUGAR.email2.accounts.rebuildFolderList();
212
213         },
214
215         /**
216          */
217         ieSaveSuccess : function(o) {
218                 document.getElementById('saveButton').disabled = false;
219                 var a = JSON.parse(o.responseText);
220                 if (a) {
221                         if(a.error) {
222                                 overlay(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_CHECK_IE_SETTINGS, 'alert');
223                                 SUGAR.email2.accounts.ieAccountError(SUGAR.email2.accounts.errorStyle);
224                         } else {
225                                 resp = JSON.parse(o.responseText);
226                                 SUGAR.email2.accounts.refreshInboundAccountTable();
227                                 SUGAR.email2.accounts.refreshOuboundAccountTable();
228                                 SUGAR.email2.folders.startEmailCheckOneAccount(resp.id, true);
229                                 SUGAR.email2.accounts.inboundAccountEditDialog.hide();
230                         }
231                 } else {
232                      hideOverlay();
233                      overlay(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_SAVE_ACCOUNT, 'alert');
234                 }
235
236         },
237
238         /**
239          */
240         loadAttachments : function(o) {
241                 var result = JSON.parse(o.responseText);
242
243                 SUGAR.email2.composeLayout.loadAttachments(result);
244         },
245
246         /**
247          */
248         loadSignature : function(o) {
249                 var ret = JSON.parse(o.responseText);
250                 SUGAR.email2.signatures[ret.id] = ret.signature_html;
251                 SUGAR.email2.composeLayout.setSignature(SUGAR.email2.signatures.targetInstance);
252         },
253
254         /**
255          * Follow up to mark email read|unread|flagged
256          */
257         markEmailCleanup : function(o) {
258                 var ret = JSON.parse(o.responseText);
259                 if (!ret['status']) {
260                 hideOverlay();
261                         overlay(app_strings.LBL_EMAIL_ERROR_DESC, ret['message'], 'alert');
262                 } else {
263                         SUGAR.email2.contextMenus.markEmailCleanup();
264                 } // else
265         },
266
267         /**
268          */
269         rebuildShowFolders : function(o) {
270                 var t = JSON.parse(o.responseText);
271                 var show = document.getElementById('ieAccountListShow');
272
273                 SUGAR.email2.util.emptySelectOptions(show);
274
275                 for(i=0; i<t.length; i++) { // iterate through assoc array
276                         var opt = new Option(t[i].text, t[i].value, t[i].selected);
277                         opt.selected = t[i].selected;
278                         show.options.add(opt);
279                 }
280
281                 SUGAR.email2.accounts.renderTree();
282         },
283         /**
284          */
285         saveListViewSortOrderPart2 : function() {
286                 // create the JSON string the func expects
287                 focusFolderPath = '[ "Home", "' + ieName + '"';
288
289                 var f = new String(focusFolder);
290                 var fEx = f.split('.');
291
292                 for(i=0; i<fEx.length; i++) {
293                         focusFolderPath += ', "' + fEx[i] +'"'
294                 }
295
296                 focusFolderPath += ']';
297
298                 YAHOO.namespace('frameFolders').selectednode = SUGAR.email2.folders.getNodeFromMboxPath(focusFolderPath);
299                 SUGAR.email2.listView.populateListFrame(YAHOO.namespace('frameFolders').selectednode, ieId, 'true');
300         },
301
302         /**
303          *
304          */
305         sendEmailCleanUp : function(o) {
306                 hideOverlay();
307                 var ret = JSON.parse(o.responseText);
308                 if (ret) {
309                   SUGAR.email2.composeLayout.forceCloseCompose(ret.composeLayoutId);
310                   //SUGAR.email2.addressBook.showContactMatches(ret.possibleMatches);
311                 } else if (o.responseText) {
312                   overlay(mod_strings.LBL_SEND_EMAIL_FAIL_TITLE, o.responseText, 'alert');
313                 }
314
315                 if (typeof(SE.grid) != 'undefined')
316                         SE.listView.refreshGrid();
317                 //Disabled while address book is disabled
318
319                 //If this call back was initiated by quick compose from a Detail View page, refresh the
320                 //history subpanel.  If it was initiated by quickcreate from shortcut bar, then
321                 //close the shortcut bar menu
322                 if ( (typeof(action_sugar_grp1) != 'undefined')) {
323                         if(action_sugar_grp1 == 'DetailView') {
324                                 showSubPanel('history',null,true);
325                         } else if(action_sugar_grp1 == 'quickcreate') {
326                                 closeEmailOverlay();
327                         }
328                 }
329
330         },
331
332         ieSendSuccess : function(o) {
333                 hideOverlay();
334                 overlay(app_strings.LBL_EMAIL_TEST_OUTBOUND_SETTINGS_SENT, app_strings.LBL_EMAIL_TEST_NOTIFICATION_SENT, 'plain');
335         },
336
337         /**
338          */
339         settingsFolderRefresh : function(o) {
340                 //SUGAR.email2.accounts.rebuildFolderList(); // refresh frameFolder
341                 var ret = JSON.parse(o.responseText);
342                 var user = document.getElementById('userFolders');
343
344                 SUGAR.email2.util.emptySelectOptions(user);
345
346                 for(i=0; i<ret.userFolders.length; i++) {
347                         var display = ret.userFolders[i].name;
348                         var value = ret.userFolders[i].id;
349                         var selected = (ret.userFolders[i].selected != "") ? true : false;
350                         var opt = new Option(display, value, selected);
351                         opt.selected = selected;
352                         user.options.add(opt);
353                 }
354         },
355
356         /**
357          */
358         startRequest : function(callback, args, forceAbort) {
359                 if(this.currentRequestObject != null) {
360                         if(this.forceAbort == true) {
361                                 YAHOO.util.Connect.abort(this.currentRequestObject, null, false);
362                         }
363                 }
364                 this.currentRequestObject = YAHOO.util.Connect.asyncRequest('POST', "./index.php", callback, args);
365                 this._reset();
366         },
367
368         requestInProgress : function() {
369                 return (YAHOO.util.Connect.isCallInProgress(this.currentRequestObject));
370         },
371
372         /**
373          */
374         updateFolderSubscriptions : function() {
375                 SUGAR.email2.folders.lazyLoadSettings(); // refresh view in Settings overlay
376                 SUGAR.email2.folders.setSugarFolders(1000);// refresh view in TreeView
377                 hideOverlay();
378         },
379
380         /**
381          */
382         updateFrameFolder : function() {
383                 SUGAR.email2.folders.checkEmailAccounts();
384         },
385
386         /**
387          */
388         updateUserPrefs : function(o) {
389                 SUGAR.email2.userPrefs = JSON.parse(o.responseText);
390                 SUGAR.email2.folders.startCheckTimer(); // starts the auto-check interval
391         },
392
393         /**
394          */
395         uploadAttachmentSuccessful : function(o) {
396                 // clear out field
397                 document.getElementById('email_attachment').value = '';
398
399                 var ret = JSON.parse(o.responseText);
400                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
401                 var overall = document.getElementById('addedFiles' + idx);
402                 var index = overall.childNodes.length;
403                 var out =
404                         "<div id='email_attachment_bucket" + idx + index + "'>" +
405                                 // remove button
406                                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=minus.gif' " +
407                                         "style='cursor:pointer' align='absmiddle' onclick='SUGAR.email2.composeLayout.deleteUploadAttachment(\"" +
408                                         idx + index + "\",\"" + ret.guid + ret.name + "\");'/>" +
409                                 // file icon
410                                 "<img src='index.php?entryPoint=getImage&themeName=" + SUGAR.themes.theme_name + "&imageName=attachment.gif' " +
411                                         "id='email_attachmentImage'" + idx + index + "align='absmiddle' />" +
412                                 // hidden id field
413                                 "<input type='hidden' value='" + ret.guid + ret.name + "' name='email_attachment" + index + "' id='email_attachment" + idx + index + "' />" +
414                                 // file name
415                                 ((ret.nameForDisplay != null) ? ret.nameForDisplay + "&nbsp;" : ret.name + "&nbsp;") +
416                                 "<br/>" +
417                         "</div>";
418                 overall.innerHTML += out;
419                 if(SUGAR.email2.util.isIe()) {
420                         document.getElementById('addedFiles' + idx).innerHTML = document.getElementById('addedFiles' + idx).innerHTML;
421                 }
422
423                 // hide popup
424                 SUGAR.email2.addFileDialog.hide();
425                 // focus attachments
426                 SUGAR.email2.composeLayout.showAttachmentPanel(idx);
427         }
428 };
429
430
431 ///////////////////////////////////////////////////////////////////////////
432 ////    PER MODULE CALLBACK OBJECTS
433 AjaxObject.accounts = {
434         saveOutboundCleanup : function(o) {
435                 SUGAR.email2.accounts.refreshOuboundAccountTable();
436                 SUGAR.email2.accounts.outboundDialog.hide();
437                 var id = o.responseText;
438                 SUGAR.email2.accounts.newAddedOutboundId = id;
439                 },
440         saveDefaultOutboundCleanup: function(o){
441
442         },
443         callbackEditOutbound : {
444                 success : function(o)
445                 {
446                         var ret = JSON.parse(o.responseText);
447                         // show overlay
448                         SUGAR.email2.accounts.showAddSmtp();
449
450                         // fill values
451                         document.getElementById("mail_id").value = ret.id;
452                         document.getElementById("type").value = ret.type;
453                         document.getElementById("mail_sendtype").value = ret.mail_sendtype;
454                         document.getElementById("mail_name").value = ret.name;
455                         document.getElementById("mail_smtpserver").value = ret.mail_smtpserver;
456                         document.getElementById("outboundEmailForm").mail_smtptype.value = ret.mail_smtptype;
457                         document.getElementById("mail_smtpport").value = ret.mail_smtpport;
458                         document.getElementById("mail_smtpuser").value = ret.mail_smtpuser;
459                         //document.getElementById("mail_smtppass").value = ret.mail_smtppass;
460                         document.getElementById("mail_smtpauth_req").checked = (ret.mail_smtpauth_req == 1) ? true : false;
461                         SUGAR.email2.accounts.smtp_authenticate_field_display();
462                         document.getElementById("mail_smtpssl").options[ret.mail_smtpssl].selected = true;
463
464             if(ret.type == 'system-override') {
465                              SUGAR.email2.accounts.toggleOutboundAccountDisabledFields(true);
466                              SUGAR.email2.accounts.changeEmailScreenDisplay(ret.mail_smtptype,true);
467             }
468             else {
469                              SUGAR.email2.accounts.toggleOutboundAccountDisabledFields(false);
470                              SUGAR.email2.accounts.changeEmailScreenDisplay(ret.mail_smtptype,false);
471             }
472
473
474                 },
475                 failure : AjaxObject.handleFailure,
476                 timeout : AjaxObject.timeout,
477                 scope   : AjaxObject
478         },
479         callbackDeleteOutbound : {
480                 success : function(o) {
481                     var ret = JSON.parse(o.responseText);
482                     if(ret.is_error)
483                     {
484                         if(confirm(ret.error_message))
485                 {
486                     overlay(app_strings.LBL_EMAIL_IE_DELETE, app_strings.LBL_EMAIL_ONE_MOMENT);
487                     AjaxObject.startRequest(AjaxObject.accounts.callbackDeleteOutbound, urlStandard + "&emailUIAction=deleteOutbound&confirm=true&outbound_email=" + ret.outbound_email);
488                 }
489                 else
490                     hideOverlay();
491                     }
492                     else
493                     {
494                              hideOverlay();
495                              SUGAR.email2.accounts.refreshOuboundAccountTable();
496                     }
497                 },
498
499                 failure : AjaxObject.handleFailure,
500                 timeout : AjaxObject.timeout,
501                 scope   : AjaxObject
502         },
503
504         callbackCheckMailProgress : {
505            success : function(o) {
506                if (typeof(SUGAR.email2.accounts.totalMsgCount) == "undefined") {
507                    SUGAR.email2.accounts.totalMsgCount = -1;
508                }
509
510                //Check for server timeout / errors
511                var ret = JSON.parse(o.responseText);
512                var done = false;
513
514                if (typeof(o.responseText) == 'undefined' || o.responseText == "" || ret == false) {
515                    hideOverlay();
516                    overlay(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_TIMEOUT, 'alert');
517                    SUGAR.email2.accounts.totalMsgCount = -1;
518                //SUGAR.email2.folders.rebuildFolders();
519                done = true;
520                }
521
522                var currIeId = ret['ieid'];
523
524
525                var serverCount = ret.count;
526
527                if (ret['status'] == 'done') {
528                    for(i=0; i < SUGAR.email2.accounts.ieIds.length; i++) {
529                        if (i == SUGAR.email2.accounts.ieIds.length - 1) {
530                            //We are all done
531                            done = true;
532                            break;
533                        } else if (SUGAR.email2.accounts.ieIds[i] == currIeId) {
534                            //Go to next account
535                            currIeId = SUGAR.email2.accounts.ieIds[i+1];
536                            ret.count = 0;
537                            SUGAR.email2.accounts.totalMsgCount = -1;
538                            break;
539                        }
540                    }
541                }
542                else if (ret.mbox && ret.totalcount && ret.count) {
543                    SUGAR.email2.accounts.totalMsgCount = ret.totalcount;
544                    if (ret.count >= ret.totalcount) {
545                        serverCount = 0;
546                    }
547                } else if (SUGAR.email2.accounts.totalMsgCount < 0 && ret.totalcount) {
548                    SUGAR.email2.accounts.totalMsgCount = ret.totalcount;
549                } else {
550                        hideOverlay();
551                overlay(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_TIMEOUT, 'alert');
552                SUGAR.email2.accounts.totalMsgCount = -1;
553                done = true;
554                    }
555
556                if (done) {
557                    SUGAR.email2.accounts.totalMsgCount = -1;
558                    hideOverlay();
559                    SUGAR.email2.folders.rebuildFolders();
560                    SE.listView.refreshGrid();
561                } else if (SUGAR.email2.accounts.totalMsgCount < 0) {
562                YAHOO.SUGAR.MessageBox.updateProgress(0, mod_strings.LBL_CHECKING_ACCOUNT + ' '+ (i + 2) + ' '+ mod_strings.LBL_OF + ' ' + SUGAR.email2.accounts.ieIds.length);
563                AjaxObject.startRequest(AjaxObject.accounts.callbackCheckMailProgress, urlStandard +
564                                 '&emailUIAction=checkEmailProgress&ieId=' + currIeId + "&currentCount=0&synch=" + ret.synch);
565            } else {
566                YAHOO.SUGAR.MessageBox.updateProgress((ret.count / SUGAR.email2.accounts.totalMsgCount) * 100,
567                    app_strings.LBL_EMAIL_DOWNLOAD_STATUS.replace(/\[\[count\]\]/, ret.count).replace(/\[\[total\]\]/, SUGAR.email2.accounts.totalMsgCount));
568                    AjaxObject.startRequest(AjaxObject.accounts.callbackCheckMailProgress, urlStandard +
569                    '&emailUIAction=checkEmailProgress&ieId=' + currIeId + "&currentCount=" + serverCount +
570                    '&mbox=' + ret.mbox + '&synch=' + ret.synch + '&totalcount=' + SUGAR.email2.accounts.totalMsgCount);
571                }
572            },
573            failure : AjaxObject.handleFailure,
574        timeout : AjaxObject.timeout,
575        scope   : AjaxObject
576         }
577 };
578
579 ///////////////////////////////////////////////////////////////////////////////
580 ////    COMPOSE LAYOUT
581 AjaxObject.composeLayout = {
582         /**
583          * Populates the record id
584          */
585         saveDraftCleanup : function(o) {
586                 hideOverlay();
587                 var ret = JSON.parse(o.responseText);
588                 if(ret)
589                   SUGAR.email2.composeLayout.forceCloseCompose(ret.composeLayoutId);
590                 else if (o.responseText)
591                   overlay(mod_strings.LBL_ERROR_SAVING_DRAFT, o.responseText, 'alert');
592         }
593 };
594
595 AjaxObject.composeLayout.callback = {
596         saveDraft : {
597                 success : AjaxObject.composeLayout.saveDraftCleanup,
598                 failure : AjaxObject.handleFailure,
599                 timeout : AjaxObject.timeout,
600                 scope   : AjaxObject
601         }
602 };
603
604 AjaxObject.detailView = {
605         /**
606          * Pops-up a printable view of an email
607          */
608         displayPrintable : function(o) {
609                 var ret = JSON.parse(o.responseText);
610                 var displayTemplate = new YAHOO.SUGAR.Template(SUGAR.email2.templates['viewPrintable']);
611                 // 2 below must be in global context
612                 meta = ret.meta;
613                 meta['panelId'] = SUGAR.email2.util.getPanelId();
614                 email = ret.meta.email;
615                 if (typeof(email.cc) == 'undefined') {
616                   email.cc = "";
617                 }
618
619                 var out = displayTemplate.exec({
620                         'app_strings'   : app_strings,
621                         'theme'                 : theme,
622                         'idx'                   : 'Preview',
623                         'meta'                  : meta,
624                         'email'                 : meta.email
625                 });
626
627                 // open popup window
628                 var popup = window.open('modules/Emails/templates/_blank.html', 'printwin' ,
629                     'scrollbars=yes,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
630
631                 popup.document.write(out);
632                 popup.document.close();
633         },
634
635         /**
636          * Takes formatted response and creates a modal pop-over containing a title and content
637          */
638         displayView : function(o) {
639                 var SED = SUGAR.email2.detailView;
640                 var ret = JSON.parse(o.responseText);
641
642                 if(!SED.viewDialog) {
643                         SED.viewDialog = new YAHOO.widget.Dialog("viewDialog", {
644                                 modal:true,
645                 visible:true,
646                 fixedcenter:true,
647                 constraintoviewport: true,
648                                 shadow  : true
649                         });
650             SED.viewDialog.renderEvent.subscribe(function() {
651                 var content = this.body.firstChild;
652                 var viewH = YAHOO.util.Dom.getViewportHeight();
653                 if (content) {
654                         this.body.style.overflow = "auto";
655                         this.body.style.width = "800px";
656                         this.body.style.height = (viewH - 75 > content.clientHeight ? (content.clientHeight) : (viewH - 75)) + "px";
657                 }
658             }, SED.viewDialog);
659                 } // end lazy load
660                 SED.viewDialog.setHeader(ret.title);
661                 SED.viewDialog.setBody(ret.html);
662                 SED.viewDialog.render();
663                 SED.viewDialog.show();
664         },
665
666         /**
667          * Generates a modal popup to populate with the contents of bean's full EditView
668          */
669         showQuickCreateForm : function(o) {
670                 var SED = SUGAR.email2.detailView;
671                 var ret = JSON.parse(o.responseText);
672
673                 if(!SED.quickCreateDialog) {
674                         SED.quickCreateDialog = new YAHOO.widget.Dialog("quickCreate", {
675                                 modal:true,
676                                 visible:true,
677                 fixedcenter:true,
678                 constraintoviewport: true,
679                                 shadow  : true
680                         });
681
682             SED.quickCreateDialog.renderEvent.subscribe(function() {
683                 var viewH = YAHOO.util.Dom.getViewportHeight();
684                 var contH = 0;
685                 for (var i in this.body.childNodes) {
686                         if (this.body.childNodes[i].clientHeight) {
687                                 contH += this.body.childNodes[i].clientHeight;
688                         } else if (this.body.childNodes[i].offsetHeight) {
689                                 contH += this.body.childNodes[i].offsetHeight;
690                         } // if
691                 }
692                         this.body.style.width = "800px";
693                         this.body.style.height = (viewH - 75 > contH ? (contH + 10) : (viewH - 75)) + "px";
694                         this.body.style.overflow = "auto";
695             }, SED.quickCreateDialog);
696
697             SED.quickCreateDialog.hideEvent.subscribe(function(){
698                                 var qsFields = YAHOO.util.Dom.getElementsByClassName('.sqsEnabled', null, this.body);
699                                 /*for(var qsField in qsFields){
700                                         if (typeof QSFieldsArray[qsFields[qsField].id] != 'undefined')
701                                         Ext.getCmp('combobox_'+qsFields[qsField].id).destroy();
702                                 }*/
703                         });
704             SED.quickCreateDialog.setHeader(app_strings.LBL_EMAIL_QUICK_CREATE);
705                 } // end lazy load
706                 if (ret.html) {
707                         ret.html = ret.html.replace('<script type="text/javascript" src="include/SugarEmailAddress/SugarEmailAddress.js"></script>', "");
708                 }
709                 SED.quickCreateDialog.setBody(ret.html ? ret.html : "&nbsp;");
710                 SED.quickCreateDialog.render();
711                 SUGAR.util.evalScript(ret.html + '<script language="javascript">enableQS(true);</script>');
712
713                 SED.quickCreateDialog.ieId = ret.ieId;
714                 SED.quickCreateDialog.uid = ret.uid;
715         SED.quickCreateDialog.mbox = ret.mbox;
716         SED.quickCreateDialog.qcmodule = ret.module;
717
718         SED.quickCreateDialog.show();
719
720                 var editForm = document.getElementById('form_EmailQCView_' + ret.module);
721                 if (editForm) {
722                   editForm.module.value = 'Emails';
723                   var count = SUGAR.EmailAddressWidget.count[ret.module] ? SUGAR.EmailAddressWidget.count[ret.module] : 0;
724                   var tableId = YAHOO.util.Dom.getElementsByClassName('emailaddresses', 'table', editForm)[0].id;
725                   var instId = ret.module + count;
726                   SED.quickCreateEmailsToAdd = ret.emailAddress;
727                   SED.quickCreateEmailCallback = function(instId, tableId) {
728                           var eaw = SUGAR.EmailAddressWidget.instances[instId];
729                           if (typeof(eaw) == "undefined")
730                                   window.setTimeout("SUGAR.email2.detailView.quickCreateEmailCallback('"
731                                                 + instId + "','" + tableId + "');", 100);
732                           eaw.prefillEmailAddresses(tableId, SUGAR.email2.detailView.quickCreateEmailsToAdd);
733                   }
734                   window.setTimeout("SUGAR.email2.detailView.quickCreateEmailCallback('"
735                                         + instId + "','" + tableId + "');", 100);
736                 }
737         },
738
739         saveQuickCreateForm : function(o) {
740             hideOverlay();
741                 SUGAR.email2.detailView.quickCreateDialog.hide();
742                 validate['EditView'] = [ ];
743         },
744
745         saveQuickCreateFormAndReply : function(o) {
746             hideOverlay();
747             var ret = JSON.parse(o.responseText);
748         SUGAR.email2.detailView.quickCreateDialog.hide();
749         var qcd = SUGAR.email2.detailView.quickCreateDialog;
750         var type = (qcd.qcmodule == 'Cases') ? 'replyCase' : 'reply';
751         if (ret) {
752             var emailID = ret.id;
753             SUGAR.email2.composeLayout.c0_replyForwardEmail(null, ret.id, 'sugar::Emails', type);
754         } else {
755             SUGAR.email2.composeLayout.c0_replyForwardEmail(qcd.ieId, qcd.uid, qcd.mbox, type);
756         }
757         //Cean the validate cache to prevent errors on the next call
758         validate['EditView'] = [ ];
759     },
760
761         saveQuickCreateFormAndAddToAddressBook : function(o) {
762            hideOverlay();
763                 SUGAR.email2.detailView.quickCreateDialog.hide();
764                 SUGAR.email2.complexLayout.findPanel('contactsTab').show();
765                 validate['EditView'] = [ ];
766         },
767
768         handleAssignmentDialogAssignAction : function() {
769
770
771             var assign_user_id = window.document.forms['Distribute'].elements['assigned_user_id'].value;
772
773             var dist = 'direct';
774             var users = false;
775             var rules = false;
776             var get = "";
777             var found_teams = false;
778             var warning_message = mod_strings.LBL_WARN_NO_USERS;
779             if(!found_teams && assign_user_id == '' )
780             {
781                 alert(warning_message);
782                 return;
783             }
784
785             var emailUids = SUGAR.email2.listView.getUidsFromSelection();
786             var uids = "";
787             for(i=0; i<emailUids.length; i++) {
788                 if(uids != '') {
789                     uids += app_strings.LBL_EMAIL_DELIMITER;
790                 }
791                 uids += emailUids[i];
792             }
793
794             var row = SUGAR.email2.grid.getSelectedRows()[0];
795             var data = SUGAR.email2.grid.getRecord(row).getData();
796             var ieid = data.ieId;
797             var mbox = data.mbox;
798             AjaxObject.startRequest(callbackAssignmentAction, urlStandard + '&emailUIAction=' + "doAssignmentAssign&uids=" + uids + "&ieId=" + ieid + "&folder=" + mbox + "&distribute_method=" + dist + "&users=" +assign_user_id + get);
799             SUGAR.email2.contextMenus.assignToDialogue.hide();
800             overlay('Assignment', app_strings.LBL_EMAIL_ONE_MOMENT);
801
802         },
803
804         handleAssignmentDialogDeleteAction : function() {
805                 // TO pass list of UIDS/emailIds
806                 var uids = SUGAR.email2.listView.getUidsFromSelection();
807                 var row = SUGAR.email2.grid.getSelections()[0];
808                 var ieid = row.data.ieId;
809             var mbox = row.data.mbox;
810         AjaxObject.startRequest(callbackAssignmentAction, urlStandard + '&emailUIAction=' + "doAssignmentDelete&uids=" + uids + "&ieId=" + ieId + "&folder=" + mbox);
811         SUGAR.email2.contextMenus.assignmentDialog.hide();
812                 overlay(app_strings.LBL_EMAIL_PERFORMING_TASK, app_strings.LBL_EMAIL_ONE_MOMENT);
813
814                 // AJAX Call
815
816         },
817
818         showEmailDetailView : function(o) {
819         hideOverlay();
820         var SED = SUGAR.email2.detailView;
821                 var ret = JSON.parse(o.responseText);
822
823                 if(!SED.quickCreateDialog) {
824                         SED.quickCreateDialog = new YAHOO.widget.Dialog("emailDetailDialog", {
825                                 modal:true,
826                                 visible:true,
827                 //fixedcenter:true,
828                 constraintoviewport: true,
829                 draggable: true,
830                                 autofillheight: "body",
831                                 shadow  : true
832                         });
833                         SED.quickCreateDialog.renderEvent.subscribe(function() {
834                 var viewHeight = YAHOO.util.Dom.getViewportHeight();
835                 var contH = 0;
836                 for (var i in this.body.childNodes) {
837                         if (this.body.childNodes[i].offsetHeight)
838                                 contH += this.body.childNodes[i].offsetHeight;
839                 }
840                         this.body.style.overflow = "auto";
841                         this.body.style.width = "800px";
842                         this.body.style.height = (viewHeight - 75 > contH ? (contH + 10) : (viewHeight - 75)) + "px";
843                         this.center();
844             }, SED.quickCreateDialog);
845                 }
846                 SED.quickCreateDialog.setHeader(app_strings.LBL_EMAIL_RECORD);
847                 SED.quickCreateDialog.setBody(ret.html);
848                 SED.quickCreateDialog.render();
849         SUGAR.util.evalScript(ret.html);
850         SED.quickCreateDialog.show();
851         },
852
853         showAssignmentDialogWithData : function(o) {
854         var SEC = SUGAR.email2.contextMenus;
855                 hideOverlay();
856         var ret = JSON.parse(o.responseText);
857         if (!SEC.assignmentDialog) {
858                 SEC.assignmentDialog = new YAHOO.widget.Dialog("assignmentDialog", {
859                         visible:false,
860                 fixedcenter:true,
861                 constraintoviewport: true,
862                         modal   : true
863                 });
864                 SEC.assignmentDialog.setBody("");
865                 SEC.assignmentDialog.setHeader(app_strings.LBL_EMAIL_ASSIGNMENT);
866                 SEC.assignmentDialog.renderEvent.subscribe(function() {
867                 var iev = YAHOO.util.Dom.get("Distribute");
868                 if (iev) {
869                         this.body.style.width = "700px";
870                 }
871             },  SEC.assignmentDialog);
872                 SEC.assignmentDialog.render();
873         }
874         SEC.assignmentDialog.setBody(ret);
875         SEC.assignmentDialog.render();
876         validate = [];
877         SEC.assignmentDialog.show();
878         SUGAR.util.evalScript(ret);
879         },
880
881         showImportForm : function(o) {
882                 var SED = SUGAR.email2.detailView;
883                 var ret = JSON.parse(o.responseText);
884
885         document.getElementById('quickCreateContent').innerHTML = "";
886         hideOverlay();
887         if (!ret) {
888             return false;
889         }
890
891         if(!SED.importDialog) {
892             SED.importDialog = new YAHOO.widget.Dialog("importDialog", {
893                 modal:true,
894                 visible:false,
895                 fixedcenter:true,
896                 constraintoviewport: true,
897                 buttons : [{
898                         text: app_strings.LBL_EMAIL_ARCHIVE_TO_SUGAR, isDefault: true, handler: function(){
899                                 AjaxObject.detailView.getImportAction(SED.importDialog.ret); }
900                 }]//,
901                 //scroll : true
902             });
903             SED.importDialog.setHeader(app_strings.LBL_EMAIL_IMPORT_SETTINGS);
904             SED.importDialog.setBody("");
905             SED.importDialog.hideEvent.subscribe(function(){
906                 for(var i in QSFieldsArray) {
907                         if (QSFieldsArray[i] != null && typeof(QSFieldsArray[i]) == "object") {
908                                 QSFieldsArray[i].destroy();
909                                 delete QSFieldsArray[i];
910                         }
911                         if (QSProcessedFieldsArray[i]) {
912                                 QSProcessedFieldsArray[i] = false;
913                         } // if
914                                 }
915             });
916             SED.importDialog.renderEvent.subscribe(function() {
917                 var iev = YAHOO.util.Dom.get("ImportEditView");
918                 if (iev) {
919                         //this.body.style.height = (iev.clientHeight + 10) + "px";
920                         this.body.style.width = "600px";
921                 }
922             }, SED.importDialog);
923             SED.importDialog.render();
924         } // end lazy load
925         SED.importDialog.setBody(ret.html);
926         SED.importDialog.ret = ret;
927         SUGAR.util.evalScript(ret.html);
928         SED.importDialog.render();
929         validate = [];
930         SED.importDialog.show();
931         SED.importDialog.focusFirstButton();
932     },
933     getImportAction : function(ret) {
934         if (!check_form('ImportEditView')) return false;
935         if (!SUGAR.collection.prototype.validateTemSet('ImportEditView', 'team_name')) {
936                 alert(mod_strings.LBL_EMAILS_NO_PRIMARY_TEAM_SPECIFIED);
937                 return false;
938         } // if
939                 var get = "";
940         var editView = document.getElementById('ImportEditView');
941         if (editView.assigned_user_id != null) {
942             get = get + "&user_id=" + editView.assigned_user_id.value
943             //var user_id = editView.assigned_user_id.value;
944         }
945         var parent_id = editView.parent_id.value;
946         var parent_type = editView.parent_type.value;
947         var row = SUGAR.email2.grid.getSelectedRows()[0];
948         row = SUGAR.email2.grid.getRecord(row);
949         var data = row.getData();
950         var ieId = data.ieId;
951         var mbox = data.mbox;
952         var serverDelete = editView.serverDelete.checked;
953         var emailUids = SUGAR.email2.listView.getUidsFromSelection();
954         var uids = "";
955         for(i=0; i<emailUids.length; i++) {
956             if(uids != '') {
957                 uids += app_strings.LBL_EMAIL_DELIMITER;
958             }
959             uids += emailUids[i];
960         }
961
962         var action = 'importEmail&uid=';
963         if (ret.move) {
964             action = 'moveEmails';
965             action = action + '&sourceFolder=' + ret['srcFolder'];
966             action = action + '&sourceIeId=' + ret['srcIeId'];
967             action = action + '&destinationFolder=' + ret['dstFolder'];
968             action = action + '&destinationIeId=' + ret['dstIeId'];
969             action = action + '&emailUids=';
970         }
971         if (action.search(/importEmail/) != -1) {
972             overlay(app_strings.LBL_EMAIL_IMPORTING_EMAIL, app_strings.LBL_EMAIL_ONE_MOMENT);
973         } else {
974             overlay("Moving Email(s)", app_strings.LBL_EMAIL_ONE_MOMENT);
975         }
976
977         AjaxObject.startRequest(callbackStatusForImport, urlStandard + '&emailUIAction=' + action + uids + "&ieId=" + ieId + "&mbox=" + mbox +
978         get + "&parent_id=" + parent_id + "&parent_type=" + parent_type + '&delete=' + serverDelete);
979         SUGAR.email2.detailView.importDialog.hide();
980         document.getElementById('importDialogContent').innerHTML = "";
981
982     },
983     showRelateForm : function(o) {
984         var SED = SUGAR.email2.detailView;
985         var ret = JSON.parse(o.responseText);
986         document.getElementById('quickCreateContent').innerHTML = "";
987         hideOverlay();
988         if (!ret) {
989             return false;
990         }
991         dialog_loaded = true;
992
993         if(!SED.relateDialog) {
994             SED.relateDialog = new YAHOO.widget.Dialog('relateDialog', {
995                                 modal:true,
996                                 visible:true,
997                 fixedcenter:true,
998                 width: '800px',
999                 constraintoviewport: true,
1000                                 buttons : [{
1001                         text: app_strings.LBL_EMAIL_RELATE_TO, isDefault: true, handler: function(){
1002                                         if (!check_form('RelateEditView')) return false;
1003                                         var get = "";
1004                         var editView = document.getElementById('RelateEditView');
1005                         var parent_id = editView.parent_id.value;
1006                         var parent_type = editView.parent_type.value;
1007                         var row = SUGAR.email2.grid.getSelectedRows()[0];
1008                         row  = SUGAR.email2.grid.getRecord(row);
1009                         var ieId = row.getData().ieId;
1010                         var mbox = row.getData().mbox;
1011                         var emailUids = SUGAR.email2.listView.getUidsFromSelection();
1012                         var uids = "";
1013                         for(i=0; i<emailUids.length; i++) {
1014                             if(uids != '') {
1015                                 uids += app_strings.LBL_EMAIL_DELIMITER;
1016                             }
1017                             uids += emailUids[i];
1018                         }
1019                         overlay(app_strings.LBL_EMAIL_PERFORMING_TASK, app_strings.LBL_EMAIL_ONE_MOMENT);
1020                         AjaxObject.startRequest(callbackStatusForImport, urlStandard + '&emailUIAction=relateEmails&uid=' + uids
1021                             + "&ieId=" + ieId + "&mbox=" + mbox + "&parent_id=" + parent_id + "&parent_type=" + parent_type);
1022                         SED.relateDialog.hide();
1023                         document.getElementById('relateDialogContent').innerHTML = "";
1024                         }
1025                 }]
1026                         });
1027
1028             SED.relateDialog.hideEvent.subscribe(function(){
1029                 if (QSFieldsArray['ImportEditView_parent_name'] != null) {
1030                         QSFieldsArray['ImportEditView_parent_name'].destroy();
1031                         delete QSFieldsArray['ImportEditView_parent_name'];
1032                 } // if
1033                                 if (QSProcessedFieldsArray['ImportEditView_parent_name']) {
1034                                         QSProcessedFieldsArray['ImportEditView_parent_name'] = false;
1035                                 } // if
1036             });
1037
1038             SED.relateDialog.renderEvent.subscribe(function() {
1039                 var viewPortHeight = YAHOO.util.Dom.getViewportHeight();
1040                 var contH = 0;
1041                 for (var i in this.body.childNodes) {
1042                         if (this.body.childNodes[i].clientHeight)
1043                                 contH += this.body.childNodes[i].clientHeight;
1044                 }
1045             }, SED.relateDialog);
1046             SED.relateDialog.setHeader(app_strings.LBL_EMAIL_RELATE_EMAIL);
1047                 } // end lazy load
1048
1049         SED.relateDialog.setBody(ret.html);
1050         SED.relateDialog.render();
1051         SUGAR.util.evalScript(ret.html);
1052         SED.relateDialog.show();
1053     }
1054 };
1055 /**
1056  * DetailView callbacks
1057  */
1058 AjaxObject.detailView.callback = {
1059         emailDetail : {
1060                 success : function(o) {
1061                         SUGAR.email2.o = o;
1062                         var ret = JSON.parse(o.responseText);
1063                         SUGAR.email2.detailView.consumeMetaDetail(ret);
1064                 },
1065                 argument: [targetDiv],
1066                 failure : AjaxObject.handleFailure,
1067                 timeout : 0,
1068                 scope   : AjaxObject
1069         },
1070         emailPreview : {
1071                 success : function(o) {
1072                         SUGAR.email2.o = o;
1073                         var ret = JSON.parse(o.responseText);
1074                         SUGAR.email2.detailView.consumeMetaPreview(ret);
1075                 },
1076                 failure : AjaxObject.handleFailure,
1077                 timeout : 0,
1078                 scope   : AjaxObject
1079         },
1080         viewPrint : {
1081                 success : AjaxObject.detailView.displayPrintable,
1082                 failure : AjaxObject.handleFailure,
1083                 timeout : AjaxObject.timeout,
1084                 scope   : AjaxObject
1085         },
1086         viewRaw : {
1087                 success : AjaxObject.detailView.displayView,
1088                 failure : AjaxObject.handleFailure,
1089                 timeout : AjaxObject.timeout,
1090                 scope   : AjaxObject
1091         }
1092 };
1093
1094
1095
1096
1097
1098 AjaxObject.folders = {
1099         /**
1100          * check-mail post actions
1101          */
1102         checkMailCleanup : function(o) {
1103                 hideOverlay();
1104                 AjaxObject.folders.rebuildFolders(o); // rebuild TreeView
1105
1106                 // refresh focus ListView
1107                 SE.listView.refreshGrid();
1108                 SUGAR.email2.folders.startCheckTimer(); // resets the timer
1109         },
1110
1111         /**
1112          */
1113         rebuildFolders : function(o) {
1114                 hideOverlay();
1115
1116                 var data = JSON.parse(o.responseText);
1117
1118                 email2treeinit(SUGAR.email2.tree, data.tree_data, 'frameFolders', data.param);
1119                 SUGAR.email2.folders.setSugarFolders();
1120         }
1121 };
1122 AjaxObject.folders.callback = {
1123         checkMail : {
1124                 success : AjaxObject.folders.checkMailCleanup,
1125                 failure : AjaxObject.handleFailure,
1126                 timeout : 600000, // 5 mins
1127                 scope   : AjaxObject
1128         }
1129 }
1130
1131 AjaxObject.rules = {
1132         loadRulesForSettings : function(o) {
1133                 document.getElementById("rulesListCell").innerHTML = o.responseText;
1134                 // assume we have the class we need
1135                 SUGAR.routing.getStrings();
1136                 SUGAR.routing.getDependentDropdowns();
1137         }
1138 };
1139 ////    END PER MODULE CALLBACK OBJECTS
1140 ///////////////////////////////////////////////////////////////////////////
1141
1142
1143 var callback = {
1144         success : AjaxObject.handleSuccess,
1145         failure : AjaxObject.handleFailure,
1146         timeout : AjaxObject.timeout,
1147         scope   : AjaxObject
1148 };
1149 var callbackAccount = {
1150         success : AjaxObject.ieSaveSuccess,
1151         failure : AjaxObject.handleFailure,
1152         timeout : AjaxObject.timeout,
1153         scope   : AjaxObject
1154 };
1155 var callbackAccountDelete = {
1156         success : AjaxObject.ieDeleteSuccess,
1157         failure : AjaxObject.handleFailure,
1158         timeout : AjaxObject.timeout,
1159         scope   : AjaxObject
1160 };
1161 var callbackOutboundTest = {
1162         success : AjaxObject.ieSendSuccess,
1163         failure : AjaxObject.handleFailure,
1164         timeout : AjaxObject.timeout,
1165         scope   : AjaxObject
1166 };
1167
1168
1169
1170 var callbackTeamInfoForSettings = {
1171 success : function (o) {
1172         var data = JSON.parse(o.responseText);
1173         document.getElementById('EditViewGroupFolderTeamTD').innerHTML = data.defaultgroupfolder;
1174 },
1175 failure : AjaxObject.handleFailure,
1176 timeout : AjaxObject.timeout,
1177 scope   : AjaxObject
1178
1179 };
1180
1181 var callbackStatusForImport = {
1182 success : function (o) {
1183         hideOverlay();
1184         if (o.responseText != "")  {
1185                 var statusString = "";
1186                 var data = JSON.parse(o.responseText);
1187                 for(i=0; i<data.length; i++) {
1188                         statusString = statusString + data[i] + '<br/>';
1189                 }
1190                 overlay(SUGAR.language.get('Emails','LBL_IMPORT_STATUS_TITLE'), statusString, 'alert');
1191         }
1192         SE.listView.refreshGrid();
1193
1194 },
1195 failure : AjaxObject.handleFailure,
1196 timeout : AjaxObject.timeout,
1197 scope   : AjaxObject
1198
1199 };
1200 var callbackComposeCache = {
1201         success : AjaxObject.composeCache,
1202         failure : AjaxObject.handleFailure,
1203         timeout : AjaxObject.timeout,
1204         scope   : AjaxObject
1205 };
1206 var callbackDelete = {
1207         success : AjaxObject.handleDeleteReturn,
1208         failure : AjaxObject.handleFailure,
1209         timeout : AjaxObject.timeout,
1210         scope   : AjaxObject
1211 };
1212 var callbackEmailDetailMultiple = {
1213         success : function(o) {
1214                 hideOverlay();
1215                 var retMulti = JSON.parse(o.responseText);
1216                 var ret = new Object();
1217
1218                 for(var i=0; i<retMulti.length; i++) {
1219                         ret = retMulti[i];
1220
1221                         SUGAR.email2._setDetailCache(ret);
1222                         SUGAR.email2.detailView.populateDetailView(ret.meta.uid, ret.meta.mbox, ret.meta.ieId, true, SUGAR.email2.innerLayout);
1223                 }
1224         },
1225         failure : AjaxObject.handleFailure,
1226         timeout : 0,
1227         scope   : AjaxObject
1228 };
1229 var callbackListViewSortOrderChange = {
1230         success : AjaxObject.saveListViewSortOrderPart2,
1231         failure : AjaxObject.handleFailure,
1232         timeout : AjaxObject.timeout,
1233         scope   : AjaxObject,
1234         argument        : [ieId, ieName, focusFolder]
1235 };
1236 var callbackEmptyTrash = {
1237         success : function(o) {
1238                 hideOverlay();
1239                 AjaxObject.folderRenameCleanup;
1240         },
1241         failure : AjaxObject.handleFailure,
1242         timeout : AjaxObject.timeout,
1243         scope   : AjaxObject
1244 };
1245 var callbackClearCacheFiles = {
1246         success : function(o) {
1247                 hideOverlay();
1248         },
1249         failure : AjaxObject.handleFailure,
1250         timeout : AjaxObject.timeout,
1251         scope   : AjaxObject
1252 };
1253 var callbackFolderRename = {
1254         success : function(o) {hideOverlay();SUGAR.email2.folders.rebuildFolders();},
1255         failure : AjaxObject.handleFailure,
1256         timeout : AjaxObject.timeout,
1257         scope   : AjaxObject
1258 };
1259 var callbackFolderDelete = {
1260         success : function(o) {
1261                 var ret = JSON.parse(o.responseText);
1262                 if (ret.status) {
1263                     if (ret.folder_id) {
1264                         var node = SUGAR.email2.folders.getNodeFromId(ret.folder_id);
1265                         if(node)
1266                                 SUGAR.email2.tree.removeNode(node, true);
1267                     } else if (ret.ieId && ret.mbox) {
1268                         var node = SUGAR.email2.folders.getNodeFromIeIdAndMailbox(ret.ieId, ret.mbox);
1269                         if(node)
1270                                 SUGAR.email2.tree.removeNode(node, true);
1271                     }
1272                         hideOverlay();
1273                         //SUGAR.email2.folders.loadSettingFolder();
1274                 } else {
1275                         hideOverlay();
1276                         overlay(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorMessage, 'alert');
1277                 } // else
1278         },
1279         failure : AjaxObject.handleFailure,
1280         timeout : AjaxObject.timeout,
1281         scope   : AjaxObject
1282 };
1283 var callbackFolderSave = {
1284         success : function(o) {
1285                 var ret = JSON.parse(o.responseText);
1286
1287                 switch(ret.action) {
1288                         case 'newFolderSave':
1289                                 SUGAR.email2.folders.rebuildFolders();
1290                         break;
1291                 }
1292         },
1293         failure : AjaxObject.handleFailure,
1294         timeout : AjaxObject.timeout,
1295         scope   : AjaxObject
1296 };
1297 var callbackFolderSubscriptions = {
1298         success : AjaxObject.updateFolderSubscriptions,
1299         failure : AjaxObject.handleFailure,
1300         timeout : AjaxObject.timeout,
1301         scope   : AjaxObject
1302 };
1303 var callbackFolderUpdate = {
1304         success : AjaxObject.updateFrameFolder,
1305         failure : AjaxObject.handleFailure,
1306         timeout : AjaxObject.timeout,
1307         scope   : AjaxObject
1308 };
1309 var callbackFolders = {
1310         success : AjaxObject.folders.rebuildFolders,
1311         //success : void(true),
1312         failure : AjaxObject.handleFailure,
1313         timeout : AjaxObject.timeout,
1314         scope   : AjaxObject
1315 };
1316 var callbackFullSync = {
1317         success : AjaxObject.fullSyncCleanup,
1318         failure : AjaxObject.handleFailure,
1319         timeout : 9999999999999,
1320         scope   : AjaxObject
1321 };
1322 var callbackGeneric = {
1323         success : function() {
1324                 hideOverlay();
1325         },
1326         failure : AjaxObject.handleFailure,
1327         timeout : AjaxObject.timeout,
1328         scope   : AjaxObject
1329 };
1330 var callbackIeAccountRetrieve = {
1331         success : function(o) {
1332                 // return JSON encoding
1333                 hideOverlay();
1334                 SUGAR.email2.accounts.fillIeAccount(o.responseText);
1335                 SUGAR.email2.accounts.showEditInboundAccountDialogue(false);
1336         },
1337         failure : AjaxObject.handleFailure,
1338         timeout : AjaxObject.timeout,
1339         scope   : AjaxObject
1340 };
1341 var callbackImportOneEmail = {
1342         success :  AjaxObject.detailView.showImportForm,
1343         failure : AjaxObject.handleFailure,
1344         timeout : AjaxObject.timeout,
1345         scope   : AjaxObject
1346 };
1347 var callbackRelateEmail = {
1348     success : AjaxObject.detailView.showRelateForm,
1349     failure : AjaxObject.handleFailure,
1350     timeout : AjaxObject.timeout,
1351     scope   : AjaxObject
1352 }
1353 var callbackEmailDetailView = {
1354     success : AjaxObject.detailView.showEmailDetailView,
1355     failure : AjaxObject.handleFailure,
1356     timeout : AjaxObject.timeout,
1357     scope   : AjaxObject
1358 }
1359 var callbackAssignmentDialog = {
1360         success :  AjaxObject.detailView.showAssignmentDialogWithData,
1361         failure : AjaxObject.handleFailure,
1362         timeout : AjaxObject.timeout,
1363         scope   : AjaxObject
1364 };
1365 var callbackAssignmentAction = {
1366         success :  function(o) {
1367                 SE.listView.refreshGrid();
1368                 hideOverlay();
1369                 if(o.responseText != '') {
1370                overlay('Assignment action result', o.responseText, 'alert');
1371             } // if
1372         } ,
1373         failure : AjaxObject.handleFailure,
1374         timeout : AjaxObject.timeout,
1375         scope   : AjaxObject
1376 };
1377 var callbackMoveEmails = {
1378         success :  function(o) {
1379             SE.listView.refreshGrid();
1380                 hideOverlay();
1381                 if(o.responseText != '') {
1382                overlay(app_strings.LBL_EMAIL_ERROR_DESC, o.responseText, 'alert');
1383             } // if
1384         } ,
1385         failure : AjaxObject.handleFailure,
1386         timeout : AjaxObject.timeout,
1387         scope   : AjaxObject
1388 };
1389 var callbackLoadAttachments = {
1390         success : AjaxObject.loadAttachments,
1391         failure : AjaxObject.handleFailure,
1392         timeout : AjaxObject.timeout,
1393         scope   : AjaxObject
1394 };
1395 var callbackLoadRules = {
1396         success : AjaxObject.rules.loadRulesForSettings,
1397         failure : AjaxObject.handleFailure,
1398         timeout : AjaxObject.timeout,
1399         scope   : AjaxObject
1400 };
1401 var callbackLoadSignature = {
1402         success : AjaxObject.loadSignature,
1403         failure : AjaxObject.handleFailure,
1404         timeout : AjaxObject.timeout,
1405         scope   : AjaxObject
1406 };
1407 var callbackDeleteSignature = {
1408         success : AjaxObject.handleDeleteSignature,
1409         failure : AjaxObject.handleFailure,
1410         timeout : AjaxObject.timeout,
1411         scope   : AjaxObject
1412 };
1413 /*var callbackMoveEmails = {
1414     success : function(o) { SUGAR.email2.listView.moveEmailsCleanup(o) },
1415     failure : AjaxObject.handleFailure,
1416     timeout : AjaxObject.timeout,
1417     scope   : AjaxObject
1418 }*/
1419 var callbackOutboundSave = {
1420         success : AjaxObject.accounts.saveOutboundCleanup,
1421         failure : AjaxObject.handleFailure,
1422         timeout : AjaxObject.timeout,
1423         scope   : AjaxObject
1424 };
1425 var callbackDefaultOutboundSave = {
1426         success : AjaxObject.accounts.saveDefaultOutboundCleanup,
1427         failure : AjaxObject.handleFailure,
1428         timeout : AjaxObject.timeout,
1429         scope   : AjaxObject
1430 };
1431 var callbackQuickCreate = {
1432         success : AjaxObject.detailView.showQuickCreateForm,
1433         failure : AjaxObject.handleFailure,
1434         timeout : AjaxObject.timeout,
1435         scope   : AjaxObject
1436 };
1437 var callbackQuickCreateSave = {
1438         success : AjaxObject.detailView.saveQuickCreateForm,
1439         failure : AjaxObject.handleFailure,
1440         timeout : AjaxObject.timeout,
1441         scope   : AjaxObject
1442 };
1443 var callbackQuickCreateSaveAndAddToAddressBook = {
1444         success : AjaxObject.detailView.saveQuickCreateFormAndAddToAddressBook,
1445         failure : AjaxObject.handleFailure,
1446         timeout : AjaxObject.timeout,
1447         scope   : AjaxObject
1448 };
1449 var callbackQuickCreateSaveAndReply = {
1450     success : AjaxObject.detailView.saveQuickCreateFormAndReply,
1451     failure : AjaxObject.handleFailure,
1452     timeout : AjaxObject.timeout,
1453     scope   : AjaxObject
1454 }
1455 var callbackQuickCreateSaveAndReplyCase = {
1456     success : AjaxObject.detailView.saveQuickCreateFormAndReplyCase,
1457     failure : AjaxObject.handleFailure,
1458     timeout : AjaxObject.timeout,
1459     scope   : AjaxObject
1460 }
1461 var callbackRebuildShowAccountList = {
1462         success : AjaxObject.rebuildShowFolders,
1463         failure : AjaxObject.handleFailure,
1464         timeout : AjaxObject.timeout,
1465         scope   : AjaxObject
1466 };
1467
1468 var callbackRefreshSugarFolders = {
1469         success : function(o) {
1470                 var t = JSON.parse(o.responseText);
1471                 SUGAR.email2.folders.setSugarFoldersEnd(t);
1472         },
1473         failure : AjaxObject.handleFailure,
1474         timeout : AjaxObject.timeout,
1475         scope   : AjaxObject
1476 };
1477 var callbackReplyForward = {
1478         success : AjaxObject.handleReplyForward,
1479         finish : function(a, retryCount) {
1480                 if (typeof(retryCount) == 'undefined') {
1481                         retryCount = 0;
1482                 } else {
1483                         retryCount++;
1484                 }
1485                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
1486                 var t = tinyMCE.getInstanceById('htmleditor' + idx);
1487         try {
1488                         var html = t.getContent();
1489
1490             if (a.type != 'draft') {
1491                         if(SUGAR.email2.userPrefs.signatures.signature_prepend == 'true') {
1492                                 html += "&nbsp;<div><hr></div>" + a.description;
1493                         } else {
1494                                 html =  "&nbsp;<div><hr></div>" + a.description + html;
1495                         }
1496             }else {
1497                 html = a.description;
1498             }
1499
1500                         t.setContent(html);//
1501
1502                 } catch(e) {
1503                         if (retryCount < 5) {
1504                                 setTimeout("callbackReplyForward.finish(globalA, " + retryCount + ");", 500);
1505                                 return;
1506                         }
1507                 }
1508                 var tabArray = SUGAR.email2.innerLayout.get("tabs");
1509                 if (tabArray != null && tabArray.length > 0) {
1510                         for (i = 0 ; i < tabArray.length ; i++) {
1511                                 var tabObject = tabArray[i];
1512                                 if (tabObject.get("id") == ("composeTab" + idx)) {
1513                                         var tabLabel = a.name;
1514                                 if (tabLabel != null && tabLabel.length > 25) {
1515                                         tabLabel = tabLabel.substring(0, 25) + "...";
1516                                 } // if
1517                                         tabObject.get("labelEl").firstChild.data = tabLabel;
1518                                         break;
1519                                 }
1520                         }
1521                 }
1522
1523                 //SUGAR.email2.innerLayout.regions.center.getPanel('composeLayout' + idx).setTitle(a.name);
1524                 if (a.parent_name != null && a.parent_name != "") {
1525                         document.getElementById('data_parent_name' + idx).value = a.parent_name;
1526                 }
1527                 if (a.parent_type != null && a.parent_type != "") {
1528                         document.getElementById('data_parent_type' + idx).value = a.parent_type;
1529                 }
1530                 if (a.parent_id != null && a.parent_id != "") {
1531                         document.getElementById('data_parent_id' + idx).value = a.parent_id;
1532                 }
1533                 if (a.fromAccounts.status) {
1534                         var addressFrom = document.getElementById('addressFrom' + idx);
1535                 SUGAR.email2.util.emptySelectOptions(addressFrom);
1536                 var fromAccountOpts = a.fromAccounts.data;
1537                 for(i=0; i<fromAccountOpts.length; i++) {
1538                       var key = fromAccountOpts[i].value;
1539                       var display = fromAccountOpts[i].text;
1540                       var opt = new Option(display, key);
1541                       if (fromAccountOpts[i].selected) {
1542                         opt.selected = true;
1543                       }
1544                       addressFrom.options.add(opt);
1545                 }
1546                 } // if
1547                 hideOverlay();
1548
1549         },
1550         failure : AjaxObject.handleFailure,
1551         timeout : AjaxObject.timeout,
1552         scope   : AjaxObject,
1553         argument        : [sendType]
1554 };
1555 var callbackSendEmail = {
1556         success : AjaxObject.sendEmailCleanUp,
1557         failure : AjaxObject.handleFailure,
1558         timeout : AjaxObject.timeout,
1559         scope   : AjaxObject
1560 };
1561 var callbackSettings = {
1562         success : AjaxObject.updateUserPrefs,
1563         failure : AjaxObject.handleFailure,
1564         timeout : AjaxObject.timeout,
1565         scope   : AjaxObject
1566 };
1567 var callbackSettingsFolderRefresh = {
1568         success : AjaxObject.settingsFolderRefresh,
1569         failure : AjaxObject.handleFailure,
1570         timeout : AjaxObject.timeout,
1571         scope   : AjaxObject
1572 };
1573 var callbackLoadSettingFolder = {
1574         success : function(o) {
1575                 AjaxObject.settingsFolderRefresh(o);
1576                 SUGAR.email2.accounts.rebuildFolderList(); // refresh frameFolder
1577         },
1578         failure : AjaxObject.handleFailure,
1579         timeout : AjaxObject.timeout,
1580         scope   : AjaxObject
1581
1582 };
1583 var callbackUploadAttachment = {
1584         success : AjaxObject.uploadAttachmentSuccessful,
1585         upload  : AjaxObject.uploadAttachmentSuccessful,
1586         failure : AjaxObject.handleFailure,
1587         timeout : AjaxObject.timeout,
1588         scope   : AjaxObject
1589 };
1590 var callbackUserPrefs = {
1591         success : function(o) {
1592                 SUGAR.email2.userPrefs = JSON.parse(o.responseText);
1593         },
1594         failure : AjaxObject.handleFailure,
1595         timeout : AjaxObject.timeout,
1596         scope   : AjaxObject
1597 };
1598
1599 var callbackContextmenus = {
1600         markUnread : {
1601                 success : AjaxObject.markEmailCleanup,
1602                 failure : AjaxObject.handleFailure,
1603                 timeout : AjaxObject.timeout,
1604                 scope   : AjaxObject
1605         }
1606 };
1607
1608 var callbackCheckEmail2 = {
1609         success : function(o) {
1610                 var ret = JSON.parse(o.responseText);
1611                 overlay(app_strings.LBL_EMAIL_CHECKING_NEW, ret.text);
1612
1613
1614         },
1615         failure : AjaxObject.handleFailure,
1616         timeout : AjaxObject.timeout,
1617         scope   : AjaxObject
1618 }