var weekend = [0,6];
var weekendColor = "#e0e0e0";
var fontface = "Verdana";
var fontsize = 2;

var dtmDate = new Date();
var objGenWindowCalendar;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

Calendar.Months = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin",
"Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"];
Calendar.Days = ["D", "L", "M", "M", "J", "V","S"];
// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(objFieldDateToFill, objWorkingWindow, intMois, intYear, strFormat) {
	if ((intMois == null) && (intYear == null))	return;

	if (objWorkingWindow == null)
		this.gWinCal = objGenWindowCalendar;
	else
		this.gWinCal = objWorkingWindow;
	
	if (intMois == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(intMois);
		this.gMonth = new Number(intMois);
		this.gYearly = false;
	}

	this.gYear = intYear;
	this.gFormat = strFormat;
	this.gBGColor = "white";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
	this.gReturnItem = objFieldDateToFill;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = GetDaysInMonth;
Calendar.getDayOfTheWeek = Calendar_getDayOfTheWeek;
Calendar.print = Calendar_print;

function Calendar_get_month(intMois) {
	return Calendar.Months[intMois];
}

function GetDaysInMonth(intMois, intYear) {
	/* 
	Retourne le nombre de jours dans le mois
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((intYear % 4) == 0) {
		if ((intYear % 100) == 0 && (intYear % 400) != 0)
			return Calendar.DOMonth[intMois];
	
		return Calendar.lDOMonth[intMois];
	} else
		return Calendar.DOMonth[intMois];
}

function Calendar_getDayOfTheWeek(intMois, intYear)
{
	/* 
	Retourne le jour (0-6) de la semaine pour la première journée du mois
	*/
	objDate = new Date(intYear, intMois, 1);
	return (objDate.getDay());
}

function Calendar_print() {
	objGenWindowCalendar.print();
}
function SubtractOneMonth(dtmDate)
{
	var intDay = dtmDate.getDate();
	var intMonth = dtmDate.getMonth();
	var intYear = dtmDate.getFullYear();
	var intDaysInMonth =  GetDaysInMonth(intMonth, intYear);
	if (intMonth == 1)
	{
		intMonth = 12;
		intYear -= 1;
	}
	else
	{
		intMonth -= 1;
	}
	
	if (intDay > intDaysInMonth )
	{
		intDay = intDaysInMonth;
	}

	return new Date(intYear,intMonth,intDay)
}

function AddOneMonth(dtmDate)
{
	var intDay = dtmDate.getDate();
	var intMonth = dtmDate.getMonth();
	var intYear = dtmDate.getFullYear();
	var intDaysInMonth =  GetDaysInMonth(intMonth, intYear);

	if (intMonth == 12)
	{
		intMonth = 1
		intYear += 1
	}
	else
	{
		intMonth += 1
	}
	if (intDay > intDaysInMonth )
	{
		intDay = intDaysInMonth;
	}

	return new Date(intYear,intMonth,intDay)
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	
	// Begin Table Drawing code here..
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE>";
	
	return vCode;
}

Calendar.prototype.show = function() {
	var vCode = "";
	
	this.gWinCal.document.open();

	// Setup the page...
	this.wwrite("<html>");
	this.wwrite("<head><title>Calendrier</title>");
	this.wwrite("<style>");
	this.wwrite(".Jour {color: #000080; font-family: Arial; font-size: 12px; font-weight: bold}");
	this.wwrite(".JourHilite {color: #FF0000; font-family: Arial; font-size: 12px; font-weight: bold}");
	this.wwrite(".HeaderLink {text-decoration: none;font-family: Arial; font-size: 16px; color: #FFFF00; font-weight: bold;}");
	this.wwrite(".Header {font-family: Arial; font-size: 12px; color: #FFFF00; font-weight: bold; background-color: #000099}");
	this.wwrite(".WeekDays {background-color: #DEFCFE}");
	this.wwrite(".WeekendDays {background-color: #99CCFF}");

	this.wwrite("</style>");
	this.wwrite("</head>");

	this.wwrite("<body >");
	// Show navigation buttons
	varThisDate = new Date(this.gYear, this.gMonth, 1)
	var dtmPreviousdate = SubtractOneMonth(varThisDate);
	var intPreviousMonth = dtmPreviousdate.getMonth();
	var intPreviousYear = dtmPreviousdate.getFullYear();

	var dtmNextDate = AddOneMonth(varThisDate);
	var intNextMonth = dtmNextDate.getMonth();
	var intNextYear = dtmNextDate.getFullYear();

	
	this.wwrite("<Table Border=\"0\" cellspacing=\"0\" cellpadding=\"3\"><TR Class=\"Header\"><TD colspan=\"7\">");
	this.wwrite("<Table Width=\"100%\" Border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><TR>");
	this.wwrite("<TD ALIGN=\"Left\"><a Class=\"HeaderLink\"  HREF=\"" +
		"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + intPreviousMonth + "', '" + intPreviousYear + "', '" + this.gFormat + "'" +
		");" +
		"\">&lt;</a></TD>");
	this.wwrite("<TD  Class=\"Header\" ALIGN=\"center\">" +  this.gMonthName + " " + this.gYear  +"</TD>");	
	this.wwrite("<TD ALIGN=\"Right\"><a Class=\"HeaderLink\" HREF=\"" +
		"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + intNextMonth + "', '" + intNextYear + "', '" + this.gFormat + "'" +
		");" +
		"\">&gt;</a></TD></TR></TABLE>");
	this.wwrite("</TD></TR>");

	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	this.wwrite(vCode);

	this.wwrite("</body></html>");
	this.gWinCal.document.close();
}


Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext) {
	this.gWinCal.document.write(wtext);
}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	
	vCode = vCode + "<TR>";
	for(x=0;x<7;x++)
	{
		vCode = vCode + "<TD ALIGN=\"center\" Class=\"Header\">" + Calendar.Days[x] + "</TD>";
	}
	vCode = vCode + "</TR>";
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);

	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/

	vCode = vCode + "<TR>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD" + this.write_weekend_string(i) + ">&nbsp;</TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "<TD ALIGN=\"center\"" + this.write_weekend_string(j) + ">" + 
			"<A " + this.format_day(vDay) + "  HREF='#' " + 
					"onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 
					this.format_data(vDay) + 
					"';window.close();\">" + 
				vDay + 
				"</A>" + 
				"</TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";

	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";

		for (j=0; j<7; j++) {
			vCode = vCode + "<TD ALIGN=\"center\"" + this.write_weekend_string(j) + ">" + 
			"<A " + this.format_day(vDay) + "  HREF='#' " + 
					"onClick=\"self.opener.document." + this.gReturnItem + ".value='" + 
					this.format_data(vDay) + 
					"';window.close();\">" + 
				vDay + 
				"</A>" + 
				"</TD>";
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>";
		if (vOnLastDay == 1)
			break;
	}
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++)
	{
		vCode = vCode + "<TD" + this.write_weekend_string(j+m) +  ">&nbsp;</TD>"
	}
	vCode = vCode + "</tr>"
	for (z=k+1; z<7; z++)
	{
		vCode = vCode + "<tr>"
		// Fill up the rest of last week with proper blanks, so that we get proper square blocks
		for (m=0; m<(7); m++)
		{
			vCode = vCode + "<TD" + this.write_weekend_string(m) + ">&nbsp;</TD>"
		}
		vCode = vCode + "</tr>"

	}
	return vCode;
}

Calendar.prototype.format_day = function(vday) {
	var vNowDay = dtmDate.getDate();
	var vNowMonth = dtmDate.getMonth();
	var vNowYear = dtmDate.getFullYear();

	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		return ("Class=\"JourHilite\"");
	else
		return ("Class=\"Jour\"");
}

Calendar.prototype.write_weekend_string = function(vday) {
	var i;

	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])
			return (" Class=\"WeekendDays\" ");
	}
	
	return " Class=\"WeekDays\" ";
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.gFormat) {
		case "MM\/DD\/YYYY" :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
			break;
		case "MM-DD-YYYY" :
			vData = vMonth + "-" + vDD + "-" + vY4;
			break;
		case "DD\/MON\/YYYY" :
			vData = vDD + "\/" + vMon + "\/" + vY4;
			break;
		case "DD-MON-YYYY" :
			vData = vDD + "-" + vMon + "-" + vY4;
			break;
		case "DD\/MONTH\/YYYY" :
			vData = vDD + "\/" + vFMon + "\/" + vY4;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDD + "-" + vFMon + "-" + vY4;
			break;
		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		case "DD-MM-YYYY" :
			vData = vDD + "-" + vMonth + "-" + vY4;
			break;
		case "YYYY\/MM\/DD" :
			vData =  vY4 + "\/" + vMonth + "\/" + vDD;
			break;
		default :
			vData =  vY4 + " " + vMonth + " " + vDD;
	}

	return vData;
}

function Build(objFieldDateToFill, intMois, intYear, strFormat) {
	var objWindowCalenderToBuild = objGenWindowCalendar;
	objCalendar = new Calendar(objFieldDateToFill, objWindowCalenderToBuild, intMois, intYear, strFormat);

	// Customize your Calendar here..
	objCalendar.gBGColor="white";
	objCalendar.gLinkColor="black";
	objCalendar.gTextColor="black";
	objCalendar.gHeaderColor="white";

	objCalendar.show();
}

function show_calendar() {
	/* 
		intMois : 0-11 for Jan-Dec; 12 for All Months.
		intYear	: 4-digit year
		strFormat: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		objFieldDateToFill	: Return Item.
	*/

	objFieldDateToFill = arguments[0];
	if (arguments[1] == null)
		intMois = dtmDate.getMonth();
	else
		intMois = arguments[1];
	if (arguments[2] == "" || arguments[2] == null)
		intYear = dtmDate.getFullYear();
	else
		intYear = arguments[2];
	if (arguments[3] == null)
		strFormat = "YYYY\/MM\/DD";
	else
		strFormat = arguments[3];

	objWindowCalendar = window.open("", "Calendar", "width=160,height=200,status=no,resizable=no,top=200,left=200");
	objWindowCalendar.opener = self;
	objGenWindowCalendar = objWindowCalendar;

	Build(objFieldDateToFill, intMois, intYear, strFormat);
}