// JavaScript Document
// This javascript agenda module should NOT be confused with the PHP agenda module.
// This piece of javascript simply displays an agenda and allows you to select a valid date from it.
// The PHP agenda module is much more detailed and allows for more options, but isn't interactive on the 'client-side-only'

var MonthLength		= new Array();
MonthLength[0]		= 31;
MonthLength[1]		= 28;
MonthLength[2]		= 31;
MonthLength[3]		= 30;
MonthLength[4]		= 31;
MonthLength[5]		= 30;
MonthLength[6]		= 31;
MonthLength[7]		= 31;
MonthLength[8]		= 30;
MonthLength[9]		= 31;
MonthLength[10]		= 30;
MonthLength[11]		= 31;

var Year			= 2010;
var Month			= 0; // 0 is januari, 2 is febuari, etc
var Day				= 1;

var DateYear_Now	= 2010;
var DateMonth_Now	= 0;
var DateDay_Now		= 1;

var DateYear_Sel	= 0;
var DateMonth_Sel	= 0;
var DateDay_Sel		= 0;

var YearMin			= 2009;
var YearMax			= 2012;

var BG_Green		= 'CBEB4B';
var BG_Yellow		= 'FFE000';
var BG_Base			= 'FFFFFF';

var Field_ID		= 'date_day';
var Div_ID			= 'AgendaDiv';
var Hover			= '1';

function GenerateMonthHTML(Field_ID_TMP)
	{
	if (Field_ID_TMP) Field_ID = Field_ID_TMP;
	
	var HTML			= '';
	
	var DateThis 	= new Date(Year,Month,1);
	var DateDayT	= DateThis.getDay();
	
	var WeekDay		= 0;
	var CountDay	= 0;
	var Month_TMP 	= Month + 1;
	
	if (Month_TMP < 10) Month_TMP 	= '0' + Month_TMP;
	
	if (DateDayT != 0)
		{
		for (i = 0; i < DateDayT; i++)
			{
			HTML 	= HTML + '<td style="background:#CCCCCC;"></td>';
			WeekDay	= WeekDay + 1;
			CountDay= CountDay + 1;
			}
		}
	
	for (i = 1; i <= MonthLength[Month]; i++)
		{
		var Day_TMP		= i;
		
		if (Day_TMP < 10)	Day_TMP		= '0' + Day_TMP;
		
		if (Year == DateYear_Sel && Month_TMP == DateMonth_Sel && i == DateDay_Sel)
			{
			BG_Color = BG_Green;
			}
		else if (Year == DateYear_Now && Month == DateMonth_Now && i == DateDay_Now)
			{
			BG_Color = BG_Yellow;
			}
		else
			{
			BG_Color = BG_Base;
			}
			
		HTML 	= HTML + '<td style="background:#' + BG_Color + ';" onclick="AssignValue(\''+ Field_ID + '\',\'' + Day_TMP + '-' +  Month_TMP + '-' + Year + '\'); ActivateDate(' + Year + ',' +  Month + ',' + Day_TMP + '); ToggleElement(\'Agenda\');">' + i + '</td>';
		WeekDay	= WeekDay + 1;
		CountDay= CountDay + 1;
		
		if (WeekDay >= 7)
			{
			HTML 	= HTML + '</tr><tr>';
			WeekDay	= 0;
			}
		}
	
	if (CountDay > 28)
		{
		if (CountDay > 35) { var DayMax = 42; }
		else { var DayMax = 35; }
		
		for (i = CountDay; i < DayMax; i++)
			{
			HTML 	= HTML + '<td style="background:#CCCCCC;"></td>';
			}
		}
	
	HTML = '<table style="width:auto;"><tr><td class="Negative GreenText" onclick="SwitchMonth(\'p\')">&lt;</td><td colspan="5" class="Negative GreenText" style="text-align:center; width:auto;">' + Months[Month] + ' ' + Year + '</td><td class="Negative GreenText" onclick="SwitchMonth(\'n\')">&gt;</td></tr><tr><td class="Negative">' + Weekdays[0] + '</td><td class="Negative">' + Weekdays[1] + '</td><td class="Negative">' + Weekdays[2] + '</td><td class="Negative">' + Weekdays[3] + '</td><td class="Negative">' + Weekdays[4] + '</td><td class="Negative">' + Weekdays[5] + '</td><td class="Negative">' + Weekdays[6] + '</td></tr><tr>' + HTML + '</tr></table>';
	
	if (Hover == '1')
		{
		RemoveElement('Agenda');
		
		HTML = '<div id="Agenda" style="position:relative; width:0px; height:0px;"><div style="position:absolute; top:-30px; left:30px; float:left; display:block;">' + HTML + '</div></div>';

		var objNode		= document.getElementById(Field_ID);
		var objParent	= objNode.parentNode;
		var oldHTML		= objParent.innerHTML;
		
		objParent.innerHTML = oldHTML + HTML;
		}
	else
		{
		var objAgenda 				= document.getElementById(Div_ID);
		objAgenda.style.display 	= 'block';
		objAgenda.innerHTML 		= HTML;
		}
	}

function ActivateDate(Year,Month,Day)
	{
	DateYear_Sel 	= Year;
	DateMonth_Sel 	= Month + 1;
	DateDay_Sel 	= Day;
	}

function SwitchMonth(Action)
	{
	if (Action == 'p')
		{
		var Month_TMP = Month - 1;
		
		if (Month_TMP < 0  &&  Year > YearMin)
			{
			Month 	= 11;
			Year--;
			}
		else if (Month_TMP >= 0)
			{
			Month = Month_TMP;
			}
		}
	else if (Action == 'n')
		{
		var Month_TMP = Month + 1;
		
		if (Month_TMP > 11  &&  Year < YearMax)
			{
			Month 	= 0;
			Year++;
			}
		else if (Month_TMP <= 11)
			{
			Month = Month_TMP;
			}
		}
	
	GenerateMonthHTML();
	}

function AgendaParseCP()
	{
	var objAgenda 				= document.getElementById('AgendaCP');
	objAgenda.innerHTML 		= FetchFileAsTxt('/js/pAgendaCP.php?a=agendaCP');
	}

function AgendaParse()
	{
	var objAgenda 				= document.getElementById('Agenda');
	objAgenda.innerHTML 		= FetchFileAsTxt('/js/pAgenda.php');
	}
