//Written by Bogdan Blaszczak/BlaTek
//blatek@25.pl
//original version @ http://js.webhelp.pl/js/prz/blatek/datepicker.html
//Modified by PablO @ 2005
//full desc available @ http://piskorz.info/narzedzia/date-picker
//last update 15-11-2006
function Calendar(T,E,i,j,tE,tBody,Row,f,t,d,wd){if(!document.getElementById||!document.body.appendChild)return
T=this
function cEl(t,p,h,w){p.appendChild(t=document.createElement(t))
if(h)t.innerHTML=h
if(w)t.colSpan=5
return t}
function cB(x){with(T.Cal){B=0;x==0?Y--:x==1?Y++:x==2?(!M?(M=11,Y--):M--):(M==11?(M=0,Y++):M++)}}
if(!T.Cal){E=document.createElement('ins')
E.className='clndr'
T.parentNode.insertBefore(E,T.nextSibling)
T.Cal={E:E,B:1,cD:new Date().getUTCDate(),cM:new Date().getMonth(),cY:new Date().getFullYear(),sD:0,sM:-1,sY:0,M:-1,Y:0}
if((i=T.value.match(/\d{2,4}/g))&&i.length==3){with(T.Cal){sD=+i[0];sM=M=i[1]-1;sY=Y=i[2]}}
}
E=T.Cal.E
with(T.Cal){if(B){M=M+1?M:sM+1?sM:cM;Y=Y||sY||cY}f=Date.UTC(Y,M,1);t=Date.UTC(Y,M+1,1)}
while(E.childNodes[0])E.removeChild(E.firstChild)
tE=cEl('table',E)
tBody=cEl('thead',tE)
tBody.onmouseup=function(){T.focus()}
tBody.onmousemove=function(){if(window.getSelection)window.getSelection().removeAllRanges()}
Row=cEl('tr',tBody)
cEl('td',Row,'&#171;').onmousedown=function(){cB(0)}
cEl('th',Row,T.Cal.Y,1)
cEl('td',Row,'&#187;').onmousedown=function(){cB(1)}
Row=cEl('tr',tBody)
cEl('td',Row,'&#171;').onmousedown=function(){cB(2)}
cEl('th',Row,cld_BlaTek.month[T.Cal.M],1)
cEl('td',Row,'&#187;').onmousedown=function(){cB(3)}
tBody=cEl('tbody',tE)
Row=cEl('tr',tBody)
for(i=0;i<7;i++){cEl('th',Row,cld_BlaTek.day[i]).className='day'+(i+1)%7}
for(i=f;i<t;i+=86400000){with(new Date(i)){d=getUTCDate();wd=getUTCDay()}
if(wd==1||i==f)Row=cEl('tr',tBody)
if(wd!=1&&d==1){for(j=1;j<(wd?wd:7);j++)cEl('td',Row).className='empty day'+j}
(tE=cEl('td',Row,d)).className='day'+wd
tE.onmousedown=function(x){x=+this.innerHTML
with(T.Cal){sD=x;sM=M;sY=Y;T.value=(x<10?'0'+x:x)+'-'+((x=M+1)<10?'0'+x:x)+'-'+Y}}
with(T.Cal){if(d==cD&&M==cM&&Y==cY)tE.className+=' today';if(d==sD&&M==sM&&Y==sY)tE.className+=' selected'}}
for(i=wd||7;i<7;){cEl('td',Row).className='empty day'+(++i%7)}
T.onblur=function(){if(T.Cal.B)E.className='clndr hide';T.form.className=T.form.className.replace(/\bhideSel\b/,'')}
T.Cal.B=1
E.className='clndr'
T.form.className+=' hideSel'}


//global object (You can translate month&day names)
cld_BlaTek={
 day:['pn','wt','śr','cz','pt','so','n'],
 month:['styczeń','luty','marzec','kwiecień','maj','czerwiec',
 'lipiec','sierpień','wrzesień','październik','listopad','grudzień']
}

//this function change days displayed in the input field
//x - number of days to add to input field
function changeDay(x,T,d){T=gId(T)
if((i=T.value.match(/\d{1,4}/g))&&i.length==3){x=new Date(Date.UTC(1*i[2],i[1]-1,1*i[0],0,0,0)+(0x5265C00*x))
T.value=((d=x.getUTCDate())<10?'0'+d:d)+'-'+((d=x.getMonth()+1)<10?'0'+d:d)+'-'+x.getFullYear()}} 

//this function add onfocus event listener to input fields
function addCalendar(a,i){aEL(window,'load',function(){for(j in a){if(i=document.getElementById(a[j]))aEL(i,'focus',Calendar)}})}

function aEL(o,t,f){var a=[].slice.call(arguments,3)
o[t+f]=function(e){return !1===f.apply(o,[e||window.event].concat(a))?e.preventDefault?e.preventDefault():!1:1}
return(o.x=o.addEventListener)?o.x(t,o[t+f],!1):(o.x=o.attachEvent)?o.x('on'+t,o[t+f]):!1}
function gId(i){return document.getElementById(i)}

//below line activates calendar on input fields with id: to, from, today
addCalendar(['to','from','today'])
