]> err.no Git - scalable-opengroupware.org/blob - UI/WebServerResources/skycalendar.js
git-svn-id: http://svn.opengroupware.org/SOGo/inverse/trunk@1294 d1b88da0-ebda-0310...
[scalable-opengroupware.org] / UI / WebServerResources / skycalendar.js
1 // Title: Tigra Calendar
2 // Description: See the demo at url
3 // URL: http://www.softcomplex.com/products/tigra_calendar/
4 // Version: 3.1 (European date format)
5 // Date: 08-08-2002 (mm-dd-yyyy)
6 // Feedback: feedback@softcomplex.com (specify product title in the subject)
7 // Note: Permission given to use this script in ANY kind of applications if
8 //    header lines are left unchanged.
9 // Note: Script consists of two files: calendar?.js and calendar.html
10 // About us: Our company provides offshore IT consulting services.
11 //    Contact us at sales@softcomplex.com if you have any programming task you
12 //    want to be handled by professionals. Our typical hourly rate is $20.
13
14 // modified by Martin Hoerning, mh@skyrix.com, 2002-12-05
15 // 2003-01-23 added date format support (Martin Hoerning)
16
17 // if two digit year input dates after this year considered 20 century.
18 var NUM_CENTYEAR = 30;
19 // are year scrolling buttons required by default
20 var BUL_YEARSCROLL = true;
21 var DEF_CALPAGE = 'skycalendar.html';
22
23 var calendars = [];
24 var RE_NUM = /^\-?\d+$/;
25 var dateFormat = "yyyy-mm-dd";
26
27 function skycalendar(obj_target) {
28   // assing methods
29   this.gen_date = cal_gen_date1;
30   this.gen_tsmp = cal_gen_tsmp1;
31   this.prs_date = cal_prs_date1;
32   this.prs_tsmp = cal_prs_tsmp1;
33   this.popup    = cal_popup1;
34   this.setCalendarPage = cal_setcalpage1;
35   this.setDateFormat   = cal_setdateformat1;
36
37   // validate input parameters
38   if (!obj_target)
39     return cal_error("Error calling the calendar: no target control specified");
40   if (obj_target.value == null)
41     return cal_error("Error calling the calendar: parameter specified is not valid tardet control");
42   this.target = obj_target;
43   this.year_scroll = BUL_YEARSCROLL;
44   this.calpage     = DEF_CALPAGE;
45         
46   // register in global collections
47   this.id = calendars.length;
48   calendars[this.id] = this;
49 }
50
51 function cal_setcalpage1(str_page) {
52   this.calpage = str_page;
53 }
54
55 function cal_setdateformat1(str_dateformat) {
56   this.dateFormat = str_dateformat;
57 }
58
59 function cal_popup1(str_datetime) {
60   this.dt_current = this.prs_tsmp(str_datetime ? str_datetime : this.target.value);
61   if (!this.dt_current) return;
62
63   var obj_calwindow = window.open(
64     this.calpage+'?datetime=' + this.dt_current.valueOf()+ '&id=' + this.id,
65       'Calendar', 'width=200,height=190'+
66       ',status=no,resizable=no,top=200,left=200,dependent=yes,alwaysRaised=yes'
67     );
68   obj_calwindow.opener = window;
69   obj_calwindow.focus();
70 }
71
72 // timestamp generating function
73 function cal_gen_tsmp1(dt_datetime) {
74   return this.gen_date(dt_datetime);
75 }
76
77 // date generating function
78 function cal_gen_date1(dt_datetime) {
79   var out = this.dateFormat;
80   var idx;
81   if (out.indexOf("yyyy") != 1) {
82     t = out.split("yyyy");
83     out = t.join(dt_datetime.getFullYear());
84   }
85   else {
86     return cal_error("Missing year-part 'yyyy' in format: '"+this.dateFormat);
87   }
88   if (out.indexOf("mm") != 1) {
89     t = out.split("mm");
90     out = t.join((dt_datetime.getMonth() < 9 ? '0' : '')+
91                  (dt_datetime.getMonth()+1));
92   }
93   else {
94     return cal_error("Missing month-part 'mm' in format: '"+this.dateFormat);
95   }
96   if (out.indexOf("dd") != 1) {
97     t = out.split("dd");
98     out = t.join((dt_datetime.getDate() < 10 ? '0' : '')+
99                  dt_datetime.getDate());
100   }
101   else {
102     return cal_error("Missing day-part 'dd' in format: '"+this.dateFormat);
103   }
104
105   return out;
106 }
107
108 // timestamp parsing function
109 function cal_prs_tsmp1(str_datetime) {
110   // if no parameter specified return current timestamp
111   if (!str_datetime)
112     return (new Date());
113
114   // if positive integer treat as milliseconds from epoch
115   if (RE_NUM.exec(str_datetime))
116     return new Date(str_datetime);
117                 
118   return this.prs_date(str_datetime);
119 }
120
121 // date parsing function
122 function cal_prs_date1(str_date) {
123   var idx;
124   var year  = null;
125   var month = null;
126   var day   = null;
127
128   if (str_date.length != this.dateFormat.length) {
129     return cal_error ("Invalid date format: '"+str_date+
130                       "'.\nFormat accepted is '"+this.dateFormat+"'.");
131   }
132
133   if ((idx = this.dateFormat.indexOf("yyyy")) != 1) {
134     year = str_date.substring(idx, idx+4);
135   }
136   else {
137     return cal_error("Missing year-part 'yyyy' in format: '"+this.dateFormat);
138   }
139   if ((idx = this.dateFormat.indexOf("mm")) != 1) {
140     month = str_date.substring(idx, idx+2);
141   }
142   else {
143     return cal_error("Missing month-part 'mm' in format: '"+this.dateFormat);
144   }
145   if ((idx = this.dateFormat.indexOf("dd")) != 1) {
146     day = str_date.substring(idx, idx+2);
147   }
148   else {
149     return cal_error("Missing day-part 'dd' in format: '"+this.dateFormat);
150   }
151
152   if (!day) return cal_error("Invalid date format: '"+str_date+
153                              "'.\nNo day of month value can be found.");
154   if (!RE_NUM.exec(day))
155     return cal_error("Invalid day of month value: '"+day+
156                      "'.\nAllowed values are unsigned integers.");
157
158   if (!month) return cal_error("Invalid date format: '"+str_date+
159                              "'.\nNo month of year value can be found.");
160   if (!RE_NUM.exec(month))
161     return cal_error("Invalid month of year value: '"+month+
162                      "'.\nAllowed values are unsigned integers.");
163   
164   if (!year) return cal_error("Invalid date format: '"+str_date+
165                              "'.\nNo year value can be found.");
166   if (!RE_NUM.exec(year))
167     return cal_error("Invalid year value: '"+year+
168                      "'.\nAllowed values are unsigned integers.");
169
170   
171   var dt_date = new Date();
172   dt_date.setDate(1);
173   if (month < 1 || month > 12)
174     return cal_error("Invalid month value: '"+month+
175                      "'.\nAllowed range is 01-12.");
176   dt_date.setMonth(month-1);
177   if (year < 100) year = Number(year)+(year < NUM_CENTYEAR ? 2000 : 1900);
178   dt_date.setFullYear(year);
179   var dt_numdays = new Date(year, month, 0);
180   dt_date.setDate(day);
181   if (dt_date.getMonth() != (month-1))
182     return cal_error("Invalid day of month value: '"+day+
183                      "'.\nAllowed range is 01-"+dt_numdays.getDate()+".");
184   return (dt_date);
185 }
186
187 function cal_error(str_message) {
188   alert (str_message);
189   return null;
190 }