    if (window.addEventListener) {
	window.addEventListener("load",initCalendar,false);
} else if (window.attachEvent) {
	window.attachEvent("onload",initCalendar);
} else {
	window.onload = function() {initCalendar();}
}
function initCalendar() { myCalendar = new Calendar(); }
    
   
function Calendar() {
    this.dni=new Array('Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So', 'N');
    this.miesiace=new Array('Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec','Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień');
    
    this.calendardiv=null;
    this.field=null;
    this.year=null;
    this.month=null;
    this.date=null;
    this.initialize();
}
Calendar.prototype.initialize = function() {
    //dopisanie onfocus do elementow
    var els = document.getElementsByTagName('input');
    //var exp= new RegExp("^(.* )?calendar( .*)?$", "g");
    var exp=new RegExp('\\bcalendar\\b');

    for(i=0; i<els.length; i++) {
        if (exp.test(els[i].className)){
            
            els[i].onfocus=function() { myCalendar.start(this); };
            els[i].readOnly=true;
        }
    }
    //dolozenie warstwy z kalendarzem
    this.calendardiv=document.createElement('div');
    this.calendardiv.setAttribute('id','calendarDiv');
    document.body.appendChild(this.calendardiv);
}
Calendar.prototype.start = function(field) {
    this.field=field;
    this.date=new Date();
    if(this.field.value!='') {
        this.date.setFullYear(this.field.value.substr(0,4));
        this.date.setMonth(parseInt(this.field.value.substr(5,2),10)-1);
        this.date.setDate(parseInt(this.field.value.substr(8,2),10));
    }
    this.month=this.date.getMonth()+1;
    this.year=this.date.getFullYear();
    //ustalamy tuz pod polem ;)
    e=this.field;
    var left=0;
    var top=0;
    while (e.offsetParent){
    	left += e.offsetLeft;
    	top  += e.offsetTop;
    	e     = e.offsetParent;
    }
    left += e.offsetLeft;
    top  += e.offsetTop;
    this.calendardiv.style.left=left+'px';
    this.calendardiv.style.top=(top+this.field.offsetHeight)+'px';
    this.drawCalendar();
    
}
Calendar.prototype.returnDate = function(day) {
    if(this.month<10) { month='0'+this.month; } else { month=this.month; }
    if(day<10) { day='0'+day; }
    this.field.value=this.year+'-'+month+'-'+day;
    this.calendardiv.style.display='none';
}
Calendar.prototype.nextMonth = function() {
    if(this.month==12) { this.month=1; this.year++; }
    else { this.month++; }
    this.drawCalendar();
}
Calendar.prototype.prevMonth = function() {
    if(this.month==1) { this.month=12; this.year--; }
    else { this.month--; }
    this.drawCalendar();
}
Calendar.prototype.drawCalendar = function() {
    str='<a href="#" onClick="myCalendar.prevMonth(); return false;" class="prev">&lsaquo;</a>';
    str+='<span class="current">'+this.miesiace[this.month-1]+' '+this.year+'</span>';
    str+='<a href="#" onClick="myCalendar.nextMonth(); return false;" class="next">&rsaquo;</a>';
    str+='<table cellspacing="0" cellpadding="0">';
    
    str+='<tr>';
    for(i=0; i<this.dni.length; i++) {
        str+='<th>'+this.dni[i]+'</th>';
    }
    str+'</tr>';
    firstInMonth=new Date(this.year, this.month-1, 1).getDay()-1;
    if(firstInMonth<0) { firstInMonth=6; } //0 to poniedzialek, 6-niedziela
    lastInMonth=new Date(this.year, this.month,1);
    lastInMonth.setDate(lastInMonth.getDate()-1);
    lastInMonth=lastInMonth.getDate();
    str += '<tr>';
        
    dayInWeek = 0;
    for (i = 0; i < firstInMonth; i++) {
        str += "<td>&nbsp;</td>";
        dayInWeek++;
    }
        
    for(i=1; i<= lastInMonth; i++) {
        if(dayInWeek==7) {
            str += "</tr><tr>";
            dayInWeek = 0;
        }
        sel='';
        
        if(this.year==this.date.getFullYear()&&(this.month==this.date.getMonth()+1)&&i==this.date.getDate()) {
            sel=' class="sel"';
        }
        str+='<td><a href="#"'+sel+' onClick="myCalendar.returnDate('+i+'); return false;">'+i+'</a></td>';
        dayInWeek++;
    }
    for (i = dayInWeek; i < 7; i++) {
        str += "<td>&nbsp;</td>";
    }
    str += "</tr></table>";
    this.calendardiv.innerHTML=str;
    this.calendardiv.style.display='block';
}
