]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/ajax.js
Release 6.2.0
[Github/sugarcrm.git] / modules / Emails / javascript / ajax.js
1 /*********************************************************************************
2  * SugarCRM Community Edition 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_smtpauth_req").checked = (ret.mail_smtpauth_req == 1) ? true : false;
460                         SUGAR.email2.accounts.smtp_authenticate_field_display();
461                         document.getElementById("mail_smtpssl").options[ret.mail_smtpssl].selected = true;
462
463             if(ret.type == 'system-override') {
464                              SUGAR.email2.accounts.toggleOutboundAccountDisabledFields(true);
465                              SUGAR.email2.accounts.changeEmailScreenDisplay(ret.mail_smtptype,true);
466             }
467             else {
468                              SUGAR.email2.accounts.toggleOutboundAccountDisabledFields(false);
469                              SUGAR.email2.accounts.changeEmailScreenDisplay(ret.mail_smtptype,false);
470             }
471             SUGAR.util.setEmailPasswordDisplay('mail_smtppass', ret.has_password);
472
473                 },
474                 failure : AjaxObject.handleFailure,
475                 timeout : AjaxObject.timeout,
476                 scope   : AjaxObject
477         },
478         callbackDeleteOutbound : {
479                 success : function(o) {
480                     var ret = JSON.parse(o.responseText);
481                     if(ret.is_error)
482                     {
483                         if(confirm(ret.error_message))
484                 {
485                     overlay(app_strings.LBL_EMAIL_IE_DELETE, app_strings.LBL_EMAIL_ONE_MOMENT);
486                     AjaxObject.startRequest(AjaxObject.accounts.callbackDeleteOutbound, urlStandard + "&emailUIAction=deleteOutbound&confirm=true&outbound_email=" + ret.outbound_email);
487                 }
488                 else
489                     hideOverlay();
490                     }
491                     else
492                     {
493                              hideOverlay();
494                              SUGAR.email2.accounts.refreshOuboundAccountTable();
495                     }
496                 },
497
498                 failure : AjaxObject.handleFailure,
499                 timeout : AjaxObject.timeout,
500                 scope   : AjaxObject
501         },
502
503         callbackCheckMailProgress : {
504            success : function(o) {
505                if (typeof(SUGAR.email2.accounts.totalMsgCount) == "undefined") {
506                    SUGAR.email2.accounts.totalMsgCount = -1;
507                }
508
509                //Check for server timeout / errors
510                var ret = JSON.parse(o.responseText);
511                var done = false;
512
513                if (typeof(o.responseText) == 'undefined' || o.responseText == "" || ret == false) {
514                    hideOverlay();
515                    overlay(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_TIMEOUT, 'alert');
516                    SUGAR.email2.accounts.totalMsgCount = -1;
517                //SUGAR.email2.folders.rebuildFolders();
518                done = true;
519                }
520
521                var currIeId = ret['ieid'];
522
523
524                var serverCount = ret.count;
525
526                if (ret['status'] == 'done') {
527                    for(i=0; i < SUGAR.email2.accounts.ieIds.length; i++) {
528                        if (i == SUGAR.email2.accounts.ieIds.length - 1) {
529                            //We are all done
530                            done = true;
531                            break;
532                        } else if (SUGAR.email2.accounts.ieIds[i] == currIeId) {
533                            //Go to next account
534                            currIeId = SUGAR.email2.accounts.ieIds[i+1];
535                            ret.count = 0;
536                            SUGAR.email2.accounts.totalMsgCount = -1;
537                            break;
538                        }
539                    }
540                }
541                else if (ret.mbox && ret.totalcount && ret.count) {
542                    SUGAR.email2.accounts.totalMsgCount = ret.totalcount;
543                    if (ret.count >= ret.totalcount) {
544                        serverCount = 0;
545                    }
546                } else if (SUGAR.email2.accounts.totalMsgCount < 0 && ret.totalcount) {
547                    SUGAR.email2.accounts.totalMsgCount = ret.totalcount;
548                } else {
549                        hideOverlay();
550                overlay(app_strings.LBL_EMAIL_ERROR_DESC, app_strings.LBL_EMAIL_ERROR_TIMEOUT, 'alert');
551                SUGAR.email2.accounts.totalMsgCount = -1;
552                done = true;
553                    }
554
555                if (done) {
556                    SUGAR.email2.accounts.totalMsgCount = -1;
557                    hideOverlay();
558                    SUGAR.email2.folders.rebuildFolders();
559                    SE.listView.refreshGrid();
560                } else if (SUGAR.email2.accounts.totalMsgCount < 0) {
561                YAHOO.SUGAR.MessageBox.updateProgress(0, mod_strings.LBL_CHECKING_ACCOUNT + ' '+ (i + 2) + ' '+ mod_strings.LBL_OF + ' ' + SUGAR.email2.accounts.ieIds.length);
562                AjaxObject.startRequest(AjaxObject.accounts.callbackCheckMailProgress, urlStandard +
563                                 '&emailUIAction=checkEmailProgress&ieId=' + currIeId + "&currentCount=0&synch=" + ret.synch);
564            } else {
565                YAHOO.SUGAR.MessageBox.updateProgress((ret.count / SUGAR.email2.accounts.totalMsgCount) * 100,
566                    app_strings.LBL_EMAIL_DOWNLOAD_STATUS.replace(/\[\[count\]\]/, ret.count).replace(/\[\[total\]\]/, SUGAR.email2.accounts.totalMsgCount));
567                    AjaxObject.startRequest(AjaxObject.accounts.callbackCheckMailProgress, urlStandard +
568                    '&emailUIAction=checkEmailProgress&ieId=' + currIeId + "&currentCount=" + serverCount +
569                    '&mbox=' + ret.mbox + '&synch=' + ret.synch + '&totalcount=' + SUGAR.email2.accounts.totalMsgCount);
570                }
571            },
572            failure : AjaxObject.handleFailure,
573        timeout : AjaxObject.timeout,
574        scope   : AjaxObject
575         }
576 };
577
578 ///////////////////////////////////////////////////////////////////////////////
579 ////    COMPOSE LAYOUT
580 AjaxObject.composeLayout = {
581         /**
582          * Populates the record id
583          */
584         saveDraftCleanup : function(o) {
585                 hideOverlay();
586                 var ret = JSON.parse(o.responseText);
587                 if(ret)
588                   SUGAR.email2.composeLayout.forceCloseCompose(ret.composeLayoutId);
589                 else if (o.responseText)
590                   overlay(mod_strings.LBL_ERROR_SAVING_DRAFT, o.responseText, 'alert');
591         }
592 };
593
594 AjaxObject.composeLayout.callback = {
595         saveDraft : {
596                 success : AjaxObject.composeLayout.saveDraftCleanup,
597                 failure : AjaxObject.handleFailure,
598                 timeout : AjaxObject.timeout,
599                 scope   : AjaxObject
600         }
601 };
602
603 AjaxObject.detailView = {
604         /**
605          * Pops-up a printable view of an email
606          */
607         displayPrintable : function(o) {
608                 var ret = JSON.parse(o.responseText);
609                 var displayTemplate = new YAHOO.SUGAR.Template(SUGAR.email2.templates['viewPrintable']);
610                 // 2 below must be in global context
611                 meta = ret.meta;
612                 meta['panelId'] = SUGAR.email2.util.getPanelId();
613                 email = ret.meta.email;
614                 if (typeof(email.cc) == 'undefined') {
615                   email.cc = "";
616                 }
617
618                 var out = displayTemplate.exec({
619                         'app_strings'   : app_strings,
620                         'theme'                 : theme,
621                         'idx'                   : 'Preview',
622                         'meta'                  : meta,
623                         'email'                 : meta.email
624                 });
625
626                 // open popup window
627                 var popup = window.open('modules/Emails/templates/_blank.html', 'printwin' ,
628                     'scrollbars=yes,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
629
630                 popup.document.write(out);
631                 popup.document.close();
632         },
633
634         /**
635          * Takes formatted response and creates a modal pop-over containing a title and content
636          */
637         displayView : function(o) {
638                 var SED = SUGAR.email2.detailView;
639                 var ret = JSON.parse(o.responseText);
640
641                 if(!SED.viewDialog) {
642                         SED.viewDialog = new YAHOO.widget.Dialog("viewDialog", {
643                                 modal:true,
644                 visible:true,
645                 fixedcenter:true,
646                 constraintoviewport: true,
647                                 shadow  : true
648                         });
649             SED.viewDialog.renderEvent.subscribe(function() {
650                 var content = this.body.firstChild;
651                 var viewH = YAHOO.util.Dom.getViewportHeight();
652                 if (content) {
653                         this.body.style.overflow = "auto";
654                         this.body.style.width = "800px";
655                         this.body.style.height = (viewH - 75 > content.clientHeight ? (content.clientHeight) : (viewH - 75)) + "px";
656                 }
657             }, SED.viewDialog);
658                 } // end lazy load
659                 SED.viewDialog.setHeader(ret.title);
660                 SED.viewDialog.setBody(ret.html);
661                 SED.viewDialog.render();
662                 SED.viewDialog.show();
663         },
664
665         /**
666          * Generates a modal popup to populate with the contents of bean's full EditView
667          */
668         showQuickCreateForm : function(o) {
669                 var SED = SUGAR.email2.detailView;
670                 var ret = JSON.parse(o.responseText);
671
672                 if(!SED.quickCreateDialog) {
673                         SED.quickCreateDialog = new YAHOO.widget.Dialog("quickCreate", {
674                                 modal:true,
675                                 visible:true,
676                 fixedcenter:true,
677                 constraintoviewport: true,
678                                 shadow  : true
679                         });
680
681             SED.quickCreateDialog.renderEvent.subscribe(function() {
682                 var viewH = YAHOO.util.Dom.getViewportHeight();
683                 var contH = 0;
684                 for (var i in this.body.childNodes) {
685                         if (this.body.childNodes[i].clientHeight) {
686                                 contH += this.body.childNodes[i].clientHeight;
687                         } else if (this.body.childNodes[i].offsetHeight) {
688                                 contH += this.body.childNodes[i].offsetHeight;
689                         } // if
690                 }
691                         this.body.style.width = "800px";
692                         this.body.style.height = (viewH - 75 > contH ? (contH + 10) : (viewH - 75)) + "px";
693                         this.body.style.overflow = "auto";
694             }, SED.quickCreateDialog);
695
696             SED.quickCreateDialog.hideEvent.subscribe(function(){
697                                 var qsFields = YAHOO.util.Dom.getElementsByClassName('.sqsEnabled', null, this.body);
698                                 /*for(var qsField in qsFields){
699                                         if (typeof QSFieldsArray[qsFields[qsField].id] != 'undefined')
700                                         Ext.getCmp('combobox_'+qsFields[qsField].id).destroy();
701                                 }*/
702                         });
703             SED.quickCreateDialog.setHeader(app_strings.LBL_EMAIL_QUICK_CREATE);
704                 } // end lazy load
705                 if (ret.html) {
706                         ret.html = ret.html.replace('<script type="text/javascript" src="include/SugarEmailAddress/SugarEmailAddress.js"></script>', "");
707                 }
708                 SED.quickCreateDialog.setBody(ret.html ? ret.html : "&nbsp;");
709                 SED.quickCreateDialog.render();
710                 SUGAR.util.evalScript(ret.html + '<script language="javascript">enableQS(true);</script>');
711
712                 SED.quickCreateDialog.ieId = ret.ieId;
713                 SED.quickCreateDialog.uid = ret.uid;
714         SED.quickCreateDialog.mbox = ret.mbox;
715         SED.quickCreateDialog.qcmodule = ret.module;
716
717         SED.quickCreateDialog.show();
718
719                 var editForm = document.getElementById('form_EmailQCView_' + ret.module);
720                 if (editForm) {
721                   editForm.module.value = 'Emails';
722                   var count = SUGAR.EmailAddressWidget.count[ret.module] ? SUGAR.EmailAddressWidget.count[ret.module] : 0;
723                   var tableId = YAHOO.util.Dom.getElementsByClassName('emailaddresses', 'table', editForm)[0].id;
724                   var instId = ret.module + count;
725                   SED.quickCreateEmailsToAdd = ret.emailAddress;
726                   SED.quickCreateEmailCallback = function(instId, tableId) {
727                           var eaw = SUGAR.EmailAddressWidget.instances[instId];
728                           if (typeof(eaw) == "undefined")
729                                   window.setTimeout("SUGAR.email2.detailView.quickCreateEmailCallback('"
730                                                 + instId + "','" + tableId + "');", 100);
731                           eaw.prefillEmailAddresses(tableId, SUGAR.email2.detailView.quickCreateEmailsToAdd);
732                   }
733                   window.setTimeout("SUGAR.email2.detailView.quickCreateEmailCallback('"
734                                         + instId + "','" + tableId + "');", 100);
735                 }
736         },
737
738         saveQuickCreateForm : function(o) {
739             hideOverlay();
740                 SUGAR.email2.detailView.quickCreateDialog.hide();
741                 validate['EditView'] = [ ];
742         },
743
744         saveQuickCreateFormAndReply : function(o) {
745             hideOverlay();
746             var ret = JSON.parse(o.responseText);
747         SUGAR.email2.detailView.quickCreateDialog.hide();
748         var qcd = SUGAR.email2.detailView.quickCreateDialog;
749         var type = (qcd.qcmodule == 'Cases') ? 'replyCase' : 'reply';
750         if (ret) {
751             var emailID = ret.id;
752             SUGAR.email2.composeLayout.c0_replyForwardEmail(null, ret.id, 'sugar::Emails', type);
753         } else {
754             SUGAR.email2.composeLayout.c0_replyForwardEmail(qcd.ieId, qcd.uid, qcd.mbox, type);
755         }
756         //Cean the validate cache to prevent errors on the next call
757         validate['EditView'] = [ ];
758     },
759
760         saveQuickCreateFormAndAddToAddressBook : function(o) {
761            hideOverlay();
762                 SUGAR.email2.detailView.quickCreateDialog.hide();
763                 SUGAR.email2.complexLayout.findPanel('contactsTab').show();
764                 validate['EditView'] = [ ];
765         },
766
767         handleAssignmentDialogAssignAction : function() {
768
769
770             var assign_user_id = window.document.forms['Distribute'].elements['assigned_user_id'].value;
771
772             var dist = 'direct';
773             var users = false;
774             var rules = false;
775             var get = "";
776             var found_teams = false;
777             var warning_message = mod_strings.LBL_WARN_NO_USERS;
778             if(!found_teams && assign_user_id == '' )
779             {
780                 alert(warning_message);
781                 return;
782             }
783
784             var emailUids = SUGAR.email2.listView.getUidsFromSelection();
785             var uids = "";
786             for(i=0; i<emailUids.length; i++) {
787                 if(uids != '') {
788                     uids += app_strings.LBL_EMAIL_DELIMITER;
789                 }
790                 uids += emailUids[i];
791             }
792
793             var row = SUGAR.email2.grid.getSelectedRows()[0];
794             var data = SUGAR.email2.grid.getRecord(row).getData();
795             var ieid = data.ieId;
796             var mbox = data.mbox;
797             AjaxObject.startRequest(callbackAssignmentAction, urlStandard + '&emailUIAction=' + "doAssignmentAssign&uids=" + uids + "&ieId=" + ieid + "&folder=" + mbox + "&distribute_method=" + dist + "&users=" +assign_user_id + get);
798             SUGAR.email2.contextMenus.assignToDialogue.hide();
799             overlay('Assignment', app_strings.LBL_EMAIL_ONE_MOMENT);
800
801         },
802
803         handleAssignmentDialogDeleteAction : function() {
804                 // TO pass list of UIDS/emailIds
805                 var uids = SUGAR.email2.listView.getUidsFromSelection();
806                 var row = SUGAR.email2.grid.getSelections()[0];
807                 var ieid = row.data.ieId;
808             var mbox = row.data.mbox;
809         AjaxObject.startRequest(callbackAssignmentAction, urlStandard + '&emailUIAction=' + "doAssignmentDelete&uids=" + uids + "&ieId=" + ieId + "&folder=" + mbox);
810         SUGAR.email2.contextMenus.assignmentDialog.hide();
811                 overlay(app_strings.LBL_EMAIL_PERFORMING_TASK, app_strings.LBL_EMAIL_ONE_MOMENT);
812
813                 // AJAX Call
814
815         },
816
817         showEmailDetailView : function(o) {
818         hideOverlay();
819         var SED = SUGAR.email2.detailView;
820                 var ret = JSON.parse(o.responseText);
821
822                 if(!SED.quickCreateDialog) {
823                         SED.quickCreateDialog = new YAHOO.widget.Dialog("emailDetailDialog", {
824                                 modal:true,
825                                 visible:true,
826                 //fixedcenter:true,
827                 constraintoviewport: true,
828                 draggable: true,
829                                 autofillheight: "body",
830                                 shadow  : true
831                         });
832                         SED.quickCreateDialog.renderEvent.subscribe(function() {
833                 var viewHeight = YAHOO.util.Dom.getViewportHeight();
834                 var contH = 0;
835                 for (var i in this.body.childNodes) {
836                         if (this.body.childNodes[i].offsetHeight)
837                                 contH += this.body.childNodes[i].offsetHeight;
838                 }
839                         this.body.style.overflow = "auto";
840                         this.body.style.width = "800px";
841                         this.body.style.height = (viewHeight - 75 > contH ? (contH + 10) : (viewHeight - 75)) + "px";
842                         this.center();
843             }, SED.quickCreateDialog);
844                 }
845                 SED.quickCreateDialog.setHeader(app_strings.LBL_EMAIL_RECORD);
846                 SED.quickCreateDialog.setBody(ret.html);
847                 SED.quickCreateDialog.render();
848         SUGAR.util.evalScript(ret.html);
849         SED.quickCreateDialog.show();
850         },
851
852         showAssignmentDialogWithData : function(o) {
853         var SEC = SUGAR.email2.contextMenus;
854                 hideOverlay();
855         var ret = JSON.parse(o.responseText);
856         if (!SEC.assignmentDialog) {
857                 SEC.assignmentDialog = new YAHOO.widget.Dialog("assignmentDialog", {
858                         visible:false,
859                 fixedcenter:true,
860                 constraintoviewport: true,
861                         modal   : true
862                 });
863                 SEC.assignmentDialog.setBody("");
864                 SEC.assignmentDialog.setHeader(app_strings.LBL_EMAIL_ASSIGNMENT);
865                 SEC.assignmentDialog.renderEvent.subscribe(function() {
866                 var iev = YAHOO.util.Dom.get("Distribute");
867                 if (iev) {
868                         this.body.style.width = "700px";
869                 }
870             },  SEC.assignmentDialog);
871                 SEC.assignmentDialog.render();
872         }
873         SEC.assignmentDialog.setBody(ret);
874         SEC.assignmentDialog.render();
875         validate = [];
876         SEC.assignmentDialog.show();
877         SUGAR.util.evalScript(ret);
878         },
879
880         showImportForm : function(o) {
881                 var SED = SUGAR.email2.detailView;
882                 var ret = JSON.parse(o.responseText);
883
884         document.getElementById('quickCreateContent').innerHTML = "";
885         hideOverlay();
886         if (!ret) {
887             return false;
888         }
889
890         if(!SED.importDialog) {
891             SED.importDialog = new YAHOO.widget.Dialog("importDialog", {
892                 modal:true,
893                 visible:false,
894                 fixedcenter:true,
895                 constraintoviewport: true,
896                 buttons : [{
897                         text: app_strings.LBL_EMAIL_ARCHIVE_TO_SUGAR, isDefault: true, handler: function(){
898                                 AjaxObject.detailView.getImportAction(SED.importDialog.ret); }
899                 }]//,
900                 //scroll : true
901             });
902             SED.importDialog.setHeader(app_strings.LBL_EMAIL_IMPORT_SETTINGS);
903             SED.importDialog.setBody("");
904             SED.importDialog.hideEvent.subscribe(function(){
905                 for(var i in QSFieldsArray) {
906                         if (QSFieldsArray[i] != null && typeof(QSFieldsArray[i]) == "object") {
907                                 QSFieldsArray[i].destroy();
908                                 delete QSFieldsArray[i];
909                         }
910                         if (QSProcessedFieldsArray[i]) {
911                                 QSProcessedFieldsArray[i] = false;
912                         } // if
913                                 }
914             });
915             SED.importDialog.renderEvent.subscribe(function() {
916                 var iev = YAHOO.util.Dom.get("ImportEditView");
917                 if (iev) {
918                         //this.body.style.height = (iev.clientHeight + 10) + "px";
919                         this.body.style.width = "600px";
920                 }
921             }, SED.importDialog);
922             SED.importDialog.render();
923         } // end lazy load
924         SED.importDialog.setBody(ret.html);
925         SED.importDialog.ret = ret;
926         SUGAR.util.evalScript(ret.html);
927         SED.importDialog.render();
928         validate = [];
929         SED.importDialog.show();
930         SED.importDialog.focusFirstButton();
931     },
932     getImportAction : function(ret) {
933         if (!check_form('ImportEditView')) return false;
934         if (!SUGAR.collection.prototype.validateTemSet('ImportEditView', 'team_name')) {
935                 alert(mod_strings.LBL_EMAILS_NO_PRIMARY_TEAM_SPECIFIED);
936                 return false;
937         } // if
938                 var get = "";
939         var editView = document.getElementById('ImportEditView');
940         if (editView.assigned_user_id != null) {
941             get = get + "&user_id=" + editView.assigned_user_id.value
942             //var user_id = editView.assigned_user_id.value;
943         }
944         var parent_id = editView.parent_id.value;
945         var parent_type = editView.parent_type.value;
946         var row = SUGAR.email2.grid.getSelectedRows()[0];
947         row = SUGAR.email2.grid.getRecord(row);
948         var data = row.getData();
949         var ieId = data.ieId;
950         var mbox = data.mbox;
951         var serverDelete = editView.serverDelete.checked;
952         var emailUids = SUGAR.email2.listView.getUidsFromSelection();
953         var uids = "";
954         for(i=0; i<emailUids.length; i++) {
955             if(uids != '') {
956                 uids += app_strings.LBL_EMAIL_DELIMITER;
957             }
958             uids += emailUids[i];
959         }
960
961         var action = 'importEmail&uid=';
962         if (ret.move) {
963             action = 'moveEmails';
964             action = action + '&sourceFolder=' + ret['srcFolder'];
965             action = action + '&sourceIeId=' + ret['srcIeId'];
966             action = action + '&destinationFolder=' + ret['dstFolder'];
967             action = action + '&destinationIeId=' + ret['dstIeId'];
968             action = action + '&emailUids=';
969         }
970         if (action.search(/importEmail/) != -1) {
971             overlay(app_strings.LBL_EMAIL_IMPORTING_EMAIL, app_strings.LBL_EMAIL_ONE_MOMENT);
972         } else {
973             overlay("Moving Email(s)", app_strings.LBL_EMAIL_ONE_MOMENT);
974         }
975
976         AjaxObject.startRequest(callbackStatusForImport, urlStandard + '&emailUIAction=' + action + uids + "&ieId=" + ieId + "&mbox=" + mbox +
977         get + "&parent_id=" + parent_id + "&parent_type=" + parent_type + '&delete=' + serverDelete);
978         SUGAR.email2.detailView.importDialog.hide();
979         document.getElementById('importDialogContent').innerHTML = "";
980
981     },
982     showRelateForm : function(o) {
983         var SED = SUGAR.email2.detailView;
984         var ret = JSON.parse(o.responseText);
985         document.getElementById('quickCreateContent').innerHTML = "";
986         hideOverlay();
987         if (!ret) {
988             return false;
989         }
990         dialog_loaded = true;
991
992         if(!SED.relateDialog) {
993             SED.relateDialog = new YAHOO.widget.Dialog('relateDialog', {
994                                 modal:true,
995                                 visible:true,
996                 fixedcenter:true,
997                 width: '800px',
998                 constraintoviewport: true,
999                                 buttons : [{
1000                         text: app_strings.LBL_EMAIL_RELATE_TO, isDefault: true, handler: function(){
1001                                         if (!check_form('RelateEditView')) return false;
1002                                         var get = "";
1003                         var editView = document.getElementById('RelateEditView');
1004                         var parent_id = editView.parent_id.value;
1005                         var parent_type = editView.parent_type.value;
1006                         var row = SUGAR.email2.grid.getSelectedRows()[0];
1007                         row  = SUGAR.email2.grid.getRecord(row);
1008                         var ieId = row.getData().ieId;
1009                         var mbox = row.getData().mbox;
1010                         var emailUids = SUGAR.email2.listView.getUidsFromSelection();
1011                         var uids = "";
1012                         for(i=0; i<emailUids.length; i++) {
1013                             if(uids != '') {
1014                                 uids += app_strings.LBL_EMAIL_DELIMITER;
1015                             }
1016                             uids += emailUids[i];
1017                         }
1018                         overlay(app_strings.LBL_EMAIL_PERFORMING_TASK, app_strings.LBL_EMAIL_ONE_MOMENT);
1019                         AjaxObject.startRequest(callbackStatusForImport, urlStandard + '&emailUIAction=relateEmails&uid=' + uids
1020                             + "&ieId=" + ieId + "&mbox=" + mbox + "&parent_id=" + parent_id + "&parent_type=" + parent_type);
1021                         SED.relateDialog.hide();
1022                         document.getElementById('relateDialogContent').innerHTML = "";
1023                         }
1024                 }]
1025                         });
1026
1027             SED.relateDialog.hideEvent.subscribe(function(){
1028                 if (QSFieldsArray['ImportEditView_parent_name'] != null) {
1029                         QSFieldsArray['ImportEditView_parent_name'].destroy();
1030                         delete QSFieldsArray['ImportEditView_parent_name'];
1031                 } // if
1032                                 if (QSProcessedFieldsArray['ImportEditView_parent_name']) {
1033                                         QSProcessedFieldsArray['ImportEditView_parent_name'] = false;
1034                                 } // if
1035             });
1036
1037             SED.relateDialog.renderEvent.subscribe(function() {
1038                 var viewPortHeight = YAHOO.util.Dom.getViewportHeight();
1039                 var contH = 0;
1040                 for (var i in this.body.childNodes) {
1041                         if (this.body.childNodes[i].clientHeight)
1042                                 contH += this.body.childNodes[i].clientHeight;
1043                 }
1044             }, SED.relateDialog);
1045             SED.relateDialog.setHeader(app_strings.LBL_EMAIL_RELATE_EMAIL);
1046                 } // end lazy load
1047
1048         SED.relateDialog.setBody(ret.html);
1049         SED.relateDialog.render();
1050         SUGAR.util.evalScript(ret.html);
1051         SED.relateDialog.show();
1052     }
1053 };
1054 /**
1055  * DetailView callbacks
1056  */
1057 AjaxObject.detailView.callback = {
1058         emailDetail : {
1059                 success : function(o) {
1060                         SUGAR.email2.o = o;
1061                         var ret = JSON.parse(o.responseText);
1062                         SUGAR.email2.detailView.consumeMetaDetail(ret);
1063                 },
1064                 argument: [targetDiv],
1065                 failure : AjaxObject.handleFailure,
1066                 timeout : 0,
1067                 scope   : AjaxObject
1068         },
1069         emailPreview : {
1070                 success : function(o) {
1071                         SUGAR.email2.o = o;
1072                         var ret = JSON.parse(o.responseText);
1073                         SUGAR.email2.detailView.consumeMetaPreview(ret);
1074                 },
1075                 failure : AjaxObject.handleFailure,
1076                 timeout : 0,
1077                 scope   : AjaxObject
1078         },
1079         viewPrint : {
1080                 success : AjaxObject.detailView.displayPrintable,
1081                 failure : AjaxObject.handleFailure,
1082                 timeout : AjaxObject.timeout,
1083                 scope   : AjaxObject
1084         },
1085         viewRaw : {
1086                 success : AjaxObject.detailView.displayView,
1087                 failure : AjaxObject.handleFailure,
1088                 timeout : AjaxObject.timeout,
1089                 scope   : AjaxObject
1090         }
1091 };
1092
1093
1094
1095
1096
1097 AjaxObject.folders = {
1098         /**
1099          * check-mail post actions
1100          */
1101         checkMailCleanup : function(o) {
1102                 hideOverlay();
1103                 AjaxObject.folders.rebuildFolders(o); // rebuild TreeView
1104
1105                 // refresh focus ListView
1106                 SE.listView.refreshGrid();
1107                 SUGAR.email2.folders.startCheckTimer(); // resets the timer
1108         },
1109
1110         /**
1111          */
1112         rebuildFolders : function(o) {
1113                 hideOverlay();
1114
1115                 var data = JSON.parse(o.responseText);
1116
1117                 email2treeinit(SUGAR.email2.tree, data.tree_data, 'frameFolders', data.param);
1118                 SUGAR.email2.folders.setSugarFolders();
1119         }
1120 };
1121 AjaxObject.folders.callback = {
1122         checkMail : {
1123                 success : AjaxObject.folders.checkMailCleanup,
1124                 failure : AjaxObject.handleFailure,
1125                 timeout : 600000, // 5 mins
1126                 scope   : AjaxObject
1127         }
1128 }
1129
1130 AjaxObject.rules = {
1131         loadRulesForSettings : function(o) {
1132                 document.getElementById("rulesListCell").innerHTML = o.responseText;
1133                 // assume we have the class we need
1134                 SUGAR.routing.getStrings();
1135                 SUGAR.routing.getDependentDropdowns();
1136         }
1137 };
1138 ////    END PER MODULE CALLBACK OBJECTS
1139 ///////////////////////////////////////////////////////////////////////////
1140
1141
1142 var callback = {
1143         success : AjaxObject.handleSuccess,
1144         failure : AjaxObject.handleFailure,
1145         timeout : AjaxObject.timeout,
1146         scope   : AjaxObject
1147 };
1148 var callbackAccount = {
1149         success : AjaxObject.ieSaveSuccess,
1150         failure : AjaxObject.handleFailure,
1151         timeout : AjaxObject.timeout,
1152         scope   : AjaxObject
1153 };
1154 var callbackAccountDelete = {
1155         success : AjaxObject.ieDeleteSuccess,
1156         failure : AjaxObject.handleFailure,
1157         timeout : AjaxObject.timeout,
1158         scope   : AjaxObject
1159 };
1160 var callbackOutboundTest = {
1161         success : AjaxObject.ieSendSuccess,
1162         failure : AjaxObject.handleFailure,
1163         timeout : AjaxObject.timeout,
1164         scope   : AjaxObject
1165 };
1166
1167
1168
1169 var callbackTeamInfoForSettings = {
1170 success : function (o) {
1171         var data = JSON.parse(o.responseText);
1172         document.getElementById('EditViewGroupFolderTeamTD').innerHTML = data.defaultgroupfolder;
1173 },
1174 failure : AjaxObject.handleFailure,
1175 timeout : AjaxObject.timeout,
1176 scope   : AjaxObject
1177
1178 };
1179
1180 var callbackStatusForImport = {
1181 success : function (o) {
1182         hideOverlay();
1183         if (o.responseText != "")  {
1184                 var statusString = "";
1185                 var data = JSON.parse(o.responseText);
1186                 for(i=0; i<data.length; i++) {
1187                         statusString = statusString + data[i] + '<br/>';
1188                 }
1189                 overlay(SUGAR.language.get('Emails','LBL_IMPORT_STATUS_TITLE'), statusString, 'alert');
1190         }
1191         SE.listView.refreshGrid();
1192
1193 },
1194 failure : AjaxObject.handleFailure,
1195 timeout : AjaxObject.timeout,
1196 scope   : AjaxObject
1197
1198 };
1199 var callbackComposeCache = {
1200         success : AjaxObject.composeCache,
1201         failure : AjaxObject.handleFailure,
1202         timeout : AjaxObject.timeout,
1203         scope   : AjaxObject
1204 };
1205 var callbackDelete = {
1206         success : AjaxObject.handleDeleteReturn,
1207         failure : AjaxObject.handleFailure,
1208         timeout : AjaxObject.timeout,
1209         scope   : AjaxObject
1210 };
1211 var callbackEmailDetailMultiple = {
1212         success : function(o) {
1213                 hideOverlay();
1214                 var retMulti = JSON.parse(o.responseText);
1215                 var ret = new Object();
1216
1217                 for(var i=0; i<retMulti.length; i++) {
1218                         ret = retMulti[i];
1219
1220                         SUGAR.email2._setDetailCache(ret);
1221                         SUGAR.email2.detailView.populateDetailView(ret.meta.uid, ret.meta.mbox, ret.meta.ieId, true, SUGAR.email2.innerLayout);
1222                 }
1223         },
1224         failure : AjaxObject.handleFailure,
1225         timeout : 0,
1226         scope   : AjaxObject
1227 };
1228 var callbackListViewSortOrderChange = {
1229         success : AjaxObject.saveListViewSortOrderPart2,
1230         failure : AjaxObject.handleFailure,
1231         timeout : AjaxObject.timeout,
1232         scope   : AjaxObject,
1233         argument        : [ieId, ieName, focusFolder]
1234 };
1235 var callbackEmptyTrash = {
1236         success : function(o) {
1237                 hideOverlay();
1238                 AjaxObject.folderRenameCleanup;
1239         },
1240         failure : AjaxObject.handleFailure,
1241         timeout : AjaxObject.timeout,
1242         scope   : AjaxObject
1243 };
1244 var callbackClearCacheFiles = {
1245         success : function(o) {
1246                 hideOverlay();
1247         },
1248         failure : AjaxObject.handleFailure,
1249         timeout : AjaxObject.timeout,
1250         scope   : AjaxObject
1251 };
1252 var callbackFolderRename = {
1253         success : function(o) {hideOverlay();SUGAR.email2.folders.rebuildFolders();},
1254         failure : AjaxObject.handleFailure,
1255         timeout : AjaxObject.timeout,
1256         scope   : AjaxObject
1257 };
1258 var callbackFolderDelete = {
1259         success : function(o) {
1260                 var ret = JSON.parse(o.responseText);
1261                 if (ret.status) {
1262                     if (ret.folder_id) {
1263                         var node = SUGAR.email2.folders.getNodeFromId(ret.folder_id);
1264                         if(node)
1265                                 SUGAR.email2.tree.removeNode(node, true);
1266                     } else if (ret.ieId && ret.mbox) {
1267                         var node = SUGAR.email2.folders.getNodeFromIeIdAndMailbox(ret.ieId, ret.mbox);
1268                         if(node)
1269                                 SUGAR.email2.tree.removeNode(node, true);
1270                     }
1271                         hideOverlay();
1272                         //SUGAR.email2.folders.loadSettingFolder();
1273                 } else {
1274                         hideOverlay();
1275                         overlay(app_strings.LBL_EMAIL_ERROR_DESC, ret.errorMessage, 'alert');
1276                 } // else
1277         },
1278         failure : AjaxObject.handleFailure,
1279         timeout : AjaxObject.timeout,
1280         scope   : AjaxObject
1281 };
1282 var callbackFolderSave = {
1283         success : function(o) {
1284                 var ret = JSON.parse(o.responseText);
1285
1286                 switch(ret.action) {
1287                         case 'newFolderSave':
1288                                 SUGAR.email2.folders.rebuildFolders();
1289                         break;
1290                 }
1291         },
1292         failure : AjaxObject.handleFailure,
1293         timeout : AjaxObject.timeout,
1294         scope   : AjaxObject
1295 };
1296 var callbackFolderSubscriptions = {
1297         success : AjaxObject.updateFolderSubscriptions,
1298         failure : AjaxObject.handleFailure,
1299         timeout : AjaxObject.timeout,
1300         scope   : AjaxObject
1301 };
1302 var callbackFolderUpdate = {
1303         success : AjaxObject.updateFrameFolder,
1304         failure : AjaxObject.handleFailure,
1305         timeout : AjaxObject.timeout,
1306         scope   : AjaxObject
1307 };
1308 var callbackFolders = {
1309         success : AjaxObject.folders.rebuildFolders,
1310         //success : void(true),
1311         failure : AjaxObject.handleFailure,
1312         timeout : AjaxObject.timeout,
1313         scope   : AjaxObject
1314 };
1315 var callbackFullSync = {
1316         success : AjaxObject.fullSyncCleanup,
1317         failure : AjaxObject.handleFailure,
1318         timeout : 9999999999999,
1319         scope   : AjaxObject
1320 };
1321 var callbackGeneric = {
1322         success : function() {
1323                 hideOverlay();
1324         },
1325         failure : AjaxObject.handleFailure,
1326         timeout : AjaxObject.timeout,
1327         scope   : AjaxObject
1328 };
1329 var callbackIeAccountRetrieve = {
1330         success : function(o) {
1331                 // return JSON encoding
1332                 hideOverlay();
1333                 SUGAR.email2.accounts.fillIeAccount(o.responseText);
1334                 SUGAR.email2.accounts.showEditInboundAccountDialogue(false);
1335         },
1336         failure : AjaxObject.handleFailure,
1337         timeout : AjaxObject.timeout,
1338         scope   : AjaxObject
1339 };
1340 var callbackImportOneEmail = {
1341         success :  AjaxObject.detailView.showImportForm,
1342         failure : AjaxObject.handleFailure,
1343         timeout : AjaxObject.timeout,
1344         scope   : AjaxObject
1345 };
1346 var callbackRelateEmail = {
1347     success : AjaxObject.detailView.showRelateForm,
1348     failure : AjaxObject.handleFailure,
1349     timeout : AjaxObject.timeout,
1350     scope   : AjaxObject
1351 }
1352 var callbackEmailDetailView = {
1353     success : AjaxObject.detailView.showEmailDetailView,
1354     failure : AjaxObject.handleFailure,
1355     timeout : AjaxObject.timeout,
1356     scope   : AjaxObject
1357 }
1358 var callbackAssignmentDialog = {
1359         success :  AjaxObject.detailView.showAssignmentDialogWithData,
1360         failure : AjaxObject.handleFailure,
1361         timeout : AjaxObject.timeout,
1362         scope   : AjaxObject
1363 };
1364 var callbackAssignmentAction = {
1365         success :  function(o) {
1366                 SE.listView.refreshGrid();
1367                 hideOverlay();
1368                 if(o.responseText != '') {
1369                overlay('Assignment action result', o.responseText, 'alert');
1370             } // if
1371         } ,
1372         failure : AjaxObject.handleFailure,
1373         timeout : AjaxObject.timeout,
1374         scope   : AjaxObject
1375 };
1376 var callbackMoveEmails = {
1377         success :  function(o) {
1378             SE.listView.refreshGrid();
1379                 hideOverlay();
1380                 if(o.responseText != '') {
1381                overlay(app_strings.LBL_EMAIL_ERROR_DESC, o.responseText, 'alert');
1382             } // if
1383         } ,
1384         failure : AjaxObject.handleFailure,
1385         timeout : AjaxObject.timeout,
1386         scope   : AjaxObject
1387 };
1388 var callbackLoadAttachments = {
1389         success : AjaxObject.loadAttachments,
1390         failure : AjaxObject.handleFailure,
1391         timeout : AjaxObject.timeout,
1392         scope   : AjaxObject
1393 };
1394 var callbackLoadRules = {
1395         success : AjaxObject.rules.loadRulesForSettings,
1396         failure : AjaxObject.handleFailure,
1397         timeout : AjaxObject.timeout,
1398         scope   : AjaxObject
1399 };
1400 var callbackLoadSignature = {
1401         success : AjaxObject.loadSignature,
1402         failure : AjaxObject.handleFailure,
1403         timeout : AjaxObject.timeout,
1404         scope   : AjaxObject
1405 };
1406 var callbackDeleteSignature = {
1407         success : AjaxObject.handleDeleteSignature,
1408         failure : AjaxObject.handleFailure,
1409         timeout : AjaxObject.timeout,
1410         scope   : AjaxObject
1411 };
1412 /*var callbackMoveEmails = {
1413     success : function(o) { SUGAR.email2.listView.moveEmailsCleanup(o) },
1414     failure : AjaxObject.handleFailure,
1415     timeout : AjaxObject.timeout,
1416     scope   : AjaxObject
1417 }*/
1418 var callbackOutboundSave = {
1419         success : AjaxObject.accounts.saveOutboundCleanup,
1420         failure : AjaxObject.handleFailure,
1421         timeout : AjaxObject.timeout,
1422         scope   : AjaxObject
1423 };
1424 var callbackDefaultOutboundSave = {
1425         success : AjaxObject.accounts.saveDefaultOutboundCleanup,
1426         failure : AjaxObject.handleFailure,
1427         timeout : AjaxObject.timeout,
1428         scope   : AjaxObject
1429 };
1430 var callbackQuickCreate = {
1431         success : AjaxObject.detailView.showQuickCreateForm,
1432         failure : AjaxObject.handleFailure,
1433         timeout : AjaxObject.timeout,
1434         scope   : AjaxObject
1435 };
1436 var callbackQuickCreateSave = {
1437         success : AjaxObject.detailView.saveQuickCreateForm,
1438         failure : AjaxObject.handleFailure,
1439         timeout : AjaxObject.timeout,
1440         scope   : AjaxObject
1441 };
1442 var callbackQuickCreateSaveAndAddToAddressBook = {
1443         success : AjaxObject.detailView.saveQuickCreateFormAndAddToAddressBook,
1444         failure : AjaxObject.handleFailure,
1445         timeout : AjaxObject.timeout,
1446         scope   : AjaxObject
1447 };
1448 var callbackQuickCreateSaveAndReply = {
1449     success : AjaxObject.detailView.saveQuickCreateFormAndReply,
1450     failure : AjaxObject.handleFailure,
1451     timeout : AjaxObject.timeout,
1452     scope   : AjaxObject
1453 }
1454 var callbackQuickCreateSaveAndReplyCase = {
1455     success : AjaxObject.detailView.saveQuickCreateFormAndReplyCase,
1456     failure : AjaxObject.handleFailure,
1457     timeout : AjaxObject.timeout,
1458     scope   : AjaxObject
1459 }
1460 var callbackRebuildShowAccountList = {
1461         success : AjaxObject.rebuildShowFolders,
1462         failure : AjaxObject.handleFailure,
1463         timeout : AjaxObject.timeout,
1464         scope   : AjaxObject
1465 };
1466
1467 var callbackRefreshSugarFolders = {
1468         success : function(o) {
1469                 var t = JSON.parse(o.responseText);
1470                 SUGAR.email2.folders.setSugarFoldersEnd(t);
1471         },
1472         failure : AjaxObject.handleFailure,
1473         timeout : AjaxObject.timeout,
1474         scope   : AjaxObject
1475 };
1476 var callbackReplyForward = {
1477         success : AjaxObject.handleReplyForward,
1478         finish : function(a, retryCount) {
1479                 if (typeof(retryCount) == 'undefined') {
1480                         retryCount = 0;
1481                 } else {
1482                         retryCount++;
1483                 }
1484                 var idx = SUGAR.email2.composeLayout.currentInstanceId;
1485                 var t = tinyMCE.getInstanceById('htmleditor' + idx);
1486         try {
1487                         var html = t.getContent();
1488
1489             if (a.type != 'draft') {
1490                         if(SUGAR.email2.userPrefs.signatures.signature_prepend == 'true') {
1491                                 html += "&nbsp;<div><hr></div>" + a.description;
1492                         } else {
1493                                 html =  "&nbsp;<div><hr></div>" + a.description + html;
1494                         }
1495             }else {
1496                 html = a.description;
1497             }
1498
1499                         t.setContent(html);//
1500
1501                 } catch(e) {
1502                         if (retryCount < 5) {
1503                                 setTimeout("callbackReplyForward.finish(globalA, " + retryCount + ");", 500);
1504                                 return;
1505                         }
1506                 }
1507                 var tabArray = SUGAR.email2.innerLayout.get("tabs");
1508                 if (tabArray != null && tabArray.length > 0) {
1509                         for (i = 0 ; i < tabArray.length ; i++) {
1510                                 var tabObject = tabArray[i];
1511                                 if (tabObject.get("id") == ("composeTab" + idx)) {
1512                                         var tabLabel = a.name;
1513                                 if (tabLabel != null && tabLabel.length > 25) {
1514                                         tabLabel = tabLabel.substring(0, 25) + "...";
1515                                 } // if
1516                                         tabObject.get("labelEl").firstChild.data = tabLabel;
1517                                         break;
1518                                 }
1519                         }
1520                 }
1521
1522                 //SUGAR.email2.innerLayout.regions.center.getPanel('composeLayout' + idx).setTitle(a.name);
1523                 if (a.parent_name != null && a.parent_name != "") {
1524                         document.getElementById('data_parent_name' + idx).value = a.parent_name;
1525                 }
1526                 if (a.parent_type != null && a.parent_type != "") {
1527                         document.getElementById('data_parent_type' + idx).value = a.parent_type;
1528                 }
1529                 if (a.parent_id != null && a.parent_id != "") {
1530                         document.getElementById('data_parent_id' + idx).value = a.parent_id;
1531                 }
1532                 if (a.fromAccounts.status) {
1533                         var addressFrom = document.getElementById('addressFrom' + idx);
1534                 SUGAR.email2.util.emptySelectOptions(addressFrom);
1535                 var fromAccountOpts = a.fromAccounts.data;
1536                 for(i=0; i<fromAccountOpts.length; i++) {
1537                       var key = fromAccountOpts[i].value;
1538                       var display = fromAccountOpts[i].text;
1539                       var opt = new Option(display, key);
1540                       if (fromAccountOpts[i].selected) {
1541                         opt.selected = true;
1542                       }
1543                       addressFrom.options.add(opt);
1544                 }
1545                 } // if
1546                 hideOverlay();
1547
1548         },
1549         failure : AjaxObject.handleFailure,
1550         timeout : AjaxObject.timeout,
1551         scope   : AjaxObject,
1552         argument        : [sendType]
1553 };
1554 var callbackSendEmail = {
1555         success : AjaxObject.sendEmailCleanUp,
1556         failure : AjaxObject.handleFailure,
1557         timeout : AjaxObject.timeout,
1558         scope   : AjaxObject
1559 };
1560 var callbackSettings = {
1561         success : AjaxObject.updateUserPrefs,
1562         failure : AjaxObject.handleFailure,
1563         timeout : AjaxObject.timeout,
1564         scope   : AjaxObject
1565 };
1566 var callbackSettingsFolderRefresh = {
1567         success : AjaxObject.settingsFolderRefresh,
1568         failure : AjaxObject.handleFailure,
1569         timeout : AjaxObject.timeout,
1570         scope   : AjaxObject
1571 };
1572 var callbackLoadSettingFolder = {
1573         success : function(o) {
1574                 AjaxObject.settingsFolderRefresh(o);
1575                 SUGAR.email2.accounts.rebuildFolderList(); // refresh frameFolder
1576         },
1577         failure : AjaxObject.handleFailure,
1578         timeout : AjaxObject.timeout,
1579         scope   : AjaxObject
1580
1581 };
1582 var callbackUploadAttachment = {
1583         success : AjaxObject.uploadAttachmentSuccessful,
1584         upload  : AjaxObject.uploadAttachmentSuccessful,
1585         failure : AjaxObject.handleFailure,
1586         timeout : AjaxObject.timeout,
1587         scope   : AjaxObject
1588 };
1589 var callbackUserPrefs = {
1590         success : function(o) {
1591                 SUGAR.email2.userPrefs = JSON.parse(o.responseText);
1592         },
1593         failure : AjaxObject.handleFailure,
1594         timeout : AjaxObject.timeout,
1595         scope   : AjaxObject
1596 };
1597
1598 var callbackContextmenus = {
1599         markUnread : {
1600                 success : AjaxObject.markEmailCleanup,
1601                 failure : AjaxObject.handleFailure,
1602                 timeout : AjaxObject.timeout,
1603                 scope   : AjaxObject
1604         }
1605 };
1606
1607 var callbackCheckEmail2 = {
1608         success : function(o) {
1609                 var ret = JSON.parse(o.responseText);
1610                 overlay(app_strings.LBL_EMAIL_CHECKING_NEW, ret.text);
1611
1612
1613         },
1614         failure : AjaxObject.handleFailure,
1615         timeout : AjaxObject.timeout,
1616         scope   : AjaxObject
1617 }