// DHTML Calendar
// $Author: Karl Agius $
// $Date: 2005/02/15 21:54:32 $
// $Revision: 1.1 $
    
function Calendar (cname, DName , date, DaySearch,Reservation,SelectedDateID,SelectedDateIDValue)
{

    this.SelectedDateID=SelectedDateID;
    this.SelectedDateIDValue=SelectedDateIDValue;

    this.DaySearch=DaySearch;
	this.Reservation=Reservation;
	
	// readonly the div name
	this.DivName = DName;
	

	// 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;
	
	
	// 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);
	document.getElementById(SelectedDateID).value=this.calendarDate;

	// 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.
	
	// 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";
	
	// 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.commonDayStyleselect="cal_common_select";
	this.holidayDayStyle = "cal_holiday";
	this.holidayDayStyleselect = "cal_holiday_select";
	this.eventDayStyle = "cal_event";
	this.todayDayStyle = "cal_today";
	
	// specifies the labels for this calendar
	this.dayLabels = new Array("S", "M", "T", "W", "T", "F", "S");
	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.
	
//	selectEvent = new Function();
}
function getFirstCalendarDate (calendar)
{
	return new Date (
		calendar.calendarDate.getUTCFullYear()
		, calendar.calendarDate.getUTCMonth()
		, 1
	);
}

function renderCalendar (calendar)
{
	calHtml1 =  ("<table id=\"cal_" + calendar.name + "\" class=\"" + calendar.calendarStyle +"\">");
	calHtml1 += buildHeader(calendar);
	calHtml1 += buildCalendarTable (calendar);
	calHtml1 += ("</table>");
	document.getElementById(calendar.DivName ).innerHTML = calHtml1;
}

function scrollMonthBack (calendar)
{
	calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() - 1);
	renderCalendar (calendar,false);
}
function scrollYearBack (calendar)
{
	calendar.calendarDate.setUTCFullYear(calendar.calendarDate.getUTCFullYear() - 1);
	renderCalendar (calendar,false);
}

function selectDate (calendar, day,month,year)
{
	if (calendar.Reservation==true ) 
	{
		calendar.calendarDate.setUTCDate(day);
		calendar.calendarDate.setUTCMonth(month-1);
		calendar.calendarDate.setUTCFullYear(year);
		document.getElementById(calendar.SelectedDateID).value = calendar.calendarDate;
		if(calendar.SelectedDateIDValue!=null)
		{
			var date=(calendar.calendarDate.getUTCMonth()+1)+'/'+calendar.calendarDate.getUTCDate()+'/'+calendar.calendarDate.getUTCFullYear();
			document.getElementById(calendar.SelectedDateIDValue).value = date;
		}
		
		
		if(calendar.name=='calEnd')
		{       
		    document.getElementById(calendar.DivName ).className="hide"; 
 			var StartDateString = document.getElementById(this.calStart.SelectedDateIDValue).value ;
			var EndDateString = document.getElementById(this.calEnd.SelectedDateIDValue).value;
            Firstpos=StartDateString.indexOf('/');
            Lastpos=StartDateString.lastIndexOf('/');
		    var StartDate=new Date();
            StartDate.setFullYear(StartDateString.substring(Lastpos+1,StartDateString.length),(StartDateString.substring(0,Firstpos)-1),StartDateString.substring(Firstpos+1,Lastpos));
            Firstpos=EndDateString.indexOf('/');
            Lastpos=EndDateString.lastIndexOf('/');
   		    var EndDate=new Date();
            EndDate.setFullYear(EndDateString.substring(Lastpos+1,EndDateString.length),(EndDateString.substring(0,Firstpos)-1),EndDateString.substring(Firstpos+1,Lastpos));
            if(StartDate>EndDate)
            {
                document.getElementById(this.calStart.SelectedDateID).value=document.getElementById(this.calEnd.SelectedDateID).value;
                document.getElementById(this.calStart.SelectedDateIDValue).value=document.getElementById(this.calEnd.SelectedDateIDValue).value;
            }
        }
        if(calendar.name=='calStart')
		{        
		    document.getElementById(calendar.DivName ).className="hide";
 			var StartDateString = document.getElementById(this.calStart.SelectedDateIDValue).value ;
			var EndDateString = document.getElementById(this.calEnd.SelectedDateIDValue).value;
            Firstpos=StartDateString.indexOf('/');
            Lastpos=StartDateString.lastIndexOf('/');
		    var StartDate=new Date();
            StartDate.setFullYear(StartDateString.substring(Lastpos+1,StartDateString.length),(StartDateString.substring(0,Firstpos)-1),StartDateString.substring(Firstpos+1,Lastpos));
            Firstpos=EndDateString.indexOf('/');
            Lastpos=EndDateString.lastIndexOf('/');
   		    var EndDate=new Date();
            EndDate.setFullYear(EndDateString.substring(Lastpos+1,EndDateString.length),(EndDateString.substring(0,Firstpos)-1),EndDateString.substring(Firstpos+1,Lastpos));
            if(StartDate>EndDate)
            {
                document.getElementById(this.calEnd.SelectedDateID).value=document.getElementById(this.calStart.SelectedDateID).value;
                document.getElementById(this.calEnd.SelectedDateIDValue).value=document.getElementById(this.calStart.SelectedDateIDValue).value;
            }
        }
		renderCalendar(calendar,true);
	}
}
function selectDateEvent (calendar, day,month,year)
{
		calendar.calendarDate.setUTCDate(day);
		calendar.calendarDate.setUTCMonth(month-1);
		calendar.calendarDate.setUTCFullYear(year);
		document.getElementById(calendar.SelectedDateID).value = calendar.calendarDate;
		if(calendar.SelectedDateIDValue!=null)
		{
			var date=(calendar.calendarDate.getUTCMonth()+1)+'/'+calendar.calendarDate.getUTCDate()+'/'+calendar.calendarDate.getUTCFullYear();
			document.getElementById(calendar.SelectedDateIDValue).value = date;
		}
		var date=(calendar.calendarDate.getUTCMonth()+1)+'/'+calendar.calendarDate.getUTCDate()+'/'+calendar.calendarDate.getUTCFullYear();
		DaySearchCalendar(date,calendar.DaySearch);
		renderCalendar (calendar,true);
}


function scrollMonthForward (calendar)
{
	calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() + 1);
	renderCalendar (calendar,false);
}
function scrollYearForward (calendar)
{
	calendar.calendarDate.setUTCFullYear(calendar.calendarDate.getUTCFullYear() + 1);
	renderCalendar (calendar,false);
}
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 class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyle
		+ "\">"
		+"<a class=\"CalArrow\" onclick=\"scrollMonthBack("+calendar.name+");\"> < </a>"
		+"<a class=\"CalArrow\" onclick=\"scrollYearBack("+calendar.name+");\"> << </a>"
		+ "</td>");
		
	calHtml2 +=  (
		"<td colspan=\"5\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getUTCMonth()] 
		+ ", " + calendar.calendarDate.getUTCFullYear()
		+ "</td>");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyle
		+ "\">"
		+"<a class=\"CalArrow\" onclick=\"scrollMonthForward("+calendar.name+");\"> > </a>"
		+"<a class=\"CalArrow\" onclick=\"scrollYearForward("+calendar.name+");\"> >> </a>"

		+ "</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>");
	}
	return calHtml2
}

function RenderDayDisabled (calendar, currentDate)
{
	calHtml += ('<td class="day">');
	calHtml += ("<span class=\"" + calendar.disabledDayStyle + "\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function RenderDayEnabled (calendar, currentDate, dayStyle )
{	
	currentDayStyle = dayStyle;
	calHtml += ('<td class="day">');
//	if(dayStyle==calendar.todayDayStyle)
//	{ 
//	   if(calendar.SelectedDateID!="null")
//	   {
//		    if(calendar.calendarDate==document.getElementById(calendar.SelectedDateID).value )
//                dayStyle=calendar.todayDayStyle
//            else
//                dayStyle=calendar.commonDayStyle
//       }
//       else 
          dayStyle=dayStyle;
//	}
	//if reservation so the ordinay days is gonna be linkable 
	// if not reserve so the ordinary days will be not clickable
	if ( calendar.Reservation==true && dayStyle==calendar.commonDayStyle)
	    dayStyle=calendar.commonDayStyleselect;
	if ( calendar.Reservation==true && dayStyle==calendar.holidayDayStyle)
	    dayStyle=calendar.holidayDayStyleselect;
	calHtml += ("<span class=\"" + dayStyle + "\" onclick=\"selectDate(" + calendar.name + ", " + currentDate.getUTCDate()+ ", " +(currentDate.getUTCMonth()+1)+ ", " +currentDate.getUTCFullYear() + ")\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function RenderDayEvent (calendar, currentDate, dayStyle)
{
	currentDayStyle = dayStyle;
	calHtml += ('<td class="cal_day">');

	if(calendar.calendarDate.getUTCDate()==currentDate.getUTCDate())
	    calHtml += ("<span class=\""+ calendar.todayDayStyle)
	else
	    calHtml += ("<span class=\"" + dayStyle)
	calHtml += ("\" onclick=\"selectDateEvent(" + calendar.name + ", " + currentDate.getUTCDate()+ ", " +(currentDate.getUTCMonth()+1)+ ", " +currentDate.getUTCFullYear() +  ")\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function buildCalendarTable (calendar)
{
	currentDate = getFirstCalendarDate(calendar);
	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) + "\">")

		for (i = 0;i < 7;i++)
		{
			currentDayStyle = calendar.dayStyle;
			currentEventStyle = calendar.commonDayStyle;
			currentDateString = currentDate.getUTCFullYear() + "/" + (currentDate.getUTCMonth()+1) + "/" + currentDate.getUTCDate();
			//online
			currentDateStringEvent = (currentDate.getUTCMonth()+1) + "/" + currentDate.getUTCDate() + "/" + currentDate.getUTCFullYear();
			//local
//			currentDateStringEvent = (currentDate.getUTCMonth()+1)  ;
//			if(currentDateStringEvent.toString().length==1)
//			{
//			currentDateStringEvent="0"+currentDateStringEvent;
//			}
//			var day=currentDate.getUTCDate().toString();
//			if(day.length==1)
//			{
//			day="0"+day;
//			}
//			currentDateStringEvent=currentDateStringEvent+ "/" + day + "/" + currentDate.getUTCFullYear();
            //
			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() == calendar.calendarDate.getUTCDate() && calendar.Reservation==true)
			{
				if ((currentDate.getUTCDay() == 0) || (currentDate.getUTCDay() == 6))
				{
					if (calendar.allowWeekends == true )
					{
						RenderDayEnabled (calendar, currentDate, calendar.todayDayStyle);
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getUTCMonth();
						calendar.calendarDate.setUTCDate(calendar.calendarDate.getUTCDate()+1);
						if (month != calendar.calendarDate.getUTCMonth())
						{
							renderCalendar(calendar);
						}
					}
				} else {
					if (calendar.allowWeekdays == true )
					{
						RenderDayEnabled (calendar, currentDate, calendar.todayDayStyle);
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getUTCMonth();
						calendar.calendarDate.setUTCDate(calendar.calendarDate.getUTCDate()+1);
						if (month != calendar.calendarDate.getUTCMonth())
						{
							renderCalendar(calendar);
						}
					}
				}
			}
			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] == currentDateStringEvent) 
						{
							style = calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style);
						}
					}
					
					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] == currentDateStringEvent) 
						{
							style = calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style);
						}
					}

					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
//
