var CL = new Calendar("CL", 20, 10);

/**
 * @class Constructor of the Calendar object<br>
 *
 * @param {String} divName Name of the div which contains the calendar
 * @return {Calendar} The created Calendar object
 * @constructor
 */
function Calendar(divName) {

	/**
	 * Target of the calendar (input field)
	 * @type Object
	 */
	this.target = null;

	/**
	 * Name of the div which contains the calendar
	 * @type String
	 */
	this.name =  new String(divName);

	/**
	 * HTML content of the calendar
	 * @type String
	 */
	this.tag =  new String();

	/**
	 * Title of the calendar
	 * @type String
	 */
	this.title =  new String("CALENDAR");

	/**
	 * Date of today
	 * @type Date
	 */
	this.date = new Date();

	/**
	 * Visibility of the calendar (false at start)
	 * @type Boolean
	 */
	this.vis = new Boolean(false);

	/**
	 * Current year
	 * @type Int
	 */
	this.curYear = new Number(this.date.getFullYear());

	/**
	 * Current month
	 * @type Int
	 */
	this.curMonth = new Number(this.date.getMonth());



	/**
	 * Build HTML content for months
	 * 
	 * @return {String} The created HTML content
	 */
	this.monthList = function() {
		var tmp = "<table border='0' cellpadding='0' cellspacing='0' class='Gtab'>";
		tmp += "<tr><td align='center' class='Gname'>";
		if (document.layers) {
			tmp += "<table width='100%'>";
			tmp += "<tr>";
			tmp += "<td class='Gname' align='center'>" + this.title + "</td>";
			tmp += "<td><a href='javascript://' onclick='" + this.name + ".hide();' class='Gname'>X</a></td>";
			tmp += "</tr>";
			tmp += "</table>";
		} else {
			tmp += "<span style='float:left; text-indent:20px;'>" + this.title + "</span>";
			tmp += "<span style='float:right;padding:1px;'>";
			tmp += "<a href='javascript://' onclick='" + this.name + ".hide();' class='Gname'>X</a>";
			tmp += "</span>";
		}
		tmp += "</td></tr>";
		tmp += "<tr><td style='padding:5px;'><table border='0' cellpadding='0' cellspacing='0' class='Gtab'><tr>";
		tmp += "<td class='Gtxt'><select name='" + this.name + "month' class='Gtxt' ";
		tmp += "onchange='" + this.name + ".setMonth(this[this.selectedIndex].value)' >\n";
		for (var i = 0 ; i < this.month.length; i++) {			
			tmp += "<option value='" + i + "'";
			if (this.curMonth==i)
				tmp += " selected";
			tmp += ">" + this.month[i] + "</option>\n";
		}
		tmp += "</select></td><td class='Gtxt'>\n";
		tmp += this.yearList();
		tmp += "</td></tr></table>";
		tmp += this.dayList();
		tmp += "</td></tr></table>";
		return tmp;
	}



	/**
	 * Build HTML content for years
	 * 
	 * @return {String} The created HTML content
	 */
	this.yearList = function() {
		var tmp = "<select name='" + this.name + "year' class='Gtxt' ";
		tmp += "onchange='" + this.name + ".setYear(this[this.selectedIndex].value);' >\n";
		for (var i = 1960 ; i <= 2032 ; i += 1) {
			tmp += "<option value='" + i + "'";
			if (this.curYear == i)
				tmp += " selected";
			tmp += ">" + i + "</option>\n";
		}
		tmp += "</select>\n";
		return tmp;
	}



	/**
	 * Build HTML content for years
	 * 
	 * @param {Integer} d The day of a date
	 * @param {Integer} n The day number of the week (between 1 and 7)
	 * @return {String} The created HTML content
	 */
	this.dayCell = function(d,n) {
		var today = new Date();
		var classjour = new String('');
		var tmp = new String("");
		if (this.checkDate(d) == today.getDate() && this.curMonth == today.getMonth() && this.curYear == today.getYear()) { // Today
			tmp += "<td class='Gctoday'";
		} else { // Other days
			tmp += "<td class='Gc" + n + "'";
		}
		if (!document.layers){
			var dateStr = "dd/MM/yy";
			dateStr = dateStr.replace(/dd/g,this.checkDate(d));
			dateStr = dateStr.replace(/DD/g,this.checkDate(d));
			dateStr = dateStr.replace(/MMM/g,this.month[this.curMonth].substring(0,3));
			realCurMonth = new Number(this.curMonth);
			realCurMonth += 1;
			dateStr = dateStr.replace(/MM/g,(realCurMonth < 10)?"0"+realCurMonth:""+realCurMonth);
			dateStr = dateStr.replace(/yyyy/g,this.curYear);
			dateStr = dateStr.replace(/YYYY/g,this.curYear);
			dateStr = dateStr.replace(/yy/g,("" + this.curYear).substring(2,4));
			dateStr = dateStr.replace(/YY/g,("" + this.curYear).substring(2,4));

			tmp += "title='" + this.checkDate(d) + " " + this.month[this.curMonth] + " " + this.curYear + "'";
			tmp += " onmousedown='" + this.name + ".getDate(\"";
			tmp += dateStr + "\");' ";
			tmp += " onmouseover='this.className=this.className+\"on\";' ";
			tmp += " onmouseout='this.className= this.className.substring(0,this.className.indexOf(\"on\"));'";
		} else {
			tmp += " width='22' height='16' ";
		}
		tmp += ">";
		return tmp;
	}



	/**
	 * Build HTML content for day
	 * 
	 * @return {String} The created HTML content
	 */
	this.dayList = function() {
		var cur = new Number(1);
		var tmpDate = new Date();
		var tmp = new String("<table border='0' cellpadding='0' cellspacing='0' class='Gtab'>");
		tmpDate.setDate(cur);
		tmpDate.setMonth(this.curMonth);
		tmpDate.setFullYear(this.curYear);
		tmp += "<tr>\n";
		for (var i = 1 ; i < this.day.length ; i++)
			tmp += "<td class='Gh" + i + "'>" + this.day[i] + "</td>\n";
		tmp += "<td class='Gh0'>" + this.day[0] + "</td>\n";
		tmp += "</tr>\n";
		for (var j = 0 ; j < 6 ; j++) {
			tmp += "<tr>\n";
			for (var i = 1 ; i < this.day.length ; i++) {
				tmpDate.setDate(cur);			
				if (cur <= 31 && i == tmpDate.getDay() && this.curMonth == tmpDate.getMonth()) {
					tmp += this.dayCell(cur,i);
					tmp += this.getLink(cur);
					cur += 1;
				} else {
					tmp += "<td class='Gc" + i + "'";
					tmp += ">&nbsp;";
				}
				tmp += "</td>\n";
				tmpDate.setDate(cur);
			}
			if (cur == tmpDate.getDate() && this.curMonth == tmpDate.getMonth()) {
				tmp += this.dayCell(cur,0);
				tmp += this.getLink(cur);
				cur += 1;
			} else {
				tmp += "<td class='Gc0'";
				tmp += ">&nbsp;";
			}
			tmp += "</td>\n</tr>\n";
		}
		tmp += "</table>\n";
		return tmp;
	}



	/**
	 * Build HTML content a date link
	 * 
	 * @param {Integer} c The day of a date
	 * @return {String} The created HTML content
	 */
	this.getLink = function(c) {
		var tmp = new String("");
		if (document.layers) {
			tmp = "<a href='javascript://' ";
			tmp += "onclick='" + this.name + ".getDate(\"";
			tmp += this.checkDate(c) + "/";
			tmp += ((this.curMonth < 9)?"0" + (this.curMonth + 1):this.curMonth + 1) + "/";
			tmp += this.curYear + "\"";
			tmp += ");' class='NSday'>" + (c) + "</a>";
		} else {
			tmp = (c);
		}
		return tmp;
	}



	/**
	 * Set year to display
	 * 
	 * @param {Integer} y The year to set
	 */
	this.setYear = function (y) {
		this.curYear = y;
		this.show();
	}



	/**
	 * Set month to display
	 * 
	 * @param {Integer} m The month to set
	 */
	this.setMonth = function(m) {
		this.curMonth = m;
		this.show();
	}



	/**
	 * Set target field of the calendar
	 * 
	 * @param {Object} obj Target of calendar
	 */
	this.setTarget = function(obj) {
		this.target = obj;
	}



	/**
	 * Hide the calendar
	 */
	this.hide = function() {
		setAllComboboxVisible(true);
		if (document.layers)
			document.layers[this.div].visibility = 'hide';
		else {
			document.getElementById(this.div).style.visibility = 'hidden';
			document.getElementById(this.div).style.display = 'none';
		}
		this.vis = false;
	}



	/**
	 * Set selected date into target field of the calendar
	 * 
	 * @param {String} d Date string to set
	 */
	this.getDate = function(d) {
		if (this.target != null) {
			this.target.value = d;
			window.setTimeout("document.getElementById('" + this.target.id + "').focus();", 1);
			this.hide();
		}
	}



	/**
	 * Display the calendar
	 */
	this.show = function() {
		this.vis = true;
		this.tag = "<form name='" + this.name + "_form' method='post'>\n";
		this.tag += this.monthList();
		this.tag += "</form>\n";
		setAllComboboxVisible(false);
		if (document.layers) {
			with (document.layers[this.div]) {				
				document.open("text/html");
				document.write(this.tag);
				document.close();
				visibility = 'show';
			}
		} else {
			document.getElementById(this.div).innerHTML = "" + this.tag;
			document.getElementById(this.div).style.visibility = 'visible';
			document.getElementById(this.div).style.display = 'block';
		}
	}



	/**
	 * Initialize the calendar with actual date
	 *
	 * @param {String} d Name of the container (HTML div)
	 * @param {Object} d Target field of the calendar
	 */
	this.init = function(d, obj) {
		this.div = d;
		this.target = obj;
		this.date = new Date();
		this.date.setDate(1);		
		this.curMonth = this.date.getMonth();
		this.curYear = this.date.getFullYear();
		if (!self.movingCalendar)
			self.movingCalendar = null;
	}



	/**
	 * Build a day of month (add "0" if day < 10)
	 *
	 * @param {String} d Day number on the month
	 */
	this.checkDate = function(d) {
		if(parseInt(d) <= 9)
			return "0" + parseInt(d);
		return parseInt(d);
	}
}



/**
 * Days initials of week
 */
Calendar.prototype.day = ["D", "L", "M", "M", "J", "V", "S" ];



/**
 * Months of the year
 */
Calendar.prototype.month = ["JANVIER","FEVRIER","MARS","AVRIL","MAI","JUIN","JUILLET","AOUT","SEPTEMBRE","OCTOBRE","NOVEMBRE","DECEMBRE"];


/**
 * Show the calendar
 *
 * @param {Object} input Target field of the calendar
 */
function showCalendar(input, pageX, pageY) {
	CL.init('calend',document.getElementById(input));
	document.getElementById("calend").style.top = pageY + "px";
	var x = pageX - 100;
	document.getElementById("calend").style.left = ((x < 0)?0:((x + 180 > document.documentElement.clientWidth)?document.documentElement.clientWidth - 180:x)) + "px";
	CL.show();
}
