// ******************************************************
//	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;
//populate arrays
mon = new Array(12);
day = new Array(31);
year = new Array(5);

for (i=0; i<elementA.length; i++){
 
 mon[i] = elementA.options[i].value;
}
for (i=0; i<elementB.length; i++){

 day[i] = elementB.options[i].value;
 
}
for (i=0; i<elementC.length; i++){

 year[i] = elementC.options[i].value;
 
}


calendarWindow = window.open(".","Kalender","width=166,height=155,left=800,top=550,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=#fdffdd text=#000000 link=#000000 vlink=#000000 alink=#000000 leftmargin=0 topmargin=0 marginwidth=0>"
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=20 height=15 bgcolor=#990000 align=center valign=middle><a style=text-decoration:none; href='javascript:opener.generateCalendar(self, " + goPrevMonth + ", " + prevYear + ")'><FONT COLOR='#ffffff' SIZE='2' FACE='Arial,Helvetica'><b>&lt;&lt;</b></font></a></td>"
	calendar +="<td width=118 height=20 bgcolor=#990000 align=center valign=middle><STRONG><FONT COLOR='#ffffff' SIZE='2' FACE='Arial,Helvetica'>" + monthName[mth] + " " + year + "</FONT></STRONG></td>"
	calendar +="<td width=24 height=20 bgcolor=#990000 align=center valign=middle><a style=text-decoration:none; href='javascript:opener.generateCalendar(self, " + goNextMonth + ", " + nextYear +  ")'><FONT COLOR='#ffffff' SIZE='2' FACE='Arial,Helvetica'><b>&gt;&gt;</b></font></a></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=167>"
calendar +="<tr align=center>"
calendar +="<td width=167 align=center><table border=0 cellspacing=1 cellpadding=0 width=100%><tr><td width=21 align=center><STRONG><FONT SIZE='1' color=#CC0000 FACE='Arial,Helvetica'>So</font></STRONG></td><td width=1></td><td width=21 align=center><FONT SIZE='2' color=#003300 FACE='Arial,Helvetica'>Mo</font></td><td width=1></td><td width=21 align=center><FONT SIZE='2' color=#003300 FACE='Arial,Helvetica'>Di</font></td><td width=1></td><td width=21 align=center><FONT SIZE='2' color=#003300 FACE='Arial,Helvetica'>Mi</font></td><td width=1></td><td width=21 align=center><FONT SIZE='2' color=#003300 FACE='Arial,Helvetica'>Do</font></td><td width=1></td><td width=21 align=center><FONT SIZE='2' color=#003300 FACE='Arial,Helvetica'>Fr</font></td><td width=1></td><td width=21 align=center><FONT SIZE='2' color=#3333FF FACE='Arial,Helvetica'>Sa</font></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=168><tr>"
for (var i = 1; i < lastDay; i++)
	{
	if (i <= firstDay)
		{
		// 'empty' boxes prior to first day
		calendar +="<td bgcolor='#FDFFDD'>&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='#99ff66'><FONT SIZE='1' color=#FDFFDD FACE='Arial,Helvetica'><a href='JavaScript:opener.closeCalendar(" + selectedmonth + ", " + (i-firstDay) + ", " + selectedyear + ");self.close()'> " + (i-firstDay) + "</a></FONT>&nbsp;</td>\n"
		} else {
		calendar +="<td align=center bgcolor='#FDFFDD'><FONT SIZE='1' FACE='Arial,Helvetica'><a href='JavaScript:opener.closeCalendar(" + selectedmonth + ", " + (i-firstDay) + ", " + selectedyear + ");self.close()'> " + (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>"

target.document.write(calendar);
target.document.close()	
}

// ******************************************************
// FUNCTION closeCalendar
// changes date field when a date is clicked
// ******************************************************
function closeCalendar(m, d, y)  {
	 the_Mon = -1;
 	 for(i=0; i<mon.length; i++){
  	 if(monthName[m] == mon[i]){
	  the_Mon = i;
	  }
	 } 
	 if (the_Mon == -1) 
		the_Mon = m;
	 for(i=0; i<day.length; i++){
	 if(d == day[i]){
	  the_Day = i;
	  }
	 }
	 for(i=0; i<year.length; i++){
	 if(y == year[i]){
	  the_Year = i;
	  }
	 }
	var date = m + "-" + d + "-" + y
	elementA.selectedIndex = the_Mon;
	elementB.selectedIndex  = the_Day;
	elementC.selectedIndex  = the_Year;
}

// ******************************************************
// 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
 }
