	/* vim600: noet sw=4 ts=4 fdm=marker
	 * vim<600: noet sw=4 ts=4 */
	/************************************************************************
	 *						Raphaël Gertz (Rapsys)							*
	 ************************************************************************
	 *							Yaorte project								*
	 ************************************************************************
	 *	Project avaible under GPL revision 2 until 3 or greater is released *
	 *	Link GPL v2:		http://www.gnu.org/licenses/gpl.html			*
	 *	French translation:	http://fsffrance.org/gpl/gpl-fr.fr.html			*
	 ************************************************************************
	 *	Created with:														*
	 *		Mandriva Linux 2007 (official)									*
	 *		Vim (CSS2/PHP/XHTML/Javascript) and Kwrite						*
	 *	Tested under:														*
	 *		Konqueror, FireFox, Lynx, Opera									*
	 *		Internet Explorer (tryed to limit break for him)				*
	 ************************************************************************/

	//The months
	months = new Array(
		"Janvier","Février","Mars","Avril","Mai","Juin","Juillet","Août","Septembre","Octobre","Novembre","Décembre"
	);
	//The days
	days = new Array(
		"Lun","Mar","Mer","Jeu","Ven","Sam","Dim"
	);
	//The urls
	urls = '/agenda.html';
	//Return last month date number
	function js_calendar_last(month, year)
	{
		//Correct year if needed
		if (year < 999)
			year += 1900;
		//Last day
		var end = 31;
		//XXX: The month array start from 0
		if (month==3 || month==5 || month==8 || month==10)
			end--;
		//Check if month is february
		if (month==1)
		{
			end -= 3;
			if (year%4==0)
				end++;
			if (year%100==0)
				end--;
			if (year%400==0)
				end++;
		}
		return end;
	}

	//Calendar function
	function js_calendar(container, month, year)
	{
		//Create table element
		var table = document.createElement('table');
		table.className = 'calendar';
		//Create thead element
		var thead = document.createElement('thead');
		//Create tbody element
		var tbody = document.createElement('tbody');

		//Append table to container
		container.appendChild(table);
		//Append thead to table
		table.appendChild(thead);
		//Append tbody to table
		table.appendChild(tbody);

		//Create a tr element
		var elem_tr = document.createElement('tr');
		//Create a th element
		var elem_th = document.createElement('th');
		//Create a td element
		var elem_td = document.createElement('td');
		//Create a a element
		var elem_a = document.createElement('a');
		//Create a text element
		var elem_text = document.createTextNode('');

		//Check if now don't already exists
		if (!now)
			//Create it
			var now = new Date();
		//Set the year
		var now_year = now.getYear();
		//Corrent now year
		if (now_year < 999)
			now_year += 1900;
		//Correct the year
		if (year < 999)
			year += 1900;
		//The selected month date
		var time = new Date(year, month, 1);

		//Create the current link object
		var current_link = elem_a.cloneNode(false);
		//Create the current tr object
		var current_tr = elem_tr.cloneNode(false);
		//Create the current th object
		var current_th = elem_th.cloneNode(false);
		//Create the current td object
		var current_td = elem_td.cloneNode(false);
		//Create the current text object
		var current_text = elem_text.cloneNode(false);

		//Set previous month link
		current_link.href = 'javascript:js_calendar_replace(document.getElementById(\''+container.id+'\'), '+(month-1)+', '+year+');';
		//Check if previous month is from last year
		if (month == 0)
//TODO: add here some i18n features
			current_link.alt = months[11]+' '+(year-1);
		else
//TODO: add here some i18n features
			current_link.alt = months[(month-1)]+' '+year;
		current_text.nodeValue = '«';
		current_td.style.textAlign = 'left';

		//Append text, link, td to tr
		current_link.appendChild(current_text);
		current_td.appendChild(current_link);
		current_td.className = 'calendar_prev';
		current_tr.appendChild(current_td);

		//Recreate new element
		current_text = elem_text.cloneNode(false);
		current_link = elem_a.cloneNode(false);
		current_td = elem_td.cloneNode(false);

		//Set month and year info
//TODO: add here some i18n features
		current_text.nodeValue = months[month] + ' ' + year;
		current_td.colSpan = 5;
		current_td.style.textAlign = 'center';
		current_td.className = 'calendar_title';

		//Append text, link, td to tr
		current_td.appendChild(current_text);
		current_tr.appendChild(current_td);

		//Recreate new element
		current_text = elem_text.cloneNode(false);
		current_td = elem_td.cloneNode(false);

		//Set next month link
		current_link.href = 'javascript:js_calendar_replace(document.getElementById(\''+container.id+'\'), '+(month+1)+', '+year+');';
		//Check if next month is from next year
		if (month == 11)
//TODO: add here some i18n features
			current_link.alt = months[0]+' '+(year+1);
		else
//TODO: add here some i18n features
			current_link.alt = months[(month+1)]+' '+year;
		current_text.nodeValue = '»';
		current_td.style.textAlign = 'right';

		//Append text, link, td to tr
		current_link.appendChild(current_text);
		current_td.appendChild(current_link);
		current_td.className = 'calendar_next';
		current_tr.appendChild(current_td);

		//Append the generated tr to thead
		thead.appendChild(current_tr);

		//Recreate new element
		current_text = elem_text.cloneNode(false);
		current_link = elem_a.cloneNode(false);
		current_td = elem_td.cloneNode(false);
		current_tr = elem_tr.cloneNode(false);

		//Generate the week days names
		for (var i=0; i<7; i++)
		{
			//Set day name
			current_text.nodeValue = days[i];
			//Append text, th to tr
			current_th.appendChild(current_text);
			current_th.className = 'calendar_dayname';
			current_tr.appendChild(current_th);
			//Recreate new element
			current_text = elem_text.cloneNode(false);
			current_th = elem_th.cloneNode(false);
		}

		//Append the generated tr to tbody
		thead.appendChild(current_tr);

		//Recreate new element
		current_tr = elem_tr.cloneNode(false);

		//Generate the previous month days
		//Set the end
		end = js_calendar_last(month - 1, year);
		//Set the begin
		//XXX: the ((Date.getDay()+5)%7) is used to have week start on monday
		begin = end - ((time.getDay()+5)%7);
		//Set current generated previous month and year
		var current_month = month - 1;
		var current_year = (current_month<0)?(year-1):year;
		//Revert to last month if previous month is in previous year
		current_month = (current_month<0)?(current_month+12):current_month;
		//Iterate each days
		for (var i = begin; i<=end; i++)
		{
			//Set day link
			//XXX: current_month+1 cause we start at 0
			current_link.href = urls+'?jour='+i+'&mois='+(current_month+1)+'&annee='+current_year;
			current_link.alt = i;
			current_text.nodeValue = i;
			//Append to tr
			current_link.appendChild(current_text);
			current_td.appendChild(current_link);
			current_td.className = 'calendar_before';
			current_tr.appendChild(current_td);
			//Recreate new element
			current_text = elem_text.cloneNode(false);
			current_link = elem_a.cloneNode(false);
			current_td = elem_td.cloneNode(false);
		}

		//Generate a new row if selected month first day is a monday
		if (time.getDay() == 1)
		{
			//Append the generated tr to tbody
			tbody.appendChild(current_tr);
			current_tr = elem_tr.cloneNode(false);
		}

		//Generate selected month days
		//Set end
		end = js_calendar_last(month, year);
		//Set begin
		begin = 1;
		//Itearate each days
		for (var i = begin; i<=end; i++)
		{
			current_td.className = 'calendar_day';
			//Check if day is today (XXX: we use now_year cause now.getYear() may be in 2 digit format)
			if (i == now.getDate() && month == now.getMonth() && year == now_year)
				//Set current class
				current_td.className = 'calendar_current';
			//Set day link
			//XXX: current_month+1 cause we start at 0
			current_link.href = urls+'?jour='+i+'&mois='+(month+1)+'&annee='+year;
			current_link.alt = i;
			current_text.nodeValue = i;
			//Append to tr
			current_link.appendChild(current_text);
			current_td.appendChild(current_link);
			current_tr.appendChild(current_td);
			//Recreate new element
			current_text = elem_text.cloneNode(false);
			current_link = elem_a.cloneNode(false);
			current_td = elem_td.cloneNode(false);
			//End the row after each sunday
			if ((i+time.getDay()+6)%7 == 0)
			{
				//Append the generated tr to tbody
				tbody.appendChild(current_tr);
				current_tr = elem_tr.cloneNode(false);
			}
		}

		//Generate next month days
		//Set time to first day of next month
		time.setMonth(month + 1);
		time.setDate(1);
		//Set the begin
		begin = 1;
		//Set the end
		end = (8 - time.getDay())%7;
		//Increase of 7 the end if no one should be displayed
		if (end == 0)
			end += 7;
		//Set current generated next month and year
		current_month = month + 1;
		current_year = (current_month>11)?(year+1):year;
		//Revert to first month if next month is in next year
		current_month = (current_month>11)?(current_month-12):current_month;
		//Itearate each days
		for (var i = begin; i<=end; i++)
		{
			//Set day link
			//XXX: current_month+1 cause we start at 0
			current_link.href = urls+'?jour='+i+'&mois='+(current_month+1)+'&annee='+current_year;
			current_link.alt = i;
			current_text.nodeValue = i;
			//Append to tr
			current_link.appendChild(current_text);
			current_td.appendChild(current_link);
			current_td.className = 'calendar_after';
			current_tr.appendChild(current_td);
			//Recreate new element
			current_text = elem_text.cloneNode(false);
			current_link = elem_a.cloneNode(false);
			current_td = elem_td.cloneNode(false);
		}

		//Append the generated tr to tbody
		tbody.appendChild(current_tr);
		current_tr = elem_tr.cloneNode(false);
	}

	//Calendar replace function
	function js_calendar_replace(container, month, year)
	{
		//Remove the calendar
		js_calendar_remove(container);
		//Increase or decrease month and year if invalid
		if (month < 0)
		{
			//Readd 12 month
			month += 12;
			//Decrease the year
			year--;
		}
		if (month > 11)
		{
			//Remove 12 month
			month -= 12;
			//Increase the year
			year++;
		}
		//Add the new calendar
		js_calendar(container, month, year);
	}

	//Celendar remove function
	function js_calendar_remove(node)
	{
		//Check if node has child
		if (node.childNodes != undefined && node.childNodes.length > 0)
		{
			//Set end
			var end = node.childNodes.length;
			//Loop between all child
			for (var i = 0; i < end; i++)
			{
				//Check if there are subchilds
				//XXX: the 0 is because we remove first child from queue each time
				if (node.childNodes[0].childNodes.length > 0)
					//Remove subchilds
					js_calendar_remove(node.childNodes[0]);
				//Remove the node
				node.removeChild(node.childNodes[0]);
			}
		}
	}

