#! /Python21/python

"""
Program: calendar.py
Author : rob chavers
Purpose: shows a calendar for the year passed as a url variable:
             example.  http://calendar.py?year=2000
         You can jump to the month you want by passing that months name
             example.  http://calendar.py?year=2000?#June
         
Future: I want to display the days off for alphagene employees
         embedded in the calendar.

Output : HTML compatable
"""

import os
import sys
import cgi
import time
import string

cwd = '/html/users/www'
if os.environ.has_key('USER_DIR'):
    cwd = os.environ['USER_DIR']
href = '<A HREF="calendar.py?year=%d&month=%d">%s</A>'

########################################
#       Function Definitions
########################################

def small_month(month):
    global year, ref_year, time, href, this_year, this_month, this_day

    ref_year = year
    
    if month > 12:
        month = 1
        ref_year = ref_year +1

    if month < 1:
        month = 12
        ref_year = ref_year -1
        
    # create instance of first day of month,
    # get day of the week of the first day 0=sunday (like a calendar)
    monthStart = time.localtime(time.mktime(\
        (ref_year, month, 1, 0, 0, 0, 0, 0, 0))) 
    firstDay = string.atoi(time.strftime('%w', monthStart))

    # get the full name of the month to show
    monthName = time.strftime('%B', monthStart)
    monthYear = time.strftime('%Y', monthStart)
    monthString = monthName + ' ' + monthYear

    # create instance of last day of month,
    # then get number of days in current month
    monthLast = time.localtime(time.mktime(\
        (ref_year, month + 1, 0, 0, 0, 0, 0, 0, 0)))
    lastDay = monthLast[2]

        
    # create basic table structure
    print '<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=2>'
    print '  <TR>'
    print '    <TH COLSPAN=7>'
    print '      <FONT COLOR="#191970" size="+2">%s</FONT>' %\
          (href %(ref_year, month, monthString))
    print '    </TH>'
    print '  </TR>'

    # variables to hold constant settings
    Column = '    <TD align=right><FONT COLOR=%s size=+1>%s</FONT></TD>'


    # create list of abbreviated day names
    weekDay = ['S', 'M', 'T', 'W', 'T', 'F', 'S']


    # create first row of table to set column width and specify week day
    print '  <TR>'
    for dayNum in range(0, 7):
        print Column %("#00008B", weekDay[dayNum])
    print '  </TR>'


    # declaration and initialization of two variables to help with tables
    curDay = 1
    curCell = 0

    # now, build the table!
    while 1:
        if (curDay > lastDay):
            break
        print '  <TR>'
        for col in range(0, 7):
            if (curDay > lastDay) or (curCell < firstDay):
                print Column %("#ffffff", '&nbsp;')
                curCell = curCell + 1
                continue
            else:
                if (year == this_year) and (month == this_month) \
                   and (curDay == this_day):
                    # this is today
                    print Column %("#FF0000", `curDay`)
                else:
                    print Column %("#000000", `curDay`)

                curDay = curDay + 1
        print '  </TR>'

    # that's it, we can close the table.
    print '</TABLE>'







########################################
#            Main Program
########################################


# get current date attributes
now = time.localtime(time.time())
this_year  = now[0]
this_month = now[1]
this_day   = now[2]

form = cgi.FieldStorage()

year = this_year
request_month = this_month
if form.has_key('year'):
    year = string.atoi(form['year'].value)

if form.has_key('month'):
    request_month = string.atoi(form['month'].value)


print 'Content-type: text/html\n'
print '<HTML>'
print '<head><title>Calendar by Rob Chavers</title></head>'
print '<BODY bgcolor="#F0FFF0" topmargin="2" leftmargin="4" rightmargin="4" marginheight="2" marginwidth="4">'

print open(__path__ + '/nav_menu.txt', 'r').read()

print '<br>'
print '\n<DIV ALIGN=center>\n'

if (year < 1902) or (year > 2037):
    print '<H2>'
    print 'This program will only work for years ' +\
          'between 1902 and 2037.'
    print '</H2>'
    sys.exit(0)
    
if request_month < 1 or request_month > 12:
    print '<H2>The month must be between 1 and 12.</H2>'
    sys.exit(0)


print '<TABLE width="100%" CELLSPACING=0 CELLPADDING=0>'
print ' <TR>'
for month in range(1,13):
    # get the full name of the month to show
    monthStart = time.localtime(time.mktime(\
        (year, month, 1, 0, 0, 0, 0, 0, 0))) 
    monthName = time.strftime('%B', monthStart)

    mesg = '  <TD align=center>'
    if month == request_month:
        this_monthName = monthName
        mesg = mesg + '<font color="#FF0000" size="+2">%s %d</font>' %(monthName, year)
    else:
        mesg = mesg + href %(year, month, monthName)
    print mesg + '</TD>'
print ' </TR>'
print '</TABLE>\n\n'



# get the full name of the month to show
monthStart = time.localtime(time.mktime(\
    (year, request_month, 1, 0, 0, 0, 0, 0, 0))) 
monthName = time.strftime('%B', monthStart)
this_monthName = monthName


print '<TABLE width="100%">'
print '<TR>'


print '\n\n<TD align=center valign=bottom>'
small_month(request_month -1)
print '</td>'


print '<TD align=center>'
month = request_month


# create instance of first day of month,
# get day of the week of the first day 0=sunday (like a calendar)
monthStart = time.localtime(time.mktime(\
    (year, month, 1, 0, 0, 0, 0, 0, 0))) 
firstDay = string.atoi(time.strftime('%w', monthStart))

# get the full name of the month to show
monthName = time.strftime('%B', monthStart)

# create instance of last day of month,
# then get number of days in current month
monthLast = time.localtime(time.mktime(\
    (year, month + 1, 0, 0, 0, 0, 0, 0, 0)))
lastDay = monthLast[2]

curDay = 7 - firstDay
index = 1
while 1:
    curDay = curDay + 7
    index = index + 1
    if curDay >= lastDay: break

# constant table settings
headerHeight = 50
border = 3
cellspacing = 2
cellpadding = 1
colWidth = '60'

if index > 5:
    cellHeight = '42'
else:
    cellHeight = '50'

dayCellHeight = '25'
todayColor = "#FF0000"
dayColor = "#00008B"
dayBGcolor = "#FFFFFF"
headerColor = "#FFFFFF"
headerSize = "+3"

monthPic = '/images/calendar/%s.jpg' %monthName

# create basic table structure
print '<TABLE BORDER=3 CELLSPACING=0 CELLPADDING=0>'
print '  <TR width="420">'
print '    <TD COLSPAN=7 align=center height="140" background="%s">&nbsp;</td>' %monthPic
#      '<img src="%s" width="420" height="140" alt="%s %d"></TD>' %(monthPic, monthName, year)
print '  </TR>'


# variables to hold constant settings
openCol = '    <TD WIDTH=%s HEIGHT=%s BGCOLOR=%s><FONT COLOR=%s>'
closeCol = '</FONT></TD>'


# create list of abbreviated day names
weekDay = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']


# create first row of table to set column width and specify week day
print '  <TR ALIGN="center" VALIGN="center">'
for dayNum in range(0, 7):
    print openCol %(colWidth,dayCellHeight,dayBGcolor,dayColor) +\
          weekDay[dayNum] + closeCol 
print '  </TR>'


# declaration and initialization of two variables to help with tables
curDay = 1
curCell = 0

while 1:
    if (curDay > lastDay):
        break
    print '  <TR ALIGN="right" VALIGN="top">'
    for col in range(0, 7):
        if (curDay > lastDay) or (curCell < firstDay):
            print openCol %(colWidth,cellHeight,"#E1E1E1","#FFFFFF") +\
                  '&nbsp;' + closeCol
            curCell = curCell + 1
            continue
        else:
            if (year == this_year) and (month == this_month) \
               and (curDay == this_day):
                # this is today
                print openCol %(colWidth,cellHeight,"#FFFFFF",todayColor) +\
                      'Today' + closeCol
            else:
                print openCol %(colWidth,cellHeight,"#FFFFFF","#000000") +\
                      `curDay` + closeCol

            curDay = curDay + 1
            
    print '  </TR>'
print '</table>'
print '</TD>\n\n\n'


print '<td align=center valign=bottom>'
small_month(request_month +1)
print '</td>\n\n'


# close main table tags
print '</TR>'
print '</TABLE>\n\n\n'

print '</BODY>'
print '</HTML>'
