]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/modules/Meetings/jsclass_scheduler.js
Release 6.5.0beta5
[Github/sugarcrm.git] / jssource / src_files / modules / Meetings / jsclass_scheduler.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
38 //////////////////////////////////////////////////
39 // class: SugarWidgetListView
40 // widget to display a list view
41 //
42 //////////////////////////////////////////////////
43
44 SugarClass.inherit("SugarWidgetListView","SugarClass");
45
46 function SugarWidgetListView() {
47         this.init();
48 }
49
50 SugarWidgetListView.prototype.init = function() {
51
52 }
53
54 SugarWidgetListView.prototype.load = function(parentNode) {
55         this.parentNode = parentNode;
56         this.display();
57 }
58
59 SugarWidgetListView.prototype.display = function() {
60
61         if(typeof GLOBAL_REGISTRY['result_list'] == 'undefined') {
62                 this.display_loading();
63                 return;
64         }
65
66         var div = document.getElementById('list_div_win');
67         div.style.display = 'block';
68         //div.style.height='125px';
69         var html = '<table width="100%" cellpadding="0" cellspacing="0" border="0" class="list view">';
70         html += '<tr>';
71         html += '<th width="2%" nowrap="nowrap">&nbsp;</th>';
72         html += '<th width="20%" nowrap="nowrap">'+GLOBAL_REGISTRY['meeting_strings']['LBL_NAME']+'</th>';
73         html += '<th width="20%" nowrap="nowrap">'+GLOBAL_REGISTRY['meeting_strings']['LBL_EMAIL']+'</th>';
74         html += '<th width="20%" nowrap="nowrap">'+GLOBAL_REGISTRY['meeting_strings']['LBL_PHONE']+'</th>';
75         html += '<th width="18%" nowrap="nowrap">&nbsp;</th>';
76         html += '</tr>';
77         //var html = '<table width="100%" cellpadding="0" cellspacing="0">';
78         for(var i=0;i<GLOBAL_REGISTRY['result_list'].length;i++) {
79                 var bean = GLOBAL_REGISTRY['result_list'][i];
80                 var disabled = false;
81                 var className='evenListRowS1';
82
83                 if(typeof(GLOBAL_REGISTRY.focus.users_arr_hash[ bean.fields.id]) != 'undefined') {
84                         disabled = true;
85                 }
86                 if((i%2) == 0) {
87                         className='oddListRowS1';
88                 } else {
89                         className='evenListRowS1';
90                 }
91                 if(typeof (bean.fields.first_name) == 'undefined') {
92                         bean.fields.first_name = '';
93                 }
94                 if(typeof (bean.fields.email1) == 'undefined' || bean.fields.email1 == "") {
95                         bean.fields.email1 = '&nbsp;';
96                 }
97                 if(typeof (bean.fields.phone_work) == 'undefined' || bean.fields.phone_work == "") {
98                         bean.fields.phone_work = '&nbsp;';
99                 }
100
101                 html += '<tr class="'+className+'">';
102                 html += '<td><img src="'+GLOBAL_REGISTRY.config['site_url']+'/index.php?entryPoint=getImage&themeName='+SUGAR.themes.theme_name+'&imageName='+bean.module+'s.gif"/></td>';
103                 html += '<td>'+bean.fields.full_name+'</td>';
104                 html += '<td>'+bean.fields.email1+'</td>';
105                 html += '<td>'+bean.fields.phone_work+'</td>';
106                 html += '<td align="right">';
107                 //      hidden = 'hidden';
108                 hidden = 'visible';
109                 if(!disabled) {
110                         //      hidden = 'visible';
111                 }
112                 html += '<input type="button" class="button" onclick="this.disabled=true;SugarWidgetSchedulerAttendees.form_add_attendee('+i+');" value="'+GLOBAL_REGISTRY['meeting_strings']['LBL_ADD_BUTTON']+'"/ style="visibility: '+hidden+'"/>';
113                 html += '</td>';
114
115                 html += '</tr>';
116         }
117         html += '</table>';
118         //this.parentNode.innerHTML = html;
119         
120         div.innerHTML = html;
121 }
122
123 SugarWidgetListView.prototype.display_loading = function() {
124
125 }
126
127 //////////////////////////////////////////////////
128 // class: SugarWidgetSchedulerSearch
129 // widget to display the meeting scheduler search box
130 //
131 //////////////////////////////////////////////////
132
133 SugarClass.inherit("SugarWidgetSchedulerSearch","SugarClass");
134
135 function SugarWidgetSchedulerSearch() {
136         this.init();
137 }
138
139 SugarWidgetSchedulerSearch.prototype.init = function() {
140         this.form_id = 'scheduler_search';
141         GLOBAL_REGISTRY['widget_element_map'] = new Object();
142         GLOBAL_REGISTRY['widget_element_map'][this.form_id] = this;
143 }
144
145 SugarWidgetSchedulerSearch.prototype.load = function(parentNode) {
146         this.parentNode = parentNode;
147         this.display();
148 }
149
150 SugarWidgetSchedulerSearch.submit = function(form) {
151
152         SugarWidgetSchedulerSearch.hideCreateForm();
153
154         //construct query obj:
155         var conditions  = new Array();
156
157         if(form.search_first_name.value != '') {
158                 conditions[conditions.length] = {"name":"first_name","op":"starts_with","value":form.search_first_name.value}
159         }
160         if(form.search_last_name.value != '') {
161                 conditions[conditions.length] = {"name":"last_name","op":"starts_with","value":form.search_last_name.value}
162         }
163         if(form.search_email.value != '') {
164                 conditions[conditions.length] = {"name":"email1","op":"starts_with","value":form.search_email.value}
165         }
166
167         var query = {"modules":["Users","Contacts"
168         ,"Leads"
169         ],"group":"and","field_list":['id','full_name','email1','phone_work'],"conditions":conditions};
170         global_request_registry[req_count] = [this,'display'];
171         req_id = global_rpcClient.call_method('query',query);
172         global_request_registry[req_id] = [ GLOBAL_REGISTRY['widget_element_map'][form.id],'refresh_list'];
173 }
174
175 SugarWidgetSchedulerSearch.prototype.refresh_list = function(rslt) {
176
177         GLOBAL_REGISTRY['result_list'] = rslt['list'];
178         
179         if (rslt['list'].length > 0) {
180                 this.list_view.display();
181                 document.getElementById('empty-search-message').style.display = 'none';                 
182         }else{
183                 document.getElementById('list_div_win').style.display = 'none';
184                 document.getElementById('empty-search-message').style.display = '';     
185         }
186
187 }
188
189 SugarWidgetSchedulerSearch.prototype.display = function() {
190         var html ='<div class="schedulerInvitees"><h3>'+GLOBAL_REGISTRY['meeting_strings']['LBL_ADD_INVITEE']+'</h5><table border="0" cellpadding="0" cellspacing="0" width="100%" class="edit view">';
191         html +='<tr><td>';                  
192         html += '<form name="schedulerwidget" id="'+this.form_id+'" onsubmit="SugarWidgetSchedulerSearch.submit(this);return false;">';
193
194         html += '<table width="100%" cellpadding="0" cellspacing="0" width="100%" >'
195         html += '<tr>';
196         //html += '<form id="'+this.form_id+'"><table width="100%"><tbody><tr>';
197         html += '<td scope="col" nowrap><label for="search_first_name">'+GLOBAL_REGISTRY['meeting_strings']['LBL_FIRST_NAME']+':</label>&nbsp;&nbsp;<input  name="search_first_name" id="search_first_name" value="" type="text" size="10"></td>';
198         html += '<td scope="col" nowrap><label for="search_last_name">'+GLOBAL_REGISTRY['meeting_strings']['LBL_LAST_NAME']+':</label>&nbsp;&nbsp;<input  name="search_last_name" id="search_last_name" value="" type="text" size="10"></td>';
199         html += '<td scope="col" nowrap><label for="search_email">'+GLOBAL_REGISTRY['meeting_strings']['LBL_EMAIL']+':</label>&nbsp;&nbsp;<input  name="search_email" id="search_email" type="text" value="" size="15"></td>';
200         //html += '<td valign="center"><input type="submit" onclick="SugarWidgetSchedulerSearch.submit(this.form);" value="Search" ></td></tr></tbody></table></form>';
201         html += '<td valign="center"><input type="submit" class="button" value="'+GLOBAL_REGISTRY['meeting_strings']['LBL_SEARCH_BUTTON']+'" ></td></tr>';
202         html += '</table>';
203         html += '</form>';
204         html += '</td></tr></table></div>';
205
206         // append the list_view as the third row of the outside table
207         this.parentNode.innerHTML += html;
208         
209         
210         var div = document.createElement('div');
211         div.setAttribute('id','list_div_win');
212         div.style.overflow = 'auto';
213         div.style.width = '100%';
214         div.style.height= '100%';
215         div.style.display = 'none';
216     this.parentNode.appendChild(div);
217         
218         
219         html = '';
220         html += '<div id="create-invitees" style="margin-bottom: 10px;">';
221         html += '<div id="empty-search-message" style="display: none;">' + GLOBAL_REGISTRY['meeting_strings']['LBL_EMPTY_SEARCH_RESULT'] + '</div>';
222         html += '<h3>' + GLOBAL_REGISTRY['meeting_strings']['LBL_CREATE_INVITEE'] + '</h3>';
223         html += '<div id="create-invitees-buttons">';
224         html += '<button type="button" onclick="SugarWidgetSchedulerSearch.showCreateForm(\'Contacts\');">' + GLOBAL_REGISTRY['meeting_strings']['LBL_CREATE_CONTACT'] + '</button> ';
225         html += '<button type="button" onclick="SugarWidgetSchedulerSearch.showCreateForm(\'Leads\');">' + GLOBAL_REGISTRY['meeting_strings']['LBL_CREATE_LEAD'] + '</button> ';
226         html += '</div>';
227         
228         html += '<div id="create-invitee-edit" style="display: none;">';
229         html += '<form name="createInviteeForm" id="createInviteeForm" onsubmit="SugarWidgetSchedulerSearch.createInvitee(this); return false;">';
230         html += '<input type="hidden" name="inviteeModule" value="Contacts">';
231         html += '<table class="edit view" cellpadding="0" cellspacing="0" style="width: 330px; margin-top: 2px;">'
232         html += '<tr>';
233         html += '<td valign="top" width="33%">' + GLOBAL_REGISTRY['meeting_strings']['LBL_FIRST_NAME'] + ': </td><td valign="top"><input name="first_name" type="text" size="19"></td>';
234         html += '</tr>';
235         html += '<tr>';
236         html += '<td valign="top" width="33%">' + GLOBAL_REGISTRY['meeting_strings']['LBL_LAST_NAME'] + ': <span class="required">*</span></td><td valign="top"><input name="last_name" type="text" size="19"></td>';
237         html += '</tr>';
238         html += '<tr>';
239         html += '<td valign="top" width="33%">' + GLOBAL_REGISTRY['meeting_strings']['LBL_EMAIL'] + ': </td><td valign="top"><input name="email1" type="text" size="19"></td>';
240         html += '</tr>';
241         html += '</table>';     
242         html += '<button type="button" id="create-invitee-btn" onclick="SugarWidgetSchedulerSearch.createInvitee(this.form);">' + GLOBAL_REGISTRY['meeting_strings']['LBL_CREATE_AND_ADD'] + '</button> ';
243         html += '<button type="button" id="cancel-create-invitee-btn" onclick="SugarWidgetSchedulerSearch.hideCreateForm();">' + GLOBAL_REGISTRY['meeting_strings']['LBL_CANCEL_CREATE_INVITEE'] + '</button> ';
244         html += '</form>';
245         html += '</div>';       
246         html += '</div>';
247         this.parentNode.innerHTML += html;
248         
249         addToValidate('createInviteeForm', 'last_name', 'last_name', true, GLOBAL_REGISTRY['meeting_strings']['LBL_LAST_NAME']);
250
251     this.list_view = new SugarWidgetListView();
252         this.list_view.load(div);
253 }
254
255 SugarWidgetSchedulerSearch.showCreateForm = function(module){
256         document.getElementById('create-invitee-edit').style.display = '';
257         document.getElementById('create-invitees-buttons').style.display = 'none';
258         document.getElementById('list_div_win').style.display = 'none';
259         document.forms['createInviteeForm'].elements['inviteeModule'].value = module;   
260         
261         document.getElementById('empty-search-message').style.display = 'none';
262         
263         if (typeof document.createInviteeForm.first_name != 'undefined' && typeof document.schedulerwidget.search_first_name != 'undefined')
264                 document.createInviteeForm.first_name.value = document.schedulerwidget.search_first_name.value;
265         if (typeof document.createInviteeForm.last_name != 'undefined' && typeof document.schedulerwidget.search_last_name != 'undefined')
266                 document.createInviteeForm.last_name.value = document.schedulerwidget.search_last_name.value;
267         if (typeof document.createInviteeForm.email1 != 'undefined' && typeof document.schedulerwidget.search_email != 'undefined')
268                 document.createInviteeForm.email1.value = document.schedulerwidget.search_email.value;
269         
270 }
271
272 SugarWidgetSchedulerSearch.hideCreateForm = function(module){
273         document.getElementById('create-invitee-edit').style.display = 'none';
274         document.getElementById('create-invitees-buttons').style.display = '';
275         
276         document.forms['createInviteeForm'].reset();
277 }
278
279 SugarWidgetSchedulerSearch.createInvitee = function(form){
280         if(!(check_form('createInviteeForm'))){
281                 return false;
282         }
283         
284         document.getElementById('create-invitee-btn').setAttribute('disabled', 'disabled');
285         document.getElementById('cancel-create-invitee-btn').setAttribute('disabled', 'disabled');
286         
287         ajaxStatus.showStatus(SUGAR.language.get('app_strings', 'LBL_SAVING'));
288                 
289         var callback = {
290                 success: function (response) {
291                         
292                         var rObj = eval("("+response.responseText+")");
293                         
294                         ajaxStatus.hideStatus();
295                         
296                         if (typeof rObj.noAccess != 'undefined') {
297                                 var alertMsg = GLOBAL_REGISTRY['meeting_strings']['LBL_NO_ACCESS'];
298                                 alertMsg = alertMsg.replace("\$module", rObj.module);
299                                 SugarWidgetSchedulerSearch.hideCreateForm();                            
300                                 alert(alertMsg);                        
301                                 return false;
302                         }
303                         
304                         GLOBAL_REGISTRY.focus.users_arr[GLOBAL_REGISTRY.focus.users_arr.length] = rObj;
305                         GLOBAL_REGISTRY.scheduler_attendees_obj.display();
306                         SugarWidgetSchedulerSearch.hideCreateForm();
307                         
308                         document.getElementById('create-invitee-btn').removeAttribute('disabled');
309                         document.getElementById('cancel-create-invitee-btn').removeAttribute('disabled');                       
310                 }
311         };
312         
313         var fieldList = ['id', 'full_name', 'email1', 'phone_work'];
314
315         var t = [];
316         for (i in fieldList) {
317                 t.push("fieldList[]=" + encodeURIComponent(fieldList[i]));
318         }
319         var postData = t.join("&");
320                         
321         var url = "index.php?module=Calendar&action=CreateInvitee&sugar_body_only=true";        
322         YAHOO.util.Connect.setForm(document.forms['createInviteeForm']);
323         YAHOO.util.Connect.asyncRequest('POST', url, callback, postData);
324         
325 }
326
327 //////////////////////////////////////////////////
328 // class: SugarWidgetScheduler
329 // widget to display the meeting scheduler
330 //
331 //////////////////////////////////////////////////
332
333 SugarClass.inherit("SugarWidgetScheduler","SugarClass");
334
335 function SugarWidgetScheduler() {
336         this.init();
337 }
338
339 SugarWidgetScheduler.prototype.init = function() {
340         //var row = new SugarWidgetScheduleAttendees();
341         //row.load(this);
342 }
343
344 SugarWidgetScheduler.prototype.load = function(parentNode) {
345         this.parentNode = parentNode;
346         this.display();
347 }
348
349 SugarWidgetScheduler.fill_invitees = function(form) {
350         for(var i=0;i<GLOBAL_REGISTRY.focus.users_arr.length;i++) {
351                 if(GLOBAL_REGISTRY.focus.users_arr[i].module == 'User') {
352                         form.user_invitees.value += GLOBAL_REGISTRY.focus.users_arr[i].fields.id + ",";
353                 } else if(GLOBAL_REGISTRY.focus.users_arr[i].module == 'Contact') {
354                         form.contact_invitees.value += GLOBAL_REGISTRY.focus.users_arr[i].fields.id + ",";
355                 } else if(GLOBAL_REGISTRY.focus.users_arr[i].module == 'Lead') {
356                         form.lead_invitees.value += GLOBAL_REGISTRY.focus.users_arr[i].fields.id + ",";
357                 }
358         }
359 }
360
361 SugarWidgetScheduler.update_time = function() {
362
363         var form_name;
364         if(typeof document.EditView != 'undefined')
365                 form_name = "EditView";
366         else if(typeof document.CalendarEditView != 'undefined')
367                 form_name = "CalendarEditView";
368         else
369                 return;
370     
371    //check for field value, we can't do anything if it doesnt exist.
372     if(typeof document.forms[form_name].date_start == 'undefined')
373                 return;
374
375         var date_start = document.forms[form_name].date_start.value;
376         if(date_start.length < 16) {
377                 return;
378         }
379         var hour_start = parseInt(date_start.substring(11,13), 10);
380         var minute_start = parseInt(date_start.substring(14,16), 10);
381         var has_meridiem = /am|pm/i.test(date_start);
382         if(has_meridiem) {
383         var meridiem = trim(date_start.substring(16));
384         }
385
386         GLOBAL_REGISTRY.focus.fields.date_start = date_start;
387
388         if(has_meridiem) {
389                 GLOBAL_REGISTRY.focus.fields.time_start = hour_start + time_separator + minute_start + meridiem;
390         } else {
391                 GLOBAL_REGISTRY.focus.fields.time_start = hour_start + time_separator + minute_start;
392         }
393
394         GLOBAL_REGISTRY.focus.fields.duration_hours = document.forms[form_name].duration_hours.value;
395         GLOBAL_REGISTRY.focus.fields.duration_minutes = document.forms[form_name].duration_minutes.value;
396         GLOBAL_REGISTRY.focus.fields.datetime_start = SugarDateTime.mysql2jsDateTime(GLOBAL_REGISTRY.focus.fields.date_start,GLOBAL_REGISTRY.focus.fields.time_start);
397
398         GLOBAL_REGISTRY.scheduler_attendees_obj.init();
399         GLOBAL_REGISTRY.scheduler_attendees_obj.display();
400 }
401
402 SugarWidgetScheduler.prototype.display = function() {
403     this.parentNode.innerHTML = '';
404
405         var attendees = new SugarWidgetSchedulerAttendees();
406         attendees.load(this.parentNode);
407
408         var search = new SugarWidgetSchedulerSearch();
409         search.load(this.parentNode);
410 }
411
412
413 //////////////////////////////////////////////////
414 // class: SugarWidgetSchedulerAttendees
415 // widget to display the meeting attendees and availability
416 //
417 //////////////////////////////////////////////////
418
419 SugarClass.inherit("SugarWidgetSchedulerAttendees","SugarClass");
420
421 function SugarWidgetSchedulerAttendees() {
422         this.init();
423 }
424
425 SugarWidgetSchedulerAttendees.prototype.init = function() {
426
427         var form_name;
428         if(typeof document.EditView != 'undefined')
429                 form_name = "EditView";
430         else if(typeof document.CalendarEditView != 'undefined')
431                 form_name = "CalendarEditView";
432         else
433                 return;
434
435         // this.datetime = new SugarDateTime();
436         GLOBAL_REGISTRY.scheduler_attendees_obj = this;
437         var date_start = document.forms[form_name].date_start.value;
438         var hour_start = parseInt(date_start.substring(11,13), 10);
439         var minute_start = parseInt(date_start.substring(14,16), 10);
440         var has_meridiem = /am|pm/i.test(date_start);
441         if(has_meridiem) {
442         var meridiem = trim(date_start.substring(16));
443         }
444
445         if(has_meridiem) {
446                 GLOBAL_REGISTRY.focus.fields.time_start = hour_start + time_separator + minute_start + meridiem;
447         } else {
448                 GLOBAL_REGISTRY.focus.fields.time_start = hour_start+time_separator+minute_start;
449                 //GLOBAL_REGISTRY.focus.fields.time_start = document.forms[form_name].time_hour_start.value+time_separator+minute_start;
450         }
451
452         GLOBAL_REGISTRY.focus.fields.date_start = document.forms[form_name].date_start.value;
453         GLOBAL_REGISTRY.focus.fields.duration_hours = document.forms[form_name].duration_hours.value;
454         GLOBAL_REGISTRY.focus.fields.duration_minutes = document.forms[form_name].duration_minutes.value;
455         GLOBAL_REGISTRY.focus.fields.datetime_start = SugarDateTime.mysql2jsDateTime(GLOBAL_REGISTRY.focus.fields.date_start,GLOBAL_REGISTRY.focus.fields.time_start);
456
457         this.timeslots = new Array();
458         this.hours = 9;
459         this.segments = 4;
460         this.start_hours_before = 4;
461
462         var minute_interval = 15;
463         var dtstart = GLOBAL_REGISTRY.focus.fields.datetime_start;
464
465         // initialize first date in timeslots
466         var curdate = new Date(dtstart.getFullYear(),dtstart.getMonth(),dtstart.getDate(),dtstart.getHours()-this.start_hours_before,0);
467
468         if(typeof(GLOBAL_REGISTRY.focus.fields.duration_minutes) == 'undefined') {
469                 GLOBAL_REGISTRY.focus.fields.duration_minutes = 0;
470         }
471         GLOBAL_REGISTRY.focus.fields.datetime_end = new Date(dtstart.getFullYear(),dtstart.getMonth(),dtstart.getDate(),dtstart.getHours()+parseInt(GLOBAL_REGISTRY.focus.fields.duration_hours),dtstart.getMinutes()+parseInt(GLOBAL_REGISTRY.focus.fields.duration_minutes),0);
472
473         var has_start = false;
474         var has_end = false;
475
476         for(i=0;i < this.hours*this.segments; i++) {
477                 var hash = SugarDateTime.getUTCHash(curdate);
478                 var obj = {"hash":hash,"date_obj":curdate};
479                 if(has_start == false && GLOBAL_REGISTRY.focus.fields.datetime_start.getTime() <= curdate.getTime()) {
480                         obj.is_start = true;
481                         has_start = true;
482                 }
483                 if(has_end == false && GLOBAL_REGISTRY.focus.fields.datetime_end.getTime() <= curdate.getTime()) {
484                         obj.is_end = true;
485                         has_end = true;
486                 }
487                 this.timeslots.push(obj);
488
489                 curdate = new Date(curdate.getFullYear(),curdate.getMonth(),curdate.getDate(),curdate.getHours(),curdate.getMinutes()+minute_interval);
490         }
491 }
492
493 SugarWidgetSchedulerAttendees.prototype.load = function (parentNode) {
494         this.parentNode = parentNode;
495         this.display();
496 }
497
498 SugarWidgetSchedulerAttendees.prototype.display = function() {
499
500         var form_name;
501         if(typeof document.EditView != 'undefined')
502                 form_name = "EditView";
503         else if(typeof document.CalendarEditView != 'undefined')
504                 form_name = "CalendarEditView";
505         else
506                 return;
507
508         var dtstart = GLOBAL_REGISTRY.focus.fields.datetime_start;
509         var top_date = SugarDateTime.getFormattedDate(dtstart);
510         var html = '<h3>'+GLOBAL_REGISTRY['meeting_strings']['LBL_SCHEDULING_FORM_TITLE']+'</h3><table id ="schedulerTable">';
511         html += '<tr class="schedulerTopRow">';
512         html += '<th colspan="'+((this.hours*this.segments)+2)+'"><h4>'+ top_date +'</h4></th>';
513         html += '</tr>';
514         html += '<tr class="schedulerTimeRow">';
515         html += '<td>&nbsp;</td>';
516
517         for(var i=0;i < (this.timeslots.length/this.segments); i++) {
518                 var hours = this.timeslots[i*this.segments].date_obj.getHours();
519                 var am_pm = '';
520
521                 if(time_reg_format.indexOf('A') >= 0 || time_reg_format.indexOf('a') >= 0) {
522                         am_pm = "AM";
523
524                         if(hours > 12) {
525                                 am_pm = "PM";
526                                 hours -= 12;
527                         }
528                         if(hours == 12) {
529                                 am_pm = "PM";
530                         }
531                         if(hours == 0) {
532                                 hours = 12;
533                                 am_pm = "AM";
534                         }
535                         if(time_reg_format.indexOf('a') >= 0) {
536                                 am_pm = am_pm.toLowerCase();
537                         }
538                         if(hours != 0 && hours != 12 && i != 0) {
539                                 am_pm = "";
540                         }
541
542                 }
543
544                 var form_hours = hours+time_separator+"00";
545                 html += '<th scope="col" colspan="'+this.segments+'">'+form_hours+am_pm+'</th>';
546         }
547
548         html += '<td>&nbsp;</td>';
549         html += '</tr>';
550         html += '</table>';
551     if ( this.parentNode.childNodes.length < 1 )
552         this.parentNode.innerHTML += '<div class="schedulerDiv">' + html + '</div>';
553     else
554         this.parentNode.childNodes[0].innerHTML = html;
555
556         var thetable = "schedulerTable";
557
558         if(typeof (GLOBAL_REGISTRY) == 'undefined') {
559                 return;
560         }
561
562         //set the current user (as event-coordinator) so that they can be added to invitee list
563         //only IF the first removed flag has not been set AND this is a new record
564         if((typeof (GLOBAL_REGISTRY.focus.users_arr) == 'undefined' || GLOBAL_REGISTRY.focus.users_arr.length == 0)
565       && document.forms[form_name].record.value =='' && typeof(GLOBAL_REGISTRY.FIRST_REMOVE)=='undefined') {
566                 GLOBAL_REGISTRY.focus.users_arr = [ GLOBAL_REGISTRY.current_user ];
567         }
568         
569         if(typeof GLOBAL_REGISTRY.focus.users_arr_hash == 'undefined') {
570                 GLOBAL_REGISTRY.focus.users_arr_hash = new Object();
571         }
572
573         // append attendee rows
574         for(var i=0;i < GLOBAL_REGISTRY.focus.users_arr.length;i++) {
575                 var row = new SugarWidgetScheduleRow(this.timeslots);
576                 row.focus_bean = GLOBAL_REGISTRY.focus.users_arr[i];
577                 GLOBAL_REGISTRY.focus.users_arr_hash[ GLOBAL_REGISTRY.focus.users_arr[i]['fields']['id']] =     GLOBAL_REGISTRY.focus.users_arr[i];
578                 row.load(thetable);
579         }
580 }
581
582 SugarWidgetSchedulerAttendees.form_add_attendee = function (list_row) {
583         if(typeof (GLOBAL_REGISTRY.result_list[list_row]) != 'undefined' && typeof(GLOBAL_REGISTRY.focus.users_arr_hash[ GLOBAL_REGISTRY.result_list[list_row].fields.id]) == 'undefined') {
584                 GLOBAL_REGISTRY.focus.users_arr[ GLOBAL_REGISTRY.focus.users_arr.length ] = GLOBAL_REGISTRY.result_list[list_row];
585         }
586         GLOBAL_REGISTRY.scheduler_attendees_obj.display();
587 }
588
589
590 //////////////////////////////////////////////////
591 // class: SugarWidgetScheduleRow
592 // widget to display each row in the scheduler
593 //
594 //////////////////////////////////////////////////
595 SugarClass.inherit("SugarWidgetScheduleRow","SugarClass");
596
597 function SugarWidgetScheduleRow(timeslots) {
598         this.init(timeslots);
599 }
600
601 SugarWidgetScheduleRow.prototype.init = function(timeslots) {
602         this.timeslots = timeslots;
603 }
604
605 SugarWidgetScheduleRow.prototype.load = function (thetableid) {
606         this.thetableid = thetableid;
607         var self = this;
608
609         vcalClient = new SugarVCalClient();
610         if(typeof (GLOBAL_REGISTRY['freebusy_adjusted']) == 'undefined' ||      typeof (GLOBAL_REGISTRY['freebusy_adjusted'][this.focus_bean.fields.id]) == 'undefined') {
611                 global_request_registry[req_count] = [this,'display'];
612                 vcalClient.load(this.focus_bean.fields.id,req_count);
613                 req_count++;
614         } else {
615                 this.display();
616         }
617 }
618
619 SugarWidgetScheduleRow.prototype.display = function() {
620         SUGAR.util.doWhen("document.getElementById('" + this.thetableid + "') != null", function(){
621         var tr;
622         this.thetable = document.getElementById(this.thetableid);
623
624         if(typeof (this.element) != 'undefined') {
625             if (this.element.parentNode != null)
626                 this.thetable.deleteRow(this.element.rowIndex);
627
628             tr = document.createElement('tr');
629             this.thetable.appendChild(tr);
630         } else {
631             tr = this.thetable.insertRow(this.thetable.rows.length);
632         }
633         tr.className = "schedulerAttendeeRow";
634
635         td = document.createElement('td');
636         tr.appendChild(td);
637         //insertCell(tr.cells.length);
638
639         // icon + full name
640         td.scope = 'row';
641         var img = '<img align="absmiddle" src="index.php?entryPoint=getImage&themeName='
642                 + SUGAR.themes.theme_name+'&imageName='+this.focus_bean.module+'s.gif"/>&nbsp;';
643         td.innerHTML = img;
644
645         td.innerHTML = td.innerHTML;
646
647         if (this.focus_bean.fields.full_name)
648             td.innerHTML += ' ' + this.focus_bean.fields.full_name;
649         else
650             td.innerHTML += ' ' + this.focus_bean.fields.name;
651
652         // add freebusy tds here:
653         this.add_freebusy_nodes(tr);
654
655         // delete button
656         var td = document.createElement('td');
657         tr.appendChild(td);
658         //var td = tr.insertCell(tr.cells.length);
659         td.className = 'schedulerAttendeeDeleteCell';
660         td.noWrap = true;
661         //CCL - Remove check to disallow removal of assigned user or current user
662         //if ( GLOBAL_REGISTRY.focus.fields.assigned_user_id != this.focus_bean.fields.id && GLOBAL_REGISTRY.current_user.fields.id != this.focus_bean.fields.id) {
663        td.innerHTML = '<a title="'+ GLOBAL_REGISTRY['meeting_strings']['LBL_REMOVE']
664                     + '" class="listViewTdToolsS1" style="text-decoration:none;" '
665                     + 'href="javascript:SugarWidgetScheduleRow.deleteRow(\''+this.focus_bean.fields.id+'\');">&nbsp;'
666                     + '<img src="index.php?entryPoint=getImage&themeName='+SUGAR.themes.theme_name+'&imageName=delete_inline.gif" '
667                     + 'align="absmiddle" alt="'+ GLOBAL_REGISTRY['meeting_strings']['LBL_REMOVE'] +'" border="0"> '
668                     + GLOBAL_REGISTRY['meeting_strings']['LBL_REMOVE'] +'</a>';
669         //}
670         this.element = tr;
671         this.element_index = this.thetable.rows.length - 1;
672     }, null, this);
673 }
674
675 SugarWidgetScheduleRow.deleteRow = function(bean_id) {
676         // can't delete organizer
677         /*
678         if(GLOBAL_REGISTRY.focus.users_arr.length == 1 || GLOBAL_REGISTRY.focus.fields.assigned_user_id == bean_id) {
679                 return;
680         }
681     */
682         for(var i=0;i<GLOBAL_REGISTRY.focus.users_arr.length;i++) {
683                 if(GLOBAL_REGISTRY.focus.users_arr[i]['fields']['id']==bean_id) {
684                         delete GLOBAL_REGISTRY.focus.users_arr_hash[GLOBAL_REGISTRY.focus.users_arr[i]['fields']['id']];
685                         GLOBAL_REGISTRY.focus.users_arr.splice(i,1);
686                 //set first remove flag to true for processing in display() function
687                         GLOBAL_REGISTRY.FIRST_REMOVE = true;
688                         GLOBAL_REGISTRY.container.root_widget.display();
689                 }
690         }
691 }
692
693
694 function DL_GetElementLeft(eElement) {
695         /*
696          * ifargument is invalid
697          * (not specified, is null or is 0)
698          * and function is a method
699          * identify the element as the method owner
700          */
701         if(!eElement && this) {
702                 eElement = this;
703         }
704
705         /*
706          * initialize var to store calculations
707          * identify first offset parent element
708          * move up through element hierarchy
709          * appending left offset of each parent
710          * until no more offset parents exist
711          */
712         var nLeftPos = eElement.offsetLeft;
713         var eParElement = eElement.offsetParent;
714         while (eParElement != null) {
715                 nLeftPos += eParElement.offsetLeft;
716                 eParElement = eParElement.offsetParent;
717         }
718         return nLeftPos; // return the number calculated
719 }
720
721
722 function DL_GetElementTop(eElement) {
723         if(!eElement && this) {
724                 eElement = this;
725         }
726
727         var nTopPos = eElement.offsetTop;
728         var eParElement = eElement.offsetParent;
729         while (eParElement != null) {
730                 nTopPos += eParElement.offsetTop;
731                 eParElement = eParElement.offsetParent;
732         }
733         return nTopPos;
734 }
735
736
737 //////////////////////////////////////////
738 // adds the <td>s for freebusy display within a row
739 SugarWidgetScheduleRow.prototype.add_freebusy_nodes = function(tr,attendee) {
740         var hours = 9;
741         var segments = 4;
742         var html = '';
743         var is_loaded = false;
744
745         if(typeof GLOBAL_REGISTRY['freebusy_adjusted'] != 'undefined' && typeof GLOBAL_REGISTRY['freebusy_adjusted'][this.focus_bean.fields.id] != 'undefined') {
746                 is_loaded = true;
747         }
748
749         for(var i=0;i < this.timeslots.length; i++) {
750                 var td = document.createElement('td');
751                 tr.appendChild(td);
752                 //var td = tr.insertCell(tr.cells.length);
753         td.innerHTML = '&nbsp;';
754                 if(typeof(this.timeslots[i]['is_start']) != 'undefined') {
755                         td.className = 'schedulerSlotCellStartTime';
756                 }
757                 if(typeof(this.timeslots[i]['is_end']) != 'undefined') {
758                         td.className = 'schedulerSlotCellEndTime';
759                 }
760
761                 if(is_loaded) {
762                         // iftheres a freebusy stack in this slice
763                         if(     typeof(GLOBAL_REGISTRY['freebusy_adjusted'][this.focus_bean.fields.id][this.timeslots[i].hash]) != 'undefined') {
764                                 td.style.backgroundColor="#4D5EAA";
765
766                                 fb_limit = 1;
767                                 if(typeof(GLOBAL_REGISTRY.focus.orig_users_arr_hash) != 'undefined' && typeof(GLOBAL_REGISTRY.focus.orig_users_arr_hash[this.focus_bean.fields.id]) != 'undefined') {
768                                         fb_limit = 2;
769                                 }
770
771                                 if(     GLOBAL_REGISTRY['freebusy_adjusted'][this.focus_bean.fields.id][this.timeslots[i].hash] >= fb_limit) {
772                                         td.style.backgroundColor="#AA4D4D";
773                                 }
774                         }
775                 }
776         }
777 }