var yearcurso = 2004
var months = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"];
var daycounts = [31,28,31,30,31,30,31,31,30,31,30,31]; //for leap years, remember to set february to 29 days
//2002 firstdays = [1,4,4,0,2,5,0,3,6,1,4,6];
//2003 firstdays = [2,5,5,1,3,6,1,4,0,2,5,0];
var firstdays = [3,6,7,3,5,1,3,6,2,4,0,2];

// This is where you put in the appointments. follow pattern [fromday,frommonth,today,tomonth,message]

function DisplayCalendar() {
	document.write("<DIV id=\"HOJA4_div\" style=\"position:absolute; width:655px; height:450px; z-index:23; left: 50px; top: 50px; background-color: #CCCCCC; layer-background-color: #CCCCCC; border: 1px none #000000; visibility: visible\">")
        document.write("<p align=\"center\"><font size=6 face=\"Arial, Helvetica, sans-serif\" color=\"#006699\">2004 Calendario</font></font></p>")

	document.write("<TABLE  align=\"center\" >")
	for (var i = 0; i < 3; i++) {
		document.write("<TR valign=top>")
		for (var j = 0; j < 4; j++) {
                                document.write("<TD class=\"outer\" width=\"18\" height=\"38\">")
				PrintMonth(4*i + j)				
				document.write("</TD>")
		}
		document.write("</TR>")
	}
	document.write("</TABLE>")
	document.write("</DIV>")
}

function CheckDate(month,dayno)
{
   var retval = new String(dayno);
   var m = month + 1;
   for(var app = 0; app < apps.length; app++)
   {
      if(m == apps[app][1] ) //first month
      {
         if(apps[app][3] - apps[app][1] > 0)
         {
            if(dayno >= apps[app][0])
            {
               retval = "<div class='hol' title='" + apps[app][4] + "'>" + dayno + "</div>";
            }
         }
         else
         {
            if(dayno >= apps[app][0] && dayno <= apps[app][2])
            {
               retval = "<div class='hol' title='" + apps[app][4] + "'>" + dayno + "</div>";
            }
         }
      }
      else if(m == apps[app][3]) // second month
      {
         if(dayno <= apps[app][2])
         {
            retval = "<div class='hol' title='" + apps[app][4] + "'>" + dayno + "</div>";
         }
      }
      else if( m > apps[app][1] && m < apps[app][3] )
      {    
         retval = "<div class='hol' title='" + apps[app][4] + "'>" + dayno + "</div>";
      }
   }
   return retval;
}

function PrintMonth(month)
{
   var done = false;
   var day = 0;
   var pridia = new Date(yearcurso,month,1).getDay()

   if (month == 1)
   {
	endDate	= new Date (yearcurso,month+1,1);
	endDate = new Date (endDate	- (24*60*60*1000));
	daycounts[month] = endDate.getDate()
   }

   firstdays[month] = pridia - 1
   document.write("<b>" + months[month] + "</b>");
   document.write("<table class='inner'>");
//   document.write("<table class='inner'><caption><b>" + months[month] + "</b></caption>");
   document.write("<thead><th>Lun</th><th>Mar</th><th>Mie</th><th>Jue</th><th>Vie</th><th>Sab</th><th>Dom</th></thead>");
   while(!done)
   {
      document.write("<tr>");
      PrintWeek(month,day, firstdays[month], daycounts[month]);
      document.write("</tr>");
      day = day + 7;
      if( day > daycounts[month] + firstdays[month])
      {
         done = true;
      }
   }
   document.write("</table><p>");
}

function PrintWeek(monthno,start,min,max)
{
   var d;
   var desc;
   for(var j = 0; j < 7; j++)
   {
      document.write("<td>");
      d = start + j;
      if(d >= min && d < max + min)
      {
         desc = CheckDate(monthno,d - min + 1);
         document.write(desc);
      }
      document.write("</td>");
   }
}
