]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Emails/javascript/Email.js
Release 6.4.0
[Github/sugarcrm.git] / modules / Emails / javascript / Email.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
38
39 var uploads_arr=new Array();
40 var uploads_count_map=new Object();
41 var uploads_count = -1;
42 var current_uploads_id = -1;
43 var append = false; // Ed has method InsertHTML which inserts at cursor point - plain does not
44 //following varaibles store references to input fields grouped with the clicked email selection button (select).
45 var current_contact = '';
46 var current_contact_id = '';
47 var current_contact_email = '';
48 var current_contact_name = '';
49 var uploadIndex = 0;
50 var select_image = SUGAR.language.get('app_strings', 'LBL_ONLY_IMAGE_ATTACHMENT');
51 var lbl_remove = SUGAR.language.get('app_strings', 'LBL_REMOVE');
52 var lbl_email_attachments_file = SUGAR.language.get('app_strings', 'LBL_EMAIL_ATTACHMENTS_FILE');
53 var lbl_email_attachments_document = SUGAR.language.get('app_strings', 'LBL_EMAIL_ATTACHMENTS_DOCUMENT');
54 var lbl_email_attachments_embeded = SUGAR.language.get('app_strings', 'LBL_EMAIL_ATTACHMENTS_EMBEDED');
55
56
57 function toggleRawEmail() {
58         var raw = document.getElementById('rawEmail');
59         var button = document.getElementById('rawButton');
60
61         if(raw.style.display == '') {
62                 raw.style.display = 'none';
63                 button.value = showRaw;
64         } else {
65                 raw.style.display = '';
66                 button.value = hideRaw;
67         }
68 }
69
70 ///////////////////////////////////////////////////////////////////////////////
71 ////    DOCUMENT HANDLING HELPERS
72
73 function deletePriorAttachment(id) {
74         var rem = document.getElementById('removeAttachment');
75
76         if(rem.value != '') {
77                 rem.value += "::";
78         }
79         rem.value += id;
80
81         document.getElementById('noteDiv'+id).style.display = 'none';
82 }
83
84 function setDocument(target, documentId, documentName, docRevId) {
85         if(window.opener.SUGAR.email2) {
86                 var idx = window.opener.SUGAR.email2.composeLayout.currentInstanceId;
87                 window.opener.SUGAR.email2.composeLayout.setDocument(idx, target, documentId, documentName, docRevId);
88         } else {
89                 var docId = window.opener.document.getElementById('documentId' + target);
90                 var docName = window.opener.document.getElementById('documentName' + target);
91                 var docRevisionId = window.opener.document.getElementById('document' + target);
92             docId.value = documentId;
93                 docName.value = documentName;
94                 docRevisionId.value = docRevId;
95         }
96         window.close();
97 }
98
99 function setDocumentToCampaignTemplate(target, documentId, documentName,docRevId, documentType) {
100 //      var docId = eval("window.opener.document.forms.EditView.documentId" + target);
101         var docId = window.opener.document.getElementById('documentId' + target);
102         //var docName = eval("window.opener.document.EditView.documentName" + target);
103         var docName = window.opener.document.getElementById('documentName' + target);
104         var docType = window.opener.document.getElementById('documentType' + target);
105
106         var docId = window.opener.document.getElementById('documentId');
107         var docName = window.opener.document.getElementById('documentName');
108         var docRevisionId = window.opener.document.getElementById('docRevId');
109         var docType = window.opener.document.getElementById('documentType');
110
111     docId.value = documentId;
112     //docId.onchange('docUpload(); form_reset_doc();');
113         docName.value = documentName;
114         docRevisionId.value = docRevId;
115         docType.value = documentType;
116         docName.onchange('docUpload(); form_reset_doc();');
117         //alert(docName.onchange);
118         window.close();
119 }
120
121 function selectDocument(target) {
122         URL="index.php?module=Emails&action=PopupDocuments&to_pdf=true&target=" + target;
123         windowName = 'selectDocument';
124         windowFeatures = 'width=800' + ',height=600' + ',resizable=1,scrollbars=1';
125
126         win = SUGAR.util.openWindow(URL, windowName, windowFeatures);
127         if(window.focus) {
128                 // put the focus on the popup if the browser supports the focus() method
129                 win.focus();
130         }
131 }
132
133 function addDocument() {
134         for(var i=0;i<10;i++) {
135                 var elem = document.getElementById('document'+i);
136                 if(elem.style.display == 'none') {
137                         elem.style.display='block';
138                         break;
139                 }
140         }
141 }
142
143 function deleteDocument(index) {
144         var elem = document.getElementById('document'+index);
145         document.getElementById('documentId'+index).value = "";
146         document.getElementById('documentName'+index).value = "";
147         elem.style.display='none';
148 }
149
150 // attachment functions below
151 function deleteFile(index) {
152         //get div element
153         var elem = document.getElementById('file'+index);
154         //get upload widget
155         var ea_input = document.getElementById('email_attachment'+index);
156
157         //get the parent node
158         var Parent = ea_input.parentNode;
159         //create replacement node
160         var ea = document.createElement('input');
161     ea.setAttribute('id', 'email_attachment' + index);
162     ea.setAttribute('name', 'email_attachment' + index);
163     ea.setAttribute('tabindex', '120');
164     ea.setAttribute('size', '40');
165     ea.setAttribute('type', 'file');
166
167         //replace the old node with the new one
168     Parent.replaceChild(ea, ea_input);
169
170         //hide the div element
171         elem.style.display='none';
172
173 }
174
175 function addFile() {
176         for(var i=0;i<10;i++) {
177                 var elem = document.getElementById('file'+i);
178                 if(elem.style.display == 'none') {
179                         elem.style.display='block';
180                         break;
181                 }
182         }
183 }
184 ////    END DOCUMENT HANDLING HELPERS
185 ///////////////////////////////////////////////////////////////////////////////
186
187 ///// New file upload code
188
189 function multiFiles( list_target){
190         // Where to write the list
191         this.list_target = list_target;
192         //this.list_target = document.getElementById(list_target);
193         // How many elements?
194         this.count = 0;
195         this.id = 0;
196         /**
197          * Add a new file input element
198          */
199         this.addElement = function( element ){
200                 // Make sure it's a file input element
201                 if( element.tagName == 'INPUT' && element.type == 'file' ){
202                     var currCount =this.id++;
203                         element.name = 'email_attachment' + currCount;
204                         element.id   = 'email_attachment' + currCount;
205
206                         // Add reference to this object
207                         element.multi_selector = this;
208                         // What to do when a file is selected
209             element.onchange = function() {
210                 var url = 'index.php?to_pdf=1&module=EmailTemplates&action=AttachFiles',
211                     new_element;
212
213                 //AJAX call begins
214                 YAHOO.util.Connect.setForm(document.getElementById("upload_form"), true, true);
215                 YAHOO.util.Connect.asyncRequest('POST', url, {upload: function() {}}, null);
216                 //AJAX call ends
217
218                 // New file input
219                 new_element = document.createElement('input');
220                 new_element.type = 'file';
221                 // new_element.name = 'email_attachment' +up++;
222
223                 // Add new element
224                 this.parentNode.insertBefore(new_element, this);
225                 // Apply 'update' to element
226                 this.multi_selector.addElement(new_element);
227                 // Update list
228                 this.multi_selector.addListRow(this);
229                 // Hide this: we can't use display:none because Safari doesn't like it
230                 //this.style.display='none';
231                 //display none works fine for FF and IE
232                 this.style.display = 'none';
233                 //later for Safari add following
234                 //this.style.position = 'absolute';
235                 //this.style.left = '-5000px';
236             };
237                         // File element counter
238                         this.count++;
239                         // Most recent element
240                         this.current_element = element;
241
242                 } else {
243                         // This can only be applied to file input elements!
244                         alert( 'Error: not a file input element' );
245                 };
246         };
247
248         /**
249          * Add a new row to the list of files
250          */
251         this.addListRow = function( element ){
252                 // Row div
253                 var new_row = document.createElement( 'div' );
254
255                 // Delete button
256                 var new_row_button_remove = document.createElement( 'input' );
257         new_row_button_remove.type = 'button';
258         new_row_button_remove.value = lbl_remove;
259
260                 var new_row_file_name = document.createElement( 'input' );
261                 new_row_file_name.type = 'text';
262                 new_row_file_name.size = '40';
263                 new_row_file_name.disabled=true;
264
265                 var new_row_chk_box = document.createElement( 'input' );
266                 new_row_chk_box.setAttribute('id','checkBoxFile[]');
267                 new_row_chk_box.setAttribute('name','checkBoxFile[]');
268                 new_row_chk_box.type = 'checkbox';
269                 new_row_chk_box.checked =false;
270                 new_row_chk_box.disabled =true;
271
272         var new_row_attach_file = document.createElement( 'input' );
273         new_row_attach_file.type = 'image';
274         new_row_attach_file.value ='/index.php?entryPoint=getImage&themeName='+SUGAR.themes.theme_name+'&imageName=company_logo.png';
275         new_row_attach_file.disabled ='true';
276
277         var imgElement = document.createElement("img");
278         imgElement.setAttribute("src", "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=Accounts.gif");
279         imgElement.setAttribute("align","absmiddle");
280         imgElement.setAttribute("alt",lbl_email_attachments_file);
281         imgElement.setAttribute("border","0");
282         imgElement.setAttribute("height","16");
283         imgElement.setAttribute("width","16");
284
285         var new_row_button_embed = document.createElement("img");
286         new_row_button_embed.setAttribute("src", "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=attachment.gif");
287         new_row_button_embed.setAttribute("align","absmiddle");
288         new_row_button_embed.setAttribute("alt",lbl_email_attachments_embeded);
289         new_row_button_embed.setAttribute("border","0");
290         new_row_button_embed.setAttribute("height","16");
291         new_row_button_embed.setAttribute("width","16");
292
293                 // References
294                 new_row.element = element;
295         element.size='40';
296
297                 // Delete function
298                 new_row_button_remove.onclick = function() {
299                         var filePathComponents = this.parentNode.element.value.split("\\"),
300                 fileName = (filePathComponents[filePathComponents.length - 1]),
301
302                 // tinymce related
303                 tiny = tinyMCE.getInstanceById('body_text'),
304                 currValTiny = tiny.getContent();
305
306                         // Remove row element from form
307                         this.parentNode.element.parentNode.removeChild(this.parentNode.element);
308
309             // find instances of the file and set it to ''
310             while (currValTiny.indexOf(fileName) !== -1) {
311                 currValTiny = currValTiny.replace(fileName, 'QW%%^%%WQ');
312                 currValTiny = currValTiny.replace(/<img[^<]*QW%%\^%%WQ[^>]*>?/, '');
313             }
314
315                     tiny.setContent(currValTiny);
316
317                         // Remove this row from the list
318                         this.parentNode.parentNode.removeChild(this.parentNode);
319
320                         // Decrement counter
321                         this.parentNode.element.multi_selector.count--;
322                         // Re-enable input element (if it's disabled)
323                         this.parentNode.element.multi_selector.current_element.disabled = false;
324
325                         // Appease Safari
326                         // without it Safari wants to reload the browser window
327                         // which nixes your already queued uploads
328
329                         return false;
330                 };
331
332         // Embed image into the email body template
333         new_row_button_embed.onclick = function() {
334             var filePathComponents = element.value.split("\\"),
335                 fileName = filePathComponents[filePathComponents.length - 1],
336
337                 // constants
338                 allowedTypes = ['gif', 'bmp', 'png', 'jpg', 'jpeg'],
339                 imglocation = 'cache/images/';
340
341             //check if filetype is valid
342             if (SUGAR.util.validateFileExt(fileName, allowedTypes)) {
343                 cid = 'cid:' + fileName;
344                 embedImage = '<img src="' + imglocation + encodeURI(fileName) + '">';
345                 insert_variable(embedImage);
346
347                 this.parentNode.childNodes[2].checked = 'true';
348             } else {
349                 alert(select_image);
350             }
351         };
352
353                 // Set row value
354         /*
355                 var oas = new ActiveXObject("Scripting.FileSystemObject");
356         var d = document.a.b.value;
357         var e = oas.getFile(d);
358         var f = e.size;
359         alert(f + " bytes");
360                 alert(element);
361                 */
362         //new_row_file_name.value =element.value;
363         new_row_file_name_tab = element.value.split("\\");
364         //alert(new_row_file_name_tab);
365         nbr_elements = new_row_file_name_tab.length;
366         new_row_file_name.value = new_row_file_name_tab[nbr_elements-1];
367
368                 //new_row.innerHTML = element.value;
369         //add all the elements
370         //new_row.appendChild(new_row_attach_file);
371         new_row.appendChild(imgElement);
372         new_row.appendChild(new_row_button_embed);
373         new_row.appendChild(new_row_chk_box);
374                 new_row.appendChild( new_row_file_name);
375                 // Add button
376                 new_row.appendChild( new_row_button_remove);
377                 // Add it to the list
378                 this.list_target.appendChild( new_row );
379                 //document.getElementById(list_target).appendChild(new_row);
380         };
381 };
382
383
384 function docUpload() {
385
386         //var theForm = document.getElementById('EditView');
387         //var theForm = document.getElementById('upload_form');
388     //AJAX call begins
389     var rets ='';
390         var callback = {
391             upload: function(r) {
392            rets = YAHOO.lang.JSON.parse(r.responseText);
393            }
394     }
395
396         var url ='index.php?module=EmailTemplates&action=AttachDocuments';
397         YAHOO.util.Connect.asyncRequest('POST', url, callback,null);
398     //AJAX call ends
399
400         var element = document.getElementById('documentName');
401         var element1 = document.getElementById('documentId');
402     var element2 = document.getElementById('docRevId');
403     var element3 = document.getElementById('documentType');
404         var elm = document.createElement('div');
405         elm.setAttribute('id','file'+uploadIndex);
406
407         var sugarDoc = document.createElement('input');
408     sugarDoc.setAttribute('type', 'label');
409     sugarDoc.setAttribute('disabled', 'true');
410     sugarDoc.setAttribute('font', 'bold');
411     sugarDoc.setAttribute('value',"Sugar Document");
412
413
414     var new_row_button_embed_doc = document.createElement( 'input' );
415     new_row_button_embed_doc.type = 'button';
416     new_row_button_embed_doc.value = 'Embed';
417
418     var new_row_chk_box = document.createElement( 'input' );
419     new_row_chk_box.setAttribute('id','checkBoxDoc'+uploadIndex);
420     new_row_chk_box.setAttribute('name','checkBoxDoc'+uploadIndex);
421         new_row_chk_box.type = 'checkbox';
422         new_row_chk_box.checked =false;
423     new_row_chk_box.disabled='true';
424
425
426         var eah = document.createElement('input');
427     eah.setAttribute('id', 'documentId'+uploadIndex);
428     eah.setAttribute('name', 'documentId'+uploadIndex);
429     eah.setAttribute('tabindex', '120');
430     eah.setAttribute('type', 'hidden');
431     eah.setAttribute('value',element1.value);
432
433
434         var attId = document.createElement('input');
435     attId.setAttribute('id', 'docRevId'+uploadIndex);
436     attId.setAttribute('name', 'docRevId'+uploadIndex);
437     attId.setAttribute('tabindex', '120');
438     attId.setAttribute('type', 'hidden');
439     attId.setAttribute('value',element2.value);
440
441
442     var attType = document.createElement('input');
443     attType.setAttribute('id', 'documentType'+uploadIndex);
444     attType.setAttribute('name', 'documentType'+uploadIndex);
445     attType.setAttribute('tabindex', '120');
446     attType.setAttribute('type', 'hidden');
447     attType.setAttribute('value',element3.value);
448
449
450         var ea = document.createElement('input');
451     ea.setAttribute('id', 'document[]');
452     ea.setAttribute('name', 'document[]');
453     ea.setAttribute('tabindex', '120');
454     ea.setAttribute('size', '40');
455     ea.setAttribute('type', 'text');
456     ea.setAttribute('disabled',true);
457     ea.setAttribute('value',element.value);
458
459     var eai = document.createElement('input');
460     eai.setAttribute('type', 'button');
461     //eai.setAttribute('onclick', 'deleteFile('+uploadIndex+');');
462     eai.setAttribute('value', lbl_remove);
463     eai.onclick=function(){
464         var filename = this.parentNode.childNodes[4].value;
465                 if(filename){
466                 var tiny = tinyMCE.getInstanceById('body_text');
467                                 var currValTiny = tiny.getContent();
468                     while(currValTiny.indexOf(unescape(filename)) != -1){
469                                    currValTiny = currValTiny.replace(unescape(filename),'QW%%^%%WQ');
470                                    currValTiny = currValTiny.replace(/<img[^<]*QW%%\^%%WQ[^>]*>?/,'&#32');
471                                 }
472                                 tiny.setContent(currValTiny);
473                 }
474         this.parentNode.parentNode.removeChild(this.parentNode);
475     }
476
477
478     var new_row_button_embed = document.createElement("img");
479         new_row_button_embed.setAttribute("src", "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=attachment.gif");
480         new_row_button_embed.setAttribute("align","absmiddle");
481         new_row_button_embed.setAttribute("alt",lbl_email_attachments_embeded);
482         new_row_button_embed.setAttribute("border","0");
483         new_row_button_embed.setAttribute("height","16");
484         new_row_button_embed.setAttribute("width","16");
485         new_row_button_embed.onclick= function(){
486         //retrieve the documentid
487         this.parentNode.childNodes[2].checked='true';
488         var documentRevisionId = this.parentNode.childNodes[4].value;
489         var mime_type = this.parentNode.childNodes[5].value;
490                 if(mime_type == "image/gif" || mime_type == "image/bmp" || mime_type == "image/png" || mime_type == "image/x-png" || mime_type == "image/jpg" || mime_type == "image/jpeg")
491         {
492             embedImage='<img src="index.php?entryPoint=download&type=Documents&id='+documentRevisionId+'">';
493             insert_variable(embedImage);
494         }
495         else{
496             new_row_chk_box.checked =false;
497             alert(select_image);
498         }
499         };
500
501    var SugarDoc = document.createElement("img");
502    SugarDoc.setAttribute("src", "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=sugar_document.png");
503    SugarDoc.setAttribute("align","absmiddle");
504    SugarDoc.setAttribute("alt",lbl_email_attachments_document);
505    SugarDoc.setAttribute("border","0");
506    SugarDoc.setAttribute("height","16");
507    SugarDoc.setAttribute("width","16");
508
509
510     //elm.appendChild(eah);
511     elm.appendChild(SugarDoc);
512     elm.appendChild(new_row_button_embed);
513     elm.appendChild(new_row_chk_box);
514     elm.appendChild(eah);
515     elm.appendChild(attId);
516     elm.appendChild(attType);
517     elm.appendChild(ea);
518     elm.appendChild(eai);
519     elm.style.display = 'block';
520
521      var rN= document.getElementById('attachments_div');
522      rN.appendChild(elm);
523      uploadIndex++;
524 }
525
526 function addUploadFiles(form_name) {
527             var chForm = document.getElementById('upload_div');
528                 var theForm = document.getElementById(form_name);
529                 var elems = chForm.getElementsByTagName("input");
530                 //get the count of all the email_attachment file elements
531                 var count = 0;
532                 //Use the count to add the documents
533         count=this.multi_selector.id;
534         for (var i=0; i<count-1; i++) {
535     //          find out all the email_attachments and append to the EditView form
536           var el = document.getElementsByName('email_attachment'+i);
537           if(el[0] != null){
538                theForm.appendChild(el[0]);
539           }
540        }
541        var chForm = document.getElementById("upload_form");
542        var elems = chForm.getElementsByTagName("input");
543        var num = 0;
544        for (var i=0; i<elems.length; i++) {
545         //find out all the email_attachments and append to the EditView form
546           var el = elems[i];
547           //var el = document.getElementsByName('checkBoxFile[]');
548           if(el.id == 'checkBoxFile[]'){
549                 var eah = document.createElement('input');
550             eah.setAttribute('id', 'embedded'+num);
551                 eah.setAttribute('name', 'embedded'+num);
552                     eah.setAttribute('tabindex', '120');
553                     eah.setAttribute('type', 'hidden');
554                     eah.setAttribute('value',el.checked);
555                 num++;
556                 theForm.appendChild(eah);
557           }
558        }
559         }
560
561 function addUploadDocs(form_name) {
562         var chForm = document.getElementById("upload_form");
563                 var theForm = document.getElementById(form_name);
564                 var attDiv = document.getElementById("attachments_div");
565                 var elems = chForm.getElementsByTagName("input");
566                 var elems1 = attDiv.getElementsByTagName("input");
567         for (var i=0; i<elems1.length; i++) {
568         //if (elems[i].type == "file") {
569          var el = elems1[i];
570         if(el.id=='document[]') {
571             theForm.appendChild(el);
572                  }
573         }
574         for (var i=0; i<elems.length; i++) {
575         //if (elems[i].type == "file") {
576          var el = elems[i];
577
578         if(el.id=='document[]') {
579             theForm.appendChild(el);
580                 }
581                 if(el.id.indexOf('documentId')>=0 || el.id=='document[]') {
582             theForm.appendChild(el);
583                 }
584       }
585         }
586
587 function form_reset_doc() {
588         // var theForm = document.getElementById('upload_form');
589          var theForm = document.getElementById('upload_div');
590          var elems = theForm.getElementsByTagName("input");
591          for (var i=0; i<elems.length; i++) {
592                  if (elems[i].type == "text" || "hidden" ) {
593                     var el = elems[i];
594                     if(el.id == 'documentName') {
595                         var new_el =document.createElement('input');
596                         new_el.type = 'hidden';
597                         new_el.name = el.name;
598                         new_el.id = el.id;
599                         new_el.onchange = el.onchange;
600                         //new_el.disabled=true;
601                         //new_el.style.visibility="hidden";
602                         new_el.siz=true;
603                         el.parentNode.replaceChild(new_el, el);
604                         //   el.parentNode.insertBefore(new_el, el);
605                   //        el.parentNode.removeChild(el);
606                     }
607                     if(el.id == 'documentId') {
608                          var new_el =document.createElement('input');
609                          new_el.type = 'hidden';
610                          new_el.name = el.name;
611                          new_el.id = el.id;
612                          //new_el.onchange = el.onchange;
613                          //new_el.DISABLED='true';
614                          el.parentNode.replaceChild(new_el, el);
615                         //   el.parentNode.insertBefore(new_el, el);
616                           // el.parentNode.removeChild(el);
617                      }
618                  }
619         }
620 }
621
622
623 function selectDoc() {
624         URL="index.php?module=EmailTemplates&action=PopupDocumentsCampaignTemplate&to_pdf=true&target=" ;
625         windowName = 'selectDocument';
626         windowFeatures = 'width=800' + ',height=600' + ',resizable=1,scrollbars=1';
627
628         win = SUGAR.util.openWindow(URL, windowName, windowFeatures);
629         if(window.focus) {
630                 // put the focus on the popup if the browser supports the focus() method
631                 win.focus();
632         }
633 }
634 ///////////////////////////////////////////////////////////////////////////////
635 ////    HTML/PLAIN EDITOR FUNCTIONS
636 function setEditor() {
637         if(document.getElementById('setEditor').checked == true) {
638                 toggle_textonly();
639         }
640 }
641
642 // cn: bug 9690 - unchecked "Send HTML" - we hose the html_div contents
643 function prepSave() {
644         if(document.getElementById('setEditor').checked == true) {
645                 document.getElementById('html_div').innerHTML = '';
646         }
647
648 }
649
650 function toggle_textonly() {
651         var altText = document.getElementById('alt_text_div');
652         var plain = document.getElementById('text_div');
653         var html = document.getElementById('html_div');
654
655         var desc = document.getElementById('description');
656         var tiny = tinyMCE.getInstanceById('description_html');
657
658         // toggling INTO HTML editting
659         if(html.style.display == 'none') {
660                 html.style.display = 'block';
661                 if(document.getElementById('toggle_textarea_elem').checked == false) {
662                         plain.style.display = 'none';
663                 }
664                 altText.style.display = 'block';
665
666                 var plainText = new String(desc.value);
667                 plainText = plainText.replace(/\n/gi, '<br />');
668                 tiny.setContent(plainText);
669         } else {
670                 // toggling into Plain Text ONLY
671                 html.style.display = 'none';
672                 plain.style.display = 'block';
673                 altText.style.display = 'none';
674
675                 if(tiny.getContent()) {
676                         var htmlText = new String(tiny.getContent());
677                         htmlText = htmlText.replace(/<br \/>/gi, "\n");
678                         htmlText = htmlText.replace(/&gt;/gi, ">");
679                         htmlText = htmlText.replace(/&lt;/gi, "<");
680                         htmlText = htmlText.replace(/&nbsp;/gi, " ");
681                         desc.value = stripTags(htmlText);
682                 }
683         }
684 }
685
686 function stripTags(str) {
687         var theText = new String(str);
688
689         if(theText != 'undefined') {
690                 return theText.replace(/<\/?[^>]+>/gi, '');
691         }
692 }
693
694 function toggle_textarea() {
695         var checkbox = document.getElementById('toggle_textarea_elem');
696         var plain = document.getElementById('text_div');
697
698         if (checkbox.checked == false) {
699                 plain.style.display = 'none';
700         } else {
701                 plain.style.display = 'block';
702         }
703 }
704 ////    END HTML/PLAIN EDITOR FUNCTIONS
705 ///////////////////////////////////////////////////////////////////////////////
706
707
708
709
710 ///////////////////////////////////////////////////////////////////////////////
711 ////    EMAIL TEMPLATE CODE
712 function appendEmailTemplateJSON() {
713         // query based on template, contact_id0,related_to
714         if(document.EditView.name.value == '') { // cn: bug 7743, don't stomp populated Subject Line
715                 document.EditView.name.value = decodeURI(encodeURI(json_objects['email_template_object']['fields']['subject']));
716         }
717
718         document.EditView.description.value += decodeURI(encodeURI(json_objects['email_template_object']['fields']['body'])).replace(/<BR>/ig, '\n');
719         var tiny = tinyMCE.getInstanceById("description_html");
720         var tinyHtml = tiny.getContent(true);
721
722         // cn: bug 10985 - IE6/7 will show inline image at top of screen if we set this with no valid target
723         if(document.getElementById('setEditor').checked == false) {
724                 var descHtml = decodeURI(encodeURI(json_objects['email_template_object']['fields']['body_html'])).replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/<BR>/ig, '\n').replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"');
725
726                 // cn: bug 13376 - leave &amp; to last to preserve certain HTML-entities for TinyMCE editor
727                 descHtml = descHtml.replace(/&amp;/gi,'&');
728
729                 tiny.setContent(tinyHtml + descHtml);
730         }
731
732         var htmlDiv = document.getElementById('html_div');
733
734         // hide the HTML editor if this is Plain-text only
735         if((tinyHtml == '' || tinyHtml == '<br>') && htmlDiv.style.display == '') {
736                 var desc = document.getElementById('description');
737                 var plainText = new String(desc.value);
738                 plainText = plainText.replace(/\n/gi, '<br />');
739                 tiny.setContent(plainText);
740
741                 // cn: bug 6212
742                 // if the template is plain-text, then uncheck "Send HTML Email"
743                 document.getElementById('setEditor').checked = true;
744                 setEditor();
745         }
746 }
747
748 if(typeof SugarClass == "object") {
749         SugarClass.inherit("EmailJsonObj","SugarClass");
750 }
751 function EmailJsonObj() {
752 }
753 EmailJsonObj.prototype.display = function(result) {
754         var bean;
755         var block = document.getElementById('template_attachments');
756         var target = block.innerHTML;
757         var full_file_path;
758
759         for(i in result) {
760                 if(typeof result[i] == 'object') {
761                         bean = result[i];
762                         full_file_path = file_path + bean['id']+bean['filename'];
763                         target += '\n<input type="hidden" name="template_attachment[]" value="' + bean['id'] + '">';
764                         target += '\n<input type="checkbox" name="temp_remove_attachment[]" value="' + bean['id'] + '"> '+ lnk_remove + '&nbsp;&nbsp;';
765                         target += '<a href="' + full_file_path + '"target="_blank">' + bean['filename'] + '</a><br>';
766                 }
767         }
768         block.innerHTML = target;
769 }
770
771 ejo = new EmailJsonObj();
772 ////    END EMAIL TEMPLATE CODE
773 ///////////////////////////////////////////////////////////////////////////////
774
775
776 function fill_form(type, error_text) {
777         if(document.getElementById('subjectfield').value == '') {
778                 var sendAnyways = confirm(lbl_send_anyways);
779                 if(sendAnyways == false) { return false; }
780         }
781
782         if(type == 'out' && document.EditView.to_addrs.value  == '' &&
783                 document.EditView.cc_addrs.value  == '' &&
784                 document.EditView.bcc_addrs.value  == '') {
785
786                 alert(error_text);
787                 return false;
788         }
789
790         var the_form = document.EditView;
791         var inputs = the_form.getElementsByTagName('input');
792
793         //  this detects if browser needs the following hack or not..
794         if(inputs.length > 0) {
795                 // no need to appendChild to EditView to get file uploads to work
796                 return check_form('EditView');
797         }
798
799         if(! check_form('EditView')) {
800                 return false;
801         }
802         return true;
803 }
804
805 function setLabels(uploads_arr) {
806 }
807
808
809
810 //this function appends the selected email address to the aggregated email address fields.
811 function set_current_parent(id,email,name,value) {
812         current_contact_id.value += id+";";
813         current_contact_email.value += email+";";
814         current_contact_name.value += name+";";
815
816         if(current_contact.value != '') {
817                 current_contact.value += "; ";
818         }
819
820         current_contact.value += name + " <" + email + ">";
821 //      current_contact.value += value;
822 }
823
824 function set_email_return(popup_reply_data) {
825         var form_name = popup_reply_data.form_name;
826         var name_to_value_array = popup_reply_data.name_to_value_array;
827         if (typeof name_to_value_array == "object" ) {
828                 update_email_addresses(form_name,name_to_value_array);
829         } else {
830                 for(var i in name_to_value_array) {
831                         update_email_addresses(form_name,name_to_value_array[i]);
832                 }
833         }
834 }
835
836 function update_email_addresses(form_name,name_to_value) {
837         for(var the_key in name_to_value) {
838                 if(the_key == 'toJSON') {
839                         /* just ignore */
840                 } else {
841                         var displayValue = name_to_value[the_key];
842                         displayValue=displayValue.replace('&#039;',"'");  //restore escaped single quote.
843                         displayValue=displayValue.replace('&amp;',"&");  //restore escaped &.
844                         displayValue=displayValue.replace('&gt;',">");  //restore escaped >.
845                         displayValue=displayValue.replace('&lt;',"<");  //restore escaped <.
846                         displayValue=displayValue.replace('&quot; ',"\"");  //restore escaped ".
847
848                         window.document.forms[form_name].elements[the_key].value += displayValue + '; ';
849                 }
850         }
851 }
852
853 //create references to input fields associated with the select email address button.
854 //Clicked button is passed as the parameter to the function.
855 function button_change_onclick(obj) {
856         var prefix = 'to_';
857         if(obj.name.match(/^cc_/i)) {
858             prefix = 'cc_';
859         } else if(obj.name.match(/^bcc_/i)) {
860                 prefix = 'bcc_';
861         }
862
863         for(var i = 0; i < obj.form.length;i++) {
864                 child = obj.form[i];
865                 if(child.name.indexOf(prefix) != 0) {
866                         continue;
867                 }
868
869                 if(child.name.match(/addrs_emails$/i)) {
870                         current_contact_email = child;
871                 } else if(child.name.match(/addrs_ids$/i)) {
872                         current_contact_id = child;
873                 } else if(child.name.match(/addrs_names$/i)) {
874                         current_contact_name = child;
875                 } else if(child.name.match(/addrs$/i)) {
876                         current_contact = child;
877                 }
878         }
879
880         var filter = '';
881         var acct_name = '';
882
883         if(document.EditView.parent_type.value  == 'Accounts' && typeof(document.EditView.parent_name.value) != 'undefined' && document.EditView.parent_name.value != '') {
884                 filter = "&form_submit=false&query=true&html=Email_picker&account_name=" + escape(document.EditView.parent_name.value);
885                 acct_name = document.EditView.parent_name.value;
886         }
887
888         var popup_request_data =
889         {
890                 "call_back_function" : "set_email_return",
891                 "form_name" : "EditView",
892                 "field_to_name_array" :
893                 {
894                         "id" : prefix + "addrs_ids",
895                         "email1" : prefix + "addrs_emails",
896                         "name" : prefix + "addrs_names",
897                         "email_and_name1" : prefix + "addrs_field"
898                 }
899         };
900
901         return open_popup("Contacts", 600, 400, filter, true, false, popup_request_data, 'MultiSelect', false, 'popupdefsEmail');
902 }
903
904 //this function clear the value stored in the aggregated email address fields(nodes).
905 //it relies on the references set by the button_change_onclick method
906 function clear_email_addresses() {
907
908         if(current_contact != '') {
909                 current_contact.value = '';
910         }
911         if(current_contact_id != '') {
912                 current_contact_id.value = '';
913         }
914         if(current_contact_email != '') {
915                 current_contact_email.value = '';
916         }
917         if(current_contact_name != '') {
918                 current_contact_name.value = '';
919         }
920 }
921
922 function quick_create_overlib(id, theme) {
923     return overlib('<a style=\'width: 150px\' class=\'menuItem\' onmouseover=\'hiliteItem(this,"yes");\' onmouseout=\'unhiliteItem(this);\' href=\'index.php?module=Cases&action=EditView&inbound_email_id=' + id + '\'>' +
924             "<img border='0' src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=Cases.gif' style='margin-right:5px'>" + SUGAR.language.get('Emails', 'LBL_LIST_CASE') + '</a>' +
925             "<a style='width: 150px' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' href='index.php?module=Leads&action=EditView&inbound_email_id=" + id + "'>" +
926                     "<img border='0' src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=Leads.gif' style='margin-right:5px'>"
927                     + SUGAR.language.get('Emails', 'LBL_LIST_LEAD') + "</a>" +
928              "<a style='width: 150px' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' href='index.php?module=Contacts&action=EditView&inbound_email_id=" + id + "'>" +
929                     "<img border='0' src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=Contacts.gif' style='margin-right:5px'>"
930                     + SUGAR.language.get('Emails', 'LBL_LIST_CONTACT') + "</a>" +
931              "<a style='width: 150px' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' href='index.php?module=Bugs&action=EditView&inbound_email_id=" + id + "'>"+
932                     "<img border='0' src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=Bugs.gif' style='margin-right:5px'>"
933                     + SUGAR.language.get('Emails', 'LBL_LIST_BUG') + "</a>" +
934              "<a style='width: 150px' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' href='index.php?module=Tasks&action=EditView&inbound_email_id=" + id + "'>" +
935                     "<img border='0' src='index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=Tasks.gif' style='margin-right:5px'>"
936                    + SUGAR.language.get('Emails', 'LBL_LIST_TASK') + "</a>"
937             , CAPTION, SUGAR.language.get('Emails', 'LBL_QUICK_CREATE')
938             , STICKY, MOUSEOFF, 3000, CLOSETEXT, '<img border=0  style="margin-left:2px; margin-right: 2px;" src="index.php?entryPoint=getImage&themeName='+SUGAR.themes.theme_name+'&imageName=close.gif">', WIDTH, 150, CLOSETITLE, SUGAR.language.get('app_strings', 'LBL_ADDITIONAL_DETAILS_CLOSE_TITLE'), CLOSECLICK, FGCLASS, 'olOptionsFgClass',
939             CGCLASS, 'olOptionsCgClass', BGCLASS, 'olBgClass', TEXTFONTCLASS, 'olFontClass', CAPTIONFONTCLASS, 'olOptionsCapFontClass', CLOSEFONTCLASS, 'olOptionsCloseFontClass');
940 }
941
942