/**
 * moonAge.js : Cálculo de la edad de la luna para conocer la fase lunar
 */


function CalculaEpacta(ano){
	var aux = ano%19;
	aux = aux * 11;
	aux = aux%30;
	aux = aux - 1;
	return aux;
}

function Bisiesto(ano){
	if ((ano%4 == 0) && ((ano%100 != 0) || (ano%400 == 0)))
		return true;
	else 
		return false;
}


function moon_age(milliseconds){
	var date;
	if (milliseconds ==0)
		date = new Date();
	else date = new Date(milliseconds);

	var year = date.getFullYear(); //returns the year
	var month = date.getMonth(); // returns the month (0-11)
	month = month + 1;
	var day = date.getDate(); // returns day of the month (1-31)


	var epacta = CalculaEpacta (year);

//	Moon age
	var age;
	var bisiesto = 0;
	if (Bisiesto(year))
		bisiesto = 1;
	if (month > 2)
		age = epacta + (month - 2) + bisiesto;
	else if (month == 2)
		age = epacta + 1;
	else
		age = epacta;
	age = age + day;

	if (age > 29)
		age = age%30;

	return age;

}