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