/*
calendar.js - Calendar functions by Adrian Holovaty
For use only by World Online. Other use is strictly prohibited.
*/

function removeChildren(a) { // "a" is reference to an object
    while (a.hasChildNodes()) a.removeChild(a.lastChild);
}

// quickElement(tagType, parentReference, textInChildNode, [, attribute, attributeValue ...]);
function quickElement() {
    var obj = document.createElement(arguments[0]);
    if (arguments[2] != '' && arguments[2] != null) {
        var textNode = document.createTextNode(arguments[2]);
        obj.appendChild(textNode);
    }
    var len = arguments.length;
    for (var i = 3; i < len; i += 2) {
        obj.setAttribute(arguments[i], arguments[i+1]);
    }
    arguments[1].appendChild(obj);
    return obj;
}

// ShortCalendarNamespace -- Provides a collection of HTML calendar-related helper functions
var ShortCalendarNamespace = {
    daysOfWeek: 'Su M Tu W Th F Sa'.split(' '),
    draw: function(month, year, div_id, callback) { // month = 1-12, year = 1-9999
        var calDiv = document.getElementById(div_id);
        
        removeChildren(calDiv);
        
        var calTable = document.createElement('table');
        
        var tableBody = quickElement('tbody', calTable);
		
		var today=new Date;
		
		start_day=today.getDay();
		
        // Draw days-of-week header
        var tableRow = quickElement('tr', tableBody);
        for (var i = start_day; i < 7+start_day; i++) {
			if (i<7) {
				n=i
			} else {
				n=i-7
			}
            quickElement('th', tableRow, ShortCalendarNamespace.daysOfWeek[n]);
        }
		var tableRow = quickElement('tr', tableBody);
        var start_date=today.getDate();
        var days=7;

        // Draw days of week starting with today
        for (var i = 0; i <= days-1; i++) {
			currentDay=new Date(today.getTime() + (i*86400000)).getDate();
			currentMonth=new Date(today.getTime() + (i*86400000)).getMonth() + 1;
			currentYear=new Date(today.getTime() + (i*86400000)).getFullYear();
            var cell = quickElement('td', tableRow, '');
            quickElement('a', cell, currentDay, 'href', 'javascript:void(' + callback + '('+currentYear+','+currentMonth+','+currentDay+'));');
        }

        calDiv.appendChild(calTable);
    }
}

// Calendar -- A calendar instance
function Short_Calendar(div_id, callback) {
    // div_id (string) is the ID of the element in which the calendar will
    //     be displayed
    // callback (string) is the name of a JavaScript function that will be
    //     called with the parameters (year, month, day) when a day in the
    //     calendar is clicked
    this.div_id = div_id;
    this.callback = callback;
    this.today = new Date();
    this.currentMonth = this.today.getMonth() + 1;
    this.currentYear = this.today.getFullYear();
    this.drawCurrent = function() {
        ShortCalendarNamespace.draw(this.currentMonth, this.currentYear, this.div_id, this.callback);
    }
    this.drawDate = function(month, year) {
        this.currentMonth = month;
        this.currentYear = year;
        this.drawCurrent();
    }
}

