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