]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/SugarEmailAddress/SugarEmailAddress.js
Release 6.5.5
[Github/sugarcrm.git] / jssource / src_files / include / SugarEmailAddress / SugarEmailAddress.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 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 (function() {
37         //Do not double define
38         if (SUGAR.EmailAddressWidget) return;
39         
40         var Dom = YAHOO.util.Dom;
41         
42         SUGAR.EmailAddressWidget = function(module) {
43                 if (!SUGAR.EmailAddressWidget.count[module]) SUGAR.EmailAddressWidget.count[module] = 0;
44                 this.count = SUGAR.EmailAddressWidget.count[module];
45                 SUGAR.EmailAddressWidget.count[module]++;
46                 this.module = module;
47                 this.id = this.module + this.count;
48                 if (document.getElementById(module+'_email_widget_id'))
49                     document.getElementById(module+'_email_widget_id').value = this.id;
50                 SUGAR.EmailAddressWidget.instances[this.id] = this;
51         }
52         
53         SUGAR.EmailAddressWidget.instances = {};
54         SUGAR.EmailAddressWidget.count = {};
55         
56         SUGAR.EmailAddressWidget.prototype = {
57             emailTemplate : '<tr id="emailAddressRow">' + 
58                 '<td nowrap="NOWRAP"><input type="text" title="email address 0" name="emailAddress{$index}" id="emailAddress0" size="30"/></td>' +
59                 '<td><span>&nbsp;</span><img id="removeButton0" name="0" src="index.php?entryPoint=getImage&amp;themeName=Sugar&amp;imageName=delete_inline.gif"/></td>' +
60                 '<td align="center"><input type="radio" name="emailAddressPrimaryFlag" id="emailAddressPrimaryFlag0" value="emailAddress0" enabled="true" checked="true"/></td>' +
61                 '<td align="center"><input type="checkbox" name="emailAddressOptOutFlag[]" id="emailAddressOptOutFlag0" value="emailAddress0" enabled="true"/></td>' + 
62                 '<td align="center"><input type="checkbox" name="emailAddressInvalidFlag[]" id="emailAddressInvalidFlag0" value="emailAddress0" enabled="true"/></td>' + 
63                 '<td><input type="hidden" name="emailAddressVerifiedFlag0" id="emailAddressVerifiedFlag0" value="true"/></td>' + 
64                 '<td><input type="hidden" name="emailAddressVerifiedValue0" id="emailAddressVerifiedValue0" value=""/></td></tr>',
65                 
66                 numberEmailAddresses : 0,
67         replyToFlagObject : new Object(),
68         verifying : false,
69         enterPressed : false,
70                 tabPressed : false,
71         emailView:"",
72                 emailIsRequired: false,
73                 tabIndex: -1,
74                 
75                 prefillEmailAddresses: function(tableId, o){
76                         for (i = 0; i < o.length; i++) {
77                                 o[i].email_address = o[i].email_address.replace('&#039;', "'");
78                                 this.addEmailAddress(tableId, o[i].email_address, o[i].primary_address, o[i].reply_to_address, o[i].opt_out, o[i].invalid_email, o[i].email_address_id);
79                         }
80                 },
81                 
82                 retrieveEmailAddress: function (event) {
83             var callbackFunction = function success(data) {
84                     var vals = YAHOO.lang.JSON.parse(data.responseText);
85                     var target = vals.target;
86                         event = this.getEvent(event);
87                                 
88                                 if(vals.email) {
89                            var email = vals.email;
90                            if(email != '' && /\d+$/.test(target)) {
91                                var matches = target.match(/\d+$/);
92                                var targetNumber = matches[0];
93                                var optOutEl = Dom.get(this.id + 'emailAddressOptOutFlag' + targetNumber);
94                                if(optOutEl) {
95                                    optOutEl.checked = email['opt_out'] == 1 ? true : false;
96                                }
97                                var invalidEl = Dom.get(this.id + 'emailAddressInvalidFlag' + targetNumber);
98                                if(invalidEl) {
99                                    invalidEl.checked = email['invalid_email'] == 1 ? true : false;
100                                }
101                            }
102                         }
103                         //Set the verified flag to true
104                         var index = /[a-z]*\d?emailAddress(\d+)/i.exec(target)[1];
105                         
106                                 var verifyElementFlag = Dom.get(this.id + 'emailAddressVerifiedFlag' + index);
107                         
108                         if(verifyElementFlag.parentNode.childNodes.length > 1) {
109                            verifyElementFlag.parentNode.removeChild(verifyElementFlag.parentNode.lastChild);
110                         }
111                         
112                         var verifiedTextNode = document.createElement('span');
113                         verifiedTextNode.innerHTML = '';
114                         verifyElementFlag.parentNode.appendChild(verifiedTextNode);
115                         verifyElementFlag.value = "true";
116                         this.verifyElementValue = Dom.get(this.id +'emailAddressVerifiedValue' + index);
117                         this.verifyElementValue.value = Dom.get(this.id +'emailAddress' + index).value;
118                         this.verifying = false;
119                         
120                         // If Enter key or Save button was pressed then we proceed to attempt a form submission
121                         var savePressed = false;
122                         if(event) {
123                            var elm = document.activeElement || event.explicitOriginalTarget;
124                            if(typeof elm.type != 'undefined' && /submit|button/.test(elm.type.toLowerCase())) {
125                         //if we are in here, then the element has been recognized as a button or submit type, so check the id
126                         //to make sure it is related to a submit button that should lead to a form submit
127
128                         //note that the document.activeElement and explicitOriginalTarget calls do not work consistantly across
129                         // all browsers, so we have to include this check after we are sure that the calls returned something as opposed to in the coindition above.
130                         // Also, since this function is called on blur of the email widget, we can't rely on a third object as a flag (a var or hidden form input)
131                         // since this function will fire off before the click event from a button is executed, which means the 3rd object will not get updated prior to this function running.
132                         if(/save|full|cancel|change/.test(elm.value.toLowerCase())){
133                            //this is coming from either a save, full form, cancel, or view change log button, we should set savePressed = true;
134                             savePressed = true;
135                         }
136                    }
137                         }
138
139                         
140                         if(savePressed || this.enterPressed) {
141                            setTimeout("SUGAR.EmailAddressWidget.instances." + this.id + ".forceSubmit()", 2100);
142                         } else if(this.tabPressed) {
143                            Dom.get(this.id + 'emailAddressPrimaryFlag' + index).focus();
144                         }
145                     }
146
147                     var event = this.getEvent(event);
148                     var targetEl = this.getEventElement(event);
149                     var index = /[a-z]*\d?emailAddress(\d+)/i.exec(targetEl.id)[1];
150                         var verifyElementFlag = Dom.get(this.id + 'emailAddressVerifiedFlag' + index);
151
152             if(this.verifyElementValue == null || typeof(this.verifyElementValue)=='undefined'){
153                 //we can't do anything without this value, so just return
154                 return false;
155             }
156
157             this.verifyElementValue = Dom.get(this.id + 'emailAddressVerifiedValue' + index);
158                     verifyElementFlag.value = (trim(targetEl.value) == '' || targetEl.value == this.verifyElementValue.value) ? "true" : "false"
159                     
160                     //Remove the span element if it is present
161                     if(verifyElementFlag.parentNode.childNodes.length > 1) {
162                        verifyElementFlag.parentNode.removeChild(verifyElementFlag.parentNode.lastChild);
163                     }
164                     
165                     if(/emailAddress\d+$/.test(targetEl.id) && isValidEmail(targetEl.value) && !this.verifying && verifyElementFlag.value == "false") {
166                         verifiedTextNode = document.createElement('span');
167                         verifyElementFlag.parentNode.appendChild(verifiedTextNode);
168                         verifiedTextNode.innerHTML = SUGAR.language.get('app_strings', 'LBL_VERIFY_EMAIL_ADDRESS');
169                         this.verifying = true;
170                         var cObj = YAHOO.util.Connect.asyncRequest(
171                                     'GET', 
172                                         'index.php?module=Contacts&action=RetrieveEmail&target=' + targetEl.id + '&email=' + targetEl.value, 
173                                         {success: callbackFunction, failure: callbackFunction, scope: this}
174                                 );
175                     }
176             },
177
178         handleKeyDown: function (event) {
179                     var e = this.getEvent(event);
180                     var eL = this.getEventElement(e);
181                     if ((kc = e["keyCode"])) {
182                         this.enterPressed = (kc == 13) ? true : false;
183                         this.tabPressed = (kc == 9) ? true : false;
184                         
185                         if(this.enterPressed || this.tabPressed) {
186                            this.retrieveEmailAddress(e);
187                            if (this.enterPressed)
188                                this.freezeEvent(e);
189                         }
190                     }
191                 }, //handleKeyDown()
192                 
193                 getEvent :function (event) {
194                     return (event ? event : window.event);
195                 },//getEvent
196                 
197                 getEventElement : function (e) {
198                     return (e.srcElement ? e.srcElement: (e.target ? e.target : e.currentTarget));
199                 },//getEventElement
200                 
201                 freezeEvent : function (e) {
202                     if (e.preventDefault) e.preventDefault();
203                     e.returnValue = false;
204                     e.cancelBubble = true;
205                     if (e.stopPropagation) e.stopPropagation();
206                     return false;
207                 },//freezeEvent
208                 
209                 addEmailAddress : function (tableId, address, primaryFlag, replyToFlag, optOutFlag, invalidFlag, emailId) {
210                         if (this.addInProgress)
211                             return;
212                         this.addInProgress = true;
213                     if (!address)
214                 address = "";
215                         var insertInto = Dom.get(tableId);
216                     var parentObj = insertInto.parentNode;
217                     var newContent = document.createElement("input");
218                     var nav = new String(navigator.appVersion);
219
220                     var newContentRecordId = document.createElement("input");
221                     var newContentPrimaryFlag = document.createElement("input");
222                     var newContentReplyToFlag = document.createElement("input");
223                     var newContentOptOutFlag = document.createElement("input");
224                     var newContentInvalidFlag = document.createElement("input");
225                     var newContentVerifiedFlag = document.createElement("input");
226                     var newContentVerifiedValue = document.createElement("input");
227                     var removeButton = document.createElement("button");
228             var removeButtonImg = document.createElement('img');
229                     var tbody = document.createElement("tbody");
230                     var tr = document.createElement("tr");
231                     var td1 = document.createElement("td");
232                     var td2 = document.createElement("td");
233                     var td3 = document.createElement("td");
234                     var td4 = document.createElement("td");
235                     var td5 = document.createElement("td");
236                     var td6 = document.createElement("td");
237                     var td7 = document.createElement("td");
238                     var td8 = document.createElement("td");
239
240             //use the value if the tabindex value for email has been passed in from metadata (defined in include/EditView/EditView.tpl
241             //else default to 0 
242             var tabIndexCount = 0;
243             if(typeof(SUGAR.TabFields) !='undefined' && typeof(SUGAR.TabFields['email1']) != 'undefined'){
244                 tabIndexCount = SUGAR.TabFields['email1'];
245             }
246                     // set input field attributes
247                     newContent.setAttribute("type", "text");
248                     newContent.setAttribute("name", this.id + "emailAddress" + this.numberEmailAddresses);
249                     newContent.setAttribute("id", this.id + "emailAddress" + this.numberEmailAddresses);
250                     newContent.setAttribute("tabindex", tabIndexCount);
251                     newContent.setAttribute("size", "30");
252             newContent.setAttribute("title", SUGAR.language.get('app_strings', 'LBL_EMAIL_TITLE'));
253                 
254                     if(address != '') {
255                         newContent.setAttribute("value", address);
256                     }
257
258             // inner structure of remove button
259             removeButtonImg.setAttribute('src', "index.php?entryPoint=getImage&themeName="+SUGAR.themes.theme_name+"&imageName=id-ff-remove-nobg.png");
260                     
261                     // remove button
262                     removeButton.setAttribute("id", this.id + "removeButton" + this.numberEmailAddresses);
263                         removeButton.setAttribute("class", "id-ff-remove");
264                     removeButton.setAttribute("name", this.numberEmailAddresses);
265                     removeButton.setAttribute("type", "button");
266             removeButton.setAttribute("tabindex", tabIndexCount);
267             removeButton.onclick = (function(eaw) {
268                 return function() {
269                     eaw.removeEmailAddress(this.name);
270                 }
271             })(this);
272             removeButton.appendChild(removeButtonImg);
273                     
274                     // set record id
275                     newContentRecordId.setAttribute("type", "hidden");
276                     newContentRecordId.setAttribute("name", this.id + "emailAddressId" + this.numberEmailAddresses);
277                     newContentRecordId.setAttribute("id", this.id + "emailAddressId" + this.numberEmailAddresses);
278                     newContentRecordId.setAttribute("value", typeof(emailId) != 'undefined' ? emailId : '');
279                     newContentRecordId.setAttribute("enabled", "true");
280
281                     // set primary flag
282                     newContentPrimaryFlag.setAttribute("type", "radio");
283                     newContentPrimaryFlag.setAttribute("name", this.id + "emailAddressPrimaryFlag");
284                     newContentPrimaryFlag.setAttribute("id", this.id + "emailAddressPrimaryFlag" + this.numberEmailAddresses);
285                     newContentPrimaryFlag.setAttribute("value", this.id + "emailAddress" + this.numberEmailAddresses);
286                     newContentPrimaryFlag.setAttribute("enabled", "true");
287             newContentPrimaryFlag.setAttribute("tabindex", tabIndexCount);
288
289                     // set reply-to flag
290                     newContentReplyToFlag.setAttribute("type", "radio");
291                     newContentReplyToFlag.setAttribute("name", this.id + "emailAddressReplyToFlag");
292                     newContentReplyToFlag.setAttribute("id", this.id + "emailAddressReplyToFlag" + this.numberEmailAddresses);
293                     newContentReplyToFlag.setAttribute("value", this.id + "emailAddress" + this.numberEmailAddresses);
294                     newContentReplyToFlag.setAttribute("enabled", "true");
295             newContentReplyToFlag.setAttribute("tabindex", tabIndexCount);
296                     newContentReplyToFlag.eaw = this;
297                     newContentReplyToFlag['onclick']= function() {
298                         var form = document.forms[this.eaw.emailView];
299                         if (!form) {
300                             form = document.forms['editContactForm'];
301                         }
302                         var nav = new String(navigator.appVersion);
303                 
304                         if(nav.match(/MSIE/gim)) {
305                             for(i=0; i<form.elements.length; i++) {
306                                 var id = new String(form.elements[i].id);
307                                 if(id.match(/emailAddressReplyToFlag/gim) && form.elements[i].type == 'radio' && id != this.eaw.id) {
308                                     form.elements[i].checked = false;
309                                 }
310                             }           
311                         }
312                         for(i=0; i<form.elements.length; i++) {
313                             var id = new String(form.elements[i].id);
314                             if(id.match(/emailAddressReplyToFlag/gim) && form.elements[i].type == 'radio' && id != this.eaw.id) {
315                                 this.eaw.replyToFlagObject[this.eaw.id] = false;
316                             }
317                         } // for        
318                         if (this.eaw.replyToFlagObject[this.id]) {
319                             this.eaw.replyToFlagObject[this.id] = false;
320                             this.checked = false;
321                         } else {
322                             this.eaw.replyToFlagObject[this.id] = true;
323                             this.checked = true;
324                         } // else
325                     }
326
327                     // set opt-out flag
328                     newContentOptOutFlag.setAttribute("type", "checkbox");
329                     newContentOptOutFlag.setAttribute("name", this.id + "emailAddressOptOutFlag[]");
330                     newContentOptOutFlag.setAttribute("id", this.id + "emailAddressOptOutFlag" + this.numberEmailAddresses);
331                     newContentOptOutFlag.setAttribute("value", this.id + "emailAddress" + this.numberEmailAddresses);
332                     newContentOptOutFlag.setAttribute("enabled", "true");
333                         newContentOptOutFlag.eaw = this;
334             newContentOptOutFlag.setAttribute("tabindex", tabIndexCount);
335                     newContentOptOutFlag['onClick'] = function(){this.eaw.toggleCheckbox(this)};
336         
337                     // set invalid flag
338                     newContentInvalidFlag.setAttribute("type", "checkbox");
339                     newContentInvalidFlag.setAttribute("name", this.id + "emailAddressInvalidFlag[]");
340                     newContentInvalidFlag.setAttribute("id", this.id + "emailAddressInvalidFlag" + this.numberEmailAddresses);
341                     newContentInvalidFlag.setAttribute("value", this.id + "emailAddress" + this.numberEmailAddresses);
342                     newContentInvalidFlag.setAttribute("enabled", "true");
343                         newContentInvalidFlag.eaw = this;
344             newContentInvalidFlag.setAttribute("tabindex", tabIndexCount);
345                     newContentInvalidFlag['onClick']= function(){this.eaw.toggleCheckbox(this);};
346                     
347                     // set the verified flag and verified email value
348                     newContentVerifiedFlag.setAttribute("type", "hidden");
349                     newContentVerifiedFlag.setAttribute("name", this.id + "emailAddressVerifiedFlag" + this.numberEmailAddresses);
350                     newContentVerifiedFlag.setAttribute("id", this.id + "emailAddressVerifiedFlag" + this.numberEmailAddresses);
351                     newContentVerifiedFlag.setAttribute("value", "true");
352                 
353                     newContentVerifiedValue.setAttribute("type", "hidden");
354                     newContentVerifiedValue.setAttribute("name", this.id + "emailAddressVerifiedValue" + this.numberEmailAddresses);
355                     newContentVerifiedValue.setAttribute("id", this.id + "emailAddressVerifiedValue" + this.numberEmailAddresses);
356                     newContentVerifiedValue.setAttribute("value", address);
357             newContentVerifiedValue.setAttribute("tabindex", tabIndexCount);
358
359                     //Add to validation
360                     this.emailView = (this.emailView == '') ? 'EditView' : this.emailView;
361                     addToValidateVerified(this.emailView, this.id + "emailAddressVerifiedFlag" + this.numberEmailAddresses, 'bool', false, SUGAR.language.get('app_strings', 'LBL_VERIFY_EMAIL_ADDRESS'));
362                     
363                     tr.setAttribute("id", this.id + "emailAddressRow" + this.numberEmailAddresses);
364                     
365                     td1.setAttribute("nowrap", "NOWRAP");
366                     td3.setAttribute("align", "center");
367                     td4.setAttribute("align", "center");
368                     td5.setAttribute("align", "center");
369                     td6.setAttribute("align", "center");
370
371                     td1.appendChild(newContent);
372                     td1.appendChild(newContentRecordId);
373                     td1.appendChild(document.createTextNode(" "));
374                     spanNode = document.createElement('span');
375                     spanNode.innerHTML = '&nbsp;';
376                     td2.appendChild(spanNode);
377                     if (this.numberEmailAddresses != 0 || typeof (this.emailIsRequired) == "undefined" || !this.emailIsRequired)
378                        td2.appendChild(removeButton);
379                     td3.appendChild(newContentPrimaryFlag);
380                     td4.appendChild(newContentReplyToFlag);
381                     td5.appendChild(newContentOptOutFlag);
382                     td6.appendChild(newContentInvalidFlag);
383                     td7.appendChild(newContentVerifiedFlag);
384                     td8.appendChild(newContentVerifiedValue);
385                     
386                     tr.appendChild(td1);
387                     tr.appendChild(td2);
388                     tr.appendChild(td3);
389                 
390                     if(typeof(this.module) != 'undefined' && this.module == 'Users') {
391                         tr.appendChild(td4);
392                     } else {
393                         tr.appendChild(td5);
394                         tr.appendChild(td6);
395                     }
396                     
397                     tr.appendChild(td7);
398                     tr.appendChild(td8);
399                     
400                     tbody.appendChild(tr);
401                 
402                     
403
404                     insertInto.appendChild(tbody);
405                     
406                     // insert the new div->input into the DOM
407                     parentObj.insertBefore(Dom.get('targetBody'), insertInto);
408                     
409                     // CL Fix for 17651 (added OR condition check to see if this is the first email added)
410                     if(primaryFlag == '1' || (this.numberEmailAddresses == 0)) {
411                         newContentPrimaryFlag.setAttribute("checked", 'true');
412                 newContent.setAttribute("title", SUGAR.language.get('app_strings', 'LBL_EMAIL_PRIM_TITLE'));
413                     }
414                     
415                     if(replyToFlag == '1') {
416                         newContentReplyToFlag.setAttribute("checked", "true");
417                     }
418                     
419                     if (replyToFlag == '1') {
420                         this.replyToFlagObject[newContentReplyToFlag.id] = true;
421                     } else {
422                         this.replyToFlagObject[newContentReplyToFlag.id] = false;
423                     }
424                     
425                     if(optOutFlag == '1') {
426                         newContentOptOutFlag.setAttribute("checked", 'true');
427                 newContent.setAttribute("title", SUGAR.language.get('app_strings', 'LBL_EMAIL_OPT_TITLE'));
428                     }
429                     
430                     if(invalidFlag == '1') {
431                         newContentInvalidFlag.setAttribute("checked", "true");
432                 newContent.setAttribute("title", SUGAR.language.get('app_strings', 'LBL_EMAIL_INV_TITLE'));
433                     }
434                     newContent.eaw = this;
435                     newContent.onblur = function(e){this.eaw.retrieveEmailAddress(e)};
436                     newContent.onkeydown = function(e){this.eaw.handleKeyDown(e)};
437             if (YAHOO.env.ua.ie > 0) {
438                 // IE doesn't bubble up "change" events through the DOM.
439                 // So we need to fire onChange events on the parent span when the input changes
440                 var emailcontainer = Dom.getAncestorByTagName(insertInto,'span');
441                 YAHOO.util.Event.addListener(newContent, "change",
442                         function(ev, el){SUGAR.util.callOnChangeListers(el);}, emailcontainer
443                 );
444             }
445                     
446                     // Add validation to field
447             this.EmailAddressValidation(this.emailView, this.id+ 'emailAddress' + this.numberEmailAddresses,this.emailIsRequired, SUGAR.language.get('app_strings', 'LBL_EMAIL_ADDRESS_BOOK_EMAIL_ADDR'));
448             this.numberEmailAddresses++;
449                         this.addInProgress = false;
450                 }, //addEmailAddress
451
452         EmailAddressValidation : function(ev,fn,r,stR) {
453             YAHOO.util.Event.onContentReady(fn,
454     function () { addToValidate(ev, fn, 'email', r, stR);});
455         },
456
457                 removeEmailAddress : function(index) {
458                         removeFromValidate(this.emailView, this.id + 'emailAddress' + index);
459             var oNodeToRemove = Dom.get(this.id +  'emailAddressRow' + index);
460             var form = Dom.getAncestorByTagName(oNodeToRemove, "form");
461             oNodeToRemove.parentNode.removeChild(oNodeToRemove);
462
463             var removedIndex = parseInt(index);
464             //If we are not deleting the last email address, we need to shift the numbering to fill the gap
465             if(this.numberEmailAddresses != removedIndex) {
466                for(var x = removedIndex + 1; x < this.numberEmailAddresses; x++) {
467                    Dom.get(this.id + 'emailAddress' + x).setAttribute("name", this.id +"emailAddress" + (x-1));
468                    Dom.get(this.id + 'emailAddress' + x).setAttribute("id", this.id +"emailAddress" + (x-1));
469                    
470                    if(Dom.get(this.id + 'emailAddressInvalidFlag' + x)) {
471                        Dom.get(this.id + 'emailAddressInvalidFlag' + x).setAttribute("value", this.id + "emailAddress" + (x-1));
472                        Dom.get(this.id + 'emailAddressInvalidFlag' + x).setAttribute("id", this.id + "emailAddressInvalidFlag" + (x-1));
473                    }
474                    
475                    if(Dom.get(this.id + 'emailAddressOptOutFlag' + x)){
476                        Dom.get(this.id + 'emailAddressOptOutFlag' + x).setAttribute("value", this.id + "emailAddress" + (x-1));
477                        Dom.get(this.id + 'emailAddressOptOutFlag' + x).setAttribute("id", this.id + "emailAddressOptOutFlag" + (x-1));
478                    }
479                    
480                    if(Dom.get(this.id + 'emailAddressPrimaryFlag' + x)) {
481                        Dom.get(this.id + 'emailAddressPrimaryFlag' + x).setAttribute("id", this.id + "emailAddressPrimaryFlag" + (x-1));
482                    }
483                    
484                    Dom.get(this.id + 'emailAddressVerifiedValue' + x).setAttribute("id", this.id + "emailAddressVerifiedValue" + (x-1));
485                    Dom.get(this.id + 'emailAddressVerifiedFlag' + x).setAttribute("id", this.id + "emailAddressVerifiedFlag" + (x-1));
486                    
487                    var rButton = Dom.get(this.id + 'removeButton' + x);
488                    rButton.setAttribute("name", (x-1));
489                    rButton.setAttribute("id", this.id + "removeButton" + (x-1));
490                    Dom.get(this.id + 'emailAddressRow' + x).setAttribute("id", this.id + 'emailAddressRow' + (x-1));
491                }
492             }
493                         
494                         this.numberEmailAddresses--;
495             
496             
497             // CL Fix for 17651
498             if(this.numberEmailAddresses == 0) {
499                return;
500             }
501             
502             var primaryFound = false;
503             for(x=0; x < this.numberEmailAddresses; x++) {
504                 if(Dom.get(this.id + 'emailAddressPrimaryFlag' + x).checked) {
505                    primaryFound = true;
506                 }
507             }
508             
509             if(!primaryFound) {
510                Dom.get(this.id + 'emailAddressPrimaryFlag0').checked = true;
511                Dom.get(this.id + 'emailAddressPrimaryFlag0').value = this.id + 'emailAddress0';
512             }
513
514         },
515                 
516                 toggleCheckbox : function (el)
517                 {
518                         var form = document.forms[this.emailView];
519             if (!form) {
520                 form = document.forms['editContactForm'];
521             }
522             
523             if(YAHOO.env.ua.ie) {
524                 for(i=0; i<form.elements.length; i++) {
525                    var id = new String(form.elements[i].id);
526                     if(id.match(/emailAddressInvalidFlag/gim) && form.elements[i].type == 'checkbox' && id != el.id) {
527                         form.elements[i].checked = false;
528                     }
529                 }
530                 
531                 el.checked = true;
532             }
533                 },
534                 
535                 forceSubmit : function () {
536                     var theForm = Dom.get(this.emailView);
537             if(theForm) {
538                theForm.action.value = 'Save';
539                if(!check_form(this.emailView)) {
540                   return false;
541                }
542                if(this.emailView == 'EditView') {
543                    //this is coming from regular edit view form
544                   theForm.submit();
545                } else if (this.emailView.indexOf('DCQuickCreate')>0){
546                    //this is coming from the DC Quick Create Tool Bar, so call save on form
547                   DCMenu.save(theForm.id);
548                } else if(this.emailView.indexOf('QuickCreate')>=0) {
549                    //this is a subpanel create or edit form
550                   SUGAR.subpanelUtils.inlineSave(theForm.id, theForm.module.value+'_subpanel_save_button');
551                }
552             }
553         } //forceSubmit
554     };
555     emailAddressWidgetLoaded = true;
556 })();