// #################################################################################
// <SCRIPT>
// ID-3 ASP Library
// Javascript Universal Include File
// (c) 2002 ID-3 Technologies inc. All right reserved
// #################################################################################

// #################################################################################
// ID3ASP_ApplyCalendar
// #################################################################################
function ID3ASP_ApplyCalendar(obj,strFieldName,strMonthList,strWeekdayList,strEvents){
	var oNest = ID3ASP.GetElementByID(strFieldName + '_CalendarDiv',obj.parentNode);
	obj.ID3CalendarObj = new ID3ASP_CalendarObj(obj,oNest,strFieldName,strMonthList,strWeekdayList,strEvents,'ID3CalendarFix');
}

// #################################################################################
// ID3ASP_CalendarObj
// #################################################################################
function ID3ASP_CalendarObj(obj,objNest,strFieldName,strMonthList,strWeekdayList,strEvents,sType){
	this.Type				= sType		//'ID3CalendarPopUp' 'ID3CalendarButtonPopUp' 'ID3CalendarFix'
	this.Nest				= objNest;
	this.IsAbsolute			= false;
	this.Parent				= obj;
	this.DayBox				= ID3ASP.GetElementByName(strFieldName + '_D',obj.parentNode);
	this.MonthBox			= ID3ASP.GetElementByName(strFieldName + '_M',obj.parentNode);
	this.YearBox			= ID3ASP.GetElementByName(strFieldName + '_Y',obj.parentNode);
	this.ActiveDate			= new Date();
	this.MonthList			= strMonthList.split('|');
	this.WeekdayList		= strWeekdayList.split('|');
	this.FieldName			= strFieldName;
	this.Mask;
	this.DateFormat;
	this.ActiveYear;
	this.ActiveMonth;
	this.ActiveDayOfMonth;
	this.CurMonth;
	this.CurYear;
	this.Year;
	this.Month;
	this.Day;
	this.uniqueID		= obj.uniqueID;
	this.Events			= new ID3ASP_EventObj(strEvents);

	//Déclaration des méthodes de l'objet
	if (typeof(_ID3ASP_CalendarObj_prototype_called) == 'undefined'){
		_ID3ASP_CalendarObj_prototype_called = true;

		ID3ASP_CalendarObj.prototype.Build = Build;
		ID3ASP_CalendarObj.prototype.Draw = Draw;
		ID3ASP_CalendarObj.prototype.DecMonth = DecMonth;
		ID3ASP_CalendarObj.prototype.DecYear = DecYear;
		ID3ASP_CalendarObj.prototype.Hide = Hide;
		ID3ASP_CalendarObj.prototype.IncMonth = IncMonth;
		ID3ASP_CalendarObj.prototype.IncYear = IncYear;
		ID3ASP_CalendarObj.prototype.MakeNestAbsolute = MakeNestAbsolute;
		ID3ASP_CalendarObj.prototype.Show = Show;

		// Event
		ID3ASP_CalendarObj.prototype.onClassInit = onClassInit;
		ID3ASP_CalendarObj.prototype.onDateSelect = onDateSelect;
		ID3ASP_CalendarObj.prototype.onMouseOut = onMouseOut;
		ID3ASP_CalendarObj.prototype.onMouseOver = onMouseOver;
	}

	function Build(iMonth, iYear){
		var dNewDate	= new Date(iYear, iMonth, 1);
		var dPrevDate	= new Date(iYear, iMonth + 1, 0);
		var iDayOfFirst = dNewDate.getDay();
		var iDaysInMonth = dPrevDate.getDate();
		var iVarDate = 1;
		var aMonth = new Array();

		aMonth[0] = new Array(7);aMonth[1] = new Array(7);aMonth[2] = new Array(7);
		aMonth[3] = new Array(7);aMonth[4] = new Array(7);aMonth[5] = new Array(7);
		aMonth[6] = new Array(7);

		//Fixation de l'entête des jours
		aMonth[0][0] = this.WeekdayList[0].charAt(0).toUpperCase();aMonth[0][1] = this.WeekdayList[1].charAt(0).toUpperCase();
		aMonth[0][2] = this.WeekdayList[2].charAt(0).toUpperCase();aMonth[0][3] = this.WeekdayList[3].charAt(0).toUpperCase();
		aMonth[0][4] = this.WeekdayList[4].charAt(0).toUpperCase();aMonth[0][5] = this.WeekdayList[5].charAt(0).toUpperCase();
		aMonth[0][6] = this.WeekdayList[6].charAt(0).toUpperCase();

		//Construction de la première semaine
		for (var d = iDayOfFirst; d < 7; d++){
			aMonth[1][d] = iVarDate;
			iVarDate++;
		}

		for (var w = 2; w < 7; w++){
			for (d = 0; d < 7; d++){
				if (iVarDate <= iDaysInMonth) {
					aMonth[w][d] = iVarDate;
					iVarDate++;}
			}
		}
		return (aMonth);
	}

	function Draw(iMonth,iYear){
		var sHTML = new String();
		var aThisMonth = this.Build(iMonth,iYear);
		var iCellWidth = 15;

		sHTML = "<table border=0 cellspacing=1 cellpadding=0 width=100%>";
		sHTML += "<tr><td colspan=7><table border=0 cellspacing=0 width=100%><tr><td width=50%>"
		sHTML += "<input class='ID3ASP_Button' type=button value='<<' onClick='ID3ASP_GetElementByUniqueID(document.body,&quot;"+ this.uniqueID +"&quot;).ID3CalendarObj.DecMonth();'>"
		sHTML += " <SPAN class='ID3ASP_Month'>" + this.MonthList[iMonth] + "</SPAN> ";
		sHTML += "<input class='ID3ASP_Button' type=button value='>>' onClick='ID3ASP_GetElementByUniqueID(document.body,&quot;"+ this.uniqueID +"&quot;).ID3CalendarObj.IncMonth();'>"
		sHTML += "</td><td align=right width=50%>"
		sHTML += "<input class='ID3ASP_Button' type=button value='<<' onClick='ID3ASP_GetElementByUniqueID(document.body,&quot;"+ this.uniqueID +"&quot;).ID3CalendarObj.DecYear();'>"
		sHTML += " <SPAN class='ID3ASP_Year'>" + iYear + " "
		sHTML += "<input class='ID3ASP_Button' type=button value='>>' onClick='ID3ASP_GetElementByUniqueID(document.body,&quot;"+ this.uniqueID +"&quot;).ID3CalendarObj.IncYear();'>"

		if (this.Type != 'ID3CalendarFix'){
			sHTML += "</td><td><input class='ID3ASP_Button' type=button value='X' onClick='ID3ASP_GetElementByUniqueID(document.body,&quot;"+ this.uniqueID +"&quot;).ID3CalendarObj.Hide()'>";}

		sHTML += "</td></tr></table></td></tr>"

		for (var w = 0; w < 7; w++) {
			if (w==0){
				sHTML += "<tr align='center' bgColor=steelBlue>";
				for (var d = 0; d < 7; d++) {
					sHTML += "<td class='ID3ASP_Weekday' width='" + (100 / 7) + "%'>" + aThisMonth[0][d] + "</td>";
				}
			}
			else{
				sHTML += "<tr align='center'>"

				for (var d = 0; d < 7; d++) {
					//Vérification de la date passé en paramètre
					var sClassName = 'ID3ASP_DayCelEnabled';
					var sToggleClass = 'ID3ASP_DayCelEnabledOver';
					if ( (iMonth == this.ActiveMonth) && (iYear == this.ActiveYear) && (aThisMonth[w][d] == this.ActiveDayOfMonth) ){
						sToggleClass = 'ID3ASP_DayCelSelectedOver'
						sClassName = 'ID3ASP_DayCelSelected';}

					sHTML += "<td width='" + (100 / 7) + "%' height='15' style='CURSOR:Hand;' "
					sHTML += "onMouseOver='ID3ASP_ToggleClassOn(this,&quot;" + sToggleClass + "&quot;)'"
					sHTML += "onMouseOut='ID3ASP_ToggleClassOff(this)' ";

					if (isNaN(aThisMonth[w][d]) == false) {
						sHTML += " class='" + sClassName + "' onClick='ID3ASP_GetElementByUniqueID(document.body,&quot;"+ this.uniqueID +"&quot;).ID3CalendarObj.onDateSelect(" + aThisMonth[w][d] + ")'>"
						sHTML += aThisMonth[w][d];
					}
					else{
						sHTML += " class='ID3ASP_DayCelDisabled'>";}

					sHTML += "</td>";
				}
			}
			sHTML += "</tr>";
		}

		sHTML += "</table>";

		this.Nest.innerHTML = '';
		this.Nest.innerHTML = sHTML;
		aThisMonth = null;
		sHTML = null;
	}

	function DecMonth(){

		this.CurMonth--;
		if (this.CurMonth < 0){
			this.CurMonth = 11;
			this.CurYear--;}

		this.Draw(this.CurMonth,this.CurYear);

		this.Events.FireEvent('onCalChangeMonth',this)
	}

	function DecYear(){
		this.CurYear--;
		this.Draw(this.CurMonth,this.CurYear);

		this.Events.FireEvent('onCalChangeYear',this)
	}

	function Hide(){
		this.Events.FireEvent('onCalBeforeHide',this)

		this.Mask.removeNode();
		this.Nest.style.visibility = 'hidden';
		this.Parent.ID3CalendarObj = null;
		this.Parent.ID3bObjAttached = 'undefined';

		this.Events.FireEvent('onCalHide',this)
	}

	function IncMonth(){

		this.CurMonth++;
		if (this.CurMonth > 11){
			this.CurMonth = 0;
			this.CurYear++;}

		this.Draw(this.CurMonth,this.CurYear);

		this.Events.FireEvent('onCalChangeMonth',this)
	}

	function IncYear(){
		this.CurYear++;
		this.Draw(this.CurMonth,this.CurYear);

		this.Events.FireEvent('onCalChangeYear',this)
	}

	function MakeNestAbsolute(){
		if (this.IsAbsolute != true){
			var oNest = this.Nest.cloneNode(true);
			oNest.style.position = 'absolute'
			oNest.style.left = -1000
			oNest.style.display = 'none';

			this.Nest.outerHTML = ""
			this.Nest.removeNode();
			this.Nest = null;
			this.Nest = oNest;
			document.body.appendChild(oNest);
			this.IsAbsolute = true;
		}
	}

	function Show(){
		this.Events.FireEvent('onBeforeShow',this)

		this.Nest.style.backgroundColor = 'white';
		this.Nest.className = 'ID3ASP_Calendar'

		if (this.Type != 'ID3CalendarFix'){
			this.MakeNestAbsolute();
			this.Nest.style.zIndex = 1999
			this.Nest.style.position = 'absolute';
			this.Nest.style.display = 'block';
			this.Nest.style.left = ID3ASP_AbsoluteLeft(this.Parent,0);
			this.Nest.style.top = ID3ASP_AbsoluteTop(this.Parent,0) + this.Parent.offsetHeight;

			ID3ASP_AdjustPosition(this.Nest,this.Parent)
			this.Mask = ID3ASP_DrawMask(this.Nest)
		}
	}

	//---------------------------------------------------
	// EVENT
	//---------------------------------------------------
	function onClassInit(){
		var iMonth;

		switch (this.MonthBox.type){
			case 'hidden':
			case 'text':
				if (isNaN(this.MonthBox.value) == false){
					iMonth = this.MonthBox.value - 1;}
				break;
			case 'select-one':
				if (isNaN(this.MonthBox.options[this.MonthBox.selectedIndex].value) == false){
					iMonth = this.MonthBox.options[this.MonthBox.selectedIndex].value - 1;}
				else{
					iMonth = '';}
				break;
		}


		if ( (this.YearBox.value == '') || (this.DayBox.value == '') || (String(iMonth) == '') ){
			var oDate = new Date();}
		else{
			var oDate = new Date(this.YearBox.value,iMonth,this.DayBox.value);}

		if ((this.Type == 'ID3CalendarPopUp') && (this.YearBox.type == 'hidden') ){
			this.DateFormat = ID3ASP.GetElementByName(this.FieldName + '_DateFormat',this.Parent.parentNode).value;
		}

		this.ActiveYear			= oDate.getFullYear();
		this.ActiveMonth		= oDate.getMonth();
		this.ActiveDayOfMonth		= oDate.getDate();

		this.Year 			= this.ActiveYear;
		this.Month			= this.ActiveMonth + 1;
		this.Day			= this.ActiveDayOfMonth;

		this.CurMonth			= oDate.getMonth();
		this.CurYear			= oDate.getFullYear();
		this.Draw(this.ActiveMonth,this.ActiveYear);

		this.Show();
	}

	function onDateSelect(iDay){
		var sOldMonth,sOldYear,sOldDay

		switch(this.MonthBox.type){
			case 'hidden':
			case 'text':
				sOldMonth = this.MonthBox.value;
				this.MonthBox.value	= this.CurMonth + 1;
				break;
			case 'select-one':
				sOldMonth = this.MonthBox.options[this.MonthBox.selectedIndex].value;
				for (var i=0; i<this.MonthBox.options.length;i++){
					if (this.MonthBox.options[i].value == (this.CurMonth + 1) ){
						this.MonthBox.options[i].selected = true;
					}
				}
				break;
		}

		sOldYear = this.YearBox.value;
		sOldDay	 = this.DayBox.value;

		this.YearBox.value		= this.CurYear;
		this.DayBox.value		= iDay;

		this.Year 			= this.YearBox.value;
		this.Month			= this.CurMonth + 1;
		this.Day			= this.DayBox.value;

		switch (this.Type){
			case 'ID3CalendarPopUp':
				if (this.YearBox.type == 'hidden'){
					var sValue = new String(this.DateFormat);
					sValue = sValue.replace('[D]', iDay);
					sValue = sValue.replace('[MM]', this.MonthList[this.CurMonth]);
					sValue = sValue.replace('[M]', this.CurMonth + 1);
					sValue = sValue.replace('[Y]', this.CurYear);
					this.Parent.value  = sValue;
				}
				this.Hide();
				break;
			case 'ID3CalendarFix':
				this.onClassInit();
				break;
		}

		if ( (sOldYear != this.YearBox.value) || (sOldMonth != this.CurMonth + 1) || (sOldDay != this.DayBox.value) ){
			this.Events.FireEvent('onCalChange',this);
		}

		this.Events.FireEvent('onCalClick',this)
	}

	function onMouseOut(){
		this.Hide();
	}

	function onMouseOver(){
	}

	this.onClassInit();
	this.Events.FireEvent('onCalShow',this);
}
