﻿// JScript File
// DHTML Calendar
// $Author: Karl Agius $
// $Date: 2005/02/15 21:54:32 $
// $Revision: 1.1 $
function Calendar (cname, id, date)
{
	// 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_both;
	
	// Sets the date and strips out time information
	this.calendarDate = date;
	this.calendarDate.setUTCHours(0);
	this.calendarDate.setUTCMinutes(0);
	this.calendarDate.setUTCSeconds(0);
	this.calendarDate.setUTCMilliseconds(0);
	
	// 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;
	
	this.disableMonthScrolling = false;
	// The left and right month control icons
	this.controlLeft = "&lt;";
	this.controlRight = "&gt;";
		
	// The left and right month control icons (when disabled)
	this.controlLeftDisabled = "";
	this.controlRightDisabled = "";
	
	// The css classes for the calendar and header
	this.calendarStyle = "";
	this.headerStyle = "";
	this.headerCellStyle = "HomeCalLabel";
	this.headerCellStyleLabel = "HomeCalLabelBold";
	
	// The css classes for the rows
	this.weekStyle = "";
	this.evenWeekStyle = "";
	this.oddWeekStyle = "";
	
	// The css classes for the day elements
	this.dayStyle = "HomeCalCSS";
	this.disabledDayStyle = "HomeCalCSSDayDisable";
	this.commonDayStyle = "HomeCalCSS";
	this.holidayDayStyle = "HomeCalCSS";
	this.eventDayStyle = "event_on_Home";
	this.todayDayStyle = "HomeCalCSSToday";
	this.todayEventDayStyle = "HomeCalCSSTodayWithEvent";
	
	// specifies the labels for this calendar
	this.dayLabels = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
	this.monthLabels = new Array(
		"January", "February", "March", "April"
		, "May", "June", "July", "August"
		, "September", "October", "November", "December");
	
	// 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();
	
	// 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)) {
//			eval("document.getElementById(\"" + this.attachedId + "\").onchange = function () {updateFromSingle("+this.name+", this);}");
//		}

//		if ((this.fallback = this.fallback_both) || (this.fallback = this.fallback_multi)) {

//			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;

	renderCalendar1 (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);
	renderCalendar1 (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);
	renderCalendar1 (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);
	renderCalendar1 (sender);
	setFieldValue(sender.attachedId, sender.calendarDate);
}

function getFirstCalendarDate (calendar)
{
	return new Date (
		calendar.calendarDate.getUTCFullYear()
		, calendar.calendarDate.getUTCMonth()
		, 1
	);
}

function renderCalendar (calendar)
{
	calHtml1 =  ("<table id=\"cal_" + calendar.attachedId + "\" class=\"CalenderClass\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\" style=\"border: solid 1px #E6E6E6; background-color: White; border-collapse: collapse;\" title=\"Calendar\">");
	calHtml1 += ((calendar.scrolling)?buildHeader(calendar):buildStaticHeader(calendar));
	calHtml1 += buildCalendarTable (calendar);
	calHtml1 += ("</table>");
	
	document.getElementById("cal_" + calendar.attachedId + "_display").innerHTML = calHtml1;
}

function scrollMonthBack (calendar)
{
    calendar.calendarDate.setUTCDate(1);
	calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() - 1);
	setFieldValue(calendar.attachedId, calendar.calendarDate);
	renderCalendar1 (calendar);
}

function selectDate (calendar, day)
{
	if (!calendar.viewOnly) {
		calendar.calendarDate.setUTCDate(day);
		setFieldValue(calendar.attachedId, calendar.calendarDate);
		renderCalendar1 (calendar);
	}
}

function scrollMonthForward (calendar)
{
    //First set date to 1 of the current month. Because if it's not, it will skip one month in case of 31st day of month.
    //Example:Today is 31-10-2009 and on clicking next month it adds one to the month i.e. 31-11-2009 and which is not a
    //correct date so system automatically adjust it to 01-12-2009. Same is the case of previous month. So first we set the 
    //date to first day of month than add one month to it.
    calendar.calendarDate.setUTCDate(1);
	calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() + 1);
	setFieldValue(calendar.attachedId, calendar.calendarDate);
	renderCalendar1 (calendar);
}

function setFieldValue(fieldId, date) {
//	document.getElementById(fieldId).value = date.getUTCFullYear() + "/" + (date.getUTCMonth()+1) + "/" + date.getUTCDate();
//	document.getElementById(fieldId + "_year").value = date.getUTCFullYear();
//	document.getElementById(fieldId + "_month").selectedIndex = date.getUTCMonth();
//	document.getElementById(fieldId + "_day").value = date.getUTCDate();
}

function buildHeader (calendar)
{
    if(calendar.disableMonthScrolling)
    {
        enableLeft = false;
	    enableRight = false;
    }
    else
    {
        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>");
	calHtml2 +=  (
		"<td "
		+ ((enableLeft)?("class=\"HomeCalLabelPointer\" onclick=\"scrollMonthBack(" + calendar.name + ")\""):"")
		+ ">"
		+ ((enableLeft)?calendar.controlLeft:calendar.controlLeftDisabled)
		+ "</td>");
	calHtml2 +=  (
		"<td colspan=\"5\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getUTCMonth()] 
		+ " " + calendar.calendarDate.getUTCFullYear()
		+ "</td>");
	calHtml2 +=  (
		"<td "
		+ ((enableRight)?("class=\"HomeCalLabelPointer\" onclick=\"scrollMonthForward(" + calendar.name + ")\""):"")
		+ ">"
		+ ((enableRight)?calendar.controlRight:calendar.controlRightDisabled)
		+ "</td>");
	
	calHtml2 += ("</tr>");
	
	calHtml2 +=  ("<tr>")

	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 buildStaticHeader (calendar)
{
	calHtml2 = "";
	
	calHtml2 +=  ("<tr>");
	calHtml2 +=  (
		"<td colspan=\"7\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getUTCMonth()] 
		+ ", " + calendar.calendarDate.getUTCFullYear()
		+ "</td>");	
	calHtml2 += ("</tr>");
	
	calHtml2 +=  ("<tr>")

	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)
{
	calHtml += ("<td class=\"" + calendar.disabledDayStyle + "\">");
	calHtml += ("<span>");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function RenderDayEnabled (calendar, currentDate, dayStyle)
{
    var day = currentDate.getUTCDate();
    var month = currentDate.getMonth()+1;
    var year = currentDate.getFullYear();
    var DateString=month + "/" + day + "/" + year;
    
	currentDayStyle = dayStyle;
	calHtml += ("<td class=\"" + dayStyle + "\">");
	//calHtml += ("<span onclick=\"CalenderDateSelected('" + DateString + "');\">");
	calHtml += ("<span>");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function RenderDayEvent (calendar, currentDate, dayStyle, eventId)
{
    var day = currentDate.getUTCDate();
    var month = currentDate.getUTCMonth()+1;
    var year = currentDate.getUTCFullYear();
    var DateString=month + "/" + day + "/" + year;
	currentDayStyle = dayStyle;
	calHtml += ("<td class=\"" + dayStyle + "\">");
	calHtml += ("<span onclick=\"CalenderDateSelected('" + DateString + "');\" onmouseover=\"ShowResultCount1('" + eventId + "');\" onmouseout=\"javascript:HideResultCount();\" >");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function buildCalendarTable (calendar)
{
	currentDate = getFirstCalendarDate(calendar);
	var todayDate=new Date();
	odd = 0;
	while (currentDate.getUTCDay() != calendar.firstDayOfWeek)
	{
		currentDate.setUTCDate(currentDate.getUTCDate() - 1);
	}

	calHtml = "";
	do
	{
		odd += 1;

		calHtml +=  (
			//"<tr class=\"" + (((odd%2)==0) ? calendar.evenWeekStyle : calendar.oddWeekStyle) + "\">")
            "<tr>")
		for (i = 0;i < 7;i++)
		{
			currentDayStyle = calendar.dayStyle;
			currentEventStyle = calendar.commonDayStyle;
			var currentMonth=currentDate.getUTCMonth()+1;			
			var currentDateValue=currentDate.getUTCDate();
			currentDateString = currentDate.getUTCFullYear() + "/" + currentMonth + "/" + currentDateValue;

			if (currentDate < calendar.minDate) 
			{
				RenderDayDisabled (calendar, currentDate);
			} 
			else if (currentDate > calendar.maxDate) 
			{
				RenderDayDisabled (calendar, currentDate);
			} 
			else if (currentDate.getUTCMonth() != calendar.calendarDate.getUTCMonth())
			{
				RenderDayDisabled (calendar, currentDate);
			}
			else if (currentDate.getUTCDate() == todayDate.getUTCDate() && currentDate.getUTCMonth() == todayDate.getUTCMonth() && currentDate.getUTCFullYear() == todayDate.getUTCFullYear())
			{
				if ((currentDate.getUTCDay() == 0) || (currentDate.getUTCDay() == 6))
				{
					if (calendar.allowWeekends == true)
					{
						style = calendar.todayDayStyle;
						
						for (j=0; j < calendar.eventDates.length; j++)
					    {
						    if (calendar.eventDates[j][0] == currentDateString) 
						    {
						        style=calendar.todayEventDayStyle;
							    RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][1]);
						    }
					    }
					    if(style==calendar.todayDayStyle)
					    {
					        RenderDayEnabled (calendar, currentDate, style);
					    }
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getUTCMonth();
						calendar.calendarDate.setUTCDate(calendar.calendarDate.getUTCDate()+1);
						if (month != calendar.calendarDate.getUTCMonth())
						{
							renderCalendar1(calendar);
						}
						setFieldValue(calendar.attachedId, calendar.calendarDate);
					}
				} else {
					if (calendar.allowWeekdays == true)
					{
						style = calendar.todayDayStyle;
						
						for (j=0; j < calendar.eventDates.length; j++)
					    {
						    if (calendar.eventDates[j][0] == currentDateString) 
						    {
						        style=calendar.todayEventDayStyle;
							    RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][1]);
						    }
					    }
					    if(style==calendar.todayDayStyle)
					    {
					        RenderDayEnabled (calendar, currentDate, style);
					    }
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getUTCMonth();
						calendar.calendarDate.setUTCDate(calendar.calendarDate.getUTCDate()+1);
						if (month != calendar.calendarDate.getUTCMonth())
						{
							renderCalendar1(calendar);
						}
						setFieldValue(calendar.attachedId, calendar.calendarDate);
					}
				}
			}
			else if ((currentDate.getUTCDay() == 0) || (currentDate.getUTCDay() == 6))
			{
				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][1]);
						}
					}
					
					if (style == calendar.holidayDayStyle)
					{
						RenderDayEnabled (calendar, currentDate, style);
					}
				} 
				else 
				{
					RenderDayDisabled (calendar, currentDate);	
				}
			} 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][1]);
						}
					}

					if (style == calendar.commonDayStyle)
					{
						RenderDayEnabled (calendar, currentDate, style);
					}
				} 
				else 
				{
					RenderDayDisabled (calendar, currentDate);	
				}
			}

			currentDate.setUTCDate(currentDate.getUTCDate() + 1);	
		}
		
		calHtml += ("</tr>");
		

	} while (currentDate.getUTCMonth() == calendar.calendarDate.getUTCMonth());
	return calHtml;
}

// $Log: calendar.js,v $
// Revision 1.1  2005/02/15 21:54:32  Karl Agius
// Initial release
//


