]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/javascript/sugar_3.js
Release 6.5.15
[Github/sugarcrm.git] / jssource / src_files / include / javascript / sugar_3.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2013 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 /**
40  * Namespace for Sugar Objects
41  */
42 if (typeof(SUGAR) == "undefined") {
43     SUGAR = {
44
45         /**
46          * Creates a namespace if it doesn't exist and then returns it.
47          *
48          * Note: this implementation only creates a top-level namespace. Extend this function if
49          * multi-level namespaces are needed.
50          * @param ns
51          */
52         namespace: function(ns) {
53             SUGAR[ns] = SUGAR[ns] || {};
54
55             return ((typeof SUGAR[ns] === "object") && (SUGAR[ns] !== null)) ? SUGAR[ns] : false;
56         },
57
58         /**
59          * Add properties of an object to target object.
60          * @param target
61          * @param obj
62          */
63         append: function(target, obj) {
64             for (var prop in obj) {
65                 if (obj[prop] !== void 0) target[prop] = obj[prop];
66             }
67
68             return target;
69         }
70     };
71 }
72
73 // Namespaces
74 SUGAR.namespace("themes");
75 SUGAR.namespace("tour");
76
77 /**
78  * Namespace for Homepage
79  */
80  SUGAR.namespace("sugarHome");
81
82 /**
83  * Namespace for Subpanel Utils
84  */
85 SUGAR.namespace("subpanelUtils");
86
87 /**
88  * AJAX status class
89  */
90 SUGAR.namespace("ajaxStatusClass");
91
92 /**
93  * Tab selector utils
94  */
95 SUGAR.namespace("tabChooser");
96
97 /**
98  * General namespace for Sugar utils
99  */
100 SUGAR.namespace("utils");
101 SUGAR.namespace("savedViews");
102
103 /**
104  * Dashlet utils
105  */
106 SUGAR.namespace("dashlets");
107 SUGAR.namespace("unifiedSearchAdvanced");
108
109 SUGAR.namespace("searchForm");
110 SUGAR.namespace("language");
111 SUGAR.namespace("Studio");
112 SUGAR.namespace("contextMenu");
113 SUGAR.namespace("config");
114
115 var nameIndex = 0;
116 var typeIndex = 1;
117 var requiredIndex = 2;
118 var msgIndex = 3;
119 var jstypeIndex = 5;
120 var minIndex = 10;
121 var maxIndex = 11;
122 var altMsgIndex = 15;
123 var compareToIndex = 7;
124 var arrIndex = 12;
125 var operatorIndex = 13;
126 var callbackIndex = 16;
127 var allowblank = 8;
128 var validate = new Array();
129 var maxHours = 24;
130 var requiredTxt = 'Missing Required Field:';
131 var invalidTxt = 'Invalid Value:';
132 var secondsSinceLoad = 0;
133 var alertsTimeoutId;
134 var inputsWithErrors = new Array();
135 var tabsWithErrors = new Array();
136 var lastSubmitTime = 0;
137 var alertList = new Array();
138 var oldStartsWith = '';
139
140 /**
141  * @deprecated
142  *
143  * As of Sugar version 6.2.3 (MSIE Version 9) this function is deprecated.  The preferred method is to use the
144  * user agent check supplied by YUI to check for IE:
145  *
146  * for checking if a browser is IE in general :  if(YAHOO.env.ua.ie) {...}
147  *
148  * or for checking specific versions:  if (YAHOO.env.ua.ie >= 5.5 && YAHOO.env.ua.ie < 9) {...}
149  *
150  */
151 function isSupportedIE() {
152         var userAgent = navigator.userAgent.toLowerCase() ;
153
154         // IE Check supports ActiveX controls
155         if (userAgent.indexOf("msie") != -1 && userAgent.indexOf("mac") == -1 && userAgent.indexOf("opera") == -1) {
156                 var version = navigator.appVersion.match(/MSIE (\d+\.\d+)/)[1] ;
157                 if(version >= 5.5 && version < 10) {
158                         return true;
159                 } else {
160                         return false;
161                 }
162         }
163 }
164
165 function checkMinSupported(c, s) {
166     var current = c.split(".");
167     var supported = s.split(".");
168     for (var i in supported) {
169         if (current[i] && parseInt(current[i]) > parseInt(supported[i])) return true;
170         else if (current[i] && parseInt(current[i]) < parseInt(supported[i])) return false;
171     }
172     return true;
173 }
174
175 function checkMaxSupported(c, s) {
176     var current = c.split(".");
177     var supported = s.split(".");
178     for (var i in supported) {
179         if (current[i] && parseInt(current[i]) > parseInt(supported[i])) return false;
180         else if (current[i] && parseInt(current[i]) < parseInt(supported[i])) return true;
181     }
182     return true;
183 }
184
185 SUGAR.isSupportedBrowser = function(){
186     var supportedBrowsers = {
187         msie : {min:8, max:10}, // IE 8, 9, 10
188         safari : {min:534}, // Safari 5.1
189         mozilla : {min:22.0}, // Firefox 23.0
190         chrome : {min:28} // Chrome 28
191     };
192     var current = String($.browser.version);
193     var supported;
194     if ($.browser.msie){ // Internet Explorer
195         supported = supportedBrowsers['msie'];
196     }
197     else if ($.browser.mozilla) { // Firefox
198         supported = supportedBrowsers['mozilla'];
199     }
200     else {
201         $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
202         if($.browser.chrome){ // Chrome
203             current = navigator.userAgent.match(/Chrome\/(.*?) /)[1];
204             supported = supportedBrowsers['chrome'];
205         }
206         else if($.browser.safari){ // Safari
207             supported = supportedBrowsers['safari'];
208         }
209     }
210     if (current && supported)
211         return checkMinSupported(current, String(supported.min)) && (!supported.max || checkMaxSupported(current, String(supported.max)));
212     else
213         return false;
214 }
215
216 SUGAR.isIECompatibilityMode = function(){
217     var agentStr = navigator.userAgent;
218     var mode = false;
219     if (agentStr.indexOf("MSIE 7.0") > -1 &&
220         (agentStr.indexOf("Trident/5.0") > -1 || // IE9 Compatibility View
221          agentStr.indexOf("Trident/4.0") > -1    // IE8 Compatibility View
222         )
223     )
224     {
225         mode = true;
226     }
227     return mode;
228 }
229
230 SUGAR.isIE = isSupportedIE();
231 var isSafari = (navigator.userAgent.toLowerCase().indexOf('safari')!=-1);
232
233 // escapes regular expression characters
234 RegExp.escape = function(text) { // http://simon.incutio.com/archive/2006/01/20/escape
235   if (!arguments.callee.sRE) {
236     var specials = ['/', '.', '*', '+', '?', '|','(', ')', '[', ']', '{', '}', '\\'];
237     arguments.callee.sRE = new RegExp('(\\' + specials.join('|\\') + ')', 'g');
238   }
239   return text.replace(arguments.callee.sRE, '\\$1');
240 }
241
242 function addAlert(type, name,subtitle, description,time, redirect) {
243         var addIndex = alertList.length;
244         alertList[addIndex]= new Array();
245         alertList[addIndex]['name'] = name;
246         alertList[addIndex]['type'] = type;
247         alertList[addIndex]['subtitle'] = subtitle;
248         alertList[addIndex]['description'] = replaceHTMLChars(description.replace(/<br>/gi, "\n"));
249         alertList[addIndex]['time'] = time;
250         alertList[addIndex]['done'] = 0;
251         alertList[addIndex]['redirect'] = redirect;
252 }
253 function checkAlerts() {
254         secondsSinceLoad += 1;
255         var mj = 0;
256         var alertmsg = '';
257         for(mj = 0 ; mj < alertList.length; mj++) {
258                 if(alertList[mj]['done'] == 0) {
259                         if(alertList[mj]['time'] < secondsSinceLoad && alertList[mj]['time'] > -1 ) {
260                                 alertmsg = alertList[mj]['type'] + ":" + alertList[mj]['name'] + "\n" +alertList[mj]['subtitle']+ "\n"+ alertList[mj]['description'] + "\n\n";
261                                 alertList[mj]['done'] = 1;
262                                 if(alertList[mj]['redirect'] == '') {
263                                         alert(alertmsg);
264                                 }
265                                 else if(confirm(alertmsg)) {
266                                         window.location = alertList[mj]['redirect'];
267                                 }
268                         }
269                 }
270         }
271
272     alertsTimeoutId = setTimeout("checkAlerts()", 1000);
273 }
274
275 function toggleDisplay(id) {
276         if(this.document.getElementById(id).style.display == 'none') {
277                 this.document.getElementById(id).style.display = '';
278                 if(this.document.getElementById(id+"link") != undefined) {
279                         this.document.getElementById(id+"link").style.display = 'none';
280                 }
281                 if(this.document.getElementById(id+"_anchor") != undefined)
282                         this.document.getElementById(id+"_anchor").innerHTML='[ - ]';
283         }
284         else {
285                 this.document.getElementById(id).style.display = 'none';
286                 if(this.document.getElementById(id+"link") != undefined) {
287                         this.document.getElementById(id+"link").style.display = '';
288                 }
289                 if(this.document.getElementById(id+"_anchor") != undefined)
290                         this.document.getElementById(id+"_anchor").innerHTML='[+]';
291         }
292 }
293
294 function checkAll(form, field, value) {
295         for (i = 0; i < form.elements.length; i++) {
296                 if(form.elements[i].name == field)
297                         form.elements[i].checked = value;
298         }
299 }
300
301 function replaceAll(text, src, rep) {
302         offset = text.toLowerCase().indexOf(src.toLowerCase());
303         while(offset != -1) {
304                 text = text.substring(0, offset) + rep + text.substring(offset + src.length ,text.length);
305                 offset = text.indexOf( src, offset + rep.length + 1);
306         }
307         return text;
308 }
309
310 function addForm(formname) {
311         validate[formname] = new Array();
312 }
313
314 function addToValidate(formname, name, type, required, msg) {
315         if(typeof validate[formname] == 'undefined') {
316                 addForm(formname);
317         }
318         validate[formname][validate[formname].length] = new Array(name, type,required, msg);
319 }
320
321 // Bug #47961 Callback validator definition
322 function addToValidateCallback(formname, name, type, required, msg, callback)
323 {
324     addToValidate(formname, name, type, required, msg);
325     var iIndex = validate[formname].length -1;
326     validate[formname][iIndex][jstypeIndex] = 'callback';
327     validate[formname][iIndex][callbackIndex] = callback;
328 }
329
330 function addToValidateRange(formname, name, type,required,  msg,min,max) {
331         addToValidate(formname, name, type,required,  msg);
332         validate[formname][validate[formname].length - 1][jstypeIndex] = 'range';
333         validate[formname][validate[formname].length - 1][minIndex] = min;
334         validate[formname][validate[formname].length - 1][maxIndex] = max;
335 }
336
337 function addToValidateIsValidDate(formname, name, type, required, msg) {
338         addToValidate(formname, name, type, required, msg);
339         validate[formname][validate[formname].length - 1][jstypeIndex] = 'date';
340 }
341
342 function addToValidateIsValidTime(formname, name, type, required, msg) {
343         addToValidate(formname, name, type, required, msg);
344         validate[formname][validate[formname].length - 1][jstypeIndex] = 'time';
345 }
346
347 function addToValidateDateBefore(formname, name, type, required, msg, compareTo) {
348         addToValidate(formname, name, type,required,  msg);
349         validate[formname][validate[formname].length - 1][jstypeIndex] = 'isbefore';
350         validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
351 }
352
353 function addToValidateDateBeforeAllowBlank(formname, name, type, required, msg, compareTo, allowBlank) {
354         addToValidate(formname, name, type,required,  msg);
355         validate[formname][validate[formname].length - 1][jstypeIndex] = 'isbefore';
356         validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
357         validate[formname][validate[formname].length - 1][allowblank] = allowBlank;
358 }
359
360 function addToValidateBinaryDependency(formname, name, type, required, msg, compareTo) {
361         addToValidate(formname, name, type, required, msg);
362         validate[formname][validate[formname].length - 1][jstypeIndex] = 'binarydep';
363         validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
364 }
365
366 function addToValidateComparison(formname, name, type, required, msg, compareTo) {
367         addToValidate(formname, name, type, required, msg);
368         validate[formname][validate[formname].length - 1][jstypeIndex] = 'comparison';
369         validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;
370 }
371
372 function addToValidateIsInArray(formname, name, type, required, msg, arr, operator) {
373         addToValidate(formname, name, type, required, msg);
374         validate[formname][validate[formname].length - 1][jstypeIndex] = 'in_array';
375         validate[formname][validate[formname].length - 1][arrIndex] = arr;
376         validate[formname][validate[formname].length - 1][operatorIndex] = operator;
377 }
378
379 function addToValidateVerified(formname, name, type, required, msg, arr, operator) {
380         addToValidate(formname, name, type, required, msg);
381         validate[formname][validate[formname].length - 1][jstypeIndex] = 'verified';
382 }
383
384 function addToValidateLessThan(formname, name, type, required, msg, max, max_field_msg) {
385         addToValidate(formname, name, type, required, msg);
386         validate[formname][validate[formname].length - 1][jstypeIndex] = 'less';
387     validate[formname][validate[formname].length - 1][maxIndex] = max;
388     validate[formname][validate[formname].length - 1][altMsgIndex] = max_field_msg;
389
390 }
391 function addToValidateMoreThan(formname, name, type, required, msg, min) {
392         addToValidate(formname, name, type, required, msg);
393         validate[formname][validate[formname].length - 1][jstypeIndex] = 'more';
394     validate[formname][validate[formname].length - 1][minIndex] = min;
395 }
396
397
398 function removeFromValidate(formname, name) {
399         for(i = 0; i < validate[formname].length; i++)
400         {
401                 if(validate[formname][i][nameIndex] == name)
402                 {
403                         validate[formname].splice(i--,1); // We subtract 1 from i since the slice removed an element, and we'll skip over the next item we scan
404                 }
405         }
406 }
407 function checkValidate(formname, name) {
408     if(validate[formname]){
409             for(i = 0; i < validate[formname].length; i++){
410                 if(validate[formname][i][nameIndex] == name){
411                     return true;
412                 }
413             }
414         }
415     return false;
416 }
417 var formsWithFieldLogic=null;
418 var formWithPrecision =null;
419 function addToValidateFieldLogic(formId,minFieldId, maxFieldId, defaultFieldId, lenFieldId,type,msg){
420         this.formId = document.getElementById(formId);
421         this.min=document.getElementById(minFieldId);
422         this.max= document.getElementById(maxFieldId);
423         this._default= document.getElementById(defaultFieldId);
424         this.len = document.getElementById(lenFieldId);
425         this.msg = msg;
426         this.type= type;
427 }
428 //@params: formid- Dom id of the form containing the precision and float fields
429 //         valudId- Dom id of the field containing a float whose precision is to be checked.
430 //         precisionId- Dom id of the field containing precision value.
431 function addToValidatePrecision(formId, valueId, precisionId){
432         this.form = document.getElementById(formId);
433         this.float = document.getElementById(valueId);
434         this.precision = document.getElementById(precisionId);
435 }
436
437 //function checkLength(value, referenceValue){
438 //      return value
439 //}
440
441 function isValidPrecision(value, precision){
442         value = trim(value.toString());
443         if(precision == '')
444                 return true;
445         if(value == '')
446             return true;
447         //#27021
448         if( (precision == "0") ){
449                 if (value.indexOf(dec_sep)== -1){
450                         return true;
451                 }else{
452                         return false;
453                 }
454         }
455         //#27021   end
456         if(value.charAt(value.length-precision-1) == num_grp_sep){
457                 if(value.substr(value.indexOf(dec_sep), 1)==dec_sep){
458                         return false;
459                 }
460                 return  true;
461         }
462         var actualPrecision = value.substr(value.indexOf(dec_sep)+1, value.length).length;
463         return actualPrecision == precision;
464 }
465 function toDecimal(original, precision) {
466     precision = (precision == null) ? 2 : precision;
467     num = Math.pow(10, precision);
468         temp = Math.round(original*num)/num;
469         if((temp * 100) % 100 == 0)
470                 return temp + '.00';
471         if((temp * 10) % 10 == 0)
472                 return temp + '0';
473         return temp
474 }
475
476 function isInteger(s) {
477         if(typeof num_grp_sep != 'undefined' && typeof dec_sep != 'undefined')
478         {
479                 s = unformatNumberNoParse(s, num_grp_sep, dec_sep).toString();
480         }
481         return /^[+-]?[0-9]*$/.test(s) ;
482 }
483
484 function isDecimal(s) {
485     if (typeof s == "string" && s == "")    // bug# 46530, this is required in order to
486         return true;                        // not check empty decimal fields
487
488         if(typeof num_grp_sep != 'undefined' && typeof dec_sep != 'undefined')
489         {
490                 s = unformatNumberNoParse(s, num_grp_sep, dec_sep).toString();
491         }
492         return  /^[+-]?[0-9]*\.?[0-9]*$/.test(s) ;
493 }
494
495 function isNumeric(s) {
496         return isDecimal(s);
497 }
498
499 if (typeof date_reg_positions != "object") var date_reg_positions = {'Y': 1,'m': 2,'d': 3};
500 if (typeof date_reg_format != "string") var date_reg_format = '([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})';
501
502 function isDate(dtStr) {
503
504         if(dtStr.length== 0) {
505                 return true;
506         }
507
508     // Check that we have numbers
509         myregexp = new RegExp(date_reg_format)
510         if(!myregexp.test(dtStr))
511                 return false
512
513     m = '';
514     d = '';
515     y = '';
516
517     var dateParts = dtStr.match(date_reg_format);
518     for(key in date_reg_positions) {
519         index = date_reg_positions[key];
520         if(key == 'm') {
521            m = dateParts[index];
522         } else if(key == 'd') {
523            d = dateParts[index];
524         } else {
525            y = dateParts[index];
526         }
527     }
528
529     // Check that date is real
530     var dd = new Date(y,m,0);
531     // reject negative years
532     if (y < 1)
533         return false;
534     // reject month less than 1 and greater than 12
535     if (m > 12 || m < 1)
536         return false;
537     // reject days less than 1 or days not in month (e.g. February 30th)
538     if (d < 1 || d > dd.getDate())
539         return false;
540     return true;
541 }
542
543 function getDateObject(dtStr) {
544         if(dtStr.length== 0) {
545                 return true;
546         }
547
548         myregexp = new RegExp(date_reg_format)
549
550         if(myregexp.exec(dtStr)) var dt = myregexp.exec(dtStr)
551         else return false;
552
553         var yr = dt[date_reg_positions['Y']];
554         var mh = dt[date_reg_positions['m']];
555         var dy = dt[date_reg_positions['d']];
556     var dtar = dtStr.split(' ');
557     if(typeof(dtar[1])!='undefined' && isTime(dtar[1])) {//if it is a timedate, we should make date1 to have time value
558         var t1 = dtar[1].replace(/am/i,' AM');
559         var t1 = t1.replace(/pm/i,' PM');
560         //bug #37977: where time format 23.00 causes java script error
561         t1=t1.replace(/\./, ':');
562         date1 = new Date(Date.parse(mh+'/'+dy+ '/'+yr+' '+t1));
563     }
564     else
565     {
566         var date1 = new Date();
567         date1.setFullYear(yr); // xxxx 4 char year
568         date1.setMonth(mh-1); // 0-11 Bug 4048: javascript Date obj months are 0-index
569         date1.setDate(dy); // 1-31
570     }
571         return date1;
572 }
573
574 function isBefore(value1, value2) {
575         var d1 = getDateObject(value1);
576         var d2 = getDateObject(value2);
577     if(typeof(d2)=='boolean') {// if d2 is not set, we should let it pass, the d2 may not need to be set. the empty check should not be done here.
578         return true;
579     }
580         return d2 >= d1;
581 }
582
583 function isValidEmail(emailStr) {
584
585     if(emailStr.length== 0) {
586                 return true;
587         }
588         // cn: bug 7128, a period at the end of the string mangles checks. (switched to accept spaces and delimiters)
589         var lastChar = emailStr.charAt(emailStr.length - 1);
590         if(!lastChar.match(/[^\.]/i)) {
591                 return false;
592         }
593         //bug 40068, According to rules in page 6 of http://www.apps.ietf.org/rfc/rfc3696.html#sec-3,
594         //first character of local part of an email address
595         //should not be a period i.e. '.'
596
597         var firstLocalChar=emailStr.charAt(0);
598         if(firstLocalChar.match(/\./)){
599                 return false;
600         }
601
602         //bug 40068, According to rules in page 6 of http://www.apps.ietf.org/rfc/rfc3696.html#sec-3,
603         //last character of local part of an email address
604         //should not be a period i.e. '.'
605
606         var pos=emailStr.lastIndexOf("@");
607         var localPart = emailStr.substr(0, pos);
608         var lastLocalChar=localPart.charAt(localPart.length - 1);
609         if(lastLocalChar.match(/\./)){
610                 return false;
611         }
612
613
614         var reg = /@.*?;/g;
615     var results;
616         while ((results = reg.exec(emailStr)) != null) {
617                         var original = results[0];
618                         parsedResult = results[0].replace(';', '::;::');
619                         emailStr = emailStr.replace (original, parsedResult);
620         }
621
622         reg = /.@.*?,/g;
623         while ((results = reg.exec(emailStr)) != null) {
624                         var original = results[0];
625                         //Check if we were using ; as a delimiter. If so, skip the commas
626             if(original.indexOf("::;::") == -1) {
627                 var parsedResult = results[0].replace(',', '::;::');
628                             emailStr = emailStr.replace (original, parsedResult);
629             }
630         }
631
632         // mfh: bug 15010 - more practical implementation of RFC 2822 from http://www.regular-expressions.info/email.html, modifed to accept CAPITAL LETTERS
633         //if(!/[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?/.test(emailStr))
634         //      return false
635
636         //bug 40068, According to rules in page 6 of http://www.apps.ietf.org/rfc/rfc3696.html#sec-3,
637         //allowed special characters ! # $ % & ' * + - / = ?  ^ _ ` . { | } ~ in local part
638     var emailArr = emailStr.split(/::;::/);
639         for (var i = 0; i < emailArr.length; i++) {
640                 var emailAddress = emailArr[i];
641                 if (trim(emailAddress) != '') {
642                         if(!/^\s*[\w.%+\-&'#!\$\*=\?\^_`\{\}~\/]+@([A-Z0-9-]+\.)*[A-Z0-9-]+\.[\w-]{2,}\s*$/i.test(emailAddress) &&
643                            !/^.*<[A-Z0-9._%+\-&'#!\$\*=\?\^_`\{\}~]+?@([A-Z0-9-]+\.)*[A-Z0-9-]+\.[\w-]{2,}>\s*$/i.test(emailAddress)) {
644
645                            return false;
646                         } // if
647                 }
648         } // for
649         return true;
650 }
651
652 function isValidPhone(phoneStr) {
653         if(phoneStr.length== 0) {
654                 return true;
655         }
656         if(!/^[0-9\-\(\)\s]+$/.test(phoneStr))
657                 return false
658         return true
659 }
660 function isFloat(floatStr) {
661         if(floatStr.length== 0) {
662                 return true;
663         }
664         if(!(typeof(num_grp_sep)=='undefined' || typeof(dec_sep)=='undefined')) {
665                 floatStr = unformatNumberNoParse(floatStr, num_grp_sep, dec_sep).toString();
666     }
667
668         return /^(-)?[0-9\.]+$/.test(floatStr);
669 }
670 function isDBName(str) {
671
672         if(str.length== 0) {
673                 return true;
674         }
675         // must start with a letter
676         if(!/^[a-zA-Z][a-zA-Z\_0-9]*$/.test(str))
677                 return false
678         return true
679 }
680 var time_reg_format = "[0-9]{1,2}\:[0-9]{2}";
681 function isTime(timeStr) {
682     var timeRegex = time_reg_format;
683     // eliminate the am/pm from the external time_reg_format
684         timeRegex = timeRegex.replace(/[ ]*\([^)]*m\)/i, '');
685         if(timeStr.length== 0){
686                 return true;
687         }
688         //we now support multiple time formats
689         myregexp = new RegExp(timeRegex);
690         if(!myregexp.test(timeStr)) {
691         return false;
692     }
693
694         return true;
695 }
696
697 function inRange(value, min, max) {
698     if (typeof num_grp_sep != 'undefined' && typeof dec_sep != 'undefined')
699        value = unformatNumberNoParse(value, num_grp_sep, dec_sep).toString();
700     var result = true;
701     if (typeof min == 'number' && value < min)
702     {
703         result = false;
704     }
705     if (typeof max == 'number' && value > max)
706     {
707         result = false;
708     }
709     return result;
710 }
711
712 function bothExist(item1, item2) {
713         if(typeof item1 == 'undefined') { return false; }
714         if(typeof item2 == 'undefined') { return false; }
715         if((item1 == '' && item2 != '') || (item1 != '' && item2 == '') ) { return false; }
716         return true;
717 }
718
719 trim = YAHOO.lang.trim;
720
721
722 function check_form(formname) {
723         if (typeof(siw) != 'undefined' && siw
724                 && typeof(siw.selectingSomething) != 'undefined' && siw.selectingSomething)
725                         return false;
726         return validate_form(formname, '');
727 }
728
729 function add_error_style(formname, input, txt, flash) {
730     var raiseFlag = false;
731         if (typeof flash == "undefined")
732                 flash = true;
733         try {
734         inputHandle = typeof input == "object" ? input : document.forms[formname][input];
735         style = get_current_bgcolor(inputHandle);
736
737         // strip off the colon at the end of the warning strings
738         if ( txt.substring(txt.length-1) == ':' )
739             txt = txt.substring(0,txt.length-1)
740
741         // Bug 28249 - To help avoid duplicate messages for an element, strip off extra messages and
742         // match on the field name itself
743         requiredTxt = SUGAR.language.get('app_strings', 'ERR_MISSING_REQUIRED_FIELDS');
744     invalidTxt = SUGAR.language.get('app_strings', 'ERR_INVALID_VALUE');
745     nomatchTxt = SUGAR.language.get('app_strings', 'ERR_SQS_NO_MATCH_FIELD');
746     matchTxt = txt.replace(requiredTxt,'').replace(invalidTxt,'').replace(nomatchTxt,'');
747
748     YUI().use('node', function (Y) {
749         Y.one(inputHandle).get('parentNode').get('children').each(function(node, index, nodeList){
750             if(node.hasClass('validation-message') && node.get('text').search(matchTxt)){
751                 raiseFlag = true;
752             }
753         });
754     });
755
756     if(!raiseFlag) {
757         errorTextNode = document.createElement('div');
758         errorTextNode.className = 'required validation-message';
759         errorTextNode.innerHTML = txt;
760         if ( inputHandle.parentNode.className.indexOf('x-form-field-wrap') != -1 ) {
761             inputHandle.parentNode.parentNode.appendChild(errorTextNode);
762         }
763         else {
764             inputHandle.parentNode.appendChild(errorTextNode);
765         }
766         if (flash)
767                 inputHandle.style.backgroundColor = "#FF0000";
768         inputsWithErrors.push(inputHandle);
769         }
770     if (flash)
771     {
772                 // We only need to setup the flashy-flashy on the first entry, it loops through all fields automatically
773             if ( inputsWithErrors.length == 1 ) {
774               for(var wp = 1; wp <= 10; wp++) {
775                 window.setTimeout('fade_error_style(style, '+wp*10+')',1000+(wp*50));
776               }
777             }
778                 if(typeof (window[formname + "_tabs"]) != "undefined") {
779                 var tabView = window[formname + "_tabs"];
780                 var parentDiv = YAHOO.util.Dom.getAncestorByTagName(inputHandle, "div");
781                 if ( tabView.get ) {
782                     var tabs = tabView.get("tabs");
783                     for (var i in tabs) {
784                         if (tabs[i].get("contentEl") == parentDiv
785                                         || YAHOO.util.Dom.isAncestor(tabs[i].get("contentEl"), inputHandle))
786                         {
787                             tabs[i].get("labelEl").style.color = "red";
788                             if ( inputsWithErrors.length == 1 )
789                                 tabView.selectTab(i);
790                         }
791                     }
792                 }
793                 }
794                 window.setTimeout("inputsWithErrors[" + (inputsWithErrors.length - 1) + "].style.backgroundColor = '';", 2000);
795     }
796
797   } catch ( e ) {
798       // Catch errors here so we don't allow an incomplete record through the javascript validation
799   }
800 }
801
802
803 /**
804  * removes all error messages for the current form
805  */
806 function clear_all_errors() {
807     for(var wp = 0; wp < inputsWithErrors.length; wp++) {
808         if(typeof(inputsWithErrors[wp]) !='undefined' && typeof inputsWithErrors[wp].parentNode != 'undefined' && inputsWithErrors[wp].parentNode != null) {
809             if ( inputsWithErrors[wp].parentNode.className.indexOf('x-form-field-wrap') != -1 )
810             {
811                 inputsWithErrors[wp].parentNode.parentNode.removeChild(inputsWithErrors[wp].parentNode.parentNode.lastChild);
812             }
813             else
814             {
815                 inputsWithErrors[wp].parentNode.removeChild(inputsWithErrors[wp].parentNode.lastChild);
816             }
817         }
818         }
819         if (inputsWithErrors.length == 0) return;
820
821         if ( YAHOO.util.Dom.getAncestorByTagName(inputsWithErrors[0], "form") ) {
822         var formname = YAHOO.util.Dom.getAncestorByTagName(inputsWithErrors[0], "form").getAttribute("name");
823         if(typeof (window[formname + "_tabs"]) != "undefined") {
824             var tabView = window[formname + "_tabs"];
825             if ( tabView.get ) {
826                 var tabs = tabView.get("tabs");
827                 for (var i in tabs) {
828                     if (typeof tabs[i] == "object")
829                     tabs[i].get("labelEl").style.color = "";
830                 }
831             }
832         }
833         inputsWithErrors = new Array();
834     }
835 }
836
837 function get_current_bgcolor(input) {
838         if(input.currentStyle) {// ie
839                 style = input.currentStyle.backgroundColor;
840                 return style.substring(1,7);
841         }
842         else {// moz
843                 style = '';
844                 styleRGB = document.defaultView.getComputedStyle(input, '').getPropertyValue("background-color");
845                 comma = styleRGB.indexOf(',');
846                 style += dec2hex(styleRGB.substring(4, comma));
847                 commaPrevious = comma;
848                 comma = styleRGB.indexOf(',', commaPrevious+1);
849                 style += dec2hex(styleRGB.substring(commaPrevious+2, comma));
850                 style += dec2hex(styleRGB.substring(comma+2, styleRGB.lastIndexOf(')')));
851                 return style;
852         }
853 }
854
855 function hex2dec(hex){return(parseInt(hex,16));}
856 var hexDigit=new Array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
857 function dec2hex(dec){return(hexDigit[dec>>4]+hexDigit[dec&15]);}
858
859 function fade_error_style(normalStyle, percent) {
860         errorStyle = 'c60c30';
861         var r1 = hex2dec(errorStyle.slice(0,2));
862         var g1 = hex2dec(errorStyle.slice(2,4));
863         var b1 = hex2dec(errorStyle.slice(4,6));
864
865         var r2 = hex2dec(normalStyle.slice(0,2));
866         var g2 = hex2dec(normalStyle.slice(2,4));
867         var b2 = hex2dec(normalStyle.slice(4,6));
868
869
870         var pc = percent / 100;
871
872         r= Math.floor(r1+(pc*(r2-r1)) + .5);
873         g= Math.floor(g1+(pc*(g2-g1)) + .5);
874         b= Math.floor(b1+(pc*(b2-b1)) + .5);
875
876         for(var wp = 0; wp < inputsWithErrors.length; wp++) {
877                 inputsWithErrors[wp].style.backgroundColor = "#" + dec2hex(r) + dec2hex(g) + dec2hex(b);
878         }
879 }
880
881 function isFieldTypeExceptFromEmptyCheck(fieldType)
882 {
883     var results = false;
884     var exemptList = ['bool','file'];
885     for(var i=0;i<exemptList.length;i++)
886     {
887         if(fieldType == exemptList[i])
888             return true;
889     }
890     return results;
891 }
892 function validate_form(formname, startsWith){
893     requiredTxt = SUGAR.language.get('app_strings', 'ERR_MISSING_REQUIRED_FIELDS');
894     invalidTxt = SUGAR.language.get('app_strings', 'ERR_INVALID_VALUE');
895
896         if ( typeof (formname) == 'undefined')
897         {
898                 return false;
899         }
900         if ( typeof (validate[formname]) == 'undefined')
901         {
902         disableOnUnloadEditView(document.forms[formname]);
903                 return true;
904         }
905
906         var form = document.forms[formname];
907         var isError = false;
908         var errorMsg = "";
909         var _date = new Date();
910         if(_date.getTime() < (lastSubmitTime + 2000) && startsWith == oldStartsWith) { // ignore submits for the next 2 seconds
911                 return false;
912         }
913         lastSubmitTime = _date.getTime();
914         oldStartsWith = startsWith;
915
916         clear_all_errors(); // remove previous error messages
917
918         inputsWithErrors = new Array();
919         for(var i = 0; i < validate[formname].length; i++){
920                         if(validate[formname][i][nameIndex].indexOf(startsWith) == 0){
921                                 if(typeof form[validate[formname][i][nameIndex]]  != 'undefined' && typeof form[validate[formname][i][nameIndex]].value != 'undefined'){
922                                         var bail = false;
923
924
925                     //If a field is not required and it is blank or is binarydependant, skip validation.
926                     //Example of binary dependant fields would be the hour/min/meridian dropdowns in a date time combo widget, which require further processing than a blank check
927                     if(!validate[formname][i][requiredIndex] && trim(form[validate[formname][i][nameIndex]].value) == '' && (typeof(validate[formname][i][jstypeIndex]) != 'undefined' && validate[formname][i][jstypeIndex]  != 'binarydep'))
928                     {
929                        continue;
930                     }
931
932                                         if(validate[formname][i][requiredIndex]
933                                                 && !isFieldTypeExceptFromEmptyCheck(validate[formname][i][typeIndex])
934                                         ){
935                                                 if(typeof form[validate[formname][i][nameIndex]] == 'undefined' || trim(form[validate[formname][i][nameIndex]].value) == ""){
936                                                         add_error_style(formname, validate[formname][i][nameIndex], requiredTxt +' ' + validate[formname][i][msgIndex]);
937                                                         isError = true;
938                                                 }
939                                         }
940                                         if(!bail){
941                                                 switch(validate[formname][i][typeIndex]){
942                                                 case 'email':
943                                                         if(!isValidEmail(trim(form[validate[formname][i][nameIndex]].value))){
944                                                                 isError = true;
945                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
946                                                         }
947                                                          break;
948                                                 case 'time':
949                                                         if( !isTime(trim(form[validate[formname][i][nameIndex]].value))){
950                                                                 isError = true;
951                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
952                                                         } break;
953                                                 case 'date': if(!isDate(trim(form[validate[formname][i][nameIndex]].value))){
954                                                                 isError = true;
955                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
956                                                         }  break;
957                                                 case 'alpha':
958                                                         break;
959                                                 case 'DBName':
960                                                         if(!isDBName(trim(form[validate[formname][i][nameIndex]].value))){
961                                                                 isError = true;
962                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
963                                                         }
964                                                         break;
965                         // Bug #49614 : Check value without trimming before
966                                                 case 'DBNameRaw':
967                                                         if(!isDBName(form[validate[formname][i][nameIndex]].value)){
968                                                                 isError = true;
969                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
970                                                         }
971                                                         break;
972                                                 case 'alphanumeric':
973                                                         break;
974                                                 case 'file':
975                                                     var file_input = form[validate[formname][i][nameIndex] + '_file'];
976                             if( file_input && validate[formname][i][requiredIndex] && trim(file_input.value) == "" && !file_input.disabled ) {
977                                                           isError = true;
978                                                           add_error_style(formname, validate[formname][i][nameIndex], requiredTxt + " " +       validate[formname][i][msgIndex]);
979                                                       }
980                                                   break;
981                                                 case 'int':
982                                                         if(!isInteger(trim(form[validate[formname][i][nameIndex]].value))){
983                                                                 isError = true;
984                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
985                                                         }
986                                                         break;
987                                                 case 'decimal':
988                                                         if(!isDecimal(trim(form[validate[formname][i][nameIndex]].value))){
989                                                                 isError = true;
990                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
991                                                         }
992                                                         break;
993                                                 case 'currency':
994                                                 case 'float':
995                                                         if(!isFloat(trim(form[validate[formname][i][nameIndex]].value))){
996                                                                 isError = true;
997                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
998                                                         }
999                                                         break;
1000                                                 case 'teamset_mass':
1001                                                         var div_element_id = formname + '_' + form[validate[formname][i][nameIndex]].name + '_operation_div';
1002                                                         var input_elements = YAHOO.util.Selector.query('input', document.getElementById(div_element_id));
1003                                                         var primary_field_id = '';
1004                                                         var validation_passed = false;
1005                                                         var replace_selected = false;
1006
1007                                                         //Loop through the option elements (replace or add currently)
1008                                                         for(t in input_elements) {
1009                                                                 if(input_elements[t].type && input_elements[t].type == 'radio' && input_elements[t].checked == true && input_elements[t].value == 'replace') {
1010
1011                                                            //Now find where the primary radio button is and if a value has been set
1012                                                            var radio_elements = YAHOO.util.Selector.query('input[type=radio]', document.getElementById(formname + '_team_name_table'));
1013
1014                                                            for(var x = 0; x < radio_elements.length; x++) {
1015                                                                    if(radio_elements[x].name != 'team_name_type') {
1016                                                                           primary_field_id = 'team_name_collection_' + radio_elements[x].value;
1017                                                                           if(radio_elements[x].checked) {
1018                                                                                   replace_selected = true;
1019                                                                                   if(trim(document.forms[formname].elements[primary_field_id].value) != '') {
1020                                                          validation_passed = true;
1021                                                          break;
1022                                                                                       }
1023                                                                           } else if(trim(document.forms[formname].elements[primary_field_id].value) != '') {
1024                                                                                   replace_selected = true;
1025                                                                           }
1026                                                                    }
1027                                                                    }
1028                                                         }
1029                                                         }
1030
1031                                                         if(replace_selected && !validation_passed) {
1032                                                        add_error_style(formname, primary_field_id, SUGAR.language.get('app_strings', 'ERR_NO_PRIMARY_TEAM_SPECIFIED'));
1033                                                        isError = true;
1034                                                         }
1035                                                         break;
1036                                                 case 'teamset':
1037                                                            var table_element_id = formname + '_' + form[validate[formname][i][nameIndex]].name + '_table';
1038                                                            if(document.getElementById(table_element_id)) {
1039                                                                    var input_elements = YAHOO.util.Selector.query('input[type=radio]', document.getElementById(table_element_id));
1040                                                                    var has_primary = false;
1041                                                                    var primary_field_id = form[validate[formname][i][nameIndex]].name + '_collection_0';
1042
1043                                                                    for(t in input_elements) {
1044                                                                             primary_field_id = form[validate[formname][i][nameIndex]].name + '_collection_' + input_elements[t].value;
1045                                                                         if(input_elements[t].type && input_elements[t].type == 'radio' && input_elements[t].checked == true) {
1046                                                                            if(document.forms[formname].elements[primary_field_id].value != '') {
1047                                                                                   has_primary = true;
1048                                                                            }
1049                                                                            break;
1050                                                                         }
1051                                                                    }
1052
1053                                                                    if(!has_primary) {
1054                                                                           isError = true;
1055                                                                           var field_id = form[validate[formname][i][nameIndex]].name + '_collection_' + input_elements[0].value;
1056                                                                           add_error_style(formname, field_id, SUGAR.language.get('app_strings', 'ERR_NO_PRIMARY_TEAM_SPECIFIED'));
1057                                                                    }
1058                                                            }
1059                                                        break;
1060                                             case 'error':
1061                                                         isError = true;
1062                             add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex]);
1063                                                         break;
1064                                                 }
1065
1066                                                 if(typeof validate[formname][i][jstypeIndex]  != 'undefined'/* && !isError*/){
1067
1068                                                         switch(validate[formname][i][jstypeIndex]){
1069                                 // Bug #47961 May be validation through callback is best way.
1070                                 case 'callback' :
1071                                     if (typeof validate[formname][i][callbackIndex] == 'function')
1072                                     {
1073                                         var result = validate[formname][i][callbackIndex](formname, validate[formname][i][nameIndex]);
1074                                         if (result == false)
1075                                         {
1076                                             isError = true;
1077                                             add_error_style(formname, validate[formname][i][nameIndex], requiredTxt + " " +     validate[formname][i][msgIndex]);
1078                                         }
1079                                     }
1080                                     break;
1081                                                         case 'range':
1082                                                                 if(!inRange(trim(form[validate[formname][i][nameIndex]].value), validate[formname][i][minIndex], validate[formname][i][maxIndex])){
1083                                                                         isError = true;
1084                                     var lbl_validate_range = SUGAR.language.get('app_strings', 'LBL_VALIDATE_RANGE');
1085                                     if (typeof validate[formname][i][minIndex] == 'number' && typeof validate[formname][i][maxIndex] == 'number')
1086                                     {
1087                                         add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] + " value " + form[validate[formname][i][nameIndex]].value + " " + lbl_validate_range + " (" +validate[formname][i][minIndex] + " - " + validate[formname][i][maxIndex] +  ")");
1088                                     }
1089                                     else if (typeof validate[formname][i][minIndex] == 'number')
1090                                     {
1091                                         add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] + " " + SUGAR.language.get('app_strings', 'MSG_SHOULD_BE') + ' ' + validate[formname][i][minIndex] + ' ' + SUGAR.language.get('app_strings', 'MSG_OR_GREATER'));
1092                                     }
1093                                     else if (typeof validate[formname][i][maxIndex] == 'number')
1094                                     {
1095                                         add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] + " " + SUGAR.language.get('app_strings', 'MSG_IS_MORE_THAN') + ' ' + validate[formname][i][maxIndex]);
1096                                     }
1097                                                                 }
1098                                                         break;
1099                                                         case 'isbefore':
1100                                                                 compareTo = form[validate[formname][i][compareToIndex]];
1101                                                                 if(     typeof compareTo != 'undefined'){
1102                                                                         if(trim(compareTo.value) != '' || (validate[formname][i][allowblank] != 'true') ) {
1103                                                                                 date2 = trim(compareTo.value);
1104                                                                                 date1 = trim(form[validate[formname][i][nameIndex]].value);
1105
1106                                                                                 if(trim(date1).length != 0 && !isBefore(date1,date2)){
1107
1108                                                                                         isError = true;
1109                                                                                         //jc:#12287 - adding translation for the is not before message
1110                                                                                         add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] + "(" + date1 + ") " + SUGAR.language.get('app_strings', 'MSG_IS_NOT_BEFORE') + ' ' +date2);
1111                                                                                 }
1112                                                                         }
1113                                                                 }
1114                                                         break;
1115                             case 'less':
1116                                 value=unformatNumber(trim(form[validate[formname][i][nameIndex]].value), num_grp_sep, dec_sep);
1117                                                                 maximum = parseFloat(validate[formname][i][maxIndex]);
1118                                                                 if(     typeof maximum != 'undefined'){
1119                                                                         if(value>maximum) {
1120                                         isError = true;
1121                                         add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] +" " +SUGAR.language.get('app_strings', 'MSG_IS_MORE_THAN')+ ' ' + validate[formname][i][altMsgIndex]);
1122                                     }
1123                                                                 }
1124                                                         break;
1125                                                         case 'more':
1126                                 value=unformatNumber(trim(form[validate[formname][i][nameIndex]].value), num_grp_sep, dec_sep);
1127                                                                 minimum = parseFloat(validate[formname][i][minIndex]);
1128                                                                 if(     typeof minimum != 'undefined'){
1129                                                                         if(value<minimum) {
1130                                         isError = true;
1131                                         add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex] +" " +SUGAR.language.get('app_strings', 'MSG_SHOULD_BE')+ ' ' + minimum + ' ' + SUGAR.language.get('app_strings', 'MSG_OR_GREATER'));
1132                                     }
1133                                                                 }
1134                                                         break;
1135                             case 'binarydep':
1136                                                                 compareTo = form[validate[formname][i][compareToIndex]];
1137                                                                 if( typeof compareTo != 'undefined') {
1138                                                                         item1 = trim(form[validate[formname][i][nameIndex]].value);
1139                                                                         item2 = trim(compareTo.value);
1140                                                                         if(!bothExist(item1, item2)) {
1141                                                                                 isError = true;
1142                                                                                 add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex]);
1143                                                                         }
1144                                                                 }
1145                                                         break;
1146                                                         case 'comparison':
1147                                                                 compareTo = form[validate[formname][i][compareToIndex]];
1148                                                                 if( typeof compareTo != 'undefined') {
1149                                                                         item1 = trim(form[validate[formname][i][nameIndex]].value);
1150                                                                         item2 = trim(compareTo.value);
1151                                                                         if(!bothExist(item1, item2) || item1 != item2) {
1152                                                                                 isError = true;
1153                                                                                 add_error_style(formname, validate[formname][i][nameIndex], validate[formname][i][msgIndex]);
1154                                                                         }
1155                                                                 }
1156                                                         break;
1157                                                         case 'in_array':
1158                                                                 arr = eval(validate[formname][i][arrIndex]);
1159                                                                 operator = validate[formname][i][operatorIndex];
1160                                                                 item1 = trim(form[validate[formname][i][nameIndex]].value);
1161                                                                 if (operator.charAt(0) == 'u') {
1162                                                                         item1 = item1.toUpperCase();
1163                                                                         operator = operator.substring(1);
1164                                                                 } else if (operator.charAt(0) == 'l') {
1165                                                                         item1 = item1.toLowerCase();
1166                                                                         operator = operator.substring(1);
1167                                                                 }
1168                                                                 for(j = 0; j < arr.length; j++){
1169                                                                         val = arr[j];
1170                                                                         if((operator == "==" && val == item1) || (operator == "!=" && val != item1)){
1171                                                                                 isError = true;
1172                                                                                 add_error_style(formname, validate[formname][i][nameIndex], invalidTxt + " " +  validate[formname][i][msgIndex]);
1173                                                                         }
1174                                                                 }
1175                                                         break;
1176                                                         case 'verified':
1177                                                         if(trim(form[validate[formname][i][nameIndex]].value) == 'false'){
1178                                                            //Fake an error so form does not submit
1179                                                            isError = true;
1180                                                         }
1181                                                         break;
1182                                                         }
1183                                                 }
1184                                         }
1185                                 }
1186                         }
1187                 }
1188 /*      nsingh: BUG#15102
1189         Check min max default field logic.
1190         Can work with float values as well, but as of 10/8/07 decimal values in MB and studio don't have min and max value constraints.*/
1191         if(formsWithFieldLogic){
1192                 var invalidLogic=false;
1193                 if(formsWithFieldLogic.min && formsWithFieldLogic.max && formsWithFieldLogic._default) {
1194                         var showErrorsOn={min:{value:'min', show:false, obj:formsWithFieldLogic.min.value},
1195                                                         max:{value:'max',show:false, obj:formsWithFieldLogic.max.value},
1196                                                         _default:{value:'default',show:false, obj:formsWithFieldLogic._default.value},
1197                               len:{value:'len', show:false, obj:parseInt(formsWithFieldLogic.len.value,10)}};
1198
1199                         var min = (formsWithFieldLogic.min.value !='') ? parseFloat(formsWithFieldLogic.min.value) : 'undef';
1200                         var max  = (formsWithFieldLogic.max.value !='') ? parseFloat(formsWithFieldLogic.max.value) : 'undef';
1201                         var _default = (formsWithFieldLogic._default.value!='')? parseFloat(formsWithFieldLogic._default.value) : 'undef';
1202
1203                         /*Check all lengths are <= max size.*/
1204                         for(var i in showErrorsOn){
1205                                 if(showErrorsOn[i].value!='len' && showErrorsOn[i].obj.length > showErrorsOn.len.obj){
1206                                         invalidLogic=true;
1207                                         showErrorsOn[i].show=true;
1208                                         showErrorsOn.len.show=true;
1209                                 }
1210                         }
1211
1212                         if(min!='undef' && max!='undef' && _default!='undef'){
1213                                 if(!inRange(_default,min,max)){
1214                                         invalidLogic=true;
1215                                         showErrorsOn.min.show=true;
1216                                         showErrorsOn.max.show=true;
1217                                         showErrorsOn._default.show=true;
1218                                 }
1219                         }
1220                         if(min!='undef' && max!= 'undef' && min > max){
1221                                 invalidLogic = true;
1222                                 showErrorsOn.min.show=true;
1223                                 showErrorsOn.max.show=true;
1224                         }
1225                         if(min!='undef' && _default!='undef' && _default < min){
1226
1227                                 invalidLogic = true;
1228                                 showErrorsOn.min.show=true;
1229                                 showErrorsOn._default.show=true;
1230                         }
1231                         if(max!='undef' && _default !='undef' && _default>max){
1232
1233                                 invalidLogic = true;
1234                                 showErrorsOn.max.show=true;
1235                                 showErrorsOn._default.show=true;
1236                         }
1237
1238                         if(invalidLogic){
1239                                 isError=true;
1240                                 for(var error in showErrorsOn)
1241                                         if(showErrorsOn[error].show)
1242                                                 add_error_style(formname,showErrorsOn[error].value, formsWithFieldLogic.msg);
1243
1244                         }
1245
1246                         else if (!isError)
1247                                 formsWithFieldLogic = null;
1248                 }
1249         }
1250         if(formWithPrecision){
1251                 if (!isValidPrecision(formWithPrecision.float.value, formWithPrecision.precision.value)){
1252                         isError = true;
1253                         add_error_style(formname, 'default', SUGAR.language.get('app_strings', 'ERR_COMPATIBLE_PRECISION_VALUE'));
1254                 }else if(!isError){
1255                         isError = false;
1256                 }
1257         }
1258
1259 //END BUG# 15102
1260
1261         if (isError == true) {
1262                 var nw, ne, sw, se;
1263                 if (self.pageYOffset) // all except Explorer
1264                 {
1265                         nwX = self.pageXOffset;
1266                         seX = self.innerWidth;
1267                         nwY = self.pageYOffset;
1268                         seY = self.innerHeight;
1269                 }
1270                 else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
1271                 {
1272                         nwX = document.documentElement.scrollLeft;
1273                         seX = document.documentElement.clientWidth;
1274                         nwY = document.documentElement.scrollTop;
1275                         seY = document.documentElement.clientHeight;
1276                 }
1277                 else if (document.body) // all other Explorers
1278                 {
1279                         nwX = document.body.scrollLeft;
1280                         seX = document.body.clientWidth;
1281                         nwY = document.body.scrollTop;
1282                         seY = document.body.clientHeight;
1283                 }
1284
1285                 var inView = true; // is there an error within viewport of browser
1286                 for(var wp = 0; wp < inputsWithErrors.length; wp++) {
1287                         var elementCoor = findElementPos(inputsWithErrors[wp]);
1288                         if(!(elementCoor.x >= nwX && elementCoor.y >= nwY &&
1289                 elementCoor.x <= seX+nwX && elementCoor.y <= seY+nwY)) { // if input is not within viewport, modify for SI bug 52497
1290                                         inView = false;
1291                                         scrollToTop = elementCoor.y - 75;
1292                                         scrollToLeft = elementCoor.x - 75;
1293                         }
1294                         else { // on first input within viewport, don't scroll
1295                                 break;
1296                         }
1297                 }
1298
1299
1300                 if(!inView) window.scrollTo(scrollToLeft,scrollToTop);
1301
1302                 return false;
1303         }
1304
1305     disableOnUnloadEditView(form);
1306         return true;
1307
1308 }
1309
1310
1311 /**
1312  * This array is used to remember mark status of rows in browse mode
1313  */
1314 var marked_row = new Array;
1315
1316
1317 /**
1318  * Sets/unsets the pointer and marker in browse mode
1319  *
1320  * @param   object    the table row
1321  * @param   interger  the row number
1322  * @param   string    the action calling this script (over, out or click)
1323  * @param   string    the default background color
1324  * @param   string    the color to use for mouseover
1325  * @param   string    the color to use for marking a row
1326  *
1327  * @return  boolean  whether pointer is set or not
1328  */
1329 function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor) {
1330     var theCells = null;
1331
1332     // 1. Pointer and mark feature are disabled or the browser can't get the
1333     //    row -> exits
1334     if ((thePointerColor == '' && theMarkColor == '')
1335         || typeof(theRow.style) == 'undefined') {
1336         return false;
1337     }
1338
1339     // 2. Gets the current row and exits if the browser can't get it
1340     if (typeof(document.getElementsByTagName) != 'undefined') {
1341         theCells = theRow.getElementsByTagName('td');
1342     }
1343     else if (typeof(theRow.cells) != 'undefined') {
1344         theCells = theRow.cells;
1345     }
1346     else {
1347         return false;
1348     }
1349
1350     // 3. Gets the current color...
1351     var rowCellsCnt  = theCells.length;
1352     var domDetect    = null;
1353     var currentColor = null;
1354     var newColor     = null;
1355     // 3.1 ... with DOM compatible browsers except Opera that does not return
1356     //         valid values with "getAttribute"
1357     if (typeof(window.opera) == 'undefined'
1358         && typeof(theCells[0].getAttribute) != 'undefined') {
1359         currentColor = theCells[0].getAttribute('bgcolor');
1360         domDetect    = true;
1361     }
1362     // 3.2 ... with other browsers
1363     else {
1364         currentColor = theCells[0].style.backgroundColor;
1365         domDetect    = false;
1366     } // end 3
1367
1368     // 4. Defines the new color
1369     // 4.1 Current color is the default one
1370     if (currentColor == ''
1371         || (currentColor!= null && (currentColor.toLowerCase() == theDefaultColor.toLowerCase()))) {
1372         if (theAction == 'over' && thePointerColor != '') {
1373             newColor              = thePointerColor;
1374         }
1375         else if (theAction == 'click' && theMarkColor != '') {
1376             newColor              = theMarkColor;
1377             marked_row[theRowNum] = true;
1378         }
1379     }
1380     // 4.1.2 Current color is the pointer one
1381     else if (currentColor!= null && (currentColor.toLowerCase() == thePointerColor.toLowerCase())
1382              && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
1383         if (theAction == 'out') {
1384             newColor              = theDefaultColor;
1385         }
1386         else if (theAction == 'click' && theMarkColor != '') {
1387             newColor              = theMarkColor;
1388             marked_row[theRowNum] = true;
1389         }
1390     }
1391     // 4.1.3 Current color is the marker one
1392     else if (currentColor!= null && (currentColor.toLowerCase() == theMarkColor.toLowerCase())) {
1393         if (theAction == 'click') {
1394             newColor              = (thePointerColor != '')
1395                                   ? thePointerColor
1396                                   : theDefaultColor;
1397             marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
1398                                   ? true
1399                                   : null;
1400         }
1401     } // end 4
1402
1403     // 5. Sets the new color...
1404     if (newColor) {
1405         var c = null;
1406         // 5.1 ... with DOM compatible browsers except Opera
1407         if (domDetect) {
1408             for (c = 0; c < rowCellsCnt; c++) {
1409                 theCells[c].setAttribute('bgcolor', newColor, 0);
1410             } // end for
1411         }
1412         // 5.2 ... with other browsers
1413         else {
1414             for (c = 0; c < rowCellsCnt; c++) {
1415                 theCells[c].style.backgroundColor = newColor;
1416             }
1417         }
1418     } // end 5
1419
1420     return true;
1421 } // end of the 'setPointer()' function
1422
1423
1424 /**
1425   * listbox redirection
1426   */
1427 function goToUrl(selObj, goToLocation) {
1428     eval("document.location.href = '" + goToLocation + "pos=" + selObj.options[selObj.selectedIndex].value + "'");
1429 }
1430
1431
1432
1433 var json_objects = new Object();
1434
1435 function getXMLHTTPinstance() {
1436         var xmlhttp = false;
1437         var userAgent = navigator.userAgent.toLowerCase() ;
1438
1439         // IE Check supports ActiveX controls
1440         if (userAgent.indexOf("msie") != -1 && userAgent.indexOf("mac") == -1 && userAgent.indexOf("opera") == -1) {
1441                 var version = navigator.appVersion.match(/MSIE (\d+\.\d+)/)[1] ;
1442                 if(version >= 5.5 ) {
1443                         try {
1444                                 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
1445                         }
1446                         catch (e) {
1447                                 try {
1448                                         xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
1449                                 }
1450                                 catch (E) {
1451                                         xmlhttp = false;
1452                                 }
1453                         }
1454                 }
1455         }
1456
1457         if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
1458                 xmlhttp = new XMLHttpRequest();
1459         }
1460         return xmlhttp;
1461 }
1462
1463 // NOW LOAD THE OBJECT..
1464 var global_xmlhttp = getXMLHTTPinstance();
1465
1466 function http_fetch_sync(url,post_data) {
1467         global_xmlhttp = getXMLHTTPinstance();
1468         var method = 'GET';
1469
1470         if(typeof(post_data) != 'undefined') method = 'POST';
1471         try {
1472                 global_xmlhttp.open(method, url,false);
1473         }
1474         catch(e) {
1475                 alert('message:'+e.message+":url:"+url);
1476         }
1477         if(method == 'POST') {
1478                 global_xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
1479         }
1480
1481         global_xmlhttp.send(post_data);
1482
1483         if (SUGAR.util.isLoginPage(global_xmlhttp.responseText))
1484                 return false;
1485
1486         var args = {"responseText" : global_xmlhttp.responseText,
1487                                 "responseXML" : global_xmlhttp.responseXML,
1488                                 "request_id" : typeof(request_id) != "undefined" ? request_id : 0};
1489         return args;
1490
1491 }
1492 // this is a GET unless post_data is defined
1493
1494 function http_fetch_async(url,callback,request_id,post_data) {
1495         var method = 'GET';
1496         if(typeof(post_data) != 'undefined') {
1497                 method = 'POST';
1498         }
1499
1500         try {
1501                 global_xmlhttp.open(method, url,true);
1502         }
1503         catch(e) {
1504                 alert('message:'+e.message+":url:"+url);
1505         }
1506         if(method == 'POST') {
1507                 global_xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
1508         }
1509         global_xmlhttp.onreadystatechange = function() {
1510                 if(global_xmlhttp.readyState==4) {
1511                         if(global_xmlhttp.status == 200) {
1512                                 if (SUGAR.util.isLoginPage(global_xmlhttp.responseText))
1513                                         return false;
1514                                 var args = {"responseText" : global_xmlhttp.responseText,
1515                                                         "responseXML" : global_xmlhttp.responseXML,
1516                                                         "request_id" : request_id };
1517                                 callback.call(document,args);
1518                         }
1519                         else {
1520                                 alert("There was a problem retrieving the XML data:\n" + global_xmlhttp.statusText);
1521                         }
1522                 }
1523         }
1524         global_xmlhttp.send(post_data);
1525 }
1526
1527 function insert_at_cursor(field, value) {
1528  //ie:
1529         if (document.selection) {
1530                 field.focus();
1531                 sel = document.selection.createRange();
1532                 sel.text = value;
1533         }
1534  //mozilla:
1535         else if(field.selectionStart || field.selectionStart == '0') {
1536                 var start_pos = field.selectionStart;
1537                 var end_pos = field.selectionEnd;
1538                 field.value = field.value.substring(0, start_pos) + value + field.value.substring(end_pos, field.value.length);
1539         }
1540         else {
1541                 field.value += value;
1542         }
1543 }
1544
1545 function checkParentType(type,button) {
1546         if(button == null) {
1547                 return;
1548         }
1549         if(typeof disabledModules != 'undefined' && typeof(disabledModules[type]) != 'undefined') {
1550                 button.disabled='disabled';
1551         }
1552         else {
1553                 button.disabled = false;
1554         }
1555 }
1556
1557 function parseDate(input, format) {
1558         date = input.value;
1559         format = format.replace(/%/g, '');
1560         sep = format.charAt(1);
1561         yAt = format.indexOf('Y')
1562         // 1-1-06 or 1-12-06 or 1-1-2006 or 1-12-2006
1563         if(date.match(/^\d{1,2}[\/-]\d{1,2}[\/-]\d{2,4}$/) && yAt == 4) {
1564                 if(date.match(/^\d{1}[\/-].*$/)) date = '0' + date;
1565                 if(date.match(/^\d{2}[\/-]\d{1}[\/-].*$/)) date = date.substring(0,3) + '0' + date.substring(3,date.length);
1566                 if(date.match(/^\d{2}[\/-]\d{2}[\/-]\d{2}$/)) date = date.substring(0,6) + '20' + date.substring(6,date.length);
1567         }
1568         // 06-11-1 or 06-1-1
1569         else if(date.match(/^\d{2,4}[\/-]\d{1,2}[\/-]\d{1,2}$/)) {
1570                 if(date.match(/^\d{2}[\/-].*$/)) date = '20' + date;
1571                 if(date.match(/^\d{4}[\/-]\d{1}[\/-].*$/)) date = date.substring(0,5) + '0' + date.substring(5,date.length);
1572                 if(date.match(/^\d{4}[\/-]\d{2}[\/-]\d{1}$/)) date = date.substring(0,8) + '0' + date.substring(8,date.length);
1573         }
1574         else if(date.match(/^\d{4,8}$/)) { // digits only
1575                 digits = 0;
1576                 if(date.match(/^\d{8}$/)) digits = 8;// match for 8 digits
1577                 else if(date.match(/\d{6}/)) digits = 6;// match for 5 digits
1578                 else if(date.match(/\d{4}/)) digits = 4;// match for 5 digits
1579                 else if(date.match(/\d{5}/)) digits = 5;// match for 5 digits
1580
1581                 switch(yAt) {
1582                         case 0:
1583                                 switch(digits) {
1584                                         case 4: date = '20' + date.substring(0,2) + sep + '0' + date.substring(2, 3) + sep + '0' + date.substring(3,4); break;
1585                                         case 5: date = '20' + date.substring(0,2) + sep + date.substring(2, 4) + sep + '0' + date.substring(4,5); break;
1586                                         case 6: date = '20' + date.substring(0,2) + sep + date.substring(2, 4) + sep + date.substring(4,6); break;
1587                                         case 8: date = date.substring(0,4) + sep + date.substring(4, 6) + sep + date.substring(6,8); break;
1588                                 }
1589                                 break;
1590                         case 2:
1591                                 switch(digits) {
1592                                         case 4: date = '0' + date.substring(0,1) + sep + '20' + date.substring(1, 3) + sep + '0' + date.substring(3,4); break;
1593                                         case 5: date = date.substring(0,2) + sep + '20' + date.substring(2, 4) + sep + '0' + date.substring(4,5); break;
1594                                         case 6: date = date.substring(0,2) + sep + '20' + date.substring(2, 4) + sep + date.substring(4,6); break;
1595                                         case 8: date = date.substring(0,2) + sep + date.substring(2, 6) + sep + date.substring(6,8); break;
1596                                 }
1597                         case 4:
1598                                 switch(digits) {
1599                                         case 4: date = '0' + date.substring(0,1) + sep + '0' + date.substring(1, 2) + sep + '20' + date.substring(2,4); break;
1600                                         case 5: date = '0' + date.substring(0,1) + sep + date.substring(1, 3) + sep + '20' + date.substring(3,5); break;
1601                                         case 6: date = date.substring(0,2) + sep + date.substring(2, 4) + sep + '20' + date.substring(4,6); break;
1602                                         case 8: date = date.substring(0,2) + sep + date.substring(2, 4) + sep + date.substring(4,8); break;
1603                                 }
1604                                 break;
1605                 }
1606         }
1607         date = date.replace(/[\/-]/g, sep);
1608         input.value = date;
1609 }
1610
1611 // find obj's position
1612 function findElementPos(obj) {
1613     var x = 0;
1614     var y = 0;
1615     if (obj.offsetParent) {
1616       while (obj.offsetParent) {
1617         x += obj.offsetLeft;
1618         y += obj.offsetTop;
1619         obj = obj.offsetParent;
1620       }
1621     }//if offsetParent exists
1622     else if (obj.x && obj.y) {
1623       y += obj.y
1624       x += obj.x
1625     }
1626         return new coordinate(x, y);
1627 }//findElementPos
1628
1629
1630 // get dimensions of the browser window
1631 function getClientDim() {
1632         var nwX, nwY, seX, seY;
1633         if (self.pageYOffset) // all except Explorer
1634         {
1635           nwX = self.pageXOffset;
1636           seX = self.innerWidth + nwX;
1637           nwY = self.pageYOffset;
1638           seY = self.innerHeight + nwY;
1639         }
1640         else if (document.documentElement && document.documentElement.scrollTop) // Explorer 6 Strict
1641         {
1642           nwX = document.documentElement.scrollLeft;
1643           seX = document.documentElement.clientWidth + nwX;
1644           nwY = document.documentElement.scrollTop;
1645           seY = document.documentElement.clientHeight + nwY;
1646         }
1647         else if (document.body) // all other Explorers
1648         {
1649           nwX = document.body.scrollLeft;
1650           seX = document.body.clientWidth + nwX;
1651           nwY = document.body.scrollTop;
1652           seY = document.body.clientHeight + nwY;
1653         }
1654         return {'nw' : new coordinate(nwX, nwY), 'se' : new coordinate(seX, seY)};
1655 }
1656
1657 /**
1658 * stop propagation on events
1659 **/
1660 function freezeEvent(e) {
1661         if(e) {
1662           if (e.preventDefault) e.preventDefault();
1663           e.returnValue = false;
1664           e.cancelBubble = true;
1665           if (e.stopPropagation) e.stopPropagation();
1666           return false;
1667         }
1668 }
1669
1670
1671 /**
1672  * coordinate class
1673  **/
1674 function coordinate(_x, _y) {
1675   var x = _x;
1676   var y = _y;
1677   this.add = add;
1678   this.sub = sub;
1679   this.x = x;
1680   this.y = y;
1681
1682   function add(rh) {
1683     return new position(this.x + rh.x, this.y + rh.y);
1684   }
1685
1686   function sub(rh) {
1687     return new position(this.x + rh.x, this.y + rh.y);
1688   }
1689 }
1690
1691 // sends theForm via AJAX and fills in the theDiv
1692 function sendAndRetrieve(theForm, theDiv, loadingStr) {
1693         function success(data) {
1694                 document.getElementById(theDiv).innerHTML = data.responseText;
1695                 ajaxStatus.hideStatus();
1696         }
1697         if(typeof loadingStr == 'undefined') SUGAR.language.get('app_strings', 'LBL_LOADING');
1698         ajaxStatus.showStatus(loadingStr);
1699     oForm = new YAHOO.util.Element(theForm);
1700     if ( oForm.get('enctype') && oForm.get('enctype') == 'multipart/form-data' ) {
1701         // the second argument is true to indicate file upload.
1702         YAHOO.util.Connect.setForm(theForm, true);
1703         var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', {upload: success, failure: success});
1704     } else {
1705         YAHOO.util.Connect.setForm(theForm);
1706         var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', {success: success, failure: success});
1707     }
1708         return false;
1709 }
1710
1711 //save the form and redirect
1712 function sendAndRedirect(theForm, loadingStr, redirect_location) {
1713         function success(data) {
1714                 if(redirect_location){
1715                         location.href=redirect_location;
1716                 }
1717                 ajaxStatus.hideStatus();
1718         }
1719         if(typeof loadingStr == 'undefined') SUGAR.language.get('app_strings', 'LBL_LOADING');
1720         ajaxStatus.showStatus(loadingStr);
1721     oForm = new YAHOO.util.Element(theForm);
1722     if ( oForm.get('enctype') && oForm.get('enctype') == 'multipart/form-data' ) {
1723         // the second argument is true to indicate file upload.
1724         YAHOO.util.Connect.setForm(theForm, true);
1725         var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', {upload: success, failure: success});
1726     } else {
1727         YAHOO.util.Connect.setForm(theForm);
1728         var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', {success: success, failure: success});
1729     }
1730         return false;
1731 }
1732
1733 function saveForm(theForm, theDiv, loadingStr) {
1734         if(check_form(theForm)){
1735                 for(i = 0; i < ajaxFormArray.length; i++){
1736                         if(ajaxFormArray[i] == theForm){
1737                                 ajaxFormArray.splice(i, 1);
1738                         }
1739                 }
1740                 return sendAndRetrieve(theForm, loadingStr, theDiv);
1741         }
1742         else
1743                 return false;
1744 }
1745
1746 // Builds a "snapshot" of the form, so we can use it to see if someone has changed it.
1747 function snapshotForm(theForm) {
1748     var snapshotTxt = '';
1749     var elemList = theForm.elements;
1750     var elem;
1751     var elemType;
1752
1753     for( var i = 0; i < elemList.length ; i++ ) {
1754         elem = elemList[i];
1755         if ( typeof(elem.type) == 'undefined' ) {
1756             continue;
1757         }
1758
1759         elemType = elem.type.toLowerCase();
1760
1761         snapshotTxt = snapshotTxt + elem.name;
1762
1763         if ( elemType == 'text' || elemType == 'textarea' || elemType == 'password' ) {
1764             snapshotTxt = snapshotTxt + elem.value;
1765         }
1766         else if ( elemType == 'select' || elemType == 'select-one' || elemType == 'select-multiple' ) {
1767             var optionList = elem.options;
1768             for ( var ii = 0 ; ii < optionList.length ; ii++ ) {
1769                 if ( optionList[ii].selected ) {
1770                     snapshotTxt = snapshotTxt + optionList[ii].value;
1771                 }
1772             }
1773         }
1774         else if ( elemType == 'radio' || elemType == 'checkbox' ) {
1775             if ( elem.selected ) {
1776                 snapshotTxt = snapshotTxt + 'checked';
1777             }
1778         }
1779         else if ( elemType == 'hidden' ) {
1780             snapshotTxt = snapshotTxt + elem.value;
1781         }
1782     }
1783
1784     return snapshotTxt;
1785 }
1786
1787 function initEditView(theForm) {
1788     if (SUGAR.util.ajaxCallInProgress()) {
1789         window.setTimeout(function(){initEditView(theForm);}, 100);
1790         return;
1791     }
1792
1793     if ( theForm == null || theForm.id == null ) {
1794         // Not much we can do here.
1795         return;
1796     }
1797
1798     // we don't need to check if the data is changed in the search popup
1799     if (theForm.id == 'popup_query_form' || theForm.id == 'MassUpdate') {
1800         return;
1801     }
1802         if ( typeof editViewSnapshots == 'undefined' || editViewSnapshots == null ) {
1803         editViewSnapshots = new Object();
1804     }
1805     if ( typeof SUGAR.loadedForms == 'undefined' || SUGAR.loadedForms == null) {
1806         SUGAR.loadedForms = new Object();
1807     }
1808
1809     editViewSnapshots[theForm.id] = snapshotForm(theForm);
1810     SUGAR.loadedForms[theForm.id] = true;
1811 }
1812
1813 function onUnloadEditView(theForm) {
1814
1815         var dataHasChanged = false;
1816     if ( typeof editViewSnapshots == 'undefined' ) {
1817         // No snapshots, move along
1818         return;
1819     }
1820
1821     if ( typeof theForm == 'undefined' ) {
1822         // Need to check all editViewSnapshots
1823         for ( var idx in editViewSnapshots ) {
1824
1825             theForm = document.getElementById(idx);
1826             // console.log('DEBUG: Checking all forms '+theForm.id);
1827             if ( theForm == null
1828                  || typeof editViewSnapshots[theForm.id] == 'undefined'
1829                  || editViewSnapshots[theForm.id] == null
1830                  || !SUGAR.loadedForms[theForm.id]) {
1831                 continue;
1832             }
1833
1834             var snap = snapshotForm(theForm);
1835             if ( editViewSnapshots[theForm.id] != snap ) {
1836                 dataHasChanged = true;
1837             }
1838         }
1839     } else {
1840         // Just need to check a single form for changes
1841                 if ( editViewSnapshots == null  || typeof theForm.id == 'undefined' || typeof editViewSnapshots[theForm.id] == 'undefined' || editViewSnapshots[theForm.id] == null ) {
1842             return;
1843         }
1844
1845         // console.log('DEBUG: Checking one form '+theForm.id);
1846         if ( editViewSnapshots[theForm.id] != snapshotForm(theForm) ) {
1847             // Data has changed.
1848                 dataHasChanged = true;
1849         }
1850     }
1851
1852     if ( dataHasChanged == true ) {
1853         return SUGAR.language.get('app_strings','WARN_UNSAVED_CHANGES');
1854     } else {
1855         return;
1856     }
1857
1858 }
1859
1860 function disableOnUnloadEditView(theForm) {
1861     // If you don't pass anything in, it disables all checking
1862     if ( typeof theForm == 'undefined' || typeof editViewSnapshots == 'undefined' || theForm == null || editViewSnapshots == null) {
1863         window.onbeforeunload = null;
1864         editViewSnapshots = null;
1865
1866         // console.log('DEBUG: Disabling all edit view checks');
1867
1868     } else {
1869         // Otherwise, it just disables it for this form
1870         if ( typeof(theForm.id) != 'undefined' && typeof(editViewSnapshots[theForm.id]) != 'undefined' ) {
1871             editViewSnapshots[theForm.id] = null;
1872         }
1873
1874         // console.log('DEBUG : Disabling just checks for '+theForm.id);
1875
1876     }
1877 }
1878
1879 /*
1880 * save some forms using an ajax call
1881 * theForms - the ids of all of theh forms to save
1882 * savingStr - the string to display when saving the form
1883 * completeStr - the string to display when the form has been saved
1884 */
1885 function saveForms( savingStr, completeStr) {
1886         index = 0;
1887         theForms = ajaxFormArray;
1888         function success(data) {
1889                 var theForm = document.getElementById(ajaxFormArray[0]);
1890                 document.getElementById('multiedit_'+theForm.id).innerHTML = data.responseText;
1891                 var saveAllButton = document.getElementById('ajaxsaveall');
1892                 ajaxFormArray.splice(index, 1);
1893                 if(saveAllButton && ajaxFormArray.length <= 1){
1894                 saveAllButton.style.visibility = 'hidden';
1895         }
1896                 index++;
1897                 if(index == theForms.length){
1898                         ajaxStatus.showStatus(completeStr);
1899                 window.setTimeout('ajaxStatus.hideStatus();', 2000);
1900                 if(saveAllButton)
1901                         saveAllButton.style.visibility = 'hidden';
1902         }
1903
1904
1905         }
1906         if(typeof savingStr == 'undefined') SUGAR.language.get('app_strings', 'LBL_LOADING');
1907         ajaxStatus.showStatus(savingStr);
1908
1909         //loop through the forms saving each one
1910         for(i = 0; i < theForms.length; i++){
1911                 var theForm = document.getElementById(theForms[i]);
1912                 if(check_form(theForm.id)){
1913                         theForm.action.value='AjaxFormSave';
1914                         YAHOO.util.Connect.setForm(theForm);
1915                         var cObj = YAHOO.util.Connect.asyncRequest('POST', 'index.php', {success: success, failure: success});
1916                 }else{
1917                         ajaxStatus.hideStatus();
1918                 }
1919                 lastSubmitTime = lastSubmitTime-2000;
1920         }
1921         return false;
1922 }
1923
1924 // -- start sugarListView class
1925 // js functions used for ListView
1926 function sugarListView() {
1927 }
1928
1929
1930 sugarListView.prototype.confirm_action = function(del) {
1931         if (del == 1) {
1932                 return confirm( SUGAR.language.get('app_strings', 'NTC_DELETE_CONFIRMATION_NUM') + sugarListView.get_num_selected()  + SUGAR.language.get('app_strings', 'NTC_DELETE_SELECTED_RECORDS'));
1933         }
1934         else {
1935                 return confirm( SUGAR.language.get('app_strings', 'NTC_UPDATE_CONFIRMATION_NUM') + sugarListView.get_num_selected()  + SUGAR.language.get('app_strings', 'NTC_DELETE_SELECTED_RECORDS'));
1936         }
1937
1938 }
1939 sugarListView.get_num_selected = function () {
1940     var selectCount = $("input[name='selectCount[]']:first");
1941     if(selectCount.length > 0)
1942         return parseInt(selectCount.val().replace("+", ""));
1943     return 0;
1944
1945 }
1946 sugarListView.update_count = function(count, add) {
1947         if(typeof document.MassUpdate != 'undefined') {
1948                 the_form = document.MassUpdate;
1949                 for(var wp = 0; wp < the_form.elements.length; wp++) {
1950                         if(typeof the_form.elements[wp].name != 'undefined' && the_form.elements[wp].name == 'selectCount[]') {
1951                                 if(add) {
1952                                         the_form.elements[wp].value = parseInt(the_form.elements[wp].value,10) + count;
1953                                         if (the_form.select_entire_list.value == 1 && the_form.show_plus.value) {
1954                                                 the_form.elements[wp].value += '+';
1955                                         }
1956                                 } else {
1957                                         if (the_form.select_entire_list.value == 1 && the_form.show_plus.value) {
1958                                         the_form.elements[wp].value = count + '+';
1959                                     } else {
1960                                         the_form.elements[wp].value = count;
1961                                     }
1962                                 }
1963                         }
1964                 }
1965         }
1966 }
1967 sugarListView.prototype.use_external_mail_client = function(no_record_txt, module) {
1968         selected_records = sugarListView.get_checks_count();
1969         if(selected_records <1) {
1970                 alert(no_record_txt);
1971         return false;
1972         }
1973
1974     if (document.MassUpdate.select_entire_list.value == 1) {
1975                 if (totalCount > 10) {
1976                         alert(totalCountError);
1977                         return;
1978                 } // if
1979                 select = false;
1980         }
1981         else if (document.MassUpdate.massall.checked == true)
1982                 select = false;
1983         else
1984                 select = true;
1985     sugarListView.get_checks();
1986     var ids = "";
1987     if(select) { // use selected items
1988                 ids = document.MassUpdate.uid.value;
1989         }
1990         else { // use current page
1991                 inputs = document.MassUpdate.elements;
1992                 ar = new Array();
1993                 for(i = 0; i < inputs.length; i++) {
1994                         if(inputs[i].name == 'mass[]' && inputs[i].checked && typeof(inputs[i].value) != 'function') {
1995                                 ar.push(inputs[i].value);
1996                         }
1997                 }
1998                 ids = ar.join(',');
1999         }
2000     YAHOO.util.Connect.asyncRequest("POST", "index.php?", {
2001         success: this.use_external_mail_client_callback
2002     }, SUGAR.util.paramsToUrl({
2003         module: "Emails",
2004         action: "Compose",
2005         listViewExternalClient: 1,
2006         action_module: module,
2007         uid: ids,
2008         to_pdf:1
2009     }));
2010
2011         return false;
2012 }
2013
2014 sugarListView.prototype.use_external_mail_client_callback = function(o)
2015 {
2016     if (o.responseText)
2017         location.href = 'mailto:' + o.responseText;
2018 }
2019
2020 sugarListView.prototype.send_form_for_emails = function(select, currentModule, action, no_record_txt,action_module,totalCount, totalCountError) {
2021         if ( typeof(SUGAR.config.email_sugarclient_listviewmaxselect) != 'undefined' ) {
2022             maxCount = 10;
2023         }
2024         else {
2025             maxCount = SUGAR.config.email_sugarclient_listviewmaxselect;
2026         }
2027
2028     if (document.MassUpdate.select_entire_list.value == 1) {
2029                 if (totalCount > maxCount) {
2030                         alert(totalCountError);
2031                         return;
2032                 } // if
2033                 select = false;
2034         }
2035         else if (document.MassUpdate.massall.checked == true)
2036                 select = false;
2037         else
2038                 select = true;
2039
2040         sugarListView.get_checks();
2041         // create new form to post (can't access action property of MassUpdate form due to action input)
2042         var newForm = document.createElement('form');
2043         newForm.method = 'post';
2044         newForm.action = action;
2045         newForm.name = 'newForm';
2046         newForm.id = 'newForm';
2047         var uidTa = document.createElement('textarea');
2048         uidTa.name = 'uid';
2049         uidTa.style.display = 'none';
2050
2051         if(select) { // use selected items
2052                 uidTa.value = document.MassUpdate.uid.value;
2053         }
2054         else { // use current page
2055                 inputs = document.MassUpdate.elements;
2056                 ar = new Array();
2057                 for(i = 0; i < inputs.length; i++) {
2058                         if(inputs[i].name == 'mass[]' && inputs[i].checked && typeof(inputs[i].value) != 'function') {
2059                                 ar.push(inputs[i].value);
2060                         }
2061                 }
2062                 uidTa.value = ar.join(',');
2063         }
2064
2065         if(uidTa.value == '') {
2066                 alert(no_record_txt);
2067                 return false;
2068         }
2069
2070         var selectedArray = uidTa.value.split(",");
2071         if(selectedArray.length > maxCount) {
2072                 alert(totalCountError);
2073                 return;
2074         } // if
2075         newForm.appendChild(uidTa);
2076
2077         var moduleInput = document.createElement('input');
2078         moduleInput.name = 'module';
2079         moduleInput.type = 'hidden';
2080         moduleInput.value = currentModule;
2081         newForm.appendChild(moduleInput);
2082
2083         var actionInput = document.createElement('input');
2084         actionInput.name = 'action';
2085         actionInput.type = 'hidden';
2086         actionInput.value = 'Compose';
2087         newForm.appendChild(actionInput);
2088
2089         if (typeof action_module != 'undefined' && action_module!= '') {
2090                 var actionModule = document.createElement('input');
2091                 actionModule.name = 'action_module';
2092                 actionModule.type = 'hidden';
2093                 actionModule.value = action_module;
2094                 newForm.appendChild(actionModule);
2095         }
2096         //return_info must follow this pattern."&return_module=Accounts&return_action=index"
2097         if (typeof return_info!= 'undefined' && return_info != '') {
2098                 var params= return_info.split('&');
2099                 if (params.length > 0) {
2100                         for (var i=0;i< params.length;i++) {
2101                                 if (params[i].length > 0) {
2102                                         var param_nv=params[i].split('=');
2103                                         if (param_nv.length==2){
2104                                                 returnModule = document.createElement('input');
2105                                                 returnModule.name = param_nv[0];
2106                                                 returnModule.type = 'hidden';
2107                                                 returnModule.value = param_nv[1];
2108                                                 newForm.appendChild(returnModule);
2109                                         }
2110                                 }
2111                         }
2112                 }
2113         }
2114
2115         var isAjaxCall = document.createElement('input');
2116         isAjaxCall.name = 'ajaxCall';
2117         isAjaxCall.type = 'hidden';
2118         isAjaxCall.value = true;
2119         newForm.appendChild(isAjaxCall);
2120
2121         var isListView = document.createElement('input');
2122         isListView.name = 'ListView';
2123         isListView.type = 'hidden';
2124         isListView.value = true;
2125         newForm.appendChild(isListView);
2126
2127         var toPdf = document.createElement('input');
2128         toPdf.name = 'to_pdf';
2129         toPdf.type = 'hidden';
2130         toPdf.value = true;
2131         newForm.appendChild(toPdf);
2132
2133         //Grab the Quick Compose package for the listview
2134     YAHOO.util.Connect.setForm(newForm);
2135     var callback =
2136         {
2137           success: function(o) {
2138               var resp = YAHOO.lang.JSON.parse(o.responseText);
2139               var quickComposePackage = new Object();
2140               quickComposePackage.composePackage = resp;
2141               quickComposePackage.fullComposeUrl = 'index.php?module=Emails&action=Compose&ListView=true' +
2142                                                    '&uid=' + uidTa.value + '&action_module=' + action_module;
2143
2144               SUGAR.quickCompose.init(quickComposePackage);
2145           }
2146         }
2147
2148         YAHOO.util.Connect.asyncRequest('POST','index.php', callback,null);
2149
2150         // awu Bug 18624: Fixing issue where a canceled Export and unselect of row will persist the uid field, clear the field
2151         document.MassUpdate.uid.value = '';
2152
2153         return false;
2154 }
2155
2156 sugarListView.prototype.send_form = function(select, currentModule, action, no_record_txt,action_module,return_info) {
2157         if (document.MassUpdate.select_entire_list.value == 1) {
2158
2159                 if(sugarListView.get_checks_count() < 1) {
2160                    alert(no_record_txt);
2161                    return false;
2162                 }
2163
2164                 var href = action;
2165                 if ( action.indexOf('?') != -1 )
2166                         href += '&module=' + currentModule;
2167                 else
2168                         href += '?module=' + currentModule;
2169
2170                 if (return_info)
2171                         href += return_info;
2172         var newForm = document.createElement('form');
2173         newForm.method = 'post';
2174         newForm.action = href;
2175         newForm.name = 'newForm';
2176         newForm.id = 'newForm';
2177         var postTa = document.createElement('textarea');
2178         postTa.name = 'current_post';
2179         postTa.value = document.MassUpdate.current_query_by_page.value;
2180         postTa.style.display = 'none';
2181         newForm.appendChild(postTa);
2182         document.MassUpdate.parentNode.appendChild(newForm);
2183         newForm.submit();
2184                 return;
2185         }
2186         else if (document.MassUpdate.massall.checked == true)
2187                 select = false;
2188         else
2189                 select = true;
2190
2191         sugarListView.get_checks();
2192         // create new form to post (can't access action property of MassUpdate form due to action input)
2193         var newForm = document.createElement('form');
2194         newForm.method = 'post';
2195         newForm.action = action;
2196         newForm.name = 'newForm';
2197         newForm.id = 'newForm';
2198         var uidTa = document.createElement('textarea');
2199         uidTa.name = 'uid';
2200         uidTa.style.display = 'none';
2201         uidTa.value = document.MassUpdate.uid.value;
2202
2203         if(uidTa.value == '') {
2204                 alert(no_record_txt);
2205                 return false;
2206         }
2207
2208         newForm.appendChild(uidTa);
2209
2210         var moduleInput = document.createElement('input');
2211         moduleInput.name = 'module';
2212         moduleInput.type = 'hidden';
2213         moduleInput.value = currentModule;
2214         newForm.appendChild(moduleInput);
2215
2216         var actionInput = document.createElement('input');
2217         actionInput.name = 'action';
2218         actionInput.type = 'hidden';
2219         actionInput.value = 'index';
2220         newForm.appendChild(actionInput);
2221
2222         if (typeof action_module != 'undefined' && action_module!= '') {
2223                 var actionModule = document.createElement('input');
2224                 actionModule.name = 'action_module';
2225                 actionModule.type = 'hidden';
2226                 actionModule.value = action_module;
2227                 newForm.appendChild(actionModule);
2228         }
2229         //return_info must follow this pattern."&return_module=Accounts&return_action=index"
2230         if (typeof return_info!= 'undefined' && return_info != '') {
2231                 var params= return_info.split('&');
2232                 if (params.length > 0) {
2233                         for (var i=0;i< params.length;i++) {
2234                                 if (params[i].length > 0) {
2235                                         var param_nv=params[i].split('=');
2236                                         if (param_nv.length==2){
2237                                                 returnModule = document.createElement('input');
2238                                                 returnModule.name = param_nv[0];
2239                                                 returnModule.type = 'hidden';
2240                                                 returnModule.value = param_nv[1];
2241                                                 newForm.appendChild(returnModule);
2242                                         }
2243                                 }
2244                         }
2245                 }
2246         }
2247
2248         document.MassUpdate.parentNode.appendChild(newForm);
2249
2250         newForm.submit();
2251         // awu Bug 18624: Fixing issue where a canceled Export and unselect of row will persist the uid field, clear the field
2252         document.MassUpdate.uid.value = '';
2253
2254         return false;
2255 }
2256 //return a count of checked row.
2257 sugarListView.get_checks_count = function() {
2258         ar = new Array();
2259
2260         if(document.MassUpdate.uid.value != '') {
2261                 oldUids = document.MassUpdate.uid.value.split(',');
2262                 for(uid in oldUids) {
2263                     if(typeof(oldUids[uid]) != 'function') {
2264                        ar[oldUids[uid]] = 1;
2265                     }
2266                 }
2267         }
2268         // build associated array of uids, associated array ensures uniqueness
2269         inputs = document.MassUpdate.elements;
2270         for(i = 0; i < inputs.length; i++) {
2271                 if(inputs[i].name == 'mass[]') {
2272                         ar[inputs[i].value]     = (inputs[i].checked) ? 1 : 0; // 0 of it is unchecked
2273             }
2274         }
2275
2276         // build regular array of uids
2277         uids = new Array();
2278         for(i in ar) {
2279                 if((typeof(ar[i]) != 'function') && ar[i] == 1) {
2280                    uids.push(i);
2281                 }
2282         }
2283
2284         return uids.length;
2285 }
2286
2287 // saves the checks on the current page into the uid textarea
2288 sugarListView.get_checks = function() {
2289         ar = new Array();
2290         if(typeof document.MassUpdate != 'undefined' ){
2291         if(document.MassUpdate.uid.value != '') {
2292                 oldUids = document.MassUpdate.uid.value.split(',');
2293                 for(uid in oldUids) {
2294                     if(typeof(oldUids[uid]) != 'function') {
2295                        ar[oldUids[uid]] = 1;
2296                     }
2297                 }
2298         }
2299
2300         // build associated array of uids, associated array ensures uniqueness
2301         inputs = document.MassUpdate.elements;
2302         for(i = 0; i < inputs.length; i++) {
2303                 if(inputs[i].name == 'mass[]') {
2304                         ar[inputs[i].value]     = (inputs[i].checked) ? 1 : 0; // 0 of it is unchecked
2305                 }
2306         }
2307
2308         // build regular array of uids
2309         uids = new Array();
2310         for(i in ar) {
2311                 if(typeof(ar[i]) != 'function' && ar[i] == 1) {
2312                    uids.push(i);
2313                 }
2314         }
2315
2316         document.MassUpdate.uid.value = uids.join(',');
2317
2318         if(uids.length == 0) return false; // return false if no checks to get
2319         return true; // there are saved checks
2320         }
2321         else return false;
2322 }
2323
2324 sugarListView.prototype.order_checks = function(order,orderBy,moduleString){
2325         checks = sugarListView.get_checks();
2326         eval('document.MassUpdate.' + moduleString + '.value = orderBy');
2327         document.MassUpdate.lvso.value = order;
2328         if(typeof document.MassUpdate.massupdate != 'undefined') {
2329            document.MassUpdate.massupdate.value = 'false';
2330         }
2331
2332         //we must first clear the action of massupdate, change it to index
2333    document.MassUpdate.action.value = document.MassUpdate.return_action.value;
2334    document.MassUpdate.return_module.value='';
2335    document.MassUpdate.return_action.value='';
2336    document.MassUpdate.submit();
2337
2338         return !checks;
2339 }
2340 sugarListView.prototype.save_checks = function(offset, moduleString) {
2341         checks = sugarListView.get_checks();
2342         if(typeof document.MassUpdate != 'undefined'){
2343         eval('document.MassUpdate.' + moduleString + '.value = offset');
2344
2345         if(typeof document.MassUpdate.massupdate != 'undefined') {
2346            document.MassUpdate.massupdate.value = 'false';
2347         }
2348
2349         //we must first clear the action of massupdate, change it to index
2350        document.MassUpdate.action.value = document.MassUpdate.return_action.value;
2351        document.MassUpdate.return_module.value='';
2352        document.MassUpdate.return_action.value='';
2353            document.MassUpdate.submit();
2354
2355
2356         return !checks;
2357         }
2358         else return false;
2359 }
2360
2361 sugarListView.prototype.check_item = function(cb, form) {
2362         if(cb.checked) {
2363                 sugarListView.update_count(1, true);
2364
2365         }else{
2366                 sugarListView.update_count(-1, true);
2367                 if(typeof form != 'undefined' && form != null) {
2368                         sugarListView.prototype.updateUid(cb, form);
2369                 }
2370         }
2371         sugarListView.prototype.toggleSelected();
2372 }
2373
2374 sugarListView.prototype.toggleSelected = function() {
2375
2376         var numSelected = sugarListView.get_num_selected();
2377         var selectedRecords = document.getElementById("selectedRecordsTop");
2378         var selectActions = document.getElementById("selectActions");
2379         var selectActionsDisabled = document.getElementById("selectActionsDisabled");
2380         if(numSelected > 0) {
2381         $(selectedRecords).removeAttr("style").addClass("show");
2382         $(".selectActionsDisabled").hide();
2383         jQuery('ul[name=selectActions]').each(function () {
2384             jQuery(this).removeAttr("style").addClass("show");
2385         });
2386
2387         } else {
2388         $(selectedRecords).hide();
2389         $(".selectActionsDisabled").removeAttr("style").addClass("show");
2390         jQuery('ul[name=selectActions]').each(function () {
2391             jQuery(this).hide();
2392         });
2393         }
2394
2395 }
2396
2397 /**#28000, remove the  unselect record id from MassUpdate.uid **/
2398 sugarListView.prototype.updateUid = function(cb  , form){
2399     if(form.name == 'MassUpdate' && form.uid && form.uid.value && cb.value && form.uid.value.indexOf(cb.value) != -1){
2400         if(form.uid.value.indexOf(','+cb.value)!= -1){
2401             form.uid.value = form.uid.value.replace(','+cb.value , '');
2402         }else if(form.uid.value.indexOf(cb.value + ',')!= -1){
2403             form.uid.value = form.uid.value.replace(cb.value + ',' , '');
2404         }else if(form.uid.value.indexOf(cb.value)!= -1){
2405             form.uid.value = form.uid.value.replace(cb.value  , '');
2406         }
2407     }
2408 }
2409
2410 sugarListView.prototype.check_entire_list = function(form, field, value, list_count) {
2411         // count number of items
2412         count = 0;
2413     $(document.MassUpdate.massall).each(function(){
2414             $(this).attr('checked', true).attr('disabled', true);
2415         });
2416         for (i = 0; i < form.elements.length; i++) {
2417                 if(form.elements[i].name == field && form.elements[i].disabled == false) {
2418                         if(form.elements[i].checked != value) count++;
2419                                 form.elements[i].checked = value;
2420                                 form.elements[i].disabled = true;
2421                 }
2422         }
2423         document.MassUpdate.select_entire_list.value = 1;
2424         sugarListView.update_count(list_count, false);
2425     sugarListView.prototype.toggleSelected();
2426 }
2427
2428 sugarListView.prototype.check_all = function(form, field, value, pageTotal) {
2429         // count number of items
2430         count = 0;
2431     $(document.MassUpdate.massall).each(function(){$(this).attr('checked', value);});
2432         if (document.MassUpdate.select_entire_list &&
2433                 document.MassUpdate.select_entire_list.value == 1)
2434     {
2435         sugarListView.prototype.toggleSelected();
2436         $(document.MassUpdate.massall).each(function(){$(this).attr('disabled', true);});
2437         }
2438     else
2439     {
2440         $(document.MassUpdate.massall).each(function(){$(this).attr('disabled', false);});
2441     }
2442         for (i = 0; i < form.elements.length; i++) {
2443                 if(form.elements[i].name == field && !(form.elements[i].disabled == true && form.elements[i].checked == false)) {
2444                         form.elements[i].disabled = false;
2445
2446                         if(form.elements[i].checked != value)
2447                                 count++;
2448                         form.elements[i].checked = value;
2449                         if(!value){
2450                                 sugarListView.prototype.updateUid(form.elements[i], form);
2451                         }
2452                 }
2453         }
2454         if (pageTotal >= 0)
2455                 sugarListView.update_count(pageTotal);
2456         else if(value)
2457                 sugarListView.update_count(count, true);
2458         else
2459                 sugarListView.update_count(-1 * count, true);
2460
2461         sugarListView.prototype.toggleSelected();
2462 }
2463 sugarListView.check_all = sugarListView.prototype.check_all;
2464 sugarListView.confirm_action = sugarListView.prototype.confirm_action;
2465
2466 sugarListView.prototype.check_boxes = function() {
2467         var inputsCount = 0;
2468         var checkedCount = 0;
2469         var existing_onload = window.onload;
2470         var theForm = document.MassUpdate;
2471         inputs_array = theForm.elements;
2472
2473         if(typeof theForm.uid.value != 'undefined' && theForm.uid.value != "") {
2474                 checked_items = theForm.uid.value.split(",");
2475                 if (theForm.select_entire_list.value == 1) {
2476                         document.MassUpdate.massall.disabled = true;
2477             sugarListView.prototype.toggleSelected();
2478             $("a[name=selectall]:first").click();
2479         }
2480
2481                 for(var wp = 0 ; wp < inputs_array.length; wp++) {
2482                         if(inputs_array[wp].name == "mass[]") {
2483                                 inputsCount++;
2484                                 if (theForm.select_entire_list.value == 1) {
2485                                         inputs_array[wp].checked = true;
2486                                         inputs_array[wp].disabled = true;
2487                                         checkedCount++;
2488                                 }
2489                                 else {
2490                                         for(i in checked_items) {
2491                                                 if(inputs_array[wp].value == checked_items[i]) {
2492                                                         checkedCount++;
2493                                                         inputs_array[wp].checked = true;
2494                             //Bug#52748: Total # of checked items are calculated in back-end side
2495                                                         //sugarListView.prototype.check_item(inputs_array[wp], document.MassUpdate);
2496                                                 }
2497                                         }
2498                                 }
2499                         }
2500                 }
2501         }
2502         else {
2503                 for(var wp = 0 ; wp < inputs_array.length; wp++) {
2504                         if(inputs_array[wp].name == "mass[]") {
2505                                 inputs_array[wp].checked = false;
2506                                 inputs_array[wp].disabled = false;
2507                         }
2508                 }
2509                 if (document.MassUpdate.massall) {
2510                         document.MassUpdate.massall.checked = false;
2511                         document.MassUpdate.massall.disabled = false;
2512                 }
2513                 sugarListView.update_count(0)
2514         }
2515         if(checkedCount > 0 && checkedCount == inputsCount)
2516                 document.MassUpdate.massall.checked = true;
2517 }
2518
2519
2520 /**
2521  * This function is used in Email Template Module's listview.
2522  * It will check whether the templates are used in Campaing->EmailMarketing.
2523  * If true, it will notify user.
2524  */
2525 function check_used_email_templates() {
2526         var ids = document.MassUpdate.uid.value;
2527         var call_back = {
2528                 success:function(r) {
2529                         if(r.responseText != '') {
2530                                 if(!confirm(SUGAR.language.get('app_strings','NTC_TEMPLATES_IS_USED') + r.responseText)) {
2531                                         return false;
2532                                 }
2533                         }
2534                         document.MassUpdate.submit();
2535                         return false;
2536                 }
2537                 };
2538         url = "index.php?module=EmailTemplates&action=CheckDeletable&from=ListView&to_pdf=1&records="+ids;
2539         YAHOO.util.Connect.asyncRequest('POST',url, call_back,null);
2540
2541 }
2542
2543 sugarListView.prototype.send_mass_update = function(mode, no_record_txt, del) {
2544         formValid = check_form('MassUpdate');
2545         if(!formValid && !del) return false;
2546
2547
2548         if (document.MassUpdate.select_entire_list &&
2549                 document.MassUpdate.select_entire_list.value == 1)
2550                 mode = 'entire';
2551         else
2552                 mode = 'selected';
2553
2554         var ar = new Array();
2555
2556         switch(mode) {
2557                 case 'selected':
2558                         for(var wp = 0; wp < document.MassUpdate.elements.length; wp++) {
2559                                 var reg_for_existing_uid = new RegExp('^'+RegExp.escape(document.MassUpdate.elements[wp].value)+'[\s]*,|,[\s]*'+RegExp.escape(document.MassUpdate.elements[wp].value)+'[\s]*,|,[\s]*'+RegExp.escape(document.MassUpdate.elements[wp].value)+'$|^'+RegExp.escape(document.MassUpdate.elements[wp].value)+'$');
2560                                 //when the uid is already in document.MassUpdate.uid.value, we should not add it to ar.
2561                                 if(typeof document.MassUpdate.elements[wp].name != 'undefined'
2562                                         && document.MassUpdate.elements[wp].name == 'mass[]'
2563                                                 && document.MassUpdate.elements[wp].checked
2564                                                 && !reg_for_existing_uid.test(document.MassUpdate.uid.value)) {
2565                                                         ar.push(document.MassUpdate.elements[wp].value);
2566                                 }
2567                         }
2568                         if(document.MassUpdate.uid.value != '') document.MassUpdate.uid.value += ',';
2569                         document.MassUpdate.uid.value += ar.join(',');
2570                         if(document.MassUpdate.uid.value == '') {
2571                                 alert(no_record_txt);
2572                                 return false;
2573                         }
2574                         if(typeof(current_admin_id)!='undefined' && document.MassUpdate.module!= 'undefined' && document.MassUpdate.module.value == 'Users' && (document.MassUpdate.is_admin.value!='' || document.MassUpdate.status.value!='')) {
2575                                 var reg_for_current_admin_id = new RegExp('^'+current_admin_id+'[\s]*,|,[\s]*'+current_admin_id+'[\s]*,|,[\s]*'+current_admin_id+'$|^'+current_admin_id+'$');
2576                                 if(reg_for_current_admin_id.test(document.MassUpdate.uid.value)) {
2577                                         //if current user is admin, we should not allow massupdate the user_type and status of himself
2578                                         alert(SUGAR.language.get('Users','LBL_LAST_ADMIN_NOTICE'));
2579                                         return false;
2580                                 }
2581                         }
2582                         break;
2583                 case 'entire':
2584                         var entireInput = document.createElement('input');
2585                         entireInput.name = 'entire';
2586                         entireInput.type = 'hidden';
2587                         entireInput.value = 'index';
2588                         document.MassUpdate.appendChild(entireInput);
2589                         //confirm(no_record_txt);
2590                         if(document.MassUpdate.module!= 'undefined' && document.MassUpdate.module.value == 'Users' && (document.MassUpdate.is_admin.value!='' || document.MassUpdate.status.value!='')) {
2591                                 alert(SUGAR.language.get('Users','LBL_LAST_ADMIN_NOTICE'));
2592                                 return false;
2593                         }
2594                         break;
2595         }
2596
2597         if(!sugarListView.confirm_action(del))
2598                 return false;
2599
2600         if(del == 1) {
2601                 var deleteInput = document.createElement('input');
2602                 deleteInput.name = 'Delete';
2603                 deleteInput.type = 'hidden';
2604                 deleteInput.value = true;
2605                 document.MassUpdate.appendChild(deleteInput);
2606                 if(document.MassUpdate.module!= 'undefined' && document.MassUpdate.module.value == 'EmailTemplates') {
2607                         check_used_email_templates();
2608                         return false;
2609                 }
2610
2611         }
2612
2613         document.MassUpdate.submit();
2614         return false;
2615 }
2616
2617
2618 sugarListView.prototype.clear_all = function() {
2619         document.MassUpdate.uid.value = '';
2620         document.MassUpdate.select_entire_list.value = 0;
2621         sugarListView.check_all(document.MassUpdate, 'mass[]', false);
2622     $(document.MassUpdate.massall).each(function(){
2623         $(this).attr('checked', false).attr('disabled', false);
2624     });
2625         sugarListView.update_count(0);
2626         sugarListView.prototype.toggleSelected();
2627 }
2628
2629 sListView = new sugarListView();
2630 // -- end sugarListView class
2631
2632 // format and unformat numbers
2633 function unformatNumber(n, num_grp_sep, dec_sep) {
2634         var x=unformatNumberNoParse(n, num_grp_sep, dec_sep);
2635         x=x.toString();
2636         if(x.length > 0) {
2637                 return parseFloat(x);
2638         }
2639         return '';
2640 }
2641
2642 function unformatNumberNoParse(n, num_grp_sep, dec_sep) {
2643         if(typeof num_grp_sep == 'undefined' || typeof dec_sep == 'undefined') return n;
2644         n = n ? n.toString() : '';
2645         if(n.length > 0) {
2646
2647             if(num_grp_sep != '')
2648             {
2649                num_grp_sep_re = new RegExp('\\'+num_grp_sep, 'g');
2650                    n = n.replace(num_grp_sep_re, '');
2651             }
2652
2653                 n = n.replace(dec_sep, '.');
2654
2655         if(typeof CurrencySymbols != 'undefined') {
2656             // Need to strip out the currency symbols from the start.
2657             for ( var idx in CurrencySymbols ) {
2658                 n = n.replace(CurrencySymbols[idx], '');
2659             }
2660         }
2661                 return n;
2662         }
2663         return '';
2664 }
2665
2666 // round parameter can be negative for decimal, precision has to be postive
2667 function formatNumber(n, num_grp_sep, dec_sep, round, precision) {
2668   if(typeof num_grp_sep == 'undefined' || typeof dec_sep == 'undefined') return n;
2669   n = n ? n.toString() : '';
2670   if(n.split) n = n.split('.');
2671   else return n;
2672
2673   if(n.length > 2) return n.join('.'); // that's not a num!
2674   // round
2675   if(typeof round != 'undefined') {
2676     if(round > 0 && n.length > 1) { // round to decimal
2677       n[1] = parseFloat('0.' + n[1]);
2678       n[1] = Math.round(n[1] * Math.pow(10, round)) / Math.pow(10, round);
2679       n[1] = n[1].toString().split('.')[1];
2680     }
2681     if(round <= 0) { // round to whole number
2682         n[0] = Math.round(parseInt(n[0],10) * Math.pow(10, round)) / Math.pow(10, round);
2683       n[1] = '';
2684     }
2685   }
2686
2687   if(typeof precision != 'undefined' && precision >= 0) {
2688     if(n.length > 1 && typeof n[1] != 'undefined') n[1] = n[1].substring(0, precision); // cut off precision
2689         else n[1] = '';
2690     if(n[1].length < precision) {
2691       for(var wp = n[1].length; wp < precision; wp++) n[1] += '0';
2692     }
2693   }
2694
2695   regex = /(\d+)(\d{3})/;
2696   while(num_grp_sep != '' && regex.test(n[0])) n[0] = n[0].toString().replace(regex, '$1' + num_grp_sep + '$2');
2697   return n[0] + (n.length > 1 && n[1] != '' ? dec_sep + n[1] : '');
2698 }
2699
2700 // --- begin ajax status class
2701 SUGAR.ajaxStatusClass = function() {};
2702 SUGAR.ajaxStatusClass.prototype.statusDiv = null;
2703 SUGAR.ajaxStatusClass.prototype.oldOnScroll = null;
2704 SUGAR.ajaxStatusClass.prototype.shown = false; // state of the status window
2705
2706 // reposition the status div, top and centered
2707 SUGAR.ajaxStatusClass.prototype.positionStatus = function() {
2708         //this.statusDiv.style.top = document.body.scrollTop + 8 + 'px';
2709         statusDivRegion = YAHOO.util.Dom.getRegion(this.statusDiv);
2710         statusDivWidth = statusDivRegion.right - statusDivRegion.left;
2711         this.statusDiv.style.left = YAHOO.util.Dom.getViewportWidth() / 2 - statusDivWidth / 2 + 'px';
2712 }
2713
2714 // private func, create the status div
2715 SUGAR.ajaxStatusClass.prototype.createStatus = function(text) {
2716         statusDiv = document.createElement('div');
2717         statusDiv.className = 'dataLabel';
2718         statusDiv.id = 'ajaxStatusDiv';
2719         document.body.appendChild(statusDiv);
2720         this.statusDiv = document.getElementById('ajaxStatusDiv');
2721 }
2722
2723 // public - show the status div with text
2724 SUGAR.ajaxStatusClass.prototype.showStatus = function(text) {
2725         if(!this.statusDiv) {
2726                 this.createStatus(text);
2727         }
2728         else {
2729                 this.statusDiv.style.display = '';
2730         }
2731         this.statusDiv.innerHTML = '&nbsp;<b>' + text + '</b>&nbsp;';
2732         this.positionStatus();
2733         if(!this.shown) {
2734                 this.shown = true;
2735                 this.statusDiv.style.display = '';
2736                 if(window.onscroll) this.oldOnScroll = window.onscroll; // save onScroll
2737                 window.onscroll = this.positionStatus;
2738         }
2739 }
2740
2741 // public - hide it
2742 SUGAR.ajaxStatusClass.prototype.hideStatus = function(text) {
2743         if(!this.shown) return;
2744         this.shown = false;
2745         if(this.oldOnScroll) window.onscroll = this.oldOnScroll;
2746         else window.onscroll = '';
2747         this.statusDiv.style.display = 'none';
2748 }
2749
2750 SUGAR.ajaxStatusClass.prototype.flashStatus = function(text, time){
2751         this.showStatus(text);
2752         window.setTimeout('ajaxStatus.hideStatus();', time);
2753 }
2754
2755
2756 var ajaxStatus = new SUGAR.ajaxStatusClass();
2757 // --- end ajax status class
2758
2759 /**
2760  * Unified Search Advanced - for global search
2761  */
2762 SUGAR.unifiedSearchAdvanced = function() {
2763         var usa_div;
2764         var usa_img;
2765         var usa_open;
2766         var usa_content;
2767         var anim_open;
2768         var anim_close;
2769
2770         return {
2771                 init: function() {
2772                         SUGAR.unifiedSearchAdvanced.usa_div = document.getElementById('unified_search_advanced_div');
2773                         SUGAR.unifiedSearchAdvanced.usa_img = document.getElementById('unified_search_advanced_img');
2774
2775                         if(!SUGAR.unifiedSearchAdvanced.usa_div || !SUGAR.unifiedSearchAdvanced.usa_img) return;
2776                         var attributes = { height: { to: 300 } };
2777             SUGAR.unifiedSearchAdvanced.anim_open = new YAHOO.util.Anim('unified_search_advanced_div', attributes );
2778                         SUGAR.unifiedSearchAdvanced.anim_open.duration = 0.75;
2779                         SUGAR.unifiedSearchAdvanced.anim_close = new YAHOO.util.Anim('unified_search_advanced_div', { height: {to: 0} } );
2780                         SUGAR.unifiedSearchAdvanced.anim_close.duration = 0.75;
2781                         //SUGAR.unifiedSearchAdvanced.anim_close.onComplete.subscribe(function() {SUGAR.unifiedSearchAdvanced.usa_div.style.display = 'none'});
2782
2783                         SUGAR.unifiedSearchAdvanced.usa_img._x = YAHOO.util.Dom.getX(SUGAR.unifiedSearchAdvanced.usa_img);
2784                         SUGAR.unifiedSearchAdvanced.usa_img._y = YAHOO.util.Dom.getY(SUGAR.unifiedSearchAdvanced.usa_img);
2785
2786
2787                         SUGAR.unifiedSearchAdvanced.usa_open = false;
2788                         SUGAR.unifiedSearchAdvanced.usa_content = null;
2789
2790                    YAHOO.util.Event.addListener('unified_search_advanced_img', 'click', SUGAR.unifiedSearchAdvanced.get_content);
2791                 },
2792
2793                 get_content: function(e)
2794                 {
2795                     query_string = trim(document.getElementById('query_string').value);
2796                     if(query_string != '')
2797                     {
2798                         window.location.href = 'index.php?module=Home&action=UnifiedSearch&query_string=' + query_string;
2799                     } else {
2800                         window.location.href = 'index.php?module=Home&action=UnifiedSearch&form_only=true';
2801                     }
2802             },
2803
2804                 animate: function(data) {
2805                         ajaxStatus.hideStatus();
2806
2807                         if(data) {
2808                                 SUGAR.unifiedSearchAdvanced.usa_content = data.responseText;
2809                                 SUGAR.unifiedSearchAdvanced.usa_div.innerHTML = SUGAR.unifiedSearchAdvanced.usa_content;
2810                         }
2811                         if(SUGAR.unifiedSearchAdvanced.usa_open) {
2812                                 document.UnifiedSearch.advanced.value = 'false';
2813                                 SUGAR.unifiedSearchAdvanced.anim_close.animate();
2814                         }
2815                         else {
2816                                 document.UnifiedSearch.advanced.value = 'true';
2817                                 SUGAR.unifiedSearchAdvanced.usa_div.style.display = '';
2818                                 YAHOO.util.Dom.setX(SUGAR.unifiedSearchAdvanced.usa_div, SUGAR.unifiedSearchAdvanced.usa_img._x - 90);
2819                                 YAHOO.util.Dom.setY(SUGAR.unifiedSearchAdvanced.usa_div, SUGAR.unifiedSearchAdvanced.usa_img._y + 15);
2820                                 SUGAR.unifiedSearchAdvanced.anim_open.animate();
2821                         }
2822                 SUGAR.unifiedSearchAdvanced.usa_open = !SUGAR.unifiedSearchAdvanced.usa_open;
2823
2824                         return false;
2825                 },
2826
2827                 checkUsaAdvanced: function() {
2828                         if(document.UnifiedSearch.advanced.value == 'true') {
2829                                 document.UnifiedSearchAdvanced.query_string.value = document.UnifiedSearch.query_string.value;
2830                                 document.UnifiedSearchAdvanced.submit();
2831                                 return false;
2832                         }
2833                         return true;
2834                 }
2835 };
2836 }();
2837 if(typeof YAHOO != 'undefined') YAHOO.util.Event.addListener(window, 'load', SUGAR.unifiedSearchAdvanced.init);
2838
2839
2840 SUGAR.ui = {
2841         /**
2842          * Toggles the header
2843          */
2844         toggleHeader : function() {
2845                 var h = document.getElementById('header');
2846
2847                 if(h != null) {
2848                         if(h != null) {
2849                                 if(h.style.display == 'none') {
2850                                         h.style.display = '';
2851                                 } else {
2852                                         h.style.display = 'none';
2853                                 }
2854                         }
2855                 } else {
2856                         alert(SUGAR.language.get("app_strings", "ERR_NO_HEADER_ID"));
2857                 }
2858         }
2859 };
2860
2861
2862 /**
2863  * General Sugar Utils
2864  */
2865 SUGAR.util = function () {
2866         var additionalDetailsCache;
2867         var additionalDetailsCalls;
2868         var additionalDetailsRpcCall;
2869
2870         return {
2871                 getAndRemove : function (el) {
2872                         if (YAHOO && YAHOO.util && YAHOO.util.Dom)
2873                                 el = YAHOO.util.Dom.get(el);
2874                         else if (typeof (el) == "string")
2875                                 el = document.getElementById(el);
2876                         if (el && el.parentNode)
2877                                 el.parentNode.removeChild(el);
2878
2879                         return el;
2880                 },
2881         paramsToUrl : function (params) {
2882             var parts = [];
2883             for (var i in params)
2884             {
2885                 if (params.hasOwnProperty(i))
2886                 {
2887                     parts.push(encodeURIComponent(i) + '=' + encodeURIComponent(params[i]));
2888                 }
2889             }
2890             return parts.join("&")+"&";
2891         },
2892         // Evaluates a script in a global context
2893         // Workarounds based on findings by Jim Driscoll
2894         // http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
2895         globalEval: function( data ) {
2896             var rnotwhite = /\S/;
2897             if ( data && rnotwhite.test( data ) ) {
2898                 // We use execScript on Internet Explorer
2899                 // We use an anonymous function so that context is window
2900                 // rather than jQuery in Firefox
2901                 ( window.execScript || function( data ) {
2902                     window[ "eval" ].call( window, data );
2903                 } )( data );
2904             }
2905         },
2906             evalScript:function(text){
2907                         if (isSafari) {
2908                                 var waitUntilLoaded = function(){
2909                                         SUGAR.evalScript_waitCount--;
2910                                         if (SUGAR.evalScript_waitCount == 0) {
2911                       var headElem = document.getElementsByTagName('head')[0];
2912                       for ( var i = 0; i < SUGAR.evalScript_evalElem.length; i++) {
2913                         var tmpElem = document.createElement('script');
2914                         tmpElem.type = 'text/javascript';
2915                         tmpElem.text = SUGAR.evalScript_evalElem[i];
2916                         headElem.appendChild(tmpElem);
2917                       }
2918                                         }
2919                                 };
2920
2921                                 var tmpElem = document.createElement('div');
2922                                 tmpElem.innerHTML = text;
2923                                 var results = tmpElem.getElementsByTagName('script');
2924                                 if (results == null) {
2925                                         // No scripts found, bail out
2926                                         return;
2927                                 }
2928
2929                                 var headElem = document.getElementsByTagName('head')[0];
2930                                 var tmpElem = null;
2931                                 SUGAR.evalScript_waitCount = 0;
2932                                 SUGAR.evalScript_evalElem = new Array();
2933                                 for (var i = 0; i < results.length; i++) {
2934                                         if (typeof(results[i]) != 'object') {
2935                                                 continue;
2936                                         };
2937                                         tmpElem = document.createElement('script');
2938                                         tmpElem.type = 'text/javascript';
2939                                         if (results[i].src != null && results[i].src != '') {
2940                                                 tmpElem.src = results[i].src;
2941                                         } else {
2942                         // Need to defer execution of these scripts until the
2943                         // required javascript files are fully loaded
2944                         SUGAR.evalScript_evalElem[SUGAR.evalScript_evalElem.length] = results[i].text;
2945                         continue;
2946                                         }
2947                                         tmpElem.addEventListener('load', waitUntilLoaded);
2948                                         SUGAR.evalScript_waitCount++;
2949                                         headElem.appendChild(tmpElem);
2950                                 }
2951                 // Add some code to handle pages without any external scripts
2952                                 SUGAR.evalScript_waitCount++;
2953                 waitUntilLoaded();
2954
2955                                 // Don't try and process things the IE way
2956                                 return;
2957                         }
2958
2959                 var objRegex = /<\s*script([^>]*)>((.|\s|\v|\0)*?)<\s*\/script\s*>/igm;
2960                         var lastIndex = -1;
2961                         var result =  objRegex.exec(text);
2962             while(result && result.index > lastIndex){
2963                 lastIndex = result.index
2964                                 try{
2965                     // Bug #49205 : Subpanels fail to load when selecting subpanel tab
2966                     // Change approach to handle javascripts included to body of ajax response.
2967                     // To load & run javascripts and inline javascript in correct order load them as synchronous requests
2968                     // JQuery library uses this approach to eval scripts
2969                         if(result[1].indexOf("src=") > -1){
2970                                                 var srcRegex = /.*src=['"]([a-zA-Z0-9_\-\&\/\.\?=:-]*)['"].*/igm;
2971                                                 var srcResult =  result[1].replace(srcRegex, '$1');
2972
2973                         // Check is ulr cross domain or not
2974                         var r1 = /:\/\//igm;
2975                         if ( r1.test(srcResult) && srcResult.indexOf(window.location.hostname) == -1 )
2976                         {
2977                             // if script is cross domain it cannot be loaded via ajax request
2978                             // try load script asynchronous by creating script element in the body
2979                             // YUI 3.3 doesn't allow load scrips synchronously
2980                             // YUI 3.5 do it
2981                             YUI().use('get', function (Y)
2982                             {
2983                                 var url = srcResult;
2984                                 Y.Get.script(srcResult,
2985                                 {
2986                                     autopurge: false,
2987                                     onSuccess : function(o) {  },
2988                                     onFailure: function(o) { },
2989                                     onTimeout: function(o) { }
2990                                 });
2991                             });
2992                             // TODO: for YUI 3.5 - load scripts as script object synchronous
2993                             /*
2994                             YUI().use('get', function (Y) {
2995                                 var url = srcResult;
2996                                 Y.Get.js([{url: url, async: false}], function (err) {});
2997                             });
2998                             */
2999                         }
3000                         else
3001                         {
3002                             // Bug #49205 : Subpanels fail to load when selecting subpanel tab
3003                             // Create a YUI instance using the io-base module.
3004                             (function (srcResult) {
3005                                 YUI().use("io-base",function(Y)
3006                                 {
3007                                     var cfg,response;
3008                                     cfg={
3009                                         method:'GET',
3010                                         sync:true,
3011                                         on:{
3012                                             success:function(transactionid,response,arguments)
3013                                             {
3014                                                 SUGAR.util.globalEval(response.responseText);
3015                                             }
3016                                         }
3017                                     };
3018                                     // Call synchronous request to load javascript content
3019                                     // restonse will be processed in success function
3020                                     response=Y.io(srcResult,cfg);
3021                                 });
3022                             })(srcResult);
3023                         }
3024                         }else{
3025                         // Bug #49205 : Subpanels fail to load when selecting subpanel tab
3026                         // execute script in global context
3027                         // Bug #57288 : don't eval with html comment-out script; that causes syntax error in IE
3028                         var srcRegex = /<!--([\s\S]*?)-->/;
3029                         var srcResult = srcRegex.exec(result[2]);
3030                         if (srcResult && srcResult.index > -1)
3031                         {
3032                             SUGAR.util.globalEval(srcResult[1]);
3033                         }
3034                         else
3035                         {
3036                             SUGAR.util.globalEval(result[2]);
3037                         }
3038                         }
3039                       }
3040                       catch(e) {
3041                       if(typeof(console) != "undefined" && typeof(console.log) == "function")
3042                       {
3043                           console.log("error adding script");
3044                           console.log(e);
3045                           console.log(result);
3046                       }
3047                   }
3048                   result =  objRegex.exec(text);
3049                         }
3050             },
3051                 /**
3052                  * Gets the sidebar object
3053                  * @return object pointer to the sidebar element
3054                  */
3055                 getLeftColObj: function() {
3056                         leftColObj = document.getElementById('leftCol');
3057                         while(leftColObj.nodeName != 'TABLE') {
3058                                 leftColObj = leftColObj.firstChild;
3059                         }
3060                         leftColTable = leftColObj;
3061                         leftColTd = leftColTable.getElementsByTagName('td')[0];
3062                         leftColTdRegion = YAHOO.util.Dom.getRegion(leftColTd);
3063                         leftColTd.style.width = (leftColTdRegion.right - leftColTdRegion.left) + 'px';
3064
3065                         return leftColTd;
3066                 },
3067                 /**
3068                  * Fills the shortcut menu placeholders w/ actual content
3069                  * Call this on load event
3070                  *
3071                  * @param shortcutContent Array array of content to fill in
3072                  */
3073                 fillShortcuts: function(e, shortcutContent) {
3074                         return ;
3075 /*
3076             // don't do this if leftCol isn't available
3077             if (document.getElementById('leftCol') == undefined) { return; }
3078
3079                 spans = document.getElementById('leftCol').getElementsByTagName('span');
3080                         hideCol = document.getElementById('HideMenu').getElementsByTagName('span');
3081                         w = spans.length + 1;
3082                         for(i in hideCol) {
3083                                 spans[w] = hideCol[i];
3084                                 w++;
3085                         }
3086                     for(je in shortcutContent) {
3087                         for(wp in spans) {
3088                                 if(typeof spans[wp].innerHTML != 'undefined' && spans[wp].innerHTML == ('wp_shortcut_fill_' + je)) {
3089                                         if(typeof spans[wp].parentNode.parentNode == 'object') {
3090                                                 if(typeof spans[wp].parentNode.parentNode.onclick != 'undefined') {
3091                                                         spans[wp].parentNode.parentNode.onclick = null;
3092                                                 }
3093                                                 // If the wp_shortcut span is contained by an A tag, replace the A with a DIV.
3094                                                 if(spans[wp].parentNode.tagName == 'A' && !isIE) {
3095                                                         var newDiv = document.createElement('DIV');
3096                                                         var parentAnchor = spans[wp].parentNode;
3097
3098                                                         spans[wp].parentNode.parentNode.style.display = 'none';
3099
3100                                                         // Copy styles over to the new container div
3101                                                         if(window.getComputedStyle) {
3102                                                                 var parentStyle = window.getComputedStyle(parentAnchor, '');
3103                                                                 for(var styleName in parentStyle) {
3104                                                                         if(typeof parentStyle[styleName] != 'function'
3105                                                                     && styleName != 'display'
3106                                                                     && styleName != 'borderWidth'
3107                                                                     && styleName != 'visibility') {
3108                                                                         try {
3109                                                                                         newDiv.style[styleName] = parentStyle[styleName];
3110                                                                                 } catch(e) {
3111                                                                                         // Catches .length and .parentRule, and others
3112                                                                                 }
3113                                                                         }
3114                                                                 }
3115                                                         }
3116
3117                                                         // Replace the A with the DIV
3118                                                         newDiv.appendChild(spans[wp]);
3119                                                         parentAnchor.parentNode.replaceChild(newDiv, parentAnchor);
3120
3121                                                         spans[wp].parentNode.parentNode.style.display = '';
3122                                                 }
3123                                         }
3124                                     spans[wp].innerHTML = shortcutContent[je]; // fill w/ content
3125                                     if(spans[wp].style) spans[wp].style.display = '';
3126                                 }
3127                         }
3128                         }*/
3129                 },
3130                 /**
3131                  * Make an AJAX request.
3132                  *
3133                  * @param       url                             string  resource to load
3134                  * @param       theDiv                  string  id of element to insert loaded data into
3135                  * @param       postForm                string  if set, a POST request will be made to resource specified by url using the form named by postForm
3136                  * @param       callback                string  name of function to invoke after HTTP response is recieved
3137                  * @param       callbackParam   any             parameter to pass to callback when invoked
3138                  * @param       appendMode              bool    if true, HTTP response will be appended to the contents of theDiv, or else contents will be overriten.
3139                  */
3140             retrieveAndFill: function(url, theDiv, postForm, callback, callbackParam, appendMode) {
3141                         if(typeof theDiv == 'string') {
3142                                 try {
3143                                         theDiv = document.getElementById(theDiv);
3144                                 }
3145                         catch(e) {
3146                                         return;
3147                                 }
3148                         }
3149
3150                         var success = function(data) {
3151                                 if (typeof theDiv != 'undefined' && theDiv != null)
3152                                 {
3153                                         try {
3154                                                 if (typeof appendMode != 'undefined' && appendMode)
3155                                                 {
3156                                                         theDiv.insertAdjacentHTML('beforeend', data.responseText);
3157                                                 }
3158                                                 else
3159                                                 {
3160                                                         theDiv.innerHTML = data.responseText;
3161                                                 }
3162                                         }
3163                                         catch (e) {
3164                                                 return;
3165                                         }
3166                                 }
3167                                 if (typeof callback != 'undefined' && callback != null) callback(callbackParam);
3168                         }
3169
3170                         if(typeof postForm == 'undefined' || postForm == null) {
3171                                 var cObj = YAHOO.util.Connect.asyncRequest('GET', url, {success: success, failure: success});
3172                         }
3173                         else {
3174                                 YAHOO.util.Connect.setForm(postForm);
3175                                 var cObj = YAHOO.util.Connect.asyncRequest('POST', url, {success: success, failure: success});
3176                         }
3177                 },
3178                 checkMaxLength: function() { // modified from http://www.quirksmode.org/dom/maxlength.html
3179                         var maxLength = this.getAttribute('maxlength');
3180                         var currentLength = this.value.length;
3181                         if (currentLength > maxLength) {
3182                                 this.value = this.value.substring(0, maxLength);
3183                         }
3184                         // not innerHTML
3185                 },
3186                 /**
3187                  * Adds maxlength attribute to textareas
3188                  */
3189                 setMaxLength: function() { // modified from http://www.quirksmode.org/dom/maxlength.html
3190                         var x = document.getElementsByTagName('textarea');
3191                         for (var i=0;i<x.length;i++) {
3192                                 if (x[i].getAttribute('maxlength')) {
3193                                         x[i].onkeyup = x[i].onchange = SUGAR.util.checkMaxLength;
3194                                         x[i].onkeyup();
3195                                 }
3196                         }
3197                 },
3198
3199                 /**
3200                  * Renders Query UI Help Dialog
3201                  */
3202                 showHelpTips: function(el,helpText,myPos,atPos,id) {
3203                                 if(myPos == undefined || myPos == "") {
3204                                         myPos = "left top";
3205                                 }
3206                                 if(atPos == undefined || atPos == "") {
3207                                         atPos = "right top";
3208                                 }
3209
3210                                 var pos = $(el).offset(),
3211                     ofWidth = $(el).width(),
3212                     elmId = id || 'helpTip' + pos.left + '_' + ofWidth,
3213                     $dialog = elmId ? ( $("#"+elmId).length > 0 ? $("#"+elmId) : $('<div></div>').attr("id", elmId) ) : $('<div></div>');
3214                 $dialog.html(helpText)
3215                                         .dialog({
3216                                                 autoOpen: false,
3217                                                 title: SUGAR.language.get('app_strings', 'LBL_HELP'),
3218                                                 position: {
3219                                                     my: myPos,
3220                                                     at: atPos,
3221                                                     of: $(el)
3222                                                 }
3223                                         });
3224
3225
3226                                         var width = $dialog.dialog( "option", "width" );
3227
3228                                         if((pos.left + ofWidth) - 40 < width) {
3229                                                 $dialog.dialog("option","position",{my: 'left top',at: 'right top',of: $(el)})  ;
3230                                         }
3231                                         $dialog.dialog('open');
3232                                         $(".ui-dialog").appendTo("#content");
3233
3234
3235                 },
3236                 getStaticAdditionalDetails: function(el, body, caption, show_buttons) {
3237                         if(typeof show_buttons == "undefined") {
3238                                 show_buttons = false;
3239                         }
3240
3241                         $(".ui-dialog").find(".open").dialog("close");
3242
3243                         var $dialog = $('<div class="open"></div>')
3244                         .html(body)
3245                         .dialog({
3246                                 autoOpen: false,
3247                                 title: caption,
3248                                 width: 300,
3249                                 position: {
3250                                     my: 'right top',
3251                                     at: 'left top',
3252                                     of: $(el)
3253                           }
3254                         });
3255
3256                         if(show_buttons) {
3257                                 $(".ui-dialog").find('.ui-dialog-titlebar-close').css("display","none");
3258                                 $(".ui-dialog").find('.ui-dialog-title').css("width","100%");
3259                         }
3260
3261
3262                         var width = $dialog.dialog( "option", "width" );
3263                         var pos = $(el).offset();
3264                         var ofWidth = $(el).width();
3265
3266                         if((pos.left + ofWidth) - 40 < width) {
3267                                 $dialog.dialog("option","position",{my: 'left top',at: 'right top',of: $(el)})  ;
3268                         }
3269
3270                         $dialog.dialog('open');
3271                         $(".ui-dialog").appendTo("#content");
3272
3273                 },
3274
3275                 closeStaticAdditionalDetails: function() {
3276                         $(".ui-dialog").find(".open").dialog("close");
3277                 },
3278                 /**
3279                  * Retrieves additional details dynamically
3280                  */
3281                 getAdditionalDetails: function(bean, id, spanId, show_buttons) {
3282                         if(typeof show_buttons == "undefined")
3283                                 show_buttons = false;
3284                                 var el = '#'+spanId;
3285                         go = function() {
3286                                 oReturn = function(body, caption, width, theme) {
3287
3288                                         $(".ui-dialog").find(".open").dialog("close");
3289
3290                                         var $dialog = $('<div class="open"></div>')
3291                                         .html(body)
3292                                         .dialog({
3293                                                 autoOpen: false,
3294                                                 title: caption,
3295                                                 width: 300,
3296                                                 position: {
3297                                                     my: 'right top',
3298                                                     at: 'left top',
3299                                                     of: $(el)
3300                                           }
3301                                         });
3302                                 if(show_buttons) {
3303                                         $(".ui-dialog").find('.ui-dialog-titlebar-close').css("display","none");
3304                                         $(".ui-dialog").find('.ui-dialog-title').css("width","100%");
3305                                 }
3306
3307                                         var width = $dialog.dialog( "option", "width" );
3308                                         var pos = $(el).offset();
3309                                         var ofWidth = $(el).width();
3310
3311                                         if((pos.left + ofWidth) - 40 < width) {
3312                                                 $dialog.dialog("option","position",{my: 'left top',at: 'right top',of: $(el)})  ;
3313                                         }
3314
3315                                         $dialog.dialog('open');
3316                                         $(".ui-dialog").appendTo("#content");
3317                                 }
3318
3319                                 success = function(data) {
3320                                         eval(data.responseText);
3321
3322                                         SUGAR.util.additionalDetailsCache[id] = new Array();
3323                                         SUGAR.util.additionalDetailsCache[id]['body'] = result['body'];
3324                                         SUGAR.util.additionalDetailsCache[id]['caption'] = result['caption'];
3325                                         SUGAR.util.additionalDetailsCache[id]['width'] = result['width'];
3326                                         SUGAR.util.additionalDetailsCache[id]['theme'] = result['theme'];
3327                                         ajaxStatus.hideStatus();
3328                                         return oReturn(SUGAR.util.additionalDetailsCache[id]['body'], SUGAR.util.additionalDetailsCache[id]['caption'], SUGAR.util.additionalDetailsCache[id]['width'], SUGAR.util.additionalDetailsCache[id]['theme']);
3329                                 }
3330
3331                                 if(typeof SUGAR.util.additionalDetailsCache[id] != 'undefined')
3332                                         return oReturn(SUGAR.util.additionalDetailsCache[id]['body'], SUGAR.util.additionalDetailsCache[id]['caption'], SUGAR.util.additionalDetailsCache[id]['width'], SUGAR.util.additionalDetailsCache[id]['theme']);
3333
3334                                 if(typeof SUGAR.util.additionalDetailsCalls[id] != 'undefined') // call already in progress
3335                                         return;
3336                                 ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_LOADING'));
3337                                 url = 'index.php?to_pdf=1&module=Home&action=AdditionalDetailsRetrieve&bean=' + bean + '&id=' + id;
3338                                 if(show_buttons)
3339                                         url += '&show_buttons=true';
3340                                 SUGAR.util.additionalDetailsCalls[id] = YAHOO.util.Connect.asyncRequest('GET', url, {success: success, failure: success});
3341
3342                                 return false;
3343                         }
3344                         SUGAR.util.additionalDetailsRpcCall = window.setTimeout('go()', 250);
3345                 },
3346                 clearAdditionalDetailsCall: function() {
3347                         if(typeof SUGAR.util.additionalDetailsRpcCall == 'number') window.clearTimeout(SUGAR.util.additionalDetailsRpcCall);
3348                 },
3349                 /**
3350                  * A function that extends functionality from parent to child.
3351                  */
3352                 extend : function(subc, superc, overrides) {
3353                         subc.prototype = new superc;    // set the superclass
3354                         // overrides
3355                         if (overrides) {
3356                             for (var i in overrides)    subc.prototype[i] = overrides[i];
3357                         }
3358                 },
3359                 hrefURL : function(url) {
3360                         if(SUGAR.isIE) {
3361                                 // IE needs special treatment since otherwise it would not pass Referer
3362                                 var trampoline = document.createElement('a');
3363                                 trampoline.href = url;
3364                                 document.body.appendChild(trampoline);
3365                                 trampoline.click();
3366                                 document.body.removeChild(trampoline);
3367                         } else {
3368                                 document.location.href = url;
3369                         }
3370                 },
3371
3372                 openWindow : function(URL, windowName, windowFeatures) {
3373                         if(SUGAR.isIE) {
3374                                 // IE needs special treatment since otherwise it would not pass Referer
3375                                 win = window.open('', windowName, windowFeatures);
3376                                 var trampoline = document.createElement('a');
3377                                 trampoline.href = URL;
3378                                 trampoline.target = windowName;
3379                                 document.body.appendChild(trampoline);
3380                                 trampoline.click();
3381                                 document.body.removeChild(trampoline);
3382                         } else {
3383                                 win = window.open(URL, windowName, windowFeatures);
3384                         }
3385                         return win;
3386                 },
3387         //Reset the scroll on the window
3388         top : function() {
3389                         window.scroll(0,0);
3390                 },
3391
3392         //Based on YUI onAvailible, but will use any boolean function instead of an ID
3393         doWhen : function(condition, fn, params, scope)
3394         {
3395             this._doWhenStack.push({
3396                 check:condition,
3397                 fn:         fn,
3398                 obj:        params,
3399                 overrideContext:   scope
3400             });
3401
3402             this._doWhenretryCount = 50;
3403             this._startDoWhenInterval();
3404         },
3405
3406         _startDoWhenInterval : function(){
3407             if (!this._doWhenInterval) {
3408                 this._doWhenInterval = YAHOO.lang.later(50, this, this._doWhenCheck, null, true);
3409             }
3410         },
3411         _doWhenStack : [],
3412         _doWhenInterval : false,
3413         _doWhenCheck : function() {
3414                 if (this._doWhenStack.length === 0) {
3415                     this._doWhenretryCount = 0;
3416                     if (this._doWhenInterval) {
3417                         // clearInterval(this._interval);
3418                         this._doWhenInterval.cancel();
3419                         this._doWhenInterval = null;
3420                     }
3421                     return;
3422                 }
3423
3424                 if (this._doWhenLocked) {
3425                     return;
3426                 }
3427
3428                 if (SUGAR.isIE) {
3429                     // Hold off if DOMReady has not fired and check current
3430                     // readyState to protect against the IE operation aborted
3431                     // issue.
3432                     if (!YAHOO.util.Event.DOMReady) {
3433                         this._startDoWhenInterval();
3434                         return;
3435                     }
3436                 }
3437
3438                 this._doWhenLocked = true;
3439
3440
3441                 // keep trying until after the page is loaded.  We need to
3442                 // check the page load state prior to trying to bind the
3443                 // elements so that we can be certain all elements have been
3444                 // tested appropriately
3445                 var tryAgain = YAHOO.util.Event.DOMReady;
3446                 if (!tryAgain) {
3447                     tryAgain = (this._doWhenretryCount > 0 && this._doWhenStack.length > 0);
3448                 }
3449
3450                 // onAvailable
3451                 var notAvail = [];
3452
3453                 var executeItem = function (context, item) {
3454                     if (item.overrideContext) {
3455                         if (item.overrideContext === true) {
3456                             context = item.obj;
3457                         } else {
3458                             context = item.overrideContext;
3459                         }
3460                     }
3461                     if(item.fn) {
3462                         item.fn.call(context, item.obj);
3463                     }
3464                 };
3465
3466                 var i, len, item, test;
3467
3468                 // onAvailable onContentReady
3469                 for (i=0, len=this._doWhenStack.length; i<len; i=i+1) {
3470                     item = this._doWhenStack[i];
3471                     if (item) {
3472                         test = item.check;
3473                         if ((typeof(test) == "string" && eval(test)) || (typeof(test) == "function" && test())) {
3474                             executeItem(this, item);
3475                             this._doWhenStack[i] = null;
3476                         }
3477                          else {
3478                             notAvail.push(item);
3479                         }
3480                     }
3481                 }
3482
3483                 this._doWhenretryCount--;
3484
3485                 if (tryAgain) {
3486                     for (i=this._doWhenStack.length-1; i>-1; i--) {
3487                         item = this._doWhenStack[i];
3488                         if (!item || !item.check) {
3489                             this._doWhenStack.splice(i, 1);
3490                         }
3491                     }
3492                     this._startDoWhenInterval();
3493                 } else {
3494                     if (this._doWhenInterval) {
3495                         // clearInterval(this._interval);
3496                         this._doWhenInterval.cancel();
3497                         this._doWhenInterval = null;
3498                     }
3499                 }
3500                 this._doWhenLocked = false;
3501             },
3502         buildAccessKeyLabels : function() {
3503             if (typeof(Y.env.ua) !== 'undefined'){
3504                 envStr = '';
3505                 browserOS = Y.env.ua['os'];
3506                 isIE = Y.env.ua['ie'];
3507                 isCR = Y.env.ua['chrome'];
3508                 isFF = Y.env.ua['gecko'];
3509                 isWK = Y.env.ua['webkit'];
3510                 isOP = Y.env.ua['opera'];
3511                 controlKey = '';
3512
3513                 //first determine the OS
3514                 if(browserOS=='macintosh'){
3515                     //we got a mac, lets use the mac specific commands while we check the browser
3516                     if(isIE){
3517                         //IE on a Mac? Not possible, but let's assign alt anyways for completions sake
3518                         controlKey = 'Alt+';
3519                     }else if(isWK || isFF){
3520                         //Chrome or safari on a mac
3521                         //firefox moved to webkit standard starting with FF14
3522                         controlKey = 'Ctrl+Opt+';
3523                     }else if(isOP){
3524                         //Opera on a mac
3525                         controlKey = 'Shift+Esc: ';
3526                     }else{
3527                         //default for everything else on a mac
3528                         controlKey = 'Ctrl+';
3529                     }
3530                 }else{
3531                     //this is not a mac so let's use the windows/unix commands while we check the browser
3532                     if(isFF){
3533                         //FF on windows/unix
3534                         controlKey = 'Alt+Shift+';
3535                     }else if(isOP){
3536                         //Opera on windows/unix
3537                         controlKey = 'Shift+Esc: ';
3538                     }else {
3539                         //this is the default for safari, IE and Chrome
3540                         //if this is webkit and is NOT google, then we are most likely looking at Safari
3541                         controlKey = 'Alt+';
3542                     }
3543
3544                 }
3545
3546                 //now lets retrieve all elements of type input
3547                 allButtons = document.getElementsByTagName('input');
3548                 //iterate through list and modify title if the accesskey is not empty
3549                 for(i=0;i<allButtons.length;i++){
3550                     if(allButtons[i].getAttribute('accesskey') && allButtons[i].getAttribute('type') && allButtons[i].getAttribute('type')=='button'){
3551                         allButtons[i].setAttribute('title',allButtons[i].getAttribute('title')+' ['+controlKey+allButtons[i].getAttribute('accesskey')+']');
3552                     }
3553                 }
3554                 //now change the text in the help div
3555                 $("#shortcuts_dialog").html(function(i, text)  {
3556                     return text.replace(/Alt\+/g,controlKey);
3557                 });
3558             }// end if (typeof(Y.env.ua) !== 'undefined')
3559         }//end buildAccessKeyLabels()
3560         };
3561 }(); // end util
3562 SUGAR.util.additionalDetailsCache = new Array();
3563 SUGAR.util.additionalDetailsCalls = new Array();
3564 if(typeof YAHOO != 'undefined') YAHOO.util.Event.addListener(window, 'load', SUGAR.util.setMaxLength); // allow textareas to obey maxlength attrib
3565
3566 SUGAR.savedViews = function() {
3567         var selectedOrderBy;
3568         var selectedSortOrder;
3569         var displayColumns;
3570         var hideTabs;
3571         var columnsMeta; // meta data for the display columns
3572
3573         return {
3574                 setChooser: function() {
3575
3576                         var displayColumnsDef = new Array();
3577                         var hideTabsDef = new Array();
3578
3579                     var left_td = document.getElementById('display_tabs_td');
3580                     if(typeof left_td == 'undefined' || left_td == null) return; // abort!
3581                     var right_td = document.getElementById('hide_tabs_td');
3582
3583                     var displayTabs = left_td.getElementsByTagName('select')[0];
3584                     var hideTabs = right_td.getElementsByTagName('select')[0];
3585
3586                         for(i = 0; i < displayTabs.options.length; i++) {
3587                                 displayColumnsDef.push(displayTabs.options[i].value);
3588                         }
3589
3590                         if(typeof hideTabs != 'undefined') {
3591                                 for(i = 0; i < hideTabs.options.length; i++) {
3592                                  hideTabsDef.push(hideTabs.options[i].value);
3593                                 }
3594                         }
3595                         if (!SUGAR.savedViews.clearColumns)
3596                                 document.getElementById('displayColumnsDef').value = displayColumnsDef.join('|');
3597                         document.getElementById('hideTabsDef').value = hideTabsDef.join('|');
3598                 },
3599
3600                 select: function(saved_search_select) {
3601                         for(var wp = 0; wp < document.search_form.saved_search_select.options.length; wp++) {
3602                                 if(typeof document.search_form.saved_search_select.options[wp].value != 'undefined' &&
3603                                         document.search_form.saved_search_select.options[wp].value == saved_search_select) {
3604                                                 document.search_form.saved_search_select.selectedIndex = wp;
3605                                                 document.search_form.ss_delete.style.display = '';
3606                                                 document.search_form.ss_update.style.display = '';
3607                                 }
3608                         }
3609                 },
3610                 saved_search_action: function(action, delete_lang) {
3611                         if(action == 'delete') {
3612                                 if(!confirm(delete_lang)) return;
3613                         }
3614                         if(action == 'save') {
3615                                 if(document.search_form.saved_search_name.value.replace(/^\s*|\s*$/g, '') == '') {
3616                                         alert(SUGAR.language.get('app_strings', 'LBL_SAVED_SEARCH_ERROR'));
3617                                         return;
3618                                 }
3619                         }
3620
3621                         // This check is needed for the Activities module (Calls/Meetings/Tasks).
3622                         if (document.search_form.saved_search_action)
3623                         {
3624                                 document.search_form.saved_search_action.value = action;
3625                                 document.search_form.search_module.value = document.search_form.module.value;
3626                                 document.search_form.module.value = 'SavedSearch';
3627                                 // Bug 31922 - Make sure to specify that we want to hit the index view here of
3628                                 // the SavedSearch module, since the ListView doesn't have the logic to save the
3629                                 // search and redirect back
3630                                 document.search_form.action.value = 'index';
3631                         }
3632                         SUGAR.ajaxUI.submitForm(document.search_form);
3633                 },
3634                 shortcut_select: function(selectBox, module) {
3635                         //build url
3636                         selecturl = 'index.php?module=SavedSearch&search_module=' + module + '&action=index&saved_search_select=' + selectBox.options[selectBox.selectedIndex].value
3637                         //add searchFormTab to url if it is available.  This determines what tab to render
3638                         if(typeof(document.getElementById('searchFormTab'))!='undefined'){
3639                                 selecturl = selecturl + '&searchFormTab=' + document.search_form.searchFormTab.value;
3640                         }
3641                         //add showSSDIV to url if it is available.  This determines whether saved search sub form should
3642                         //be rendered open or not
3643                         if(document.getElementById('showSSDIV') && typeof(document.getElementById('showSSDIV') !='undefined')){
3644                                 selecturl = selecturl + '&showSSDIV='+document.getElementById('showSSDIV').value;
3645                         }
3646                         //use created url to navigate
3647                         document.location.href = selecturl;
3648                 },
3649                 handleForm: function() {
3650                         SUGAR.tabChooser.movementCallback = function(left_side, right_side) {
3651                                 while(document.getElementById('orderBySelect').childNodes.length != 0) { // clear out order by options
3652                                         document.getElementById('orderBySelect').removeChild(document.getElementById('orderBySelect').lastChild);
3653                                 }
3654
3655                                 var selectedIndex = 0;
3656                                 var nodeCount = -1; // need this because the counter i also includes "undefined" nodes
3657                                                                         // which was breaking Calls and Meetings
3658
3659                                 for(i in left_side.childNodes) { // fill in order by options
3660                                         if(typeof left_side.childNodes[i].nodeName != 'undefined' &&
3661                                                 left_side.childNodes[i].nodeName.toLowerCase() == 'option' &&
3662                                                 typeof SUGAR.savedViews.columnsMeta[left_side.childNodes[i].value] != 'undefined' && // check if column is sortable
3663                                                 typeof SUGAR.savedViews.columnsMeta[left_side.childNodes[i].value]['sortable'] == 'undefined' &&
3664                                                 SUGAR.savedViews.columnsMeta[left_side.childNodes[i].value]['sortable'] != false) {
3665                                                         nodeCount++;
3666                                                         optionNode = document.createElement('option');
3667                                                         optionNode.value = left_side.childNodes[i].value;
3668                                                         optionNode.innerHTML = left_side.childNodes[i].innerHTML;
3669                                                         document.getElementById('orderBySelect').appendChild(optionNode);
3670                                                         if(optionNode.value == SUGAR.savedViews.selectedOrderBy)
3671                                                                 selectedIndex = nodeCount;
3672                                         }
3673                                 }
3674                                 // Firefox needs this to be set after all the option nodes are created.
3675                                 document.getElementById('orderBySelect').selectedIndex = selectedIndex;
3676                         };
3677                         SUGAR.tabChooser.movementCallback(document.getElementById('display_tabs_td').getElementsByTagName('select')[0]);
3678
3679                         // This check is needed for the Activities module (Calls/Meetings/Tasks).
3680             if (document.search_form.orderBy)
3681             {
3682                 if (document.search_form.orderBy.length > 1 && document.search_form.orderBy[1].type == 'select-one')
3683                 {
3684                     document.search_form.orderBy[1].options.value = SUGAR.savedViews.selectedOrderBy;
3685                 }
3686                 else
3687                 {
3688                     document.search_form.orderBy.options.value = SUGAR.savedViews.selectedOrderBy;
3689                 }
3690             }
3691
3692                         // handle direction
3693                         if(SUGAR.savedViews.selectedSortOrder == 'DESC') document.getElementById('sort_order_desc_radio').checked = true;
3694                         else document.getElementById('sort_order_asc_radio').checked = true;
3695                 }
3696         };
3697 }();
3698
3699 SUGAR.searchForm = function() {
3700         var url;
3701         return {
3702                 // searchForm tab selector util
3703                 searchFormSelect: function(view, previousView) {
3704                         var module = view.split('|')[0];
3705                         var theView = view.split('|')[1];
3706                         // retrieve form
3707                         var handleDisplay = function() { // hide other divs
3708                                 document.search_form.searchFormTab.value = theView;
3709                                 patt = module+"(.*)SearchForm$";
3710                                 divId=document.search_form.getElementsByTagName('div');
3711                                 // Hide all the search forms and retrive the name of the previous search tab (useful for the first load because previousView is empty)
3712                                 for (i=0;i<divId.length;i++){
3713                                         if(divId[i].id.match(module)==module){
3714                                                 if(divId[i].id.match('SearchForm')=='SearchForm'){
3715                                 if(document.getElementById(divId[i].id).style.display == ''){
3716                                    previousTab=divId[i].id.match(patt)[1];
3717                                 }
3718                                 document.getElementById(divId[i].id).style.display = 'none';
3719                             }
3720                                         }
3721                                 }
3722
3723                 //clear thesearch form accesekeys and reset them to the appropriate link
3724                 adv = document.getElementById('advanced_search_link');
3725                 bas = document.getElementById('basic_search_link');
3726                 adv.setAttribute('accesskey','');
3727                 bas.setAttribute('accesskey','');
3728                 a_key = SUGAR.language.get("app_strings", "LBL_ADV_SEARCH_LNK_KEY");
3729
3730                 //reset the ccesskey based on theview
3731                 if(theView === 'advanced_search'){
3732
3733                     bas.setAttribute('accesskey',a_key);
3734                 }else{
3735                     adv.setAttribute('accesskey',a_key);
3736                 }
3737                 
3738                                 // show the good search form.
3739                                 document.getElementById(module + theView + 'SearchForm').style.display = '';
3740                 //if its not the first tab show there is a previous tab.
3741                 if(previousView) {
3742                      thepreviousView=previousView.split('|')[1];
3743                  }
3744                  else{
3745                      thepreviousView=previousTab;
3746                  }
3747                  thepreviousView=thepreviousView.replace(/_search/, "");
3748                  // Process to retrieve the completed field from one tab to an other.
3749                  for(num in document.search_form.elements) {
3750                      if(document.search_form.elements[num]) {
3751                          el = document.search_form.elements[num];
3752                          pattern="^(.*)_"+thepreviousView+"$";
3753                          if(typeof el.type != 'undefined' && typeof el.name != 'undefined' && el.name.match(pattern)) {
3754                              advanced_input_name = el.name.match(pattern)[1]; // strip
3755                              advanced_input_name = advanced_input_name+"_"+theView.replace(/_search/, "");
3756                              if(typeof document.search_form[advanced_input_name] != 'undefined')  // if advanced input of same name exists
3757                                  SUGAR.searchForm.copyElement(advanced_input_name, el);
3758                          }
3759                      }
3760                  }
3761                 // Remove the previously selected div, so that only data from current div to be sent as form data to the server-side
3762                 if (document.getElementById(module + thepreviousView + '_search' + 'SearchForm'))
3763                 {
3764                     document.getElementById(module + thepreviousView + '_search' + 'SearchForm').innerHTML = '';
3765                 }
3766                         }
3767
3768                         // if tab is not cached
3769                         if(document.getElementById(module + theView + 'SearchForm').innerHTML == '') {
3770                                 ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_LOADING'));
3771                                 var success = function(data) {
3772                                         document.getElementById(module + theView + 'SearchForm').innerHTML = data.responseText;
3773
3774                                         SUGAR.util.evalScript(data.responseText);
3775                                         // pass script variables to global scope
3776                                         if(theView == 'saved_views') {
3777                                                 if(typeof columnsMeta != 'undefined') SUGAR.savedViews.columnsMeta = columnsMeta;
3778                                                 if(typeof selectedOrderBy != 'undefined') SUGAR.savedViews.selectedOrderBy = selectedOrderBy;
3779                                                 if(typeof selectedSortOrder != 'undefined') SUGAR.savedViews.selectedSortOrder = selectedSortOrder;
3780                                         }
3781
3782                                         handleDisplay();
3783                                         enableQS(true);
3784                                         ajaxStatus.hideStatus();
3785                                 }
3786                                 url =   'index.php?module=' + module + '&action=index&search_form_only=true&to_pdf=true&search_form_view=' + theView;
3787
3788                                 //check to see if tpl has been specified.  If so then pass location through url string
3789                                 var tpl ='';
3790                                 if(document.getElementById('search_tpl') !=null && typeof(document.getElementById('search_tpl')) != 'undefined'){
3791                                         tpl = document.getElementById('search_tpl').value;
3792                                         if(tpl != ''){url += '&search_tpl='+tpl;}
3793                                 }
3794
3795                                 if(theView == 'saved_views') // handle the tab chooser
3796                                         url += '&displayColumns=' + SUGAR.savedViews.displayColumns + '&hideTabs=' + SUGAR.savedViews.hideTabs + '&orderBy=' + SUGAR.savedViews.selectedOrderBy + '&sortOrder=' + SUGAR.savedViews.selectedSortOrder;
3797
3798                                 var cObj = YAHOO.util.Connect.asyncRequest('GET', url, {success: success, failure: success});
3799                         }
3800                         else { // that form already retrieved
3801                                 handleDisplay();
3802                         }
3803                 },
3804
3805                 // copies one input to another
3806                 copyElement: function(inputName, copyFromElement) {
3807                         switch(copyFromElement.type) {
3808                                 case 'select-one':
3809                                 case 'text':
3810                                         document.search_form[inputName].value = copyFromElement.value;
3811                                         break;
3812                         }
3813                 },
3814         // This function is here to clear the form, instead of "resubmitting it
3815                 clear_form: function(form, skipElementNames) {
3816             var elemList = form.elements;
3817             var elem;
3818             var elemType;
3819
3820             for( var i = 0; i < elemList.length ; i++ ) {
3821                 elem = elemList[i];
3822                 if ( typeof(elem.type) == 'undefined' ) {
3823                     continue;
3824                 }
3825
3826                 if ( typeof(elem.type) != 'undefined' && typeof(skipElementNames) != 'undefined'
3827                         && SUGAR.util.arrayIndexOf(skipElementNames, elem.name) != -1 )
3828                 {
3829                     continue;
3830                 }
3831
3832                 elemType = elem.type.toLowerCase();
3833
3834                 if ( elemType == 'text' || elemType == 'textarea' || elemType == 'password' ) {
3835                     elem.value = '';
3836                 } else if (elemType == 'select-one') {
3837                     // We have, what I hope, is a select box, time to unselect all options
3838                     var optionList = elem.options,
3839                         selectedIndex = 0;
3840                     for (var ii = 0; ii < optionList.length; ii++) {
3841                         if (optionList[ii].value == '') {
3842                             selectedIndex = ii;
3843                             break;
3844                         }
3845                     }
3846                     if (optionList.length > 0) {
3847                         optionList[selectedIndex].selected = "selected";
3848                     }
3849                 } else if (elemType == 'select-multiple') {
3850                     var optionList = elem.options;
3851                     for ( var ii = 0 ; ii < optionList.length ; ii++ ) {
3852                         optionList[ii].selected = false;
3853                     }
3854                 }
3855                 else if ( elemType == 'radio' || elemType == 'checkbox' ) {
3856                     elem.checked = false;
3857                     elem.selected = false;
3858                 }
3859                 else if ( elemType == 'hidden' ) {
3860                     if (
3861                         // For bean selection
3862                         elem.name.indexOf("_id") != -1
3863                         // For custom fields
3864                         || elem.name.indexOf("_c") != -1
3865                         // For advanced fields, like team collection, or datetime fields
3866                         || elem.name.indexOf("_advanced") != -1
3867                         )
3868                     {
3869                         elem.value = '';
3870                     }
3871                 }
3872             }
3873
3874             // If there are any collections
3875             if (typeof(collection) !== 'undefined')
3876             {
3877                 // Loop through all the collections on the page and run clean_up()
3878                 for (key in collection)
3879                 {
3880                     // Clean up only removes blank fields, if any
3881                     collection[key].clean_up();
3882                 }
3883             }
3884
3885             SUGAR.searchForm.clearBasicSearchOrderToDefault(form);
3886                         SUGAR.savedViews.clearColumns = true;
3887                 },
3888         // This function sets sorting to default Sugar values after BasicSearch Clear button is pressed
3889         clearBasicSearchOrderToDefault: function(form)
3890         {
3891             if(form.elements['searchFormTab']){
3892                 var formType = form.elements['searchFormTab'].value;
3893                 if (formType == 'basic_search')
3894                 {
3895                     form.elements['orderBy'].value = 'DATE_ENTERED';
3896                     form.elements['sortOrder'].value = 'DESC';
3897                 }
3898             }
3899         }
3900         };
3901 }();
3902 // Code for the column/tab chooser used on homepage and in admin section
3903 SUGAR.tabChooser = function () {
3904         var     object_refs = new Array();
3905         return {
3906                         /* Describe certain transfers as invalid */
3907                         frozenOptions: [],
3908
3909                         movementCallback: function(left_side, right_side) {},
3910                         orderCallback: function(left_side, right_side) {},
3911
3912                         freezeOptions: function(left_name, right_name, target) {
3913                                 if(!SUGAR.tabChooser.frozenOptions) { SUGAR.tabChooser.frozenOptions = []; }
3914                                 if(!SUGAR.tabChooser.frozenOptions[left_name]) { SUGAR.tabChooser.frozenOptions[left_name] = []; }
3915                                 if(!SUGAR.tabChooser.frozenOptions[left_name][right_name]) { SUGAR.tabChooser.frozenOptions[left_name][right_name] = []; }
3916                                 if(typeof target == 'array') {
3917                                         for(var i in target) {
3918                                                 SUGAR.tabChooser.frozenOptions[left_name][right_name][target[i]] = true;
3919                                         }
3920                                 } else {
3921                                         SUGAR.tabChooser.frozenOptions[left_name][right_name][target] = true;
3922                                 }
3923                         },
3924
3925                         buildSelectHTML: function(info) {
3926                                 var text = "<select";
3927
3928                         if(typeof (info['select']['size']) != 'undefined') {
3929                                 text +=" size=\""+ info['select']['size'] +"\"";
3930                         }
3931
3932                         if(typeof (info['select']['name']) != 'undefined') {
3933                                 text +=" name=\""+ info['select']['name'] +"\"";
3934                         }
3935
3936                         if(typeof (info['select']['style']) != 'undefined') {
3937                                 text +=" style=\""+ info['select']['style'] +"\"";
3938                         }
3939
3940                         if(typeof (info['select']['onchange']) != 'undefined') {
3941                                 text +=" onChange=\""+ info['select']['onchange'] +"\"";
3942                         }
3943
3944                         if(typeof (info['select']['multiple']) != 'undefined') {
3945                                 text +=" multiple";
3946                         }
3947                         text +=">";
3948
3949                         for(i=0; i<info['options'].length;i++) {
3950                                 option = info['options'][i];
3951                                 text += "<option value=\""+option['value']+"\" ";
3952                                 if ( typeof (option['selected']) != 'undefined' && option['selected']== true) {
3953                                         text += "SELECTED";
3954                                 }
3955                                 text += ">"+option['text']+"</option>";
3956                         }
3957                         text += "</select>";
3958                         return text;
3959                         },
3960
3961                         left_to_right: function(left_name, right_name, left_size, right_size) {
3962                                 SUGAR.savedViews.clearColumns = false;
3963                             var left_td = document.getElementById(left_name+'_td');
3964                             var right_td = document.getElementById(right_name+'_td');
3965
3966                             var display_columns_ref = left_td.getElementsByTagName('select')[0];
3967                             var hidden_columns_ref = right_td.getElementsByTagName('select')[0];
3968
3969                             var selected_left = new Array();
3970                             var notselected_left = new Array();
3971                             var notselected_right = new Array();
3972
3973                             var left_array = new Array();
3974
3975                             var frozen_options = SUGAR.tabChooser.frozenOptions;
3976                             frozen_options = frozen_options && frozen_options[left_name] && frozen_options[left_name][right_name]?frozen_options[left_name][right_name]:[];
3977
3978                                 // determine which options are selected in left
3979                             for (i=0; i < display_columns_ref.options.length; i++)
3980                             {
3981                                 if ( display_columns_ref.options[i].selected == true && !frozen_options[display_columns_ref.options[i].value])
3982                                 {
3983                                     selected_left[selected_left.length] = {text: display_columns_ref.options[i].text, value: display_columns_ref.options[i].value};
3984                                 }
3985                                 else
3986                                 {
3987                                     notselected_left[notselected_left.length] = {text: display_columns_ref.options[i].text, value: display_columns_ref.options[i].value};
3988                                 }
3989
3990                             }
3991
3992                             for (i=0; i < hidden_columns_ref.options.length; i++)
3993                             {
3994                                 notselected_right[notselected_right.length] = {text:hidden_columns_ref.options[i].text, value:hidden_columns_ref.options[i].value};
3995
3996                             }
3997
3998                             var left_select_html_info = new Object();
3999                             var left_options = new Array();
4000                             var left_select = new Object();
4001
4002                             left_select['name'] = left_name+'[]';
4003                             left_select['id'] = left_name;
4004                             left_select['size'] = left_size;
4005                             left_select['multiple'] = 'true';
4006
4007                             var right_select_html_info = new Object();
4008                             var right_options = new Array();
4009                             var right_select = new Object();
4010
4011                             right_select['name'] = right_name+'[]';
4012                             right_select['id'] = right_name;
4013                             right_select['size'] = right_size;
4014                             right_select['multiple'] = 'true';
4015
4016                             for (i = 0; i < notselected_right.length; i++) {
4017                                 right_options[right_options.length] = notselected_right[i];
4018                             }
4019
4020                             for (i = 0; i < selected_left.length; i++) {
4021                                 right_options[right_options.length] = selected_left[i];
4022                             }
4023                             for (i = 0; i < notselected_left.length; i++) {
4024                                 left_options[left_options.length] = notselected_left[i];
4025                             }
4026                             left_select_html_info['options'] = left_options;
4027                             left_select_html_info['select'] = left_select;
4028                             right_select_html_info['options'] = right_options;
4029                             right_select_html_info['select'] = right_select;
4030                             right_select_html_info['style'] = 'background: lightgrey';
4031
4032                             var left_html = this.buildSelectHTML(left_select_html_info);
4033                             var right_html = this.buildSelectHTML(right_select_html_info);
4034
4035                             left_td.innerHTML = left_html;
4036                             right_td.innerHTML = right_html;
4037
4038                                 object_refs[left_name] = left_td.getElementsByTagName('select')[0];
4039                                 object_refs[right_name] = right_td.getElementsByTagName('select')[0];
4040
4041                                 this.movementCallback(object_refs[left_name], object_refs[right_name]);
4042
4043                             return false;
4044                         },
4045
4046
4047                         right_to_left: function(left_name, right_name, left_size, right_size, max_left) {
4048                                 SUGAR.savedViews.clearColumns = false;
4049                             var left_td = document.getElementById(left_name+'_td');
4050                             var right_td = document.getElementById(right_name+'_td');
4051
4052                             var display_columns_ref = left_td.getElementsByTagName('select')[0];
4053                             var hidden_columns_ref = right_td.getElementsByTagName('select')[0];
4054
4055                             var selected_right = new Array();
4056                             var notselected_right = new Array();
4057                             var notselected_left = new Array();
4058
4059                             var frozen_options = SUGAR.tabChooser.frozenOptions;
4060                             frozen_options = SUGAR.tabChooser.frozenOptions && SUGAR.tabChooser.frozenOptions[right_name] && SUGAR.tabChooser.frozenOptions[right_name][left_name]?SUGAR.tabChooser.frozenOptions[right_name][left_name]:[];
4061
4062                             for (i=0; i < hidden_columns_ref.options.length; i++)
4063                             {
4064                                 if (hidden_columns_ref.options[i].selected == true && !frozen_options[hidden_columns_ref.options[i].value])
4065                                 {
4066                                     selected_right[selected_right.length] = {text:hidden_columns_ref.options[i].text, value:hidden_columns_ref.options[i].value};
4067                                 }
4068                                 else
4069                                 {
4070                                     notselected_right[notselected_right.length] = {text:hidden_columns_ref.options[i].text, value:hidden_columns_ref.options[i].value};
4071                                 }
4072
4073                             }
4074
4075                             if(max_left != '' && (display_columns_ref.length + selected_right.length) > max_left) {
4076                                 alert('Maximum of ' + max_left + ' columns can be displayed.');
4077                                         return;
4078                             }
4079
4080                             for (i=0; i < display_columns_ref.options.length; i++)
4081                             {
4082                                 notselected_left[notselected_left.length] = {text:display_columns_ref.options[i].text, value:display_columns_ref.options[i].value};
4083
4084                             }
4085
4086                             var left_select_html_info = new Object();
4087                             var left_options = new Array();
4088                             var left_select = new Object();
4089
4090                             left_select['name'] = left_name+'[]';
4091                             left_select['id'] = left_name;
4092                             left_select['multiple'] = 'true';
4093                             left_select['size'] = left_size;
4094
4095                             var right_select_html_info = new Object();
4096                             var right_options = new Array();
4097                             var right_select = new Object();
4098
4099                             right_select['name'] = right_name+ '[]';
4100                             right_select['id'] = right_name;
4101                             right_select['multiple'] = 'true';
4102                             right_select['size'] = right_size;
4103
4104                             for (i = 0; i < notselected_left.length; i++) {
4105                                 left_options[left_options.length] = notselected_left[i];
4106                             }
4107
4108                             for (i = 0; i < selected_right.length; i++) {
4109                                 left_options[left_options.length] = selected_right[i];
4110                             }
4111                             for (i = 0; i < notselected_right.length; i++) {
4112                                 right_options[right_options.length] = notselected_right[i];
4113                             }
4114                             left_select_html_info['options'] = left_options;
4115                             left_select_html_info['select'] = left_select;
4116                             right_select_html_info['options'] = right_options;
4117                             right_select_html_info['select'] = right_select;
4118                             right_select_html_info['style'] = 'background: lightgrey';
4119
4120                             var left_html = this.buildSelectHTML(left_select_html_info);
4121                             var right_html = this.buildSelectHTML(right_select_html_info);
4122
4123                             left_td.innerHTML = left_html;
4124                             right_td.innerHTML = right_html;
4125
4126                                 object_refs[left_name] = left_td.getElementsByTagName('select')[0];
4127                                 object_refs[right_name] = right_td.getElementsByTagName('select')[0];
4128
4129                                 this.movementCallback(object_refs[left_name], object_refs[right_name]);
4130
4131                             return false;
4132                         },
4133
4134                         up: function(name, left_name, right_name) {
4135                                 SUGAR.savedViews.clearColumns = false;
4136                             var left_td = document.getElementById(left_name+'_td');
4137                             var right_td = document.getElementById(right_name+'_td');
4138                             var td = document.getElementById(name+'_td');
4139                             var obj = td.getElementsByTagName('select')[0];
4140                             obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
4141                             if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
4142                                 return false;
4143                             var sel = new Array();
4144
4145                             for (i=0; i<obj.length; i++) {
4146                                 if (obj[i].selected == true) {
4147                                     sel[sel.length] = i;
4148                                 }
4149                             }
4150                             for (i=0; i < sel.length; i++) {
4151                                 if (sel[i] != 0 && !obj[sel[i]-1].selected) {
4152                                     var tmp = new Array(obj[sel[i]-1].text, obj[sel[i]-1].value);
4153                                     obj[sel[i]-1].text = obj[sel[i]].text;
4154                                     obj[sel[i]-1].value = obj[sel[i]].value;
4155                                     obj[sel[i]].text = tmp[0];
4156                                     obj[sel[i]].value = tmp[1];
4157                                     obj[sel[i]-1].selected = true;
4158                                     obj[sel[i]].selected = false;
4159                                 }
4160                             }
4161
4162                                 object_refs[left_name] = left_td.getElementsByTagName('select')[0];
4163                                 object_refs[right_name] = right_td.getElementsByTagName('select')[0];
4164
4165                                 this.orderCallback(object_refs[left_name], object_refs[right_name]);
4166
4167                             return false;
4168                         },
4169
4170                         down: function(name, left_name, right_name) {
4171                                 SUGAR.savedViews.clearColumns = false;
4172                                 var left_td = document.getElementById(left_name+'_td');
4173                             var right_td = document.getElementById(right_name+'_td');
4174                             var td = document.getElementById(name+'_td');
4175                             var obj = td.getElementsByTagName('select')[0];
4176                             if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
4177                                 return false;
4178                             var sel = new Array();
4179                             for (i=obj.length-1; i>-1; i--) {
4180                                 if (obj[i].selected == true) {
4181                                     sel[sel.length] = i;
4182                                 }
4183                             }
4184                             for (i=0; i < sel.length; i++) {
4185                                 if (sel[i] != obj.length-1 && !obj[sel[i]+1].selected) {
4186                                     var tmp = new Array(obj[sel[i]+1].text, obj[sel[i]+1].value);
4187                                     obj[sel[i]+1].text = obj[sel[i]].text;
4188                                     obj[sel[i]+1].value = obj[sel[i]].value;
4189                                     obj[sel[i]].text = tmp[0];
4190                                     obj[sel[i]].value = tmp[1];
4191                                     obj[sel[i]+1].selected = true;
4192                                     obj[sel[i]].selected = false;
4193                                 }
4194                             }
4195
4196                                 object_refs[left_name] = left_td.getElementsByTagName('select')[0];
4197                                 object_refs[right_name] = right_td.getElementsByTagName('select')[0];
4198
4199                                 this.orderCallback(object_refs[left_name], object_refs[right_name]);
4200
4201                             return false;
4202                         }
4203                 };
4204 }(); // end tabChooser
4205
4206 SUGAR.language = function() {
4207     return {
4208         languages : new Array(),
4209
4210         setLanguage: function(module, data) {
4211            if (!SUGAR.language.languages) {
4212
4213            }
4214             SUGAR.language.languages[module] = data;
4215         },
4216
4217         get: function(module, str) {
4218             if(typeof SUGAR.language.languages[module] == 'undefined' || typeof SUGAR.language.languages[module][str] == 'undefined')
4219             {
4220                 return 'undefined';
4221             }
4222             return SUGAR.language.languages[module][str];
4223         },
4224
4225         translate: function(module, str)
4226         {
4227             text = this.get(module, str);
4228             return text != 'undefined' ? text : this.get('app_strings', str);
4229         }
4230     }
4231 }();
4232
4233 SUGAR.contextMenu = function() {
4234         return {
4235                 objects: new Object(),
4236                 objectTypes: new Object(),
4237                 /**
4238                  * Registers a new object for the context menu.
4239                  * objectType - name of the type
4240                  * id - element id
4241                  * metaData - metaData to pass to the action function
4242                  **/
4243                 registerObject: function(objectType, id, metaData) {
4244                         SUGAR.contextMenu.objects[id] = new Object();
4245             SUGAR.contextMenu.objects[id] = {'objectType' : objectType, 'metaData' : metaData};
4246                 },
4247                 /**
4248                  * Registers a new object type
4249                  * name - name of the type
4250                  * menuItems - array of menu items
4251                  **/
4252                 registerObjectType: function(name, menuItems) {
4253                         SUGAR.contextMenu.objectTypes[name] = new Object();
4254                         SUGAR.contextMenu.objectTypes[name] = {'menuItems' : menuItems, 'objects' : new Array()};
4255                 },
4256                 /**
4257                  * Determines which menu item was clicked
4258                  **/
4259                 getListItemFromEventTarget: function(p_oNode) {
4260             var oLI;
4261             if(p_oNode.tagName == "LI") {
4262                     oLI = p_oNode;
4263             }
4264             else {
4265                     do {
4266                         if(p_oNode.tagName == "LI") {
4267                             oLI = p_oNode;
4268                             break;
4269                         }
4270
4271                     } while((p_oNode = p_oNode.parentNode));
4272                 }
4273             return oLI;
4274          },
4275          /**
4276           * handles movement within context menu
4277           **/
4278          onContextMenuMove: function() {
4279             var oNode = this.contextEventTarget;
4280             var bDisabled = (oNode.tagName == "UL");
4281             var i = this.getItemGroups()[0].length - 1;
4282             do {
4283                 this.getItem(i).cfg.setProperty("disabled", bDisabled);
4284             }
4285             while(i--);
4286         },
4287         /**
4288          * handles clicks on a context menu ITEM
4289          **/
4290                 onContextMenuItemClick: function(p_sType, p_aArguments, p_oItem) {
4291             var oLI = SUGAR.contextMenu.getListItemFromEventTarget(this.parent.contextEventTarget);
4292             id = this.parent.contextEventTarget.parentNode.id; // id of the target
4293             funct = eval(SUGAR.contextMenu.objectTypes[SUGAR.contextMenu.objects[id]['objectType']]['menuItems'][this.index]['action']);
4294             funct(this.parent.contextEventTarget, SUGAR.contextMenu.objects[id]['metaData']);
4295                 },
4296                 /**
4297                  * Initializes all context menus registered
4298                  **/
4299                 init: function() {
4300                         for(var i in SUGAR.contextMenu.objects) { // make a variable called objects in objectTypes containg references to all triggers
4301                 if(typeof SUGAR.contextMenu.objectTypes[SUGAR.contextMenu.objects[i]['objectType']]['objects'] == 'undefined')
4302                     SUGAR.contextMenu.objectTypes[SUGAR.contextMenu.objects[i]['objectType']]['objects'] = new Array();
4303                                 SUGAR.contextMenu.objectTypes[SUGAR.contextMenu.objects[i]['objectType']]['objects'].push(document.getElementById(i));
4304                         }
4305             // register the menus
4306                         for(var i in SUGAR.contextMenu.objectTypes) {
4307                     var oContextMenu = new YAHOO.widget.ContextMenu(i, {'trigger': SUGAR.contextMenu.objectTypes[i]['objects']});
4308                                 var aMainMenuItems = SUGAR.contextMenu.objectTypes[i]['menuItems'];
4309                     var nMainMenuItems = aMainMenuItems.length;
4310                     var oMenuItem;
4311                     for(var j = 0; j < nMainMenuItems; j++) {
4312                         oMenuItem = new YAHOO.widget.ContextMenuItem(aMainMenuItems[j].text, { helptext: aMainMenuItems[j].helptext });
4313                         oMenuItem.clickEvent.subscribe(SUGAR.contextMenu.onContextMenuItemClick, oMenuItem, true);
4314                         oContextMenu.addItem(oMenuItem);
4315                     }
4316                     //  Add a "move" event handler to the context menu
4317                     oContextMenu.moveEvent.subscribe(SUGAR.contextMenu.onContextMenuMove, oContextMenu, true);
4318                     // Add a "keydown" event handler to the context menu
4319                     oContextMenu.keyDownEvent.subscribe(SUGAR.contextMenu.onContextMenuItemClick, oContextMenu, true);
4320                     // Render the context menu
4321                     oContextMenu.render(document.body);
4322                 }
4323                 }
4324         };
4325 }();
4326
4327 SUGAR.contextMenu.actions = function() {
4328         return {
4329                 /**
4330                  * redirects to a new note with the clicked on object as the target
4331                  **/
4332                 createNote: function(itemClicked, metaData) {
4333                         loc = 'index.php?module=Notes&action=EditView';
4334                         for(i in metaData) {
4335                                 if(i == 'notes_parent_type') loc += '&parent_type=' + metaData[i];
4336                                 else if(i != 'module' && i != 'parent_type') loc += '&' + i + '=' + metaData[i];
4337                         }
4338                         document.location = loc;
4339                 },
4340                 /**
4341                  * redirects to a new note with the clicked on object as the target
4342                  **/
4343                 scheduleMeeting: function(itemClicked, metaData) {
4344                         loc = 'index.php?module=Meetings&action=EditView';
4345                         for(i in metaData) {
4346                                 if(i != 'module') loc += '&' + i + '=' + metaData[i];
4347                         }
4348                         document.location = loc;
4349                 },
4350                 /**
4351                  * redirects to a new note with the clicked on object as the target
4352                  **/
4353                 scheduleCall: function(itemClicked, metaData) {
4354                         loc = 'index.php?module=Calls&action=EditView';
4355                         for(i in metaData) {
4356                                 if(i != 'module') loc += '&' + i + '=' + metaData[i];
4357                         }
4358                         document.location = loc;
4359                 },
4360                 /**
4361                  * redirects to a new contact with the clicked on object as the target
4362                  **/
4363                 createContact: function(itemClicked, metaData) {
4364                         loc = 'index.php?module=Contacts&action=EditView';
4365                         for(i in metaData) {
4366                                 if(i != 'module') loc += '&' + i + '=' + metaData[i];
4367                         }
4368                         document.location = loc;
4369                 },
4370                 /**
4371                  * redirects to a new task with the clicked on object as the target
4372                  **/
4373                 createTask: function(itemClicked, metaData) {
4374                         loc = 'index.php?module=Tasks&action=EditView';
4375                         for(i in metaData) {
4376                                 if(i != 'module') loc += '&' + i + '=' + metaData[i];
4377                         }
4378                         document.location = loc;
4379                 },
4380                 /**
4381                  * redirects to a new opportunity with the clicked on object as the target
4382                  **/
4383                 createOpportunity: function(itemClicked, metaData) {
4384                         loc = 'index.php?module=Opportunities&action=EditView';
4385                         for(i in metaData) {
4386                                 if(i != 'module') loc += '&' + i + '=' + metaData[i];
4387                         }
4388                         document.location = loc;
4389                 },
4390                 /**
4391                  * redirects to a new opportunity with the clicked on object as the target
4392                  **/
4393                 createCase: function(itemClicked, metaData) {
4394                         loc = 'index.php?module=Cases&action=EditView';
4395                         for(i in metaData) {
4396                                 if(i != 'module') loc += '&' + i + '=' + metaData[i];
4397                         }
4398                         document.location = loc;
4399                 },
4400                 /**
4401                  * handles add to favorites menu selection
4402                  **/
4403                 addToFavorites: function(itemClicked, metaData) {
4404                         success = function(data) {
4405                         }
4406                         var cObj = YAHOO.util.Connect.asyncRequest('GET', 'index.php?to_pdf=true&module=Home&action=AddToFavorites&target_id=' + metaData['id'] + '&target_module=' + metaData['module'], {success: success, failure: success});
4407
4408                 }
4409         };
4410 }();
4411 //if(typeof YAHOO != 'undefined') YAHOO.util.Event.addListener(window, 'load', SUGAR.contextMenu.init);
4412
4413 // initially from popup_parent_helper.js
4414 var popup_request_data;
4415 var close_popup;
4416
4417 function get_popup_request_data()
4418 {
4419         return YAHOO.lang.JSON.stringify(window.document.popup_request_data);
4420 }
4421
4422 function get_close_popup()
4423 {
4424         return window.document.close_popup;
4425 }
4426
4427 function open_popup(module_name, width, height, initial_filter, close_popup, hide_clear_button, popup_request_data, popup_mode, create, metadata)
4428 {
4429         if (typeof(popupCount) == "undefined" || popupCount == 0)
4430            popupCount = 1;
4431
4432         // set the variables that the popup will pull from
4433         window.document.popup_request_data = popup_request_data;
4434         window.document.close_popup = close_popup;
4435
4436         //globally changing width and height of standard pop up window from 600 x 400 to 800 x 800
4437         width = (width == 600) ? 800 : width;
4438         height = (height == 400) ? 800 : height;
4439
4440         // launch the popup
4441         URL = 'index.php?'
4442                 + 'module=' + module_name
4443                 + '&action=Popup';
4444
4445         if (initial_filter != '') {
4446                 URL += '&query=true' + initial_filter;
4447                 // Bug 41891 - Popup Window Name
4448                 popupName = initial_filter.replace(/[^a-z_0-9]+/ig, '_');
4449                 windowName = module_name + '_popup_window' + popupName;
4450         } else {
4451                 windowName = module_name + '_popup_window' + popupCount;
4452         }
4453         popupCount++;
4454
4455         if (hide_clear_button) {
4456                 URL += '&hide_clear_button=true';
4457         }
4458
4459         windowFeatures = 'width=' + width
4460                 + ',height=' + height
4461                 + ',resizable=1,scrollbars=1';
4462
4463         if (popup_mode == '' || popup_mode == undefined) {
4464                 popup_mode='single';
4465         }
4466         URL+='&mode='+popup_mode;
4467         if (create == '' || create == undefined) {
4468                 create = 'false';
4469         }
4470         URL+='&create='+create;
4471
4472         if (metadata != '' && metadata != undefined) {
4473                 URL+='&metadata='+metadata;
4474         }
4475
4476     // Bug #46842 : The relate field field_to_name_array fails to copy over custom fields
4477     // post fields that should be populated from popup form
4478         if(popup_request_data.jsonObject) {
4479                 var request_data = popup_request_data.jsonObject;
4480         } else {
4481                 var request_data = popup_request_data;
4482         }
4483     var field_to_name_array_url = '';
4484
4485     if (request_data && request_data.field_to_name_array != undefined) {
4486         for(var key in request_data.field_to_name_array) {
4487             if ( key.toLowerCase() != 'id' ) {
4488                 field_to_name_array_url += '&field_to_name[]='+encodeURIComponent(key.toLowerCase());
4489             }
4490         }
4491     }
4492     if ( field_to_name_array_url ) {
4493         URL+=field_to_name_array_url;
4494     }
4495
4496         win = SUGAR.util.openWindow(URL, windowName, windowFeatures);
4497
4498         if(window.focus)
4499         {
4500                 // put the focus on the popup if the browser supports the focus() method
4501                 win.focus();
4502         }
4503
4504         win.popupCount = popupCount;
4505
4506         return win;
4507 }
4508
4509 /**
4510  * The reply data must be a JSON array structured with the following information:
4511  *  1) form name to populate
4512  *  2) associative array of input names to values for populating the form
4513  */
4514 var from_popup_return  = false;
4515
4516 //Function replaces special HTML chars for usage in text boxes
4517 function replaceHTMLChars(value) {
4518         return value.replace(/&amp;/gi,'&').replace(/&lt;/gi,'<').replace(/&gt;/gi,'>').replace(/&#039;/gi,'\'').replace(/&quot;/gi,'"');
4519 }
4520
4521 function set_return_basic(popup_reply_data,filter)
4522 {
4523         var form_name = popup_reply_data.form_name;
4524         var name_to_value_array = popup_reply_data.name_to_value_array;
4525         for (var the_key in name_to_value_array)
4526         {
4527                 if(the_key == 'toJSON')
4528                 {
4529                         /* just ignore */
4530                 }
4531                 else if(the_key.match(filter))
4532                 {
4533                         var displayValue=replaceHTMLChars(name_to_value_array[the_key]);
4534                         // begin andopes change: support for enum fields (SELECT)
4535                         if(window.document.forms[form_name] && window.document.forms[form_name].elements[the_key]) {
4536                                 if(window.document.forms[form_name].elements[the_key].tagName == 'SELECT') {
4537                                         var selectField = window.document.forms[form_name].elements[the_key];
4538                                         for(var i = 0; i < selectField.options.length; i++) {
4539                                                 if(selectField.options[i].text == displayValue) {
4540                                                         selectField.options[i].selected = true;
4541                             SUGAR.util.callOnChangeListers(selectField);
4542                                                         break;
4543                                                 }
4544                                         }
4545                                 } else {
4546                                         window.document.forms[form_name].elements[the_key].value = displayValue;
4547                     SUGAR.util.callOnChangeListers(window.document.forms[form_name].elements[the_key]);
4548                                 }
4549                         }
4550                         // end andopes change: support for enum fields (SELECT)
4551                 }
4552         }
4553 }
4554
4555 function set_return(popup_reply_data)
4556 {
4557         from_popup_return = true;
4558         var form_name = popup_reply_data.form_name;
4559         var name_to_value_array = popup_reply_data.name_to_value_array;
4560         if(typeof name_to_value_array != 'undefined' && name_to_value_array['account_id'])
4561         {
4562                 var label_str = '';
4563                 var label_data_str = '';
4564                 var current_label_data_str = '';
4565                 var popupConfirm = popup_reply_data.popupConfirm;
4566                 for (var the_key in name_to_value_array)
4567                 {
4568                         if(the_key == 'toJSON')
4569                         {
4570                                 /* just ignore */
4571                         }
4572                         else
4573                         {
4574                                 var displayValue=replaceHTMLChars(name_to_value_array[the_key]);
4575                                 if(window.document.forms[form_name] && document.getElementById(the_key+'_label') && !the_key.match(/account/)) {
4576                     var data_label = document.getElementById(the_key+'_label').innerHTML.replace(/\n/gi,'').replace(/<\/?[^>]+(>|$)/g, "");
4577                                         label_str += data_label + ' \n';
4578                                         label_data_str += data_label  + ' ' + displayValue + '\n';
4579                                         if(window.document.forms[form_name].elements[the_key]) {
4580                                                 current_label_data_str += data_label + ' ' + window.document.forms[form_name].elements[the_key].value +'\n';
4581                                         }
4582                                 }
4583                         }
4584                 }
4585
4586         if(label_data_str != label_str && current_label_data_str != label_str){
4587                 // Bug 48726 Start
4588                 if (typeof popupConfirm != 'undefined')
4589                 {
4590                         if (popupConfirm > -1) {
4591                                 set_return_basic(popup_reply_data,/\S/);
4592                         } else {
4593                                 set_return_basic(popup_reply_data,/account/);
4594                         }
4595                 }
4596                 // Bug 48726 End
4597                 else if(confirm(SUGAR.language.get('app_strings', 'NTC_OVERWRITE_ADDRESS_PHONE_CONFIRM') + '\n\n' + label_data_str))
4598                         {
4599                         set_return_basic(popup_reply_data,/\S/);
4600                         }
4601                 else
4602                         {
4603                         set_return_basic(popup_reply_data,/account/);
4604                         }
4605                 }else if(label_data_str != label_str && current_label_data_str == label_str){
4606                         set_return_basic(popup_reply_data,/\S/);
4607                 }else if(label_data_str == label_str){
4608                         set_return_basic(popup_reply_data,/account/);
4609                 }
4610         }else{
4611                 set_return_basic(popup_reply_data,/\S/);
4612         }
4613 }
4614
4615 function set_return_lead_conv(popup_reply_data) {
4616     set_return(popup_reply_data);
4617     if (document.getElementById('lead_conv_ac_op_sel') && typeof onBlurKeyUpHandler=='function') {
4618         onBlurKeyUpHandler();
4619     }
4620 }
4621
4622 function set_return_and_save(popup_reply_data)
4623 {
4624         var form_name = popup_reply_data.form_name;
4625         var name_to_value_array = popup_reply_data.name_to_value_array;
4626
4627         for (var the_key in name_to_value_array)
4628         {
4629                 if(the_key == 'toJSON')
4630                 {
4631                         /* just ignore */
4632                 }
4633                 else
4634                 {
4635                         window.document.forms[form_name].elements[the_key].value = name_to_value_array[the_key];
4636                 }
4637         }
4638
4639         window.document.forms[form_name].return_module.value = window.document.forms[form_name].module.value;
4640         window.document.forms[form_name].return_action.value = 'DetailView';
4641         window.document.forms[form_name].return_id.value = window.document.forms[form_name].record.value;
4642         window.document.forms[form_name].action.value = 'Save';
4643         window.document.forms[form_name].submit();
4644 }
4645
4646 /**
4647  * This is a helper function to construct the initial filter that can be
4648  * passed into the open_popup() function.  It assumes that there is an
4649  * account_id and account_name field in the given form_name to use to
4650  * construct the intial filter string.
4651  */
4652 function get_initial_filter_by_account(form_name)
4653 {
4654         var account_id = window.document.forms[form_name].account_id.value;
4655         var account_name = escape(window.document.forms[form_name].account_name.value);
4656         var initial_filter = "&account_id=" + account_id + "&account_name=" + account_name;
4657
4658         return initial_filter;
4659 }
4660 // end code from popup_parent_helper.js
4661
4662 // begin code for address copy
4663 /**
4664  * This is a function used by the Address widget that will fill
4665  * in the given array fields using the fromKey and toKey as a
4666  * prefix into the form objects HTML elements.
4667  *
4668  * @param form The HTML form object to parse
4669  * @param fromKey The prefix of elements to copy from
4670  * @param toKey The prefix of elements to copy into
4671  * @return boolean true if successful, false otherwise
4672  */
4673 function copyAddress(form, fromKey, toKey) {
4674
4675     var elems = new Array("address_street", "address_city", "address_state", "address_postalcode", "address_country");
4676     var checkbox = document.getElementById(toKey + "_checkbox");
4677
4678     if(typeof checkbox != "undefined") {
4679         if(!checkbox.checked) {
4680                     for(x in elems) {
4681                         t = toKey + "_" + elems[x];
4682                             document.getElementById(t).removeAttribute('readonly');
4683                     }
4684         } else {
4685                     for(x in elems) {
4686                             f = fromKey + "_" + elems[x];
4687                             t = toKey + "_" + elems[x];
4688
4689                             document.getElementById(t).value = document.getElementById(f).value;
4690                             document.getElementById(t).setAttribute('readonly', true);
4691                     }
4692             }
4693     }
4694         return true;
4695 }
4696 // end code for address copy
4697
4698 /**
4699  * This function is used in Email Template Module.
4700  * It will check whether the template is used in Campaing->EmailMarketing.
4701  * If true, it will notify user.
4702  */
4703
4704 function check_deletable_EmailTemplate() {
4705         id = document.getElementsByName('record')[0].value;
4706         currentForm = document.getElementById('form');
4707         var call_back = {
4708                 success:function(r) {
4709                         if(r.responseText == 'true') {
4710                                 if(!confirm(SUGAR.language.get('app_strings','NTC_TEMPLATE_IS_USED'))) {
4711                                         return false;
4712                                 }
4713                         } else {
4714                                 if(!confirm(SUGAR.language.get('app_strings','NTC_DELETE_CONFIRMATION'))) {
4715                                         return false;
4716                                 }
4717                         }
4718                         currentForm.return_module.value='EmailTemplates';
4719                         currentForm.return_action.value='ListView';
4720                         currentForm.action.value='Delete';
4721                         currentForm.submit();
4722                 }
4723                 };
4724         url = "index.php?module=EmailTemplates&action=CheckDeletable&from=DetailView&to_pdf=1&record="+id;
4725         YAHOO.util.Connect.asyncRequest('POST',url, call_back,null);
4726 }
4727
4728 SUGAR.image = {
4729      remove_upload_imagefile : function(field_name) {
4730             var field=document.getElementById('remove_imagefile_' + field_name);
4731             field.value=1;
4732
4733             //enable the file upload button.
4734             var field=document.getElementById( field_name);
4735             field.style.display="";
4736
4737             //hide the image and remove button.
4738             var field=document.getElementById('img_' + field_name);
4739             field.style.display="none";
4740             var field=document.getElementById('bt_remove_' + field_name);
4741             field.style.display="none";
4742
4743             if(document.getElementById(field_name + '_duplicate')) {
4744                var field = document.getElementById(field_name + '_duplicate');
4745                field.value = "";
4746             }
4747     },
4748
4749     confirm_imagefile : function(field_name) {
4750             var field=document.getElementById(field_name);
4751             var filename=field.value;
4752             var fileExtension = filename.substring(filename.lastIndexOf(".")+1);
4753             fileExtension = fileExtension.toLowerCase();
4754             if (fileExtension == "jpg" || fileExtension == "jpeg"
4755                 || fileExtension == "gif" || fileExtension == "png" || fileExtension == "bmp"){
4756                     //image file
4757                 }
4758             else{
4759                 field.value=null;
4760                 alert(SUGAR.language.get('app_strings', 'LBL_UPLOAD_IMAGE_FILE_INVALID'));
4761             }
4762     },
4763
4764     lightbox : function(image)
4765         {
4766         if (typeof(SUGAR.image.lighboxWindow) == "undefined")
4767                         SUGAR.image.lighboxWindow = new YAHOO.widget.SimpleDialog('sugarImageViewer', {
4768                     type:'message',
4769                     modal:true,
4770                     id:'sugarMsgWindow',
4771                     close:true,
4772                     title:"Alert",
4773                     msg: "<img src='" + image + "'> </img>",
4774                     buttons: [ ]
4775                 });
4776                 SUGAR.image.lighboxWindow.setBody("<img src='" + image + "'> </img>");
4777                 SUGAR.image.lighboxWindow.render(document.body);
4778         SUGAR.image.lighboxWindow.show();
4779                 SUGAR.image.lighboxWindow.center()
4780     }
4781 }
4782
4783 SUGAR.append(SUGAR.util, {
4784     isTouchScreen: function() {
4785         // first check if we have forced use of the touch enhanced interface
4786         if (Get_Cookie("touchscreen") == '1') {
4787             return true;
4788         }
4789
4790         // next check if we should use the touch interface with our device
4791         if ((navigator.userAgent.match(/iPad/i) != null)) {
4792             return true;
4793         }
4794
4795         return false;
4796     },
4797
4798     isLoginPage: function(content) {
4799         //skip if this is packageManager screen
4800         if(SUGAR.util.isPackageManager()) {return false;}
4801         var loginPageStart = "<!DOCTYPE";
4802         if (content.substr(0, loginPageStart.length) == loginPageStart && content.indexOf("<html>") != -1  && content.indexOf("login_module") != -1) {
4803                 window.location.href = window.location.protocol + window.location.pathname;
4804                 return true;
4805         }
4806     },
4807
4808 isPackageManager: function(){
4809         if(typeof(document.the_form) !='undefined' && typeof(document.the_form.language_pack_escaped) !='undefined'){
4810                 return true;
4811         }else{return false;}
4812 },
4813
4814 ajaxCallInProgress: function(){
4815     var t = true;
4816     //First check if we are in a popup.
4817     if (typeof (send_back) != "function"){
4818         //If the page content is blank, it means we are probably still waiting on something
4819         var c = document.getElementById("content");
4820         if (!c) return true;
4821         t = YAHOO.lang.trim(SUGAR.util.innerText(c));
4822     }
4823     return SUGAR_callsInProgress != 0 || t == "";
4824 },
4825 //Firefox doesn't support innerText (textContent includes script content)
4826 innerText : function(el) {
4827     if (el.tagName == "SCRIPT")
4828         return "";
4829     if(typeof(el.innerText) == "string")
4830         return el.innerText;
4831     var t = "";
4832     for (var i in el.childNodes){
4833         var c = el.childNodes[i];
4834         if (typeof(c) != "object")
4835             continue;
4836         if (typeof(c.nodeName) == "string" && c.nodeName == "#text")
4837             t += c.nodeValue;
4838         else
4839             t += SUGAR.util.innerText(c);
4840     }
4841     return t;
4842 },
4843
4844 callOnChangeListers: function(field){
4845         var listeners = YAHOO.util.Event.getListeners(field, 'change');
4846         if (listeners != null) {
4847                 for (var i = 0; i < listeners.length; i++) {
4848                         var l = listeners[i];
4849                         l.fn.call(l.scope ? l.scope : this, l.obj);
4850                 }
4851         }
4852 },
4853
4854 closeActivityPanel: {
4855     show:function(module,id,new_status,viewType,parentContainerId){
4856         if (SUGAR.util.closeActivityPanel.panel)
4857                         SUGAR.util.closeActivityPanel.panel.destroy();
4858             var singleModule = SUGAR.language.get("app_list_strings", "moduleListSingular")[module];
4859             singleModule = typeof(singleModule != 'undefined') ? singleModule.toLowerCase() : '';
4860             var closeText =  SUGAR.language.get("app_strings", "LBL_CLOSE_ACTIVITY_CONFIRM").replace("#module#",singleModule);
4861         SUGAR.util.closeActivityPanel.panel =
4862             new YAHOO.widget.SimpleDialog("closeActivityDialog",
4863                      { width: "300px",
4864                        fixedcenter: true,
4865                        visible: false,
4866                        draggable: false,
4867                        close: true,
4868                        text: closeText,
4869                        constraintoviewport: true,
4870                        buttons: [ { text:SUGAR.language.get("app_strings", "LBL_EMAIL_OK"), handler:function(){
4871                            if (SUGAR.util.closeActivityPanel.panel)
4872                             SUGAR.util.closeActivityPanel.panel.hide();
4873
4874                         ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));
4875                         var args = "action=save&id=" + id + "&record=" + id + "&status=" + new_status + "&module=" + module;
4876                         // 20110307 Frank Steegmans: Fix for bug 42361, Any field with a default configured in any activity will be set to this default when closed using the close dialog
4877                         // TODO: Take id out and regression test. Left id in for now to not create any other unexpected problems
4878                         //var args = "action=save&id=" + id + "&status=" + new_status + "&module=" + module;
4879                         var callback = {
4880                             success:function(o)
4881                             {
4882                                 // Bug 51984: We need to submit the form just incase we have a form already submitted
4883                                 // so we dont get a popup stating that the form needs to be resubmitted like it doesn,
4884                                 // when you do a reload/refresh
4885                                 window.setTimeout(function(){if(document.getElementById('search_form')) document.getElementById('search_form').submit(); else window.location.reload(true);}, 0);
4886                             },
4887                             argument:{'parentContainerId':parentContainerId}
4888                         };
4889
4890                         YAHOO.util.Connect.asyncRequest('POST', 'index.php', callback, args);
4891
4892                        }, isDefault:true },
4893                                   { text:SUGAR.language.get("app_strings", "LBL_EMAIL_CANCEL"),  handler:function(){SUGAR.util.closeActivityPanel.panel.hide(); }} ]
4894                      } );
4895
4896             SUGAR.util.closeActivityPanel.panel.setHeader(SUGAR.language.get("app_strings", "LBL_CLOSE_ACTIVITY_HEADER"));
4897         SUGAR.util.closeActivityPanel.panel.render(document.body);
4898         SUGAR.util.closeActivityPanel.panel.show();
4899     }
4900 },
4901
4902 setEmailPasswordDisplay: function(id, exists, formName) {
4903         link = document.getElementById(id+'_link');
4904         pwd = document.getElementById(id);
4905         if(!pwd || !link) return;
4906         if(exists) {
4907             pwd.disabled = true;
4908         pwd.style.display = 'none';
4909         link.style.display = '';
4910         if(typeof(formName) != 'undefined')
4911             removeFromValidate(formName, id);
4912         } else {
4913             pwd.disabled = false;
4914         pwd.style.display = '';
4915         link.style.display = 'none';
4916         }
4917 },
4918
4919 setEmailPasswordEdit: function(id) {
4920         link = document.getElementById(id+'_link');
4921         pwd = document.getElementById(id);
4922         if(!pwd || !link) return;
4923         pwd.disabled = false;
4924         pwd.style.display = '';
4925         link.style.display = 'none';
4926 },
4927
4928     /**
4929      * Compares a filename with a supplied array of allowed file extensions.
4930      * @param fileName string
4931      * @param allowedTypes array of allowed file extensions
4932      * @return bool
4933      */
4934     validateFileExt: function(fileName, allowedTypes) {
4935         var ext = fileName.split('.').pop().toLowerCase();
4936
4937         for (var i = allowedTypes.length; i >= 0; i--) {
4938             if (ext === allowedTypes[i]) {
4939                 return true;
4940             }
4941         }
4942
4943         return false;
4944     },
4945
4946     arrayIndexOf: function(arr, val, start) {
4947         if (typeof arr.indexOf == "function")
4948             return arr.indexOf(val, start);
4949         for (var i = (start || 0), j = arr.length; i < j; i++) {
4950             if (arr[i] === val) {
4951                 return i;
4952             }
4953         }
4954         return -1;
4955     }
4956 });
4957
4958 SUGAR.clearRelateField = function(form, name, id)
4959 {
4960     if (typeof form[name] == "object"){
4961         form[name].value = '';
4962         SUGAR.util.callOnChangeListers(form[name]);
4963     }
4964
4965     if (typeof form[id] == "object"){
4966         form[id].value = '';
4967         SUGAR.util.callOnChangeListers(form[id]);
4968     }
4969 };
4970 if(typeof(SUGAR.AutoComplete) == 'undefined') SUGAR.AutoComplete = {};
4971
4972 SUGAR.AutoComplete.getOptionsArray = function(options_index){
4973     var return_arr = [];
4974
4975     var opts = SUGAR.language.get('app_list_strings', options_index);
4976     if(typeof(opts) != 'undefined'){
4977         for(key in opts){
4978             // Since we are using auto complete, we excluse blank dropdown entries since they can just leave it blank
4979             if(key != '' && opts[key] != ''){
4980                 var item = [];
4981                 item['key'] = key;
4982                 item['text'] = opts[key];
4983                 return_arr.push(item);
4984             }
4985         }
4986     }
4987     return return_arr;
4988 }
4989
4990 if(typeof(SUGAR.MultiEnumAutoComplete) == 'undefined') SUGAR.MultiEnumAutoComplete = {};
4991
4992 SUGAR.MultiEnumAutoComplete.getMultiSelectKeysFromValues = function(options_index, val_string){
4993     var opts = SUGAR.language.get('app_list_strings', options_index);
4994     var selected_values = val_string.split(", ");
4995     // YUI AutoComplete adds a blank. We remove it automatically here
4996     if(selected_values.length > 0 && selected_values.indexOf('') == selected_values.length - 1){
4997         selected_values.pop();
4998     }
4999     var final_arr = new Array();
5000     for(idx in selected_values){
5001         for(o_idx in opts){
5002             if(selected_values[idx] == opts[o_idx]){
5003                 final_arr.push(o_idx);
5004             }
5005         }
5006     }
5007     return final_arr;
5008 }
5009
5010 SUGAR.MultiEnumAutoComplete.getMultiSelectValuesFromKeys = function(options_index, val_string){
5011     var opts = SUGAR.language.get('app_list_strings', options_index);
5012     val_string=val_string.replace(/^\^/,'').replace(/\^$/,'') //fixes bug where string starts or ends with ^
5013     var selected_values = val_string.split("^,^");
5014
5015     // YUI AutoComplete adds a blank. We remove it automatically here
5016     if(selected_values.length > 0 && selected_values.indexOf('') == selected_values.length - 1){
5017         selected_values.pop();
5018     }
5019
5020     var final_arr = new Array();
5021     for(idx in selected_values){
5022         for(o_idx in opts){
5023             if(selected_values[idx] == o_idx){
5024                 final_arr.push(opts[o_idx]);
5025             }
5026         }
5027     }
5028     return final_arr;
5029 }
5030
5031 function convertReportDateTimeToDB(dateValue, timeValue)
5032 {
5033     var date_match = dateValue.match(date_reg_format);
5034     var time_match = timeValue.match(/([0-9]{1,2})\:([0-9]{1,2})([ap]m)/);
5035     if ( date_match != null && time_match != null) {
5036         time_match[1] = parseInt(time_match[1]);
5037         if (time_match[3] == 'pm') {
5038             time_match[1] = time_match[1] + 12;
5039             if (time_match[1] >= 24) {
5040                 time_match[1] = time_match[1] - 24;
5041             }
5042         } else if (time_match[3] == 'am' && time_match[1] == 12) {
5043             time_match[1] = 0;
5044         }
5045         if (time_match[1] < 10) {
5046             time_match[1] = '0' + time_match[1];
5047         }
5048         return date_match[date_reg_positions['Y']] + "-"+date_match[date_reg_positions['m']] + "-"+date_match[date_reg_positions['d']] + ' '+ time_match[1] + ':' + time_match[2] + ':00';
5049     }
5050     return '';
5051 }