/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                          Calendar JS  v2.0                           +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Copyright (C) 2005 by Michael Loesler, http://www.derletztekick.de   +
+                                                                      +
+                                                                      +
+ This program is free software; you can redistribute it and/or modify +
+ it under the terms of the GNU General Public License as published by +
+ the Free Software Foundation; either version 2 of the License, or    +
+ (at your option) any later version.                                  +
+                                                                      +
+ This program is distributed in the hope that it will be useful,      +
+ but WITHOUT ANY WARRANTY; without even the implied warranty of       +
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        +
+ GNU General Public License for more details.                         +
+                                                                      +
+ You should have received a copy of the GNU General Public License    +
+ along with this program; if not, write to the                        +
+ Free Software Foundation, Inc.,                                      +
+ 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            +
+                                                                      +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

var now = new Date();
var date = now.getDate();
var month = now.getMonth();
var year = now.getFullYear();


function prevMonth(pd, pm, py, formelement){
	if (pm == 0){
		pm = 11;
		py -= 1;
	}
	else{
		pm -= 1;
	}
	getCalendar(pd, pm, py, formelement);
}

function nextMonth(nd, nm, ny, formelement){
	if (nm == 11){
		nm = 0;
		ny += 1;
	}
	else{
		nm += 1;
	}
	getCalendar(nd, nm, ny, formelement);
}


function CalOut(dd, mm, yy, formelement){
	if(dd < 10) dd = 0+""+dd;
	document.getElementsByName(formelement)[0].value = (dd) + "." + (mm+1) + "." + yy;
	document.getElementById("calendar").innerHTML = "";
	document.getElementById("calendar").style.display = "none";
}


function getCalendar(dd, mm, yy, formelement){

	var sevendaysaweek = 0;
	var begin = new Date(yy,mm,1);
	var firstday = begin.getDay()-1;
	if (firstday < 0){
		firstday = 6;
	}
	dayname = new Array("Mo","Di","Mi","Do","Fr","Sa","So");
	monthname = new Array("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");
	dayspermonth = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if ((yy%4==0) && ((yy%100!=0) || (yy%400==0))){
		dayspermonth[1] = 29;
	}
	if ((mm != month) || (yy != year)){
		var today = '';
	}
	else {
		var today = date+'.';
	}

	var showcalendar = document.getElementById("calendar");

	for (var i=0; i<showcalendar.childNodes.length; i++){
		showcalendar.removeChild(showcalendar.childNodes[i]);
	}
	var newCalTable = document.createElement('table');
	newCalTable.setAttribute('id','caltable');

	var newCalTHead = document.createElement('thead');
	var newCalTBody = document.createElement('tbody');

	var newCalRow = document.createElement('tr');

	var newCalHead = document.createElement('th');
	newCalHead.setAttribute('class','select');
	newCalHead.setAttribute('title','vorheriger');
	newCalHead.appendChild(document.createTextNode("«"));
	newCalHead.style.cursor= ("hand"||"pointer")?"hand":"pointer";

	newCalHead.onclick=function () { prevMonth(dd, mm, yy, formelement); };

	newCalRow.appendChild(newCalHead);


	var newCalHead = document.createElement('th');
	newCalHead.setAttribute('colspan','5');
	newCalHead.colSpan = 5;
	newCalHead.appendChild(document.createTextNode(today+" "+monthname[mm]+" "+yy));
	newCalRow.appendChild(newCalHead);

	var newCalHead = document.createElement('th');
	newCalHead.setAttribute('class','select');
	newCalHead.setAttribute('title','nächster');
	newCalHead.appendChild(document.createTextNode("»"));
	newCalHead.style.cursor= ("hand"||"pointer")?"hand":"pointer";
	newCalHead.onclick=function () { nextMonth(dd, mm, yy, formelement); };


	newCalRow.appendChild(newCalHead);

	newCalTHead.appendChild(newCalRow);

	var newCalRow = document.createElement('tr');
	for (var i=0; i<dayname.length; i++){
		var newCalDayname = document.createElement('td');
		newCalDayname.setAttribute('class','days');
		newCalDayname.appendChild(document.createTextNode(dayname[i]));
		newCalRow.appendChild(newCalDayname);
	}
	newCalTBody.appendChild(newCalRow);

	var newCalRow = document.createElement('tr');
	for (var i=0; i<firstday; i++){
		var newCalDays = document.createElement('td');
		newCalDays.appendChild(document.createTextNode(String.fromCharCode(160)));
		newCalRow.appendChild(newCalDays);
		sevendaysaweek++;
	}
	var j=1;
	for (var i=1; i<=dayspermonth[mm]; i++){
		if (dayname.length == sevendaysaweek){
			newCalTBody.appendChild(newCalRow);
			var newCalRow = document.createElement('tr');
			var newCalDays = document.createElement('td');
			newCalDays.appendChild(document.createTextNode(i));
                         newCalDays.onmouseout = function () {this.style.backgroundColor = "#FFFFFF";};
                         newCalDays.onclick = function () { CalOut(this.innerHTML, mm, yy, formelement); };
                         newCalDays.onmouseover = function () {this.style.backgroundColor = "#E8EEFF";};
			newCalDays.style.cursor= ("hand"||"pointer")?"hand":"pointer";
			newCalRow.appendChild(newCalDays);
			sevendaysaweek = 0;
		}
		else {
			var newCalDays = document.createElement('td');
			newCalDays.appendChild(document.createTextNode(i));
                         newCalDays.onmouseout = function () {this.style.backgroundColor = "#FFFFFF";};
                         newCalDays.onclick = function () { CalOut(this.innerHTML, mm, yy, formelement); };
                         newCalDays.onmouseover = function () {this.style.backgroundColor = "#E8EEFF";};
			newCalDays.style.cursor= ("hand"||"pointer")?"hand":"pointer";
			newCalRow.appendChild(newCalDays);

		}
		sevendaysaweek++;
                 j++;
	}


	for (var i=sevendaysaweek; i<dayname.length; i++){
		var newCalDays = document.createElement('td');
		newCalDays.appendChild(document.createTextNode(String.fromCharCode(160)));
		newCalRow.appendChild(newCalDays);
	}

	newCalTBody.appendChild(newCalRow);

	newCalTable.appendChild(newCalTHead);
	newCalTable.appendChild(newCalTBody);
	showcalendar.appendChild(newCalTable);
}

function startCalendar(obj, formelement, direction){


	document.getElementById("calendar").innerHTML = "";

	document.getElementById("calendar").style.display = "inline";


	var left = 0;
	var top = 0;
	var temp_obj = obj;
	while(temp_obj.tagName != "BODY"){
		if(temp_obj.tagName != ("TBODY" && "TR")){
			left = left + temp_obj.offsetLeft;
			top = top + temp_obj.offsetTop;
		}
		temp_obj = temp_obj.parentNode;
	}

	document.getElementById("calendar").style.left = left;
	document.getElementById("calendar").style.top = top;

	getCalendar(date, month, year, formelement);
	if(direction == 'up') document.getElementById("calendar").style.top = document.getElementById("calendar").offsetTop - document.getElementById("calendar").offsetHeight;
	if(direction == 'down')  document.getElementById("calendar").style.top = document.getElementById("calendar").offsetTop + obj.offsetHeight;
	document.getElementById("calendar").style.left = document.getElementById("calendar").offsetLeft - document.getElementById("calendar").offsetWidth + obj.offsetWidth;

	//document.onclick = calendarStatus;
	calendarStatus;
}
function calendarStatus(){
	obj = event.srcElement;
	var ret = false;

	var temp_obj = obj;
	while(temp_obj.tagName != "BODY"){
		if(temp_obj.id == 'calendar' || temp_obj.name == 'calendar'){
			ret = true;
			break;
		}
		temp_obj = temp_obj.parentNode;
	}
	if(ret === false){
		document.getElementById("calendar").innerHTML = "";
		document.getElementById("calendar").style.display = "none";
		document.onclick = null; 
	}

	return ret;

}
//window.onload=startCalendar;