// ******************************************************
//	Author:	Breck Morrison
//	Date:	07-22-1999
// ******************************************************

// Defining global variable elements
var elementA;
var elementB;
var elementC;
var the_Mon;
var the_Day;
var the_Year;
var monthName = new Array ("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember")

// ******************************************************
//	FUNCTION createCalendar
//	opens a new window for the calendar
// ******************************************************
function createCalendar(A, B, C) {
elementA = A;
elementB = B;
elementC = C;

calendarWindow = window.open(".","Kalender","width=166,height=155,resizable=no,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>Kalender</title></head><body bgcolor=#E8E8E7 text=#000000 link=#000000 vlink=#000000 alink=#000000 leftmargin=0 topmargin=0 marginwidth=0>"
calendar +="<table border=0 cellspacing=0 cellpadding=0 width=100%>"
calendar +="<tr valign=top height=21 width=100%>"

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=20 height=15 bgcolor=#333399 align=center valign=middle style='font-family:Arial,Helvetica,sans-serif;font-size:10pt;'><a style=text-decoration:none; href='javascript:opener.generateCalendar(self, " + goPrevMonth + ", " + prevYear + ")'><font color=#ffffff><b>&lt;&lt;</b></a></font></td>"
	calendar +="<td width=118 height=20 bgcolor=#333399 align=center valign=middle style='color:#fff;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'><b>" + monthName[mth] + " " + year + "</b></td>"
	calendar +="<td width=24 height=20 bgcolor=#333399 align=center valign=middle style='font-family:Arial,Helvetica,sans-serif;font-size:10pt;'><a style=text-decoration:none; href='javascript:opener.generateCalendar(self, " + goNextMonth + ", " + nextYear +  ")'><font color=#ffffff><b>&gt;&gt;</b></a></font></td></tr>"
	calendar +="</table></body></html>"
	target.document.close();

//writes in the day of the week labels
calendar +="<table border=0 cellspacing=0 cellpadding=0 width=100%>"
calendar +="<tr align=center>"

calendar +="<td width=100% align=center><table border=0 cellspacing=1 cellpadding=0 width=100%><tr><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>So</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Mo</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Di</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Mi</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Do</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Fr</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Sa</td></tr></table></td>"

//calendar +="<td width=167 align=center><table border=0 cellspacing=1 cellpadding=0 width=100%><tr><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Su</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Mo</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Tu</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>We</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Th</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Fr</td><td width=1></td><td width=21 align=center style='color:#c00;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'>Sa</td></tr></table></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=100%><tr>"
for (var i = 1; i < lastDay; i++)
	{
	if (i <= firstDay)
		{
		// 'empty' boxes prior to first day
		calendar +="<td bgcolor='#ffffff'>&nbsp;</td>"
		}
	else  
	 
		{
		// enter date number
		var lookd = today.getDate()
		var lookm = today.getMonth()
		if (( selectedmonth == lookm ) && ( lookd == (i-firstDay) )) {
		calendar +="<td width=21 align=center bgcolor='#cc0000' style='color:#fff;font-family:Arial,Helvetica,sans-serif;font-size:10pt;'><a href='JavaScript:opener.closeCalendar(" + selectedmonth + ", " + (i-firstDay) + ", " + selectedyear + ");self.close()'> " + (i-firstDay) + "</a></td>\n"
		} else {
		calendar +="<td width=21 align=center bgcolor='#ffffff' style='font-family:Arial,Helvetica,sans-serif;font-size:10pt;'><a href='JavaScript:opener.closeCalendar(" + selectedmonth + ", " + (i-firstDay) + ", " + selectedyear + ");self.close()'> " + (i-firstDay) + "</a></td>\n"
	}}
	//must start new row after each week
	if (i % 7 == 0 &&  i != lastDay)
		{
		calendar +="</tr><tr>"
		}
	}
calendar +="</tr></table>"

target.document.write(calendar);
target.document.close()	
}

// ******************************************************
// FUNCTION closeCalendar
// changes date field when a date is clicked
// ******************************************************
function closeCalendar(m, d, y)  {

	elementA.value = m+1;
	elementB.value = d;
	elementC.value = y;

}

// ******************************************************
// 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
 }
