// JavaScript Document

function Calendar(eventDays,Year, Month, Day, ContainerId, ClassName)
{
    Calendar.MonthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    //If no parameter is passed use the current date.
    this.oDate = new Date();
    this.Year = (Year == null) ? this.oDate.getFullYear() : Year;
    this.Month = (Month == null) ? this.oDate.getMonth() : Month - 1;
    this.Day = (Day == null) ? 0 : Day;
    this.oDate = new Date(this.Year, this.Month, 1);
    this.NextMonth = new Date(this.Year, this.Month + 1, 1);
    this.WeekStart = this.oDate.getDay();
    // Get the number of months in current month
    this.MonthDays = Math.round((this.NextMonth.getTime() - this.oDate.getTime()) / 86400000) + 1;
   
    this.HTML = '<table class="' + ((ClassName == null) ? 'tblCalendar' : ClassName) + '" cellspacing="0">';
    // Title bar
    this.HTML += '<tr><td colspan="7" class="Title">' + Calendar.MonthNames[this.Month] + ' ' + this.Year + '</td></tr>';
    // Week Names
    this.HTML += '<tr class="WeekName"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';
    this.HTML += '<tr>';
    // Fill the previous month days with space
    for(DayCounter = 0; DayCounter < this.WeekStart; DayCounter++)
    {
        this.HTML += '<td>&nbsp;</td>';
    }
    // Populate current month
    for(DayCounter = 1; DayCounter < this.MonthDays; DayCounter++)
    {
        if((DayCounter + this.WeekStart) % 7 == 1) this.HTML += '<tr>';
        if(DayCounter == this.Day)
            this.HTML += '<td class="SelectedDay">' + DayCounter + '</td>';
        else if( in_array(DayCounter,eventDays) )
			this.HTML += '<td><b>' + DayCounter + '</b></td>';
		else
			this.HTML += '<td>' + DayCounter + '</td>';
			
        if((DayCounter + this.WeekStart) % 7 == 0) 
			this.HTML += '</tr>';
    }
    // Fill the next month days with space
    for(j = (42 - (this.MonthDays + this.WeekStart)), DayCounter = 0; DayCounter <= j; DayCounter++)
    {
        this.HTML += '<td></td>';
        if((j - DayCounter) % 7 == 0) this.HTML += '</tr>';
    }
    this.HTML += '</table>';
    // Check whether the Container Id is null
    if(ContainerId != null)
    {
        this.Container = document.getElementById(ContainerId);
        // If an object exists with the given ContainerId insert the Calendar into the object
        if(this.Container)
            document.getElementById(ContainerId).innerHTML = this.HTML;
        // Else create an element with the given id and insert the calendar
        else
            document.write('<div id="' + ContainerId + '">' + this.HTML + '</div>');
    }
    else
    {
        // Loop until a unique id is obtained for the container
        do
        {
            ContainerId = 'tblCalendar' + Math.round(Math.random() * 1000);
        }
        while(document.getElementById(ContainerId));
        // create an element with the new id and insert the calendar
        document.write('<div id="' + ContainerId + '">' + this.HTML + '</div>');
    }
    // Returns Id of the element containing the calendar
    return ContainerId;
}
////////////////////////////////////////
function array_search (needle, haystack, argStrict) {
    // +   original by: Kevin van Zonneveld 
    // +      input by: Brett Zamir

    var strict = !!argStrict;
    var key = '';

    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            return key;
        }
    }

    return false;
}
////////////////////////////////////////
function in_array (needle, haystack, argStrict) {
    // +   original by: Kevin van Zonneveld 
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir

    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
////////////////////////////////////////
function ShowHide(btn, id)
{
    StyleObj = document.getElementById(id).style
    if(StyleObj.display != 'none')
    {
        StyleObj.display = 'none';
        btn.value = 'Show';
    }
    else
    {
        StyleObj.display = 'block';
        btn.value = 'Hide';
    }
       
}
