// MODIFICATIONS MADE BY BRIAN OF SCRIPTASYLUM.COM NOTED
// BY "BG MODIFY" IN COMMENTS:
// - Now works in Netscape 4, Netscape 6, and IE 4.
// - Converted buttons, words, etc to English.
// SOME DOCUMENTATION IS IN FRENCH (I THINK) SO ANY SUPPORT
// SHOULD BE OBTAINED FROM THE AUTHOR AT THE ADDRESS BELOW

ns4 = (navigator.appName.indexOf("Netscape")>=0 && document.layers)? true : false;
ie4 = (document.all && !document.getElementById)? true : false;
ie5 = (document.all && document.getElementById)? true : false;
ns6 = (document.getElementById && navigator.appName.indexOf("Netscape")>=0 )? true: false;
w3c = (document.getElementById)? true : false;
// END BG MODIFY

var HTMLCode = "";
var DaysList = new Array("","Пн","Вт","Ср","Чт","Пт","Сб","Вс");
var MonthsList = new Array("Mois_Vide","Январь","Феврать","Март","Апрель","Май","Июнь","Июль","Август","Сентабрь","Октябрь","Ноябрь","Декабрь");
var MonthLength = new Array("Mois_longueur_vide",31,29,31,30,31,30,31,31,30,31,30,31);

var QueryDate = 0;      // Jour demande (date)
var QueryMonth = 0;     // Mois demande
var QueryYear = 0;      // Annee demandee
var QueryDay = 0;       // Jour de la semaine du jour demande, inconnu
var FirstDay = 0;       // Jour de la semaine du 1er jour du mois
var WeekRef = 0;        // Numerotation des semaines
var WeekOne = 0;        // Numerotation des semaines

//var url = "";   // Ссылка, по которой переходит пользователь, кликнув по числу месяца

var MinYear = 2005;
var Today = new Date();
var TodaysYear = Today.getYear();
var TodaysMonth = Today.getMonth() + 1;
var TodaysDate = Today.getDate();
var TodaysDay = Today.getDay() + 1;
if (TodaysYear < 2000) { TodaysYear += 1900; }

var SelDate = TodaysDate;

var topicId;
// On commence par verifier les donnees fournies par l'utilisateur
function CheckData(tid,year,month,day)
{
//	QueryDate = document.Cal.Date.selectedIndex + 1;
	if (tid) topicId = tid;
	QueryDate = day?day:TodaysDate;	
	QueryMonth = month?month:(document.Cal.month.selectedIndex + 1);
	QueryYear = year?year:(MinYear + document.Cal.year.selectedIndex);

	MonthLength[2] = CheckLeap(QueryYear);

	// on teste si la date choisie est anterieure au lundi 20 decembre 1582
	if ((QueryYear * 10000 + QueryMonth * 100 + QueryDate) < 15821220)
	{
		alert("You have chosen a date before Dec 20th 1582.\n\nPlease make another selection.");
		document.Cal.reset();
		CheckData(tid,year,month,day);
	}
	else if (MonthLength[QueryMonth] < QueryDate)       // on verifie si la date est coherente
	{
		alert("Il n'y a pas " + QueryDate + " jours en " + MonthsList[QueryMonth] + " " + QueryYear + " mais " + MonthLength[QueryMonth] + ". \nVeuillez choisir une autre date.");
		document.Cal.reset();
		CheckData(tid,year,month,day);
	}
	else { DisplaySchedule(year,month,day); }

}

// Teste une annee pour determiner si elle est bissextile ou pas
function CheckLeap(yy)
{
	if ((yy % 100 != 0 && yy % 4 == 0) || (yy % 400 == 0)) { return 29; }
	else { return 28; }
}

// Renvoie le numero de la semaine correspondant a la date requise
function DefWeekNum(dd)
{
	numd = 0;
	numw = 0;
	for (n=1; n<QueryMonth; n++)
	{
		numd += MonthLength[n];
	}
	numd = numd + dd - (9 - DefDateDay(QueryYear,1,1));
	numw = Math.floor(numd / 7) + 1;

	if (DefDateDay(QueryYear,1,1) == 1) { numw++; }
	return numw;
}

// Renvoie le numero du jour de la semaine correspondant a la date requise
function DefDateDay(yy,mm,dd)
{
	return Math.floor((Date2Days(yy,mm,dd)-2) % 7) + 1;
}

// Transforme la date en nb de jours theoriques
function Date2Days(yy,mm,dd)
{
	if (mm > 2)
	{
		var bis = Math.floor(yy/4) - Math.floor(yy/100) + Math.floor(yy/400);
		var zy = Math.floor(yy * 365 + bis);
		var zm = (mm-1) * 31 - Math.floor(mm * 0.4 + 2.3);
		return (zy + zm + dd);
	}
	else
	{
		var bis = Math.floor((yy-1)/4) - Math.floor((yy-1)/100) + Math.floor((yy-1)/400);
		var zy = Math.floor(yy * 365 + bis);
		return (zy + (mm-1) * 31 + dd);
	}
}
function HideDiv(name)
{
	if(ie4)
	{
		document.all[name].style.visibility = 'hidden';
		document.all[name].style.position = 'absolute';
	}
	if(w3c)
	{
		document.getElementById(name).style.visibility = 'hidden';
		document.getElementById(name).style.position = 'absolute';
	}		
} 
function ShowDiv(name)
{
	if(ie4)
	{
		document.all[name].style.visibility = 'visible';
		document.all[name].style.position = 'relative';
	}
	if(w3c)
	{
		document.getElementById(name).style.visibility = 'visible';
		document.getElementById(name).style.position = 'relative';
	}		
} 

function setSelDate(date)
{
	SelDate = date;
	CheckData();
	HTMLCode = QueryDate + ", " + MonthsList[QueryMonth] + ", " +  QueryYear ;
	if(ie4){document.all['text'].innerHTML = HTMLCode;}
	if(w3c)	{document.getElementById('text').innerHTML = HTMLCode;}	
	HideDiv('Calendrier');
	ShowDiv('text');
} 


// ELEMENT FINDER. BEGIN BG MODIFY
function findid(name,doc){
	var i,layer;
	for(i=0;i<doc.layers.length;i++){
		layer=doc.layers[i];
		if(layer.name==name)return layer;
		if(layer.document.layers.length>0)if((layer=findid(name,layer.document))!=null)return layer;
	}
	return null;
}
// END BG MODIFY


// Produit le code HTML qui formera le calendrier
function DisplaySchedule(tid,year,month,day)
{
	HTMLCode = "<table cellspacing=0 cellpadding=3 border=1 bordercolor=#404056 class='calendar'>";
	QueryDay = DefDateDay(QueryYear,QueryMonth,QueryDate);
	WeekRef = DefWeekNum(QueryDate);
	WeekOne = DefWeekNum(1);
//	HTMLCode += "<tr align=center ><td colspan=8 style='font-size: 11px; text-align:center'><table border=0 cellspacing=0 cellpadding=0 width='100%' style='text-align:center'><tr><td style='font-size: 11px;' width=25><a class='calendar' onClick=\"BackYear(); CheckData()\"><<</a></td><td width='25'><a class='calendar' onClick=\"BackMonth(); CheckData()\"><</a></td><td><b>" + MonthsList[QueryMonth] + " " + QueryYear + "</b></td><td width=25><a class='calendar' onClick=\"NextMonth(); CheckData()\">></a></td><td width=25><a class='calendar' onClick=\"NextYear(); CheckData()\">>></a></td></tr></table> </td></tr><tr align=center>";
	HTMLCode += "<tr align=center ><td colspan=8 style='font-size: 11px; text-align:center'><b>" + MonthsList[QueryMonth] + " " + QueryYear + "</b> </td></tr><tr align=center>";

	for (s=1; s<8; s++)
	{
		if (QueryDay == s) { HTMLCode += "<td width=35 style='font-size:11px;'>&nbsp;<b><font color=#ff6600>" + DaysList[s] + "</font></b>&nbsp;</td>"; }
		else { HTMLCode += "<td width=35 style='font-size:11px;'>&nbsp;<b>" + DaysList[s] + "</b>&nbsp;</td>"; }
	}

	HTMLCode += "<td style='font-size:11px;'><b><font color=#888888>Неделя</font></b></td></tr>";
	a = 0;

	for (i=(1-DefDateDay(QueryYear,QueryMonth,1)); i<MonthLength[QueryMonth]; i++)
	{
		HTMLCode += "<tr align=center style='font-size:11px;'>";
		for (j=1; j<8; j++)
		{
			if ((i+j) <= 0) { HTMLCode += "<td>&nbsp;</td>"; }
			else if ((QueryYear==TodaysYear && QueryMonth==TodaysMonth && (i+j)>TodaysDate) || (QueryYear>=TodaysYear && QueryMonth>TodaysMonth )) { HTMLCode += "<td style='font-size:11px;'><font color=#888888>" + (i+j) + "</font></td>"; }
			else if ((i+j) == day) { HTMLCode += "<td style='font-size:11px;'><b><font color=#ff6600>" + (i+j) + "</font></b></td>"; }
			else if ((i+j) == QueryDate ) { HTMLCode += "<td style='font-size:11px;'><b><font color='#ff6600' >" + (i+j) + "</font></b></td>"; }
			else if ((i+j) > MonthLength[QueryMonth]) { HTMLCode += "<td style='font-size:11px;'>&nbsp;</td>"; }
			else { HTMLCode += "<td><a href='index.php?tid="+topicId+"&year="+QueryYear+"&month="+QueryMonth+"&date="+(i+j)+"' class='calendar'>" + (i+j) + "</a></td>"; }
		}
		if ((WeekOne+a) == WeekRef) { HTMLCode += "<td style='font-size:11px;'><b><font color=#888888 >" + WeekRef + "</font></b></td>"; }
		else { HTMLCode += "<td style='font-size:11px;'><font color=#888888>" + (WeekOne+a) + "</font></td>"; }
		HTMLCode += "</tr>";
		a++;
		i = i + 6;
	}

	// ALLOWS CROSS-BROWSER WRITING IN DOCUMENT. BEGIN BG MODIFY
	if(ie4)document.all['Calendrier'].innerHTML = HTMLCode + "</table>";
	if(w3c)document.getElementById('Calendrier').innerHTML= HTMLCode + "</table>";
	if(ns4){
		var tns4=findid('Calendrier',document);
		tns4.document.open();
		tns4.document.write('<center>'+HTMLCode+'</table></center>');
		tns4.document.close();
	}
	// END BG MODIFY

}

function BackMonth()
{
	if (document.Cal.month.selectedIndex == 0 && document.Cal.year.selectedIndex == 0)
		return false;
	document.Cal.month.selectedIndex = document.Cal.month.selectedIndex - 1;
	if (document.Cal.month.selectedIndex == -1) 
	{
		document.Cal.year.selectedIndex = document.Cal.year.selectedIndex - 1; 
		document.Cal.month.selectedIndex = 11; 
	}
}
function BackYear()
{
	document.Cal.year.selectedIndex = document.Cal.year.selectedIndex - 1;
	if (document.Cal.year.selectedIndex == -1) { document.Cal.year.selectedIndex = 0; }
}

function NextMonth()
{
	if (document.Cal.month.selectedIndex == 11) 
	{ 
		document.Cal.year.selectedIndex = document.Cal.year.selectedIndex+1;
		document.Cal.month.selectedIndex = 0;
	}
	else 
	{
		document.Cal.month.selectedIndex = document.Cal.month.selectedIndex + 1
	};
}
function NextYear()
{
	document.Cal.year.selectedIndex = document.Cal.year.selectedIndex + 1;
	if (document.Cal.year.selectedIndex == -1) { document.Cal.year.selectedIndex = 0; }
}

//EVENT HANDLERS
window.onload=CheckData;
if(ns4)window.onresize=function(){
	setTimeout('history.go(0)',400);
}


