// DHTML Calendar
// $Author: Karl Agius $
// $Date: 2005/02/15 21:54:32 $
// $Revision: 1.1 $

function Calendar (cname, id, date, pageId)
{
	// Used to notify the calendar that it is attached to a single html field.
	this.fallback_single = 0;

	// Used to notify the claendar that it is attached to 3 html fields.
	this.fallback_multi = 1;

	// Used to notify the calendar that it is attached to both field sets.
	this.fallback_both = 2;

	// Read-only calendar
	this.viewOnly = false;

	// Allows the user to select weekends
	this.allowWeekends = true;

	// Allows the user to select weekdays
	this.allowWeekdays = true;

	// The minimum date that the user can select (inclusive)
	this.minDate = "--";

	// The maximum date that the user can select (exclusive)
	this.maxDate = "--";

	// Allow the user to scroll dates
	this.scrolling = true;

	// The id of this calendar
	this.name = cname;

	// The first day of the week in the calendar (0-Sunday, 6-Saturday)
	this.firstDayOfWeek = 0;

	// Fallback method
	this.fallback = this.fallback_single;

	// Sets the date and strips out time information
	this.calendarDate = date;
	//alert(this.calendarDate);
	this.calendarDate.setUTCHours(0);
	this.calendarDate.setUTCMinutes(0);
	this.calendarDate.setUTCSeconds(0);
	this.calendarDate.setUTCMilliseconds(0);

	this.selectedDate = new Date();
	this.selectedDate.setDate(this.calendarDate.getDate());

	// The field id that the calendar is attached to.
	// For single input, this is used "as is". for the
	// Multi-input, it is given a suffix for _day, _month
	// and _year inputs.
	this.attachedId = id;
	//alert(this.attachedId);

	// The left and right month control icons
	this.controlLeft = "&#171;";
	this.controlRight = "&#187;";

	// The left and right month control icons (when disabled)
	this.controlLeftDisabled = "";
	this.controlRightDisabled = "";

	// The css classes for the calendar and header
	this.calendarStyle = "cal_calendar";
	this.headerStyle = "cal_header";
	this.headerCellStyle = "cal_cell";
	this.headerCellStyleLabel = "cal_labelcell";
	this.daysHeaderStyle = "cal_days_header";
	this.daysHeaderCellStyle = "cal_days_cell";
	this.daysTableStyle = "cal_days_table";
	this.daysTdStyle = "cal_days_td";
	this.daysTrStyle = "cal_days_tr";

	// The css classes for the rows
	this.weekStyle = "cal_week";
	this.evenWeekStyle = "cal_evenweek";
	this.oddWeekStyle = "cal_oddweek";

	// The css classes for the day elements
	this.dayStyle = "cal_day";
	this.disabledDayStyle = "cal_disabled";
	this.commonDayStyle = "cal_common";
	this.holidayDayStyle = "cal_holiday";
	this.eventDayStyle = "cal_event";
	this.todayDayStyle = "cal_today";

	// specifies the labels for this calendar
	this.dayLabels = new Array("Nie", "Pn", "Wt", "&#346;r", "Cz", "Pt", "So");
	this.monthLabels = new Array(
		"Stycze&#324;", "Luty", "Marzec", "Kwiecie&#324;"
		, "Maj", "Czerwiec", "Lipiec", "Sierpie&#324;"
		, "Wrzesie&#324;", "Pa&#378;dziernik", "Listopad", "Grudzie&#324;");

	// Specifies the dates of any event. The events are to be defined as arrays,
	// with element 0 being the date and element 1 being an id.
	this.eventDates = new Array();

	this.pageId  = pageId;

	this.formName = "";

	// Attach event handlers to any fallback fields.
	if (this.viewOnly == false) {
		setFieldValue(this.attachedId, this.calendarDate);


		if ((this.fallback == this.fallback_both) || (this.fallback == this.fallback_single)) {
		//alert('OK');
			eval("document.getElementById(\"" + this.attachedId + "\").onchange = function () {updateFromSingle("+this.name+", this);}");
		}

		if ((this.fallback == this.fallback_both) || (this.fallback == this.fallback_multi)) {
			alert('ERROR');
			eval("document.getElementById(\"" + this.attachedId + "_day\").onchange = function () {updateFromMultiDay("+this.name+", this);}");
			eval("document.getElementById(\"" + this.attachedId + "_month\").onchange = function () {updateFromMultiMonth("+this.name+", this);}");
			eval("document.getElementById(\"" + this.attachedId + "_year\").onchange = function () {updateFromMultiYear("+this.name+", this);}");

		}
	}

	selectEvent = new Function();
}

function updateFromSingle (sender, helper) {
	newDate = new Date (helper.value);
	newDate.setUTCDate(newDate.getUTCDate()+1);
	sender.calendarDate = newDate;

	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function updateFromMultiDay (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getUTCDate();
		return false;
	}

	sender.calendarDate.setUTCDate(helper.value);
	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function updateFromMultiMonth (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getUTCMonths() -1;
		return false;
	}

	sender.calendarDate.setUTCMonth(helper.value-1);
	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function updateFromMultiYear (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getUTCFullYear();
		return false;
	}

	sender.calendarDate.setUTCFullYear(helper.value);
	renderCalendar (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function getFirstCalendarDate (calendar)
{
	return new Date (
		calendar.calendarDate.getUTCFullYear()
		, calendar.calendarDate.getUTCMonth()
		, 1
	);
}

function renderCalendar (calendar)
{
	//calHtml1 = "<div align=\"left\" style=\"border: 2px solid red;\">";
	calHtml1 = ("<table id=\"cal_" + calendar.attachedId + "\" class=\"" + calendar.calendarStyle +"\">");
	calHtml1 += ((calendar.scrolling)?buildHeader(calendar):buildStaticHeader(calendar));
	calHtml1 += buildCalendarTable (calendar);
	calHtml1 += ("</table>");
	calHtml1 += ("<div id=\"cloud\" align=\"left\" style=\"position:absolute;visibility:hidden;\" onmouseout=\"javascript: DrawCloud(event,'',0,0,0);\">");
    calHtml1 += ("<table cellspacing=\"0\" cellpadding=\"0\" id=\"cloudTable\">");
	calHtml1 += ("<tr>");
	calHtml1 += ("<td style=\"width:14px;height:7px;background-image:url('/static/images/calendar/CloudeLeftTop.gif');background-repeat:no-repeat;\"></td>");
	calHtml1 += ("<td style=\"height:7px;background-image:url('/static/images/calendar/CloudeTop.gif');background-repeat:repeat-x;\"></td>");
	calHtml1 += ("<td style=\"width:8px;height:7px;background-image:url('/static/images/calendar/CloudeRightTop.gif');background-repeat:no-repeat;\"></td>");
	calHtml1 += ("</tr>");
	calHtml1 += ("<tr>");
	calHtml1 += ("<td style=\"width:14px;background-image:url('/static/images/calendar/CloudeLeft.gif');background-repeat:repeat-y;\"></td>");
	calHtml1 += ("<td style=\"background-color:white\" align=\"left\" id=\"cloud_text\"></td>");
	calHtml1 += ("<td style=\"width:8px;background-image:url('/static/images/calendar/CloudeRight.gif');background-repeat:repeat-y;\"></td>");
	calHtml1 += ("</tr>");
	calHtml1 += ("<tr>");
	calHtml1 += ("<td style=\"width:14px;height:13px;background-image:url('/static/images/calendar/CloudeLeftBottom.gif');background-repeat:no-repeat;\"></td>");
	calHtml1 += ("<td style=\"height:13px;background-image:url('/static/images/calendar/CloudeBottom.gif');background-repeat:repeat-x;\"></td>");
	calHtml1 += ("<td style=\"width:8px;height:13px;background-image:url('/static/images/calendar/CloudeRightBottom.gif');background-repeat:no-repeat;\"></td>");
	calHtml1 += ("</tr>");
	calHtml1 += ("</table>");
	calHtml1 += ("</div>");
	//calHtml1 += ("</div>");

	document.getElementById("cal_" + calendar.attachedId + "_display").innerHTML = calHtml1;
}

//month scroll
function scrollMonthBack (calendar)
{
	calendar.calendarDate.setUTCDate(1);
	calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() - 1);
	setFieldValue(calendar.attachedId, calendar.calendarDate);
	renderCalendar (calendar);
}

//day select
function selectDate (calendar, day)
{
	if (!calendar.viewOnly) {
		calendar.calendarDate.setUTCDate(day);
		calendar.selectedDate.setUTCDate(day);
		calendar.selectedDate.setUTCMonth(calendar.calendarDate.getUTCMonth());
		setFieldValue(calendar.attachedId, calendar.calendarDate);
		//ustawianie daty w JS calendar - wykonuje sie za kazdym razem jak klikniemy cos w kalendarzu
		//ustaw kliknieta date w formularzu Struts - to trzeba niestety przesłac... :/
		f = document.forms[calendar.formName];
		f.dates.value = calendar.calendarDate.getUTCFullYear() + "/" + (calendar.calendarDate.getUTCMonth()+1) + "/" + calendar.calendarDate.getUTCDate();
		f.selectedDate.value = calendar.selectedDate.getUTCFullYear() + "/" + (calendar.selectedDate.getUTCMonth()+1) + "/" + calendar.selectedDate.getUTCDate();
		f.actionName.value = 'now';
		f.day.value = calendar.calendarDate.getUTCDate();
		f.year.value = calendar.calendarDate.getUTCFullYear();
		f.month.value = (calendar.calendarDate.getUTCMonth()+1);
		f.submit();
		//renderCalendar (calendar);
	}
}

//month scroll
function scrollMonthForward (calendar)
{
	calendar.calendarDate.setUTCDate(1);
	calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() + 1);
	setFieldValue(calendar.attachedId, calendar.calendarDate);
	renderCalendar (calendar);
}

function setFieldValue(fieldId, date) {
	document.getElementById(fieldId).value = date.getUTCFullYear() + "/" + (date.getUTCMonth()+1) + "/" + date.getUTCDate();
}

function buildHeader (calendar)
{

	enableLeft = true;
	enableRight = true;

	if (calendar.minDate != "--")
	{
		if (calendar.calendarDate.getUTCFullYear() <= calendar.minDate.getUTCFullYear())
		{
			if (calendar.calendarDate.getUTCMonth() <= calendar.minDate.getUTCMonth())
			{
				enableLeft = false;
			}
		}
	}

	if (calendar.maxDate != "--")
	{
		if (calendar.calendarDate.getUTCFullYear() >= calendar.maxDate.getUTCFullYear())
		{
			if (calendar.calendarDate.getUTCMonth() >= calendar.maxDate.getUTCMonth())
			{
				enableRight = false;
			}
		}
	}

	calHtml2 = "";

	calHtml2 +=  (
		"<tr id=\"calendarHeaderTr\" class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyle
		+ ((enableLeft)?("\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"scrollMonthBack(" + calendar.name + ")"):"")
		+ "\">"
		+ ((enableLeft)?calendar.controlLeft:calendar.controlLeftDisabled)
		+ "</td>");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getUTCMonth()]
		+ " " + calendar.calendarDate.getUTCFullYear()
		+ "</td>");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyle
		+ ((enableRight)?("\" onmouseover=\"this.style.cursor='pointer'\" onclick=\"scrollMonthForward(" + calendar.name + ")"):"")
		+ "\">"
		+ ((enableRight)?calendar.controlRight:calendar.controlRightDisabled)
		+ "</td>");

	calHtml2 += ("</tr>");

	return calHtml2
}

function buildStaticHeader (calendar)
{
	calHtml2 = "";

	calHtml2 +=  (
		"<tr id=\"calendarHeaderTr\" class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtml2 +=  (
		"<td colspan=\"7\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getUTCMonth()]
		+ ", " + calendar.calendarDate.getUTCFullYear()
		+ "</td>");
	calHtml2 += ("</tr>");

	calHtml2 +=  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">")

	for (i = 0; i < 7; i++) {
		showDay = i + calendar.firstDayOfWeek;
		if (showDay > 6) showDay = showDay - 7;
		calHtml2 +=  (
			"<td class=\""
			+ calendar.headerCellStyle
			+ "\">"
			+ calendar.dayLabels[showDay]
			+ "</td>");
	}

	calHtml2 += ("</tr>");
	return calHtml2
}




function RenderDayDisabled (calendar, currentDate, wolnyDay)
{
	if (!wolnyDay)
		calHtml += ('<td class="day">');
	else
		calHtml += ('<td class="cal_wolny">');
	calHtml += ("<span class=\"" + calendar.disabledDayStyle + "\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function RenderDayEnabled (calendar, currentDate, dayStyle, wolnyDay)
{
	/* to bylo do tej pory ale nie zazanczalo wydarzenia w biezacym dniu
	currentDayStyle = dayStyle;
	if (!wolnyDay)
		calHtml += ('<td class="day">');
	else
		calHtml += ('<td class="cal_wolny">');
	calHtml += ("<span class=\"" + dayStyle + "\">"); //onmouseover=\"this.style.cursor='pointer'\" onclick=\"selectDate(" + calendar.name + ", " + currentDate.getUTCDate() + ")\"
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
	*/

	currentDateString = currentDate.getUTCFullYear() + "/" + (currentDate.getUTCMonth()+1) + "/" + currentDate.getUTCDate();
	isTodayDay = false;
	numberElement = 0;
	for (j=0; j < calendar.eventDates.length; j++)
		{
		if (calendar.eventDates[j][0] == currentDateString) {
			isTodayDay = true;
			numberElement = j;
		}
	}
	if (isTodayDay) {
		calHtml += ('<td class=\"cal_zdarzenie\">');
		calHtml += ("<span class=\"" + dayStyle + "\" onmouseover=\"this.style.cursor='pointer';");
		calHtml +=("\"");
		calHtml += ("onclick=\"selectDate(" + calendar.name + ", " + currentDate.getUTCDate() + "); " + calendar.name + ".selectEvent('" + calendar.eventDates[numberElement][0] + "')\">");
		calHtml += (currentDate.getUTCDate());
		calHtml += ("</span>");
		calHtml += ("</td>");
	} else {
		currentDayStyle = dayStyle;
		if (!wolnyDay)
			calHtml += ('<td class="day">');
		else
			calHtml += ('<td class="cal_wolny">');
		calHtml += ("<span class=\"" + dayStyle + "\">"); //onmouseover=\"this.style.cursor='pointer'\" onclick=\"selectDate(" + calendar.name + ", " + currentDate.getUTCDate() + ")\"
		calHtml += (currentDate.getUTCDate());
		calHtml += ("</span>");
		calHtml += ("</td>");
	}
}

function RenderDayEvent (calendar, currentDate, dayStyle, eventId, cloudContent, row, column)
{
	currentDayStyle = dayStyle;
	calHtml += ("<td class=\"cal_zdarzenie\">"); //onmouseout=\"javascript: DrawCloud(event,'',0,0,0);\"
	calHtml += ("<span class=\"" + dayStyle + "\" onmouseover=\"this.style.cursor='pointer';");
	//if (cloudContent != null && cloudContent.length > 0) {
	//	calHtml += ("DrawCloud(event,'"+RenderCloudContent(cloudContent)+"',1,"+row+","+column+",'"+calendar.attachedId+"');");
	//}
	calHtml +=("\"");
	//if (cloudContent != null && cloudContent.length > 0) {
	//	calHtml += ("onmouseout=\"javascript: DrawCloud(event,'',0,0,0);\"");
	//}
	calHtml += ("onclick=\"selectDate(" + calendar.name + ", " + currentDate.getUTCDate() + "); " + calendar.name + ".selectEvent('" + eventId + "')\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("<img id=\"cloudImg"+row+""+column+"\" style=\"position:absolute;width:0px;height:0px;\"></span>");
	calHtml += ("</td>");
}

function RenderCloudContent(cloudContent) {
	cloudContentString = "";
	for(ias = 0; ias < cloudContent.length; ias++){
		cloudContentString += cloudContent[ias];
		if(ias < cloudContent.length -1){
			cloudContentString += ("<br/><br/>");
		}
	}
	return cloudContentString;
}
function buildCalendarTable (calendar)
{
	currentDate = getFirstCalendarDate(calendar);
	odd = 0;
	while (currentDate.getUTCDay() != calendar.firstDayOfWeek)
	{
		currentDate.setUTCDate(currentDate.getUTCDate() - 1);
	}

	calHtml = "";
	calHtml +=  (
		"<tr class=\""
		+ calendar.daysTrStyle
		+ "\">");
	calHtml += ("<td class=\""
		+ calendar.daysTdStyle
		+ "\" colspan=\"3\">");
	calHtml += ("<table	class=\""
		+ calendar.daysTableStyle
		+ "\">");
	calHtml += ("<tr id=\"calendarDaysHeaderTr\" class=\""
		+ calendar.daysHeaderStyle
		+ "\">");

	for (i = 0; i < 7; i++) {
		showDay = i + calendar.firstDayOfWeek;
		if (showDay > 6) showDay = showDay - 7;
		calHtml +=  (
			"<td id=\"calendarDaysHeaderTd"+i+"\" class=\""
			+ calendar.daysHeaderCellStyle
			+ "\">"
			+ calendar.dayLabels[showDay]
			+ "</td>");
	}

	calHtml += ("</tr>");
	do
	{
		odd += 1;

		calHtml +=  (
			"<tr id=\"calendarDaysTr"+odd+"\" class=\"" + (((odd%2)==0) ? calendar.evenWeekStyle : calendar.oddWeekStyle) + "\">")

		for (i = 0;i < 7;i++)
		{
			currentDayStyle = calendar.dayStyle;
			currentEventStyle = calendar.commonDayStyle;
			currentDateString = currentDate.getUTCFullYear() + "/" + (currentDate.getUTCMonth()+1) + "/" + currentDate.getUTCDate();

			var wolnyDay = false;
			if ((currentDate.getUTCDay() == 0) || (currentDate.getUTCDay() == 6))
				wolnyDay = true;

			if (currentDate < calendar.minDate)
			{
				RenderDayDisabled (calendar, currentDate, wolnyDay);
			}
			else if (currentDate > calendar.maxDate)
			{
				RenderDayDisabled (calendar, currentDate, wolnyDay);
			}
			else if (currentDate.getUTCMonth() != calendar.calendarDate.getUTCMonth())
			{
				RenderDayDisabled (calendar, currentDate, wolnyDay);
			}
			else if ((currentDate.getUTCDate() == calendar.selectedDate.getUTCDate()) &&
				currentDate.getUTCMonth() == calendar.selectedDate.getUTCMonth())
			{
				if (wolnyDay)
				{
					if (calendar.allowWeekends == true)
					{
						RenderDayEnabled (calendar, currentDate, calendar.todayDayStyle, wolnyDay);
					}
					else
					{
						RenderDayDisabled (calendar, currentDate, wolnyDay);
						month = calendar.calendarDate.getUTCMonth();
						calendar.calendarDate.setUTCDate(calendar.calendarDate.getUTCDate()+1);
						if (month != calendar.calendarDate.getUTCMonth())
						{
							renderCalendar(calendar);
						}
						setFieldValue(calendar.attachedId, calendar.calendarDate);
					}
				} else {
					if (calendar.allowWeekdays == true)
					{
						RenderDayEnabled (calendar, currentDate, calendar.todayDayStyle, wolnyDay);
					}
					else
					{
						RenderDayDisabled (calendar, currentDate, wolnyDay);
						month = calendar.calendarDate.getUTCMonth();
						calendar.calendarDate.setUTCDate(calendar.calendarDate.getUTCDate()+1);
						if (month != calendar.calendarDate.getUTCMonth())
						{
							renderCalendar(calendar);
						}
						setFieldValue(calendar.attachedId, calendar.calendarDate);
					}
				}
			}
			else if (wolnyDay)
			{
				if (calendar.allowWeekends == true)
				{

					style = calendar.holidayDayStyle

					for (j=0; j < calendar.eventDates.length; j++)
					{
						if (calendar.eventDates[j][0] == currentDateString)
						{
							style = calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][0], calendar.eventDates[j][2],odd,i);
						}
					}

					if (style == calendar.holidayDayStyle)
					{
						RenderDayEnabled (calendar, currentDate, style, wolnyDay);
					}
				}
				else
				{
					RenderDayDisabled (calendar, currentDate, wolnyDay);
				}
			} else {
				if (calendar.allowWeekdays == true)
				{
					style = calendar.commonDayStyle

					for (j=0; j < calendar.eventDates.length; j++)
					{
						if (calendar.eventDates[j][0] == currentDateString)
						{
							style = calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][0], calendar.eventDates[j][2],odd,i);
						}
					}

					if (style == calendar.commonDayStyle)
					{
						RenderDayEnabled (calendar, currentDate, style, wolnyDay);
					}
				}
				else
				{
					RenderDayDisabled (calendar, currentDate, wolnyDay);
				}
			}

			currentDate.setUTCDate(currentDate.getUTCDate() + 1);
		}

		calHtml += ("</tr>");


	} while (currentDate.getUTCMonth() == calendar.calendarDate.getUTCMonth());
	calHtml += ("</table></td></tr>");
	return calHtml;
}

// $Log: calendar.js,v $
// Revision 1.1  2005/02/15 21:54:32  Karl Agius
// Initial release
//

function DrawCloud(MouseEvent,CloudTitle,Operation,row,column,calendarTableId){
      if(Operation==1){
      	  document.getElementById("cloud_text").innerHTML = CloudTitle;
		  PosX = document.getElementById("cloudImg"+row+""+column).offsetLeft;
		  PosX = PosX + "px";
		  PosY = document.getElementById("cloudImg"+row+""+column).offsetTop;
		  PosY = windowHeight() - PosY;
		  PosY = PosY + "px";
          document.getElementById("cloud").style.left = PosX;
          document.getElementById("cloud").style.bottom = PosY;
          DivWidth = document.getElementById("cloudTable").offsetWidth + 54 + "px";
     	  document.getElementById("cloud").style.width = DivWidth;
          setTimeout("DisplayCloud(1)",100);
      } else if(Operation==0){
      	  DisplayCloud(0);
      }
  }
  function DisplayCloud(Display){
      if(Display==1){
          document.getElementById("cloud").style.visibility = "visible";
      } else if(Display==0){
          document.getElementById("cloud").style.visibility = "hidden";
      }
  }

function windowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = document.documentElement.clientHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight + document.documentElement.scrollTop;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

