/*=============================================================================
                                     opcssCalendar
-------------------------------------------------------------------------------
Last version : http://www.wipix.com/calendar/
=============================================================================*/
var popupactive = 0;





function opcssCalendar(sJson) {
  if (document.getElementById && document.createElement && document.body && document.body.appendChild) {
  	// Analysis of parameters
    this.parameters = eval('(' + sJson + ')');
    if (xDef(this.parameters.labels)) {
			if (!xDef(this.parameters.labels.title))
				this.parameters.labels.title = "";
			if (!xDef(this.parameters.labels.monthList))
				this.parameters.labels.monthList = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
			if (!xDef(this.parameters.labels.monthShortList)) 
				this.parameters.labels.monthShortList = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
			if (!xDef(this.parameters.labels.dayList))
				this.parameters.labels.dayList = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
			if (!xDef(this.parameters.labels.dayShortList))
				this.parameters.labels.dayShortList = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
			if (!xDef(this.parameters.labels.close))
				this.parameters.labels.close = "Close";
			}
			else {
				this.parameters.labels = new Object();
				this.parameters.labels.title = "";
				this.parameters.labels.dayList = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
				this.parameters.labels.dayShortList = new Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun");
				this.parameters.labels.monthList = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
				this.parameters.labels.monthShortList = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
				this.parameters.labels.close = "Close";
			}
    if (xDef(this.parameters.behavior)) {
			if (!xDef(this.parameters.behavior.outputFormat))
				this.parameters.behavior.outputFormat = "[dd]/[mm]/[yy]";
			if (!xDef(this.parameters.behavior.showClose))
				this.parameters.behavior.showClose = true;
			if (!xDef(this.parameters.behavior.autoClose))
				this.parameters.behavior.autoClose = false;
			if (!xDef(this.parameters.behavior.dateTarget))
				this.parameters.behavior.dateTarget = null;
			if (!xDef(this.parameters.behavior.selectedDate))
				this.parameters.behavior.selectedDate = "";
			if (!xDef(this.parameters.behavior.execScript))
				this.parameters.behavior.execScript = null;
			if (!xDef(this.parameters.behavior.execPage))
				this.parameters.behavior.execPage = null;
			if (!xDef(this.parameters.behavior.notBefore))
				this.parameters.behavior.notBefore = null;
			if (!xDef(this.parameters.behavior.notAfter))
				this.parameters.behavior.notAfter = null;
			if (!xDef(this.parameters.behavior.enableDrag))
				this.parameters.behavior.enableDrag = false;
			if (!xDef(this.parameters.behavior.firstDay))
				this.parameters.behavior.firstDay = "M";
			if (!xDef(this.parameters.behavior.showYearBefore))
				this.parameters.behavior.showYearBefore = 2;
			if (!xDef(this.parameters.behavior.showYearAfter))
				this.parameters.behavior.showYearAfter = 5;
        
      if (this.parameters.behavior.showYearBefore > 5)
        this.parameters.behavior.showYearBefore = 5;
      if (this.parameters.behavior.showYearAfter > 5)
        this.parameters.behavior.showYearAfter = 5;
		}
		else {
			this.parameters.behavior = new Object();
			this.parameters.behavior.outputFormat = "[mm]/[dd]/[yy]";
			this.parameters.behavior.showClose = true;
			this.parameters.behavior.autoClose = true;
	    this.parameters.behavior.dateTarget = null;
			this.parameters.behavior.selectedDate = "";
			this.parameters.behavior.execScript = null;
			this.parameters.behavior.execPage = null;
			this.parameters.behavior.notBefore = "0000/00/00";
			this.parameters.behavior.notAfter = "9999/99/99";
			this.parameters.behavior.enableDrag = false;
			this.parameters.behavior.firstDay = "M";
			this.parameters.behavior.showYearBefore = 2;
			this.parameters.behavior.showYearAfter = 5;
		}

		this.parameters.vars = new Object();
		this.parameters.vars.monthLength = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30, 31);
    this.parameters.vars.outputDate = "";

    d = new Date();

    this.parameters.vars.curYear = d.getFullYear();
    this.parameters.vars.curMonth = d.getMonth();
    this.parameters.vars.curDay = d.getDay();

		// Adjust days depending on the selected first day of week
		if (this.parameters.behavior.firstDay == "S") {
			dummy = this.parameters.labels.dayList.pop();
			this.parameters.labels.dayList = this.parameters.labels.dayList.reverse();
			this.parameters.labels.dayList.push(dummy);
			this.parameters.labels.dayList = this.parameters.labels.dayList.reverse();

			dummy = this.parameters.labels.dayShortList.pop();
			this.parameters.labels.dayShortList = this.parameters.labels.dayShortList.reverse();
			this.parameters.labels.dayShortList.push(dummy);
			this.parameters.labels.dayShortList = this.parameters.labels.dayShortList.reverse();
		}
		
    // Create main DIV container
    var e = document.createElement('DIV');
    this.ele = e;
    e.id = this.parameters.tech.divId;
    e.name = this.parameters.tech.divId;
    e.className = this.parameters.tech.divStyle;

		if (xDef(this.parameters.tech.parentId))    {
			eParent = xGetElementById(this.parameters.tech.parentId);
			xAppendChild(eParent, e);
		}
		else {
	    document.body.appendChild(e);
	  }
    this.open = false;
    xHide(e);
    
    // Positionning of the element
    if (xDef(this.parameters.display)) {
    	if (!xDef(this.parameters.display.mode))
    		this.parameters.display.mode = "generic";
    		
			if (this.parameters.display.mode == "css") {
				e.style.position = this.parameters.display.method;
				xTop(e, this.parameters.display.top);
				xLeft(e, this.parameters.display.left);
			}
			else if (this.parameters.display.mode == "element") {
				if (!xDef(this.parameters.display.margin))
					this.parameters.display.margin = 0;
				this.parameters.display.method = "absolute";
				e.style.position = this.parameters.display.method;
				switch (this.parameters.display.side) {
					case "right"	:
						x = xPageX(this.parameters.display.elementId) - xWidth(e) - this.parameters.display.margin;
						y = xPageY(this.parameters.display.elementId);
						break;
					case "left"		:
						x = xPageX(this.parameters.display.elementId) + xWidth(this.parameters.display.elementId) + this.parameters.display.margin;
						y = xPageY(this.parameters.display.elementId);
						break;
					case "top"		: 
						x = xPageX(this.parameters.display.elementId);
						y = xPageY(this.parameters.display.elementId) - xHeight(e) - this.parameters.display.margin;
						break;
					case "bottom"	:
						x = xPageX(this.parameters.display.elementId);
						y = xPageY(this.parameters.display.elementId) + xHeight(this.parameters.display.elementId) + this.parameters.display.margin;
				}
				xTop(e, y);
				xLeft(e, x);
			}
			else if (this.parameters.display.mode == "screen") {
				this.parameters.display.method = "absolute";
				e.style.position = this.parameters.display.method;
				xTop(e, (xClientHeight() + this.parameters.display.top));
				xLeft(e, (xClientWidth() + this.parameters.display.left));
			}
			else {
				this.parameters.display.method = "absolute";
				e.style.position = this.parameters.display.method;
				xTop(e, (xClientHeight() - xHeight(e)) / 2);
				xLeft(e, (xClientWidth() - xWidth(e)) / 2);
			}
			if (xDef(this.parameters.display.opacity))
				xOpacity(e, this.parameters.display.opacity);
			else
				xOpacity(e, 1.0);
			if (!xDef(this.parameters.display.widthForMonth))
				this.parameters.display.widthForMonth = 100;
			if (!xDef(this.parameters.display.useImage))
				this.parameters.display.useImage = false;
			if (!xDef(this.parameters.display.shadow))
				this.parameters.display.shadow = null;
			else {
				if (!xDef(this.parameters.display.shadow.color))
					this.parameters.display.shadow.color = "#000000";
				if (!xDef(this.parameters.display.shadow.border))
					this.parameters.display.shadow.border = 5;
				if (!xDef(this.parameters.display.shadow.position))
					this.parameters.display.shadow.position = "BR";
			}
			if (!xDef(this.parameters.display.zIndex))
				this.parameters.display.zIndex = 1000;
		}
		else {
			this.parameters.display = new Object();
			this.parameters.display.mode = "screen";
			this.parameters.display.shadow = null;
			this.parameters.display.zIndex = 1000;
			this.parameters.display.method = "absolute";
			e.style.position = this.parameters.display.method;
			xTop(e, (xClientHeight() - xHeight(e)) / 2);
			xLeft(e, (xClientWidth() - xWidth(e)) / 2);
			this.parameters.display.widthForMonth = 100;
				this.parameters.display.useImage = false;
		}
  }
} 
// end opcssCalendar

// methods
opcssCalendar.prototype.updateDate = function() {
  var e = this.ele;
	var srcCal = "";
	
	// Create title/drag area
	if ( (this.parameters.labels.title.length > 0) || (this.parameters.behavior.enableDrag) ){
		srcCal = srcCal + '<div id="' + this.parameters.tech.divStyle + 'Drag" class="' + this.parameters.tech.divStyle + 'Title">';
		if (this.parameters.labels.title.length > 0)
			srcCal = srcCal + this.parameters.labels.title + '</div>';
		else
			srcCal = srcCal + "&nbsp;</div>";
	}
	
	if (this.parameters.display.useImage) {
		nextCode = '<img src="/modules/davidsoncalendar/rtarrow.gif" alt="&nbsp;&gt;&nbsp;"/>';
		prevCode = '<img src="/modules/davidsoncalendar/ltarrow.gif" alt="&nbsp;&lt;&nbsp;"/>';
	}
	else {
		nextCode = '&nbsp;&gt;&nbsp;';
		prevCode = '&nbsp;&lt;&nbsp;';
	}
	
	// Create year/month table
	srcCal = srcCal + '<div class="' + this.parameters.tech.divStyle + 'Default"><table class="' + this.parameters.tech.divStyle + 'DaysTbl"><tr><td align="right" class="' + this.parameters.tech.divStyle + 'Year" name="prevYear"></td><td width="' + this.parameters.display.widthForMonth + '" class="' + this.parameters.tech.divStyle + 'YearLbl"></td><td align="left" class="' + this.parameters.tech.divStyle + 'Year" name="nextYear"></td></tr><tr><th align="left" class="' + this.parameters.tech.divStyle + 'Month" name="prevMonth">' + prevCode + '</th><th align="center" class="' + this.parameters.tech.divStyle + 'MonthLbl">' + this.parameters.labels.monthList[this.parameters.vars.curMonth] + ' ' + this.parameters.vars.curYear +  '</th><th align="right" class="' + this.parameters.tech.divStyle + 'Month" name="nextMonth">' + nextCode + '</th></tr></table></div>';

	// Create days table
	srcCal = srcCal + '<div class="' + this.parameters.tech.divStyle + 'Days"><table class="' + this.parameters.tech.divStyle + 'DaysTbl" id="' + this.parameters.tech.divStyle + 'Tbl' + this.parameters.tech.jsFunction + '"><tr>';
	for (i = 0; i < 7; i++) 
		srcCal = srcCal + '<th class="' + this.parameters.tech.divStyle + 'DaysHead">' + this.parameters.labels.dayShortList[i] + '</th>';
	srcCal = srcCal + '</tr>';
	
	var firstDay = new Date(this.parameters.vars.curYear, this.parameters.vars.curMonth, 1);
	if ( (firstDay.getDay() == 0) && (this.parameters.behavior.firstDay == "M") )
		idFirstDay = 7;
	else
		idFirstDay = firstDay.getDay();
	var cDay = new Date();
	this.parameters.vars.curDay = cDay.getFullYear().toString() + (cDay.getMonth()+1).toString() + cDay.getDate().toString();
		//
	var dayCount = 1;
	var setFirstDay = false;
	var monthLength = this.parameters.vars.monthLength[this.parameters.vars.curMonth];
	// Adjust february length
	if ( (monthLength == 28) && ((this.parameters.vars.curYear % 4) == 0) )
		monthLength = 29;

  if (this.parameters.behavior.firstDay == "M") {
  	minCount = 1;
  	maxCount = 8;
  }
  else {
  	minCount = 0;
  	maxCount = 7;
  }
  while (dayCount <= monthLength) {
  	srcCal = srcCal + '<tr>';
		for (i = minCount; i < maxCount; i++) {
			editDay = this.parameters.vars.curYear.toString() + (this.parameters.vars.curMonth + 1).toString() + dayCount.toString();
			if (!setFirstDay) {
				if (i == idFirstDay)
					setFirstDay = true;
			}
			if ((dayCount <= monthLength) && (setFirstDay))
			  srcCal = srcCal + '<td>' + (dayCount++) + '</td>';
			else
				srcCal = srcCal + '<td>&nbsp;</td>';
  	}
		srcCal = srcCal + '</tr>';
  }
  
	srcCal = srcCal + '</table></div>';

	// Create close line


  // Fill the element
	e.innerHTML = srcCal;
	
	calRoot = xGetElementById(this.parameters.tech.divId);
	xZIndex(calRoot, this.parameters.display.zIndex);
	
	// Create shadow if needed
	if (this.parameters.display.shadow != null) {
	  if (!xGetElementById(this.parameters.tech.divId + "Shadow")) {
			var eltShadow = document.createElement('DIV');
			xAppendChild(xParent(calRoot), eltShadow);
			eltShadow.id = this.parameters.tech.divId + "Shadow";
			eltShadow.name = this.parameters.tech.divId + "Shadow";
			eltShadow.style.position = this.parameters.display.method;
			xZIndex(eltShadow, this.parameters.display.zIndex - 10);
			if (this.parameters.display.shadow.position == "BR" || this.parameters.display.shadow.position == "R" || this.parameters.display.shadow.position == "TR" )
				xLeft(eltShadow, xLeft(calRoot) + this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "BL" || this.parameters.display.shadow.position == "L" || this.parameters.display.shadow.position == "TL" )
				xLeft(eltShadow, xLeft(calRoot) - this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "T" || this.parameters.display.shadow.position == "B")
				xLeft(eltShadow, xLeft(calRoot));
			if (this.parameters.display.shadow.position == "TL" || this.parameters.display.shadow.position == "T" || this.parameters.display.shadow.position == "TR" )
				xTop(eltShadow, xTop(calRoot) - this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "BL" || this.parameters.display.shadow.position == "B" || this.parameters.display.shadow.position == "BR" )
				xTop(eltShadow, xTop(calRoot) + this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "L" || this.parameters.display.shadow.position == "R")
				xTop(eltShadow, xTop(calRoot));
			xWidth(eltShadow, xWidth(calRoot));
			xHeight(eltShadow, xHeight(calRoot)); 	
			xBackground(eltShadow, this.parameters.display.shadow.color);
			xOpacity(eltShadow, 0.5);
			eltShadow.parameters = this.parameters;
		}
		else {
			eltShadow = xGetElementById(this.parameters.tech.divId + "Shadow");
			xWidth(eltShadow, xWidth(calRoot));
			xHeight(eltShadow, xHeight(calRoot)); 	
		}
	}
	
	// Apply event based actions for years
	var years = xGetElementsByClassName(this.parameters.tech.divStyle + "Year", calRoot);
	years[0].datas = this.parameters;
	years[0].onclick = yearClick;
	years[1].datas = this.parameters;
	years[1].onclick = yearClick;
	
  var yearLbl = xGetElementsByClassName(this.parameters.tech.divStyle + "YearLbl", calRoot);
	yearLbl[0].datas = this.parameters;
	yearLbl[0].onclick = yearSelectionClick;
  
	// Apply event based actions for month
	var months = xGetElementsByClassName(this.parameters.tech.divStyle + "Month", calRoot);
	months[0].datas = this.parameters;
	months[0].onclick = monthClick;
	months[1].datas = this.parameters;
	months[1].onclick = monthClick;

  var monthLbl = xGetElementsByClassName(this.parameters.tech.divStyle + "MonthLbl", calRoot);
	monthLbl[0].datas = this.parameters;
	monthLbl[0].onclick = monthSelectionClick;

	// Apply event based action for close
	if (this.parameters.behavior.showClose) {
		var calClose = xGetElementsByClassName(this.parameters.tech.divStyle + "Close", calRoot);
		calClose[0].datas = this.parameters;
		calClose[0].onclick = closeClick;
	}

	// Apply CSS and event based action for days
	var tbl = xGetElementById(this.parameters.tech.divStyle + "Tbl" + this.parameters.tech.jsFunction);
	xTableIterate(tbl, initTbl, this.parameters);

	function IsNumeric(str) {
		var strValidChars = "0123456789";
		var strChar;
		var result = true;
		
		if (str.length == 0) 
			return false;
		for (i = 0; i < str.length && result == true; i++) {
			strChar = str.charAt(i);
			if (strValidChars.indexOf(strChar) == -1) {
				result = false;
			}
		}
		return result;
	}
	
	function initTbl(obj, isRow, r, c, evtParams) {	  
	  if (!isRow) {
	  	if (IsNumeric(obj.innerHTML)) {
				obj.datas = evtParams;
				classDay = "";
				editDay = obj.datas.vars.curYear.toString() + (obj.datas.vars.curMonth + 1).toString() + obj.innerHTML;
				var activeDay = obj.datas.vars.curYear.toString() + "/";
				if ((obj.datas.vars.curMonth + 1) < 10)
					activeDay = activeDay + "0" + (obj.datas.vars.curMonth + 1).toString() + "/";
				else
					activeDay = activeDay + (obj.datas.vars.curMonth + 1).toString() + "/";
				if (obj.innerHTML.length < 2)
					activeDay = activeDay + "0" + obj.innerHTML;
				else
					activeDay = activeDay + obj.innerHTML;
					
				
				if ( (activeDay < obj.datas.behavior.notBefore) || (activeDay > obj.datas.behavior.notAfter) ){
					obj.className = obj.datas.tech.divStyle + "DayOff";
				}
				else {
						
// RR! this is where we set the class to color code for ID/MP3/Walsh

					if (eventlist[activeDay]) 
						classDay = classDay + obj.datas.tech.divStyle +  "EvtDay ";					
					if (classDay == "")
						obj.className = obj.datas.tech.divStyle + "Day";
					else
						obj.className = classDay;
					obj.onmouseover = tdOver;
					obj.onmouseout = tdOut;
					//obj.onclick = tdClick;
				}
	    }
	  }
	}
	
	function tdOver() {
		popupactive=0;
		xAddClass(this, this.datas.tech.divStyle + "SelDay");
		obj = this;
		classDay = "";
		editDay = obj.datas.vars.curYear.toString() + (obj.datas.vars.curMonth + 1).toString() + obj.innerHTML;
		var activeDay = obj.datas.vars.curYear.toString() + "/";
		if ((obj.datas.vars.curMonth + 1) < 10)
			activeDay = activeDay + "0" + (obj.datas.vars.curMonth + 1).toString() + "/";
		else
			activeDay = activeDay + (obj.datas.vars.curMonth + 1).toString() + "/";
		
		if (obj.innerHTML.length < 2)
			activeDay = activeDay + "0" + obj.innerHTML;
		else
			activeDay = activeDay + obj.innerHTML;
		
// RR! if it's an active event day, fade in the event popup 
		if (eventlist[activeDay]) 
		{
			popupactive = 1;
			xInnerHtml('calendar_popup',eventlist[activeDay]['blurb']);
			xOpacity('calendar_popup',0);
			xZIndex('calendar_popup',9999);			
			xMoveTo('calendar_popup',xPageX(this)+5,xPageY(this)+12);
			xShow('calendar_popup');
			xAniOpacity('calendar_popup',1,150);			
		}
			

				
		
	}
	
	function tdOut() {
		xRemoveClass(this, this.datas.tech.divStyle + "SelDay");
		popupactive=0;		
		setTimeout("hidepopup();",300);		
		
		//setTimeout("xAniOpacity('calendar_popup',0,150)",1000);
		//xAniOpacity('calendar_popup',0,150)
		//xHide('calendar_popup');
		//xZIndex('calendar_popup',999);

	}
	
	function tdClick() {
		var selDay = this.datas.vars.curYear.toString() + "|" + (this.datas.vars.curMonth + 1).toString() + '|' + this.innerHTML;
		eval(this.datas.tech.jsFunction + '.choose("' + selDay +'")');
	}
	
	function yearClick() {
		var yearData = this.innerHTML;
		if ( (yearData.indexOf("&lt;") > -1) || (yearData.indexOf("left") > -1) )
			eval(this.datas.tech.jsFunction + '.setYear(-1)');
		else
			eval(this.datas.tech.jsFunction + '.setYear(1)');
	}
	
	function monthClick() {
		var monthData = this.innerHTML;
		if ( (monthData.indexOf("&lt;") > -1) || (monthData.indexOf("ltarrow") > -1) )
			return eval(this.datas.tech.jsFunction + '.setMonth(-1)');
		else
			eval(this.datas.tech.jsFunction + '.setMonth(1)');
	}

 	function yearSelectionClick(oEvent) {
    calRoot = xGetElementById(this.datas.tech.divId);
    var yearList = xGetElementById(this.datas.tech.divId + "YearLbl");
    var yearLbl = xGetElementsByClassName(this.datas.tech.divStyle + "YearLbl", calRoot); 
    if (yearList) {
      xShow(yearList);
      yearList.innerHTML = "";
    }
    else {
			var yearList = document.createElement('DIV');
			xAppendChild(xParent(calRoot), yearList);
			yearList.id = this.datas.tech.divId + "YearLbl";
			yearList.name = this.datas.tech.divId + "YearLbl";
      yearList.className = this.datas.tech.divStyle + "YearLblList";
			yearList.style.position = "absolute";
			xZIndex(yearList, this.datas.display.zIndex + 10);
    }
    var startFrom = this.datas.behavior.showYearBefore * -1;
    var endAt = this.datas.behavior.showYearAfter;
    var code = "";
    for (i = startFrom; i <= endAt; i++) {
      code = code + '<div class="' + this.datas.tech.divStyle + 'SelectYear">' + (this.datas.vars.curYear + i) + '</div>';
    }
    var e = new xEvent(oEvent);
    yearList.innerHTML = code;
    xLeft(yearList, e.pageX - 16);
    xTop(yearList, e.pageY - 16);
  
  	// Apply event based actions for year list
  	var year = xGetElementById(this.datas.tech.divId + "YearLbl", calRoot);
    year.onmouseout = closeYearList;
    
  	// Apply event based actions for year choise
  	var years = xGetElementsByClassName(this.datas.tech.divStyle + "SelectYear", yearList);
    for (i = startFrom; i <= endAt; i++) {
      years[i - startFrom].datas = this.datas;
      years[i - startFrom].onclick = setYearClick;
      years[i - startFrom].onmouseover = setYearOver;
      years[i - startFrom].onmouseout = setYearOut;
    }
}

 	function monthSelectionClick(oEvent) {
    calRoot = xGetElementById(this.datas.tech.divId);
    var monthList = xGetElementById(this.datas.tech.divId + "MonthLbl");
    var monthLbl = xGetElementsByClassName(this.datas.tech.divStyle + "MonthLbl", calRoot); 
    if (monthList) {
      xShow(monthList);
      monthList.innerHTML = "";
    }
    else {
			var monthList = document.createElement('DIV');
			xAppendChild(xParent(calRoot), monthList);
			monthList.id = this.datas.tech.divId + "MonthLbl";
			monthList.name = this.datas.tech.divId + "MonthLbl";
      monthList.className = this.datas.tech.divStyle + "MonthLblList";
			monthList.style.position = "absolute";
			xZIndex(monthList, this.datas.display.zIndex + 10);
    }
    var code = "";
    for (i = 0; i <= 11; i++) {
      code = code + '<div class="' + this.datas.tech.divStyle + 'SelectMonth">' + this.datas.labels.monthList[i] + '</div>';
    }
    var e = new xEvent(oEvent);
    monthList.innerHTML = code;
    xLeft(monthList, e.pageX - 16);
    xTop(monthList, e.pageY - 16);
  
   	// Apply event based actions for month list
  	var month = xGetElementById(this.datas.tech.divId + "MonthLbl", calRoot);
    month.onmouseout = closeMonthList;
    
// Apply event based actions for month choice
  	var months = xGetElementsByClassName(this.datas.tech.divStyle + "SelectMonth", monthList);
    for (i = 0; i <= 11; i++) {
      months[i].datas = this.datas;
      months[i].onclick = setMonthClick;
      months[i].onmouseover = setMonthOver;
      months[i].onmouseout = setMonthOut;
    }
  }

  function closeYearList(oEvent) {
		var e = new xEvent(oEvent);
		var src = e.target;
		if (src.id != "")
			xHide(this);
  }
  
  function setYearClick() {
		var yearData = this.innerHTML;
    xHide(xParent(this));
		eval(this.datas.tech.jsFunction + '.setYear(' + (yearData - this.datas.vars.curYear) +')');
  }

	function setYearOver() {
		xAddClass(this, this.datas.tech.divStyle + "SelYear");
	}
	
	function setYearOut() {
		xRemoveClass(this, this.datas.tech.divStyle + "SelYear");
	}
	
  function closeMonthList(oEvent) {
		var e = new xEvent(oEvent);
		var src = e.target;
		if (src.id != "")
			xHide(this);
  }
   
  function setMonthClick() {
		var monthData = this.innerHTML;
    var monthId = -1;
    var cpt = 0;
    while (monthId < 0) {
      if (this.datas.labels.monthList[cpt] == monthData)
        monthId = cpt;
      cpt++;
    }
    xHide(xParent(this));
		eval(this.datas.tech.jsFunction + '.setMonth(' + (monthId - this.datas.vars.curMonth) +')');
  }

	function setMonthOver() {
		xAddClass(this, this.datas.tech.divStyle + "SelMonth");
	}
	
	function setMonthOut() {
		xRemoveClass(this, this.datas.tech.divStyle + "SelMonth");
	}
	 
	function closeClick() {
    var yearList = xGetElementById(this.datas.tech.divId + "YearLbl");
    if (yearList) {
      xHide(yearList);
    }
    var monthList = xGetElementById(this.datas.tech.divId + "MonthLbl");
    if (monthList) {
      xHide(monthList);
    }
		eval(this.datas.tech.jsFunction + '.hide()');
	}	

	// Update style for drag/drop event
	if (this.parameters.behavior.enableDrag) {
		var titleArea = xGetElementById(this.parameters.tech.divStyle + "Drag"); 
		titleArea.style.cursor = "move";
		xEnableDrag(titleArea, null, moveCalendar, null);
	}

	function moveCalendar(elt, mdx, mdy) {
		var eltShadow = xGetElementById(xName(e) + "Shadow");
    var yearList = xGetElementById(xName(e) + "YearLbl");
    var monthList = xGetElementById(xName(e) + "MonthLbl");
		var x = xLeft(e) + mdx;
		var y = xTop(e) + mdy;
		if (x < 0) x = 0;
		if (y < 0) y = 0;
		xMoveTo(e, x, y);
    if (yearList) {
      var xYear = xLeft(yearList) + mdx;
      var yYear = xTop(yearList) + mdy;
      xMoveTo(yearList, xYear, yYear);
    }
    if (monthList) {
      var xMonth = xLeft(monthList) + mdx;
      var yMonth = xTop(monthList) + mdy;
      xMoveTo(monthList, xMonth, yMonth);
    }
		if (eltShadow)
			switch(eltShadow.parameters.display.shadow.position) {
				case "TL":
					xMoveTo(eltShadow, x - eltShadow.parameters.display.shadow.border, y - eltShadow.parameters.display.shadow.border);
					break;
				case "T":
					xMoveTo(eltShadow, x, y - eltShadow.parameters.display.shadow.border);
					break;
				case "TR":
					xMoveTo(eltShadow, x + eltShadow.parameters.display.shadow.border, y - eltShadow.parameters.display.shadow.border);
					break;
				case "L":
					xMoveTo(eltShadow, x - eltShadow.parameters.display.shadow.border, y);
					break;
				case "R":
					xMoveTo(eltShadow, x + eltShadow.parameters.display.shadow.border, y);
					break;
				case "BL":
					xMoveTo(eltShadow, x - eltShadow.parameters.display.shadow.border, y + eltShadow.parameters.display.shadow.border);
					break;
				case "B":
					xMoveTo(eltShadow, x, y + eltShadow.parameters.display.shadow.border);
					break;
				case "BR":
					xMoveTo(eltShadow, x + eltShadow.parameters.display.shadow.border, y + eltShadow.parameters.display.shadow.border);
					break;
    	}
	}	
};

// Display calendar
opcssCalendar.prototype.show = function() {
  if (!this.open) {
    this.updateDate();
    var e = this.ele;
	
    if (this.parameters.display.mode == "generic") {
			xTop(e, xScrollTop() + (xClientHeight() - xHeight(e)) / 2);
			xLeft(e, xScrollLeft() + (xClientWidth() - xWidth(e)) / 2);
    }
    if (this.parameters.display.mode == "screen") {
			xTop(e, (xScrollTop() + this.parameters.display.top));
			xLeft(e, (xScrollLeft() + this.parameters.display.left));
    }
    xShow(e);
		if (this.parameters.display.shadow != null) {
			var eltShadow = xGetElementById(this.parameters.tech.divId + "Shadow");
			if (this.parameters.display.shadow.position == "BR" || this.parameters.display.shadow.position == "R" || this.parameters.display.shadow.position == "TR")
				xLeft(eltShadow, xLeft(calRoot) + this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "BL" || this.parameters.display.shadow.position == "L" || this.parameters.display.shadow.position == "TL")
				xLeft(eltShadow, xLeft(calRoot) - this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "T" || this.parameters.display.shadow.position == "B")
				xLeft(eltShadow, xLeft(calRoot));
			if (this.parameters.display.shadow.position == "TL" || this.parameters.display.shadow.position == "T" || this.parameters.display.shadow.position == "TR")
				xTop(eltShadow, xTop(calRoot) - this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "BL" || this.parameters.display.shadow.position == "B" || this.parameters.display.shadow.position == "BR")
				xTop(eltShadow, xTop(calRoot) + this.parameters.display.shadow.border);
			if (this.parameters.display.shadow.position == "L" || this.parameters.display.shadow.position == "R")
				xTop(eltShadow, xTop(calRoot));
	    xShow(eltShadow);
	  }
    this.open = true;
  }
};

// Hide calendar
opcssCalendar.prototype.hide = function() {
  if (this.open) {
    var e = this.ele;
    xHide(e);
		if (this.parameters.display.shadow != null) {
			var eltShadow = xGetElementById(this.parameters.tech.divId + "Shadow");
			xHide(eltShadow);
		}
    this.open = false;
  }
};

// Display calendar
opcssCalendar.prototype.isVisible = function() {
  if (this.open)
    return true;
  else
  	return false;
};

// Selection of the date
opcssCalendar.prototype.choose = function(selDate) {
  if (this.open) {
    var e = this.ele;
    var dateElements = selDate.split("|");
    var dateChoosen = new Date(dateElements[0], dateElements[1] - 1, dateElements[2]);
    if (this.parameters.behavior.firstDay == "M")
	    var dayOfWeek = (dateChoosen.getDay() == 0) ? 7 : dateChoosen.getDay();
	  else
	    var dayOfWeek = dateChoosen.getDay() + 1;
    
    this.parameters.behavior.selectedDate = dateElements[0] + dateElements[1] + dateElements[2];
    this.parameters.vars.outputDate = this.parameters.behavior.outputFormat;
    
    if (this.parameters.vars.outputDate.indexOf("[dddd]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[dddd]", this.parameters.labels.dayList[dayOfWeek - 1]);
    if (this.parameters.vars.outputDate.indexOf("[ddd]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[ddd]", this.parameters.labels.dayShortList[dayOfWeek - 1]);
    if (this.parameters.vars.outputDate.indexOf("[mmmm]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[mmmm]", this.parameters.labels.monthList[dateElements[1] - 1]);
    if (this.parameters.vars.outputDate.indexOf("[mmm]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[mmm]", this.parameters.labels.monthShortList[dateElements[1] - 1]);
    if ( (this.parameters.vars.outputDate.indexOf("[dd]") > -1) && (dateElements[2].length == 1) )
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[dd]", "0" + dateElements[2]);
   	else
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[dd]", dateElements[2]);
    if (this.parameters.vars.outputDate.indexOf("[d]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[d]", dateElements[2]);
    if ( (this.parameters.vars.outputDate.indexOf("[mm]") > -1) && (dateElements[1].length == 1) )
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[mm]", "0" + dateElements[1]);
   	else
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[mm]", dateElements[1]);
    if (this.parameters.vars.outputDate.indexOf("[m]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[m]", dateElements[1]);
    if (this.parameters.vars.outputDate.indexOf("[yy]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[yy]", dateElements[0]);
    if (this.parameters.vars.outputDate.indexOf("[y]") > -1)
   		this.parameters.vars.outputDate = this.parameters.vars.outputDate.replace("[y]", dateElements[0].substring(2,4));

    this.updateDate();
    if (this.parameters.behavior.dateTarget) {
			var tgt = xGetElementById(this.parameters.behavior.dateTarget);
			if (xDef(tgt.value))
				tgt.value = this.parameters.vars.outputDate;
			else
				tgt.innerHTML = this.parameters.vars.outputDate;
    }
    if (this.parameters.behavior.autoClose) {
      xHide(e);
      this.open = false;
    }
		if (this.parameters.behavior.execScript) {
			eval(this.parameters.behavior.execScript + "('" + this.parameters.vars.outputDate + "')");    
		}
		if (this.parameters.behavior.execPage) {
			window.document.location = this.parameters.behavior.execPage + "cssCal=" + this.parameters.vars.outputDate;    
		}
  }
};

// Update year
opcssCalendar.prototype.setYear = function(delta) {
  d = new Date((this.parameters.vars.curYear + delta), this.parameters.vars.curMonth, 1);
	this.parameters.vars.curYear = d.getFullYear();
	this.parameters.vars.curMonth = d.getMonth();
	this.parameters.vars.curDay = d.getDay();
	this.updateDate();
};

// Update month
opcssCalendar.prototype.setMonth = function(delta) {
  d = new Date(this.parameters.vars.curYear, (this.parameters.vars.curMonth + delta), 1);
	this.parameters.vars.curYear = d.getFullYear();
	this.parameters.vars.curMonth = d.getMonth();
	this.parameters.vars.curDay = d.getDay();
	davidson_loadlist(d);
	this.updateDate();
};

// Return selected date
opcssCalendar.prototype.getSelectedDate = function() {
  if (this.parameters.vars.outputDate != "")
	  return this.parameters.vars.outputDate;
	else
		return "NODATE";
};

// Set the lower limit for date available for selection
opcssCalendar.prototype.setNotBefore = function(lowLimit) {
	this.parameters.behavior.notBefore = lowLimit;
};

// Set the upper limit for date available for selection
opcssCalendar.prototype.setNotAfter = function(upLimit) {
	this.parameters.behavior.notAfter = upLimit;
};




function pausecmp(millis)
{
var date = new Date();
var curDate = null;
do { curDate = new Date(); }
while(curDate-date < millis);
} 




function hidepopup()
{
		if (popupactive == 0) {
			xAniOpacity('calendar_popup',0,150)
			xZIndex('calendar_popup',999);
			//setTimeout("xMoveTo('calendar_popup',0,0);",150);			
		} else if (xOpacity('calendar_popup') == 0) {
			popupactive == 0;
		} else {
			setTimeout("hidepopup();",200);
		}
		
}

function davidson_loadlist(d)
{

	year = d.getFullYear();
	month = d.getMonth();
	$("#loadcal").text("Loading...");
	$("#loadcal").load("?m=" + month + "&y=" + year);
}
