/*Copyright 1996 - Tomer and Yehuda Shiran
Feel free to "steal" this code provided that you leave this notice as is.
Additional examples from the book can be found at http://www.geocities.com/SiliconValley/9000/
For more information contact Tomer or Yehuda Shiran <yshiran@iil.intel.com>*/

setCal()

function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
/* else */ // else not needed when statement is "return"
return false // is not leap year
}

function getDays(month, year) {
// create array to hold number of days in each month
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December


// return number of days in the specified month (parameter)
return ar[month]
}

function getMonthName(month) {
// create array to hold name of each month
var ar = new Array(12)
ar[0] = "JANUARY"
ar[1] = "FEBRUARY"
ar[2] = "MARCH"
ar[3] = "APRIL"
ar[4] = "MAY"
ar[5] = "JUNE"
ar[6] = "JULY"
ar[7] = "AUGUST"
ar[8] = "SEPTEMBER"
ar[9] = "OCTOBER"
ar[10] = "NOVEMBER"
ar[11] = "DECEMBER"

// return name of specified month (parameter)
return ar[month]
}

function setCal() {
// standard time attributes
var now = new Date()
var year = now.getYear()
if (year < 1000)
year+=1900
var month = now.getMonth()
var monthName = getMonthName(month)
var date = now.getDate()
now = null

// create instance of first day of month, and extract the day on which it occurs
var firstDayInstance = new Date(year, month, 1)
var firstDay = firstDayInstance.getDay()
firstDayInstance = null


// number of days in current month
var days = getDays(month, year)

// call function to draw calendar
drawCal(firstDay + 1, days, date, monthName, year)
}

function drawCal(firstDay, lastDate, date, monthName, year) {
// constant table settings
var headerHeight = 17 // height of the table's header cell
var border = 0 // 3D height of table's border
var cellspacing = 1 // width of table's border
var cellpadding = 0 // width of table's border
var headerColor = "#eeeeee" // color of table's header
var headerSize = "1" // size of tables header font
var colWidth = 15 // width of columns in table
var dayCellHeight = 11 // height of cells containing days of the week
var dayColor = "darkblue" // color of font representing week days
var cellHeight = 11 // height of cells representing dates in the calendar
var todayColor = "red" // color specifying today's date in the calendar
var pastColor = "silver" // color specifying today's date in the calendar
var linkColor = "black" // color specifying today's date in the calendar


// create basic table structure
var text = "" // initialize accumulative variable to empty string
//text += '<CENTER>'
text += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + ' CELLPADDING=' + cellpadding + '>' // table settings
text += '<tr><TD COLSPAN=7 HEIGHT=' + headerHeight + '>' // create table header cell
text += '<FONT face="verdana" COLOR="' + headerColor + '" SIZE=' + headerSize + '><b>' // set font for table header
text += monthName + ' ' + year 
text += '</b></FONT>' // close table header's font settings
text += '</TD></tr>' // close header cell

// variables to hold constant settings
var openCol = '<TD WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '>'
openCol += '<FONT COLOR="' + dayColor + '">'
var closeCol = '</FONT></TD>'


// create array of abbreviated day names
var weekDay = new Array(7)
weekDay[0] = "<!--Sun--><img src='../images/calendar/sun.gif'>"
weekDay[1] = "<!--Mon--><img src='../images/calendar/mon.gif'>"
weekDay[2] = "<!--Tues--><img src='../images/calendar/tue.gif'>"
weekDay[3] = "<!--Wed--><img src='../images/calendar/wed.gif'>"
weekDay[4] = "<!--Thu--><img src='../images/calendar/thu.gif'>"
weekDay[5] = "<!--Fri--><img src='../images/calendar/fri.gif'>"
weekDay[6] = "<!--Sat--><img src='../images/calendar/sat.gif'>"

// create first row of table to set column width and specify week day
text += '<TR ALIGN="center" VALIGN="center">'
for (var dayNum = 0; dayNum < 7; ++dayNum) {
text += openCol + weekDay[dayNum] + closeCol 
}
text += '</TR>'

// declaration and initialization of two variables to help with tables
var digit = 1
var curCell = 1

for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
text += '<TR ALIGN="right" VALIGN="top">'
for (var col = 1; col <= 7; ++col) {
if (digit > lastDate)
break
if (curCell < firstDay) {
text += '<TD></TD>';
curCell++
} else {

//event update here
if (digit == date) { // current cell represent today's date
text += '<TD HEIGHT=' + cellHeight + '>'
//text += '<a href='+monthName+'/'+digit+'.html target=_blank>'
text += '<a href=javascript:writeNewDiv("1","'+monthName+'/'+digit+'.jpg");>'
text += '<FONT COLOR="' + todayColor + '">'
//text += digit
text += '<img src="../images/calendar/current/'+digit+'.gif" width=15 height=11 border=0>'
text += '</FONT><BR>'
text += '</a>'
text += '</TD>'
} else
if (digit < date) { // current cell represent today's date
text += '<TD HEIGHT=' + cellHeight + '>'
//text += '<a href='+monthName+'/'+digit+'.html target=_blank>'
text += '<a href=javascript:writeNewDiv("1","'+monthName+'/'+digit+'.jpg");>'
text += '<img src="../images/calendar/past/'+digit+'.gif" width=15 height=11 border=0>'
text += '</a>'
text += '</TD>'
//} else
//if (digit == 28) { // current cell represent today's date
//text += '<TD HEIGHT=' + cellHeight + '>'
//text += '<a href='+monthName+'/'+digit+'.html target=_blank>'
//text += '<img src="../images/calendar/'+digit+'.gif" width=15 height=11 border=0>'
//text += '</a>'
//text += '</TD>'
//} else
//if ((monthName == 'SEPTEMBER') && (digit == '25')) { // current cell represent today's date
//text += '<TD HEIGHT=' + cellHeight + '>'
//text += '<a href='+monthName+'/'+digit+'.html target=_blank>'
//text += '<img src="../images/calendar/current/'+digit+'.gif" width=15 height=11 border=0>'
//text += '</a>'
//text += '</TD>'
} 

// unscheduled date 
else
text += '<TD HEIGHT=' + cellHeight + '><img src="../images/calendar/'+digit+'.gif" width=15 height=11></TD>'
digit++
}
}
text += '</TR>'
// if ((monthName == 2) && (digit == 15)) document.write("erwerwerwe");
}


// close all basic table tags
text += '</TABLE>'
//text += '</CENTER>'


// print accumulative HTML string
document.write(text) 
}
