]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/modules/Calendar/Cal.js
Release 6.5.0
[Github/sugarcrm.git] / jssource / src_files / modules / Calendar / Cal.js
1 /*********************************************************************************
2  * SugarCRM Community Edition is a customer relationship management program developed by
3  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
4  * 
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Affero General Public License version 3 as published by the
7  * Free Software Foundation with the addition of the following permission added
8  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
9  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
10  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
11  * 
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
15  * details.
16  * 
17  * You should have received a copy of the GNU Affero General Public License along with
18  * this program; if not, see http://www.gnu.org/licenses or write to the Free
19  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  * 
22  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
23  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
24  * 
25  * The interactive user interfaces in modified source and object code versions
26  * of this program must display Appropriate Legal Notices, as required under
27  * Section 5 of the GNU Affero General Public License version 3.
28  * 
29  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
30  * these Appropriate Legal Notices must retain the display of the "Powered by
31  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
32  * technical reasons, the Appropriate Legal Notices must display the words
33  * "Powered by SugarCRM".
34  ********************************************************************************/
35
36         
37         var CAL = {};
38
39         CAL.slot_height = 14;
40         CAL.dropped = 0;
41         CAL.records_openable = true;
42         CAL.moved_from_cell = "";
43         CAL.deleted_id = "";
44         CAL.deleted_module = "";
45         CAL.tmp_header = "";
46         CAL.disable_creating = false;
47         CAL.record_editable = false;
48         CAL.shared_users = {};
49         CAL.shared_users_count = 0;
50         CAL.script_evaled = false;
51         CAL.editDialog = false;
52         CAL.settingsDialog = false;     
53         CAL.sharedDialog = false;
54         CAL.basic = {};
55         CAL.basic.items = {};
56         CAL.update_dd = new YAHOO.util.CustomEvent("update_dd");
57         CAL.dd_registry = new Object();
58         CAL.resize_registry = new Object();
59         CAL.print = false;
60         
61         CAL.dom = YAHOO.util.Dom;
62         CAL.get = YAHOO.util.Dom.get;
63         CAL.query = YAHOO.util.Selector.query;  
64         
65         CAL.arrange_slot = function (cell_id){
66                 if(!cell_id)
67                         return; 
68                 cellElm = document.getElementById(cell_id);
69                 if(cellElm){                            
70                         var total_height = 0;
71                         var prev_i = 0;
72                         var first = 1;
73                         var top = 0;
74                         var height = 0;
75                         var cnt = 0;
76                         var child_cnt = cellElm.childNodes.length;
77                         for(var i = 0; i < child_cnt; i++){
78                                         var width_p = (92 / child_cnt);
79                                         width = width_p.toString() + "%";
80                                         if(cellElm.childNodes[i].tagName == "DIV"){
81                                                 cellElm.childNodes[i].style.top = "-1px";
82                                                 cellElm.childNodes[i].style.left = "-" + (cnt + 1) + "px"; 
83                                                 cellElm.childNodes[i].style.width = width                                                       
84                                                 cnt++;                                          
85                                                 prev_i = i;                                     
86                                         }
87                         }
88                 }
89         }
90         
91         // prevent item overlapping
92         CAL.arrange_column = function(column){
93                 
94                 for(var i = 0; i < column.childNodes.length; i++){
95                         for(var j = 0; j < column.childNodes[i].childNodes.length; j++){
96                                 var el = column.childNodes[i].childNodes[j];
97                                 if(YAHOO.util.Dom.hasClass(el,"empty")){
98                                         el.parentNode.removeChild(el);
99                                         j--;
100                                 }
101                         }
102                 }
103         
104                 var slots = column.childNodes;
105                 
106                 var start = 0;
107                 var end = slots.length; 
108                 var slot_count = end;
109                 var level = 0;
110                 
111                 var affected_slots = new Array();
112                 var affected_items = Array();           
113                 var ol = new Array();           
114                 
115                 // fill ol array with groups of overlapping items to fit them bit later
116                 find_overlapping(null,start,end,level,null);
117                 
118                 // add transparent empty blocks to the left
119                 for(var i = 0; i < ol.length; i++){
120                         var ol_group = ol[i];
121                         var depth = ol_group.depth;
122                         for(var j = 0; j < ol_group.items.length; j++){
123                                 var el_id = ol_group.items[j]['id'];
124                                 var level = ol_group.items[j]['level'];                         
125                                 var el = CAL.get(el_id);                                        
126                                 var node = el;                  
127                                 var pos = 0;
128                                 while(node.previousSibling){
129                                         pos++;
130                                         node = node.previousSibling;
131                                 }                               
132                                 insert_empty_items(el,level - 1 - pos,false);
133                         }                               
134                 }
135                 
136                 // add transparent empty blocks to the right
137                 for(var i = 0; i < ol.length; i++){
138                         var ol_group = ol[i];
139                         var depth = ol_group.depth;
140                         for(var j = 0; j < ol_group.items.length; j++){
141                                 var el_id = ol_group.items[j]['id'];
142                                 
143                                 var el = CAL.get(el_id);
144                                 var cnt = el.parentNode.childNodes.length;
145                                 insert_empty_items(el,depth - cnt,true);
146                         }                               
147                 }
148                 
149                 CAL.each(affected_slots,function(i,v){                  
150                         CAL.arrange_slot(affected_slots[i]);
151                 });             
152         
153
154                 function find_overlapping(el,start,end,level,ol_group){
155                 
156                         if(level > 20)
157                                 return;
158                 
159                         var depth = level;
160
161                         if(el != null){
162                                 if(level == 1){
163                                         ol_group = {};
164                                         ol_group.items = new Array();   
165                                 }
166                                 ol_group.items.push({
167                                         id: el.id,
168                                         level: level
169                                 });                             
170                                 affected_items.push(el.id);                     
171                         }                       
172                                                 
173                         for(var i = start; i < end; i++){
174                                 if(i >= slot_count)
175                                         break;
176                                         
177                                 if(typeof slots[i].childNodes != 'undefined' && typeof slots[i].childNodes[0] != 'undefined'){
178                                 
179                                         var pos = 0;                                    
180                                         if(i == start){                                         
181                                                 var node = slots[i].childNodes[0];
182                                                 while(node.nextSibling && CAL.contains(affected_items,node.id)){                                                                                                                                
183                                                         node = node.nextSibling;
184                                                         pos ++;
185                                                 }                                                                                               
186                                         }
187                                                 
188                                         var current = slots[i].childNodes[pos];                                                                                                         
189                                         var slots_takes = parseInt(current.getAttribute('duration_coef'));
190                                         
191                                         if(CAL.contains(affected_items,current.id))
192                                                 continue;
193                                         
194                                         if(pos == 0){
195                                                 var slot_id = current.parentNode.id;    
196                                                 if(!CAL.contains(affected_slots,slot_id))
197                                                         affected_slots.push(slot_id);
198                                         }
199                                                                                                                                                         
200                                         if(slots_takes > 0){
201                                                 var k = find_overlapping(current, i, i + slots_takes, level + 1,ol_group);
202                                                 if(k > depth)
203                                                         depth = k;                                                      
204                                         }
205                                 }
206                         }                                               
207                         
208                         if(level == 1){
209                                 ol_group.depth = depth;
210                                 ol.push(ol_group);
211                         }
212                                                 
213                         return depth;
214                 }
215                 
216                 function insert_empty_items(el,count,to_end){
217                         var slot = el.parentNode;
218                         for(var i = 0; i < count; i++){
219                                 var empty = document.createElement("div");
220                                 empty.className = "act_item empty";
221                                 empty.style.zIndex = "-1";
222                                 empty.style.width = "1px";                              
223                                 if(to_end == true){
224                                         slot.appendChild(empty);
225                                 }else{  
226                                         slot.insertBefore(empty,slot.firstChild);
227                                 }                                               
228                         }
229                 }
230         
231         }
232         
233         CAL.arrange_advanced = function (){
234                 var nodes = CAL.query("#cal-grid #cal-scrollable .col");
235                 for(var i = 0; i < nodes.length; i++){
236                         CAL.arrange_column(nodes[i]);   
237                 }
238                 CAL.update_dd.fire();
239         }
240                 
241         
242         CAL.create_item = function (params){
243                 var item = params.item;
244                 
245                 var id = params.id + params.id_suffix;
246                 
247                 //var content = "";
248                 //if(params.type == "advanced"){
249                         //content = "<div class='content' " + params.content_style + ">" + params.item_text + "</div>";
250                 //}
251                 
252                 //var el = document.createElement("div");       
253                 //el.innerHTML = "<div class='head'><div class='adicon' onmouseover='return CAL.show_additional_details(" + '"' + id + '"'  +  ");' onmouseout='nd(400); SUGAR.util.clearAdditionalDetailsCall();' >&nbsp;&nbsp;</div><div>" + params.head_text + "</div>" + "</div>" + content; 
254                 
255                 var el = document.createElement("div");
256                 var elHead = document.createElement("div");
257                 elHead.setAttribute("class","head");
258                 
259                 var elHeadInfo = document.createElement("div");
260                 elHeadInfo.setAttribute("class","adicon");
261                 elHeadInfo.setAttribute("id","div_"+id);
262                 
263                 var params_click = new Object();
264                 params_click.id = id;
265                 YAHOO.util.Event.on(elHeadInfo,"click",function(e){
266                          YAHOO.util.Event.stopPropagation(e); 
267                                 CAL.show_additional_details(params_click.id);
268                 },params_click);
269                 
270                 
271                 
272                 var head_text = document.createElement("div");
273                 head_text.innerHTML = params.head_text;
274                 
275                 
276                 elHead.appendChild(elHeadInfo);
277                 elHead.appendChild(head_text);
278                 
279                 el.appendChild(elHead);
280                 
281                 
282                 if(params.type == "advanced"){
283                 var elContent = document.createElement("div");
284                         elContent.setAttribute("class","content");
285                         if(params.content_style != "") {
286                                 elContent.style[params.content_style] = params.content_style_value;
287                         }
288                         elContent.innerHTML = params.item_text;
289                         el.appendChild(elContent);
290                 }
291                 
292
293                 
294                         
295                         
296                 el.setAttribute("id",id);
297                 el.className = "act_item" + " " + item.type+"_item";    
298                 el.style.backgroundColor = CAL.activity_colors[item.module_name]['body'];
299                 el.style.borderColor = CAL.activity_colors[item.module_name]['border'];                                         
300
301                 el.setAttribute("record",params.id);
302                 el.setAttribute("module_name",item.module_name);
303                 el.setAttribute("record",item.record);
304                 el.setAttribute("status",item.status);  
305                 el.setAttribute("detail",item.detail);
306                 el.setAttribute("edit",item.edit);
307                 if(typeof item.repeat_parent_id != "undefined" && item.repeat_parent_id != "")
308                         el.setAttribute("repeat_parent_id",item.repeat_parent_id);
309                 if(params.type == "basic"){
310                         el.style.left = "-1px";
311                         el.style.display = "block";
312                         el.setAttribute("days",params.days);
313                         el.style.width = ((params.days)*100) + "%";
314                         el.style.top = parseInt(params.position * CAL.slot_height - params.slot.childNodes.length * (CAL.slot_height + 1)) + "px";
315                         
316                 }else{
317                         el.style.height = parseInt((CAL.slot_height + 1) * params.duration_coef - 1) + "px";
318                         el.setAttribute("duration_coef",params.duration_coef);                  
319                 }       
320                 
321                 YAHOO.util.Event.on(el,"mouseover",function(){                          
322                                 if(!CAL.records_openable)
323                                         return;
324                                 CAL.disable_creating = true;
325                                 var e;
326                                 if(e = CAL.get(el.id))
327                                         e.style.zIndex = 2;                              
328                 });
329                 YAHOO.util.Event.on(el,"mouseout",function(event){      
330                                 if(!CAL.records_openable)
331                                         return; 
332                                 var node = event.toElement || event.relatedTarget;
333                                 var i = 3;
334                                 while(i > 0){
335                                         if(node == this)
336                                                 return;
337                                         else
338                                                 node = node.parentNode;
339                                         i--;
340                                 }
341                                 CAL.get(el.id).style.zIndex = '';
342                                 CAL.disable_creating = false;                                           
343                 });
344                 
345                 CAL.clear_additional_details(params.id);        
346                 
347                 return el;              
348         }
349         
350         CAL.make_draggable = function (id,type){
351                 
352                 var border;
353                 var prefix;
354                 var id_prefix;
355                 
356                 border = "cal-grid";
357                 if(type == "basic"){
358                         if(CAL.view != "month" && CAL.view != "shared")
359                                 border = "cal-multiday-bar";
360                         prefix = "basic_";
361                         id_prefix = "b_";
362                 }else{
363                         if(CAL.view != "month")
364                                 border = "cal-scrollable";
365                         prefix = "";
366                         id_prefix = "t_";
367                 }               
368         
369                 var dd = new YAHOO.util.DDCAL(id,prefix+"cal",{isTarget: false,cont: border});
370                 
371                 CAL.dd_registry[id] = dd;                                                                       
372                                                                                         
373                 dd.onInvalidDrop = function(e){ 
374                         if(type == "basic"){
375                                 CAL.basic.populate_grid();
376                                 CAL.fit_grid();
377                         }else{
378                                 CAL.arrange_slot(this.el.parentNode.getAttribute("id"));
379                                 if(CAL.dropped == 0){
380                                         this.el.childNodes[0].innerHTML = CAL.tmp_header;
381                                 }
382                         }                       
383                         CAL.records_openable = true;
384                         CAL.disable_creating = false;                                            
385                 }                                       
386                                                 
387                 dd.onMouseDown = function(e){           
388                         YAHOO.util.DDM.mode = YAHOO.util.DDM.POINT;
389                         YAHOO.util.DDM.clickPixelThresh = 20;
390                 }
391                                 
392                 dd.onMouseUp = function(e){
393                         YAHOO.util.DDM.mode = YAHOO.util.DDM.INTERSECT;
394                         YAHOO.util.DDM.clickPixelThresh = 3;
395                 }
396                                         
397                 dd.startDrag = function(x,y){
398                                                 
399                         this.el = document.getElementById(this.id);
400                         this.el.style.zIndex = 5;
401                         CAL.dropped = 0;
402                         CAL.records_openable = false;
403                         CAL.disable_creating = true;
404                         CAL.tmp_header = this.el.childNodes[0].innerHTML;
405                         CAL.moved_from_cell = this.el.parentNode.id;                                            
406                         
407                         this.setDelta(2,2);
408                 }
409                                         
410                 dd.endDrag  = function(x,y){            
411                         this.el = document.getElementById(this.id);
412                         this.el.style.zIndex = "";
413                 
414                         var nodes = CAL.query("#cal-grid div."+prefix+"slot");
415                         CAL.each(nodes,function(i,v){
416                                 YAHOO.util.Dom.removeClass(nodes[i],"slot_active");
417                         });                                             
418                 }
419                                         
420                 dd.onDragDrop = function(e,id){ 
421                                                         
422                         var slot = document.getElementById(id);
423                         YAHOO.util.Dom.removeClass(slot,"slot_active");
424                         if(CAL.dropped) // prevent dropping on two slots in same time
425                                 return;
426                         CAL.dropped = 1;
427                                                                                                 
428                         this.el.style.position = "relative";
429                         this.el.style.cssFloat = "none";                        
430                                                                         
431                         if(type == "advanced"){                 
432                                 if(CAL.view != 'shared'){
433                                         var box_id = this.id;
434                                         var slot_id = id;
435                                         var ex_slot_id = CAL.moved_from_cell;                           
436                                         CAL.move_activity(box_id,slot_id,ex_slot_id);
437                                 }else{                                  
438                                         var record = this.el.getAttribute("record");
439                                         var tid = id;
440                                         var tar = tid.split("_");                                       
441                                         var timestamp = tar[1];
442                                         var tid = CAL.moved_from_cell;
443                                         var tar = tid.split("_");                                       
444                                         var ex_timestamp = tar[1];
445
446                                         for(i = 0; i < CAL.shared_users_count; i++){            
447                                                 var box_id = record + "____" + i;       
448                                                 var slot_id = id_prefix + timestamp + "_" + i;
449                                                 var ex_slot_id = id_prefix + ex_timestamp + "_" + i;                                            
450                                                 CAL.move_activity(box_id,slot_id,ex_slot_id);
451                                         }                               
452                                 }
453                         }
454                                                 
455                         var callback = {
456                                 success: function(o){
457                                         try{
458                                                 res = eval("("+o.responseText+")");
459                                         }catch(err){
460                                                 alert(CAL.lbl_error_saving);                                                    
461                                                 ajaxStatus.hideStatus();
462                                                 return; 
463                                         }       
464                                         if(res.access == 'yes'){                                                
465                                                 CAL.add_item(res);
466                                                 CAL.disable_creating = false;
467                                                 CAL.records_openable = true;
468                                                 CAL.update_vcal();
469                                                 ajaxStatus.hideStatus();
470                                         
471                                         } else {
472                                                 if (res.errorMessage != 'undefined') {
473                                                         alert(res.errorMessage);                                                        
474                                                 }
475                                                 CAL.refresh();
476                                                 ajaxStatus.hideStatus();        
477                                         }
478                                 }                                                        
479                         };
480
481                         ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));                                         
482                         var url = "index.php?module=Calendar&action=Reschedule&sugar_body_only=true";
483                         var data = {
484                                         "current_module" : this.el.getAttribute("module_name"),
485                                         "record" : this.el.getAttribute("record"),
486                                         "datetime" : slot.getAttribute("datetime"),
487                                         "calendar_style" : type
488                         };                                              
489                         YAHOO.util.Connect.asyncRequest('POST',url,callback,CAL.toURI(data));
490         
491                         YAHOO.util.Dom.removeClass(slot,"slot_active"); 
492                                                 
493                 }
494
495                 dd.onDragOver = function(e,id){
496                         var slot = document.getElementById(id);
497                         if(!YAHOO.util.Dom.hasClass(slot,"slot_active"))
498                                 YAHOO.util.Dom.addClass(slot,"slot_active");
499                         if(type == "advanced")                                          
500                                 this.el.childNodes[0].childNodes[1].childNodes[0].innerHTML = slot.getAttribute('time');                                                                                        
501                 }
502
503                 dd.onDragOut = function(e,id){
504                         var slot = document.getElementById(id);
505                         YAHOO.util.Dom.removeClass(slot,"slot_active");
506                 }               
507         }
508         
509         CAL.make_resizable = function (id,slot){
510         
511                 var pos = 0, e = slot;
512                 while(e = e.previousSibling){ 
513                         pos++;
514                 }               
515                 var max_height = (CAL.cells_per_day - pos) * (CAL.slot_height + 1) - 1;
516                 var old_width;
517                 var resize = new YAHOO.util.Resize(id,{
518                         handles: ['b'],
519                         maxHeight : max_height
520                 });
521                 
522                 CAL.resize_registry[id] = resize;
523                 
524                 resize.on('startResize', function(e){
525                         var el = CAL.get(id);
526                         if(el){
527                                 el.style.zIndex = 3;
528                         }
529                         CAL.records_openable = false;
530                         CAL.disable_creating = true;            
531                 });                                     
532                 resize.on('endResize', function(e){
533         
534                         elm_id = id;
535                         
536                         var duration = e.height / (CAL.slot_height + 1) * CAL.t_step;
537                         var remainder = duration % 15;
538                         if(remainder > 7.5)
539                                 remainder = (-1) * (15 - remainder);                            
540                         duration = duration - remainder;
541                         var duration_hours = parseInt(duration / 60);
542                         var duration_minutes = duration % 60;
543                         
544                         var new_size = (duration / CAL.t_step) * (CAL.slot_height + 1) - 1;
545                                                 
546                         var el = CAL.get(elm_id);
547                         if(el){
548                                 el.style.zIndex = '';                   
549                                 el.style.height = new_size + "px";
550                                 CAL.arrange_slot(slot.id);
551                                 
552                                 var nodes = CAL.query("#cal-grid div.act_item");
553                                 CAL.each(nodes,function(i,v){
554                                         nodes[i].style.zIndex = '';
555                                 });
556                                 
557                                 var duration_coef = duration / CAL.t_step;
558                                 el.setAttribute("duration_coef",duration_coef);                         
559                                 if(duration_coef < 1.75)
560                                         el.childNodes[1].style.display = "none";
561                                 else
562                                         el.childNodes[1].style.display = "";
563
564                                 var callback = {
565                                         success: function(o){
566                                                 try{                                                    
567                                                         res = eval("("+o.responseText+")");
568                                                 }catch(err){
569                                                         alert(CAL.lbl_error_saving);                                            
570                                                         ajaxStatus.hideStatus();
571                                                         return; 
572                                                 }       
573                                                 if(res.access == 'yes'){                                                
574                                                         CAL.update_vcal();
575                                                         CAL.clear_additional_details(el.getAttribute("record"));
576                                                         CAL.arrange_column(slot.parentNode);
577                                                         CAL.update_dd.fire();
578                                                         ajaxStatus.hideStatus();
579                                                         CAL.disable_creating = false;
580                                                         CAL.records_openable = true;
581                                                 }
582                                         }                                                        
583                                 };
584                                 ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));                                         
585                                 var url = "index.php?module=Calendar&action=Resize&sugar_body_only=true";
586                                 var data = {
587                                                 "current_module" : el.getAttribute("module_name"),
588                                                 "record" : el.getAttribute("record"),
589                                                 "duration_hours" : duration_hours,
590                                                 "duration_minutes" : duration_minutes                                           
591                                 };                                              
592                                 YAHOO.util.Connect.asyncRequest('POST',url,callback,CAL.toURI(data));                                                                                   
593                         }                                               
594                 });     
595         }
596         
597         CAL.destroy_ui = function (id){
598                 if(CAL.items_resizable && typeof CAL.resize_registry[id] != "undefined"){
599                         CAL.resize_registry[id].destroy();
600                         delete CAL.resize_registry[id];
601                 }
602                 if(CAL.items_draggable && typeof CAL.dd_registry[id] != "undefined")
603                         CAL.dd_registry[id].unreg();
604                         delete CAL.dd_registry[id];
605         }
606         
607         CAL.basic.remove = function (item){             
608                 if(typeof CAL.basic.items[item.user_id] == 'undefined')
609                         CAL.basic.items[item.user_id] = new Object();                           
610                 delete CAL.basic.items[item.user_id][item.record];              
611         }
612         
613         CAL.basic.add = function (item){
614                 if(typeof CAL.basic.items[item.user_id] == 'undefined')
615                         CAL.basic.items[item.user_id] = new Object();                           
616                 CAL.basic.items[item.user_id][item.record] = item;
617         }
618         
619         CAL.basic.populate_grid = function(){
620
621                 var nodes = CAL.query("#cal-grid .cal-basic .col .act_item");
622                 CAL.each(nodes,function(i,v){
623                         nodes[i].parentNode.removeChild(nodes[i]);
624                 });                     
625
626                 var users_arr = new Array();
627                 if(CAL.view != "shared"){
628                         users_arr.push(CAL.current_user_id);
629                 }else{
630                         CAL.each(CAL.shared_users,function(i,v){
631                                 users_arr.push(i);
632                         });                     
633                 }
634                 
635                 for(ui = 0; ui < users_arr.length; ui++){
636                 
637                         var user_id = users_arr[ui];            
638                         suffix = "";
639                         id_suffix = "";
640                         if(CAL.view == 'shared'){
641                                 suffix = "_" + CAL.shared_users[user_id];       
642                                 id_suffix = '____' + CAL.shared_users[user_id];         
643                         }
644
645                         var step = 7;
646                         if(CAL.view == "day")
647                                 step = 1;
648                 
649                         var portions_count = 1;
650                         if(CAL.view == "month"){
651                                 var e = CAL.get("b_" + CAL.grid_start_ts + suffix);
652                                 if(e)
653                                         portions_count = e.parentNode.parentNode.parentNode.parentNode.parentNode.childNodes.length;
654                                 else
655                                         continue;                       
656                         }
657                                  
658                         var start = CAL.grid_start_ts;  
659                 
660                         for(w = 0; w < portions_count; w++){
661                 
662                                 var end = start + step * 3600 * 24;                     
663                                 var portion = Array();
664                         
665                                 CAL.each(CAL.basic.items[user_id],function(id,item){
666                         
667                                         var c = !((item.ts_start < start && item.ts_end <= start) || (end <= item.ts_start && end <= item.ts_end));                     
668                                         if(c){
669                                                 if(item.ts_start < start)
670                                                         var from = start;
671                                                 else
672                                                         var from = item.ts_start;
673                                                 if(end <= item.ts_end)
674                                                         var to = end;
675                                                 else
676                                                         var to = item.ts_end;
677                                         
678                                                 portion.push({
679                                                         id : id,
680                                                         offset: item.offset,
681                                                         from : from,
682                                                         to : to                         
683                                                 });                             
684                                         }
685                                 });
686                 
687                                 portion.sort(function(a,b){
688                                         return a.offset - b.offset;
689                                 });                     
690                 
691                                 var equalizer = Array();
692                                 for(i = 0; i < step; i++){
693                                         equalizer[i] = 0;
694                                 }
695                                 var max_pos = 0;                
696                                 CAL.each(portion,function(i,v){
697                                         var from = (portion[i].from - start) / (3600 * 24);
698                                         var to = (portion[i].to - start) / (3600 * 24);
699                         
700                                         var pos = 0;
701                                         for(d = from; d < to; d++){
702                                                 if(equalizer[d] > pos)
703                                                         pos = equalizer[d];
704                                         }
705                                         for(d = from; d < to; d++){
706                                                 equalizer[d] = pos + 1;
707                                         }
708                                 
709                                         portion[i].position = pos;
710                                         portion[i].days = to - from;                            
711                         
712                                         if(pos > max_pos)
713                                                 max_pos = pos; 
714                                 
715                                         var item = CAL.basic.items[user_id][portion[i].id];
716                                                 
717                                         var slot = CAL.get("b_" + portion[i].from + suffix);
718                                         if(slot){
719                                                 var time_start = item.time_start;
720                                                 if(item.ts_start < start)
721                                                         time_start = "...&nbsp;";
722                                                 
723                                                 var head_text = CAL.get_header_text(item.type,time_start,item.name,item.record);
724
725                                                 var el = CAL.create_item({
726                                                         item: item,
727                                                         type: 'basic',
728                                                         head_text: head_text,
729                                                         id: portion[i].id,
730                                                         position: portion[i].position,
731                                                         slot: slot,
732                                                         id_suffix: id_suffix,
733                                                         days: portion[i].days                   
734                                                 });
735                                                 
736                                                 YAHOO.util.Event.on(el,"click",function(){
737                                                                 if(this.getAttribute('detail') == "1")
738                                                                         CAL.load_form(this.getAttribute('module_name'),this.getAttribute('record'),false);
739                                                 });
740                                 
741                                                 slot.appendChild(el);
742                                                 
743                                                 if(CAL.items_draggable && item.edit == 1){
744                                                         CAL.make_draggable(el.getAttribute("id"),"basic");
745                                                 }
746                                         }                       
747                                 });
748                 
749                                 h = parseInt((max_pos + 1) * CAL.slot_height + (CAL.slot_height + 1));                                  
750                                 if(h < CAL.basic.min_height)
751                                         h = CAL.basic.min_height;
752                                         
753                                 
754                                 var height_string = h + "px";           
755                                 var row = CAL.get("b_" + start + suffix).parentNode.parentNode;
756                                         
757                 
758                                 row.parentNode.childNodes[0].childNodes[0].style.height = height_string;
759                                 CAL.each(row.childNodes,function(i,v){
760                                         if(typeof row.childNodes[i] == 'object')                                
761                                                 row.childNodes[i].childNodes[0].style.height = height_string;
762                                 });                                     
763                         
764                                 start = start + step * 3600 * 24;               
765                         }
766                 }                       
767         }
768         
769         
770         
771         CAL.add_item_to_grid = function (item){ 
772                                                         
773                         var suffix = "";
774                         var id_suffix = "";                     
775                         
776                         if( item.user_id != "" && CAL.view == 'shared'){
777                                 suffix = "_" + CAL.shared_users[item.user_id];  
778                                 id_suffix = '____' + CAL.shared_users[item.user_id];            
779                         }
780                         
781                         var e = CAL.get(item.record + id_suffix);
782                         if(e){
783                                 e.parentNode.removeChild(e);
784                                 CAL.destroy_ui(item.record + id_suffix);
785                         }
786                         
787                         CAL.basic.remove(item);
788             //Add item to top grid if items have a duration > 1 day and view style is basic
789                         if(CAL.style == "basic" || item.days > 1){
790                                 CAL.basic.add(item);
791                                 return;
792                         }
793                                 
794                         var head_text = CAL.get_header_text(item.type,item.time_start,item.name,item.record);                                   
795                         var time_cell = item.timestamp - item.timestamp % (CAL.t_step * 60);                    
796                         var duration_coef; 
797                         if(item.module_name == 'Tasks'){
798                                 duration_coef = 1;
799                         }else{  
800                                 if((item.duration_minutes < CAL.t_step) && (item.duration_hours == 0))
801                                         duration_coef = 1;
802                                 else
803                                         duration_coef = (parseInt(item.duration_hours) * 60 + parseInt(item.duration_minutes)) / CAL.t_step;
804                         }
805
806                         var item_text = SUGAR.language.languages.app_list_strings[item.type +'_status_dom'][item.status];
807                         
808                         var content_style = "";
809                         var content_style_value = "";
810                         if(duration_coef < 1.75) {
811                                 content_style = "display";
812                                 content_style_value = "none";
813                         }
814                         
815                         var elm_id = item.record + id_suffix;
816                                                 
817                         var el = CAL.create_item({
818                                 item: item,
819                                 type: 'advanced',
820                                 head_text: head_text,
821                                 duration_coef: duration_coef,
822                                 id: item.record,
823                                 id_suffix: id_suffix,
824                                 item_text: item_text,
825                                 content_style: content_style,
826                                 content_style_value: content_style_value
827                         });
828                         
829                         YAHOO.util.Event.on(el,"click",function(){
830                                         if(this.getAttribute('detail') == "1")
831                                                 CAL.load_form(this.getAttribute('module_name'),this.getAttribute('record'),false);
832                         });                     
833                         var slot;
834                         if(slot = CAL.get("t_" + time_cell + suffix)){
835
836                                 slot.appendChild(el);   
837                                 
838                                 if(item.edit == 1){
839                                         if(CAL.items_draggable)                         
840                                                 CAL.make_draggable(elm_id,"advanced");
841                                         if(item.module_name != "Tasks" && CAL.items_resizable)                          
842                                                     CAL.make_resizable(elm_id,slot);
843                 }
844
845                                 CAL.cut_record(item.record + id_suffix);                                
846                                 //CAL.arrange_slot("t_" + time_cell + suffix);
847                         }
848                                 
849         }
850
851         CAL.get_header_text = function (type,time_start,text,record){
852                         var start_text = "<span class='start_time'>" + time_start + "</span> " + text;
853                         return start_text;
854         }
855         
856         CAL.cut_record = function (id){ 
857                         var el = CAL.get(id);                   
858                         if(!el)
859                                 return;                         
860                         var duration_coef = el.getAttribute("duration_coef");                   
861                         real_celcount = CAL.cells_per_day;                      
862                         var celpos = 0;                 
863                         var s = el.parentNode;
864                         while(s.previousSibling){
865                                 celpos++;
866                                 s = s.previousSibling;
867                         }                       
868                         celpos = celpos + 1;                    
869                         if(real_celcount - celpos - duration_coef < 0)
870                                 duration_coef = real_celcount - celpos + 1;                                                     
871                         el.style.height = parseInt((CAL.slot_height + 1) * duration_coef - 1) + "px";                   
872         }
873
874         CAL.init_edit_dialog = function (params){
875                 CAL.editDialog = false;
876                 
877                 var rd = CAL.get("cal-edit");
878                 var content = CAL.get("edit-dialog-content");
879                 
880                 if(CAL.dashlet && rd){
881                         document.getElementById("content").appendChild(rd);
882                 }
883                         
884                 rd.style.width = params.width + "px";                           
885                 content.style.height = params.height + "px";
886                 content.style.overflow = "auto";
887                 content.style.padding = "0";
888
889                 CAL.editDialog = new YAHOO.widget.Dialog("cal-edit",{ 
890                         draggable : true,
891                         visible : false,
892                         modal : true,
893                         close : true,
894                         zIndex : 10
895                 });
896                 var listeners = new YAHOO.util.KeyListener(document, { keys : 27 }, {fn: function() { CAL.editDialog.cancel();} } );
897                 CAL.editDialog.cfg.queueProperty("keylisteners", listeners);
898                 
899                 CAL.editDialog.cancelEvent.subscribe(function(e, a, o){                                 
900                         CAL.close_edit_dialog();
901                 });
902                 
903                 rd.style.display = "block";
904                 CAL.editDialog.render();
905                 
906                 rd.style.overflow = "auto";
907                 rd.style.overflowX = "hidden";
908                 rd.style.outline = "0 none";
909                 rd.style.height = "auto";
910         }
911         
912
913         CAL.open_edit_dialog = function (params){
914                                                 
915                                                 document.getElementById("form_content").innerHTML = "";
916                                                 
917                                                 CAL.editDialog.center();                                                
918                                                 CAL.editDialog.show();          
919                                                 
920                                                 var nodes = CAL.query("#cal-tabs li a");
921                                                 CAL.each(nodes,function(i,v){
922                                                         YAHOO.util.Event.on(nodes[i], 'click', function(){      
923                                                                 CAL.select_tab(this.getAttribute("tabname"));
924                                                         });
925                                                 });
926                                                 
927                                                 stay_on_tab = false
928                                                 if(typeof params != "undefined" && typeof params.stay_on_tab != "undefined" && params.stay_on_tab)
929                                                         stay_on_tab = true;
930                                                 
931                                                 if(!stay_on_tab){
932                                                         var nodes_li = CAL.query("#cal-tabs li");
933                                                         CAL.each(nodes_li,function(j,v){                                                                                                                                                
934                                                                 CAL.dom.removeClass(nodes_li[j],"selected");
935                                                                 if(j == 0)
936                                                                         CAL.dom.addClass(nodes_li[j],"selected");       
937                                                         });
938                                                 
939                                                         var nodes = CAL.query(".yui-nav");
940                                                         CAL.each(nodes,function(i,v){
941                                                                 nodes[i].style.overflowX = "visible";
942                                                         });
943                                                 }                                       
944
945         }
946
947         CAL.close_edit_dialog = function (){                                    
948                 CAL.reset_edit_dialog();
949         }
950         
951         CAL.remove_edit_dialog = function(){
952                 var rd_c = CAL.get("cal-edit_c");
953                 if(rd_c){
954                         rd_c.parentNode.removeChild(rd_c);
955                 }               
956         }       
957         
958         CAL.reset_edit_dialog = function (){
959                 var e;
960
961                 document.forms["CalendarEditView"].elements["current_module"].value = "Meetings";
962         
963                 CAL.get("radio_call").removeAttribute("disabled");
964                 CAL.get("radio_meeting").removeAttribute("disabled");
965                 CAL.get("radio_call").checked = false;
966                 CAL.get("radio_meeting").checked = true;
967                 
968                 CAL.get("send_invites").value = "";
969         
970                 if(e = CAL.get("record"))
971                         e.value = "";
972                         
973                 if(e = CAL.get("list_div_win"))
974                         e.style.display = "none";
975                         
976                 if(typeof SugarWidgetSchedulerSearch.hideCreateForm != 'undefined')
977                         SugarWidgetSchedulerSearch.hideCreateForm();
978                         
979                 $("#scheduler .schedulerInvitees").css("display", "");
980                 $("#create-invitees-title").css("display", "");
981                 $("#create-invitees-buttons").css("display", "");
982                         
983                 if(CAL.enable_repeat){                  
984                         CAL.reset_repeat_form();
985                 }                       
986         
987                 CAL.GR_update_focus("Meetings",""); 
988                 CAL.select_tab("cal-tab-1");
989                 
990                 QSFieldsArray = new Array();
991                 QSProcessedFieldsArray = new Array();
992         }
993         
994         CAL.reset_repeat_form = function (){
995                         document.forms['CalendarRepeatForm'].reset();
996                         var fields = ['type','interval','count','until','dow'];
997                         CAL.each(fields,function(i,field){
998                                 CAL.get('repeat_' + field).value = "";
999                         });
1000                         toggle_repeat_type();           
1001                         CAL.get("repeat_parent_id").value = "";
1002                         CAL.get("edit_all_recurrences").value = "";
1003                         CAL.get("edit_all_recurrences_block").style.display = "none";
1004                         CAL.get("cal-repeat-block").style.display = "none";     
1005         }
1006
1007         CAL.select_tab = function (tid){        
1008                 var nodes_li = CAL.query("#cal-tabs li");
1009                 CAL.each(nodes_li,function(j,v){
1010                         CAL.dom.removeClass(nodes_li[j],"selected");
1011                 });                                                     
1012                 
1013                 CAL.dom.addClass(CAL.get(tid + "-link").parentNode,"selected");
1014                                                                         
1015                 var nodes = CAL.query("#cal-tabs .yui-content");
1016                 CAL.each(nodes,function(i,v){
1017                         nodes[i].style.display = "none";
1018                 });
1019                 var nodes = CAL.query("#cal-tabs #"+tid);
1020                 CAL.each(nodes,function(i,v){
1021                         nodes[i].style.display = "block";
1022                 });
1023         }
1024         
1025         CAL.fill_repeat_data = function (){
1026         
1027                 if (CAL.enable_repeat && (CAL.get("current_module").value == "Meetings" || CAL.get("current_module").value == "Calls")) {               
1028                         if(repeat_type = document.forms['CalendarRepeatForm'].repeat_type.value){                                                       
1029                                 document.forms['CalendarEditView'].repeat_type.value = repeat_type;
1030                                 document.forms['CalendarEditView'].repeat_interval.value = document.forms['CalendarRepeatForm'].repeat_interval.value;
1031                                 if(document.getElementById("repeat_count_radio").checked){
1032                                         document.forms['CalendarEditView'].repeat_count.value = document.forms['CalendarRepeatForm'].repeat_count.value;
1033                                         document.forms['CalendarEditView'].repeat_until.value = "";
1034                                 }else{
1035                                         document.forms['CalendarEditView'].repeat_until.value = document.forms['CalendarRepeatForm'].repeat_until.value;
1036                                         document.forms['CalendarEditView'].repeat_count.value = "";
1037                                 }
1038                                 if(repeat_type == 'Weekly'){
1039                                         var repeat_dow = "";                                    
1040                                         for(var i = 0; i < 7; i++)
1041                                                 if(CAL.get("repeat_dow_" + i).checked)
1042                                                         repeat_dow += i.toString();
1043                                         CAL.get("repeat_dow").value = repeat_dow;                               
1044                                 }
1045                         }
1046                 }
1047         }
1048         
1049         CAL.fill_repeat_tab = function(data){
1050                 
1051                 if(!CAL.enable_repeat)
1052                         return;
1053         
1054                 if(typeof data.repeat_parent_id != "undefined"){
1055                         CAL.get("cal-repeat-block").style.display = "none";
1056                         CAL.get("edit_all_recurrences_block").style.display = "";
1057                         CAL.get("edit_all_recurrences").value = "";
1058                         CAL.get("repeat_parent_id").value = data.repeat_parent_id;                      
1059                         return;
1060                 }               
1061                 
1062                 CAL.get("cal-repeat-block").style.display = "";
1063                 
1064                 var repeat_type = "";
1065                 var set_default_repeat_until = true;    
1066                 if(typeof data.repeat_type != "undefined"){
1067                         repeat_type = data.repeat_type;
1068                         
1069                         document.forms['CalendarRepeatForm'].repeat_type.value = data.repeat_type;
1070                         document.forms['CalendarRepeatForm'].repeat_interval.value = data.repeat_interval;                      
1071                         if(data.repeat_count != '' && data.repeat_count != 0){
1072                                 document.forms['CalendarRepeatForm'].repeat_count.value = data.repeat_count;
1073                                 CAL.get("repeat_count_radio").checked = true;
1074                                 CAL.get("repeat_until_radio").checked = false;                          
1075                         }else{
1076                                 document.forms['CalendarRepeatForm'].repeat_until.value = data.repeat_until;
1077                                 CAL.get("repeat_until_radio").checked = true;
1078                                 CAL.get("repeat_count_radio").checked = false;
1079                                 set_default_repeat_until = false;                               
1080                         }
1081                         if(data.repeat_type == "Weekly"){
1082                                 var arr = data.repeat_dow.split("");
1083                                 CAL.each(arr,function(i,d){
1084                                         CAL.get("repeat_dow_" + d).checked = true;
1085                                 });
1086                         }                       
1087                         
1088                         CAL.get("cal-repeat-block").style.display = "";
1089                         CAL.get("edit_all_recurrences_block").style.display = "none";
1090                         toggle_repeat_type();
1091                 }
1092                 
1093                 CAL.get("edit_all_recurrences").value = "true";
1094                 
1095                 if(typeof data.current_dow != "undefined" && repeat_type != "Weekly")
1096                         CAL.get("repeat_dow_" + data.current_dow).checked = true;
1097                         
1098                 if(typeof data.default_repeat_until != "undefined" && set_default_repeat_until)
1099                         CAL.get("repeat_until_input").value = data.default_repeat_until;
1100                                         
1101                 
1102         }
1103         
1104         CAL.repeat_tab_handle = function (module_name){ 
1105                 if(!CAL.enable_repeat)
1106                         return;         
1107                 CAL.reset_repeat_form();
1108                 if(module_name == "Meetings" || module_name == "Calls"){
1109                         CAL.get("tab_repeat").style.display = "";
1110                 }else{
1111                         CAL.get("tab_repeat").style.display = "none";
1112                 }               
1113                 clear_all_errors();                     
1114                 toggle_repeat_type();           
1115         }
1116         
1117
1118         CAL.GR_update_user = function (user_id){
1119                 var callback = {
1120                         success: function(o){                           
1121                                 res = eval(o.responseText);
1122                                 GLOBAL_REGISTRY.focus.users_arr_hash = undefined;                                                                                       
1123                                 //SugarWidgetScheduler.update_time();
1124                         }
1125                 };      
1126                 var data = {
1127                         "users": user_id
1128                 };
1129                 var url = "index.php?module=Calendar&action=GetGRUsers&sugar_body_only=true";   
1130                 YAHOO.util.Connect.asyncRequest('POST',url,callback,CAL.toURI(data));
1131
1132         }
1133                 
1134         CAL.GR_update_focus = function (module,record){
1135                 if(record == ""){
1136                         GLOBAL_REGISTRY["focus"] = {"module":module, users_arr:[],fields:{"id":"-1"}};
1137                         SugarWidgetScheduler.update_time();                     
1138                 }else{          
1139                         var callback = {
1140                                 success: function(o){
1141                                         res = eval(o.responseText);
1142                                         SugarWidgetScheduler.update_time();                                                                                     
1143                                         if(CAL.record_editable){
1144                                                 CAL.enable_buttons();
1145                                         }
1146                                 }
1147                         };      
1148
1149                         var url = 'index.php?module=Calendar&action=GetGR&sugar_body_only=true&type=' + module + '&record=' + record;   
1150                         YAHOO.util.Connect.asyncRequest('POST',url,callback,false);
1151                 }               
1152         }
1153
1154         CAL.toggle_settings = function (){
1155                 var sd = CAL.get("settings_dialog");            
1156                         
1157                         
1158                 if(!CAL.settingsDialog){        
1159                         CAL.settingsDialog = new YAHOO.widget.Dialog("settings_dialog",{ 
1160                                         fixedcenter: true,
1161                                         draggable: false,
1162                                         visible : false, 
1163                                         modal : true,
1164                                         close: true
1165                         });
1166                         var listeners = new YAHOO.util.KeyListener(document, { keys : 27 }, {fn: function() { CAL.settingsDialog.cancel();} } );
1167                         CAL.settingsDialog.cfg.queueProperty("keylisteners", listeners);
1168                 }
1169                 CAL.settingsDialog.cancelEvent.subscribe(function(e, a, o){
1170                         CAL.get("form_settings").reset();
1171                 });
1172                 sd.style.display = "block";      
1173                 CAL.settingsDialog.render();
1174                 CAL.settingsDialog.show();
1175         }
1176
1177                 
1178         CAL.fill_invitees = function (){        
1179                 
1180                 CAL.get("user_invitees").value = "";
1181                 CAL.get("contact_invitees").value = "";
1182                 CAL.get("lead_invitees").value = "";    
1183
1184                 CAL.each( GLOBAL_REGISTRY['focus'].users_arr,   function(i,v){
1185                                                                         var field_name = "";
1186                                                                         if(v.module == "User")
1187                                                                                 field_name = "user_invitees";
1188                                                                         if(v.module == "Contact")
1189                                                                                 field_name = "contact_invitees";
1190                                                                         if(v.module == "Lead")
1191                                                                                 field_name = "lead_invitees";
1192                                                                         var str = CAL.get(field_name).value;
1193                                                                         CAL.get(field_name).value = str + v.fields.id + ",";    
1194                                                                 }
1195                 );      
1196         }       
1197                                 
1198         
1199         CAL.repeat_type_selected = function (){
1200                 var rt;
1201                 if(rt = CAL.get("repeat_type")){
1202                         if(rt.value == 'Weekly'){
1203                                 var nodes = CAL.query(".weeks_checks_div");
1204                                 CAL.each(nodes,function (i,v){
1205                                         nodes[i].style.display = "block";                               
1206                                 });
1207                         }else{
1208                                 var nodes = CAL.query(".weeks_checks_div");
1209                                 CAL.each(nodes,function (i,v){
1210                                         nodes[i].style.display = "none";                                
1211                                 });
1212                         }
1213                 
1214                         if(rt.value == ''){
1215                                 CAL.get("repeat_interval").setAttribute("disabled","disabled");
1216                                 CAL.get("repeat_end_date").setAttribute("disabled","disabled");
1217                         }else{
1218                                 CAL.get("repeat_interval").removeAttribute("disabled");
1219                                 CAL.get("repeat_end_date").removeAttribute("disabled"); 
1220                         }
1221                 }
1222         }
1223                 
1224         CAL.load_form = function (module_name, record, edit_all_recurrences){
1225         
1226                 CAL.disable_creating = true;
1227         
1228                 var e;
1229                 var to_open = true;
1230                 if(module_name == "Tasks")
1231                         to_open = false;
1232
1233                 if(to_open && CAL.records_openable){
1234                         CAL.get("form_content").style.display = "none";
1235                 
1236                         CAL.disable_buttons();  
1237         
1238                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_loading;
1239                         
1240                         CAL.repeat_tab_handle(module_name);
1241                         
1242                         ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_LOADING'));
1243                         
1244                         params = {};
1245                         if(edit_all_recurrences)
1246                                 params = {stay_on_tab: true};
1247                         
1248                         CAL.open_edit_dialog(params);   
1249                         CAL.get("record").value = "";   
1250                                 
1251                         if(!edit_all_recurrences)
1252                                 edit_all_recurrences = "";              
1253         
1254                         var callback = {
1255                                                                                                                                         
1256                                         success: function(o){
1257                                                 try{
1258                                                         res = eval("("+o.responseText+")");
1259                                                 }catch(err){
1260                                                         alert(CAL.lbl_error_loading);
1261                                                         CAL.editDialog.cancel();                                                        
1262                                                         ajaxStatus.hideStatus();
1263                                                         return; 
1264                                                 }                       
1265                                                 if(res.access == 'yes'){                                                        
1266                                                                                                         
1267                                                         var fc = document.getElementById("form_content");
1268                                                         CAL.script_evaled = false;
1269                                                         fc.innerHTML = '<script type="text/javascript">CAL.script_evaled = true;</script>'+res.html; 
1270                                                         if(!CAL.script_evaled){                                                                                 
1271                                                                 SUGAR.util.evalScript(res.html);
1272                                                         }
1273                                                         
1274                                                         CAL.get("record").value = res.record;
1275                                                         CAL.get("current_module").value = res.module_name;      
1276                                                         
1277                                                         var mod_name = res.module_name; 
1278                                                         
1279                                                         if(mod_name == "Meetings")
1280                                                                 CAL.get("radio_meeting").checked = true;
1281                                                         if(mod_name == "Calls")
1282                                                                 CAL.get("radio_call").checked = true;
1283                                                                 
1284                                                         if(res.edit == 1){
1285                                                                 CAL.record_editable = true;
1286                                                         }else{
1287                                                                 CAL.record_editable = false;
1288                                                         }
1289                                                         
1290                                                         CAL.get("radio_call").setAttribute("disabled","disabled");
1291                                                         CAL.get("radio_meeting").setAttribute("disabled","disabled");                                           
1292                                                                                                                                                                 
1293                 
1294                                                         eval(res.gr);                                                   
1295                                                         SugarWidgetScheduler.update_time();                                                                                     
1296                                                         if(CAL.record_editable){
1297                                                                 CAL.enable_buttons();
1298                                                         }                                                                                                               
1299                                                         
1300                                                         CAL.get("form_content").style.display = "";
1301                                                         
1302                                                         if(typeof res.repeat != "undefined"){
1303                                                                 CAL.fill_repeat_tab(res.repeat);
1304                                                         }
1305                                                         
1306                                                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_edit;
1307                                                         ajaxStatus.hideStatus();
1308                                                         CAL.get("btn-save").focus();
1309                                                                 
1310                                                         setTimeout(function(){
1311                                                                 if (!res.edit) {                                                                        
1312                                                                         $("#scheduler .schedulerInvitees").css("display", "none");
1313                                                                         $("#create-invitees-buttons").css("display", "none");
1314                                                                         $("#create-invitees-title").css("display", "none");
1315                                                                 }
1316                                                                 enableQS(false);
1317                                                                 disableOnUnloadEditView();
1318                                                         },500); 
1319                                                 }else
1320                                                         alert(CAL.lbl_error_loading);
1321                                                                                                                                                         
1322                                         },
1323                                         failure: function(){
1324                                                 alert(CAL.lbl_error_loading);
1325                                         }
1326                         };
1327                         var url = "index.php?module=Calendar&action=QuickEdit&sugar_body_only=true";
1328                         var data = {
1329                                 "current_module" : module_name,
1330                                 "record" : record,
1331                                 "edit_all_recurrences" : edit_all_recurrences
1332                         };
1333                         YAHOO.util.Connect.asyncRequest('POST',url,callback,CAL.toURI(data));
1334                         
1335                 }
1336                                                                 
1337         }
1338         
1339         
1340         CAL.edit_all_recurrences = function (){         
1341                 var record = CAL.get("record").value;
1342                 if(CAL.get("repeat_parent_id").value != ""){
1343                         record = CAL.get("repeat_parent_id").value;
1344                         CAL.get("repeat_parent_id").value = "";
1345                 }                               
1346                 var module = CAL.get("current_module").value;           
1347                 if(record != ""){
1348                         CAL.load_form(module, record, true);                    
1349                 }               
1350         }
1351                 
1352         
1353         CAL.remove_shared = function (record_id, edit_all_recurrences){
1354                         if(typeof edit_all_recurrences == "undefined")
1355                                 edit_all_recurrences = false;
1356         
1357                         var e;                  
1358                         var arr = new Array();
1359                         if(CAL.enable_repeat && edit_all_recurrences){
1360                                 var nodes = CAL.query("div.act_item[repeat_parent_id='" + record_id + "']");                                                    
1361                                 CAL.each(nodes,function (i,v){
1362                                         var record = nodes[i].getAttribute("record");
1363                                         if(!CAL.contains(arr,record))
1364                                                 arr.push(record);
1365                                         nodes[i].parentNode.removeChild(nodes[i]);
1366                                         CAL.destroy_ui(nodes[i].id);                    
1367                                 });
1368                         }
1369                                                                 
1370                         CAL.each(CAL.shared_users,function(user_id,v){                          
1371                                 if(e = CAL.get(record_id + '____' + v)){
1372                                         CAL.destroy_ui(e.id);
1373                                         e.parentNode.removeChild(e);
1374                                 }                               
1375                                 CAL.basic.remove({
1376                                         record: record_id,
1377                                         user_id: user_id 
1378                                 });                             
1379                                 CAL.each(arr,function(i,id){                                    
1380                                         CAL.basic.remove({
1381                                                 record: id,
1382                                                 user_id: user_id 
1383                                         });
1384                                 });
1385                         });
1386         }
1387                         
1388         CAL.add_item = function (item){ 
1389         
1390                         var edit_all_recurrences = false;
1391                         if(typeof item.edit_all_recurrences != "undefined" && item.edit_all_recurrences == 'true')
1392                                 edit_all_recurrences = true;
1393                         
1394                         if(CAL.view != 'shared'){                       
1395                                 var arr = new Array();
1396                                 if(CAL.enable_repeat && edit_all_recurrences){
1397                                         var nodes = CAL.query("div.act_item[repeat_parent_id='" + item.record + "']");                  
1398                                         CAL.each(nodes,function (i,v){
1399                                                 var record = nodes[i].getAttribute("record");
1400                                                 if(!CAL.contains(arr,record))
1401                                                         arr.push(record);
1402                                                 nodes[i].parentNode.removeChild(nodes[i]);                      
1403                                         });
1404                                 }                                               
1405                                 CAL.each(arr,function(i,id){
1406                                         CAL.basic.remove({
1407                                                 record: id,
1408                                                 user_id: CAL.current_user_id 
1409                                         });
1410                                 });                     
1411                                 CAL.add_item_to_grid(item);
1412                                 var record_id = item.record;
1413                                 if(CAL.enable_repeat && typeof item.repeat != "undefined"){ 
1414                                         CAL.each(item.repeat,function(j,r){
1415                                                         var clone = CAL.clone(item);
1416                                                         clone.record = r.id;
1417                                                         clone.timestamp = r.timestamp;
1418                                                         clone.ts_start = r.ts_start;
1419                                                         clone.ts_end = r.ts_end;
1420                                                         clone.repeat_parent_id = record_id;
1421                                                         CAL.add_item_to_grid(clone);                                            
1422                                         });
1423                                 }
1424                         }else{
1425                                 CAL.remove_shared(item.record, edit_all_recurrences);
1426                                 record_id = item.record;                                
1427                                 CAL.each(item.users, function (i,user_id){
1428                                         item.user_id = user_id;
1429                                         CAL.add_item_to_grid(item);                                     
1430                                         if(CAL.enable_repeat && typeof item.repeat != "undefined"){ 
1431                                                 CAL.each(item.repeat,function(j,r){
1432                                                                 var clone = CAL.clone(item);                                                            
1433                                                                 clone.record = r.id;
1434                                                                 clone.timestamp = r.timestamp;
1435                                                                 clone.ts_start = r.ts_start;
1436                                                                 clone.ts_end = r.ts_end;
1437                                                                 clone.repeat_parent_id = record_id;                                                             
1438                                                                 CAL.add_item_to_grid(clone);                                                    
1439                                                 });
1440                                         }                                                                                                                                                                                       
1441                                 });
1442                         }                       
1443                         
1444                         CAL.arrange_advanced();
1445                         CAL.basic.populate_grid();
1446                         CAL.fit_grid();                         
1447         }
1448                 
1449         CAL.move_activity = function (box_id,slot_id,ex_slot_id){
1450                                 var u,s;                                                
1451                                 if(u = CAL.get(box_id)){
1452                                         if(s = CAL.get(slot_id)){
1453                                                 s.appendChild(u);
1454                                                 
1455                                                 CAL.destroy_ui(box_id);                 
1456                                                 
1457                                                 CAL.arrange_column(document.getElementById(slot_id).parentNode);
1458                                                 CAL.arrange_column(document.getElementById(ex_slot_id).parentNode);
1459                                                 CAL.update_dd.fire();
1460                                                 
1461                                                 CAL.cut_record(box_id);                                 
1462                                                 var start_text = CAL.get_header_text(CAL.act_types[u.getAttribute('module_name')],s.getAttribute('time'),' ... ',u.getAttribute('record'));
1463                                                 var date_field = "date_start";
1464                                                 if(u.getAttribute('module_name') == "Tasks")
1465                                                         date_field = "date_due";
1466                                                 u.setAttribute(date_field,s.getAttribute("datetime"));                          
1467                                                 u.childNodes[0].childNodes[1].innerHTML = start_text;
1468                                         }
1469                                 }
1470         }
1471         
1472         
1473         CAL.change_activity_type = function (mod_name){
1474                 if(typeof CAL.current_params.module_name != "undefined" )
1475                         if(CAL.current_params.module_name == mod_name)
1476                                 return;                         
1477                                                                         
1478                 var e, user_name, user_id, date_start;          
1479                 CAL.get("title-cal-edit").innerHTML = CAL.lbl_loading;                          
1480                 document.forms["CalendarEditView"].elements["current_module"].value = mod_name;         
1481                 CAL.current_params.module_name = mod_name;              
1482                 QSFieldsArray = new Array();
1483                 QSProcessedFieldsArray = new Array();
1484                                 
1485                 CAL.load_create_form(CAL.current_params);                               
1486         }
1487
1488
1489         
1490         CAL.load_create_form = function (params){       
1491                 
1492                         CAL.disable_buttons();  
1493         
1494                         ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_LOADING'));
1495                         
1496                         CAL.repeat_tab_handle(CAL.current_params.module_name);
1497                         
1498                         var callback = {
1499                                                                                                                                         
1500                                         success: function(o){
1501                                                 try{
1502                                                         res = eval("("+o.responseText+")");
1503                                                 }catch(err){
1504                                                         alert(CAL.lbl_error_loading);
1505                                                         CAL.editDialog.cancel();                                                        
1506                                                         ajaxStatus.hideStatus();
1507                                                         return; 
1508                                                 }                       
1509                                                 if(res.access == 'yes'){                                                
1510                                                         var fc = document.getElementById("form_content");
1511                                                         CAL.script_evaled = false;
1512                                                         fc.innerHTML = '<script type="text/javascript">CAL.script_evaled = true;</script>'+res.html; 
1513                                                         
1514                                                         if(!CAL.script_evaled){                                                                                 
1515                                                                 SUGAR.util.evalScript(res.html);
1516                                                         }
1517                                                                                                                 
1518                                                         CAL.get("record").value = "";
1519                                                         CAL.get("current_module").value = res.module_name;                                                              
1520                                                         var mod_name = res.module_name; 
1521                                                                                                         
1522                                                         if(res.edit == 1){
1523                                                                 CAL.record_editable = true;
1524                                                         }else{
1525                                                                 CAL.record_editable = false;
1526                                                         }
1527                                                                                                                 
1528                                                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_create_new;
1529                                                         
1530                                                         if(typeof res.repeat != "undefined"){
1531                                                                 CAL.fill_repeat_tab(res.repeat);
1532                                                         }
1533                                                         
1534                                                         CAL.enable_buttons();
1535                                                                 
1536                                                         setTimeout(function(){
1537                                                                 SugarWidgetScheduler.update_time();
1538                                                                 enableQS(false);
1539                                                                 disableOnUnloadEditView();
1540                                                         },500);
1541                                                         
1542                                                         ajaxStatus.hideStatus();
1543                                                 }else{
1544                                                         alert(CAL.lbl_error_loading);
1545                                                         ajaxStatus.hideStatus();
1546                                                 }
1547                                         },
1548                                         failure: function() {
1549                                                 alert(CAL.lbl_error_loading);
1550                                                 ajaxStatus.hideStatus();
1551                                         }       
1552                         };                      
1553                                                         
1554                         var url = "index.php?module=Calendar&action=QuickEdit&sugar_body_only=true";
1555                         var data = {
1556                                 "current_module" : params.module_name,
1557                                 "assigned_user_id" : params.user_id,
1558                                 "assigned_user_name" : params.user_name,
1559                                 "date_start" : params.date_start
1560                         };
1561                         YAHOO.util.Connect.asyncRequest('POST',url,callback,CAL.toURI(data));   
1562         }
1563         
1564         CAL.full_form = function() {
1565                 var e = document.createElement('input');
1566                 e.setAttribute('type', 'hidden');
1567                 e.setAttribute('name', 'module');
1568                 e.value = CAL.get('current_module').value;              
1569                 CAL.get('form_content').parentNode.appendChild(e);
1570                 
1571                 var e = document.createElement('input');
1572                 e.setAttribute('type', 'hidden');
1573                 e.setAttribute('name', 'action');
1574                 e.value = 'EditView';
1575                 CAL.get('form_content').parentNode.appendChild(e);
1576                 
1577                 document.forms['CalendarEditView'].action = "index.php";
1578                 document.forms['CalendarEditView'].full_form = "true";          
1579                 document.forms['CalendarEditView'].submit();
1580                 
1581         }       
1582         
1583         CAL.disable_buttons = function() {
1584                 CAL.get("btn-save").setAttribute("disabled","disabled");
1585                 CAL.get("btn-send-invites").setAttribute("disabled","disabled");
1586                 CAL.get("btn-delete").setAttribute("disabled","disabled");
1587                 CAL.get("btn-full-form").setAttribute("disabled","disabled");
1588                 if (CAL.enable_repeat) {
1589                         CAL.get("btn-edit-all-recurrences").setAttribute("disabled","disabled");
1590                         CAL.get("btn-remove-all-recurrences").setAttribute("disabled","disabled");
1591                 }
1592         }
1593         
1594         CAL.enable_buttons = function() {
1595                 CAL.get("btn-save").removeAttribute("disabled");
1596                 CAL.get("btn-send-invites").removeAttribute("disabled");                
1597                 if (CAL.get("record").value != "")
1598                         CAL.get("btn-delete").removeAttribute("disabled");      
1599                 CAL.get("btn-full-form").removeAttribute("disabled");
1600                 if (CAL.enable_repeat) {
1601                         CAL.get("btn-edit-all-recurrences").removeAttribute("disabled");
1602                         CAL.get("btn-remove-all-recurrences").removeAttribute("disabled");
1603                 }
1604         }
1605                 
1606         
1607         CAL.dialog_create = function (cell){
1608         
1609                         var e,user_id,user_name;
1610                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_loading;                                                                                                          
1611                         CAL.open_edit_dialog();
1612                         
1613                         CAL.disable_buttons();
1614                         
1615                         var module_name = CAL.get("current_module").value;
1616                         
1617                         if(CAL.view == 'shared'){
1618                                 user_name = cell.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute("user_name");
1619                                 user_id = cell.parentNode.parentNode.parentNode.parentNode.parentNode.getAttribute("user_id");
1620                                 CAL.GR_update_user(user_id);
1621                         }else{
1622                                 user_id = CAL.current_user_id;
1623                                 user_name = CAL.current_user_name;
1624                                 CAL.GR_update_user(CAL.current_user_id);                                                                                                
1625                         }                                               
1626                         
1627                         var params = {  
1628                                 'module_name': module_name,
1629                                 'user_id': user_id,     
1630                                 'user_name': user_name,                 
1631                                 'date_start': cell.getAttribute("datetime")                             
1632                         };
1633                         CAL.current_params = params;
1634                         CAL.load_create_form(CAL.current_params);
1635                                                         
1636         }
1637         
1638         CAL.dialog_save = function(){
1639                                                 
1640                                                 CAL.disable_buttons();
1641                                                 
1642                                                 ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));
1643                                                 
1644                                                 if (CAL.get("send_invites").value == "1") {
1645                                                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_sending;                                                          
1646                                                 } else {
1647                                                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_saving;
1648                                                 }
1649                                                                                                                                                                                                                         
1650                                                 CAL.fill_invitees();                                                    
1651                                                 CAL.fill_repeat_data();
1652                                                                                                 
1653                                                 var callback = {
1654                                                                 success: function(o){
1655                                                                         try{
1656                                                                                 res = eval("("+o.responseText+")");
1657                                                                         }catch(err){
1658                                                                                 alert(CAL.lbl_error_saving);
1659                                                                                 CAL.editDialog.cancel();                                                        
1660                                                                                 ajaxStatus.hideStatus();
1661                                                                                 return; 
1662                                                                         }
1663                                                                         if(res.access == 'yes'){
1664                                                                                 
1665                                                                                 if(typeof res.limit_error != "undefined"){
1666                                                                                         var alert_msg = CAL.lbl_repeat_limit_error;
1667                                                                                         alert(alert_msg.replace("\$limit",res.limit));
1668                                                                                         CAL.get("title-cal-edit").innerHTML = CAL.lbl_edit;                                                                                     
1669                                                                                         ajaxStatus.hideStatus();                                                                                        
1670                                                                                         CAL.enable_buttons();
1671                                                                                         
1672                                                                                         return;
1673                                                                                 }
1674                                                                                 CAL.add_item(res);
1675                                                                                 CAL.editDialog.cancel();
1676                                                                                 CAL.update_vcal();
1677                                                                                 ajaxStatus.hideStatus();                                                                                                
1678                                                                         }else{
1679                                                                                 alert(CAL.lbl_error_saving);
1680                                                                                 ajaxStatus.hideStatus();
1681                                                                         }                                                                                                               
1682                                                                 },
1683                                                                 failure: function(){
1684                                                                                 alert(CAL.lbl_error_saving);
1685                                                                                 ajaxStatus.hideStatus();
1686                                                                 }
1687                                                 };                                              
1688                                                 var url = "index.php?module=Calendar&action=SaveActivity&sugar_body_only=true"; 
1689                                                 YAHOO.util.Connect.setForm(CAL.get("CalendarEditView"));
1690                                                 YAHOO.util.Connect.asyncRequest('POST',url,callback,false);     
1691         }       
1692         
1693         CAL.remove_all_recurrences = function(){                                
1694         
1695                 if(confirm(CAL.lbl_confirm_remove_all_recurring)){
1696                         if(CAL.get("repeat_parent_id").value != ''){
1697                                 CAL.get("record").value = CAL.get("repeat_parent_id").value;
1698                         }
1699                         CAL.get("edit_all_recurrences").value = true;
1700                         CAL.dialog_remove();            
1701                 }
1702         
1703         }
1704         
1705         CAL.dialog_remove = function(){
1706                                                                         CAL.deleted_id = CAL.get("record").value;
1707                                                                         CAL.deleted_module = CAL.get("current_module").value;
1708                                                                         
1709                                                                         var remove_all_recurrences = CAL.get("edit_all_recurrences").value;
1710                                                                         
1711                                                                         var isRecurrence = false;
1712                                                                         if (CAL.enable_repeat) {
1713                                                                                 if (CAL.get("repeat_parent_id").value != '') {
1714                                                                                         var isRecurrence = true;
1715                                                                                 } else {
1716                                                                                         if (document.CalendarRepeatForm.repeat_type.value != '') {
1717                                                                                                 var isRecurrence = true;
1718                                                                                         }
1719                                                                                 }
1720                                                                         }
1721                                                                                         
1722                                                                         var callback = {
1723                                                                                         success: function(o){
1724                                                                                                 try{
1725                                                                                                         res = eval("("+o.responseText+")");
1726                                                                                                 }catch(err){
1727                                                                                                         alert(CAL.lbl_error_saving);
1728                                                                                                         CAL.editDialog.cancel();                                                        
1729                                                                                                         ajaxStatus.hideStatus();
1730                                                                                                         return; 
1731                                                                                                 }
1732                                                                                                         
1733                                                                                                 var e,cell_id;
1734                                                                                                 if(e = CAL.get(CAL.deleted_id))
1735                                                                                                         cell_id = e.parentNode.id;
1736                                                                                                         
1737                                                                                                 if(CAL.view == 'shared'){       
1738                                                                                                         if (remove_all_recurrences && isRecurrence) {
1739                                                                                                                 CAL.refresh();
1740                                                                                                         } else {
1741                                                                                                                 CAL.remove_shared(CAL.deleted_id, remove_all_recurrences);
1742                                                                                                         }
1743                                                                                                                                                                                                                 
1744                                                                                                 }else{                                                                                                          
1745                                                                                                         if(e = CAL.get(CAL.deleted_id)){
1746                                                                                                                 e.parentNode.removeChild(e);
1747                                                                                                                 CAL.destroy_ui(CAL.deleted_id);
1748                                                                                                         }
1749                                                                                                         
1750                                                                                                         CAL.basic.remove({
1751                                                                                                                 record: CAL.deleted_id,
1752                                                                                                                 user_id: CAL.current_user_id
1753                                                                                                         });     
1754                                                                                                 
1755                                                                                                         if(CAL.enable_repeat && remove_all_recurrences && isRecurrence){                                                                                                                
1756                                                                                                                 CAL.refresh();
1757                                                                                                         }
1758                                                                                                 }
1759                                                                                                 
1760                                                                                                 CAL.records_openable = true;
1761                                                                                                 CAL.disable_creating = false;
1762
1763                                                                                                 CAL.arrange_advanced();
1764                                                                                                 CAL.basic.populate_grid();
1765                                                                                                 CAL.fit_grid();                                                 
1766                                                                                                                                                                                                                 
1767                                                                                         },
1768                                                                                         failure: function(){
1769                                                                                                         alert(CAL.lbl_error_saving);
1770                                                                                         }
1771                                                                         };                                                                      
1772                                                                         
1773                                                                         var data = {
1774                                                                                 "current_module" : CAL.deleted_module,
1775                                                                                 "record" : CAL.deleted_id,
1776                                                                                 "remove_all_recurrences": remove_all_recurrences
1777                                                                         };
1778                                                                         var url = "index.php?module=Calendar&action=Remove&sugar_body_only=true";                                                                       
1779                                                                         YAHOO.util.Connect.asyncRequest('POST',url,callback,CAL.toURI(data));
1780
1781                                                                         CAL.editDialog.cancel();        
1782         }
1783         
1784         CAL.refresh = function () {     
1785         
1786                 var callback = {
1787                         success: function(o) {
1788                                 
1789                                 try {
1790                                         var activities = eval("("+o.responseText+")");
1791                                 } catch(err) {
1792                                         alert(CAL.lbl_error_saving);                                    
1793                                         ajaxStatus.hideStatus();
1794                                         return; 
1795                                 }
1796                                 
1797                                 CAL.each(activities, function(i, v) {
1798                                         CAL.add_item_to_grid(activities[i]);
1799                                 });                             
1800
1801                                 CAL.arrange_advanced();
1802                                 CAL.basic.populate_grid();              
1803                                 CAL.fit_grid();
1804                                 CAL.update_dd.fire();
1805                         }
1806                 }
1807         
1808                 var data = {
1809                         "view": CAL.view,
1810                         "year": CAL.year,
1811                         "month": CAL.month,
1812                         "day": CAL.day
1813                 };
1814                 var url = "index.php?module=Calendar&action=getActivities&sugar_body_only=true";                                                                        
1815                 YAHOO.util.Connect.asyncRequest('POST', url, callback, CAL.toURI(data));
1816                 
1817                 CAL.clear();
1818         }
1819         
1820         CAL.clear = function () {
1821                 CAL.basic.items = {};
1822                 var nodes = CAL.query("#cal-grid div.act_item");                        
1823                 CAL.each(nodes, function(i,v) {
1824                         nodes[i].parentNode.removeChild(nodes[i]);
1825                 });     
1826         }
1827         
1828         CAL.show_additional_details = function (id){
1829                 
1830                 var obj = CAL.get(id);  
1831                 var record = obj.getAttribute("record");
1832                 var module_name = obj.getAttribute("module_name");              
1833                 SUGAR.util.getAdditionalDetails(module_name, record, 'div_'+id, true);
1834                 return;
1835         }
1836         
1837         CAL.clear_additional_details = function (id){
1838                 if(typeof SUGAR.util.additionalDetailsCache[id] != "undefined")
1839                         SUGAR.util.additionalDetailsCache[id] = undefined;              
1840                 if(typeof SUGAR.util.additionalDetailsCalls[id] != "undefined")
1841                         SUGAR.util.additionalDetailsCalls[id] = undefined;              
1842         }                       
1843         
1844         CAL.toggle_shared_edit = function (){
1845                 
1846                 var sd = CAL.get("shared_cal_edit");                    
1847                 if(!CAL.sharedDialog){  
1848                         CAL.sharedDialog = new YAHOO.widget.Dialog("shared_cal_edit",{ 
1849                                         fixedcenter: true,
1850                                         draggable: false,
1851                                         visible : false, 
1852                                         modal : true,
1853                                         close: true
1854                         });
1855                         var listeners = new YAHOO.util.KeyListener(document, { keys : 27 }, {fn: function() { CAL.sharedDialog.cancel();} } );
1856                         CAL.sharedDialog.cfg.queueProperty("keylisteners", listeners);
1857                 }
1858                 CAL.sharedDialog.cancelEvent.subscribe(function(e, a, o){
1859                         //CAL.get("form_settings").reset();
1860                 });
1861                 sd.style.display = "block";      
1862                 CAL.sharedDialog.render();
1863                 CAL.sharedDialog.show();
1864         }
1865
1866         CAL.goto_date_call = function (){
1867                 var date_string = CAL.get("goto_date").value;
1868                 var date_arr = [];
1869                 date_arr = date_string.split("/");
1870                 
1871                 window.location.href = "index.php?module=Calendar&view="+CAL.view+"&day="+date_arr[1]+"&month="+date_arr[0]+"&year="+date_arr[2];       
1872         }
1873         
1874         CAL.check_forms = function (){  
1875                 if(!(check_form('CalendarEditView') && cal_isValidDuration())){
1876                         CAL.select_tab("cal-tab-1");
1877                         return false;
1878                 }
1879                 if(CAL.enable_repeat && CAL.get("edit_all_recurrences").value != ""){
1880                         lastSubmitTime = lastSubmitTime - 2001; // allow to run check second time
1881                         if(!check_form('CalendarRepeatForm')){
1882                                 CAL.select_tab("cal-tab-3");
1883                                 return false;
1884                         }
1885                 }
1886                 return true;
1887         }
1888         
1889         CAL.toURI = function (a){
1890                         t=[];
1891                         for(x in a){                    
1892                                 if(!(a[x].constructor.toString().indexOf('Array') == -1)){
1893                                         for(i in a[x])
1894                                                 t.push(x+"[]="+encodeURIComponent(a[x][i]));
1895                                 }else                   
1896                                         t.push(x+"="+encodeURIComponent(a[x]));
1897                         }
1898                         return t.join("&");
1899         }       
1900         
1901         CAL.each = function (object, callback) {
1902                 
1903                 if(typeof object == "undefined")
1904                         return;
1905                 var name, i = 0,
1906                 length = object.length,
1907                 isObj = (length === undefined) || (typeof (object) === "function");
1908                 if(isObj){
1909                             for (name in object) {
1910                                 if (callback.call(object[name], name, object[name]) === false) {
1911                                     break;
1912                                 }
1913                             }
1914                 }else{
1915                             for (; i < length;) {
1916                                 if (callback.call(object[i], i, object[i++]) === false) {
1917                                     break;
1918                                 }
1919                             }
1920                 }
1921                 return object;
1922         }
1923         
1924         CAL.clone = function (o) {
1925                 var c = new Object();
1926                 for(var e in o)
1927                         c[e] = o[e];            
1928                 return c;
1929         }
1930         
1931         CAL.contains = function(a, obj){
1932                         var i = a.length;
1933                         while(i--)
1934                                 if (a[i] === obj)
1935                                         return true;
1936                         return false;
1937         }
1938         
1939         CAL.update_vcal = function(){
1940                                                                                                 
1941                                         var v = CAL.current_user_id;                                                                                            
1942                                         var callback = {
1943                                                 success: function(result){ 
1944                                                         if (typeof GLOBAL_REGISTRY.freebusy == 'undefined') {
1945                                                                 GLOBAL_REGISTRY.freebusy = new Object();
1946                                                         }
1947                                                         if (typeof GLOBAL_REGISTRY.freebusy_adjusted == 'undefined') {
1948                                                                 GLOBAL_REGISTRY.freebusy_adjusted = new Object();
1949                                                         }
1950                                                         // parse vCal and put it in the registry using the user_id as a key:
1951                                                         GLOBAL_REGISTRY.freebusy[v] = SugarVCalClient.parseResults(result.responseText, false);
1952                                                         // parse for current user adjusted vCal
1953                                                         GLOBAL_REGISTRY.freebusy_adjusted[v] = SugarVCalClient.parseResults(result.responseText, true);
1954                                                         SugarWidgetScheduler.update_time();
1955                                                 }                                                                                               
1956                                         };
1957                                                         
1958                                         var url = "vcal_server.php?type=vfb&source=outlook&user_id="+v;                                                                 
1959                                         YAHOO.util.Connect.asyncRequest('GET',url,callback,false);
1960         }
1961         
1962         CAL.fit_grid = function(control_call){
1963                 
1964                 if (CAL.view == 'year') {
1965                         return;
1966                 }
1967                 
1968                 var day_width;
1969                 var cal_width = document.getElementById("cal-width-helper").offsetWidth;
1970                 
1971                 if (CAL.print) {
1972                         cal_width = 800;
1973                 }
1974                         
1975                 var left_width = 80;
1976                 if(CAL.style == "basic"){
1977                         if(CAL.view != "month"){
1978                                 left_width = 20;
1979                         }else
1980                                 left_width = 60;                
1981                 }                                               
1982                 
1983                 if(CAL.view == "day"){
1984                         day_width = parseInt((cal_width - left_width - 10));    
1985                         if(typeof control_call == "undefined" || !control_call){
1986                                 setTimeout(function(){
1987                                         CAL.fit_grid(true);
1988                                         setTimeout(function(){
1989                                                 CAL.fit_grid(true);                                     
1990                                         },100);                                 
1991                                 },100);
1992                         }
1993                 }else{                                                  
1994                         day_width = parseInt((cal_width - left_width) / 7);
1995                 }                       
1996                         
1997                 var nodes = CAL.query("#cal-grid div.col");
1998                 CAL.each(nodes, function(i,v){          
1999                         nodes[i].style.width = day_width + "px";
2000                 });
2001                 
2002                 var nodes = CAL.query("#cal-grid .cal-basic .act_item");        
2003                 CAL.each(nodes, function(i,v){  
2004                         var days = nodes[i].getAttribute('days');       
2005                         nodes[i].style.width = (day_width * days - 1) + "px";
2006                 });
2007                 document.getElementById("cal-grid").style.visibility = "";
2008
2009         }
2010         
2011
2012         YAHOO.util.DDCAL = function(id, sGroup, config){ 
2013                 this.cont = config.cont; 
2014                 YAHOO.util.DDCAL.superclass.constructor.apply(this, arguments);
2015         }
2016         YAHOO.extend(YAHOO.util.DDCAL, YAHOO.util.DD, {
2017                 cont: null,                             
2018                 init: function(){
2019                         YAHOO.util.DDCAL.superclass.init.apply(this, arguments);
2020                         this.initConstraints();                 
2021                         CAL.update_dd.subscribe(function(type, args, dd){
2022                                 dd.resetConstraints();                                  
2023                                 dd.initConstraints();
2024                         },this);
2025                 },
2026                 initConstraints: function() {           
2027                         var region = YAHOO.util.Dom.getRegion(this.cont);                       
2028                         var el = this.getEl();
2029
2030                         var xy = YAHOO.util.Dom.getXY(el);                      
2031                         var width = parseInt(YAHOO.util.Dom.getStyle(el, 'width'), 10);
2032                         var height = parseInt(YAHOO.util.Dom.getStyle(el, 'height'), 10);
2033                         var left = xy[0] - region.left;
2034                         var right = region.right - xy[0] - width;
2035                         var top = xy[1] - region.top;
2036                         var bottom = region.bottom - xy[1] - height;
2037                         
2038                         if(xy){
2039                                 this.setXConstraint(left, right);
2040                                 this.setYConstraint(top, bottom);
2041                         }
2042                 }
2043         });
2044         
2045         CAL.remove_edit_dialog();
2046         
2047         var cal_loaded = true;