moonpha= new Image(28, 28);

monthNames = new Array(13)
monthNames[1]  = "janvier"
monthNames[2]  = "février"
monthNames[3]  = "mars"
monthNames[4]  = "avril"
monthNames[5]  = "mai"
monthNames[6]  = "juin"
monthNames[7]  = "juillet"
monthNames[8]  = "août"
monthNames[9]  = "septembre"
monthNames[10] = "octobre"
monthNames[11] = "novembre"
monthNames[12] = "décembre"
monthDays = new Array(13)
monthDays[1]  = 31
monthDays[2]  = 29
monthDays[3]  = 31
monthDays[4]  = 30
monthDays[5]  = 31
monthDays[6]  = 30
monthDays[7]  = 31
monthDays[8]  = 31
monthDays[9]  = 30
monthDays[10] = 31
monthDays[11] = 30
monthDays[12] = 31
dayNames = new Array(8)
dayNames[1]  = "dimanche"
dayNames[2]  = "lundi"
dayNames[3]  = "mardi"
dayNames[4]  = "mercredi"
dayNames[5]  = "jeudi"
dayNames[6]  = "vendredi"
dayNames[7]  = "samedi"

function getMoonAge(year,month,day)
{	
	d = Math.floor(year/19)
	r = year-(d*19) //r is the remainder of (year/19)
	while (r>9)
	{	
		r = r-19
	}
	r = r*11
	while (r>29)
	{	
		r = r-30
	}
	if (month<3)
	{	
	month = month+2
	}
	r = r+month+day
	r = r-2
	while(r>29)
	{	
		r = r-30
	}
	while(r<0)
	{	
		r = r+30
	}
	return r
}


function setmoon()
{
	theDate = new Date()
	theYear = theDate.getYear()
	theMonth = theDate.getMonth()+1
	theDay = theDate.getDate()
	theMoonAge = getMoonAge(theYear, theMonth, theDay)
	moonpha.src="images/phases_lune/Day"+ escape(theMoonAge)+".gif"
	image = eval("moonpha.src");
   	document.moonphase.src = image	
}

function getMoonPhase(moonAge)
{
	if (moonAge<2) return "Lors ou juste après la nouvelle Lune, la nuit est propice à l'astrophotographie."
	if (moonAge<5) return "Bon moment pour photographier un fin croissant de Lune en début de soirée, ou essayer de photographier la lumière cendrée."
	if (moonAge<7) return "La Lune présente un joli croissant en début de soirée. Sa lumière devient gênante pour les observations du ciel profond. Des clichés pris à ce moment mettent en évidence le relief lunaire aux abords du terminateur."
	if (moonAge==7) return "Premier quartier. Admirez les jeux d'ombre et de lumière au terminateur."
	if (moonAge<14) return "Lune croissante en phase gibbeuse. Pas d'observation du ciel profond ce soir..."
	if (moonAge==14) return "Bientôt la pleine Lune. A part une observation planétaire, il y a peu à faire ce soir..."
	if (moonAge==15) return "Pleine Lune. A part une observation planétaire, il y a peu à faire ce soir..."
	if (moonAge<22) return "Lune décroissante en phase gibbeuse. Pas d'observation du ciel profond ce soir..."
	if (moonAge==22) return "Dernier quartier. Admirez les jeux d'ombre et de lumière au terminateur."
	if (moonAge<27) return "La Lune se lève tard dans la soirée. Profitez-en pour observer ou photographier le ciel profond. Les plus tenaces peuvent tenter des photographies de la surface lunaire aux petites heures. En effet, l'air est plus calme en fin de nuit."
	if (moonAge<29) return "Bon moment pour photographier un fin croissant de Lune avant le lever du soleil, sous un aspect peu souvent montré."
	if (moonAge<30) return "Un peu avant ou lors de la nouvelle Lune, la nuit est propice à l'astrophotographie."
}

function getLongDate(dateObj)
{	
	theDay = dayNames[dateObj.getDay()+1]
	theMonth = monthNames[dateObj.getMonth()+1]
	theDate = dateObj.getDate()
	theYear = dateObj.getYear()
	if (theYear<100)
	{	
		theYear = 1900+dateObj.getYear()
	}
	return ""+theDay+" "+theDate+" "+theMonth+" "+theYear
}

function getNextFull(moonAge)
{	
	currMilSecs = (new Date()).getTime()
	daysToGo = 15 - moonAge
	while(daysToGo<2)
	{	
		daysToGo = daysToGo+29
	}
	milSecsToGo = daysToGo*24*60*60*1000
	nextMoonTime = currMilSecs+milSecsToGo
	nextMoonDate = new Date(nextMoonTime)
	return nextMoonDate
}

function getNextNew(moonAge)
{	
	currMilSecs = (new Date()).getTime()
	daysToGo = 29.5 - moonAge
	while(daysToGo<2)
	{	
		daysToGo = daysToGo+29.5
	}
	milSecsToGo = daysToGo*24*60*60*1000
	nextMoonTime = currMilSecs+milSecsToGo
	nextMoonDate = new Date(nextMoonTime)
	return nextMoonDate
}

function montharr(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11)
{
	this[0] = m0;
	this[1] = m1;
	this[2] = m2;
	this[3] = m3;
	this[4] = m4;
	this[5] = m5;
	this[6] = m6;
	this[7] = m7;
	this[8] = m8;
	this[9] = m9;
	this[10] = m10;
	this[11] = m11;
}

function calendar(month,year)
{
	var today = new Date(year,month-1,1);
	var realday= new Date();
	var thisDay;
	var monthDays = new montharr(31, 28, 31, 30, 31, 30, 31, 31, 30,31, 30, 31);
	year = today.getYear();
	thisDay = realday.getDate();
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
		monthDays[1] = 29;
	nDays = monthDays[month-1];
	firstDay = today;
	firstDay.setDate(1,month,year); 
	testMe = firstDay.getDate();
	if (testMe == 2)
		firstDay.setDate(0);    
	startDay = firstDay.getDay();
	document.writeln("<LEFT>");
	document.write("<TABLE BORDER='0' CELLSPACING='20'>");
	document.write("<TR><TD COLSPAN='7' ALIGN='center'>");
	document.write("<BR/>"+monthNames[month]+" "+year+"<BR/>"+"<BR/></TD></TR>");
	document.write("<TR align='center'><TD>Lun</TD><TD>Mar</TD><TD>Mer</TD><TD>Jeu</TD><TD>Ven</TD><TD>Sam</TD><TD>Dim");
	document.write("</TD><TR>");
	column = 0;
	for (i=1; i<startDay; i++) {
		document.write("<TD>");
		column++;
	}
	startp=getMoonAge(year-1900, month, 1)-1;
	for (i=1; i<=nDays; i++) {
		document.write("<TD>");
		if (startp>28)
		{	
			startp=-1
		}
		startp++;
		document.write("<IMG SRC='images/phases_lune/Day"+ escape(startp)+".gif' WIDTH='38' HEIGHT='38' ALIGN='left' VSPACE='3'>")
		if (i == thisDay)
			document.write("<FONT COLOR=\"#FF0000\">")
		document.write("<B>"+i+"</B>")
		if (i == thisDay)
			document.write("</FONT>")
		document.write("<br><FONT COLOR=\'#FF7F00\'>"+startp);
		column++;
		if (column == 7) {
			document.write("<TR>"); 
			column = 0;
		}
	}
	document.write("</TABLE>");
	document.writeln("</LEFT>");
}