﻿/*

Dynamic javascript calendar.

*/
function Calendar(name, containerId, textBoxId, languageCode, dateFormat)
{
    this.Container = document.getElementById(containerId);
    this.DateFormat = dateFormat;
    this.Name = name;
    this.SelectedDate = new Date();
    this.TextBox = document.getElementById(textBoxId);
    this.Visible = false;
    this.VisibleDate = new Date();
    
    this.Container.style.position = 'absolute';
    
    if (languageCode == 'FR')
    {
        this.Cancel = 'Annuler';
        this.Months = new Array('Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
        this.WeekDays = new Array('D', 'L', 'M', 'M', 'J', 'V', 'S');
    }
    else
    {
        this.Cancel = 'Cancel';
        this.Months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
        this.WeekDays = new Array('S', 'M', 'T', 'W', 'T', 'F', 'S');
    }
    
    this.DaysInMonth = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);     
    
    if (this.TextBox.value != '') this.VisibleDate = this.ParseDate(this.TextBox.value);
    
    this.Hide();
}

Calendar.prototype.Draw = function()
{
    var month, year, url, day, date, empty, html, firstDayOfMonth, idx, className, c;
       
    if (this.TextBox.value != '') this.SelectedDate = this.ParseDate(this.TextBox.value);
    
    year = this.VisibleDate.getFullYear();
    month = this.VisibleDate.getMonth();
    day = 1;
    
    firstDayOfMonth = new Date(year, month, day);
    date = firstDayOfMonth;
    
    if (year % 4 == 0 && year % 1000 != 0) this.DaysInMonth[1] = 29;
    
    html = "<table border=0 cellpadding=0 cellspacing=1 class='calCalendar' unselectable='on'>";
    html += "   <tr>";
    html += "       <td colspan=7>";
    html += "           <table border=0 cellpadding=0 cellspacing=0 class='calTitle' width='100%'>";
    html += "               <tr>";
    html += "                   <td onclick='" + this.Name + ".SetMonth(-1);' width='1'>&nbsp;&lt;</td>";
    html += "                   <td align='center'>" + this.Months[month] + " " + year + "</td>";
    html += "                   <td onclick='" + this.Name + ".SetMonth(1);' width='1'>&gt;&nbsp;</td>";
    html += "               </tr>";
    html += "           </table>";
    html += "       </td>";
    html += "   </tr>";
    
    html += "   <tr>";    
    for (idx=0; idx<this.WeekDays.length; idx++)
    {
        html += "       <td align='center' class='calDayHeader'>" + this.WeekDays[idx] + "</td>";
    }   
    html += "   </tr>";
    
    idx = 1 - firstDayOfMonth.getDay();
    day = 1;
    c = 0;
    
    while (day <= this.DaysInMonth[month])
    {       
        if (idx <= 0)
		{
			html += "<td class='calDay'>&nbsp;</td>";
		}
		else
		{			
			date.setDate(day);

			if (date.Format(this.DateFormat) == this.SelectedDate.Format(this.DateFormat)) className = 'calSelectedDay';
			else className = 'calDay';
					
			html += "<td align='center' class='" + className + "' onMouseOver='" + this.Name + ".MouseOver(this)' onMouseOut='" + this.Name + ".MouseOut(this)' onclick='" + this.Name + ".SetDate(" + date.valueOf() + ")'>" + day + "</td>";
			
			if (date.getDay() == 6 && day != this.DaysInMonth[month]) html += "</tr><tr>";
			
			day++;
		}
							
		idx++;
		c++;
    }
    
    while (c % 7 != 0)
    {
        html += "<td class='calDay'>&nbsp;</td>";
        c++;
    }
    
    html += "</tr>";
    html += "   <td align='center' colspan=7 class='calFooter' onMouseOver='" + this.Name + ".MouseOver(this)' onMouseOut='" + this.Name + ".MouseOut(this)' onclick='" + this.Name + ".Hide()'>" + this.Cancel + "</td>";
    html += "</tr>";
    html += "</table>";      
    
    this.Container.innerHTML = html;
}

Calendar.prototype.ParseDate = function(value)
{
    var date, year, month, day;
    
    year = value.substr(this.DateFormat.search('yyyy'), 4);
    month = value.substr(this.DateFormat.search('MM'), 2);
    day = value.substr(this.DateFormat.search('dd'), 2);
    
    date = new Date();
    date.setYear(year);
    date.setMonth(month-1);
    date.setDate(day);
    
    return date;
    
}

Calendar.prototype.Hide = function()
{
    this.Container.style.display = "none";
    this.Container.style.visibility = "hidden";
    this.Visible = false;
    if (this.OnHide) this.OnHide();
}

Calendar.prototype.MouseOut = function(obj)
{
    obj.className = obj.OrigClassName;
}


Calendar.prototype.MouseOver = function(obj)
{
    obj.OrigClassName = obj.className;
    obj.className = 'calDayHover';
}

Calendar.prototype.SetDate = function(value)
{   
    if (value != '') 
    {   
        this.SelectedDate = new Date(value);
        this.VisibleDate = new Date(value);
        this.TextBox.value = this.SelectedDate.Format(this.DateFormat);
    }    
    
    this.Hide();    
}

Calendar.prototype.SetMonth = function(value)
{
    var month;
    month = this.VisibleDate.getMonth();   
    this.VisibleDate.setMonth(month+value);
    this.Draw();
}

Calendar.prototype.Show = function()
{
    if (!this.Visible)
    {       
        this.Draw();          
        this.Visible = true;
        this.Container.style.display = "";
        this.Container.style.visibility = "";
        if (this.OnShow) this.OnShow();
    }
    else
    {
        this.Hide();
    }
}

Calendar.prototype.constructor = Calendar; 

Date.prototype.Format = function(f)
{
    if (!this.valueOf())
        return '&nbsp;';

    var d = this;

    return f.replace(/(yyyy|mm|dd|hh|nn|ss|a\/p)/gi,
        function($1)
        {
            switch ($1.toLowerCase())
            {
                case 'yyyy': return d.getFullYear();
                case 'mm':   return CalendarFormatNumber(d.getMonth() + 1);
                case 'dd':   return CalendarFormatNumber(d.getDate());
                case 'hh':   return CalendarFormatNumber((h = d.getHours() % 12) ? h : 12);
                case 'nn':   return CalendarFormatNumber(d.getMinutes());
                case 'ss':   return CalendarFormatNumber(d.getSeconds());
                case 'a/p':  return d.getHours() < 12 ? 'a' : 'p';
            }
        }
    );
}

function CalendarFormatNumber(number)
{
	if (number < 10)
	{
		return '0' + number;
	}
	else
	{
		return number;
	}
}