
// Defining global variable elements
var the_Mon;
var the_Day;
var the_Year;
//var monthName = new Array ("January","Feburary","March","Aprl","May","June","July","August","September","October","November","December")
var monthName = new Array ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
var field;

function createCalendar(fld) {
field = fld;
calendarWindow = window.open("","Calendar","width=166,height=150,resizable=yes,scrollbars=no,status=no");
// Get todays Date Object
var today = new Date();																														

// Allows them to select which month to start with on calendar from drop down box named month
// var mthIdx = month.options.selectedIndex
// Instead I will pull the current month from date object (today)
var mth = today.getMonth();																											

// Allows them to select which year to start with on calendar from drop down box named year
// var yearVal = year.options[year.options.selectedIndex].text
// Instead I will pull the current year from date object (d)
var yearVal = today.getFullYear();																											

// call the function to populate the window
generateCalendar(calendarWindow, mth, yearVal)
}


// ******************************************************
// FUNCTION generateCalendar
// generates the meat of the calendar
// ******************************************************

function generateCalendar(target, mth, year) {

// begin table for calendar
target.document.open()
calendar = "<html><head><title>calendar</title><style type=text/css><!--.boxes {  font-family: Arial, Helvetica, sans-serif; font-size: 9pt; font-weight: normal; text-decoration: none}--></style></head><body bgcolor=#ffffff topmargin=0 link=#0000FF vlink=#0000FF alink=#0000FF leftmargin=0 marginwidth=0 marginheight=0>"
calendar +="<form>"
calendar +="<table border=0 cellspacing=0 cellpadding=0 width=167>"
calendar +="<tr valign=top height=21 width=166>"

var endday = getDaysInMonth(mth, year)
var goPrevMonth = prevMonth(mth)
var goNextMonth = nextMonth(mth)
var nextYear = changeYear("next",mth,parseInt(year))
var prevYear = changeYear("prev",mth,parseInt(year))
//writes month and next and back buttons
	calendar +="<td width=24 height=21 bgcolor=#cccccc><a href='javascript:opener.generateCalendar(self, " + goPrevMonth + ", " + prevYear + ")'><img src='images/cal_left_arrow.gif' width=24 height=21 border=0></a></td>"
	calendar +="<td width=118 height=21 bgcolor=#cccccc align='center'><div class='boxes'>" + buildMonthSelect(mth) + " " + buildYearSelect(year) + "</div></td>"
	calendar +="<td width=24 height=21 bgcolor=#cccccc><a href='javascript:opener.generateCalendar(self, " + goNextMonth + ", " + nextYear +  ")'><img src='images/cal_right_arrow.gif' width=24 height=21 border=0></a></td></tr>"
	calendar +="</table>"
	target.document.close();

//writes in the day of the week labels
calendar +="<table border=0 cellspacing=0 cellpadding=0 width=167>"
calendar +="<tr align=center>"
calendar +="<td width=167 align=center><img src='/images/header_days.gif' width=167 height=12 border=0></td>"

calendar +="</tr></table>"

// get the first day of the month
thedate = new Date (year, mth, 1);
firstDay = thedate.getDay()

selectedmonth = mth;
var today = new Date();
var thisyear = today.getYear() + 1900;
selectedyear = year

var lastDay = (endday + firstDay+1)

calendar +="<table border=0 cellspacing=1 cellpadding=0 width=168><tr>"
for (var i = 1; i < lastDay; i++)
	{
	if (i <= firstDay)
		{
		// 'empty' boxes prior to first day
		calendar +="<td bgcolor='#e8e8e7'>&nbsp;</td>"
		}
	else  
	 
		{
		// enter date number
		var lookd = today.getDate()
		var lookm = today.getMonth()
		if (( selectedmonth == lookm ) && ( lookd == (i-firstDay) )) {
		calendar +="<td align=center bgcolor='#cccccc'><FONT SIZE='2' FACE='Arial,Helvetica'><a href='JavaScript:self.close();opener.closeCalendar(" + selectedmonth + ", " + (i-firstDay) + ", " + selectedyear + ")'> " + (i-firstDay) + "</a></FONT>&nbsp;</td>\n"
		} else {
		calendar +="<td align=center bgcolor='#e8e8e7'><FONT SIZE='2' FACE='Arial,Helvetica'><a href='JavaScript:self.close();opener.closeCalendar(" + selectedmonth + ", " + (i-firstDay) + ", " + selectedyear + ")'> " + (i-firstDay) + "</a></FONT>&nbsp;</td>\n"
	}}
	//must start new row after each week
	if (i % 7 == 0 &&  i != lastDay)
		{
		calendar +="</tr><tr>"
		}
	}
calendar +="</tr></table></form></body></html>"

target.document.write(calendar);
target.document.close()	
}

// ******************************************************
// FUNCTION closeCalendar
// changes date field when a date is clicked
// ******************************************************
function closeCalendar(m, d, y)  {
        m = m + 1;
        if (m < 10) 
          m = "0" + m;
        if (d < 10)
          d = "0" + d;
	var date = m + "/" + d + "/" + y
        field.value = date;
        field.focus();
}

// ******************************************************
// FUNCTION getDaysInMonth
// finds the number of days in the month (mthldx)
// ******************************************************

function getDaysInMonth(mth, YrStr)
{

// all the rest have 31
var maxDays=31

// expect Feb. (of course)
if (mth==1) 
	{
	if (isLeapYear(YrStr))
		{
		maxDays=29;
		}
	else 
		{
		maxDays=28;
		}
	}

// thirty days hath...
if (mth==3 || mth==5 || mth==8 || mth==10)
	{
	maxDays=30;
	}
return maxDays;
}


// ******************************************************
// FUNCTION isLeapYear
// finds if the year (yrStr) is a leap year
// ******************************************************
function isLeapYear(yrStr)
{
var leapYear=false;
var year = parseInt(yrStr, 10);
// every fourth year is a leap year
if (year%4 == 0)
	{
	leapYear=true;
	// unless it's a multiple of 100
	if (year%100 == 0)
		{
		leapYear=false;
		// unless it's a multiple of 400
		if (year%400 == 0)
			{
			leapYear=true;
			}
		}
	}
return leapYear;
}


// ******************************************************
// FUNCTION nextMonth
// finds the next month
// ******************************************************
function nextMonth(month) 
{
if (month==11)
	{
	return 0;
	}
else
	{
	return (month+1);
	}
}


// ******************************************************
// FUNCTION prevMonth
// finds the previous month
// ******************************************************
function prevMonth(month) 
{
var prevMonth = (month-1)
if (month==0)
	{
	prevMonth = 11;
	}
return prevMonth
}


// ******************************************************
// FUNCTION changeYear
// increments or decrements month when it goes past Jan or Dec
// ******************************************************

function changeYear(direction,month,year)
{
var theYear = year
if (direction=="next")
	{
	if (month == 11)
		{
		theYear = (year+1)
		}
	}
if (direction=="prev")
	{
	if (month == 0)
		{
		theYear = (year-1)
		}
	}
return theYear
 }

// ******************************************************
// FUNCTION buildYearSelect
// 
// ******************************************************
function buildYearSelect(theYear) {
  var today = new Date();
  var out = "<select name='year' onChange='opener.changeCalendar(opener, self, this)'>";
  for (var i = today.getFullYear()-10; i <= today.getFullYear()+10 ; i++) {
    if (i == theYear) {
      out += "<option selected value='" + i + "'>" + i + "</option>";
    } else {
      out += "<option value='" + i + "'>" + i + "</option>";
    }
  }
  out += "</select>";

  return out;
}

// ******************************************************
// FUNCTION buildYearSelect
// 
// ******************************************************
function buildMonthSelect(theMonth) {
  var today = new Date();
  var out = "<select name='month' onChange='opener.changeCalendar(opener, self, this)'>";
  for (var i = 0; i < 12 ; i++) {
    if (i == theMonth) {
      out += "<option selected value='" + i + "'>" + monthName[i] + "</option>";
    } else {
      out += "<option value='" + i + "'>" + monthName[i] + "</option>";
    }
  }
  out += "</select>";

  return out;
}

function changeCalendar(scriptwin, target, box) {
  var theMonth;
  var theYear;
  var idx;
  var form;

  form = box.form;
  idx = form.year.selectedIndex;
  theYear = form.year.options[idx].value;
  idx = form.month.selectedIndex;
  theMonth = form.month.options[idx].value;
  scriptwin.generateCalendar(target, theMonth, theYear);
}
