Remove iso-accents
[infodrom.org/service.infodrom.org] / src / calendar.js
1 /* --- Swazz Javascript Calendar ---
2 /* --- v 1.0 3rd November 2006
3 By Oliver Bryant
4 http://calendar.swazz.org/
5
6 License: Public Domain
7   The javascript calendar available here is a free, lightweight widget
8   which shows a calendar for use in picking dates on form fields.
9
10 Code to activate the calendar
11
12    <script src="calendar.js"></script>
13
14    <input type="text" value="dd/mm/yy" onfocus="lcs(this)" onclick="event.cancelBubble=true;lcs(this)">
15
16    <input id="date" type="text" value="dd/mm/yy">
17    <img src="calendar.gif" onclick="event.cancelBubble=true;popcalendar('date');">
18
19 close.gif could be from
20   http://lh6.ggpht.com/_S4TaEbhoBZc/Rt8ad-K_AZI/AAAAAAAABlw/moApV80jJvo/close.gif
21   (search www.google.com for small pictures, pick one)
22
23 Call calendar({opt1: val1, opt2: val2}) for configuration.
24 */
25
26 var ccc = {past: false,
27            startat: 0,
28            close: false,
29            delim: '/',
30            imgpath: ''};
31
32 var caldays = new Array('S','M','T','W','T','F','S');
33 var mn=new Array('JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC');
34
35 function calendar(info)
36 {
37     for (var e in ccc)
38         if (info[e])
39             ccc[e] = info[e];
40     gencalendar();
41 }
42
43 function getObj(objID)
44 {
45     if (document.getElementById) {return document.getElementById(objID);}
46     else if (document.all) {return document.all[objID];}
47     else if (document.layers) {return document.layers[objID];}
48 }
49
50 function checkClick(e) {
51         e?evt=e:evt=event;
52         CSE=evt.target?evt.target:evt.srcElement;
53         if (getObj('fc'))
54                 if (!isChild(CSE,getObj('fc')))
55                         getObj('fc').style.display='none';
56 }
57
58 function isChild(s,d) {
59         while(s) {
60                 if (s==d) 
61                         return true;
62                 s=s.parentNode;
63         }
64         return false;
65 }
66
67 function Left(obj)
68 {
69         var curleft = 0;
70         if (obj.offsetParent)
71         {
72                 while (obj.offsetParent)
73                 {
74                         curleft += obj.offsetLeft
75                         obj = obj.offsetParent;
76                 }
77         }
78         else if (obj.x)
79                 curleft += obj.x;
80         return curleft;
81 }
82
83 function Top(obj)
84 {
85         var curtop = 0;
86         if (obj.offsetParent)
87         {
88                 while (obj.offsetParent)
89                 {
90                         curtop += obj.offsetTop
91                         obj = obj.offsetParent;
92                 }
93         }
94         else if (obj.y)
95                 curtop += obj.y;
96         return curtop;
97 }
98         
99 function gencalendar()
100 {
101     if (getObj('fc'))
102         return;
103
104 document.write('<table id="fc" style="position:absolute;border-collapse:collapse;background:#FFFFFF;border:1px solid #ABABAB;display:none" cellpadding=2>');
105 document.write('<tr><td style="cursor:pointer" onclick="csubm()"><img src="'+ccc.imgpath+'arrowleftmonth.gif"></td>');
106 if (ccc.close)
107     document.write('<td colspan=4 id="mns" align="center" style="font:bold 13px Arial">');
108 else
109     document.write('<td colspan=5 id="mns" align="center" style="font:bold 13px Arial">');
110 document.write('</td><td align="right" style="cursor:pointer" onclick="caddm()"><img src="'+ccc.imgpath+'arrowrightmonth.gif"></td>');
111 if (ccc.close)
112     document.write('<td align="right" style="cursor:pointer" onclick="cal_close()"><img src="'+ccc.imgpath+'close.gif"></td>');
113 document.write('</tr>');
114 document.write('<tr>');
115 for (var i=0; i<7; i++)
116     document.write('<td align=center style="background:#ABABAB;font:12px Arial">'+caldays[(i+ccc.startat)%7]+'</td>');
117 document.write('</tr>');
118 for(var kk=1;kk<=6;kk++) {
119         document.write('<tr>');
120         for(var tt=1;tt<=7;tt++) {
121                 num=7 * (kk-1) - (-tt);
122                 document.write('<td id="v' + num + '" style="width:18px;height:18px">&nbsp;</td>');
123         }
124         document.write('</tr>');
125 }
126 document.write('</table>');
127 }
128
129 document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);
130
131
132 // Calendar script
133 var now = new Date;
134 var sccm=now.getMonth();
135 var sccy=now.getFullYear();
136 var ccm;
137 var ccy;
138
139 function popcalendar(name)
140 {
141   var input = getObj(name);
142
143   if (!input) return;
144
145   lcs(input);
146 }
147
148 var updobj;
149 function lcs(ielem) {
150         gencalendar();
151         updobj=ielem;
152         updobj.select();
153         getObj('fc').style.left=Left(ielem);
154         getObj('fc').style.top=Top(ielem)+ielem.offsetHeight;
155         getObj('fc').style.display='';
156         
157         // First check date is valid
158         curdt=ielem.value;
159         curdtarr=curdt.split(ccc.delim);
160         isdt=true;
161         for(var k=0;k<curdtarr.length;k++) {
162                 if (isNaN(curdtarr[k]))
163                         isdt=false;
164         }
165         if (isdt&(curdtarr.length==3)) {
166                 ccm=curdtarr[1]-1;
167                 ccy=curdtarr[2];
168                 prepcalendar(curdtarr[0],curdtarr[1]-1,curdtarr[2]);
169         }
170         else
171         {
172                 ccm=now.getMonth();
173                 ccy=now.getFullYear();
174                 prepcalendar('',ccm,ccy);
175         }
176         
177 }
178
179 function evtTgt(e)
180 {
181         var el;
182         if(e.target)el=e.target;
183         else if(e.srcElement)el=e.srcElement;
184         if(el.nodeType==3)el=el.parentNode; // defeat Safari bug
185         return el;
186 }
187 function EvtObj(e){if(!e)e=window.event;return e;}
188 function cs_over(e) {
189         evtTgt(EvtObj(e)).style.background='#FFCC66';
190 }
191 function cs_out(e) {
192         evtTgt(EvtObj(e)).style.background='#C4D3EA';
193 }
194 function cs_click(e) {
195         updobj.value=calvalarr[evtTgt(EvtObj(e)).id.substring(1,evtTgt(EvtObj(e)).id.length)];
196         getObj('fc').style.display='none';
197 }
198 function cal_close(e) {
199         getObj('fc').style.display='none';
200         
201 }
202
203 var mnn=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
204 var mnl=new Array('31','29','31','30','31','30','31','31','30','31','30','31');
205 var calvalarr=new Array(42);
206
207 function f_cps(obj) {
208         obj.style.background='#C4D3EA';
209         obj.style.font='10px Arial';
210         obj.style.color='#333333';
211         obj.style.textAlign='center';
212         obj.style.textDecoration='none';
213         obj.style.border='1px solid #6487AE';
214         obj.style.cursor='pointer';
215 }
216
217 function f_cpps(obj) {
218         obj.style.background='#C4D3EA';
219         obj.style.font='10px Arial';
220         obj.style.color='#ABABAB';
221         obj.style.textAlign='center';
222         obj.style.textDecoration='line-through';
223         obj.style.border='1px solid #6487AE';
224         obj.style.cursor='default';
225 }
226
227 function f_hds(obj) {
228         obj.style.background='#FFF799';
229         obj.style.font='bold 10px Arial';
230         obj.style.color='#333333';
231         obj.style.textAlign='center';
232         obj.style.border='1px solid #6487AE';
233         obj.style.cursor='pointer';
234 }
235
236 // day selected
237 function prepcalendar(hd,cm,cy) {
238         now=new Date();
239         sd=now.getDate();
240         td=new Date();
241         td.setDate(1);
242         td.setFullYear(cy);
243         td.setMonth(cm);
244         cd=td.getDay() - ccc.startat;
245         if (cd<0) cd += 7;
246         getObj('mns').innerHTML=mn[cm]+ ' ' + cy;
247         marr=( (ccy % 4 == 0) || (ccy % 100 == 0) || (ccy % 400 == 0) )?mnl:mnn;
248         for(var d=1;d<=42;d++) {
249                 f_cps(getObj('v'+parseInt(d)));
250                 if ((d >= (cd -(-1))) && (d<=cd-(-marr[cm]))) {
251                         dip=ccc.past?false:((d-cd < sd)&&(cm==sccm)&&(cy==sccy));
252                         htd=((hd!='')&&(d-cd==hd));
253
254                         if (dip)
255                                 f_cpps(getObj('v'+parseInt(d)));
256                         else if (htd)
257                                 f_hds(getObj('v'+parseInt(d)));
258                         else
259                                 f_cps(getObj('v'+parseInt(d)));
260
261                         getObj('v'+parseInt(d)).onmouseover=(dip)?null:cs_over;
262                         getObj('v'+parseInt(d)).onmouseout=(dip)?null:cs_out;
263                         getObj('v'+parseInt(d)).onclick=(dip)?null:cs_click;
264                         
265                         getObj('v'+parseInt(d)).innerHTML=d-cd; 
266                         calvalarr[d]=''+(d-cd)+ccc.delim+(cm-(-1))+ccc.delim+cy;
267                 }
268                 else {
269                         getObj('v'+d).innerHTML='&nbsp;';
270                         getObj('v'+parseInt(d)).onmouseover=null;
271                         getObj('v'+parseInt(d)).onmouseout=null;
272                         getObj('v'+parseInt(d)).style.cursor='default';
273                         }
274         }
275 }
276
277 //getObj('fc'+cc).style.visibility='hidden';
278
279 function caddm() {
280         marr=( (ccy % 4 == 0) || (ccy % 100 == 0) || (ccy % 400 == 0) )?mnl:mnn;
281         
282         ccm+=1;
283         if (ccm>=12) {
284                 ccm=0;
285                 ccy++;
286         }
287         cdayf();
288         prepcalendar('',ccm,ccy);
289 }
290
291 function csubm() {
292         marr=( (ccy % 4 == 0) || (ccy % 100 == 0) || (ccy % 400 == 0) )?mnl:mnn;
293         
294         ccm-=1;
295         if (ccm<0) {
296                 ccm=11;
297                 ccy--;
298         }
299         cdayf();
300         prepcalendar('',ccm,ccy);
301 }
302
303 function cdayf() {
304 if (ccc.past)
305         return;
306
307 if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))
308         return;
309 else {
310         ccy=sccy;
311         ccm=sccm;
312         }
313 }