// Title: Tigra Calendar
// Description: See the demo at url
// URL: http://www.softcomplex.com/products/tigra_calendar/
// Version: 2.0 (Customized for i4u.com.au) Pop-up version
// Date: 03-25-2001 (mm-dd-yyyy)
// Author: Denis Gritcyuk <denis@softcomplex.com>

var calendars = [];

// --------------------------------------------------------------------------------
// --- tigra calendar object constructor
function calendar() {

        // set default values.
        this.prev_icon = '<img src="img/b_prev.gif" width="11" height="11" border="0" alt="Previous Month">';
        this.next_icon = '<img src="img/b_next.gif" width="11" height="11" border="0" alt="Next Month">';
        this.empty_icon = '<img src="img/pixel.gif" width="11" height="11" border="0">';
        this.arr_months = ["January", "February", "March", "April", "May",
                "June", "July", "August", "September", "October", "November", "December"];
        this.week_days = ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"];
        this.n_weekstart = 1;

        // assign methods
        this.get_cl_code = cal_get_cl_code;
        this.get_lb_code = cal_get_lb_code;
        this.cl_refresh  = cal_cl_refresh;
        this.lb_refresh  = cal_lb_refresh;
        this.cl_init     = cal_cl_init;
        this.lb_init     = cal_lb_init;
        this.get_element = (document.all ? cal_ie_get_element : cal_nn_get_element);

        // register in global collection
        this.id = calendars.length;
        calendars[this.id] = this;
}

// --------------------------------------------------------------------------------
function cal_cl_init() {
        document.write(
                '<div id="cal' + this.id + '_clcont">' + this.get_cl_code() + '</div>');
        this.el_clcont = this.get_element('cal' + this.id + '_clcont');
}

// --------------------------------------------------------------------------------
function cal_lb_init(str_month_name, str_day_name) {
	this.month_name = str_month_name;
	this.day_name   = str_day_name;
        document.write(
                '<div id="cal' + this.id + '_lbcont">' + this.get_lb_code() + '</div>');
        this.el_lbcont = this.get_element('cal' + this.id + '_lbcont');
        this.el_date   = this.get_element('cal' + this.id + '_day');
        this.el_month  = this.get_element('cal' + this.id + '_month');
}

// --------------------------------------------------------------------------------
function cal_cl_refresh(n_value) {
        var dt_datetime = time_reset(n_value ? new Date(n_value) : new Date());

        this.el_clcont.innerHTML = this.get_cl_code(dt_datetime);
        this.el_lbcont.innerHTML = this.get_lb_code(dt_datetime);
        this.el_date   = this.get_element('cal' + this.id + '_day');
        this.el_month  = this.get_element('cal' + this.id + '_month');
}

// --------------------------------------------------------------------------------
function cal_lb_refresh() {

        var re_date = /^(\d+)\/(\d+)$/;
        if (!re_date.exec(this.el_month.value)) return;
        var dt_datetime = new Date(RegExp.$2, RegExp.$1 - 1, this.el_date.value);
        var dt_verif_date = new Date(RegExp.$2, RegExp.$1 - 1, 1);
        if (dt_datetime.getMonth() != dt_verif_date.getMonth())
                dt_datetime.setDate(0);

        var dt_now = time_reset();
        if (dt_datetime < dt_now) {
                alert('Past dates are not allowed - switching to current date.');
                return this.cl_refresh(dt_now.valueOf());
        }
        this.el_clcont.innerHTML = this.get_cl_code(dt_datetime);
}

// --------------------------------------------------------------------------------
function cal_ie_get_element(str_id) {
        return document.all[str_id];
}

// --------------------------------------------------------------------------------
function cal_nn_get_element(str_id) {
        return document.getElementById(str_id);
}

// --------------------------------------------------------------------------------
function cal_get_cl_code (dt_datetime) {

        dt_datetime = time_reset(dt_datetime ? dt_datetime : new Date());

        // get same date in previous month
        var dt_prev_month = new Date(dt_datetime);
        dt_prev_month.setMonth(dt_datetime.getMonth() - 1);
        if (dt_datetime.getMonth() % 12 != (dt_prev_month.getMonth()+1) % 12) {
                dt_prev_month.setMonth(dt_datetime.getMonth());
                dt_prev_month.setDate(0);
        }

        // get same date in next month
        var dt_next_month = new Date(dt_datetime);
        dt_next_month.setMonth(dt_datetime.getMonth() + 1);
        if ((dt_datetime.getMonth() + 1) % 12 != dt_next_month.getMonth() % 12)
                dt_next_month.setDate(0);

        // get the first day to be shown
        var dt_firstday = new Date(dt_datetime);
        dt_firstday.setDate(1);
        dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - this.n_weekstart) % 7);

        // get the last day to be shown
        var dt_lastday = new Date(dt_next_month);
        dt_lastday.setDate(0);

        // get today
        var dt_now = time_reset();

        // get first day of the next month
        var dt_ncurrent_month = new Date(dt_now);
        dt_ncurrent_month.setDate(1);
        dt_ncurrent_month.setMonth(dt_ncurrent_month.getMonth() + 1);

        // prepare calendar header
        var str_buffer = new String (
                '<table class="calTable" cellspacing="0">' +
                '<tr>' +
                '       <td class="calMonth">' + (dt_datetime < dt_ncurrent_month ? this.empty_icon : '<a href="javascript:calendars[' + this.id + '].cl_refresh(' + dt_prev_month.valueOf() + ');">' + this.prev_icon + '</a>') + '</td>' +
                '       <td colspan="5" class="calHeader">' + this.arr_months[dt_datetime.getMonth()] + " " + dt_datetime.getFullYear() + '</td>' +
                '       <td class="calMonth"><a href="javascript:calendars[' + this.id + '].cl_refresh(' + dt_next_month.valueOf() + ');">' + this.next_icon + '</a></td>' +
                '</tr>'
        );

        // print weekdays titles
        var dt_current_day = new Date(dt_firstday);
        str_buffer += '<tr>';
        for (var n = 0; n < 7; n++)
                str_buffer += ' <td class="calWeekday">'+ this.week_days[(this.n_weekstart + n) % 7] + '</td>';
        str_buffer += '</tr>';

        // print calendar body
        while (dt_current_day.getMonth() == dt_datetime.getMonth() ||
                dt_current_day.getMonth() == dt_firstday.getMonth()) {
                // print row heder
                str_buffer += '<tr>';
                for (var n_current_wday = 0; n_current_wday < 7; n_current_wday++) {

                                if (dt_current_day.getDate() == dt_datetime.getDate() &&
                                        dt_current_day.getMonth() == dt_datetime.getMonth())
                                        // print current date
                                        str_buffer += '<td class="calCurrent">';
                                else if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
                                        // weekend days
                                        str_buffer += '<td class="calWeekend">';
                                else
                                        // print working days of current month
                                        str_buffer += '<td class="calWorkday">';

                                if (dt_now <= dt_current_day)
                                        str_buffer += '<a class="calFuture' +
                                                (dt_current_day.getMonth() != dt_datetime.getMonth() ? 'Other' : '') +
                                                '" href="javascript:calendars[' + this.id + '].cl_refresh(' +
                                                dt_current_day.valueOf() + ');">' + dt_current_day.getDate() + '</a>';
                                else
                                        str_buffer += '<span class="calPast' +
                                                (dt_current_day.getMonth() != dt_datetime.getMonth() ? 'Other' : '') +
                                                '">' + dt_current_day.getDate() + '</span>';

                                str_buffer += '</td>';
                                dt_current_day.setDate(dt_current_day.getDate() + 1);
                }
                // print row footer
                str_buffer += '</tr>';
        }
        // print calendar footer
        str_buffer +=  '</table>';
        return str_buffer;
}

// --------------------------------------------------------------------------------
function cal_get_lb_code(dt_datetime) {

        dt_datetime = time_reset(dt_datetime ? dt_datetime : new Date());
        var dt_now = time_reset();

			// alert("dt_datetime is: " + dt_datetime);
			// alert("dt_now is: " + dt_now);

        // get number of days in current month
        var dt_lastday = new Date(dt_datetime);
        dt_lastday.setMonth(dt_lastday.getMonth() + 1);
        dt_lastday.setDate(0);
        var n_days_in_month = dt_lastday.getDate();

        var n_months = 12; // number of months to display in lisboxes starting from current
        // print more options in the listbox if date selected is out of range available
        var dt_now_verif = new Date (dt_now);
        dt_now_verif.setMonth(dt_now_verif.getMonth() + n_months - 1);
        while (dt_now_verif < dt_datetime) {
                n_months++;
                dt_now_verif.setMonth(dt_now_verif.getMonth() + 1);
        }

        // listboxes named cal<zero-based-calendar-number>_day and cal<zero-based-calendar-number>_month
        var str_buffer =
                '<table cellpadding="3" cellspacing="0" border="0">' +
                '<tr>' +
                '<td>' +
                '       <select class="calListbox" id="cal' + this.id + '_day" name="' + this.day_name
                + '" onchange="calendars[' + this.id + '].lb_refresh()">';
        for (var i = 1; i <= n_days_in_month ; i++) {
                str_buffer += '<option value="' + (i < 10 ? '0' + i : i) +
                        '"' + (i == dt_datetime.getDate() ? ' selected' : '') + '>' + i + '</option>';
        }
        str_buffer +=
                '       </select>' +
                '</td>' +
                '<td>' +
                '       <select class="calListbox" id="cal' + this.id + '_month" name="' + this.month_name
                + '" onchange="calendars[' + this.id + '].lb_refresh()">';
        for (var i = 0; i < n_months ; i++) {
                str_buffer += '<option value="' +
                        // returned month and year format is generated here (MM/YYYY)
                        (dt_now.getMonth() < 9 ? '0' + (dt_now.getMonth() + 1) : (dt_now.getMonth() + 1)) +
                        '/' + dt_now.getFullYear() + '"' +
                        (dt_now.getFullYear() == dt_datetime.getFullYear()
                                && dt_now.getMonth() == dt_datetime.getMonth() ? ' selected' : '') +
                        '>' + this.arr_months[dt_now.getMonth()] + ' ' + dt_now.getFullYear() +
                        '</option>';
                dt_now.setMonth(dt_now.getMonth() + 1);
        }
        str_buffer +=
                '       </select>' +
                '</td>' +
                '</tr>' +
                '</table>';

        return str_buffer;
}

// --------------------------------------------------------------------------------
// removes time component from date object to allow valid date comparison
function time_reset(dt_datetime) {
        if (!dt_datetime) dt_datetime = new Date();
        dt_datetime.setHours(0);
        dt_datetime.setMinutes(0);
        dt_datetime.setSeconds(0);
        dt_datetime.setMilliseconds(0);
        return dt_datetime;
}

// --------------------------------------------------------------------------------

