var synodic = 29.53058867;     // the synodic period
var msPerDay = 24 * 60 * 60 * 1000;   // milliseconds per day


function moonPhPer(anyDate)
  {
  // calculate the moon phase as a percent
  // a convenient New Moon date was Dec 18, 1998 at 21:36 UT (16:36 Eastern)
  // Updated on Aug 26, 2003 for more current full moon date, July 29, at 6:53 UT
  var baseDate = new Date();
  baseDate.setUTCFullYear(2003);
  baseDate.setUTCMonth(8);
  baseDate.setUTCDate(29);
  baseDate.setUTCHours(6);
  baseDate.setUTCMinutes(53);
  baseDate.setUTCSeconds(0);

  var diff = anyDate - baseDate + msPerDay;   // the difference in milliseconds
  var phase = Math.abs(diff) / (synodic * msPerDay); // phase percent
  phase *=100;        // make it a ##.##% figure
  while(phase>100)
    {
    phase -= 100;
    }
  return phase;
  }

function moonPhase(phase)
  // convert moon phase percent to nice words
  {
  
  if(phase >= 0 && phase < 5)
      
      var phaseName = "new.gif";
else      
  
  if(phase >= 5 && phase < 12.5)
    
    var phaseName = "ln1.gif";
    
  else if(phase >=12.5  && phase < 25)
    
    var phaseName = "ln2.gif";
    
  else if(phase >=25 && phase < 37.5)
    
    var phaseName = "cc1.gif";
    
  else if(phase >= 37.5 && phase <46 )
    
    var phaseName = "cc2.gif";
  
  else if(phase >= 46 && phase <55 )
    
    var phaseName = "full.gif";
    
  else if(phase >= 55 && phase < 62.5)
    
    var phaseName = "ll1.gif";
    
  else if(phase >=62.5 && phase <75 )
    
    var phaseName = "ll2.gif";
    
  else if(phase >=75 && phase < 87.5 )
    
    var phaseName = "cm2.gif  ";
    
  else if(phase >=87.5  && phase < 96)
    
    var phaseName = "cm1.gif";
  else if(phase >=96  && phase <100)
    
    var phaseName = "new.gif";
    
  return phaseName;
  }





monthNames = new Array(13);
monthNames[1]  = "Ene";
monthNames[2]  = "Feb";
monthNames[3]  = "Mar";
monthNames[4]  = "Abr";
monthNames[5]  = "May";
monthNames[6]  = "Jun";
monthNames[7]  = "Jul";
monthNames[8]  = "Ago";
monthNames[9]  = "Sep";
monthNames[10] = "Oct";
monthNames[11] = "Nov";
monthNames[12] = "Dic";
                 
dayNames = new Array(8);
dayNames[1]  = "Dom";
dayNames[2]  = "Lun";
dayNames[3]  = "Mar";
dayNames[4]  = "Mi&eacute;";
dayNames[5]  = "Jue";
dayNames[6]  = "Vie";
dayNames[7]  = "S&aacute;b";

function julday(mm, id, iyyy) {
id=id+7;
	if (iyyy == 0) {
		alert("Julday: there is no year zero");
		return 0;
	} else {
		if (iyyy < 0) { iyyy += 1; }
		if (mm > 2) {
			jy = iyyy;
			jm = 1.0 * mm + 1;
		} else {
  			jy = iyyy - 1;
  			jm = 1.0 * mm + 13;
		}
		j1 = 1.0 * Math.floor(365.25 *jy);
		j2 = 1.0 * j1 + Math.floor(30.6001 * jm);
		j3 = 1.0 * j2 + 1.0 * id;
		j4 = 1.0 * j3 + 1720995;
		if (id+31*(mm+12*iyyy) >= (15+31*(10+12*1582))) {
			  ja = 1.0 * Math.floor(0.01 * jy);
			jb = 1.0 * Math.floor(0.25 * ja);
			  j5 = 1.0 * j4 + 2;
			j6 = 1.0 * j5 - ja;
			jul = 1.0 * j6 + jb;
		} else {
			jul = j4;
		}
		return jul;
	}
}
var month, day, year;
function caldat(julian) {
	julian = 0 + julian;
	if (julian >= 2299161) {
		jalpha = Math.floor((1.0 * (julian - 1867216)-0.25)/36524.25);
		ja = 1 * julian + 1 + jalpha - Math.floor(0.25 * jalpha);
	} else {
		ja = julian;
	}
	jb = ja + 1524;
	jc = Math.floor(6680.0 + ((jb - 2439870)-122.1)/365.25);
	jd = 365 * jc + Math.floor(0.25 * jc);
	je = Math.floor((jb - jd)/ 30.6001);
	day = jb - jd - Math.floor(30.6001 * je);
	month = je - 1;
	if (month > 12) { month -= 12; }
	year = jc - 4715;
	if (month > 2) { --year; }
	if (year <= 0) { --year; }
}
var jd, frac;
function flmoon(n, nph) {
	RAD = 3.14159265/180.0;
	c = n + nph / 4.0;
	t = c / 1236.85;
	t2 = t * t;
	as = 359.2242 + 29.105356 * c;
	am = 306.0253 + 385.816918 * c + 0.010730 * t2;
	jd = 2415020 + 28 * n + 7 * nph;
	xtra = 0.75933 + 1.53058868 * c + ((1.178e-4) - (1.55e-7) * t) * t2;
	if (nph == 0 || nph == 2)
		xtra += (0.1734 - 3.93e-4 * t) * Math.sin(RAD * as) - 0.4068 * Math.sin(RAD * am);
	else if (nph == 1 || nph == 3)
		xtra += (0.1721 - 4.0e-4 * t) * Math.sin(RAD * as) - 0.6280 * Math.sin(RAD * am);
	else
		alert("nph is unknown in flmoon");
	i = (xtra > 0.0 ? Math.floor(xtra) :  Math.ceil(xtra - 1.0));
	jd += i;
	frac = xtra - i;
}

var field1=new Array(4);var field2=new Array(4);var field3=new Array(4);var field4=new Array(4);
function doit() {

	
	TZ=Math.round(-3 * 4) / 4;
	TZ=-4;
	
	timzon = TZ / 24.0;
	n = Math.floor(12.37 * (thisyear -1900 + ((1.0 * thismonth - 0.5)/12.0)));
	nph = 2;
	j1 = julday(thismonth,thisday,thisyear);
	flmoon(n,nph); 
	n += Math.floor(1.0 * (j1 - jd) / 28.0); cycle =0;
	for (var iii = 1; iii <= 12; iii++) {
		flmoon(n, nph);
		frac = 24.0 * (frac + timzon);
		if (frac < 0) {
			--jd;
			frac += 24.0;
		}
		if (frac > 12.0) {
			++jd;
			frac -= 12.0;
		} else
			frac += 12.0;
		i1 = Math.floor(frac);
		secs = 3600.0 * (frac - i1);
		i2 = Math.floor(secs / 60.0);
		i3 = Math.floor(secs - 60 * i2 + 0.5);
		caldat(jd);
		theDate = new Date(year,month-1,day,i1,i2,i3);
		if (nph == 2) { 
			field1[cycle] = dayNames[theDate.getDay()+1]+" "+theDate.getDate()+" "+monthNames[theDate.getMonth()+1];
		} else if (nph == 3) {
			field2[cycle]=dayNames[theDate.getDay()+1]+" "+theDate.getDate()+" "+monthNames[theDate.getMonth()+1];
		} else if (nph == 0) {
			field3[cycle]=dayNames[theDate.getDay()+1]+" "+theDate.getDate()+" "+monthNames[theDate.getMonth()+1];
		} else if (nph == 1) {
			field4[cycle]=dayNames[theDate.getDay()+1]+" "+theDate.getDate()+" "+monthNames[theDate.getMonth()+1];
		}
		if (nph == 3) {
			nph = 0;
			++n;
		} else
			++nph;
		if (nph == 2) {
			++cycle;
		}
	}

	


}

today = new Date(); thisyear = today.getYear(); 
thismonth = today.getMonth(); thisday = today.getDate();
if (thisyear < 50) { thisyear += 2000; }
if (thisyear < 100) { thisyear += 1900; }
thismonth += 1;
//TZ = Math.round(today.getTimezoneOffset() / 15) / 4;
//TZ = -TZ; 
doit();

