//THIS CLASS DRAWS THE CALENDAR.  To DRAW THE CLAENDAR CALL THE drawMonth(int m, int y)

/**
 *
 * @author  alieberman
 * @version 
 */
 
 import java.io.*;
 
public class perpetualCalendar {
   int mo;
   int ye;
  // StringBuffer out = new StringBuffer();
   int DaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
   String months[]= {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
   static final int FEBRUARY = 1;
   
   private PrintWriter	out = null;
   
    /** Creates new perpetualCalendar */
    public perpetualCalendar(PrintWriter itsPrintWriter) {
    	out = itsPrintWriter;
       
    }
    
   /*
    public String toString() {
         
         return out.toString();
    
    }
*/
    
    public String drawDay(int dayNumber, int monthNumber){
        String d = new String();
        

        if (dayNumber > DaysInMonth[monthNumber] || dayNumber< 1)
        { 
            d ="<td></td>";
        }
        else{

        d="<td><a href=\"javascript:choose("+ dayNumber + ")\">" +       
           dayNumber + "</a></td>";
        }

        return d;
        }
   
 
    public void drawWeek(int firstDayNumber, int displacement, int weeklength, int monthNumber){
        String w = new String(); 
        w += "<tr>";
        while(weeklength > 0){
            if (displacement == 0){
                w += drawDay(firstDayNumber, monthNumber);
                weeklength --;
                firstDayNumber++;
            }
            else{
                 w += drawDay(800, 0); 
                 weeklength--;
    
                 displacement --;               
                }
            }
        w += "</tr>";
        out.println(w);
      }
      










      public void drawMonth(int m, int y){
       		//JAP
         // out = new StringBuffer();
          out.println(months[m]);
          out.println(y);


          out.println("<form METHOD=GET NAME=\"date2\" ");
          out.println("ACTION=\"/jd/Calendar\">");
          out.println("<input type=hidden NAME=\"d\">");
          out.println("<script language=\"JavaScript\">");
          out.println("function choose(n)");
          out.println("{document.date2.d.value = n;");
          out.println("document.date2.submit();}");
          out.println("</script>");



          out.println("<table>");
          int disp = this.CalcFirstOfMonth(y, m);
          int tempFirstDay = 7-(disp-1);
          drawWeek(1, disp, 7, m);
          while(tempFirstDay < DaysInMonth[m]){
          drawWeek(tempFirstDay, 0, 7, m);
          tempFirstDay += 7;
          }
          out.println("</FORM>");
          out.println("</table>");
      }
     
       
       public void drawMonth(String m, int y){
           if ("Jan".equals(m) ){
                       this.drawMonth(0, y);
                      }
           else if ("Feb".equals(m)){
                       this.drawMonth(1, y);
                      }
           else if ("Mar".equals(m)){
                       this.drawMonth(2, y);
                      }
           else if ("Apr".equals(m)){
                       this.drawMonth(3, y);
                      }
           else if ("May".equals(m)){
                       this.drawMonth(4, y);
                      }
           else if ("June".equals(m)){
                       this.drawMonth(5, y);
                      }
           else if ("July".equals(m)){
                       this.drawMonth(6, y);
                      }
           else if ("Aug".equals(m)){
                       this.drawMonth(7, y);
                      }
           else if ("Sept".equals(m)){
                       this.drawMonth(8, y);
                      }
           else if ("Oct".equals(m)){
                       this.drawMonth(9, y);
                      }
           else if ("Nov".equals(m)){
                       this.drawMonth(10, y);
                      }
           else if ("Dec".equals(m)){
                       this.drawMonth(11, y);
                     }
           else {
                  //JAP
                // out = new StringBuffer();
                   out.println("this is not a real month");
                }

      }
      
      
  int CalcFirstOfMonth(int year, int month)
  /*
     USE:  Calculates day of the week the first day of the month falls on.
     IN:   year = given year after 1582 (start of the Gregorian calendar).
           month = 0 for January, 1 for February, etc.
     OUT:  First day of month: 0 = Sunday, 1 = Monday, etc.
  */
    {
    int firstDay;       /* day of week for Jan 1, then first day of month */
    int i;              /* to traverse months before given month */
  
    /* Start at 1582, when modern calendar starts. */
    if (year < 1582) return (-1);
  
    /* Catch month out of range. */
    if ((month < 0) || (month > 11)) return (-1);
  
    /* Get day of week for Jan 1 of given year. */
    firstDay = CalcJanuaryFirst(year);
  
    /* Increase firstDay by days in year before given month to get first day
     * of month.
     */
    for (i = 0; i < month; i++)
      firstDay += DaysInMonth[i];
  
    /* Increase by one if month after February and leap year. */
    if ((month > FEBRUARY) && IsLeapYear(year)) firstDay++;
  
    /* Convert to day of the week and return. */
    return (firstDay % 7);
    } // CalcFirstOfMonth
  
  boolean IsLeapYear(int year)
  /*
     USE:  Determines if given year is a leap year.
     IN:   year = given year after 1582 (start of the Gregorian calendar).
     OUT:  TRUE if given year is leap year, FALSE if not.
     NOTE: Formulas capture definition of leap years; cf CalcLeapYears().
  */
    {
  
    /* If multiple of 100, leap year iff multiple of 400. */
    if ((year % 100) == 0) return((year % 400) == 0);
  
    /* Otherwise leap year iff multiple of 4. */
    return ((year % 4) == 0);
    } // IsLeapYear
    
    
    
    
    
    
    

 
  int CalcJanuaryFirst(int year)
  /*
     USE:  Calculate day of the week on which January 1 falls for given year.
     IN:   year = given year after 1582 (start of the Gregorian calendar).
     OUT:  Day of week for January 1: 0 = Sunday, 1 = Monday, etc.
     NOTE: Formula starts with a 5, since January 1, 1582 was a Friday; then
           advances the day of the week by one for every year, adding the
           number of leap years intervening, because those years Jan 1
           advanced by two days. Calculate mod 7 to get the day of the week.
  */
    {
    /* Start at 1582, when modern calendar starts. */
    if (year < 1582) return (-1);
  
    /* Start Fri 01-01-1582; advance a day for each year, 2 for leap yrs. */
    return ((5 + (year - 1582) + CalcLeapYears(year)) % 7);
    } // CalcJanuaryFirst
  


  int CalcLeapYears(int year)
  /*
     USE:  Calculate number of leap years since 1582.
     IN:   year = given year after 1582 (start of the Gregorian calendar).
     OUT:  number of leap years since the given year, -1 if year < 1582
     NOTE: Count doesn't include the given year if it is a leap year.
           In the Gregorian calendar, used since 1582, every fourth year
           is a leap year, except for years that are a multiple of a
           hundred, but not a multiple of 400, which are no longer leap
           years. Years that are a multiple of 400 are still leap years:
           1700, 1800, 1990 were not leap years, but 2000 will be.
  */
    {
    int leapYears;      /* number of leap years to return */
    int hundreds;       /* number of years multiple of a hundred */
    int fourHundreds;   /* number of years multiple of four hundred */
  
    /* Start at 1582, when modern calendar starts. */
    if (year < 1582) return (-1);
  
    /* Calculate number of years in interval that are a multiple of 4. */
    leapYears = (year - 1581) / 4;
  
    /* Calculate number of years in interval that are a multiple of 100;
     * subtract, since they are not leap years.
     */
    hundreds = (year - 1501) / 100;
    leapYears -= hundreds;
  
    /* Calculate number of years in interval that are a multiple of 400;
     * add back in, since they are still leap years.
     */
    fourHundreds = (year - 1201) / 400;
    leapYears += fourHundreds;
  
    return (leapYears);
    } // CalcLeapYears
 
    
}





