]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxAppointmentEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1267 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / UIxAppointmentEditor.js
1 /*
2   Copyright (C) 2005 SKYRIX Software AG
3  
4   This file is part of OpenGroupware.org.
5  
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10  
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 var contactSelectorAction = 'calendars-contacts';
23
24 function uixEarlierDate(date1, date2) {
25   // can this be done in a sane way?
26   if (date1.getYear()  < date2.getYear()) return date1;
27   if (date1.getYear()  > date2.getYear()) return date2;
28   // same year
29   if (date1.getMonth() < date2.getMonth()) return date1;
30   if (date1.getMonth() > date2.getMonth()) return date2;
31   //   // same month
32   if (date1.getDate() < date2.getDate()) return date1;
33   if (date1.getDate() > date2.getDate()) return date2;
34   // same day
35   return null;
36 }
37
38 function validateAptEditor() {
39   var e, startdate, enddate, tmpdate;
40
41   e = $('summary');
42   if (e.value.length == 0) {
43     if (!confirm(labels.validate_notitle))
44       return false;
45   }
46
47   e = $('startTime_date');
48   if (e.value.length != 10) {
49     alert(labels.validate_invalid_startdate);
50     return false;
51   }
52   startdate = e.calendar.prs_date(e.value);
53   if (startdate == null) {
54     alert(labels.validate_invalid_startdate);
55     return false;
56   }
57       
58   e = $('endTime_date');
59   if (e.value.length != 10) {
60     alert(labels.validate_invalid_enddate);
61     return false;
62   }
63   enddate = e.calendar.prs_date(e.value);
64   if (enddate == null) {
65     alert(labels.validate_invalid_enddate);
66     return false;
67   }
68   tmpdate = uixEarlierDate(startdate, enddate);
69   if (tmpdate == enddate) {
70     alert(labels.validate_endbeforestart);
71     return false;
72   }
73   else if (tmpdate == null /* means: same date */) {
74     // TODO: check time
75     var start, end;
76     
77     start = parseInt(document.forms[0]['startTime_time_hour'].value);
78     end = parseInt(document.forms[0]['endTime_time_hour'].value);
79
80     if (start > end) {
81       alert(labels.validate_endbeforestart);
82       return false;
83     }
84     else if (start == end) {
85       start = parseInt(document.forms[0]['startTime_time_minute'].value);
86       end = parseInt(document.forms[0]['endTime_time_minute'].value);
87       if (start > end) {
88         alert(labels.validate_endbeforestart);
89         return false;
90       }
91     }
92   }
93
94   return true;
95 }
96
97 function toggleDetails() {
98   var div = $("details");
99   var buttons = $("buttons");
100   var buttonsHeight = buttons.clientHeight * 3;
101
102   if (div.style.visibility) {
103     div.style.visibility = null;
104     window.resizeBy(0, -(div.clientHeight + buttonsHeight));
105     $("detailsButton").innerHTML = labels["Show Details"];
106   } else {
107     div.style.visibility = 'visible;';
108     window.resizeBy(0, (div.clientHeight + buttonsHeight));
109     $("detailsButton").innerHTML = labels["Hide Details"];
110   }
111
112   return false;
113 }
114
115 function toggleCycleVisibility(node, nodeName, hiddenValue) {
116   var spanNode = $(nodeName);
117   var newVisibility = ((node.value == hiddenValue) ? null : 'visible;');
118   spanNode.style.visibility = newVisibility;
119
120   if (nodeName == 'cycleSelectionFirstLevel') {
121     var otherSpanNode = $('cycleSelectionSecondLevel');
122     if (!newVisibility)
123       {
124         otherSpanNode.superVisibility = otherSpanNode.style.visibility;
125         otherSpanNode.style.visibility = null;
126       }
127     else
128       {
129         otherSpanNode.style.visibility = otherSpanNode.superVisibility;
130         otherSpanNode.superVisibility = null;
131       }
132   }
133 }
134
135 function addContact(tag, fullContactName, contactId, contactName, contactEmail) {
136   var uids = $('uixselector-participants-uidList');
137   log ("contactId: " + contactId);
138   if (contactId)
139     {
140       var re = new RegExp("(^|,)" + contactId + "($|,)");
141
142       log ("uids: " + uids);
143       if (!re.test(uids.value))
144         {
145           log ("no match... realling adding");
146           if (uids.value.length > 0)
147             uids.value += ',' + contactId;
148           else
149             uids.value = contactId;
150
151           var names = $('uixselector-participants-display');
152           names.innerHTML += ('<li onmousedown="return false;"'
153                               + ' onclick="onRowClick(event);"><img src="'
154                               + ResourcesURL + '/abcard.gif" />'
155                               + contactName + '</li>');
156         }
157       else
158         log ("match... ignoring contact");
159     }
160
161   return false;
162 }
163
164 function saveEvent(sender) {
165   if (validateAptEditor())
166     document.forms['editform'].submit();
167
168   return false;
169 }
170
171 function startDayAsShortString() {
172   return $('startTime_date').valueAsShortDateString();
173 }
174
175 function endDayAsShortString() {
176   return $('endTime_date').valueAsShortDateString();
177 }
178
179 function _getDate(which) {
180   var date = window.timeWidgets[which]['date'].valueAsDate();
181   date.setHours( window.timeWidgets[which]['hour'].value );
182   date.setMinutes( window.timeWidgets[which]['minute'].value );
183
184   return date;
185 }
186
187 function _getShadowDate(which) {
188   var date = window.timeWidgets[which]['date'].getAttribute("shadow-value").asDate();
189   var intValue = parseInt(window.timeWidgets[which]['hour'].getAttribute("shadow-value"));
190   date.setHours(intValue);
191   intValue = parseInt(window.timeWidgets[which]['minute'].getAttribute("shadow-value"));
192   date.setMinutes(intValue);
193   //   window.alert("shadow: " + date);
194
195   return date;
196 }
197
198 function getStartDate() {
199   return this._getDate('start');
200 }
201
202 function getEndDate() {
203   return this._getDate('end');
204 }
205
206 function getShadowStartDate() {
207   return this._getShadowDate('start');
208 }
209
210 function getShadowEndDate() {
211   return this._getShadowDate('end');
212 }
213
214 function _setDate(which, newDate) {
215   window.timeWidgets[which]['date'].setValueAsDate(newDate);
216   window.timeWidgets[which]['hour'].value = newDate.getHours();
217   var minutes = newDate.getMinutes();
218   if (minutes % 15)
219     minutes += (15 - minutes % 15);
220   window.timeWidgets[which]['minute'].value = minutes;
221 }
222
223 function setStartDate(newStartDate) {
224   this._setDate('start', newStartDate);
225 }
226
227 function setEndDate(newEndDate) {
228   //   window.alert(newEndDate);
229   this._setDate('end', newEndDate);
230 }
231
232 function onAdjustEndTime(event) {
233   var dateDelta = (window.getStartDate().valueOf()
234                    - window.getShadowStartDate().valueOf());
235   //   window.alert(window.getEndDate().valueOf() + '  ' + dateDelta);
236   var newEndDate = new Date(window.getEndDate().valueOf() + dateDelta);
237   window.setEndDate(newEndDate);
238   window.timeWidgets['start']['date'].updateShadowValue();
239   window.timeWidgets['start']['hour'].updateShadowValue();
240   window.timeWidgets['start']['minute'].updateShadowValue();
241 }
242
243 function onAllDayChanged(event) {
244   for (var type in window.timeWidgets) {
245     window.timeWidgets[type]['hour'].disabled = this.checked;
246     window.timeWidgets[type]['minute'].disabled = this.checked;
247   }
248 }
249
250 function initTimeWidgets(widgets) {
251   this.timeWidgets = widgets;
252
253   Event.observe(widgets['start']['date'], "change",
254                 this.onAdjustEndTime, false);
255   Event.observe(widgets['start']['hour'], "change",
256                 this.onAdjustEndTime, false);
257   Event.observe(widgets['start']['minute'], "change",
258                 this.onAdjustEndTime, false);
259
260   var allDayLabel = $("allDay");
261   var input = $(allDayLabel).childNodesWithTag("input")[0];
262   Event.observe(input, "change", onAllDayChanged.bindAsEventListener(input));
263   if (input.checked) {
264     for (var type in widgets) {
265       widgets[type]['hour'].disabled = true;
266       widgets[type]['minute'].disabled = true;
267     }
268   }
269 }
270
271 function onAppointmentEditorLoad() {
272   assignCalendar('startTime_date');
273   assignCalendar('endTime_date');
274
275   var widgets = {'start': {'date': $("startTime_date"),
276                            'hour': $("startTime_time_hour"),
277                            'minute': $("startTime_time_minute")},
278                  'end': {'date': $("endTime_date"),
279                          'hour': $("endTime_time_hour"),
280                          'minute': $("endTime_time_minute")}};
281   initTimeWidgets(widgets);
282 }
283
284 FastInit.addOnLoad(onAppointmentEditorLoad);