/**********************************************************************
*          Calendar JavaScript [DOM] v3.01 by Michael Loesler          *
************************************************************************
* Copyright (C) 2005-07 by Michael Loesler, http//derletztekick.com    *
*                                                                      *
*                                                                      *
* 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 3 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, see <http://www.gnu.org/licenses/>  *
* or write to the                                                      *
* Free Software Foundation, Inc.,                                      *
* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.            *
*                                                                      *
 **********************************************************************/

	var strCalendar_Type = "";


	function CalendarJS() {
//		alert("kalender");
		this.now = new Date();
		this.dayname = ["Mo","Di","Mi","Do","Fr","Sa","So"];
		this.monthname = ["Januar","Februar","M�rz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"];
		this.dayspermonth = [31,28,31,30,31,30,31,31,30,31,30,31];
		this.tooltip = ["vorheriger Monat","n�chster Monat"];
		this.monthCell = document.createElement("th");
		this.tableHead = null;
		this.parEl = null;

		this.init = function( id ) {
			this.date = this.now.getDate();
			this.month = this.mm = this.now.getMonth();
			this.year = this.yy = this.now.getFullYear();
			this.monthCell.colSpan = 5;
			this.monthCell.appendChild(document.createTextNode( this.monthname[this.mm]+" "+this.yy ));
			this.tableHead = this.createTableHead();
			this.parEl = document.getElementById( id );
			this.show();
		}

		this.removeElements = function( Obj ) {
			for (var i=0; i<Obj.childNodes.length; i++)
				Obj.removeChild(Obj.childNodes[i]);
			return Obj;
		}

		this.show = function() {
			this.parEl = this.removeElements( this.parEl );
			this.monthCell.firstChild.replaceData(0, this.monthCell.firstChild.nodeValue.length, this.monthname[this.mm]+" "+this.yy);
			var table = document.createElement("table");
			table.appendChild( this.createTableBody() );
			table.appendChild( this.tableHead );
			this.parEl.appendChild( table );
		}

		this.createTableHead = function() {
			var thead = document.createElement("thead");
			var tr = document.createElement("tr");
			var th = document.createElement("th");
			th.appendChild(document.createTextNode( "\u00AB" ));
			th.Instanz = this;
			th.onclick = function() { this.Instanz.switchMonth("prev"); };
			th.title = this.tooltip[0];
			try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
			tr.appendChild( th );
			tr.appendChild( this.monthCell );
			th = document.createElement("th");
			th.appendChild(document.createTextNode( "\u00BB" ));
			th.Instanz = this;
			th.onclick = function() { this.Instanz.switchMonth("next"); };
			th.title = this.tooltip[1];
			try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
			tr.appendChild( th );
			thead.appendChild( tr );
			tr = document.createElement('tr');
			for (var i=0; i<this.dayname.length; i++)
				tr.appendChild( this.getCell("th", this.dayname[i], "weekday" , 0, "") );
			thead.appendChild( tr );
			return thead;
		}

		this.createTableBody = function() {
			var sevendaysaweek = 0;
			var begin = new Date(this.yy, this.mm, 1);
			var firstday = begin.getDay()-1;
			if (firstday < 0)
				firstday = 6;
			if ((this.yy%4==0) && ((this.yy%100!=0) || (this.yy%400==0))){
				this.dayspermonth[1] = 29;
			} else {
				this.dayspermonth[1] = 28;
			}

			var tbody = document.createElement("tbody");
			var tr = document.createElement('tr');

			for (var i=0; i<firstday; i++, sevendaysaweek++)
				tr.appendChild( this.getCell( "td", " ", null , 0, "") );

			for (var i=1; i<=this.dayspermonth[this.mm]; i++, sevendaysaweek++){
				if (this.dayname.length == sevendaysaweek){
					tbody.appendChild( tr );
					tr = document.createElement('tr');
					sevendaysaweek = 0;
				}

				/*
					Datum pr�fen mit this.yy-this.mm-this.i
				*/
				// Tag mit f�hrender 0
				if (i < 10){
					strDay = "0" + i;
				} else {
					strDay = i;
				}

				// Monat mit f�hrender 0
				if (this.mm + 1 < 10){
					strMounth = "0" + (this.mm + 1);
				} else {
					strMounth = (this.mm + 1);
				}

				strClassBooked_Out = "booked_out" + get_str_Booked_Out_Status(this.yy + "-" + strMounth + "-" + strDay);

				// Links immer anzeigen wenn Adminbereich
				if (strCalendar_Type == "admin") {
					blnLink = 1;
				} else {
					// Links nur in Buchungsformularen und wenn Datum >= heute
					strDate_Now = this.year + "-" + this.month + "-" + this.date;
					strDate_Loop = this.yy + "-" + this.mm + "-" + i;
					if ((strCalendar_Type == "booking_from" || strCalendar_Type == "booking_to" || strCalendar_Type == "start_from" || strCalendar_Type == "start_to" || strCalendar_Type == "doksy_start_from" || strCalendar_Type == "doksy_start_to" || strCalendar_Type == "result_from" || strCalendar_Type == "result_to" || strCalendar_Type == "doksy_result_from" || strCalendar_Type == "doksy_result_to") && strDate_Loop >= strDate_Now) {
						blnLink = 1;
					} else {
						blnLink = 0;
					}
				}

				if (i==this.date && this.mm==this.month && this.yy==this.year && (sevendaysaweek == 5 || sevendaysaweek == 6))
					// Adminbreich und gesetzets Datum?
					if (strCalendar_Type == "admin" || strCalendar_Type == "detail" || strCalendar_Type == "detail_doksy"){
						if ( get_blnIs_Booked_Out(this.yy + "-" + strMounth + "-" + strDay)){
							tr.appendChild( this.getCell( "td", i , "today weekend " + strClassBooked_Out , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						} else {
							tr.appendChild( this.getCell( "td", i , "today weekend" , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , "today weekend" , blnLink, (i + "." + strMounth + "." + this.yy)) );
					}
				else if (i==this.date && this.mm==this.month && this.yy==this.year)
					// Adminbreich und gesetzets Datum?
					if (strCalendar_Type == "admin" || strCalendar_Type == "detail" || strCalendar_Type == "detail_doksy"){
						if ( get_blnIs_Booked_Out(this.yy + "-" + strMounth + "-" + strDay)){
							tr.appendChild( this.getCell( "td", i , "today " + strClassBooked_Out , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						} else {
							tr.appendChild( this.getCell( "td", i , "today" , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , "today" , blnLink, (i + "." + strMounth + "." + this.yy)) );
					}
				else if (sevendaysaweek == 5 || sevendaysaweek == 6)
					// Adminbreich und gesetzets Datum?
					if (strCalendar_Type == "admin" || strCalendar_Type == "detail" || strCalendar_Type == "detail_doksy"){
						if ( get_blnIs_Booked_Out(this.yy + "-" + strMounth + "-" + strDay)){
							tr.appendChild( this.getCell( "td", i , "weekend " + strClassBooked_Out , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						} else {
							tr.appendChild( this.getCell( "td", i , "weekend" , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , "weekend" , blnLink, (i + "." + strMounth + "." + this.yy)) );
					}
				else
					// Adminbreich und gesetzets Datum?
					if (strCalendar_Type == "admin" || strCalendar_Type == "detail" || strCalendar_Type == "detail_doksy"){
						if ( get_blnIs_Booked_Out(this.yy + "-" + strMounth + "-" + strDay)){
							tr.appendChild( this.getCell( "td", i , strClassBooked_Out , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						} else {
							tr.appendChild( this.getCell( "td", i , null , blnLink, (this.yy + "-" + strMounth + "-" + strDay)) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , null , blnLink, (i + "." + strMounth + "." + this.yy)) );
					}
			}

			for (var i=sevendaysaweek; i<this.dayname.length; i++)
				tr.appendChild( this.getCell( "td", " ", null  , 0, "") );

			tbody.appendChild( tr );
			return tbody;

		}

		this.getCell = function(tag, str, cssClass, blnLink, strDate) {
			var El = document.createElement( tag );
			if (blnLink == 1){
				El.innerHTML = "<a href=\"javascript:setDate('" + strDate + "');\" class='link_calendar " + cssClass + "'>" + str + "</a>";
				El.setAttribute("id", strDate);
			} else {
				El.appendChild(document.createTextNode( str ));
			}
			if (cssClass != null)
				El.className = cssClass;
			return El;
		}

		this.switchMonth = function( s ){
			switch (s) {
				case "prev":
					this.yy = (this.mm == 0)?this.yy-1:this.yy;
					this.mm = (this.mm == 0)?11:this.mm-1;
				break;

				case "next":
					this.yy = (this.mm == 11)?this.yy+1:this.yy;
					this.mm = (this.mm == 11)?0:this.mm+1;
				break;
			}
			this.show();
		}
	}

	var DOMContentLoaded = false;
	function addContentLoadListener (func) {
		if (document.addEventListener) {
			var DOMContentLoadFunction = function () {
				window.DOMContentLoaded = true;
				func();
			};
			document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
		}
		var oldfunc = (window.onload || new Function());
		window.onload = function () {
			if (!window.DOMContentLoaded) {
				oldfunc();
				func();
			}
		};
	}

//	jQuery(document).ready(function(){
		addContentLoadListener( function() {
				new CalendarJS().init("calendar");
		});
//	});


	function show_Calendar(p_strCalendar_Type){
		strCalendar_Type = p_strCalendar_Type;

		switch (strCalendar_Type){
			case "start_from":
				document.getElementById("content_z_index_calendar").style.left = "370px";
				document.getElementById("content_z_index_calendar").style.top = "300px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
			case "doksy_start_from":
			case "doksy_result_from":
				document.getElementById("content_z_index_calendar").style.left = "370px";
				document.getElementById("content_z_index_calendar").style.top = "220px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
			case "result_from":
				document.getElementById("content_z_index_calendar").style.left = "370px";
				document.getElementById("content_z_index_calendar").style.top = "195px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
			case "booking_from":
				document.getElementById("calendar_content").style.margin = "0px 0px 0px 130px";
				document.getElementById("content_z_index_calendar_inline").style.display = "block";
				document.getElementById("calendar_inline").style.display = "block";
				break;
			case "start_to":
				document.getElementById("content_z_index_calendar").style.left = "545px";
				document.getElementById("content_z_index_calendar").style.top = "300px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
			case "doksy_start_to":
			case "doksy_result_to":
				document.getElementById("content_z_index_calendar").style.left = "545px";
				document.getElementById("content_z_index_calendar").style.top = "220px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
			case "result_to":
				document.getElementById("content_z_index_calendar").style.left = "545px";
				document.getElementById("content_z_index_calendar").style.top = "195px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
			case "booking_to":
				document.getElementById("calendar_content").style.margin = "0px 0px 0px 305px";
				document.getElementById("content_z_index_calendar_inline").style.display = "block";
				document.getElementById("calendar_inline").style.display = "block";
				break;
			case "detail":
				document.getElementById("content_z_index_calendar").style.left = "750px";
				document.getElementById("content_z_index_calendar").style.top = "100px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
			case "detail_doksy":
				document.getElementById("content_z_index_calendar").style.left = "590px";
				document.getElementById("content_z_index_calendar").style.top = "200px";
				document.getElementById("content_z_index_calendar").style.display = "inline";
				break;
		}
	}

	function close_Calendar(p_strCalendar_ID){
		document.getElementById(p_strCalendar_ID).style.display = "none";
		strCalendar_Type = p_strCalendar_Type;
		switch (strCalendar_Type){
			case "booking_from":
			case "booking_to":
				document.getElementById("calendar_inline").style.display = "none";
				break;
		}
	}

	function setDate(p_strDate){
		switch (strCalendar_Type){
			case "start_from":
			case "doksy_start_from":
			case "result_from":
			case "doksy_result_from":
				document.getElementById("txtFrom").value = p_strDate;
				close_Calendar("content_z_index_calendar");
				break;
			case "booking_from":
				document.getElementById("txtFrom").value = p_strDate;
				close_Calendar("content_z_index_calendar_inline");
				break;
			case "start_to":
			case "doksy_start_to":
			case "result_to":
			case "doksy_result_to":
				document.getElementById("txtTo").value = p_strDate;
				close_Calendar("content_z_index_calendar");
				break;
			case "booking_to":
				document.getElementById("txtTo").value = p_strDate;
				close_Calendar("content_z_index_calendar_inline");
				break;
			case "admin":
				set_Booked_Out(p_strDate);
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value.replace(",,", ",");
				break;
			case "detail":
				break;
			case "detail_doksy":
				break;
		}
	}

	function get_blnIs_Booked_Out(p_strDate){
		// Pr�fen, ob ein Datum ausgebucht
		if (strDate_Out[p_strDate] == 1){
			return true;
		} else {
			return false;
		}
	}

	function get_str_Booked_Out_Status(p_strDate){
//		alert(p_strDate);
		// Pr�fen, ob ein Datum ausgebucht
		if (strDate_Out[p_strDate] == 1){
			return "_" + strDate_Out_Status[p_strDate];
		} else {
			return "";
		}
	}

	function set_Booked_Out(p_strDate){
		switch (strDate_Out_Status[p_strDate]){
			case "f":
				// war voll ausgebucht -> auf morgens ausgebucht setzen
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value.replace(p_strDate + "|f", "");
				strDate_Out[p_strDate] = 1;
				strDate_Out_Status[p_strDate] = "m";
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value + "," + p_strDate + "|m";
				document.getElementById(p_strDate).className = "booked_out_m";
				break;
			case "m":
				// war morgens ausgebucht -> auf nachmittags ausgebucht setzen
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value.replace(p_strDate + "|m", "");
				strDate_Out[p_strDate] = 1;
				strDate_Out_Status[p_strDate] = "a";
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value + "," + p_strDate + "|a";
				document.getElementById(p_strDate).className = "booked_out_a";
				break;
			case "a":
				// war nachmittags ausgebucht -> auf frei setzen
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value.replace(p_strDate + "|a", "");
				strDate_Out[p_strDate] = 0;
				strDate_Out_Status[p_strDate] = "";
				document.getElementById(p_strDate).className = "";
				break;
			default:
				// Datum war nicht gesetzt -> auf voll Ausgebucht setzen
				strDate_Out[p_strDate] = 1;
				strDate_Out_Status[p_strDate] = "f";
				document.getElementById("txtObject_Out_Date_List").value = document.getElementById("txtObject_Out_Date_List").value + "," + p_strDate + "|f";
				document.getElementById(p_strDate).className = "booked_out_f";
				break;
		}
	}
