function CheckDate(month,dayno)

{
   var retval = new String(dayno);
   var daystr = new String();
   var color = new String();
   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])
            {
               daystr = daystr + apps[app][4] + "/";
               color = datetypes[apps[app][5]][1];
            }
         }
         else
         {
            if(dayno >= apps[app][0] && dayno <= apps[app][2])
            {
               daystr = daystr + apps[app][4] + "/";
               color = datetypes[apps[app][5]][1];
            }
         }
      }
      else if(m == apps[app][3]) // second month
      {
         if(dayno <= apps[app][2])
         {
            daystr = daystr + apps[app][4] + "/";
            color = datetypes[apps[app][5]][1];
         }
      }
      else if( m > apps[app][1] && m < apps[app][3] )
      {    
         daystr = daystr + apps[app][4] + "/";
         color = datetypes[apps[app][5]][1];
      }
   }

   daystr = daystr.substring(0,daystr.length-1);

   var blue = parseInt(color.substring(color.length - 6),16) % 256; 
   var green = (parseInt(color.substring(color.length - 6),16) - blue) / 256 % 256;
   var red = (parseInt(color.substring(color.length - 6),16) - blue - (green * 256)) / (256*256);
   var tColor = "Navy";

   if ((blue + green + red) / 3 < 150)
   {
      tColor = "White";	
   }
   retval = "<div style='color : " + tColor + "; background-color : " + color + "; border : 0; cellpadding : 0' title='" + daystr + "'>" + dayno + "</div>";
   return retval;
}

function PrintMonth(month)
{
   var done = false;
   var day = 0;
   var week = 0;

   document.write("<table class='inner'><caption><b>" + months[month] + "</b></caption><thead>");
   document.write("<th>Su</th><th>M</th><th>Tu</th><th>W</th><th>Th</th><th>F</th><th>Sa</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;
      }
      week++;
   }
   while(week<6)
   {
   	document.write("<tr><td>&nbsp</td></tr>")
  	week++;
   }
   document.write("</table>");
}

function PrintWeek(monthno,start,min,max)
{
   var d;
   var desc;

   for(var j = 0; j < 7; j++)
   {
      document.write("<td width='20'>");
      d = start + j;
      if(d >= min && d < max + min)
      {
         desc = CheckDate(monthno,d - min + 1);
         document.write(desc);
      }
      document.write("</td>");
   }
}

function PrintDays()
{
    document.write("<table class='inner'><caption><b>Day color definitions</b></caption>")
    
    for(var i = 0; i < datetypes.length; i++)
    {
      document.write("<tr>");
      document.write("<td width='20'>")
      document.write("<div style='background-color : " + datetypes[i][1] + "; border : solid #000000 0px;'>&nbsp</div>")
      document.write("</td>")
      document.write("<td>")
      document.write(datetypes[i][0])
      document.write("</td>")
      document.write("</tr>");
   }
   document.write("</table>")
}
