<!-- This code is derived from the DatePicker implementation -->
<!-- originally proposed by :  Kedar R. Bhave (softricks@hotmail.com) -->
<!-- Web Site:  http://www.softricks.com -->
<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Remastered by A. Stockus (astockus@univ-lr.fr) -->
<!-- University of La Rochelle, http://www.univ-lr.fr -->

// This indicates the week-end days
var weekend = [5,6];

// Today
var gNow = new Date();
var ggWinCal;

DatePicker.Months = ["Janvier", "F&eacute;vrier", "Mars", "Avril", "Mai", "Juin",
"Juillet", "Ao&ucirc;t", "Septembre", "Octobre", "Novembre", "D&eacute;cembre"];

// Non-Leap year Month days..
DatePicker.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
DatePicker.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

/*
Originally the object was called Calendar. It is renamed now to avoid
possible conflicts with future versions of JavaScript, where Calendar
class should apear (like Java language).
This constructor takes:
p_item - the pointer to the field that holds data value.
p_WinCal - windows handler (current document representation)
p_month - the month where calendar should be positionned (mandatory)
p_year - the year where calendar should be positionned (mandatory)
p_format - date format to use
*/
function DatePicker(p_item, p_WinCal, p_month, p_year, p_format) {
  if ((p_month == null) || (p_year == null)) return;

  if (p_WinCal == null)
    this.gWinCal = ggWinCal;
  else
    this.gWinCal = p_WinCal;
  //
  this.gMonthName = DatePicker.getMonth(p_month);
  this.gMonth = new Number(p_month);
  this.gYearly = false;
  //
  this.gCSS = "";
  this.gYear = p_year;
  this.gFormat = p_format;
  this.gReturnItem = p_item;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new DatePicker();

DatePicker.getMonth = function(monthNo) {
	return DatePicker.Months[monthNo];
}


	/* 
	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. 
	*/
DatePicker.getDaysOfMonth = function(monthNo, p_year) {
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return DatePicker.DOMonth[monthNo];
	
		return DatePicker.lDOMonth[monthNo];
	} else
		return DatePicker.DOMonth[monthNo];
}

	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
DatePicker.prototype.calcMonthYear = function (p_Month, p_Year, incr) {
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year, 10) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month, 10) - 1;
			ret_arr[1] = parseInt(p_Year, 10);
		}
		ret_arr[2] = parseInt(p_Year, 10) - 1;
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year, 10) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month, 10) + 1;
			ret_arr[1] = parseInt(p_Year, 10);
		}
		ret_arr[2] = parseInt(p_Year, 10) + 1;
	}
	
	return ret_arr;
}


DatePicker.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}

DatePicker.prototype.wwriteA = function(wtext) {
	this.gWinCal.document.write(wtext);
}

DatePicker.prototype.getDayOfWeekFr = function(aDay) {
    if (aDay == 0)
      return (aDay+6);
    else
      return (aDay-1);
}

/*
Creatiion of calendar document starts here.
*/
DatePicker.prototype.drawCalendar = function() {
	this.gWinCal.document.open();
	
	this.wwrite("<html>")
	this.drawCalendarHeader();
	this.wwrite("<body class=\"calBody\">");
	this.drawCalendarNavBar();
	this.drawCalendarContents();
	this.wwrite("</body></html>");
	this.gWinCal.document.close();
}

DatePicker.prototype.drawCalendarHeader = function() {
	// Setup the page...
	this.wwrite("<head><title>Calendrier</title>");
	if (this.gCSS.length > 0)
	  this.wwrite("<link rel=\"stylesheet\" type=\"text/css\" href=\""+this.gCSS+"\">");
	this.wwrite("</head>");
}

DatePicker.prototype.drawCalendarNavBar = function() {
	// Show navigation buttons
	var MMYYYY = this.calcMonthYear(this.gMonth, this.gYear, -1);
	var prevMM = MMYYYY[0];
	var prevMMYYYY = MMYYYY[1];
	var prevYYYY = MMYYYY[2];

	MMYYYY = this.calcMonthYear(this.gMonth, this.gYear, 1);
	var nextMM = MMYYYY[0];
	var nextMMYYYY = MMYYYY[1];
	var nextYYYY = MMYYYY[2];
  var vCode = "";
  
	// Navigation bar
	vCode = "<table width=\"100%\" border=0 cellspacing=0 cellpadding=0 class=\"navBarBorder\"><tr><td>\n";
	vCode += "<table width=\"100%\" border=0 cellspacing=1 cellpadding=2><tr>";
	vCode += "\n";
	vCode += "<td align=center width=15 class=\"navBarArrows\">";
	vCode += "<a class=\"navBarLink\" href=\"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + this.gMonth + "', '" + prevYYYY + "', '" + this.gFormat + "', '" + this.gCSS +
		"');\" ";
	vCode += "title=\""+DatePicker.getMonth(this.gMonth)+" "+prevYYYY+"\">&lt;<\/a></td>";
	vCode += "\n"
	vCode += "<td align=center width=15 class=\"navBarArrows\">";
	vCode += "<a class=\"navBarLink\" href=\"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevMMYYYY + "', '" + this.gFormat + "', '" + this.gCSS +
		"');\" ";
	vCode += "title=\""+DatePicker.getMonth(prevMM)+" "+prevMMYYYY+"\">&lt;&lt;<\/a></td>";
	vCode += "\n"
	vCode += "<td align=center class=\"navBarText\">"+this.gMonthName + " " + this.gYear + "</td>";
	vCode += "\n";
	vCode += "<td align=center width=15 class=\"navBarArrows\">";
	vCode += "<a class=\"navBarLink\" href=\"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextMMYYYY + "', '" + this.gFormat + "', '" + this.gCSS +
		"');\" ";
	vCode += "title=\""+DatePicker.getMonth(nextMM)+" "+nextMMYYYY+"\">&gt;&gt;<\/a></td>";
	vCode += "\n";
	vCode += "<td align=center width=15 class=\"navBarArrows\">";
	vCode += "<a class=\"navBarLink\" href=\"javascript:window.opener.Build(" + 
		"'" + this.gReturnItem + "', '" + this.gMonth + "', '" + nextYYYY + "', '" + this.gFormat + "', '" + this.gCSS +
		"');\" ";
	vCode += "title=\""+DatePicker.getMonth(this.gMonth)+" "+nextYYYY+"\">&gt;<\/a></td>";
	vCode += "\n";
	vCode += "</tr></table>";
	vCode += "\n"
	vCode += "</tr></table><br>";
	this.wwrite(vCode);
}

DatePicker.prototype.drawCalendarContents = function() {
	// Begin Table Drawing code here.
	this.wwrite("<table width=\"100%\" border=0 align=center cellspacing=0 cellpadding=0 class=\"contBorder\"><tr><td>");
	this.wwrite("<table width=\"100%\" border=0 align=center cellspacing=1 cellpadding=2>");
	
	this.drawCalHeader();
	this.drawCalData();
	
	this.wwrite("</table></td></tr></table>");
}

DatePicker.prototype.drawCalHeader = function() {
  var vCode = "<tr align=center>\n";
	vCode += "<td width='14%' class=\"contHeaderCell\">Lun</td>\n";
	vCode += "<td width='14%' class=\"contHeaderCell\">Mar</td>\n";
	vCode += "<td width='14%' class=\"contHeaderCell\">Mer</td>\n";
	vCode += "<td width='14%' class=\"contHeaderCell\">Jeu</td>\n";
	vCode += "<td width='14%' class=\"contHeaderCell\">Ven</td>\n";
	vCode += "<td width='15%' class=\"contHeaderCell\">Sam</td>\n";
	vCode += "<td width='15%' class=\"contHeaderCell\">Dim</td>\n";
	vCode += "</tr>";
	this.wwrite(vCode);
}

DatePicker.prototype.drawCalData = function() {
  var vDate = new Date();
  vDate.setDate(1);
  vDate.setMonth(this.gMonth);
  vDate.setFullYear(this.gYear);

  var vFirstDay = this.getDayOfWeekFr(vDate.getDay());
  var vDay=1;
  var vLastDay = DatePicker.getDaysOfMonth(this.gMonth, this.gYear);
  var vOnLastDay = 0;
  var vCode = "";
  var VDate = "";
  var vReturnItem = "";
  
  // The days of previous month
  var prevMMYYYY = this.calcMonthYear(this.gMonth, this.gYear, -1);
  var prevDD = DatePicker.getDaysOfMonth(prevMMYYYY[0], prevMMYYYY[1]);

  /*
  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 += "<tr align='center'>\n";
  for (i=0; i<vFirstDay; i++) {
    vDay = prevDD-vFirstDay + i + 1;
    vCode += "<td " + this.getCellFormat(i) + "><font "+this.getTextFormat(vDay, false)+">"+ vDay +"</font></td>\n";
  }

  // Write rest of the 1st week
  vDay = 1;
  vReturnItem = " href='#' onClick=\"self.opener.document." + this.gReturnItem + ".value='";
  for (j=vFirstDay; j<7; j++) {
    vDate = this.formatDate(vDay);
    vCode += "<td " + this.getCellFormat(j) + ">";
    vCode += "<a "+this.getTextFormat(vDay, true) + vReturnItem + vDate;
    vCode += "';window.close();\" title=\""+vDate+"\">";
    vCode += vDay;
    vCode += "</a>";
    vCode += "</td>\n";
    vDay=vDay + 1;
  }
  vCode += "</tr>\n" ;

  // Write the rest of the weeks
  for (k=2; k<7; k++) {
    vCode += "<tr align='center'>";
    for (j=0; j<7; j++) {
      vDate = this.formatDate(vDay);
      vCode += "<td " + this.getCellFormat(j) + ">";
      vCode += "<a "+this.getTextFormat(vDay, true) + vReturnItem + vDate;
      vCode += "';window.close();\" title=\""+vDate+"\">";
      vCode += vDay;
      vCode += "</a>";
      vCode += "</td>\n";
      vDay=vDay + 1;

      if (vDay > vLastDay) {
        vOnLastDay = 1;
        break;
      }
    }
    if (j == 6) vCode += "</tr>\n";
    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 += "<td "+this.getCellFormat(j+m)+"><font "+this.getTextFormat(m, false)+">" + m + "</font></td>\n";
  }
  this.wwrite(vCode);
}

DatePicker.prototype.getTextFormat = function(vday, isActive) {
  var vNowDay = gNow.getDate();
  var vNowMonth = gNow.getMonth();
  var vNowYear = gNow.getFullYear();

  if ((vday == vNowDay) && (this.gMonth == vNowMonth) && (this.gYear == vNowYear)) {
    if (isActive)
      return "class=\"contTextNowActive\"";
    else
      return "class=\"contTextNowInactive\"";
  } else {
    if (isActive)
      return "class=\"contTextActive\"";
    else
      return "class=\"contTextInactive\"";
  }
}

DatePicker.prototype.getCellFormat = function(vday) {
  var i;
  // Return special formatting for the weekend day.
  for (i=0; i<weekend.length; i++) {
    if (vday == weekend[i])
      return "class=\"contWeekEndCell\"";
  }
  return "class=\"contWeekCell\"";
}

DatePicker.prototype.formatDate = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = DatePicker.getMonth(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = DatePicker.getMonth(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	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\/YY" :
			vData = vMonth + "\/" + vDD + "\/" + vY2;
			break;
		case "MM-DD-YYYY" :
			vData = vMonth + "-" + vDD + "-" + vY4;
			break;
		case "MM-DD-YY" :
			vData = vMonth + "-" + vDD + "-" + vY2;
			break;

		case "DD\/MON\/YYYY" :
			vData = vDD + "\/" + vMon + "\/" + vY4;
			break;
		case "DD\/MON\/YY" :
			vData = vDD + "\/" + vMon + "\/" + vY2;
			break;
		case "DD-MON-YYYY" :
			vData = vDD + "-" + vMon + "-" + vY4;
			break;
		case "DD-MON-YY" :
			vData = vDD + "-" + vMon + "-" + vY2;
			break;

		case "DD\/MONTH\/YYYY" :
			vData = vDD + "\/" + vFMon + "\/" + vY4;
			break;
		case "DD\/MONTH\/YY" :
			vData = vDD + "\/" + vFMon + "\/" + vY2;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDD + "-" + vFMon + "-" + vY4;
			break;
		case "DD-MONTH-YY" :
			vData = vDD + "-" + vFMon + "-" + vY2;
			break;

		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		case "DD\/MM\/YY" :
			vData = vDD + "\/" + vMonth + "\/" + vY2;
			break;
		case "DD-MM-YYYY" :
			vData = vDD + "-" + vMonth + "-" + vY4;
			break;
    case "DD-MM-YY" :
      vData = vDD + "-" + vMonth + "-" + vY2;
      break;
    default :
      vData = vMonth + "\/" + vDD + "\/" + vY4;
  }

  return vData;
}

function getYearMonthFromString(strDate, strFormat) {
  var separator;
  var mIdx, mLen;
  var yIdx, yLen;
  var sIdx1, sIdx2;
  var year_month = new Array();
  
  if (strFormat.indexOf("/") > 0) {
    separator = "/";
  } else if (strFormat.indexOf("-") > 0) {
    separator = "-";
  } else {
    return null;
  }
  
  sIdx1 = 2; sIdx2 = 5;
	switch (strFormat) {
		case "MM/DD/YYYY" :
		case "MM-DD-YYYY" :
			mIdx = 0; mLen = 2;
			yIdx = 6; yLen = 4;
			break;

		case "MM/DD/YY" :
		case "MM-DD-YY" :
			mIdx = 0; mLen = 2;
			yIdx = 6; yLen = 2;
			break;

		case "DD/MM/YYYY" :
		case "DD-MM-YYYY" :
			mIdx = 3; mLen = 2;
			yIdx = 6; yLen = 4;
			break;

		case "DD/MM/YY" :
    case "DD-MM-YY" :
			mIdx = 3; mLen = 2;
			yIdx = 6; yLen = 2;
      break;
    default :
      return null;
  }
  if ((strDate.substr(sIdx1, 1) != separator) ||
      (strDate.substr(sIdx2, 1) != separator))
    return null;
  year_month[0] = strDate.substr(yIdx, yLen);
  year_month[1] = String(parseInt(strDate.substr(mIdx, mLen), 10)-1);
  return year_month;
}

function Build(p_item, p_month, p_year, p_format, p_css) {
  var p_WinCal = ggWinCal;
  gCal = new DatePicker(p_item, p_WinCal, p_month, p_year, p_format);

  // Customize your Calendar here..
  gCal.gCSS = p_css;
  gCal.drawCalendar();
}


/* 
Takes three arguments:
0 : date value/return item
1 : date format (optional)
2 : css file url
	*/
function showCalendar() {
  var item_date;
  p_month = "";
  p_item = arguments[0];
  
  if (arguments[1] == null)
    p_format = "DD/MM/YYYY";
  else
    p_format = arguments[1];

  if (arguments[2] == null)
    p_css = "";
  else
    p_css = arguments[2];

  if ((p_item != null) && (p_item.length > 0)) {
    y_m = getYearMonthFromString(eval("document."+p_item+ ".value"), p_format);
    if (y_m != null) {
      p_month = y_m[1];
      p_year = y_m[0];
    }
  } 
  if (p_month.length == 0) {
    // p_month = new String(gNow.getMonth());
    p_month = gNow.getMonth();
    p_year = new String(gNow.getFullYear().toString());
  }

  vWinCal = window.open("", "Calendrier", 
	                      "width=230,height=175,top=200,left=200,status=no,resizable=no,menu=no,titlebar=no");
  vWinCal.opener = self;
  ggWinCal = vWinCal;

  Build(p_item, p_month, p_year, p_format, p_css);
}

