var calendarMesi = 	new Array();/*
		"January", "February", "March", "April"
		, "May", "June", "July", "August"
		, "September", "October", "November", "December");*/
var calendarGiorni = new Array();/*"D", "L", "M", "M", "G", "V", "S")*/

function Calendar (cname, id, date, today)
{
	// 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 = "--";
	this.minDate = new Date(new Date().getFullYear()-1, 0, 1);
	
	// The maximum date that the user can select (exclusive)
	//this.maxDate = "--";
	this.maxDate = new Date(new Date().getFullYear()+1, 11, 31);
	
	// 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 = 1;
	this.setFirstDayOfWeek = f_setFirstDayOfWeek;
	
	// Fallback method
	this.fallback = this.fallback_both;
	
	// Sets today
	this.today = today
	
	// Sets the date and strips out time information
	this.calendarDate = date;
	this.calendarDate.setHours(0);
	this.calendarDate.setMinutes(0);
	this.calendarDate.setSeconds(0);
	this.calendarDate.setMilliseconds(0);
	this.setCallbackMonth = f_setCallbackMonth;
	
	
	// 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;
	
	// The left and right month control icons
	this.controlLeft = "";
	this.controlRight = "";
		
	// The left and right month control icons (when disabled)
	//@IV fix width from 5 to 4 with new layout
	this.controlLeftDisabled = "<img border='0' src='/"+window.w3gContex+"/img/px.gif' width='4' height='15'/>";
	this.controlRightDisabled = "<img border='0' src='/"+window.w3gContex+"/img/px.gif' width='4' height='15'/>";
	
	this.eventOn = "onmouseover";
	
	// 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.holidayDayStyle = "cal_holiday";
	this.eventDayStyle = "cal_event";
	this.todayDayStyle = "cal_today";
	
	// specifies the labels for this calendar
	this.dayLabels = calendarGiorni;
	this.monthLabels = calendarMesi;
	
	// 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;
	
	
	selectEvent = new Function();
}

function f_setFirstDayOfWeek(primoGiorno)
 {
 this.firstDayOfWeek = primoGiorno;
 }
 
function f_setCallbackMonth(funzione)
 {
 this.callback = funzione;
 }
 
function updateFromSingle(sender, helper)
 {
	newDate = new Date (helper.value);
	newDate.setDate(newDate.getDate()+1);
	sender.calendarDate = newDate;
	if(calendar.callback)
		calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
	renderCalendar (sender);

}

function updateFromMultiDay (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getDate();
		return false;
	}

	sender.calendarDate.setDate(helper.value);
	if(calendar.callback)
		calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
	renderCalendar (sender);
}

function updateFromMultiMonth (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getMonth() -1;
		return false;
	}
	
	sender.calendarDate.setMonth(helper.value-1);
	if(calendar.callback)
		calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
	renderCalendar (sender);
}

function updateFromMultiYear (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getFullYear();
		return false;
	}
	
	sender.calendarDate.setFullYear(helper.value);
	if(calendar.callback)
		calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
	renderCalendar (sender);
}

function getFirstCalendarDate (calendar)
{
	return new Date (
		calendar.calendarDate.getFullYear()
		, calendar.calendarDate.getMonth()
		, 1
	);
}

function renderCalendar(calendar)
{
	calHtml1 =  ("<DIV id=calmonth>");
	calHtml1 += buildHeader(calendar);
	calHtml1 += buildCalendarTable (calendar);
	calHtml1 += ("</DIV>");
	document.getElementById("cal_" + calendar.attachedId + "_display").innerHTML = calHtml1;
}

function scrollMonthBack (calendar)
{
	// temp = new Date();
	
	// if (calendar.calendarDate.getFullYear()>= ( temp.getFullYear()-1 ) ) {
	
		calendar.calendarDate.setMonth(calendar.calendarDate.getMonth() - 1);
		if(calendar.callback)
			calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
		redrawCalendar(
			calendar.calendarDate.getFullYear(),
			calendar.calendarDate.getMonth() 
		);
	
	 // }
}

function selectDate (calendar, day)
  {
  if(!calendar.viewOnly)
	{
	calendar.calendarDate.setDate(day);
	if(calendar.callback)
	  calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
	renderCalendar(calendar);
	}
  }

function scrollMonthForward (calendar)
{		
	temp = new Date();
	
	// if (calendar.calendarDate.getFullYear()<=( temp.getFullYear()+1 ) ) {
	
		temp.setDate(calendar.calendarDate.getDate());
		temp.setMonth(calendar.calendarDate.getMonth());
		temp.setFullYear(calendar.calendarDate.getFullYear());
		var month1 = temp.getMonth();
		temp.setMonth(temp.getMonth() + 1);
		var month2 = temp.getMonth();
		if(month2 == ((month1 + 1)%12)){
		calendar.calendarDate.setMonth(calendar.calendarDate.getMonth() + 1);
		}
		else{
		month3 = calendar.calendarDate.getMonth();
			while(calendar.calendarDate.getMonth() != ((month3 +1)%12)){
				calendar.calendarDate.setDate(calendar.calendarDate.getDate() + 1);
			}
		}
		redrawCalendar(
			calendar.calendarDate.getFullYear(),
			calendar.calendarDate.getMonth() 
		);
		
	//}
}



function buildHeader (calendar)
{

	enableLeft = true;
	enableRight = true;
	
	if (calendar.minDate != "--") 
	{
		if (calendar.calendarDate.getFullYear() <= calendar.minDate.getFullYear())
		{
			if (calendar.calendarDate.getMonth() <= calendar.minDate.getMonth())
			{
				enableLeft = false;
			}
		}
	}

	if (calendar.maxDate != "--") 
	{
		if (calendar.calendarDate.getFullYear() >= calendar.maxDate.getFullYear())
		{
			if (calendar.calendarDate.getMonth() >= calendar.maxDate.getMonth())
			{
				enableRight = false;
			}
		}
	}

	
	
	
	calHtml2 = "";
	
	
	//-------------------------- inizio header ----------------------------------	
	
	calHtml2 +=  ("<DIV id=contenitoremonth>");
	/*
	calHtml2 +=  (
		"<a href='javascript:scrollMonthBack(" + calendar.name + ")'"
		+ ">"
		+ ((enableLeft)?calendar.controlLeft:calendar.controlLeftDisabled)
		+ "</a>");
	*/	
	if (enableLeft) {
		calHtml2 += ("<a href='javascript:scrollMonthBack(" + calendar.name + ")'"
		+ ">" + calendar.controlLeft + "</a>");
	} else {
		calHtml2 += (calendar.controlLeftDisabled);
	}
	calHtml2 +=  (
		//"<DIV id=title><span style=\"cursor:pointer;\" onclick=\"showEventList('"+calendar.calendarDate.getFullYear()+"','"+ calendar.calendarDate.getMonth()+"');\">"
		"<DIV id=title><span>" //<span style=\"cursor:pointer;\">"
		+ calendar.monthLabels[calendar.calendarDate.getMonth()] 
		+ " " + calendar.calendarDate.getFullYear()
		+ "</span></DIV>");
	
	/*	
	calHtml2 +=  (
		"<a href='javascript:scrollMonthForward(" + calendar.name + ")'"
		+ ">"
		+ ((enableRight)?calendar.controlRight:calendar.controlRightDisabled)
		+ "</a>");
	*/
	if (enableRight) {
	calHtml2 +=  (
		"<a href='javascript:scrollMonthForward(" + calendar.name + ")'"
		+ ">"
		+ calendar.controlRight
		+ "</a>");	
	} else {
		calHtml2 += (calendar.controlRightDisabled);
	}
	
	calHtml2 += ("</DIV>");
	
	
	//-------------------------- fine header ----------------------------------
	
	//-------------------------- inizio day labels ----------------------------------
	
	calHtml2 +=  ("<DIV id=contenitoreweek>")

	for (i = 0; i < 7; i++) {
		showDay = i + calendar.firstDayOfWeek;
		if (showDay > 6) showDay = showDay - 7;
		calHtml2 +=  (
			"<DIV class=elementoweek>"
			+ calendar.dayLabels[showDay]
			+ "</DIV>");
	}

	calHtml2 += ("</DIV>");
	
	//-------------------------- fine day labels ----------------------------------
	
	return calHtml2
}



function RenderDayDisabled (calendar, currentDate)
{
	calHtml += ('<DIV class=disabled>');
	calHtml += (currentDate.getDate());
	calHtml += ("</DIV>");
}

function RenderDayEnabled (calendar, currentDate, dayStyle)
{
	currentDayStyle = dayStyle;
	calHtml += ("<DIV class=" + dayStyle + ">");
	calHtml += (currentDate.getDate());
	calHtml += ("</DIV>");
}

function RenderDayEvent (calendar, currentDate, dayStyle, eventDate, eventTitle, color, link, index)
{	
	currentDayStyle = dayStyle;
	if (currentDate.getFullYear() + "/" + (currentDate.getMonth()+1) + "/" + currentDate.getDate() == calendar.today)
	{ 
	calHtml += ("<DIV class=\"elementoToday\" style=\"COLOR: white; cursor:pointer; BACKGROUND-COLOR: " + color + "\">");
	}else{
	calHtml += ("<DIV class=\"elementoselected\" style=\"COLOR: white; cursor:pointer; BACKGROUND-COLOR: " + color + "\">");
	}
	calHtml += ("<span id=\"evDate"+index+"\" class=\"" + dayStyle + "\">");
	// calHtml += ("<a class=\"" + dayStyle + "\" href=\"javascript:showDetail("+index+");\">");
//	calHtml += ("<a class=\"" + dayStyle + "\" href=\"\javascript:void(0);\" "+ calendar.eventOn +"=\"javascript:showDetail("+index+");\">");	
	calHtml += ("<div class=\"" + dayStyle + "\" href=\"\javascript:void(0);\" "+ calendar.eventOn +"=\"javascript:showDetail("+index+");\">");	
	calHtml += (currentDate.getDate());
//	calHtml += ("</a>")
	calHtml += ("</div>")
	calHtml += ("</span>");
	calHtml += ("</DIV>");
}
function addZero(src){
	/*if(src){
		if(src.length=1)
			src='0'+src;
	}*/
	return src;
}
function buildCalendarTable (calendar)
{
	currentDate = getFirstCalendarDate(calendar);
	odd = 0;
	while (currentDate.getDay() != calendar.firstDayOfWeek)
	{
		currentDate.setDate(currentDate.getDate() - 1);
	}
	calHtml = "";
	
	var numCalRow=0;
	
	do
	{
		odd += 1;

		calHtml +=  ("<DIV id=contenitore>");
		
		numCalRow=numCalRow+1;
		
		for (i = 0;i < 7;i++)
		{
			currentDayStyle = calendar.dayStyle;
			currentEventStyle = calendar.commonDayStyle;
			currentDateString = currentDate.getFullYear() + "/" + addZero(currentDate.getMonth()+1) + "/" + addZero(currentDate.getDate());
			
			if (currentDate < calendar.minDate) 
			{
				RenderDayDisabled (calendar, currentDate);
			} 
			else if (currentDate > calendar.maxDate) 
			{
				RenderDayDisabled (calendar, currentDate);
			} 
			else if (currentDate.getMonth() != calendar.calendarDate.getMonth())
			{
				RenderDayDisabled (calendar, currentDate);
			}
			else if (currentDate.getFullYear() + "/" + addZero(currentDate.getMonth()+1) + "/" + addZero(currentDate.getDate()) == calendar.today)
			{
			    if ((currentDate.getDay() == 0) || (currentDate.getDay() == 6))
				{
					if (calendar.allowWeekends == true)
					{
					var check1 = true;
						//RenderDayEnabled (calendar, currentDate, "elementoToday");
						for (j=0; j < calendar.eventDates.length; j++)	{
							if (calendar.eventDates[j][1] == currentDateString) 
							{
							style = calendar.eventDayStyle;
							check1 = false;
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][1], calendar.eventDates[j][2], calendar.eventDates[j][3],calendar.eventDates[j][0]);
							}
							
						}
						if (check1){
							today2 = new Date(Date.parse(calendar.today));
							RenderDayEnabled (calendar, today2, "elementoToday");
							}	
					} 
					else 
					{						
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getMonth();
						temp = new Date();
						temp.setDate(calendar.calendarDate.getDate());
						temp.setMonth(calendar.calendarDate.getMonth());
						temp.setFullYear(calendar.calendarDate.getFullYear());
						temp.setHours(0);
						temp.setMinutes(0);
						temp.setSeconds(0);
						temp.setMilliseconds(0);
						temp.setDate(temp.getDate()+1);
						if (month != temp.getMonth())
							{	
							if(calendar.callback)
								calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
							renderCalendar(calendar);
							}
					}
				} else {
					if (calendar.allowWeekdays == true)
					{	
						var check = true;
						//RenderDayEnabled (calendar, currentDate, "elementoToday");
						for (j=0; j < calendar.eventDates.length; j++){
							if (calendar.eventDates[j][1] == currentDateString) 
							{
							check = false;
							style =  calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][1], calendar.eventDates[j][2], calendar.eventDates[j][3],calendar.eventDates[j][4],calendar.eventDates[j][0]);
							}
						}
							if(check){
							today1 = new Date(Date.parse(calendar.today));
							RenderDayEnabled (calendar, today1, "elementoToday");
							}
					} 
					else 
					{
						RenderDayDisabled (calendar, currentDate);	
						month = calendar.calendarDate.getMonth();
						temp = new Date();
						temp.setDate(calendar.calendarDate.getDate());
						temp.setMonth(calendar.calendarDate.getMonth());
						temp.setFullYear(calendar.calendarDate.getFullYear());
						temp.setHours(0);
						temp.setMinutes(0);
						temp.setSeconds(0);
						temp.setMilliseconds(0);
						temp.setDate(temp.getDate()+1);
						if (month != temp.getMonth())
						{
							if(calendar.callback)
								calendar.callback(calendar.calendarDate.getFullYear(), calendar.calendarDate.getMonth());
							renderCalendar(calendar);
						}
						
					}
					
				} 
			}
			else if ((currentDate.getDay() == 0) || (currentDate.getDay() == 6))
			{
				if (calendar.allowWeekends == true)
				{
				
					style = calendar.holidayDayStyle
					
					for (j=0; j < calendar.eventDates.length; j++)
					{
						if (calendar.eventDates[j][1] == currentDateString) 
						{
							style = calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][1], calendar.eventDates[j][2], calendar.eventDates[j][3], calendar.eventDates[j][4],calendar.eventDates[j][0]);
						}
					}
					
					if (style == calendar.holidayDayStyle)
					{
						RenderDayEnabled (calendar, currentDate, "elemento");
					}
				} 
				else 
				{
					RenderDayDisabled (calendar, currentDate);	
				}
			} else {
				if (calendar.allowWeekdays == true)
				{
					style = calendar.commonDayStyle

					for (j=0; j < calendar.eventDates.length; j++)
					{
						if (calendar.eventDates[j][1] == currentDateString) 
						{
							style = calendar.eventDayStyle;
							RenderDayEvent (calendar, currentDate, style, calendar.eventDates[j][1], calendar.eventDates[j][2], calendar.eventDates[j][3], calendar.eventDates[j][4],calendar.eventDates[j][0]);
						}
					}

					if (style == calendar.commonDayStyle)
					{
						RenderDayEnabled (calendar, currentDate, "elemento");
					}
				} 
				else 
				{
					RenderDayDisabled (calendar, currentDate);	
				}
			}

			currentDate.setDate(currentDate.getDate() + 1);	
		}
		
		calHtml += ("</DIV>");
		

	} while (currentDate.getMonth() == calendar.calendarDate.getMonth());
	/*
	if (numCalRow==5) {
		calHtml +=("<DIV id=contenitore>");
		for (var i = 0;i < 7;i++){
			calHtml +=("<div class='disabled'>&nbsp;</div>");
		}
		calHtml +=("</DIV>");
	}
	*/
	return calHtml;
}

// $Log: calendar.js,v $
// Revision 1.1  2005/02/15 21:54:32  Karl Agius
// Initial release
//
