var oCalendar, oCalendarMonth, oCalendarYear, oCalendarDays;
var aMonthLen = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var nMonth, nYear, dateStart, dateStartTemp;
var oDateInput;
var nNextLineNumber, pHighlightedElement, bResults;


function InitializeCalendar()
{
    oCalendar = GetElement("calendar");
    oCalendar.onclick = CalendarCatchClick;
    oCalendarMonth = GetElement("calendarMonth");
    oCalendarYear = GetElement("calendarYear");
    oCalendarDays = GetElement("calendarDays");
}

function CalendarCatchClick(e)
{
    var oEvent = e;
    if (oEvent == null)
        oEvent = window.event;
    oEvent.cancelBubble = true;
}

function ShowCalendar(e)
{
    var oEvent = e;
    if (oEvent == null)
        oEvent = window.event;
    oEvent.cancelBubble = true;
    oDateInput = GetElement(this.id.replace("Calendar", ""));
    if (oDateInput == null)
        return;
    oDateInput.focus();
    
    if (CheckDate(oDateInput.value))
    {
        var aSplit = oDateInput.value.split(".");
        dateStart = new Date(aSplit[2], aSplit[1] - 1, 1);
    }
    else
    {
//      dateStart - new Date()     ---------------uprava funkcnost pj 21/2/2011
        dateStartTemp = new Date();
	dateStart = new Date(dateStartTemp.getFullYear(), dateStartTemp.getMonth(), 1);
    }

    DrawCalendar();

    oCalendar.style.left = (oEvent.clientX + document.body.scrollLeft).toString() + "px";
    oCalendar.style.top = (oEvent.clientY + document.body.scrollTop).toString() + "px";
    oCalendar.style.display = "";
// document.write(oCalendar.style.left); 

    return false;
}


function HideCalendar()
{
    if (oCalendar != null)
    {
        // alert("hiding...");
        oCalendar.style.display = "none";
    }
}


function DrawCalendar()
{
    var nDayOfWeek, nMonthLen;
    nYear = dateStart.getFullYear();
    nMonth = dateStart.getMonth();
    nDayOfWeek = dateStart.getDay();
    if ((nDayOfWeek == 0) && (strFirstDayOfWeek == "monday"))
        nDayOfWeek = 7;
    nMonthLen = aMonthLen[nMonth];
    if ((nMonth == 1) && (nYear % 4 == 0) && !(nYear % 100 == 0))
        nMonthLen++;

    oCalendarMonth.innerHTML = aCalendarMonths[nMonth];
    oCalendarYear.innerHTML = nYear;

    var i, j, oDay, k = -nDayOfWeek + (strFirstDayOfWeek == "monday" ? 1 : 0);
    for (j = 0; j < 6; j++)
    {
        for (i = 0; i < 7; i++)
        {
            k++;
            if ((k < 1) || (k > nMonthLen))
            {
                oDay = oCalendarDays.childNodes[j].childNodes[i];
                oDay.innerHTML = "&nbsp;";
                oDay.onmouseover = null;
                oDay.onmouseout = null;
                oDay.onclick = null;
            }
            else
            {
                oDay = oCalendarDays.childNodes[j].childNodes[i];
                oDay.innerHTML = k;
                oDay.onmouseover = CalendarMouseOver;
                oDay.onmouseout = CalendarMouseOut;
                oDay.onclick = CalendarClick;
            }
        }
    }
}


function CalendarMouseOver()
{
    this.className = "active";
}


function CalendarMouseOut()
{
    this.className = "";
}


function CalendarClick()
{
    if (oDateInput != null)
    {
        oDateInput.value = this.innerHTML + "." + (nMonth + 1) + "." + nYear;
        oDateInput.focus();
    }
    this.className = "";
    HideCalendar();
}


function CalendarPrevMonth()
{
    nMonth--;
    if (nMonth < 0)
    {
        nMonth = 11;
        if (nYear > 1901)
            nYear--;
    }
    dateStart = new Date(nYear, nMonth, 1);
    DrawCalendar();
}


function CalendarNextMonth()
{
    nMonth++;
    if (nMonth > 11)
    {
        nMonth = 0;
        if (nYear < 2100)
            nYear++;
    }
    dateStart = new Date(nYear, nMonth, 1);
    DrawCalendar();
}


function CalendarPrevYear()
{
    if (nYear > 1901)
        nYear--;
    dateStart = new Date(nYear, nMonth, 1);
    DrawCalendar();
}


function CalendarNextYear()
{
    if (nYear < 2100)
        nYear++;
    dateStart = new Date(nYear, nMonth, 1);
    DrawCalendar();
}

function CheckDate(strInput)
{
    if (strInput.search(/^[1-9][0-9]?\.[1-9][0-9]?\.[1-9][0-9]{3}$/) == -1)
        return false;
    var aSplit = strInput.split(".");
    var dateInput = new Date(aSplit[2], aSplit[1] - 1, aSplit[0]);
    if (isNaN(dateInput))
        return false;
    if ((dateInput.getDate() != aSplit[0]) || (dateInput.getMonth() != aSplit[1] - 1) || (dateInput.getFullYear() != aSplit[2]))
        return false;
    return true;
}

