// ----------------------------------------------------------------------
// Calendar grid
// Copyright (C) 2004 by the Alpha Design Company
// http://www.alphadiz.com/
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WIthOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Original Author of file: Eugene Onischenko
// Purpose of file: Show calendar grid
// ----------------------------------------------------------------------
// Some functions have been taken QDate C++ class present in QT library.
// See http://www.trolltech.com/ more more info about QT library

var calendarUrl="index.php?year={Y}&amp;month={M}&amp;day={D}";
var calendarCurrentDate;
calendarCurrentDate=new Date();

var calendarMonthDays;
calendarMonthDays=new Array();
calendarMonthDays[1]=31;
calendarMonthDays[2]=28;
calendarMonthDays[3]=31;
calendarMonthDays[4]=30;
calendarMonthDays[5]=31;
calendarMonthDays[6]=30;
calendarMonthDays[7]=31;
calendarMonthDays[8]=31;
calendarMonthDays[9]=30;
calendarMonthDays[10]=31;
calendarMonthDays[11]=30;
calendarMonthDays[12]=31;
var calendarMonthNames;
calendarMonthNames=new Array();
calendarMonthNames[1]="январь";
calendarMonthNames[2]="февраль";
calendarMonthNames[3]="март";
calendarMonthNames[4]="апрель";
calendarMonthNames[5]="май";
calendarMonthNames[6]="июнь";
calendarMonthNames[7]="июль";
calendarMonthNames[8]="август";
calendarMonthNames[9]="сентябрь";
calendarMonthNames[10]="октябрь";
calendarMonthNames[11]="ноябрь";
calendarMonthNames[12]="декабрь";

var calendarWeekNames;
calendarWeekNames=new Array();
calendarWeekNames[1]="пн";
calendarWeekNames[2]="вт";
calendarWeekNames[3]="ср";
calendarWeekNames[4]="чт";
calendarWeekNames[5]="пт";
calendarWeekNames[6]="сб";
calendarWeekNames[7]="вс";

// Copied from QDate::gregorianToJulian .
// See http://www.trolltech.com/ more more info about QT library
function gregorianToJulian(y,  m,  d){
	var c, ya;
	if ( y <= 99 )
		y += 1900;
	if ( m > 2 ) {
		m -= 3;
	} else {
		m += 9;
		y--;
	}
	ya = y % 100;
	c=(y-ya)/100;

	return 1721119 + d +
		Math.floor((146097*c)/4) +
		Math.floor((1461*ya)/4) +
		Math.floor((153*m+2)/5);
}

// Copied from QDate::dayOfWeek.
// See http://www.trolltech.com/ more more info about QT library
function dayOfWeek(y, m, d){
	return (gregorianToJulian(y, m, d) % 7)+1;
}

// Copied from QDate::leapYear.
// See http://www.trolltech.com/ more more info about QT library
function leapYear(y){
	return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
}

// Copied from QDate::daysInMonth.
// See http://www.trolltech.com/ more more info about QT library
function daysInMonth(y, m){
	if(m==2 && leapYear(y))
		return 29;
	else
		return calendarMonthDays[m];
}

/*return workDay or restDay */
// function dayTypeClass(y, m, d){
// 	var dw=dayOfWeek(y,m,d);
// 	return ((dw==6) || (dw==7))?'restDay':'workDay';
// }

function weekDayTypeClass(dw){
	return ((dw==6) || (dw==7))?'restDay':'workDay';
}

function monthOptions(m){
	var result='';
	for(var i=1; i<=12; ++i){
		result+="<option value=\""+i+"\"";
		if(i==m)result+="selected"
		result+=">"+calendarMonthNames[i]+"</option>";
	}
	return result;
}

function yearOptions(y){
	var result;
	var maxY;
	var minY;
	result='';
	maxY=calendarCurrentDate.getFullYear()>y?
		calendarCurrentDate.getFullYear():y;
	minY=
		calendarCurrentDate.getFullYear()-10<y?
		calendarCurrentDate.getFullYear()-10:y;
	for(var i=minY; i<=maxY; ++i){
		result+="<option value=\""+i+"\"";
		if(i==y)result+="selected"
		result+=">"+i+"</option>";
	}
	return result;
}

function dateToUrl(y, m ,d){
	return calendarUrl.replace(/\{Y\}/, y).replace(/\{M\}/, m).replace(/\{D\}/, d);
}

function drawCalendar(elemId, y, m, d){
	aDay=new Date(y, m-1, d);
	if(isNaN(aDay)) aDay=new Date();
	y=aDay.getFullYear();
	m=aDay.getMonth()+1;
	d=calendarCurrentDate.getDate();
	var html="<table class=\"calendar\" border=\"0\"><tr>"+
		"<td class=\"prevmonth\"><a href=\""+
		dateToUrl(m==1?y-1:y, m==1?12:m-1, d)+
		"\" onclick=\"return drawCalendar(&quot;"+elemId+"&quot;, "+
			(m==1?y-1:y)+", "+(m==1?12:m-1)+", "+d+")\">&lt;</a></td>"+
		"<td class=\"monthYear\">"+
		"<select id=\"monthSelect\""+
			"onchange=\"return drawCalendar(&quot;"+elemId+"&quot;, "+
			"document.getElementById(&quot;yearSelect&quot;).value,"+
			"document.getElementById(&quot;monthSelect&quot;).value,"+
			d+")\""+
		">"+
		monthOptions(m)+
		"</select>"+
		"<select id=\"yearSelect\""+
			"onchange=\"return drawCalendar(&quot;"+elemId+"&quot;, "+
			"document.getElementById(&quot;yearSelect&quot;).value,"+
			"document.getElementById(&quot;monthSelect&quot;).value,"+
			d+")\""+
		">"+
		yearOptions(y)+
		"</select>"+
		"</td>"+
		"<td class=\"nextmonth\"><a href=\""+
		dateToUrl(m==12?y+1:y, m==12?1:m+1, d)+
		"\" onclick=\"return drawCalendar(&quot;"+elemId+"&quot;, "+
			(m==12?y+1:y)+", "+(m==12?1:m+1)+", "+d+")\">&gt;</a></td>"+
		"</tr><table>";
	//Drawing days grid
	html+="<table  class=\"calendar day\" border=\"0\">"+
		"<thead>"+
		"<tr class=\"daynames\">"+
			"<th class=\"workDay\">пн</th>"+
			"<th class=\"workDay\">вт</th>"+
			"<th class=\"workDay\">ср</th>"+
			"<th class=\"workDay\">чт</th>"+
			"<th class=\"workDay\">пт</th>"+
			"<th class=\"restDay\">сб</th>"+
			"<th class=\"restDay\">вс</th>"+
		"</tr>"+
		"</thead>"+
		"<tbody>";
	var maxDay=daysInMonth(y, m);
	var startDay=dayOfWeek(y, m, 1);
	//Filling empty days of week
	for(var i=1; i<startDay; ++i){
		if(i==1)html+="<tr>";
		html+="<td></td>";
	}
	var weekDay=startDay;
	for(var i=1; i<=maxDay; ++i){
		if(weekDay==1)html+="<tr>";
		html+="<td class=\""+
			weekDayTypeClass(weekDay);
		if(
			(calendarCurrentDate.getDate()==i) &&
			(calendarCurrentDate.getMonth()+1==m) &&
			(calendarCurrentDate.getFullYear()==y)
		)html+=" currentDay";
		var mFunc = ((m<10)?'0':'')+m;
		var iFunc = ((i<10)?'0':'')+i;
		html+="\"><a href=\""+
			dateToUrl(y, mFunc, iFunc)+
			"\">"+i+"</a></td>";
		if(weekDay==7){
			html+="</tr>";
			weekDay=1;
		}else
			++weekDay;
	}
	if(weekDay!=1){
		for(; weekDay<=7; ++weekDay){
			html+="<td></td>";
		}
		html+="</tr>";
	}
	html+="</tbody></table>";
// 	alert(html);
	document.getElementById(elemId).innerHTML=html;
	return false;
}
