function KalendarzJS(E, T) {
	var i, j, tBody, Row, od_data, do_data, d, dzis;
	function nowyElement(t, p, h, w) {
		p.appendChild (t = document.createElement(t))
		if (h) t.innerHTML=h;
		if (w) t.style.width='25px';
		return t;
	}
	function dodajMiesRok(x) { //dodaje lub odejmuje miesiac / rok
		with (kalendarz) {
			B = 0;
			x==0?R--:x==1?R++:x==2?(!M?(M=11,R--):M--):(M==11?(M=0,R++):M++)
		}
	}
	
	with (kalendarz) {
		od_data = Date.UTC(R, M, 1);
		do_data = Date.UTC(R, M+1, 1);
	}
	
	E = document.getElementById(E);
	while (E.childNodes.length) E.removeChild(E.firstChild);
	tBody = nowyElement('tbody', nowyElement('table', E));
	tBody.onmouseup = function() {T.focus()}
	tBody.onmousemove = function() {
	if (window.getSelection) window.getSelection().removeAllRanges()
	}
	Row = nowyElement('tr',tBody);
	nowyElement('td',Row,'&#171;',1).onmousedown = function() {dodajMiesRok(0)}
	nowyElement('th',Row,kalendarz.R);
	nowyElement('td',Row,'&#187;',1).onmousedown = function() {dodajMiesRok(1)}
	Row = nowyElement('tr',tBody);
	nowyElement('td',Row,'&#171;',1).onmousedown = function() {dodajMiesRok(2)}
	nowyElement('th',Row,kalendarz.month[kalendarz.M]);
	nowyElement('td',Row,'&#187;',1).onmousedown = function() {dodajMiesRok(3)}
	
	tBody = nowyElement('tbody', nowyElement('table', E));
	Row = nowyElement('tr', tBody);
	for(i=0; i<7; i++)
		with (nowyElement('th', Row, kalendarz.day[i]));
	if (i==6) style.backgroundColor='#ff0000'
	
	for (i=od_data; i<do_data; i+=86400000) {
		with (new Date(i)) {
			d = getUTCDate();
			dzis = getUTCDay();
		}
		if (dzis==1||i==od_data) Row = nowyElement('tr', tBody);
		if (dzis!=1&&d==1) for (j=1; j<(dzis?dzis:7); j++) nowyElement('td', Row);
		dzis = nowyElement('td', Row, d);
		dzis.onmousedown = function(x) {
			x=+this.innerHTML;
				with (kalendarz) {
				y=x;
				T.value=R+'-'+((x=M+1)<10?'0'+x:x)+'-'+(y<10?'0'+y:y);
				}
		}
		with (kalendarz)
			if (d==D.getDate()&&M==D.getMonth()&&R==D.getFullYear())
				dzis.style.backgroundColor='#ffffff';
	}
	
	T.onblur = function() {
		if (kalendarz.B) E.style.display='none';
	}
	kalendarz.B=1;
	E.style.display='block';
}

kalendarz = {
	day:['Pn','Wt','Sr','Cz','Pt','So','Nd'],
	month:['Styczen','Luty','Marzec','Kwiecien','Maj','Czerwiec', 'Lipiec','Sierpien','Wrzesien','Pazdziernik','Listopad','Grudzien'],
	D:new Date(),
	M:new Date().getMonth(),
	R:new Date().getFullYear()
}




