]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxAppointmentEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1294 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
138   if (contactId)
139     {
140       var re = new RegExp("(^|,)" + contactId + "($|,)");
141
142       if (!re.test(uids.value))
143         {
144           if (uids.value.length > 0)
145             uids.value += ',' + contactId;
146           else
147             uids.value = contactId;
148
149           var names = $('uixselector-participants-display');
150           names.innerHTML += ('<li onmousedown="return false;"'
151                               + ' onclick="onRowClick(event);"><img src="'
152                               + ResourcesURL + '/abcard.gif" />'
153                               + contactName + '</li>');
154         }
155     }
156
157   return false;
158 }
159
160 function saveEvent(sender) {
161   if (validateAptEditor())
162     document.forms['editform'].submit();
163
164   return false;
165 }
166
167 function startDayAsShortString() {
168   return $('startTime_date').valueAsShortDateString();
169 }
170
171 function endDayAsShortString() {
172   return $('endTime_date').valueAsShortDateString();
173 }
174
175 function _getDate(which) {
176   var date = window.timeWidgets[which]['date'].valueAsDate();
177   date.setHours( window.timeWidgets[which]['hour'].value );
178   date.setMinutes( window.timeWidgets[which]['minute'].value );
179
180   return date;
181 }
182
183 function _getShadowDate(which) {
184   var date = window.timeWidgets[which]['date'].getAttribute("shadow-value").asDate();
185   var intValue = parseInt(window.timeWidgets[which]['hour'].getAttribute("shadow-value"));
186   date.setHours(intValue);
187   intValue = parseInt(window.timeWidgets[which]['minute'].getAttribute("shadow-value"));
188   date.setMinutes(intValue);
189
190   return date;
191 }
192
193 function getStartDate() {
194   return this._getDate('start');
195 }
196
197 function getEndDate() {
198   return this._getDate('end');
199 }
200
201 function getShadowStartDate() {
202   return this._getShadowDate('start');
203 }
204
205 function getShadowEndDate() {
206   return this._getShadowDate('end');
207 }
208
209 function _setDate(which, newDate) {
210   window.timeWidgets[which]['date'].setValueAsDate(newDate);
211   window.timeWidgets[which]['hour'].value = newDate.getHours();
212   var minutes = newDate.getMinutes();
213   if (minutes % 15)
214     minutes += (15 - minutes % 15);
215   window.timeWidgets[which]['minute'].value = minutes;
216 }
217
218 function setStartDate(newStartDate) {
219   this._setDate('start', newStartDate);
220 }
221
222 function setEndDate(newEndDate) {
223   this._setDate('end', newEndDate);
224 }
225
226 function onAdjustTime(event) {
227   var endDate = window.getEndDate();
228   var startDate = window.getStartDate();
229   
230   if ($(this).readAttribute("id").startsWith("start")) {
231     // Start date was changed
232     var delta = window.getShadowStartDate().valueOf() -
233       startDate.valueOf();
234     var newEndDate = new Date(endDate.valueOf() - delta);
235     window.setEndDate(newEndDate);
236     
237     window.timeWidgets['end']['date'].updateShadowValue();
238     window.timeWidgets['end']['hour'].updateShadowValue();
239     window.timeWidgets['end']['minute'].updateShadowValue();
240     window.timeWidgets['start']['date'].updateShadowValue();
241     window.timeWidgets['start']['hour'].updateShadowValue();
242     window.timeWidgets['start']['minute'].updateShadowValue();
243   }
244   else {
245     // End date was changed
246     var delta = endDate.valueOf() - startDate.valueOf();  
247     if (delta < 0) {
248       alert(labels.validate_endbeforestart);
249       var oldEndDate = window.getShadowEndDate();
250       window.setEndDate(oldEndDate);
251
252       window.timeWidgets['end']['date'].updateShadowValue();
253       window.timeWidgets['end']['hour'].updateShadowValue();
254       window.timeWidgets['end']['minute'].updateShadowValue();
255     }
256   }
257 }
258
259 function onAllDayChanged(event) {
260   for (var type in window.timeWidgets) {
261     window.timeWidgets[type]['hour'].disabled = this.checked;
262     window.timeWidgets[type]['minute'].disabled = this.checked;
263   }
264 }
265
266 function initTimeWidgets(widgets) {
267   this.timeWidgets = widgets;
268
269   Event.observe(widgets['start']['date'], "change",
270                 this.onAdjustTime, false);
271   Event.observe(widgets['start']['hour'], "change",
272                 this.onAdjustTime, false);
273   Event.observe(widgets['start']['minute'], "change",
274                 this.onAdjustTime, false);
275
276   Event.observe(widgets['end']['date'], "change",
277                 this.onAdjustTime, false);
278   Event.observe(widgets['end']['hour'], "change",
279                 this.onAdjustTime, false);
280   Event.observe(widgets['end']['minute'], "change",
281                 this.onAdjustTime, false);
282
283   var allDayLabel = $("allDay");
284   var input = $(allDayLabel).childNodesWithTag("input")[0];
285   Event.observe(input, "change", onAllDayChanged.bindAsEventListener(input));
286   if (input.checked) {
287     for (var type in widgets) {
288       widgets[type]['hour'].disabled = true;
289       widgets[type]['minute'].disabled = true;
290     }
291   }
292 }
293
294 function onAppointmentEditorLoad() {
295   assignCalendar('startTime_date');
296   assignCalendar('endTime_date');
297
298   var widgets = {'start': {'date': $("startTime_date"),
299                            'hour': $("startTime_time_hour"),
300                            'minute': $("startTime_time_minute")},
301                  'end': {'date': $("endTime_date"),
302                          'hour': $("endTime_time_hour"),
303                          'minute': $("endTime_time_minute")}};
304   initTimeWidgets(widgets);
305 }
306
307 FastInit.addOnLoad(onAppointmentEditorLoad);