﻿var cal1;
function Calendar (cname, date, showDrop)
{
	this.fallback_single = 0;
	this.fallback_multi = 1;
	this.fallback_both = 2;
	this.viewOnly = false;
	this.allowWeekends = true;
	this.allowWeekdays = true;
	this.minDate = "--";
	this.maxDate = "--";
	this.scrolling = true;
	this.name = cname;
	this.firstDayOfWeek = 0;
	this.fallback = this.fallback_both;
	this.calendarDate = date;
	this.calendarDate.setUTCHours(0);
	this.calendarDate.setUTCMinutes(0);
	this.calendarDate.setUTCSeconds(0);
	this.calendarDate.setUTCMilliseconds(0);
	this.attachedId = 'tesla';
	this.dropYear = showDrop;
	
	this.controlLeft = "&#171;";
	this.controlRight = "&#187;";
		
	this.controlLeftDisabled = "";
	this.controlRightDisabled = "";
	
	this.calendarStyle = "cal_calendar";
	this.headerStyle = "cal_header";
	this.headerCellStyle = "cal_cell";
	this.headerCellStyleLabel = "cal_labelcell";
	this.headerDays = "cal_header_days"; 
	
	this.weekStyle = "cal_week";
	this.evenWeekStyle = "cal_evenweek";
	this.oddWeekStyle = "cal_oddweek";
	
	this.dayStyle = "cal_day";
	this.disabledDayStyle = "cal_disabled";
	this.commonDayStyle = "cal_common";
	this.holidayDayStyle = "cal_holiday";
	this.eventDayStyle = "cal_event";
	this.todayDayStyle = "cal_today";
	
	this.dayLabels = new Array("D", "S", "T", "Q", "Q", "S", "S");
	this.monthLabels = new Array(
		"Janeiro", "Fevereiro", "Mar&ccedil;o", "Abril"
		, "Maio", "Junho", "Julho", "Agosto"
		, "Setembro", "Outubro", "Novembro", "Dezembro");
	
	this.eventDates = new Array();
	
	selectEvent = new Function();
}

function getFirstCalendarDate (calendar)
{
	return new Date (
		calendar.calendarDate.getUTCFullYear()
		, calendar.calendarDate.getUTCMonth()
		, 1
	);
}

function renderCalendar (calendar)
{
	calHtml1 =  ("<table id=\"cal_" + calendar.attachedId + "\" class=\"" + calendar.calendarStyle +"\" border='0' width='136'>");
	if( calendar.dropYear )
	    calHtml1 += BuildDrop(calendar);
	calHtml1 += ((calendar.scrolling)?buildHeader(calendar):buildStaticHeader(calendar));
	calHtml1 += buildCalendarTable (calendar);
	calHtml1 += ("</table>");
	
	document.getElementById("cal_" + calendar.attachedId + "_display").innerHTML = calHtml1;
}
function BuildDrop(calendar)
{
    year = 2007;
    today = new Date();
    maxYear = today.getFullYear() + 10;
    
    calHtmlDrop =  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtmlDrop +=  (
		"<td colspan=\"7\" class=\""
		+ calendar.headerCellStyle
		+ "\""
		+ "\">"
		+ "<select name=\"Calendario_dropAno\" onChange=\"setYear("+calendar.name+", this.options[this.selectedIndex].value, this)\">");
	
    calHtmlDrop += ("<option value=\""+year+"\">Ano</option>");
	while( year <= maxYear )
	{
	    calHtmlDrop += ("<option value=\""+year+"\">"+year+"</option>");
	    year ++;
	}
	calHtmlDrop += (
	    "</select>"
		+ "</td>");
	calHtmlDrop += ("</tr>");
	return calHtmlDrop
}

function scrollYearBack (calendar)
{
	calendar.calendarDate.setUTCFullYear(calendar.calendarDate.getUTCFullYear() - 1);
	renderCalendar (calendar);
}
function selectDate (calendar, day)
{
	if (!calendar.viewOnly) {
		calendar.calendarDate.setUTCDate(day);
		renderCalendar (calendar);
	}
}
function scrollYearForward (calendar)
{
	calendar.calendarDate.setUTCFullYear(calendar.calendarDate.getUTCFullYear() + 1);
	renderCalendar (calendar);
}

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 +=  (
		"<tr class=\""
		+ calendar.headerStyle
		+ "\">");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyle
		+ ((enableLeft)?("\" onclick=\"scrollMonthBack(" + calendar.name + ")"):"")
		+ "\">"
		+ "<"
		+ "</td>");
	calHtml2 +=  (
		"<td colspan=\"5\" class=\""
		+ calendar.headerCellStyleLabel
		+ "\">"
		+ calendar.monthLabels[calendar.calendarDate.getUTCMonth()] 
		+ " / " + calendar.calendarDate.getUTCFullYear()
		+ "</td>");
	calHtml2 +=  (
		"<td class=\""
		+ calendar.headerCellStyle
		+ ((enableRight)?("\" onclick=\"scrollMonthForward(" + calendar.name + ")"):"")
		+ "\">"
		+ ">"
		+ "</td>");
	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.headerDays
			+ "\">"
			+ calendar.dayLabels[showDay]
			+ "</td>");
	}

	calHtml2 += ("</tr>");
	return calHtml2
}

function buildStaticHeader (calendar)
{
	calHtml2 = "";
	
	calHtml2 +=  (
		"<tr 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)
{
	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">');
	calHtml += ("<span class=\"" + dayStyle + "\" onclick=\"selectDate(" + calendar.name + ", " + currentDate.getUTCDate() + ")\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span>");
	calHtml += ("</td>");
}

function RenderDayEvent (calendar, currentDate, dayStyle, eventId)
{
	currentDayStyle = dayStyle;
	calHtml += ("<td class=\"" + dayStyle + "\">");
	calHtml += ("<a onClick='ClickCalendario(\""+eventId+"\")'><span width=\"100%\">");
	calHtml += (currentDate.getUTCDate());
	calHtml += ("</span></a>");
	calHtml += ("</td>");
}

function buildCalendarTable (calendar)
{
    todayDate = new Date();
	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();

			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)
						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][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;
}
function scrollMonthBack (calendar)
{
    mes = calendar.calendarDate.getUTCMonth();
    if( mes < 1 ) mes = 12;
    calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() - 1);
    ClickCalendario(calendar.calendarDate.getUTCDate() + "/" + mes + "/" + calendar.calendarDate.getUTCFullYear());
    data = calendar.calendarDate.getUTCDate()+"/"+mes+"/"+calendar.calendarDate.getUTCFullYear();
    CarregaLembretes(data);
    renderCalendar (calendar);
}
function scrollMonthForward (calendar)
{
    mes = calendar.calendarDate.getUTCMonth() + 2;
    if( mes > 12 ) mes = 1;
    calendar.calendarDate.setUTCMonth(calendar.calendarDate.getUTCMonth() + 1);
    ClickCalendario(calendar.calendarDate.getUTCDate() + "/" + mes + "/" + calendar.calendarDate.getUTCFullYear());
    data = calendar.calendarDate.getUTCDate()+"/"+mes+"/"+calendar.calendarDate.getUTCFullYear();
    CarregaLembretes(data);
    renderCalendar (calendar);
}
function renderizaCalendario()
{
    renderCalendar (cal1);
}
function ClickCalendario( data )
{
    Ajax.GetEvents(data, ShowEvents);
}
function ShowEvents( collection )
{
    if( collection && collection.length )
	    page.rptEvents.Bind( collection );
}
function CarregaLembretes(data)
{
    page.rptEvents.Bind(null);
    Ajax.GetDates(data, ShowCalendar)
}
function ShowCalendar(collection)
{
    if( collection && collection.length )
    {
        for(var i=0 ; i<collection.length; i++)
        {
            cal1.eventDates[i] = new Array();
            //cal1.eventDates[i][0] = collection[i].ToString("yyyy/MM/dd");
            cal1.eventDates[i][0] = collection[i].getFullYear() + '/' + (collection[i].getMonth() + 1) + '/' + collection[i].getDate()
            cal1.eventDates[i][1] = collection[i].ToString("dd/MM/yyyy");
        }
        renderizaCalendario();
    }
}

/*
function updateFromSingle (sender, helper) {
	newDate = new Date (helper.value);
	newDate.setUTCDate(newDate.getUTCDate()+1);
	sender.calendarDate = newDate;

	renderCalendar (sender);
}
function updateFromMultiDay (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getUTCDate();
		return false;
	}

	sender.calendarDate.setUTCDate(helper.value);
	renderCalendar (sender);
}
function updateFromMultiMonth (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getUTCMonths() -1;
		return false;
	}
	
	sender.calendarDate.setUTCMonth(helper.value-1);
	renderCalendar (sender);
}
function updateFromMultiYear (sender, helper) {

	if (isNaN(helper.value)) {
		helper.value = sender.calendarDate.getUTCFullYear();
		return false;
	}
	
	sender.calendarDate.setUTCFullYear(helper.value);
	renderCalendar (sender);
}*/
