/* WDSCalendar.js */ 
/* jsLibrary javascript file */
/* creation : 24/02/2005 by Charley FREDAIGUES */
/* update   : 21/06/2006 by Yannick ADAM */

/* Calendar */



/* WDSCalendar : CONSTANTS - do not modify */


/* fields automatic naming  */
WDSCalendar.DAY                  = "Day";
WDSCalendar.MONTHYEAR            = "MonthYear";
WDSCalendar.MONTH                = "Month";
WDSCalendar.YEAR                 = "Year";
WDSCalendar.HOUR                 = "Hour";
WDSCalendar.ALLMONTHS            = "ALLMONTHS";
WDSCalendar.ONEMONTH             = "ONEMONTH";
WDSCalendar.SELECTMONTH          = "SELECTMONTH";
WDSCalendar.PanelTitle           = "Title"
WDSCalendar.PanelMonths          = "Months"
WDSCalendar.PanelDate            = "Date"
WDSCalendar.PanelCalendar        = "Calendar"
WDSCalendar.PanelAction					 = "Action"
/* used for linked calendars in WDSCalendar.LinkedPanels */
WDSCalendar.PanelTitleInbound    = "TitleInbound"
WDSCalendar.PanelMonthsInbound   = "MonthsInbound"
WDSCalendar.PanelDateInbound     = "DateInbound"
WDSCalendar.PanelCalendarInbound = "CalendarInbound"
WDSCalendar.PanelActionInbound   = "ActionInbound"
/* code formats */
WDSCalendar.formatDayCode        = 'DD';
WDSCalendar.formatMonthYearCode  = 'mmYYYY';
WDSCalendar.formatMonthCode      = 'mm';
WDSCalendar.formatYearCode       = 'YYYY';
WDSCalendar.formatHourCode       = 'HHMM';


/* WDSCalendar : CONSTANTS - customizable fields */
WDSCalendar.serverDate           = new Date();
WDSCalendar.serverDate.setHours(0,0,0,0);

/* display rules */
WDSCalendar.displayDay           = false;
WDSCalendar.displayMonthYear     = false;
WDSCalendar.displayMonth         = false;
WDSCalendar.displayYear          = false;
WDSCalendar.displayHour          = false;
WDSCalendar.displayPopup         = false;

/* text format */
WDSCalendar.formatDayValue       				= 'DD';
WDSCalendar.formatMonthYearValue 				= 'mmmm YYYY';
WDSCalendar.formatMonthValue     				= 'mmmm';
WDSCalendar.formatYearValue      				= 'YYYY';
WDSCalendar.formatHourValue      				= 'HH:MM';
WDSCalendar.formatOneMonthDisplayValue 	= 'mmmm YYYY';

WDSCalendar.formatResultValue    = 'DD.mm.YYYY';

/* Calendar Popup  */
WDSCalendar.CalendarCellSpacing					= '5';
WDSCalendar.PanelMonthPreviousMonth			= '<div display="none">&nbsp;</div>';
WDSCalendar.PanelMonthNextMonth					= '<div display="none">&nbsp;</div>';

/* date definitions */
/* define the min validity delay : default is 3 hours */
WDSCalendar.minValidityDelay = 3 * 60 * 60 * 1000;
/* define the max validity delay : default is 353 days */
WDSCalendar.maxValidityDelay = 353 * 24 * 60 * 60 * 1000;

/* Popup description : */
WDSCalendar.PopupOffsetX = 0;
WDSCalendar.PopupOffsetY = 0;
/* format panels */
WDSCalendar.PanelFormatMonth = WDSCalendar.ONEMONTH; /* ALLMONTHS, ONEMONTH, SELECTMONTH */
WDSCalendar.PanelFormatDate = 'DD mmmm YYYY';

/* Panels list and position : Display, TD Number, TR Number */
WDSCalendar.Panels = new Array();
/* WDSCalendar.Panels[WDSCalendar.PanelTitle]    = [true, 0, 0]; */
WDSCalendar.Panels[WDSCalendar.PanelMonths]   = [true, 0, 0];
/* WDSCalendar.Panels[WDSCalendar.PanelDate]     = [false, 0, 2]; */
WDSCalendar.Panels[WDSCalendar.PanelCalendar] = [true, 0, 1];
WDSCalendar.Panels[WDSCalendar.PanelAction]   = [true, 0, 2];

/* Panels list and position : Display, TD Number, TR Number */
WDSCalendar.LinkedPanels = new Array();
WDSCalendar.LinkedPanels[WDSCalendar.PanelTitle]    				= [true, 0, 0];
WDSCalendar.LinkedPanels[WDSCalendar.PanelMonths]   				= [true, 0, 1];
WDSCalendar.LinkedPanels[WDSCalendar.PanelCalendar] 				= [true, 0, 2];
WDSCalendar.LinkedPanels[WDSCalendar.PanelTitleInbound]   	= [true, 0, 3];
WDSCalendar.LinkedPanels[WDSCalendar.PanelMonthsInbound]   	= [true, 0, 4];
WDSCalendar.LinkedPanels[WDSCalendar.PanelCalendarInbound] 	= [true, 0, 5];
WDSCalendar.LinkedPanels[WDSCalendar.PanelActionInbound]   	= [true, 0, 6];

WDSCalendar.DisplayLinkedCalendar = true;


/* WDSCalendar class */
/* constructor */
/*    - id        : div id of the fields calendar */
/*                  MUST be the name of the variable */
/*		- resultObj : the object result date updated with the selected date */
function WDSCalendar (id, resultObj) {
	this.id               = id;
	this.resultObj        = resultObj;
	
	this.IsDisabled 				= false;
	this.linkCalendar     	= null;
	this.parentCalendar   	= null;
	this.popupOpened      	= false;
	this.title            	= "";
	this.iconText         	= "";
	this.actionClose				= "";
	this.actionChange 			= "";
	this.actionPanelPresent = false;
	
	this.startDateValidity = new Date(WDSCalendar.serverDate.getTime() + (WDSCalendar.minValidityDelay));
	this.endDateValidity   = new Date(WDSCalendar.serverDate.getTime() + (WDSCalendar.maxValidityDelay));
	this.endDateValidity.setHours(23, 59, 59);
	
	this.dateRangesValidity  = [ new DateRange(this.startDateValidity, this.endDateValidity) ];
	
	this.startDate    = new Date(this.startDateValidity);
	this.selectedDate = new Date(this.startDateValidity);
	
	this.closeCallback = null;
	this.changeCallback = null;
}


/* setEndDateValidity */
/* * setEndDateValidity */
WDSCalendar.prototype.setEndDateValidity = function (date) {
	this.endDateValidity  = new Date(date);
	this.dateRangesValidity = [ new DateRange(this.startDateValidity, this.endDateValidity) ];
}


/* setStartDateValidity */
/* * setStartDateValidity */
WDSCalendar.prototype.setStartDateValidity = function (date) {
	this.startDateValidity  = new Date(date);
	this.dateRangesValidity = [ new DateRange(this.startDateValidity, this.endDateValidity) ];
}


/* setLinkCalendar */
/* * link a calendar */
WDSCalendar.prototype.setLinkCalendar = function (calendar) {
	this.linkCalendar = calendar;
	this.linkCalendar.parentCalendar = this;
}


/* setTitle */
/* * set up the popup title */
WDSCalendar.prototype.setTitle = function (title) {
	this.title = title;
}


/* setActionClose */
/* * set up the close action string */
WDSCalendar.prototype.setActionClose = function (actionClose) {
	this.actionClose = actionClose;
}


/* setActionChange */
/* * set up the change action string */
WDSCalendar.prototype.setActionChange = function (actionChange) {
	this.actionChange = actionChange;
}


/* setResultObj */
/* * set up the result object */
WDSCalendar.prototype.setResultObj = function (iResultObj) {
	this.resultObj = iResultObj;
}


/* setIconText */
/* * set the text icon */
WDSCalendar.prototype.setIconText = function (iconText) {
	this.iconText = iconText;
}


/* setCloseCallback */
/*    - function  : callback */
/* * What does the method do ... */
WDSCalendar.prototype.setCloseCallback = function (iFunction) {
	this.closeCallback = iFunction;
}


/* setChangeCallback */
/*    - function  : callback */
/* * What does the method do ... */
WDSCalendar.prototype.setChangeCallback = function (iFunction) {
	this.changeCallback = iFunction;
}


/* getFormattedDate */
/*    - iFormat : date format */
/* * Returns the selected, formatted according to iFormat */

WDSCalendar.prototype.getFormattedDate = function (iFormat) {
	return this.selectedDate.format(iFormat);
}


/* setEngineDate */
/*    - iDate : String representing an engine date (YYYYMMDDHHHH) */
/* * Returns the selected, formatted according to iFormat */
WDSCalendar.prototype.setEngineDate = function (iDate) {
	this.selectedDate = new Date( iDate.substr(0,4), iDate.substr(4,2)-1, iDate.substr(6,2), iDate.substr(8,2), iDate.substr(10,2) );
	this.update();	
}


/* getDate */
/* * Returns the selected date */
WDSCalendar.prototype.getDate = function () {
	return this.selectedDate;
}


/* getPopupObject */
/* * get the div pop up object (the pop up calendar) */
WDSCalendar.prototype.getPopupObject = function () {
	return document.getElementById("WDSCalendarIdPopup");
}


/* getObject */
/* * get the div object */
WDSCalendar.prototype.getObject = function () {
	return document.getElementById(this.id);
}


/* display */
/* * display the input calendar */
WDSCalendar.prototype.display = function () {
	var html = "";

	html += this.getField(WDSCalendar.DAY, WDSCalendar.displayDay);
	html += this.getField(WDSCalendar.MONTHYEAR, WDSCalendar.displayMonthYear);
	html += this.getField(WDSCalendar.MONTH, WDSCalendar.displayMonth);
	html += this.getField(WDSCalendar.YEAR, WDSCalendar.displayYear);
	html += this.getField(WDSCalendar.HOUR, WDSCalendar.displayHour);
	if (WDSCalendar.displayPopup && (WDSCalendar.DisplayLinkedCalendar == false || this.parentCalendar == null)) {
		html += "<a href=\"javascript:" + this.id + ".popupToggle();\" class=WDSCalendarIcon>" + this.iconText + "</a>";
	}
	this.getObject().innerHTML = html + this.getObject().innerHTML;
}


/* disable */
/* * display the input calendar */
WDSCalendar.prototype.disable = function () {
	this.IsDisabled = true;
	
	var lObject = document.getElementById(this.id + WDSCalendar.DAY + "Id");
	if( lObject ) {
		lObject.disabled = true;
	}	
	lObject = document.getElementById(this.id + WDSCalendar.MONTHYEAR + "Id");
	if( lObject ) {
		lObject.disabled = true;
	}
	lObject = document.getElementById(this.id + WDSCalendar.MONTH + "Id");
	if( lObject ) {
		lObject.disabled = true;
	}
	lObject = document.getElementById(this.id + WDSCalendar.YEAR + "Id");
	if( lObject ) {
		lObject.disabled = true;
	}
	lObject = document.getElementById(this.id + WDSCalendar.HOUR + "Id");
	if( lObject ) {
		lObject.disabled = true;
	}	
	if (this.popupOpened) {
		this.close();
	}
}


/* enable */
/* * display the input calendar */
WDSCalendar.prototype.enable = function () {
	this.IsDisabled = false;
	
	var lObject = document.getElementById(this.id + WDSCalendar.DAY + "Id");
	if( lObject ) {
		lObject.disabled = false;
	}
	
	lObject = document.getElementById(this.id + WDSCalendar.MONTHYEAR + "Id");
	if( lObject ) {
		lObject.disabled = false;
	}	
	lObject = document.getElementById(this.id + WDSCalendar.MONTH + "Id");
	if( lObject ) {
		lObject.disabled = false;
	}
	lObject = document.getElementById(this.id + WDSCalendar.YEAR + "Id");
	if( lObject ) {
		lObject.disabled = false;
	}
	lObject = document.getElementById(this.id + WDSCalendar.HOUR + "Id");
	if( lObject ) {
		lObject.disabled = false;
	}		
}


/* getField */
/*    - type of the field */
/* * give the html code for the Field */
WDSCalendar.prototype.getField = function (type, display) {
	var html = "";
	if (display == true) {
		html = "<select name=" + this.id + type + " id=" + this.id + type + "Id";
		html += " onChange=\"" + this.id + ".updateFromDropdown();\"";
		html += "></select>&nbsp;";
	} 
	return html;
}


/* updateSelectedDate */
/* * update the selected date */
WDSCalendar.prototype.updateFromDropdown = function () {
	WDSTrace.dump("-----------------------------");
	WDSTrace.dump("BEGIN : WDSCalendar.getSelectedDate");
	
	var lPreviousDate = new Date( this.selectedDate );
	this.selectedDate = new Date();
	
	if (WDSCalendar.displayMonthYear) {
		var monthYear = WDSCommon.getTagValue(document.getElementById(this.id + WDSCalendar.MONTHYEAR + "Id"));
		/* WARNING -> format dependency */
		this.selectedDate.setYear(parseInt(monthYear.substr(2, 4), 10));
		this.selectedDate.setMonth(parseInt(monthYear.substr(0, 2), 10) - 1);
	}

	if (WDSCalendar.displayYear) {
		var year = WDSCommon.getTagValue(document.getElementById(this.id + WDSCalendar.YEAR  + "Id"));
		this.selectedDate.setYear(year);
	}
	if (WDSCalendar.displayMonth) {
		var month = WDSCommon.getTagValue(document.getElementById(this.id + WDSCalendar.MONTH + "Id"));
		this.selectedDate.setMonth(parseInt(month, 10) - 1);
	}	
		
	if (WDSCalendar.displayDay) {
		var day = WDSCommon.getTagValue(document.getElementById(this.id + WDSCalendar.DAY   + "Id"));
		this.selectedDate.setDate(day);
	}
	
	if (WDSCalendar.displayHour) {
		var hour = WDSCommon.getTagValue(document.getElementById(this.id + WDSCalendar.HOUR  + "Id"));
		/* WARNING -> format dependency */
		this.selectedDate.setHours(parseInt(hour.substr(0, 2), 10), parseInt(hour.substr(2, 2), 10), 0, 0);
	}
	
	WDSTrace.dump("range = " + this.dateRangesValidity);
	if (!this.selectedDate.dateWithinRanges(this.dateRangesValidity)) {
		this.selectedDate.setDate(1);
		if (!this.selectedDate.dateWithinRanges(this.dateRangesValidity)) {
			this.selectedDate = this.dateRangesValidity[0].from;
		}
	}
		
	/* Update the displays corresponding to the selection */
	if( (this.selectedDate.getMonth() != lPreviousDate.getMonth()) || (this.selectedDate.getYear() != lPreviousDate.getYear()) ) {
		this.updateMonthYear();
		this.updateDay();	
	}

	WDSTrace.dump("selectedDate = " + this.selectedDate);
	WDSTrace.dump("END   : WDSCalendar.getSelectedDate");
}


/* updateFromPopup */
/* * set the selected date (from popup) */
/* * update the dropdowns */
WDSCalendar.prototype.updateFromPopup = function (selectedDate) {
	
	if (this.dateRangesValidity == null || selectedDate.dateWithinRanges(this.dateRangesValidity)){
		this.selectedDate = new Date(selectedDate);
	
		if (WDSCalendar.displayMonthYear) {		
			WDSCommon.updateTag(document.getElementById(this.id + WDSCalendar.MONTHYEAR   + "Id"), null, this.selectedDate.format(WDSCalendar.formatMonthYearCode));
			this.updateMonthYear();
		}
		
		if (WDSCalendar.displayDay) {
			WDSCommon.updateTag(document.getElementById(this.id + WDSCalendar.DAY   + "Id"), null, this.selectedDate.format(WDSCalendar.formatDayCode));
			this.updateDay();
		}	
	
		if (WDSCalendar.displayHour) {
			WDSCommon.updateTag(document.getElementById(this.id + WDSCalendar.HOUR   + "Id"), null, this.selectedDate.format(WDSCalendar.formatHourCode));
		this.updateHour();
		}	
		
		if (this.resultObj) {
			WDSCommon.updateTag(this.resultObj, null, this.selectedDate.format(WDSCalendar.formatResultValue));
		}
	}
}


/* update */
/* * update the calendar */
WDSCalendar.prototype.update = function () {
	/* update result object */
	if (this.resultObj) {
		WDSCommon.updateTag(this.resultObj, null, this.selectedDate.format(WDSCalendar.formatResultValue));
	}
	
	WDSTrace.dump("---------------------------------------------------------------");
	WDSTrace.dump("Selected Date = " + this.selectedDate);
	/* populate */
	if (WDSCalendar.displayMonthYear) {
		this.updateMonthYear();
	}
	if (WDSCalendar.displayDay) {
		this.updateDay();
	}
	if (WDSCalendar.displayHour) {
		this.updateHour();
	}
	/* We are the outbound calendar, check that the inbound calendar is not set to an earlier date */
	if (this.linkCalendar != null) {
		if (this.selectedDate.getTime() > this.linkCalendar.selectedDate.getTime()) {
			this.linkCalendar.updateFromPopup( this.selectedDate );
			this.linkCalendar.update();
		} 
		/* We are the inbound calendar, check that the outbound calendar is not set to a date in advance */
	}	else if (this.parentCalendar != null) {
		if (this.selectedDate.getTime() < this.parentCalendar.selectedDate.getTime()) {		
			this.parentCalendar.updateFromPopup( this.selectedDate );
			this.parentCalendar.update();				
		} 
	}
}


/* updateDay */
/* * update Day */
WDSCalendar.prototype.updateDay = function () {
	WDSTrace.dump("-----------------------------");
	WDSTrace.dump("BEGIN : WDSCalendar.updateDay");
	var listDays = new WDSList("Days" + this.id);
	var m = this.selectedDate.getMonth();
	var d = this.selectedDate.getDate();
	var temp = new Date(this.selectedDate);
	WDSTrace.dump("dateRange = " + this.dateRangesValidity.toString());
	for (var i = 1; i <= 31; ++i) {
		temp.setDate(i);
		WDSTrace.dump("current date = " + temp.toString());
		if (m == temp.getMonth()) {
			WDSTrace.dump("test mois " + m + " == " + temp.getMonth());
			if (temp.dateWithinRanges(this.dateRangesValidity)) {
				WDSTrace.dump("add...");
				listDays.add(temp.format(WDSCalendar.formatDayCode), temp.format(WDSCalendar.formatDayValue));
			}
		}
	}
	if (this.selectedDate) {
		d = this.selectedDate.getDate();
	}
	listDays.updateHtmlSelect(this.id + WDSCalendar.DAY + "Id", d);
	WDSTrace.dump("END   : WDSCalendar.updateDay");
	
}


/* updateMonthYear */
/* * update Month Year */
WDSCalendar.prototype.updateMonthYear = function () {
	WDSTrace.dump("-----------------------------");
	WDSTrace.dump("BEGIN : WDSCalendar.updateMonthYear");
	var listMonths = new WDSList("Months" + this.id);
	
	var temp = new Date(this.startDate);
	temp.setDate(1);
	WDSTrace.dump("date = " + temp);
	WDSTrace.dump("range = " + this.dateRangesValidity);
	while ( temp.getTime() < this.dateRangesValidity[0].to.getTime() ) {
		WDSTrace.dump("current month = " + parseInt(temp.getMonth() + 1));
		if (temp.monthWithinRanges(this.dateRangesValidity)) {
			listMonths.add(temp.format(WDSCalendar.formatMonthYearCode), temp.format(WDSCalendar.formatMonthYearValue));
			WDSTrace.dump("add...");
		}
		/* increment date */
		temp.setMonth(temp.getMonth() + 1);
	}
	
	/* default value */
	value = this.startDate.format(WDSCalendar.formatMonthYearCode);
	if (this.selectedDate) {
		value = this.selectedDate.format(WDSCalendar.formatMonthYearCode);
	}
	WDSTrace.dump("default value = " + value);
	
	listMonths.updateHtmlSelect(this.id + WDSCalendar.MONTHYEAR + "Id", value);
	this.updateDay();
	WDSTrace.dump("END   : WDSCalendar.updateMonthYear");
}


/* updateMonth */
/* * update Month */
WDSCalendar.prototype.updateMonth = function () {
	alert("to be implemented");
}


/* updateYear */
/* * update Year */
WDSCalendar.prototype.updateYear = function () {
	alert("to be implemented");
}


/* updateHour */
/* * update Hours */
WDSCalendar.prototype.updateHour = function () {
	WDSTrace.dump("--------------------------------");
	WDSTrace.dump("BEGIN   : WDSCalendar.updateHour");
	var listHours = new WDSList("Hour" + this.id);
	/* var temp = new Date(this.selectedDate); */
	/* for (var h = 0; h < Date.listHours.size(); h++) { */
	/*	var hour    = Date.listHours.records[h].code.substr(0, 2); */
	/*	var minutes = Date.listHours.records[h].code.substr(2, 2); */
	/*	temp.setHours(parseInt(hour), parseInt(minutes), 0, 0); */
	/*	WDSTrace.dump("temp = " + temp); */
	/*	WDSTrace.dump("dateRangesValidity = " + this.dateRangesValidity.toString()); */
	/*	if (temp.withinRanges(this.dateRangesValidity)) { */
	/*		listHours.add(Date.listHours.records[h].code, Date.listHours.records[h].value); */
	/*	} */
	/*}	*/
	listHours.addList(Date.listHours);
	/* selected value */
	var value = null;
	if (this.selectedDate) {
		value = this.selectedDate.format(WDSCalendar.formatHourCode);
	}

	listHours.updateHtmlSelect(this.id + WDSCalendar.HOUR + "Id", value);
	WDSTrace.dump("END     : WDSCalendar.updateHour");
}



/* updatePopup */
/* * update the popupcalendar */
WDSCalendar.prototype.updatePopup = function () {
	var html = "";
	if (WDSBrowser.ie && !WDSBrowser.opera && !WDSBrowser.mac) {
		html += '<iframe src="about:blank" scrolling="no" frameborder="1"></iframe>';
	}
	html += this.getPopupHtml();
	this.getPopupObject().innerHTML = html;
}


/* getPopupHtml */
/* * getPopupHtml */
WDSCalendar.prototype.getPopupHtml = function () {
	var nbTD = 0;
	var nbTR = 0;
	
	/* Find which panel set to use depending if we're using a double calendar or not */
	var lPanels = WDSCalendar.Panels;
	if (this.linkCalendar != null || this.parentCalendar != null) {
		if (WDSCalendar.DisplayLinkedCalendar) {
			lPanels = WDSCalendar.LinkedPanels;
		}
	}
	
	/* compute <table> size */
	for (var p in lPanels) {
		/* display flag */
		var display = lPanels[p][0];
		if (display) {
			/* position */
			var x = lPanels[p][1];
			var y = lPanels[p][2];
			if (nbTD < (x + 1)) {
				nbTD = x + 1;
			}
			if (nbTR < (y + 1)) {
				nbTR = y + 1;
			}
		}
	}
	var html = "<table cellspacing=0 cellpadding=0 class='WDSCalendarPopup'>";
	for (var tr = 0; tr < nbTR; tr++) {
		html += "<tr>";
		for (var td = 0; td < nbTD; td++) {
			html += "<td>";
			html += this.getPanelHtml(td, tr);
			html += "</td>";
		}
		html += "</tr>";
	}
	html += "</table>";
	
	return html;
}


/* getPanelHtml */
/*    - td : the td position */
/*    - tr : the tr position */
/* * getPanelHtml */
WDSCalendar.prototype.getPanelHtml = function (td, tr) {
	/* We check here if the action panel is displayed - for date selection behavior */
	this.actionPanelPresent = false;	
	/* Set the panels to check ( are we linked? ) */
	var lPanels = WDSCalendar.Panels;
	var lTopCalendar = this;
	var lBottomCalendar = this;
	if (this.linkCalendar != null || this.parentCalendar != null) {
		if (WDSCalendar.DisplayLinkedCalendar) {
			lPanels = WDSCalendar.LinkedPanels;
			if (this.linkCalendar != null) {
				lBottomCalendar = this.linkCalendar;
			} else if (this.parentCalendar != null) {
				lTopCalendar = this.parentCalendar;
			}
		}
	}
	for (var p in lPanels) {
		if (lPanels[p][0] && lPanels[p][1] == td && lPanels[p][2] == tr) {
			if (p == WDSCalendar.PanelTitle) {
				return lTopCalendar.getPanelTitleHtml();
			} else if (p == WDSCalendar.PanelMonths) {
				return lTopCalendar.getPanelMonthsHtml();
			} else if (p == WDSCalendar.PanelDate) {
				return lTopCalendar.getPanelDateHtml();
			} else if (p == WDSCalendar.PanelCalendar) {
				return lTopCalendar.getPanelCalendarHtml();
			} else if (p == WDSCalendar.PanelAction) {
				this.actionPanelPresent = true;
				return lTopCalendar.getPanelActionHtml();
			} else if (p == WDSCalendar.PanelTitleInbound) {
				return lBottomCalendar.getPanelTitleHtml();
			} else if (p == WDSCalendar.PanelMonthsInbound) {
				return lBottomCalendar.getPanelMonthsHtml();
			} else if (p == WDSCalendar.PanelDateInbound) {
				return lBottomCalendar.getPanelDateHtml();
			} else if (p == WDSCalendar.PanelCalendarInbound) {
				return lBottomCalendar.getPanelCalendarHtml();
			} else if (p == WDSCalendar.PanelActionInbound) {
				this.actionPanelPresent = true;				
				return lBottomCalendar.getPanelActionHtml();
			} 
		}
	}
	return "";
}


/* getPanelTitleHtml */
/* * getPanelTitleHtml */
WDSCalendar.prototype.getPanelTitleHtml = function () {
	var html = "<table width=\"100%\" class=\"WDSCalendarPanelTitle\"><tr><td>";
	html += this.title;
	html += "</td></tr></table>";
	return html;
}


/* getPanelMonthsHtml */
/* * getPanelMonthsHtml */
WDSCalendar.prototype.getPanelMonthsHtml = function () {
	if (WDSCalendar.PanelFormatMonth == WDSCalendar.ALLMONTHS) {
		return this.getPanelMonthsHtmlAllMonths();
	} else if (WDSCalendar.PanelFormatMonth == WDSCalendar.ONEMONTH) {
		return this.getPanelMonthsHtmlOneMonth();
	} else if (WDSCalendar.PanelFormatMonth == WDSCalendar.SELECTMONTH) {
		return this.getPanelMonthsHtmlSelectMonth();
	} 
}


/* getPanelMonthsHtmlAllMonths */
/* * getPanelMonthsHtmlAllMonths */
WDSCalendar.prototype.getPanelMonthsHtmlAllMonths = function () {
	var temp = new Date(this.startDate);
	temp.setDate(1);
	var i = 0;
	var html = "<table class=WDSCalendarPanelMonth cellspacing=1 cellpadding=1 width=100%><tr>";
	while ( temp.getFullYear() < this.endDateValidity.getFullYear() ||
				 (temp.getMonth() <= this.endDateValidity.getMonth() && temp.getFullYear() <= this.endDateValidity.getFullYear()) ||
				 i%7 != 0) {
		html += "<td ";
		if (temp.monthWithinRanges(this.dateRangesValidity)) {
			if (temp.getMonth() == this.selectedDate.getMonth() && temp.getFullYear() == this.selectedDate.getFullYear()) {
				html += " class=\"WDSCalendarPanelMonthActive\"";
			} else {
				html += " class=\"WDSCalendarPanelMonthEnable\"";
				html += " onMouseOver=\"this.className='WDSCalendarPanelMonthEnableOver'\"";
				html += " onMouseOut=\"this.className='WDSCalendarPanelMonthEnable'\"";
				html += " onClick=\"" + this.id + ".setPopupMonthYear(" + temp.getMonth() + ", " + temp.getFullYear() + ")\""
			}
			html += " title=\"" + temp.getFullMonth() + " " + temp.getFullYear() + "\"";
		} else {
			html += " class=\"WDSCalendarPanelMonthDisable\"";
		}
		html += ">";
		html += temp.getShortMonth();
		html += "</td>";
		temp.setMonth(temp.getMonth() + 1);
		if (++i%7 == 0) {
			html += "</tr><tr>";
		}
	}
	html += "</tr></table>";
	return html;
}


/* getPanelMonthsHtmlOneMonth */
/* * getPanelMonthsHtmlOneMonth */
WDSCalendar.prototype.getPanelMonthsHtmlOneMonth = function () {
	var temp = new Date(this.selectedDate);
	
	var html = "<table class=WDSCalendarPanelMonth cellspacing=0 cellpadding=0 width=100%><tr>";
	html += "<td";
	temp.setDate(15);
	temp.setMonth(this.selectedDate.getMonth() - 1);
	if (temp.monthWithinRanges(this.dateRangesValidity)) {
		html += " class=\"WDSCalendarPanelMonthPreviousEnable\"";
		html += " onMouseOver=\"this.className='WDSCalendarPanelMonthPreviousEnableOver'\"";
		html += " onMouseOut=\"this.className='WDSCalendarPanelMonthPreviousEnable'\"";
		html += " onClick=\"" + this.id + ".setPopupMonthYear(" + temp.getMonth() + ", " + temp.getFullYear() + ")\"";
	} else {
		html += " class=\"WDSCalendarPanelMonthPreviousDisable\"";
	}
	html += ">" + WDSCalendar.PanelMonthPreviousMonth + "</td>";
	html += "<td class=\"WDSCalendarPanelMonthActive\">";
	html += this.selectedDate.format(WDSCalendar.formatOneMonthDisplayValue);
	html += "</td>";
	html += "<td";
	temp = new Date(this.selectedDate);
	temp.setMonth(this.selectedDate.getMonth() + 1);
	if (temp.monthWithinRanges(this.dateRangesValidity)) {
		html += " class=\"WDSCalendarPanelMonthNextEnable\"";
		html += " onMouseOver=\"this.className='WDSCalendarPanelMonthNextEnableOver'\"";
		html += " onMouseOut=\"this.className='WDSCalendarPanelMonthNextEnable'\"";	
		html += " onClick=\"" + this.id + ".setPopupMonthYear(" + temp.getMonth() + ", " + temp.getFullYear() + ")\"";
	} else {
		html += " class=\"WDSCalendarPanelMonthNextDisable\"";
	}
	html += ">" + WDSCalendar.PanelMonthNextMonth + "</td>";
	html += "</tr></table>";
	return html;
}


/* getPanelMonthsHtmlSelectMonth */
/* * getPanelMonthsHtmlSelectMonth */
WDSCalendar.prototype.getPanelMonthsHtmlSelectMonth = function () {
	var temp = new Date(this.selectedDate);
	var objMonths = document.getElementById(this.id + WDSCalendar.MONTHYEAR + "Id");
	var html = "<select name=\"popupMonth\" ";
	html += "onChange=\"" + this.id + ".setPopupMonthYear((parseInt(this.value.substr(0, 2), 10) - 1), parseInt(this.value.substr(2, 4), 10))\"";
	html += ">";
	for (var i = 0; i < objMonths.options.length; i++) {
		html += "<option value=\"" + objMonths.options[i].value + "\"";
		if (temp.format(WDSCalendar.formatMonthYearCode) == objMonths.options[i].value) {
			html += " selected";
		}
		html += ">";
		html += objMonths.options[i].text;
		html += "</option>";
	}
	html += "</select>";
	return html;
}



/* getPanelDateHtml */
/* * getPanelDateHtml */
WDSCalendar.prototype.getPanelDateHtml = function () {
	var html = "<table class=\"WDSCalendarPanelDate\" width=\"100%\" cellspacing=1><tr>";
	html += "<tr><td>";
	html += this.selectedDate.format(WDSCalendar.PanelFormatDate);
	html += "</tr></table>"
	return html;
}


/* getPanelCalendarHtml */
/* * getPanelCalendarHtml */
WDSCalendar.prototype.getPanelCalendarHtml = function () {
	var daysOffset = 1;
	
	var html = "<table class=\"WDSCalendarPanelCalendar\" width=\"100%\" cellspacing=0 cellpadding=0>";
	html += "<tr>";	
	for (var dayIndex = daysOffset; dayIndex < (7+daysOffset); ++dayIndex) {
		html += "<td class=\"WDSCalendarPanelCalendarHeader\">" + Date.listShortDays.get(dayIndex % 7) + "</td>";
	}	
	html += "</tr>";

	var temp = new Date(this.selectedDate);
	temp.setDate(1);
	temp.setHours(0,0,0,0);
	var startDay = temp.getDay();
	for (var i = -1; i < 6; ++i) {
		var rowHtmlEmpty = true;
		rowHtml = "<tr>";
		for (var j = 0; j < 8; ++j) {
			if ( (j + 7 * i + daysOffset) >= startDay && temp.getMonth() == this.selectedDate.getMonth() && j!=7) {
				rowHtml += "<td";
				if (this.dateRangesValidity == null || temp.dateWithinRanges(this.dateRangesValidity)) {
					if (temp.getDate()     == this.selectedDate.getDate() &&
							temp.getMonth()    == this.selectedDate.getMonth() &&
							temp.getFullYear() == this.selectedDate.getFullYear() ) {
						rowHtml += " class=\"WDSCalendarPanelCalendarActive\"";
						rowHtml += " onMouseOver=\"this.className='WDSCalendarPanelCalendarEnableOverActive'\"";
						rowHtml += " onMouseOut=\"this.className='WDSCalendarPanelCalendarActive'\"";
						rowHtml += " onClick=\"" + this.id + ".setPopupDate(" + temp.getDate() + ")\"";
					} else {
						rowHtml += " class=\"WDSCalendarPanelCalendarEnable\"";
						rowHtml += " onMouseOver=\"this.className='WDSCalendarPanelCalendarEnableOver'\"";
						rowHtml += " onMouseOut=\"this.className='WDSCalendarPanelCalendarEnable'\"";
						rowHtml += " onClick=\"" + this.id + ".setPopupDate(" + temp.getDate() + ")\"";
					}
				} else {
					rowHtml += " class=\"WDSCalendarPanelCalendarDisable\"";
				}
				rowHtml += "><div>" + temp.getDate() + "</div></td>";
				temp.setDate((j + 7 * i + daysOffset) + 2 - startDay);
				rowHtmlEmpty = false;
			} else {
				rowHtml += "<td class=\"WDSCalendarPanelCalendarSpacer\">&nbsp;</td>";
			}
		}
		rowHtml += "</tr>";
		if (!rowHtmlEmpty) {
			html += rowHtml;
		}
	}
	
	html += "</table>";
	return html;
}


/* getPanelActionHtml */
/* * getPanelActionHtml */
WDSCalendar.prototype.getPanelActionHtml = function () {
	var html = "<img src='CUI10_LH_V4.4_144_040210/img/blank.gif' width=1 height=1 border=0 /><table class=\"WDSCalendarPanelAction\" width=\"100%\" cellspacing=1>";
	html += "<tr>";
	
	/* close */
	html += "<td ";
	html += " class=\"WDSCalendarPanelActionClose\"";
	html += " onMouseOver=\"this.className='WDSCalendarPanelActionCloseOver'\"";
	html += " onMouseOut=\"this.className='WDSCalendarPanelActionClose'\"";
	html += " onClick=\"" + this.id + ".actionPanelClose()\"";
	html += ">" + this.actionClose + "</td>"
	html += "<td ";
	html += " class=\"WDSCalendarPanelActionChange\"";
	html += " onMouseOver=\"this.className='WDSCalendarPanelActionChangeOver'\"";
	html += " onMouseOut=\"this.className='WDSCalendarPanelActionChange'\"";
	html += " onClick=\"" + this.id + ".actionPanelChange()\"";
	html += ">" + this.actionChange + "</td>"
	html += "</tr></table>";
	return html;
}


/* setPopupDate */
/* * setPopupDate */
WDSCalendar.prototype.setPopupDate = function (date) {
	/* Set popupdate */
	this.selectedDate.setDate(date);
	/* Insert Date into hidden or visible fields */
	this.updateFromPopup(this.selectedDate);
	/* Update the date and make sure it is valid	 */
	this.update();
	/* Update the popup with the new date ( Generate new HTML ) */
	this.updatePopup();
	
	/* Close the calendar only if we have no action panel. */
	if (this.actionPanelPresent == false) {
		if (WDSCalendar.DisplayLinkedCalendar) {
			if (this.linkCalendar != null) {
				this.linkCalendar.close();
			} else if (this.parentCalendar != null) {
				this.parentCalendar.close();
			}
		} 
		this.close();
	}
}


/* setPopupMonthYear */
/* * setPopupMonthYear */
WDSCalendar.prototype.setPopupMonthYear = function (month, year) {
	this.selectedDate.setFullYear(year);
	this.selectedDate.setMonth(month);
	/* Pull date within the correct month */
	while(month != this.selectedDate.getMonth()) {
		this.selectedDate.setTime(this.selectedDate.getTime()-24 * 60 * 60 * 1000);
	}	
	/* Verify date is within range - if not pull within */
	if (this.selectedDate.getTime() < this.dateRangesValidity[0].from.getTime()) {
		this.selectedDate.setTime(this.dateRangesValidity[0].from.getTime());
	}	else if (this.selectedDate.getTime() > this.dateRangesValidity[this.dateRangesValidity.length-1].to.getTime()) {
		this.selectedDate.setTime(this.dateRangesValidity[this.dateRangesValidity.length-1].to.getTime());
	}
	/* Insert Date into hidden or visible fields */
	this.updateFromPopup(this.selectedDate);
	/* Update the date and make sure it is valid */
	this.update();
	/* Update the popup with the new date ( Generate new HTML ) */
	this.updatePopup();
}


/* popupToggle */ 
/* * popupToggle popupcalendar */
WDSCalendar.prototype.popupToggle = function () {
	if (this.popupOpened) {
		this.close();
	} else if(this.IsDisabled == false) {
		this.open();
	}
}


/* open */
/* * get the div pop up object (the pop up calendar) */

WDSCalendar.prototype.open = function () {
	this.updatePopup();
	this.getPopupObject().style.visibility = 'hidden';
	this.getPopupObject().style.display = '';
	
	this.popupX = 0;
	this.popupY = this.getObject().offsetHeight + 1;
	var temp = this.getObject();
	while (temp.offsetParent != null) {
		this.popupX += temp.offsetLeft;
		this.popupY += temp.offsetTop;
		temp = temp.offsetParent;
	}
	this.popupX += this.getObject().offsetWidth - this.getPopupObject().offsetWidth;
	
	this.getPopupObject().style.left = this.popupX + WDSCalendar.PopupOffsetX + "px";
	this.getPopupObject().style.top  = this.popupY + WDSCalendar.PopupOffsetY + "px";
	/* popup status */
	this.popupOpened = true;

	/* Bug IE concerning select zindex */
	if (WDSBrowser.ie && !WDSBrowser.opera && !WDSBrowser.mac) {
		this.getPopupObject().firstChild.style.width  = this.getPopupObject().offsetWidth + "px";
		this.getPopupObject().firstChild.style.height = this.getPopupObject().offsetHeight + "px";	
		this.getPopupObject().style.zIndex = "99";
	}
	this.getPopupObject().style.visibility = 'visible';

}


/* close */
/* * close the pop up */
WDSCalendar.prototype.close = function () {
	this.getPopupObject().style.display = 'none';
	/* popup status */
	this.popupOpened = false;
	if (this.parentCalendar != null) {
		this.parentCalendar.popupOpened = false;
	}
}


/* actionClose */
/*    - Called when the user presses the 'close' action */
/* * Executes the callback + closes the popup */
WDSCalendar.prototype.actionPanelClose = function () {
	if (this.closeCallback != null ) {
		this.closeCallback();
	}
	this.close();
}


/* actionChange */
/*    - Called when the user presses the 'change' action */
/* * Executes the callback + closes the popup */
WDSCalendar.prototype.actionPanelChange = function () {
	if (this.changeCallback != null ) {
		this.changeCallback();
	}
	this.close();
}


/* DateRange */
/*    - from  : from date */
/*    - to    : to date */
/* * constructor */
function DateRange (from, to) {
	this.from = from;
	this.to   = to;
}


/* toString */
/* * toString method */
DateRange.prototype.toString = function () {
	return ("\nfrom : " + this.from.toString() + "\nto   : " + this.to.toString());
}


/* withinRange */
/*    - myDate */
/* * return if the date entered is in the range. */
DateRange.prototype.withinRange = function (myDate) {
	return ( 
		(this.from.getTime() <= myDate.getTime()) &&
		(myDate.getTime() <= this.to.getTime()) );
}


/* intersectsRange */
/*    - myDateRange  : dateRange */
/* * What does the method do ... */
DateRange.prototype.intersectsRange = function (myDateRange) {
	return (
		(myDateRange.from.getTime() <= this.to.getTime() && this.to.getTime() <= myDateRange.to.getTime()) ||
		(myDateRange.from.getTime() <= this.from.getTime() && this.from.getTime() <= myDateRange.to.getTime()) );
}


/* intersectsRanges */
/*    - myDateRangeArray : date Range Array */
/* * What does the method do ... */
DateRange.prototype.intersectsRanges = function (myDateRangeArray) {
	for (var i = 0; i < myDateRangeArray.length; ++i) {
		if (this.intersectsRange(dateRangeArray[i])) {
			return true;
		}
	}
	return false;
}


/* override of Date Javascript Object */
/* to populate in your jsp file */
Date.listDays        = new WDSList("DAYS");
Date.listShortDays   = new WDSList("SHORT_DAYS");
Date.listMonths      = new WDSList("MONTHS");
Date.listShortMonths = new WDSList("SHORT_MONTHS");
Date.listHours       = new WDSList("HOURS");


/* padded */
/*    - value  :  */
/* * ?? */
Date.prototype.padded = function (value) {
	return (value <10 ? ('0' + value):value.toString());
}


/* getFullMonth */
/* * return the full month value */
Date.prototype.getFullMonth = function () {
	return Date.listMonths.get(this.padded(this.getMonth() + 1));
}


/* getShortMonth */
/* * return the short month value */
Date.prototype.getShortMonth = function () {
	return Date.listShortMonths.get(this.padded(this.getMonth() + 1));
}


/* getFullDay */
/* * return the full day value */
Date.prototype.getFullDay = function () {
	return Date.listDays.get(this.getDay());
}


/* getShortDay */
/* * return the short day value */
Date.prototype.getShortDay = function () {
	return Date.listShortDays.get(this.getDay());
}


/* format */
/*    - formatString  : the string format */
/* * What does the method do ... */
Date.prototype.format = function (formatString) {
	var s = new String(formatString);
	s = s.replace('HH',   this.padded(this.getHours()));
	s = s.replace('H',    this.getHours().toString());
	s = s.replace('MM',   this.padded(this.getMinutes()));
	s = s.replace('M',    this.getMinutes().toString());
	s = s.replace('SS',   this.padded(this.getSeconds()));
	s = s.replace('S',    this.getSeconds().toString());
	s = s.replace('YYYY', this.getFullYear().toString());
	s = s.replace('YY',   this.getFullYear().toString().substring(2,4));
	s = s.replace('DDDD', this.getFullDay());
	s = s.replace('DDD',  this.getShortDay());
	s = s.replace('DD',   this.padded(this.getDate()));
	s = s.replace('D',    this.getDate().toString());
	s = s.replace('mmmm', this.getFullMonth());
	s = s.replace('mmm',  this.getShortMonth());
	s = s.replace('mm',   this.padded(this.getMonth() + 1));
	/* s = s.replace('m', (this.getMonth() +1 ).toString()); // Fails on ...ember months. */
	return s;
}


/* withinRange */
/*    - myDateRange  : my Date Range */
/* * return if the date is in the date range. */
Date.prototype.withinRange = function (myDateRange) {
	return (
		myDateRange.from.getTime() <= this.getTime() &&
		this.getTime() <= myDateRange.to.getTime() );
}


/* withinRanges */
/*    - myDateRangeArray  : my Date Range Array */
/* * return if the date is in the date range array */
Date.prototype.withinRanges = function (myDateRangeArray) {
	for (var i = 0; i < myDateRangeArray.length; ++i) {
		if (this.withinRange(myDateRangeArray[i])) {
			return true;
		}
	}
	return false;
}


/* dateWithinRange */
/*    - myDateRange : my date range */
/* * What does the method do ... */
Date.prototype.dateWithinRange = function (myDateRange) {
	var from = new Date(myDateRange.from.getTime());
	var to   = new Date(myDateRange.to.getTime());
	from.setHours(0, 0, 0, 0);
	to.setHours(23, 59, 59, 999);
	return (from.getTime() <= this.getTime()&&this.getTime()<=to.getTime());
}


/* dateWithinRanges */
/*    - dateRangeArray  : my date range array */
/* * What does the method do ... */
Date.prototype.dateWithinRanges = function (dateRangeArray) {
	for (var i = 0; i < dateRangeArray.length; ++i) {
		if (this.dateWithinRange(dateRangeArray[i])) {
			return true;
		}
	}
	return false;
}


/* monthWithinRange */
/*    - dateRange  : date range */
/* * What does the method do ... */
Date.prototype.monthWithinRange = function (dateRange) {
	var from = new Date(dateRange.from.getTime());
	var to   = new Date(dateRange.to.getTime());
	from.setDate(1);
	from.setHours(0, 0, 0, 0);
	to.setMonth(dateRange.to.getMonth()+1, 1);
	to.setHours(0, 0, 0, 0);
	to.setTime(to.getTime()-1);
	return (from.getTime() <= this.getTime() && this.getTime() <= to.getTime());
}


/* monthWithinRanges */
/*    - dateRangeArray  : the date range array */
/* * What does the method do ... */
Date.prototype.monthWithinRanges = function (dateRangeArray) {
	for (var i = 0; i < dateRangeArray.length; ++i) {
		if (this.monthWithinRange(dateRangeArray[i])) {
			return true;
		}
	}
	return false;
}