var weekend = [0,6];
var weekendColor = "#D3DAED";
var fontface = "Arial";
var fontsize = 1;
var gNow = new Date();
var ggWinCal;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
Calendar.Months = ["Januar", "Februar", "März", "April", "Mai", "Juni","Juli", "August", "September", "Oktober", "November", "Dezember"];
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function Calendar(p_item, p_WinCal, p_month, p_year, p_format)
{
	if ((p_month == null) && (p_year == null))	return;
	if (p_WinCal == null)
	{
		this.gWinCal = ggWinCal;
	}
	else
	{
		this.gWinCal = p_WinCal;
	}
	if (p_month == null)
	{
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	}
	else
	{
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}
	this.gYear = p_year;
	this.gFormat = p_format;
	this.gBGColor = "white";
	this.gFGColor = "black";
	this.gTextColor = "black";
	this.gHeaderColor = "black";
	this.gReturnItem = p_item;
}
Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;
function Calendar_get_month(monthNo)
{
	return Calendar.Months[monthNo];
}
function Calendar_get_daysofmonth(monthNo, p_year)
{
	if ((p_year % 4) == 0)
	{
		if ((p_year % 100) == 0 && (p_year % 400) != 0)		return Calendar.DOMonth[monthNo];
		return Calendar.lDOMonth[monthNo];
	}
	else
	{
		return Calendar.DOMonth[monthNo];
	}
}
function Calendar_calc_month_year(p_Month, p_Year, incr)
{
	var ret_arr = new Array();
	if (incr == -1)
	{
		// B A C K W A R D
		if (p_Month == 0)
		{
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else
		{
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	else if (incr == 1)
	{
		// F O R W A R D
		if (p_Month == 11)
		{
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else
		{
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	return ret_arr;
}
function Calendar_calc_month_year(p_Month, p_Year, incr)
{
	var ret_arr = new Array();
	if (incr == -1)
	{
		// B A C K W A R D
		if (p_Month == 0)
		{
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else
		{
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	else if (incr == 1)
	{
		// F O R W A R D
		if (p_Month == 11)
		{
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else
		{
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	return ret_arr;
}
// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();
Calendar.prototype.getMonthlyCalendarCode = function()
{
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	// Begin Table Drawing code here..
	vCode += "<table style=\"margin: 0px; width: 100%; border-spacing: 1px; font-size: 10px; background-color: " + this.gBGColor + ";\">";
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode += vHeader_Code + vData_Code;
	vCode += "</table>";
	return vCode;
}

Calendar.prototype.show = function()
{
	var vCode = "";
	// Setup the page...
	// Show navigation buttons
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];
	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	vCode += "<div style=\"background-color: white; font-size: 12px; width: 14em; -moz-box-shadow: #000 1px 1px 3px 1px; -webkit-box-shadow: #000 1px 1px 3px 1px; -moz-border-radius: 3px; filter:progid:DXImageTransform.Microsoft.DropShadow(color=#333333, offx=2, offy=2);\">";
	var tstyle = "style=\"margin: 0px; font-family: arial; width: 100%; border-spacing: 0px; font-size: 10px; padding: 0px; border-width: 0px; font-weight: bold; background-position: center; background-color: #D4DBED; background-image: url(cmx/images/glossy.gif); \" ";
	vCode += "<table " + tstyle + "><tr>";
	var hrefstyle = "style=\"border-width: 0px; background: none; font-weight: bold; color: #333; text-decoration: none; font-size: 15px;\" ";
	var tdstyle = "style=\"border-width: 0px; vertical-align: middle; text-align: center; width: 1%; padding: 2px;\" ";
	var href = "href=\"" + "javascript: Build(" + "'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "', '" + this.gFormat + "'" + ");" + "\" "
	vCode += "<td " + tdstyle + "><a " + hrefstyle + href + ">&laquo;</a></td>";
	href = "href=\"" + "javascript: Build(" + "'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" + ");" + "\" ";
	vCode += "<td " + tdstyle + "><a " + hrefstyle + href + ">&lsaquo;</a></td>";
	
	vCode += "<td style=\"border-width: 0px; vertical-align: middle; text-align: center; width: 1%; padding: 2px; text-align: center; vertical-align: middle; width: 8em; white-space: nowrap; \">";
	yearpic = "onchange=\"" + "Build(" + "'" + this.gReturnItem + "', '" + this.gMonth + "', this.value, '" + this.gFormat + "'" + ");" + "\" ";
	vCode += '<select style="border: 1px inset #eee; padding: 0px; font-size: 9px; line-height: 11px; width: 45px;" ' + yearpic + '>';
	for(var i = 2100; i > 1900; i--) 
	{
		if(i == this.gYear)
		{
			vCode += '<option selected="true" value="' + i + '">' + i + '</option>';
		}
		else
		{
			vCode += '<option value="' + i + '">' + i + '</option>';
		}
	}
	vCode += '</select>';	
	vCode += "</td>";

	vCode += "<td style=\"border-width: 0px; vertical-align: middle; text-align: center; width: 1%; padding: 2px; text-align: center; vertical-align: middle; width: 8em; white-space: nowrap; \">";
	monpic = ' onchange="' + "Build(" + "'" + this.gReturnItem + "', this.value, '" + this.gYear + "', '" + this.gFormat + "'" + ");" + '" ';
	vCode += '<select style="border: 1px inset #eee; padding: 0px; font-size: 9px; line-height: 11px; width: 60px;" ' + monpic + '>';
	var monate = new Array("Januar", "Februar", "März", "April", "Mai", "Juni","Juli", "August", "September", "Oktober", "November", "Dezember");
	for(var i = 0; i < monate.length; i++) 
	{
		if(monate[i] == this.gMonthName)
		{
			vCode += '<option value="' + i + '" selected="true">' + monate[i] + '</option>';
		}
		else
		{
			vCode += '<option value="' + i + '">' + monate[i] + '</option>';
		}
	}
	vCode += '</select>';
	vCode += "</td>";

	href = "href=\"" + "javascript: Build(" + "'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" + ");" + "\" ";
	vCode += "<td " + tdstyle + "><a " + hrefstyle + href + ">&rsaquo;</a></td>";
	href = "href=\"" + "javascript: Build(" + "'" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "', '" + this.gFormat + "'" + ");" + "\" "
	vCode += "<td " + tdstyle + "><a " + hrefstyle + href + ">&raquo;</a></td>";
	vCode += "</tr></table>";

	vCode += this.getMonthlyCalendarCode();
	vCode += "</div>";
	this.wwrite(vCode);
}

Calendar.prototype.showY = function()
{
	var vCode = "";
	var i;
	// Show navigation buttons
	var prevYYYY = parseInt(this.gYear) - 1;
	var nextYYYY = parseInt(this.gYear) + 1;
	vCode += "<div style=\"font-size: 12px; width: 42em;\">";
	var tstyle = "style=\"font-family: arial; width: 100%; border-spacing: 0px; font-size: 10px; padding: 0px; border-width: 0px; font-weight: bold; background-position: center; background-color: #D4DBED; background-image: url(cmx/images/glossy.gif);\" ";
	vCode += "<table " + tstyle + "><tr>";
	var hrefstyle = "style=\"border-width: 0px; background: none; font-weight: bold; color: #333; text-decoration: none; font-size: 15px;\" ";
	var tdstyle = "style=\"text-align: center; width: 1%; padding: 3px;\" ";
	var href = "href=\"" + "javascript: Build(" + "'" + this.gReturnItem + "', null, '" + prevYYYY + "', '" + this.gFormat + "'" + ");" + "\" "
	vCode += "<td " + tdstyle + "><a " + hrefstyle + href + ">&laquo;</a></td>";

	vCode += "<td style=\"text-align: center; font-weight: bold; white-space: nowrap; \">Jahr: " + this.gYear + "</td>";
	href = "href=\"" + "javascript: Build(" + "'" + this.gReturnItem + "', null, '" + nextYYYY + "', '" + this.gFormat + "'" + ");" + "\" "
	vCode += "<td " + tdstyle + "><a " + hrefstyle + href + ">&raquo;</a></td>";
	vCode += "</tr></table>";
	// Get the complete calendar code for each month..
	var j;
	vCode += "<table style=\"width: 100%; border-width: 0px; border-collapse: collapse;\">";
	var mc = 0;
	for(a=0; a<4; a++)
	{
		vCode += "<tr>";
		for (i=0; i<3; i++)
	   	{
			vCode += "<td style=\"vertical-align: top;\">";
			this.gMonth = (a * 3) + i;
			this.gMonthName = Calendar.get_month(this.gMonth);

			vCode += '<div style="padding-top: 8px; font-family: arial; font-size: 12px; font-weight: bold;">' + this.gMonthName + "/" + this.gYear + '</div>';
			vCode += this.getMonthlyCalendarCode();
			vCode += "</td>";
		}
		vCode += "</tr>";
	}
	vCode += "</table>";
	vCode += "</div>";
	this.wwrite(vCode);
}
Calendar.prototype.wwrite = function(wtext)
{
	this.gWinCal.innerHTML = this.gWinCal.innerHTML + wtext;
}
Calendar.prototype.wwriteA = function(wtext)
{
	this.gWinCal.innerHTML = this.gWinCal.innerHTML + wtext;
}
Calendar.prototype.cal_header = function()
{
	var vCode = "";
	var td = "<td style=\"border-width: 0px; padding: 0px; width: 14%;\; font-family: " + fontface + "; font-weight: bold; color: " + this.gHeaderColor + ";\">";
	vCode += "<tr>";
	vCode += td + "Son</td>" + td + "Mon</td>" + td + "Die</td>" + td + "Mit</td>" + td + "Don</td>" + td + "Fre</td>" + td + "Sam</td>";
	vCode += "</tr>";
	return vCode;
}
Calendar.prototype.cal_data = function()
{
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);
	var vFirstDay = vDate.getDay();
	var vDay = 1;
	var vLastDay = Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay = 0;
	var vCode = "";
	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary.
	*/
	vCode += "<tr>";
	for (i=0; i<vFirstDay; i++)
	{
		vCode += "<td style=\"border-width: 0px; vertical-align: middle; text-align: center; width: 1%; padding: 2px; text-align: center; vertical-align: middle; width: 8em; white-space: nowrap; padding: 0px; text-align: center; font-family: " + fontface + "; width: 14%; " + this.write_weekend_string(i) + "\"> </td>";
	}
	// Write rest of the 1st week
	var tdstyle = "style=\"text-align: center; padding: 0px; font-family: " + fontface + "; width: 14%; cursor: pointer; font-size: 10px; border: #ddd 1px solid;\" "
	var onmouseout = "onmouseout=\"this.style.borderStyle='solid'; this.style.borderColor='#ddd';\" ";
	var onmouseover = "onmouseover=\"this.style.borderStyle='solid'; this.style.borderColor='#69f';\" ";
	for (j=vFirstDay; j<7; j++)
	{
		var onclick = "onclick=\"document.getElementById('" + this.gReturnItem + "').value='" + this.format_data(vDay) + "'; document.getElementById('" + this.gReturnItem + "').onchange(); GetDateViewValue('" + this.gReturnItem + "');\" ";
		vCode += "<td " + onclick + tdstyle + onmouseout + onmouseover + ">" + this.format_day(vDay) + "</td>";
		vDay += 1;
	}
	vCode = vCode + "</tr>";
	// Write the rest of the weeks
	for(k=2; k<7; k++)
	{
		vCode += "<tr>";
		var onmouseout = "onmouseout=\"this.style.borderStyle='solid'; this.style.borderColor='#ddd';\" ";
		var tdstyle = "";
		var onmouseover = "onmouseover=\"this.style.borderStyle='solid'; this.style.borderColor='#69f';\" ";
		for (j=0; j<7; j++)
		{
			tdstyle = "style=\"text-align: center; padding: 0px; font-size: 10px; cursor: pointer; text-align: center; border: #ddd 1px solid; width: 14%;  " + this.write_weekend_string(j) + " font-family: " + fontface + ";\" ";
			var onclick = "onclick=\"document.getElementById('" + this.gReturnItem + "').value='" + this.format_data(vDay) + "'; document.getElementById('" + this.gReturnItem + "').onchange(); GetDateViewValue('" + this.gReturnItem + "');\" ";
			vCode += "<td " + onclick + tdstyle + onmouseover + onmouseout + ">" + this.format_day(vDay) + "</td>";
			vDay += 1;
			if (vDay > vLastDay)
			{
				vOnLastDay = 1;
				break;
			}
		}
		if (j == 6)				vCode += "</tr>";
		if (vOnLastDay == 1)	break;
	}
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++)
	{
		if (this.gYearly)
		{
			vCode += "<td style=\"padding: 0px; border-width: 0px; width: 14%; center; font-family: " + fontface + "; " + this.write_weekend_string(j+m) + "\"> </td>";
		}
		else
		{
			vCode += "<td style=\"padding: 0px; border-width: 0px; width: 14%; color: gray; text-align: center; font-family: " + fontface + "; " + this.write_weekend_string(j+m) + "\">" + m + "</td>";
		}
	}
	return vCode;
}

Calendar.prototype.format_day = function(vday)
{
	if (vday == gNow.getDate() && this.gMonth == gNow.getMonth() && this.gYear == gNow.getFullYear())
	{
		return ("<span style=\"color: red; font-weight: bold;\">" + vday + "</span>");
	}
	else
	{
		return (vday);
	}
}

Calendar.prototype.write_weekend_string = function(vday)
{
	var i;
	// Return special formatting for the weekend day.
	for (i=0; i < weekend.length; i++)
	{
		if (vday == weekend[i])	return (" background-color: " + weekendColor + "; ");
	}
	return "";
}

Calendar.prototype.format_data = function(p_day)
{
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;
	vData = vY4 + "-" +  vMonth + "-" + vDD;
	return vData;
}
function Build(p_item, p_month, p_year, p_format)
{
	ggWinCal.innerHTML = "";
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year, p_format);
	// Customize your Calendar here..
	gCal.gBGColor="white";
	gCal.gLinkColor="black";
	gCal.gTextColor="black";
	gCal.gHeaderColor="black";
	// Choose appropriate show function
	if (gCal.gYearly)
	{
		gCal.showY();
	}
	else
	{
		gCal.show();
	}
}
function show_calendar(p_item,tdatum)
{
	//tdatum (deutsches format(DD.MM.YYYY)
	p_format = "YYYY-MM-DD";
	if(tdatum)
	{
		p_year = tdatum.substring(6,10);
		p_month = tdatum.substring(3,5);
	}
	else
	{
		p_year = new String(gNow.getFullYear().toString());
		p_month = new String(gNow.getMonth() + 1);
	}
	p_month=(parseInt(p_month)-1);
	ggWinCal = document.getElementById(field.id + "_calendar");
	if(ggWinCal.style.display == "none")
	{
		Build(p_item, p_month, p_year, p_format);
		ggWinCal.style.display = "block";
	}
	else
	{
		ggWinCal.style.display = "none";
	}
}
/*
Yearly Calendar Code Starts here
*/
function show_yearly_calendar(p_item, p_year, p_format) {
	// Load the defaults..
	if (p_year == null || p_year == "")
	{
		p_year = new String(gNow.getFullYear().toString());
	}
	if (p_format == null || p_format == "")
	{
		p_format = "YYYY-MM-DD";
	}
	ggWinCal = document.getElementById(field.id + "_calendar");
	if(ggWinCal.style.display == "none")
	{
		Build(p_item, null, p_year, p_format);
		ggWinCal.style.display = "block";
	}
	else
	{
		ggWinCal.style.display = "none";
	}
}
function GetDateValue(fieldname,yearly,offx,offy)
{
	field = document.getElementById(fieldname);
	viewfield = document.getElementById(fieldname + "_view");
	calendarfield = document.getElementById(fieldname + "_calendar");
	//alert(viewfield.offsetWidth);
	//calendarfield.style.left = viewfield.offsetWidth + "px";
	var top = viewfield.offsetHeight + 3;
	calendarfield.style.top = top + "px";
	if(field.value != "")
	{
		viewfield.value = "";
		field.value = "";
		field.onchange();
	}
	else
	{
		if(yearly)
		{
			show_yearly_calendar(field.id,field.value);
		}
		else
		{
			show_calendar(field.id);
		}
	}
}
function GetDateViewValue(field)
{
	var viewfield = document.getElementById(field + "_view");
	var c = document.getElementById(field + "_calendar");
	c.style.display= 'none';
	hiddenfield = document.getElementById(field);
	var datesegments = hiddenfield.value.split("-");
	var date = datesegments[2] + "." + datesegments[1] + "." + datesegments[0];
	viewfield.value = date;
}
