// ===================================================================
// Author: Jérôme Lacaille <jerome.lacaille@free.fr>
// ===================================================================


/* 
JLADate.js
Author: Jérôme Lacaille
Last modified: 3/21/02
*/

function getFullYear(){
   var year = this.getYear();
   if(year < 1000){
      year += 1900;}
   return year
}

function getFullMonth(){
	var fullMonth = new Array("Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre");

    var month = this.getMonth();
    return fullMonth[month];
}

function getFullDay(){
    var day = this.getDay();
    return (getDay(day));
}

function getDay(iday) {
	//var fullMonth = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var fullDay = new Array("Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi");
	
    return fullDay[iday];	
}

function Calendar_setMonth(m)
{
	this.today.setMonth(m - 1);
	this.firstDayOfMonth = GetFirstDayOfMonth(this.today);
	this.nbDaysInMonth = GetNbDaysInMonth(this.today);	
}

function Calendar_nextMonth()
{
	this.today.setMonth(this.today.getMonth() + 1);
	this.firstDayOfMonth = GetFirstDayOfMonth(this.today);
	this.nbDaysInMonth = GetNbDaysInMonth(this.today);	
}

function Calendar_previousMonth()
{
	this.today.setMonth(this.today.getMonth() - 1);
	this.firstDayOfMonth = GetFirstDayOfMonth(this.today);
	this.nbDaysInMonth = GetNbDaysInMonth(this.today);	
}

function Calendar_refresh()
{
	document.getElementById(this.divName).innerHTML = this.content();
}

function Calendar_display()
{		
	document.getElementById(this.divName).innerHTML = this.content();
	document.getElementById(this.divName).style.display = "block";
}

function LBCalendar(divName, calName, LBDayName, LBMonthName, LBYearName, NextCalendar)
{
	Date.prototype.getFullYear = getFullYear;
	Date.prototype.getFullMonth = getFullMonth;
	Date.prototype.getFullDay = getFullDay;	

	this.today = new Date();
	this.divName = divName;
	this.outName = null;
	this.firstDayOfWeek = 1;
	this.firstDayOfMonth = GetFirstDayOfMonth(this.today);
	this.nbDaysInMonth = GetNbDaysInMonth(this.today);
	this.nextMonth = Calendar_nextMonth;
	this.previousMonth = Calendar_previousMonth;
	this.setMonth = Calendar_setMonth;
	this.display = Calendar_display;
	this.refresh = Calendar_refresh;
	this.content = GetContent;	
	this.name = calName
	this.LBDay = LBDayName;
	this.LBMonth = LBMonthName;
	this.LBYear = LBYearName;
	this.NextCalendar = NextCalendar;
	
	this.today = new Date();
	this.firstDayOfMonth = GetFirstDayOfMonth(this.today);
	this.nbDaysInMonth = GetNbDaysInMonth(this.today);
	
}

function Calendar(divName, outName, calName)
{
	Date.prototype.getFullYear = getFullYear;
	Date.prototype.getFullMonth = getFullMonth;
	Date.prototype.getFullDay = getFullDay;	

	this.today = new Date();
	this.divName = divName;
	this.outName = outName;
	this.firstDayOfWeek = 1;
	this.firstDayOfMonth = GetFirstDayOfMonth(this.today);
	this.nbDaysInMonth = GetNbDaysInMonth(this.today);
	this.nextMonth = Calendar_nextMonth;
	this.previousMonth = Calendar_previousMonth;
	this.display = Calendar_display;
	this.refresh = Calendar_refresh;
	this.content = GetContent;	
	this.name = calName
}

function GetContent()
{
	var s = "";

		s += "<table class='Calendar' border='0' cellspacing='0' width='120' height='140'><tr class='monthRow'>";
		s += "<td class='precMonthCell'><span onclick='" + this.name + ".previousMonth(); " + this.name + ".refresh();'>&lt;&lt;</span></td>";
		s += "<td class='MonthYearCell' colspan='5' align='center'>";
		s += this.today.getFullMonth();
		s += "&nbsp;";
		s += this.today.getFullYear();
		s += "</td>";
		s += "<td class='nextMonthCell'><span onclick='" + this.name + ".nextMonth(); " + this.name + ".refresh();'>&gt;&gt;</span></td>";
		s += "</tr>";

		s += "<tr class='dayRow'>";
		for (var i=this.firstDayOfWeek; i < 7; i++) {
			s += "<td align='right' class='dayCell'>" + getDay(i).substr(0,3) + "</td>";
		}
		for (var i=0; i < this.firstDayOfWeek; i++) {
			s += "<td align='right' class='dayCell'>" + getDay(i).substr(0,3) + "</td>";
		}
		s += "</tr>";

		var id = 1;
		var idStartCol;
		
		idStartCol	= this.firstDayOfMonth - this.firstDayOfWeek;
		if (idStartCol < 0) idStartCol += 7;
		for (var row=0; row<6; row++) {
			s += "<tr class=''>";
			for (var col=0; col<7; col++) {
				if (col < idStartCol && row == 0) {
					if (col > 4) {
						s += "<td align='right' class='dayCellWeekEnd'>";					
					} else {				
						s += "<td class='dayOutMonthCell'>&nbsp;";				
					}
				} else {
					if (id <= this.nbDaysInMonth) {
						if (col > 4) {
							s += "<td align='right' class='dayCellWeekEnd' onmouseover='overCell(this);' onmouseout='notoverCellWeekEnd(this);' onclick='dateselect(this," + id + "," + this.today.getMonth() + "," + this.today.getFullYear() + "," + this.name + ");'>";					
						} else {
							s += "<td align='right' class='dayOfMonthCell' onmouseover='overCell(this);' onmouseout='notoverCell(this);' onclick='dateselect(this," + id + "," + this.today.getMonth() + "," + this.today.getFullYear() + "," + this.name + ");'>";					
						}		
						var theDay = new Date();
						if (this.today.getMonth() == theDay.getMonth() && id == theDay.getDate())
						{
							s += "<font color='#FF0000'><b>" + id + "</b></font>";
						}
						else
						{
							s += id;
						}
						id += 1;
					} else {
						if (col > 4) {
							s += "<td align='right' class='dayCellWeekEnd'>";					
						} else {				
							s += "<td class='dayOutMonthCell'>&nbsp;";				
						}
					}
				}
				s += "</td>";				
			}
			s += "</tr>";
		}
		s += "</table>";
			
	return (s);
}

// Return first day of month
// Return Value is 0 to 6 (0 = sunday)
function GetFirstDayOfMonth(iDate)
{
	firstDay = new Date(iDate);
	firstDay.setDate(1);
	
	//alert (firstDay.getDay());
	return (firstDay.getDay());
}		

// Return number of days in month
// Return Value is 1 to 31 
function GetNbDaysInMonth(iDate)
{
	var oDate = new Date(iDate);
	oDate.setMonth(oDate.getMonth() + 1);
	oDate.setDate(1);
	oDate.setDate(oDate.getDate() - 1);
	
	//alert(oDate.getDate());
	return (oDate.getDate());
}

function dateselect(objet, day, month, year, cal) {
	if (objet.style.background == '#860d01') {
		objet.style.background = '#FFFFFF';
	} else {
		objet.style.background = '#860d01';
	}
	sDay = new String(day);
	if (sDay.length == 1)
		sDay = "0" + sDay;
		
	month = month + 1;
	sMonth = new String(month);
	if (sMonth.length == 1)
		sMonth = "0" + month;
								
	//alert(sDay + "/" + sMonth + "/" + year);
	if (cal.outName == null)
	{
		document.getElementById(cal.LBDay).value = day;
		document.getElementById(cal.LBMonth).value = month;
		document.getElementById(cal.LBYear).value = year;
		if (cal.NextCalendar != null)
		{
			cal.NextCalendar.setMonth(month);
		}
	}
	else
		document.getElementById(cal.outName).value = sDay + "/" + sMonth + "/" + year;
}
function overCell(objet) {objet.style.background = '#860d01';	objet.style.color = '#FFFFFF';}
function notoverCell(objet){objet.style.background = '#FFFFFF';	objet.style.color = '#000000';}
function notoverCellWeekEnd(objet){objet.style.background = '#FFFFFF';	objet.style.color = '#000000';}
