// JavaScript Document
function calender(id)
{
	this.cal_date = new Date();
	this.div_id = id;

	this.showCal = function(flag)
	{
		if (flag == 1)
		{
			document.getElementById(this.div_id).style.display = 'block';
			this.cal_date = new Date(document.getElementById(this.div_id+'_jahr').value, document.getElementById(this.div_id+'_monat').value - 1, document.getElementById(this.div_id+'_tag').value,1,1,1);
			
			this.fillCal(this.cal_date.getMonth(),this.cal_date.getFullYear());
		}
		else if (flag == 0)
		{
			document.getElementById(this.div_id).style.display = 'none';
		}
	}

	this.chMonth = function(direction)
	{
		this.cal_date = new Date(this.cal_date.getFullYear(),this.cal_date.getMonth()+direction,1,1,1,1);
		this.fillCal(this.cal_date.getMonth(),this.cal_date.getFullYear());
		return false;
	}

	this.fillCal = function(month, year)
	{
		
	
		mNames = new Array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember');
	
		var week = 1;
		var today = new Date();
		
		var thedate = new Date(year, month, 1, 1, 1, 1);
		var weekday = thedate.getDay();
		if (weekday == 0) { weekday = 7; }
	
		var daybefore = new Date(year, month +1, 0, 1, 1, 1);
		var numdays = daybefore.getDate();
		
		document.getElementById(this.div_id+'_month').firstChild.data = mNames[thedate.getMonth()]+' '+thedate.getFullYear();
	
		for (i = 1; i <= 6; i++)
		{
			for (j = 1; j <= 7; j++)
			{
				document.getElementById(this.div_id+'_cell'+i+j).firstChild.data = '';
				document.getElementById(this.div_id+'_cell'+i+j).setAttribute("class", "blank");
			}
		}	
	
		for (i = 1; i <= numdays; i++)
		{
			//fill cell weekday week
			document.getElementById(this.div_id+'_cell'+week+weekday).firstChild.data = i;
	
			if ((today.getDate() == i) && (today.getMonth()== thedate.getMonth() ) && (today.getYear()== thedate.getYear() ) )
			{
				document.getElementById(this.div_id+'_cell'+week+weekday).setAttribute("class", "today");
			}
			else
			{
				if (weekday < 6)
				{
					document.getElementById(this.div_id+'_cell'+week+weekday).setAttribute("class", "");
				}
				else
				{
					document.getElementById(this.div_id+'_cell'+week+weekday).setAttribute("class", "weekend");
				}
			}
			
			if (weekday < 7)
			{
				weekday++;
			}
			else
			{
				week++;
				weekday = 1;
			}
		}

	}

	this.selectDate = function(myCell)
	{	
		if (myCell.firstChild.data != '')
		{
			document.getElementById(this.div_id+'_tag').value = myCell.firstChild.data;
			document.getElementById(this.div_id+'_monat').value = this.cal_date.getMonth() + 1;
			document.getElementById(this.div_id+'_jahr').value = this.cal_date.getFullYear();
			this.showCal(0); 
			document.getElementById(this.div_id+'_tag').onchange();
		}
		
	}

}