
// EVENT CALENDAR
var daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];
var currentMonth;
var currentYear;

var CellFontColor = false;
var CellBorderColor = false;  

var arADates = new Array();
var arIDates = new Array(); 

function CalMouseOver (cell)
{
    CellFontColor = cell.style.color;
    cell.style.color='#000000';
}

function CalMouseOut (cell)
{
    cell.style.color=CellFontColor;
}
 
function isLeapYear(inputYear)
{
	if(inputYear%400==0||(inputYear%4==0 && inputYear%100!=0)) return true;
	return false;	
}

function switchMonth(monthPosition)
{
	if(monthPosition == 'left')
	{
		currentMonth = currentMonth - 1;
		if(currentMonth < 0)
		{
			currentMonth = 11;
			currentYear = currentYear - 1;
		}
	}
	else
	{
		currentMonth = currentMonth + 1;
		if(currentMonth > 11)
		{
			currentMonth = 0;
			currentYear = currentYear + 1;
		}	
	}	
	DrawCalendar(currentMonth, currentYear);
}

function DrawCalendar(nMonth, nYear)
{
    arADates = new Array();
    arIDates = new Array(); 
    
    for(i=0; i<arActiveDates.length; i++)
    {
        // alert(arActiveDates[i].year + '.' + arActiveDates[i].month + '  :  ' + nYear + '.' + nMonth);
        if (arActiveDates[i].y == nYear && arActiveDates[i].m == nMonth)
        {
            arADates = arActiveDates[i].d;
        }
    }

    var d = new Date();	
	d.setFullYear(nYear);		
	d.setDate(1);		
	d.setMonth(nMonth);
    
    nMonth = parseInt(nMonth);
    nCalMont = nMonth+1;
    if (nCalMont < 10)
    {
        nCalMont = '0' + nCalMont;
    }
    
    htmlCalendar = '<table width="190" class="calendar">';
    
    htmlCalendar += '<tr>';
    htmlCalendar += '<td width="20"><img src="' + strRootPath +'images/calendar/arrow_left.gif" onclick="switchMonth(\'left\')" style="cursor:pointer;" width="11" height="11" border="0"></td>';
    if (arADates.length > 0)  
    { 
        htmlCalendar += '<td class="calmonth" align="center" width="150"><a href="' + strRootPath +strLanguage+'/messages/calendar/' + nYear + '-' + nCalMont +'.html">' + monthArray[nMonth] + ' ' + nYear + '</a></td>';
    }
    else
    {
        htmlCalendar += '<td class="calmonth" align="center" width="150">' + monthArray[nMonth] + ' ' + nYear + '</td>';                                                                
    }
    htmlCalendar += '<td width="20" align="right"><img src="' + strRootPath +'images/calendar/arrow_right.gif" onclick="switchMonth(\'right\')" style="cursor:pointer;" width="11" height="11" border="0"></td>';
    htmlCalendar += '</tr>';
    htmlCalendar += '<tr><td colspan="3">';
    
	var dayStartOfMonth = d.getDay();
	if(dayStartOfMonth == 0)
    {
        dayStartOfMonth = 7;
    }
	dayStartOfMonth--;
    var colCounter = dayStartOfMonth;
    
    htmlCalendar += '<table width="190" class="caldates"><tr>';
 	
    // begin empty 
	while(dayStartOfMonth--)
    {
        htmlCalendar += '<td>&nbsp;</td>';
	}
     

	var daysInMonth = daysInMonthArray[nMonth];
	if(daysInMonth == 28)
    {
		if(isLeapYear(nYear))
        {
            daysInMonth = 29;
        }
	}
 
	for(var nDay=1; nDay<=daysInMonth; nDay++)
    {
		d.setDate(nDay-1);
		if(colCounter > 0 && colCounter%7 == 0)
        {
            htmlCalendar += '</tr><tr>';	
            colCounter = 0;
		}
        linkDay = (nDay < 10) ? '0' + nDay : nDay;
        if(IsImportantDay(nDay))
        {
            htmlCalendar += '<td class="active" onmouseover="CalMouseOver(this);" onmouseout="CalMouseOut(this);" onclick="GetLocation(\'' + strRootPath + strLanguage + '/messages/calendar/' + nYear + '-' + nCalMont + '-' + linkDay + '.html\');"><a href="' + strRootPath + strLanguage+'/messages/calendar/' + nYear + '-' + nCalMont + '-' + linkDay + '.html">' + nDay + '</a></td>';
        }
        else if (IsActiveDay(nDay))
        {
            htmlCalendar += '<td class="active" onmouseover="CalMouseOver(this);" onmouseout="CalMouseOut(this);" onclick="GetLocation(\'' + strRootPath + strLanguage + '/messages/calendar/' + nYear + '-' + nCalMont + '-' + linkDay + '.html\');"><a href="' + strRootPath + strLanguage+'/messages/calendar/' + nYear + '-' + nCalMont + '-' + linkDay + '.html">' + nDay + '</a></td>';
        }
        else
        {
            htmlCalendar += '<td>' + nDay + '</td>'; 
        }
		colCounter++;
	}

    // end empty 
	while(colCounter++ < 7)
    {
        htmlCalendar += '<td>&nbsp;</td>';
	}
    htmlCalendar += '</tr></table></td></tr></table>'; 

    document.getElementById( 'eventcalendar' ).innerHTML = htmlCalendar; 
    
}

function IsActiveDay(nDay)
{
    for(i=0; i<arADates.length; i++)
    {
        if (arADates[i] == nDay)
        {
            return true;
        }
    }
    return false;
}

function IsImportantDay(nDay)
{
    for(i=0; i<arIDates.length; i++)
    {
        if (arIDates[i] == nDay)
        {
            return true;
        }
    }
    return false;
}

function InitCalendar()
{
    if(!currentYear)
	{
		var d = new Date();
		currentMonth = d.getMonth();
		currentYear = d.getFullYear();
	}
    DrawCalendar(currentMonth, currentYear);
}

function GetLocation(urlLocation)
{
	if (window.location.replace)
	{
		window.location.replace(urlLocation);
	}
	else
	{
		window.location = urlLocation;
	}	
}
