function calendar(calname)
{
    this.calname = calname;
    this.month_name = new Array ( "January ", "February ", "March ", "April ", "May ",
                                  "June ", "July ", "August ", "September ", "October ",
                                  "November ", "December ");
    this.day_name = new Array ( "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
    this.right_now = new Date();
    this.linefontsize = 10;
    this.hdrfontsize = 10;
    this.titlefontsize = 14;
    this.showYear = true;
    this.showNav = false;
    this.showDescription = false;

    this.setYMD = setCalendarYMD;
    this.setYear = setCalendarYear;
    this.setMonth = setCalendarMonth;
    this.setDay = setCalendarDay;
    this.getDays = getCalendarDays;
    this.getFullYear = getCalendarFullYear;
    this.clearDisplay = clearCalendarDisplay;
    this.clear = clearCalendarDisplayAndData;
    this.clearData = clearCalendarData;
    this.reload = reloadCalendar;
    this.loadDay = loadCalendarDay;
    this.addCalendar = addCalendarToContainer;
    this.resize = sizeCalendar;
    this.addLine = addCalendarLine;
    this.setLineFont = setCalendarLineFont;
    this.setHdrFont  = setCalendarHdrFont;
    this.setTitleFont = setCalendarTitleFont;
    this.setShowYear = setCalendarShowYear;
    this.setShowNav  = setCalendarShowNav;
    this.setShowDescription = setCalendarShowDescription;
    this.displayYear = displayCalendarYear;
    this.loadMonth = loadCalendarMonth;
    this.displayMonth = displayCalendarMonth;
    this.setDisplayMonthCallback = setDisplayMonthCallbackCalendar;
    this.displayDay = displayCalendarDay;
    this.setYearContainer = setCalendarYearContainer;
    this.setMonthContainer = setCalendarMonthContainer;
    this.setDayContainer = setCalendarDayContainer;
    this.incYear = incCalendarYear;
    this.decYear = decCalendarYear;

    this.addEvent = addCalendarEvent;
    this.removeEvent = removeCalendarEvent;
    this.updateEvent = updateCalendarEvent;
    this.showEvent = showCalendarEvent;


    this.month = this.right_now.getMonth();
    this.setYear(this.right_now.getYear());

    this.setMonth(this.right_now.getMonth() + 1);
    this.setDay(this.right_now.getDate());

    // create the table, rows and cells
    this.titlediv = document.createElement("div");
    this.titlediv.align = 'center';
    this.titlediv.innerHTML = '<h2>' + this.month_name[this.month] + '  ' + this.year + '</h2>';
    this.titlediv.className = "titles";

    this.celldata = new Array();
    this.celldatatitle = new Array();
    
    for (var i = 0; i < 6; i++) {  
        for (var c = 0; c < 7; c++) {
            this.celldata[(i * 7) + c] = new Array();
            this.celldatatitle[(i * 7) + c] = new Array();
        }
    }

    this.reload();
}
function getCalendarFullYear()
{
    if (this.year < 1000) {
        return this.year + 1900;
    }
    return this.year;
}
function setCalendarShowDescription(obj)
{
    this.showDescription = true;
    this.descriptionObj = obj;
}
function setCalendarShowYear(val)
{
    this.showYear = val;
}
function setCalendarShowNav(val)
{
    this.showNav = val;
}
function setCalendarLineFont(size)
{
    this.linefontsize = size;
}
function setCalendarHdrFont(size)
{
    this.hdrfontsize = size;
}
function setCalendarTitleFont(size)
{
    this.titlefontsize = size;
}
function addCalendarLine(day, text, description)
{
    if (day > this.endofmonth) {
        return;
    }
    this.celldata[day].push(text);
    this.celldatatitle[day].push(description);
//    this.loadDay(day);
}
function loadCalendarDay(day)
{
    // day is 1 based
//    var i = this.firstDay + day - 1;
//    var txt;
//    if (this.DayPage.length > 0) {
//        txt = "<a onclick='this.displayDay(0, " + day + ")'>"
//            + day + "<a/>";
//    } else {
        txt = "<b>" + day + "</b>";
//    }
//    txt += "<div>";
//    for (var r = 0; r < this.celldata[i].length; r++) {
//        txt += "<div title='" + this.celldatatitle[i][r] + "' class='daytext" + (r%2) 
//            + "'>" + this.celldata[i][r] + "</div>";
//    }
//    txt += "</div>";
//    this.cells[i].innerHTML = txt;
//    this.cells[i].style.height = this.cells[i].style.width;
}

function sizeCalendar(ht, wd)
{
//    this.titlediv.width = wd;
//    this.table.width = wd;
//    ht -= 100;
//    var rh = ht / 6;
//    var cw = wd / 7;
//    for (var i = 0; i < this.cells.length; i++) {
//        if (i < this.cells.length - 7) {
//            this.cells[i].height = rh;
//        }
//        this.cells[i].width = cw;
//    }
}
function addCalendarToContainer(obj)
{
    obj.appendChild(this.titlediv);
    obj.appendChild(this.table);
}
function reloadCalendar()
{
    this.clearDisplay();
    for (var i = 1; 
         i <= this.endofmonth; 
         i++) {
        this.loadDay(i);
    } 

    var txt = "";
//    if (this.showNav && this.MonthPage.length > 0) {
//        var mon = this.month - 1;
//        var yr = this.year;
//        if (mon < 0) {
//            mon += 12;
//            yr -= 1;
//        } 
//        txt += "<a onclick='document.location=\"" + this.MonthPage + "?year=" + yr + "&month=" + mon + "\"'><<-- </a>";
//    }
//    if (this.MonthPage.length > 0) {
//        txt += "<a onclick='document.location=\"" + this.MonthPage + "?year=" + this.year + "&month=" + this.month + "\"'>";
//    }
    txt += this.month_name[this.month];
//    if (this.MonthPage.length > 0) {
//        txt += "</a>";
//    }
    if (this.showYear == true) {
//        if (this.YearPage.length > 0) {
//            txt += "<a onclick='document.location=\"" + this.YearPage + "?year=" + this.year + "\"'>";
//        }
        txt += '  ' + this.year;
//        if (this.YearPage.length > 0) {
//            txt += "</a>";
//        }
    }
//    if (this.showNav && this.MonthPage.length > 0) {
//        var mon = this.month + 1;
//        var yr = this.year;
//        if (mon > 11) {
//            mon -= 12;
//            yr += 1;
//        } 
//        txt += "<a onclick='document.location=\"" + this.MonthPage + "?year=" + yr + "&month=" + mon + "\"'> -->></a>";
//    }
//    this.titlediv.innerHTML = txt; 
}
function clearArray(ar)
{
    while(ar.length > 0) {
        ar.pop();
    }
}
function clearCalendarDisplayAndData()
{
    this.clearData();
    this.clearDisplay();
}
function clearCalendarData()
{
    for(var i = 0; i < this.celldata.length; i++) {
        clearArray(this.celldata[i]);
        clearArray(this.celldatatitle[i]);
    }
}
    
function clearCalendarDisplay()
{
//    var cnt = this.cells.length - 7;
//    for (var i = 0; i < cnt; i++) {
//        this.cells[i].innerHTML = "";
//    }
}
function getCalendarDays()
{
    return this.endofmonth;
}
function setCalendarYMD(year, month, day)
{
    this.setYear(year);
    this.setMonth(month);
    this.setDay(day);
}
function setCalendarDay(day)
{
    this.day = day;
}
function setCalendarYear(year)
{
    if (year < 1000) {
        year += 1900;
    }
    this.year = year;
    this.setMonth(this.month + 1);
}
function getFirstDay(year, month)
{
    // month is zero based
    var dt = new Date();
    if (year < 1000) {
        year += 1900;
    }
    dt.setYear(year);

    dt.setMonth(month);
    dt.setDate(1);
    return dt.getDay();
}

function setCalendarMonth(month)
{
    if (month < 1 || month > 12) 
        return;

    this.month = month - 1;
    this.right_now.setMonth(this.month);
//    this.titlediv.innerHTML = this.month_name[month];
  
    this.firstDay = getFirstDay(this.year, this.month);

    // create a varible to specify what the
    // last day for the current month is
    this.endofmonth = getLastDay(this.year, this.month);
}

function getLastDay(year, month)
{
    // month is zero based
    var endofmonth = 31;

    switch (month) {
    case 0:
    case 2:
    case 4:
    case 6:
    case 7:
    case 9:
    case 11:
        endofmonth = 31;
        break;
    case 3:
    case 5:
    case 8:
    case 10:
        endofmonth = 30;
        break;
    }

    if (month == 1) { 
        // This will check for a leap year
        // If the year is evenly divisible by four
        // or in the case of a new century evenly divisible
        // by 400 then the end of the February month should be the 29th

        var right_year_divided = year / 4;
        var right_year_divided_string = new String(right_year_divided);
        var is_decimal = right_year_divided_string.indexOf('.');
        if (is_decimal != -1) { 
            endofmonth = 28; 
        } else { 
            endofmonth = 29; 
        }

        var right_year_string = new String(year);
        var the_century = new String(right_year_string.charAt(2)) 
        the_century = the_century + new String(right_year_string.charAt(3));
        if (the_century == "00") { 
            right_year_divided = right_year / 400;
            right_year_divided_string = new String(right_year_divided);
            is_decimal = right_year_divided_string.indexOf('.');
            if (is_decimal != -1) {
                endofmonth = 28;
            } else {
                endofmonth=29;
            }
        }
    }
    return endofmonth;
}

function setCalendarYearContainer(container)
{
    this.yearcontainer = container;
}
function setCalendarMonthContainer(container)
{
    this.monthcontainer = container;
}
function setCalendarDayContainer(container)
{
    this.daycontainer = container;
}
function incCalendarYear()
{
    this.year++;
    this.setMonth(this.month + 1);
    this.displayYear();
}
function decCalendarYear()
{
    this.year--;
    this.setMonth(this.month + 1);
    this.displayYear();
}
    
function displayCalendarYear()
{
    if (!this.yearcontainer) {
        return;
    }
    // write the HTML and set this.yearcontainer.innerHTML
    // build the table with 12 months 4 wide x 3 high
    var yr = this.year;
    if (yr < 1000) {
        yr += 1900;
    }
    var tt = "<h2><a onclick='" + this.calname + ".decYear()'>\<\<-- </a> " + yr
           + " <a onclick='" + this.calname + ".incYear()'>--\>\></a></h2>";
    var tb = tt + "<table id=" + this.calname + "yeartable class=caltb style='width:100%;height:80%'>";
    for (var r = 0; r < 3; r++) {
        tb += "<tr class=caltr>";
        for (var c = 0; c < 4; c++) {
            tb += "<td class=caltd width='25%' id=" + this.calname + "yearmonth" + ((r * 4) + c) + ">" 
               + ((r * 4) + c) + "</td>";
        }
        tb += "</tr>";
    } 
    tb += "</table>";
    this.yearcontainer.innerHTML = tb;

    // now call this.displayMonth for each td
    for (var c = 0; c < 12; c++) {
        var cell = document.getElementById(this.calname + "yearmonth" + c);
        this.displayMonth(c, cell, true);
    }
}
function loadCalendarMonth(month)
{
    if (undefined == month) {
        month = this.month;
    }
    if (month < 0) {
        this.year -= 1;
        month = 11;
    }
    if (month > 11) {
        this.year += 1;
        month = 0;
    }
    if (this.displayMonthCallback) {
        this.displayMonthCallback(this.year, month);
        return;
    }
    this.displayMonth(month, this.monthcontainer, false);
}
function setDisplayMonthCallbackCalendar(fn)
{
    this.displayMonthCallback = fn;
}
function displayCalendarMonth(month, container, bSmall)
{
    if (!container) {
        container = this.monthcontainer;
    }
    if (!container) {
        alert("displayCalendarMonth, container is NULL, month: " + month);
        return;
    }
    if (undefined == month) {
        month = this.month;
    }
    if (month < 0) {
        this.year -= 1;
        month = 11;
    }
    if (month > 11) {
        this.year += 1;
        month = 0;
    }
 
    var firstday = getFirstDay(this.year, month); //0-6, what col to start in
    var endofmonth = getLastDay(this.year, month); //how many days in the month
    var tt = "";
    if (bSmall) {
        tt = "<div align=center style='width:100%' onclick='" + this.calname + ".loadMonth(" + month + ")'>" 
           + this.month_name[month] + "</div>";
    } else {
        tt = "<h2>"
           + "<a onclick='" + this.calname + ".loadMonth(" + (month - 1) + ")'>\<\<--</a>  " 
           + this.month_name[month] + "  " 
           + "<a onclick='" + this.calname + ".displayYear()'>" + this.getFullYear() + "</a>" 
           + "  <a onclick='" + this.calname + ".loadMonth(" + (month + 1) + ")'>--\>\></a></h2>";
        tt += "<table style='width:95%'><tr>";
        for (var i = 0; i < 7; i++) {
            tt += "<td style='width:10%;text-align:center'>" + this.day_name[i] + "</td>";
        }
        tt += "</tr></table>";           
    }
    var tb = "<table class=caltb style='width:95%;height:80%;padding:0mm'>";

    var rows = (endofmonth + firstday) / 7;
    if ((endofmonth + firstday) % 7) {
        rows += 1;
    }
    var rheight = 100 / rows;
    for (var r = 0; r < rows && endofmonth > 0; r++) {
        tb += "<tr class=caltr>";
        for (var d = 0; d < 7; d++) {
            tb += "<td ";
            if (!bSmall) {
                tb += "class=caltd ";
            }
            tb += "style='width:12%;height:2cm'>";
            //tb += "width='12%' height='" + rheight + "%'>";
            if ((r * 7) + d >= firstday && endofmonth > 0) {
                tb += "<div style='height:100%;width:100%'>";
                if (!bSmall) {
                    tb += "<span class=caldaynum "
                       +  "onmouseover='this.className=\"caldaynumover\"' "
                       +  " onmouseout='this.className=\"caldaynum\"'"
                       +  ">";
                }
                var day = (r * 7) + d - firstday + 1;
                tb += day;
                if (!bSmall) {
                    tb += "</span>";
                    for (var j = 0; j < this.celldata[day].length; j++) {
                        tb += "<br><span title='" + this.celldatatitle[day][j].replace(/'/gi, "") + "' class='daytext" + (j%2)
                           + "'>" + this.celldata[day][j] + "</span>";
                    }
                }
                tb += "</div>";
                endofmonth--;
            }
            tb += "</td>";
        }
        tb += "</tr>";
    }
    tb += "</table>";
    container.innerHTML = tt + tb;
}

function displayCalendarDay(day, container)
{
    if (!container) {
        container = this.daycontainer;
    }
    if (!container) {
        return;
    }
    // day is 1 based
    var i = this.firstDay + day - 1;
    var txt;
//    if (this.DayPage.length > 0) {
//        txt = "<a onclick='this.displayDay(0, " + day + ")'>"
//            + day + "<a/>";
//    } else {
        txt = "<b>" + day + "</b>";
//    }
    txt += "<div>";
    for (var r = 0; r < this.celldata[i].length; r++) {
        txt += "<div title='" + this.celldatatitle[i][r] + "' class='daytext" + (r%2)
            + "'>" + this.celldata[i][r] + "</div>";
    }
    txt += "</div>";
    this.daycontainer.innerHTML = txt;
}

function addCalendarEvent(calevent)
{

}
function removeCalendarEvent(calevent)
{

}
function updateCalendarEvent(calevent)
{

}
function showCalendarEvent(id)
{

}
