1 /* --- Swazz Javascript Calendar ---
2 /* --- v 1.0 3rd November 2006
4 http://calendar.swazz.org/
7 The javascript calendar available here is a free, lightweight widget
8 which shows a calendar for use in picking dates on form fields.
10 Code to activate the calendar
12 <script src="calendar.js"></script>
14 <input type="text" value="dd/mm/yy" onfocus="lcs(this)" onclick="event.cancelBubble=true;lcs(this)">
16 <input id="date" type="text" value="dd/mm/yy">
17 <img src="calendar.gif" onclick="event.cancelBubble=true;popcalendar('date');">
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)
23 Call calendar({opt1: val1, opt2: val2}) for configuration.
26 var ccc = {past: false,
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');
35 function calendar(info)
43 function getObj(objID)
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];}
50 function checkClick(e) {
52 CSE=evt.target?evt.target:evt.srcElement;
54 if (!isChild(CSE,getObj('fc')))
55 getObj('fc').style.display='none';
58 function isChild(s,d) {
72 while (obj.offsetParent)
74 curleft += obj.offsetLeft
75 obj = obj.offsetParent;
88 while (obj.offsetParent)
90 curtop += obj.offsetTop
91 obj = obj.offsetParent;
99 function gencalendar()
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>');
107 document.write('<td colspan=4 id="mns" align="center" style="font:bold 13px Arial">');
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>');
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"> </td>');
124 document.write('</tr>');
126 document.write('</table>');
129 document.all?document.attachEvent('onclick',checkClick):document.addEventListener('click',checkClick,false);
134 var sccm=now.getMonth();
135 var sccy=now.getFullYear();
139 function popcalendar(name)
141 var input = getObj(name);
149 function lcs(ielem) {
153 getObj('fc').style.left=Left(ielem);
154 getObj('fc').style.top=Top(ielem)+ielem.offsetHeight;
155 getObj('fc').style.display='';
157 // First check date is valid
159 curdtarr=curdt.split(ccc.delim);
161 for(var k=0;k<curdtarr.length;k++) {
162 if (isNaN(curdtarr[k]))
165 if (isdt&(curdtarr.length==3)) {
168 prepcalendar(curdtarr[0],curdtarr[1]-1,curdtarr[2]);
173 ccy=now.getFullYear();
174 prepcalendar('',ccm,ccy);
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
187 function EvtObj(e){if(!e)e=window.event;return e;}
188 function cs_over(e) {
189 evtTgt(EvtObj(e)).style.background='#FFCC66';
192 evtTgt(EvtObj(e)).style.background='#C4D3EA';
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';
198 function cal_close(e) {
199 getObj('fc').style.display='none';
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);
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';
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';
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';
237 function prepcalendar(hd,cm,cy) {
244 cd=td.getDay() - ccc.startat;
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));
255 f_cpps(getObj('v'+parseInt(d)));
257 f_hds(getObj('v'+parseInt(d)));
259 f_cps(getObj('v'+parseInt(d)));
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;
265 getObj('v'+parseInt(d)).innerHTML=d-cd;
266 calvalarr[d]=''+(d-cd)+ccc.delim+(cm-(-1))+ccc.delim+cy;
269 getObj('v'+d).innerHTML=' ';
270 getObj('v'+parseInt(d)).onmouseover=null;
271 getObj('v'+parseInt(d)).onmouseout=null;
272 getObj('v'+parseInt(d)).style.cursor='default';
277 //getObj('fc'+cc).style.visibility='hidden';
280 marr=( (ccy % 4 == 0) || (ccy % 100 == 0) || (ccy % 400 == 0) )?mnl:mnn;
288 prepcalendar('',ccm,ccy);
292 marr=( (ccy % 4 == 0) || (ccy % 100 == 0) || (ccy % 400 == 0) )?mnl:mnn;
300 prepcalendar('',ccm,ccy);
307 if ((ccy>sccy)|((ccy==sccy)&&(ccm>=sccm)))