]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/UIxAppointmentEditor.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1209 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   //   cuicui = 'year';
27   if (date1.getYear()  < date2.getYear()) return date1;
28   if (date1.getYear()  > date2.getYear()) return date2;
29   // same year
30   //   cuicui += '/month';
31   if (date1.getMonth() < date2.getMonth()) return date1;
32   if (date1.getMonth() > date2.getMonth()) return date2;
33   //   // same month
34   //   cuicui += '/date';
35   if (date1.getDate() < date2.getDate()) return date1;
36   if (date1.getDate() > date2.getDate()) return date2;
37   // same day
38   return null;
39 }
40
41 function validateAptEditor() {
42   var e, startdate, enddate, tmpdate;
43
44   e = $('summary');
45   if (e.value.length == 0) {
46     if (!confirm(labels.validate_notitle))
47       return false;
48   }
49
50   e = $('startTime_date');
51   if (e.value.length != 10) {
52     alert(labels.validate_invalid_startdate);
53     return false;
54   }
55   startdate = e.calendar.prs_date(e.value);
56   if (startdate == null) {
57     alert(labels.validate_invalid_startdate);
58     return false;
59   }
60       
61   e = $('endTime_date');
62   if (e.value.length != 10) {
63     alert(labels.validate_invalid_enddate);
64     return false;
65   }
66   enddate = e.calendar.prs_date(e.value);
67   if (enddate == null) {
68     alert(labels.validate_invalid_enddate);
69     return false;
70   }
71   //   cuicui = '';
72   tmpdate = uixEarlierDate(startdate, enddate);
73   if (tmpdate == enddate) {
74     //     window.alert(cuicui);
75     alert(labels.validate_endbeforestart);
76     return false;
77   }
78   else if (tmpdate == null /* means: same date */) {
79     // TODO: check time
80     var start, end;
81     
82     start = parseInt(document.forms[0]['startTime_time_hour'].value);
83     end = parseInt(document.forms[0]['endTime_time_hour'].value);
84
85     if (start > end) {
86       alert(labels.validate_endbeforestart);
87       return false;
88     }
89     else if (start == end) {
90       start = parseInt(document.forms[0]['startTime_time_minute'].value);
91       end = parseInt(document.forms[0]['endTime_time_minute'].value);
92       if (start > end) {
93         alert(labels.validate_endbeforestart);
94         return false;
95       }
96     }
97   }
98
99   return true;
100 }
101
102 function toggleDetails() {
103   var div = $("details");
104   var buttons = $("buttons");
105   var buttonsHeight = buttons.clientHeight * 3;
106
107   if (div.style.visibility) {
108     div.style.visibility = null;
109     window.resizeBy(0, -(div.clientHeight + buttonsHeight));
110     $("detailsButton").innerHTML = labels["Show Details"];
111   } else {
112     div.style.visibility = 'visible;';
113     window.resizeBy(0, (div.clientHeight + buttonsHeight));
114     $("detailsButton").innerHTML = labels["Hide Details"];
115   }
116
117   return false;
118 }
119
120 function toggleCycleVisibility(node, nodeName, hiddenValue) {
121   var spanNode = $(nodeName);
122   var newVisibility = ((node.value == hiddenValue) ? null : 'visible;');
123   spanNode.style.visibility = newVisibility;
124
125   if (nodeName == 'cycleSelectionFirstLevel') {
126     var otherSpanNode = $('cycleSelectionSecondLevel');
127     if (!newVisibility)
128       {
129         otherSpanNode.superVisibility = otherSpanNode.style.visibility;
130         otherSpanNode.style.visibility = null;
131       }
132     else
133       {
134         otherSpanNode.style.visibility = otherSpanNode.superVisibility;
135         otherSpanNode.superVisibility = null;
136       }
137   }
138 }
139
140 function addContact(tag, fullContactName, contactId, contactName, contactEmail) {
141   var uids = $('uixselector-participants-uidList');
142   log ("contactId: " + contactId);
143   if (contactId)
144     {
145       var re = new RegExp("(^|,)" + contactId + "($|,)");
146
147       log ("uids: " + uids);
148       if (!re.test(uids.value))
149         {
150           log ("no match... realling adding");
151           if (uids.value.length > 0)
152             uids.value += ',' + contactId;
153           else
154             uids.value = contactId;
155
156           var names = $('uixselector-participants-display');
157           names.innerHTML += ('<li onmousedown="return false;"'
158                               + ' onclick="onRowClick(event);"><img src="'
159                               + ResourcesURL + '/abcard.gif" />'
160                               + contactName + '</li>');
161         }
162       else
163         log ("match... ignoring contact");
164     }
165
166   return false;
167 }
168
169 function saveEvent(sender) {
170   if (validateAptEditor())
171     document.forms['editform'].submit();
172
173   return false;
174 }
175
176 function startDayAsShortString() {
177   return $('startTime_date').valueAsShortDateString();
178 }
179
180 function endDayAsShortString() {
181   return $('endTime_date').valueAsShortDateString();
182 }
183
184 function _getDate(which) {
185   var date = window.timeWidgets[which]['date'].valueAsDate();
186   date.setHours( window.timeWidgets[which]['hour'].value );
187   date.setMinutes( window.timeWidgets[which]['minute'].value );
188
189   return date;
190 }
191
192 function _getShadowDate(which) {
193   var date = window.timeWidgets[which]['date'].getAttribute("shadow-value").asDate();
194   var intValue = parseInt(window.timeWidgets[which]['hour'].getAttribute("shadow-value"));
195   date.setHours(intValue);
196   intValue = parseInt(window.timeWidgets[which]['minute'].getAttribute("shadow-value"));
197   date.setMinutes(intValue);
198   //   window.alert("shadow: " + date);
199
200   return date;
201 }
202
203 function getStartDate() {
204   return this._getDate('start');
205 }
206
207 function getEndDate() {
208   return this._getDate('end');
209 }
210
211 function getShadowStartDate() {
212   return this._getShadowDate('start');
213 }
214
215 function getShadowEndDate() {
216   return this._getShadowDate('end');
217 }
218
219 function _setDate(which, newDate) {
220   window.timeWidgets[which]['date'].setValueAsDate(newDate);
221   window.timeWidgets[which]['hour'].value = newDate.getHours();
222   var minutes = newDate.getMinutes();
223   if (minutes % 15)
224     minutes += (15 - minutes % 15);
225   window.timeWidgets[which]['minute'].value = minutes;
226 }
227
228 function setStartDate(newStartDate) {
229   this._setDate('start', newStartDate);
230 }
231
232 function setEndDate(newEndDate) {
233   //   window.alert(newEndDate);
234   this._setDate('end', newEndDate);
235 }
236
237 function onAdjustEndTime(event) {
238   var dateDelta = (window.getStartDate().valueOf()
239                    - window.getShadowStartDate().valueOf());
240   //   window.alert(window.getEndDate().valueOf() + '  ' + dateDelta);
241   var newEndDate = new Date(window.getEndDate().valueOf() + dateDelta);
242   window.setEndDate(newEndDate);
243   window.timeWidgets['start']['date'].updateShadowValue();
244   window.timeWidgets['start']['hour'].updateShadowValue();
245   window.timeWidgets['start']['minute'].updateShadowValue();
246 }
247
248 function onAllDayChanged(event) {
249   for (var type in window.timeWidgets) {
250     window.timeWidgets[type]['hour'].disabled = this.checked;
251     window.timeWidgets[type]['minute'].disabled = this.checked;
252   }
253 }
254
255 function initTimeWidgets(widgets) {
256   this.timeWidgets = widgets;
257
258   Event.observe(widgets['start']['date'], "change",
259                 this.onAdjustEndTime, false);
260   Event.observe(widgets['start']['hour'], "change",
261                 this.onAdjustEndTime, false);
262   Event.observe(widgets['start']['minute'], "change",
263                 this.onAdjustEndTime, false);
264
265   var allDayLabel = $("allDay");
266   var input = $(allDayLabel).childNodesWithTag("input")[0];
267   Event.observe(input, "change", onAllDayChanged.bindAsEventListener(input));
268   if (input.checked) {
269     for (var type in widgets) {
270       widgets[type]['hour'].disabled = true;
271       widgets[type]['minute'].disabled = true;
272     }
273   }
274 }
275
276 function onAppointmentEditorLoad() {
277   assignCalendar('startTime_date');
278   assignCalendar('endTime_date');
279
280   var widgets = {'start': {'date': $("startTime_date"),
281                            'hour': $("startTime_time_hour"),
282                            'minute': $("startTime_time_minute")},
283                  'end': {'date': $("endTime_date"),
284                          'hour': $("endTime_time_hour"),
285                          'minute': $("endTime_time_minute")}};
286   initTimeWidgets(widgets);
287 }
288
289 addEvent(window, 'load', onAppointmentEditorLoad);