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