IaiCld={
        day:['pn','wt','śr','cz','pt','so','nd'],
        month:['styczen','luty','marzec','kwiecien','maj','czerwiec', 'lipiec','sierpien','wrzesien','pa1dziernik','listopad','grudzien'],
        D:new Date(),
        M:new Date().getMonth(),
        R:new Date().getFullYear()
    }

function Calendar(E, T) {
var i, j, tBody, Row, od, Do, d, dt, z = 0, zeroAllowed = false;

function cEl(t, p, h, w) {
    p.appendChild(t = document.createElement(t));
    if (h) {
        t.innerHTML = h;
    }
    if (w) {
        t.style.width = "27px";
        t.style.backgroundColor = "#ddd";
    }
    return t;
}


function cB(x) {
    with (IaiCld) {
        B = 0;
        x == 0 ? R-- : x == 1 ? R++ : x == 2 ? !M ? (M = 11, R--) : M-- : M == 11 ? (M = 0, R++) : M++;
        od = Date.UTC(R, M, 1);
        Do = Date.UTC(R, M + 1, 1);
    }
}


function draw(E, T) {
    while (E.childNodes.length) {
        E.removeChild(E.firstChild);
    }
    table = cEl("table", E);
    tBody = cEl("tbody", table);
    tBody.onmouseup = function () {draw(E, T);};
    tBody.onmousemove = function () {if (window.getSelection) {window.getSelection().removeAllRanges();}if (document.selection) {document.selection.empty();}};
    Row = cEl("tr", tBody);
    cEl("td", Row, "&laquo;", 1).onmousedown = function () {cB(0);};
    cEl("th", Row, IaiCld.R).style.backgroundColor = "#fff";
    cEl("td", Row, "&raquo;", 1).onmousedown = function () {cB(1);};
    Row = cEl("tr", tBody);
    cEl("td", Row, "&laquo;", 1).onmousedown = function () {cB(2);};
    cEl("th", Row, IaiCld.month[IaiCld.M]).style.backgroundColor = "#fff";
    cEl("td", Row, "&raquo;", 1).onmousedown = function () {cB(3);};
    tBody = cEl("tbody", cEl("table", E));
    Row = cEl("tr", tBody);
    for (i = 0; i < 7; i++) {
        with (cEl("th", Row, IaiCld.day[i])) {
            style.backgroundColor = "#ddd";
            if (i == 6) {
                style.color = "#c40";
            }
        }
    }
    for (i = od; i < Do; i += 86400000) {
        with (new Date(i)) {
            d = getUTCDate();
            dt = getUTCDay();
        }
        if (dt == 1 || i == od) {
            Row = cEl("tr", tBody);
        }
        if (dt != 1 && d == 1) {
            for (j = 1; j < (dt ? dt : 7); j++) {
                cEl("th", Row);
            }
        }
        dt = cEl("td", Row, d);
        dt.onmousedown = function (x) {x = + this.innerHTML;with (IaiCld) {a = x;T.value = R + "-" + ((x = M + 1) < 10 ? "0" + x : x) + "-" + (a < 10 ? "0" + a : a);if (T.onchange && typeof T.onchange == "function") {T.onchange.apply(T);}E.style.display = "none";}};
        with (IaiCld) {
            if (d == D.getDate() &&
                M == D.getMonth() && R == D.getFullYear()) {
                dt.style.backgroundColor = "#777";
                dt.style.color = "white";
            }
        }
    }
    if (zeroAllowed) {
        tBody = cEl("tbody", cEl("table", E));
        Row = cEl("tr", tBody);
        cEl("td", Row, "wyzeruj dat\u0119").onmousedown = function () {T.value = "0000-00-00";T.onchange.apply(T);E.style.display = "none";};
    }
    IaiCld.B = 1;
    T.onblur = function () {if (IaiCld.B && E && E.parentNode && E.parentNode.removeChild) {E.parentNode.removeChild(E);}};
    var valueT = 0, valueL = 0, cont = T;
    do {
        valueT += cont.offsetTop || 0;
        valueL += cont.offsetLeft || 0;
        cont = cont.offsetParent;
    } while (cont);
    valueL += T.offsetWidth + 2;
    var mh = !!(window.attachEvent && !window.opera) ? 172 : 151;
    if (valueT + mh > document.body.offsetHeight) {
        valueT = document.body.offsetHeight - mh;
    }
    E.style.top = valueT + "px";
    E.style.left = valueL + "px";
    T.focus();
    E.style.display = "block";
}

if (arguments.length >= 3 && arguments[2]) {
    zeroAllowed = true;
}
with (IaiCld) {
    rexp = /^((20[0-9][0-9])-(([1-9])|(0[1-9])|(1[0-2]))-(([1-9])|(0[1-9])|([12][0-9])|(3[01])))$/;
    if (rexp.test(T.value)) {
        dateParts = T.value.split("-");
        R = + dateParts[0];
        M = -1 + Number(dateParts[1]);
        D.setFullYear(R, M, dateParts[2]);
    } else {
        D = new Date;
        M = D.getMonth();
        R = D.getFullYear();
    }
    od = Date.UTC(R, M, 1);
    Do = Date.UTC(R, M + 1, 1);
}
if(!document.getElementById("clndr"+T.id)){
    E = cEl("div", document.body);
    E.className = "clndr";
    E.id = "clndr"+T.id;
    draw(E, T);
}
}

function validateDate(nazwa) {
if (arguments.length >= 2 && arguments[1]) {
    zero = true;
    rexp = /^([-0]+)|((20[0-9][0-9])-(([1-9])|(0[1-9])|(1[0-2]))-(([1-9])|(0[1-9])|([12][0-9])|(3[01])))$/;
} else {
    zero = false;
    rexp = /^((20[0-9][0-9])-(([1-9])|(0[1-9])|(1[0-2]))-(([1-9])|(0[1-9])|([12][0-9])|(3[01])))$/;
}
if (document.getElementById(nazwa).value == "") {
    window.alert("Nale\u017Cy uzupe\u0142ni\u0107 pole (pola) dat.");
    return false;
} else if (!rexp.test(document.getElementById(nazwa).value)) {
    window.alert("Format daty nie zgadza si\u0119 z szablonem Rok-Miesi\u0105c-Dzie\u0144 np. 2008-12-31");
    return false;
}
return true;
}
