// This code can be reused as long as this copyright notice is not removed
// Copyright 1999 InsideDHTML.com, LLC.  All rights reserved.  
//      See www.siteexperts.com for more information. 

// Initialize arrays.
var months = new Array("január", "február", "március", "április", "május", 
    "június", "július", "augusztus", "szeptember", "október", "november", "december");
var daysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var days = new Array("H", "K",  "Sz", "Cs", "P", "Sz", "V");

function getDays(month, year) {
    // Test for leap year when February is selected.
    if (1 == month) {
        return ((0 == year % 4) && (0 != (year % 100))) || (0 == year % 400) ? 29 : 28;
    } else {
        return daysInMonth[month];
    }
}

function getToday() {
    // Generate today's date.
    this.now = new Date();
    this.year = this.now.getFullYear();
    this.month = this.now.getMonth();
    this.day = this.now.getDate();
}


function newCalendar(days,calendarID) {
    today = new getToday();
    var parseYear = parseInt(document.getElementById('year'+calendarID).value);
    var parseMonth = parseInt(document.getElementById('month'+calendarID).value);
    var newCal = new Date(parseYear, parseMonth, 1);
    var day = -1;
    var startDay = newCal.getDay() -1;

    document.getElementById('naptarfej'+calendarID).innerHTML = parseYear + ".&nbsp;" + months[parseMonth];
    // Atforditjuk a napot a magyar formara
    if (startDay==-1) startDay=6;
    var daily = 0;
    // Cache the calendar table's tBody section, dayList.
    var tableCal = document.getElementById('dayList'+calendarID);
    var intDaysInMonth = getDays(newCal.getMonth(), newCal.getFullYear());
    for (var intWeek = 0; intWeek < tableCal.rows.length; intWeek++) {
        if (daily>intDaysInMonth) {
            tableCal.rows[intWeek].style.display = "none";
        } else {
            tableCal.rows[intWeek].style.display = "";
        }
        for (var intDay = 0; intDay < tableCal.rows[intWeek].cells.length; intDay++) {
            var cell = tableCal.rows[intWeek].cells[intDay];
            // Start counting days.
            if ((intDay == startDay) && (0 == daily)) {
                daily = 1;
            }
            // Highlight the current day.
            var nowDate = new Date();
            if (nowDate.getFullYear() == parseInt(document.getElementById("year"+calendarID).value) &&
                nowDate.getMonth() == parseInt(document.getElementById("month"+calendarID).value)) {
                cell.className = (nowDate.getDate() == daily) ? "cal_active" : "cal_normal";
            } else{
                cell.className = "cal_normal";
            }

            // Output the day number into the cell.
            if ((daily > 0) && (daily <= intDaysInMonth)) {
                var monthS = parseMonth+1;
                if (monthS<10)
                    monthS = "0"+monthS+"";
                var dayS   = daily;
                if (dayS<10)
                    dayS = "0"+dayS+"";
                var calendarIDs = '';
                if (calendarID!='') {
                    calendarIDs = '&calendarID='+calendarID;
                }
                cell.innerHTML = "<a href='"+eventPage+"?cmd=0&nap="+parseYear+"-"+monthS+"-"+dayS+calendarIDs+"'>"+daily+"</a>";

                if (days!=null)
                    boldDays(cell, days, daily, calendarID);
                daily++;
            } else {
                cell.innerHTML = "&nbsp;";
                cell.className="cal_empty";
            }//else
        }//for
    }//for
}//function

function boldDays(cell, days, daily, calendarID){
    for (var i=0; i<days.length; i++){
        if (parseInt(days[i])==daily){
            cell.className = "bold_day";
            var date = new Date();
            if (date.getDate() == daily && parseInt(document.getElementById("month"+calendarID).value)==date.getMonth() &&
                parseInt(document.getElementById("year"+calendarID).value) == date.getFullYear()){
                cell.className = "bold_active_day";
            } //if
        }//if
    }//for
}//function

function getDate(event,calendarID) {
    var sDate;
    // This code executes when the user clicks on a day
    // in the calendar.
    if ("td" == event.tagName.toLowerCase()) {
        // Test whether day is valid.
        if ("" != event.innerHTML) {
            sDate = document.getElementById('year'+calendarID).value + "-" +
            document.getElementById('month'+calendarID).value + "-" +
            event.innerHTML;
            window.returnValue = sDate;
        }
    }
}

function monthMinusz(calendarID) {
    var parseMonth = parseInt(document.getElementById('month'+calendarID).value);
    if (parseMonth==0) {
        var parseYear = parseInt(document.getElementById('year'+calendarID).value);
        document.getElementById('year'+calendarID).value=parseYear-1;
        document.getElementById('month'+calendarID).value=11;
    } else {
        document.getElementById('month'+calendarID).value = parseMonth-1;
    }
     
    document.getElementById('day'+calendarID).value=-1;

    var calendarIDs = '';
    if (calendarID!='') {
        calendarIDs = '&calendarID='+calendarID;
    }

    advAJAX.get({
        url: eventServlet+"?cmd=7&year="+document.getElementById('year'+calendarID).value+"&month="+document.getElementById('month'+calendarID).value+calendarIDs,
        onSuccess : function(obj) {
            var days = new Array(31);
            days = obj.responseText.split(",");
            newCalendar(days,calendarID);
        }
    });

    advAJAX.get({
        url: eventServlet+"?cmd=9&year="+document.getElementById('year'+calendarID).value+"&month="+document.getElementById('month'+calendarID).value+calendarIDs,
        onSuccess : function(obj) {
            document.getElementById("eventMenuList").innerHTML = obj.responseText;
        }
    });

}

function monthPlusz(calendarID) {
    var parseMonth = parseInt(document.getElementById('month'+calendarID).value);
    if (parseMonth==11) {
        var parseYear = parseInt(document.getElementById('year'+calendarID).value);
        document.getElementById('year'+calendarID).value=parseYear+1;
        document.getElementById('month'+calendarID).value=0;
    } else {
        document.getElementById('month'+calendarID).value = parseMonth+1;
    }
    document.getElementById('day'+calendarID).value=-1;

    var calendarIDs = '';
    if (calendarID!='') {
        calendarIDs = '&calendarID='+calendarID;
    }

    advAJAX.get({
        url: eventServlet+"?cmd=7&year="+document.getElementById('year'+calendarID).value+"&month="+document.getElementById('month'+calendarID).value+calendarIDs,
        onSuccess : function(obj) {
            var days = new Array(31);
            days = obj.responseText.split(",");
            newCalendar(days,calendarID);
        }
    });

    advAJAX.get({
        url: eventServlet+"?cmd=9&year="+document.getElementById('year'+calendarID).value+"&month="+document.getElementById('month'+calendarID).value+calendarIDs,
        onSuccess : function(obj) {
            document.getElementById("eventMenuList").innerHTML = obj.responseText;
        }
    });
}

// Start with a calendar for today.
var today = new getToday();

