// ===================================================================
// Author: Matt Kruse <mkruse@netexpress.net>
// WWW: http://www.mattkruse.com/
//
// NOTICE: You may use this code for any purpose, commercial or
// private, without any further permission from the author. You may
// remove this notice from your final code if you wish, however it is
// appreciated by the author if at least my web site address is kept.
//
// You may *NOT* re-distribute this code in any way except through its
// use. That means, you can include it in your product, or your web
// site, or any other form where the code is actually being used. You
// may not put the plain javascript up on your site for download or
// include it in your javascript libraries for download. Instead,
// please just point to my URL to ensure the most up-to-date versions
// of the files. Thanks.
// ===================================================================

var use_css=false;
var use_layers=false;   
if (document.all)    { use_css    = true; }
if (document.layers) { use_layers = true; }

var CALWINDOW;

// Write out default styles to document or return string
function writestyles(doc) {
	var result = "";
	result += "<STYLE>\n";
	result += "TD.kalender {BACKGROUND-COLOR: #737373}\n";
	result += "SPAN.kalender {FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: #bebebe; FONT-FAMILY: Arial, Helvetica, Verdana, Geneva, sans-serif}\n";
	result += "</STYLE>\n";
	result += "<link rel='stylesheet' type='text/css' href='css/vvs_calendar.css'>\n";
	if (doc != "") {
		doc.write(result);
		}
	else {
		return result;
		}
	}
writestyles(this.document);

function getOffsetLeft (el) {
	var scrollamount = document.body.scrollLeft;
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null) { ol += el.offsetLeft; }
	ol = ol - scrollamount;
	return ol;
	}
function getOffsetTop (el) {
	var scrollamount = document.body.scrollTop;
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null) { ot += el.offsetTop; }
	ot = ot - scrollamount;
	return ot;
	}
function showCalendar(divname, anchorname, functionname) {
	// Find position relative to anchor
	if (use_css) {
		var x = getOffsetLeft(document.all[anchorname]);
		var y = getOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name == anchorname) {
				found=1;
				break;
				}
			}
		if (found == 0) {
			return;
			}
		var x = document.anchors[i].x;
		var y = document.anchors[i].y;
		x=x-window.pageXOffset;
		y=y-window.pageYOffset;
		}
	else {
		x = 200;
		y = 200;
		//return;
		}
	x = x-152;
	y = y+25;
	// WRITE CALENDAR TO DIV
	if (divname != "") {
		// Position the calendar DIV
		if (use_layers) { var calendardiv = document.layers[divname]; }
		if (use_css)    { var calendardiv = document.all[divname].style; }
		calendardiv.left = x;
		calendardiv.top  = y;
		// Write output to calendar DIV
		if (arguments.length>4) { 
			outputCalendar(divname,functionname,arguments[3],arguments[4]);
			}
		else {
			outputCalendar(divname,functionname);
			}
		// Show the calendar DIV
		calendardiv.visibility = "visible";
		}
	// WRITE CALENDAR TO POPUP WINDOW
	else {
		if (use_layers) {
			var windowx = window.screenX;
			var windowy = window.screenY + (window.outerHeight-24-window.innerHeight);
			}
		if (use_css) {
			var windowx = window.screenLeft;
			var windowy = window.screenTop;
			}
		x = x + windowx;
		y = y + windowy;
		if (!CALWINDOW || CALWINDOW.closed) {
			CALWINDOW = window.open("about:blank","calwindow","status,width=389,height=420,screenX="+x+",left="+x+",screenY="+y+",top="+y+",resizable");
			}
		// Write output to popup window
		if (arguments.length>4) { 
			outputCalendar(divname,functionname,arguments[3],arguments[4]);
			}
		else {
			outputCalendar(divname,functionname);
			}
		}
	}

function hideCalendar(divname) {
	if (divname != "") {
		if (use_layers) { var calendardiv = document.layers[divname]; }
		if (use_css)    { var calendardiv = document.all[divname].style; }
		calendardiv.visibility = "hidden";
		}
	else {
		if (CALWINDOW && !CALWINDOW.closed) {
			CALWINDOW.close();
			}
		}
	}
	
function outputCalendar(divname, functionname) {
	var now = new Date();
	if (arguments.length > 2) { var month = arguments[2]; }
		else { var month = now.getMonth()+1; }
	if (arguments.length > 3) { var year = arguments[3]; }
		else { var year = now.getFullYear(); }
	var monthnames = new Array('Januar','Februar','März','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember');
	var daysinmonth= new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);

	if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) { // leap year
		daysinmonth[2] = 29;
		}
	var current_month = new Date(year,month-1,1);
	
	var display_year = year;
	var display_month = month;
	var display_date = 1;

	var offset = 0;
	//var weekday= current_month.getDay();
	
	if (current_month.getDay() != 0)
		weekday= current_month.getDay() - 1;
	else
		weekday = 6;
	
	if (weekday > 0) {
		display_month--;
		if (display_month < 1) { display_month = 12; display_year--; }
		display_date = daysinmonth[display_month]-weekday+1;
		}
	var next_month = month+1;
	var next_month_year = year;
	if (next_month > 12) { next_month=1; next_month_year++; }
	var last_month = month-1;
	var last_month_year = year;
	if (last_month < 1) { last_month=12; last_month_year--; }
	
	var date_class;
	var result = "";
	if (divname == "" ) {
		var windowref = "window.opener.";
		}
	else {
		var windowref = "";
		}
	// If POPUP, write entire HTML document
	if (divname == "") {
		result += "<HTML><HEAD>"+writestyles('')+"<title>Kalender</title></head>\n";
		result += '<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0" onload="if (document.getElementById(\'today\')) document.getElementById(\'today\').focus(); else document.getElementById(\'1\').focus();">\n';
		//result += '<table width="389" cellspacing="0" cellpadding="0" border="0">\n';
		//result += '<tr><td width="65" height="11" valign="top" colspan="2" class="kalender"><img src="images/spacer.gif" alt="" width="1" height="11" border="0"></td></tr>\n';
		//result += '<tr><td width="65" valign="top" class="kalender"><span>&nbsp;</span></td>\n';
		//result += '<td width="324 valign="top" class="kalender"><a href="index.php"><img src="images/logo.gif" alt="" width="38" height="40" border="0"></a></td></tr>\n';
		//result += '<tr><td width="65" height="13" valign="top" colspan="2" class="kalender"><img src="images/spacer.gif" alt="" width="1" height="13" border="0"></td></tr>\n';
		//result += '</table>\n';
		//result += '<table width="389" height="32" cellspacing="0" cellpadding="0" border="0">\n';
		//result += '<tr><td width="10" height="32" valign="top" class="kalender"><span>&nbsp;</span></td>\n';
		//result += '<td width="379" height="32" valign="top" class="kalender">\n';
		//result += '<a href="JavaScript:self.close()"><img src="images/but_fenster_schliessen.gif" width="149" height="32" border="0"></a>\n';
		//result += '</td>\n';
		//result += '</tr></table>\n';
		result += '<table width="100%" cellspacing="0" cellpadding="0" border="0">\n';
		result += '<tr><td width="100%" height="64" valign="top" class="fenster">\n';
		result += '<div id="logo"><a href="index.php"><img id="logo" src="images/logo.gif" alt="" width="42" height="41" border="0"></a></div>\n';
		result += '</td></tr>\n';
		result += '</table>\n';
		result += '<table width="100%" cellspacing="0" cellpadding="0" border="0">\n';
		result += '<tr><td width="100%" valign="middle" bgcolor="#737373">\n';
		result += '<table width="222" height="32" cellspacing="0" cellpadding="0" border="0">\n';
		result += '<tr><td width="18" valign="top" bgcolor="#5C5C5C"><span>&nbsp;</span></td>\n';
		result += '<td width="188" valign="middle" bgcolor="#5C5C5C"><span class="website"><a href="JavaScript:self.close()">Fenster schlie&szlig;en</a></span></td>\n';
		result += '<td width="16" valign="top" bgcolor="#5C5C5C"><span>&nbsp;</span></td></tr>\n';
		result += '</table>\n';
		result += '</td></tr>\n';
		result += '</table>\n';
		result += '<table width="389" height="23" cellspacing="0" cellpadding="0" border="0">\n';
		result += '<tr><td width="389" height="23" valign="top"><span>&nbsp;</span></td></tr>\n';
		result += '</table>\n';
		}
	result += '<FORM>\n';

	result += '<table width="389" height="16" cellspacing="0" cellpadding="0" border="0">\n';
	result += '<tr><td width="22" height="16" valign="top"><span>&nbsp;</span></td>\n';
	result += '<td width="82" valign="top"><span><b>Kalender</b></span></td>\n';
	result += '<td width="71" valign="top"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top">\n';
	result += '<a href="javascript:'+windowref+'outputCalendar(\''+divname+'\',\''+functionname+'\','+last_month+','+last_month_year+')">\n';
	result += '<img src="images/zurueck.gif" alt="zur&uuml;ck" width="39" height="16" border="0"></a></td>\n';
	result += '<td width="126" valign="top" align="center"><span><b>'+monthnames[month-1]+' '+year+'</b></span></td>\n';
	result += '<td width="49" valign="top">\n';
	result += '<a href="javascript:'+windowref+'outputCalendar(\''+divname+'\',\''+functionname+'\','+next_month+','+next_month_year+')">\n';
	result += '<img src="images/vor.gif" alt="vor" width="39" height="16" border="0"></a></td>\n';
	result += '</tr></table>\n';

	result += '<table width="389" height="16" cellspacing="0" cellpadding="0" border="0">\n';
	result += '<tr><td width="389" height="16" valign="top"><span>&nbsp;</span></td></tr>\n';
	result += '</table>\n';

	result += '<table width="389" cellspacing="0" cellpadding="0" border="0">\n';
	result += '<tr><td width="10" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">Mo</span></td>\n';
	result += '<td width="16" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">Di</span></td>\n';
	result += '<td width="16" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">Mi</span></td>\n';
	result += '<td width="16" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">Do</span></td>\n';
	result += '<td width="16" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">Fr</span></td>\n';
	result += '<td width="16" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">Sa</span></td>\n';
	result += '<td width="16" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '<td width="39" valign="top" align="center"><span class="kalender">So</span></td>\n';
	result += '<td width="10" valign="top" rowspan="13"><span>&nbsp;</span></td>\n';
	result += '</tr><tr><td height="17"><span>&nbsp;</span></td></tr>\n';
	
	for (var row=1; row<=6; row++) {
		result += '<TR>\n';
		for (var col=1; col<=7; col++) {
			if ((display_month == now.getMonth()+1) && (display_date==now.getDate()) && (display_year==now.getFullYear())) {
				date_img = display_date + '_on';
				name_img = "today";
				}
			else {
				if (display_month != month) {
					date_img = "0";
					name_img = display_date ;
					}
				else {
					date_img = display_date ;
					name_img = display_date ;
					}
				}
			result += '<td width="41" valign="top" align="center">\n';
			result += '<input type="image" src="images/kalender/' + date_img + '.gif" id="' + name_img + '" width="39" height="16" border="0" onclick="'+windowref+functionname+'('+display_year+','+display_month+','+display_date+');'+windowref+'hideCalendar(\''+divname+'\');"></td>\n';
			// result += '<a href="javascript:'+windowref+functionname+'('+display_year+','+display_month+','+display_date+');'+windowref+'hideCalendar(\''+divname+'\');">\n';
			// result += '<img src="images/kalender/' + date_img + '.gif" alt="' + display_date + '" width="39" height="16" border="0"></a></td>\n';
			display_date++;
			if (display_date > daysinmonth[display_month]) {
				display_date=1;
				display_month++;
				}
			if (display_month > 12) {
				display_month=1;
				display_year++;
				}
			}
		result += '</TR>\n';
		result += '<tr><td height="17"><span>&nbsp;</span></td></tr>\n';
		}
	result += '</table>\n';
	
	result += '<table width="389" height="24" cellspacing="0" cellpadding="0" border="0">\n';
	result += '<tr><td width="389" height="24" valign="top"><span>&nbsp;</span></td></tr>\n';
	result += '</table>\n';

	result += '</FORM>';
	if (divname == "") {
		result += "</BODY></HTML>";
		}

	if (divname != "") {	
		if (use_css) {
			document.all[divname].innerHTML = result;
			}
		if (use_layers) {
			var thedoc = document.layers[divname].document;
			thedoc.open;
			thedoc.write(result);
			thedoc.close();
			}
		}
	else {
		CALWINDOW.document.open();
		CALWINDOW.document.write(result);
		CALWINDOW.document.close();
		}
	}

