/**********************************************************************
*          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.            *
*                                                                      *
 **********************************************************************/



// Bereits ge�ffnete Kalender
var arrCalender_Open = new Array();
// Aktuell ge�ffneter Kalender
var strOpen_Calendar = '';
// Ausgebucht anzeigenja/nein
var blnShow_Booked_Out = false;
// Datum im Kalender verlinken ja/nein
var blnShow_Link = false;
// Textfeld
var strField_ID = ''
function open_Calendar_For_Search(p_strIn_Container_ID, p_strRead_From_Text_ID){
	var blnContainer_Exist = false;

	strField_ID = p_strRead_From_Text_ID;

	blnShow_Link = true;

	if (strOpen_Calendar == p_strIn_Container_ID){
		// gleicher Kalender bereits g�ffnet
		// einfach schliessen
		hide_Calendar();
	}else{
		hide_Calendar();

		// Pr�fen, ob bereits ein Container f�r den Kalender erstellt wurde
		jQuery.each(arrCalender_Open, function() {
			if(this == p_strIn_Container_ID){
				blnContainer_Exist = true;
				return false;
			}
	    });

		// Kalender �ffnen
	    if(blnContainer_Exist == false){
	    	// Container erstellen
	    	if(create_Calendar(p_strIn_Container_ID, p_strRead_From_Text_ID) == true){
	    		arrCalender_Open.push(p_strIn_Container_ID)
	    	}
	    }
	    show_Calendar(p_strIn_Container_ID);
	}
}
function show_Calendar(p_strIn_Container_ID){
	jQuery('#' + p_strIn_Container_ID).addClass('kalender_zeigen');
	strOpen_Calendar = p_strIn_Container_ID;

}
function hide_Calendar(){
	if(strOpen_Calendar != ''){
		jQuery('#' + strOpen_Calendar).removeClass('kalender_zeigen');
	}
	strOpen_Calendar = '';
}
function create_Calendar(p_strIn_Container_ID, p_strRead_From_Text_ID){
	var objCalendar = new CalendarJS(p_strRead_From_Text_ID, jQuery('#' + p_strRead_From_Text_ID).attr('value'));
	objCalendar.init(p_strIn_Container_ID);
	return true;
}
function setDate(strYear, strMonth, strDay){
	switch (strField_ID){
		case "txtFrom":
		case "txtTo":
			jQuery('#' + strField_ID).attr('value', strDay + "." + strMonth + "." + strYear)
			hide_Calendar();
			break;
	}
}





//	var blnShow_Link = 0;
//	var blnShow_Booked_Out = 0;
//	var strField_ID = "";
//	var objOpener;
//	var objWindow_Calendar;
	var objDate_Selected = "";
//	var strClass_Selected = "";
//	var strClass_Today = "";

//	function open_Calendar_Window (p_strLink){
//		objWindow_Calendar = window.open(p_strLink, "calendar", "width=199,height=160,left=200,top=100,scrollbars=no,dependent=yes,location=no,menubar=no,resizeable=no,status=no,toolbar=no");
//		objWindow_Calendar.focus();
//		return false;
//	}

	function get_objDate(p_strDate, p_blnReturn_Now){
		// Pr�fen, ob das angegebene Datum OK ist
		if (p_strDate != ""){
			intDay_Selected = Number(p_strDate.substring(0, 2));
			intMonth_Selected = Number(p_strDate.substring(3, 5)) - 1;
			intYear_Selected = Number(p_strDate.substring(6, 10));

			if (!isNaN(intYear_Selected) && !isNaN(intMonth_Selected) && !isNaN(intDay_Selected)){
				objDate_Selected = new Date(intYear_Selected, intMonth_Selected, intDay_Selected);
				if(objDate_Selected != ""){
					objDate = new Date(intYear_Selected, intMonth_Selected, intDay_Selected);
					return objDate;
				} else {
					if (p_blnReturn_Now){
						return new Date();
					} else {
						return false;
					}
				}
			} else {
				if (p_blnReturn_Now){
					return new Date();
				} else {
					return false;
				}
			}
		} else {
			if (p_blnReturn_Now){
				return new Date();
			} else {
				return false;
			}
		}
		alert(objDate_Selected);
	}

	function get_strDate_Check(p_strDate){
		// Pr�fen ob alle erforderlichen f�hrenden Nullen vorhanden sind
		var arrDate = p_strDate.split(".");

		// Tag mit f�hrenden Nullen
		if (arrDate.length == 3){
			if (arrDate[0].length == 1){
				arrDate[0] = "0" + arrDate[0];
			}
			if (arrDate[1].length == 1){
				arrDate[1] = "0" + arrDate[1];
			}
			if (arrDate[2].length == 2){
				arrDate[2] = "20" + arrDate[2];
			}
			return arrDate[0] + "." + arrDate[1] + "." + arrDate[2];
		} else {
			return p_strDate;
		}
	}

	function CalendarJS(p_strRead_From_Text_ID, p_strValue) {
		// auslesen, welches Datum schon ausgew�hlt ist
		this.blnSelect_To_From_Start = true;

		var strField_ID = p_strRead_From_Text_ID;
		switch(strField_ID){
			case "txtFrom":
				strDate_Selected_From = p_strValue;
				strDate_Selected_From_Checked = get_strDate_Check(strDate_Selected_From);
				if (strDate_Selected_From != strDate_Selected_From_Checked){
					strDate_Selected_From = strDate_Selected_From_Checked;
				}
				objDate = get_objDate(strDate_Selected_From, true);
				break;
			case "txtTo":
				strDate_Selected_To = p_strValue;
				strDate_Selected_To_Checked = get_strDate_Check(strDate_Selected_To);
				if (strDate_Selected_To != strDate_Selected_To_Checked){
					strDate_Selected_To = strDate_Selected_To_Checked;
				}
				objDate = get_objDate(strDate_Selected_To, false);
				if (objDate == false){
					// Datum vom Start nehmen
					strDate_Selected_From = document.getElementById('txtFrom').value;
					objDate = get_objDate(strDate_Selected_From, true);

					// Als Referenz wird das Startdatum genommen, aber nicht als markiert gesetzt
					this.blnSelect_To_From_Start = false;
				}
				break;
			default:
				strDate_Selected = "";
				objDate = new Date();
				break;
		}

		this.now = objDate;

//		if (strDate_Selected != ""){
//			intDay_Selected = Number(strDate_Selected.substring(0, 2));
//			intMonth_Selected = Number(strDate_Selected.substring(3, 5)) - 1;
//			intYear_Selected = Number(strDate_Selected.substring(6, 10));
//
//			if (!isNaN(intYear_Selected) && !isNaN(intMonth_Selected) && !isNaN(intDay_Selected)){
//				objDate_Selected = new Date(intYear_Selected, intMonth_Selected, intDay_Selected);
//				if(objDate_Selected != ""){
//					this.now = new Date(intYear_Selected, intMonth_Selected, intDay_Selected);
//				} else {
//					this.now = new Date();
//				}
//			} else {
//				this.now = new Date();
//			}
//		} else {
//			this.now = new Date();
//		}
		this.dayname = ["Mo","Di","Mi","Do","Fr","Sa","So"];
		this.monthname = ["Januar","Februar","M" + unescape("%E4") + "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" + unescape("%C4") + "chster Monat"];
		this.monthCell = document.createElement("th");
		this.tableHead = null;
		this.tableFooter = 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.tableFooter = this.createTableFooter();
			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 );
			table.appendChild( this.tableFooter );
			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") );
			thead.appendChild( tr );
			return thead;
		}
		this.createTableFooter = function() {
			var tfooter = document.createElement("tfoot");
			var tr = document.createElement("tr");
			var td = document.createElement("td");
			td.colSpan = '7';
//			td.textAlign = 'center';
			td.appendChild(document.createTextNode( "schliessen" ));
			td.Instanz = this;
			td.onclick = function() {
				hide_Calendar();
			};
			td.title = this.tooltip[0];
			try { td.style.cursor = "pointer"; } catch(e){ td.style.cursor = "hand"; }
			tr.appendChild( td );
			tfooter.appendChild( tr );
			return tfooter;
		}

		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) );

			objDate_Now = new Date();

			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);
				}

				if (blnShow_Booked_Out == 1){
					strClassBooked_Out = "booked_out" + get_str_Booked_Out_Status(this.yy + "-" + strMounth + "-" + strDay);
				}

				if (blnShow_Link == 1 && strField_ID == "txtObject_Out_Date_List") {
					blnLink = 1;
				} else {
					// Links nur in Buchungsformularen und wenn Datum > heute und nicht ausgebucht
					// Bei Startdatum nicht Status = "a"
					// Bei Enddatum nicht Status = "m"
					objDate_Loop = new Date(this.yy, this.mm, i);

					if (blnShow_Link && objDate_Loop > objDate_Now) {
						blnLink = 1;
					} else {
						blnLink = 0;
					}
				}

				if (objDate_Loop.getYear() == objDate_Now.getYear() && objDate_Loop.getMonth() == objDate_Now.getMonth() && objDate_Loop.getDate() == objDate_Now.getDate()){
					strClass_Today = "today";
					if (blnShow_Link) {
						blnLink = 1;
					}
				} else {
					strClass_Today = "";
				}

				if (objDate_Selected != "" && this.blnSelect_To_From_Start != false){
					if (objDate_Loop.getYear() == objDate_Selected.getYear() && objDate_Loop.getMonth() == objDate_Selected.getMonth() && objDate_Loop.getDate() == objDate_Selected.getDate()){
						strClass_Selected = "selected";
					} else {
						strClass_Selected = "";
					}
				} else {
					strClass_Selected = "";
				}

				if (i==this.date && this.mm==this.month && this.yy==this.year && (sevendaysaweek == 5 || sevendaysaweek == 6))
					if (blnShow_Booked_Out == 1){
						if (get_blnIs_Booked_Out(this.yy + "-" + strMounth + "-" + strDay)){
							tr.appendChild( this.getCell( "td", i , strClass_Selected + " " + strClass_Today + " weekend " + strClassBooked_Out, blnLink, this.yy, strMounth, strDay) );
						} else {
							tr.appendChild( this.getCell( "td", i , strClass_Selected + " " + strClass_Today + " weekend" , blnLink, this.yy, strMounth, strDay) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , strClass_Selected + " " + strClass_Today + " weekend" , blnLink, this.yy, strMounth, strDay) );
					}
				else if (i==this.date && this.mm==this.month && this.yy==this.year)
					if (blnShow_Booked_Out == 1){
						if ( get_blnIs_Booked_Out(this.yy + "-" + strMounth + "-" + strDay)){
							tr.appendChild( this.getCell( "td", i , strClass_Selected + " " + strClass_Today + " " + strClassBooked_Out , blnLink, this.yy, strMounth, strDay) );
						} else {
							tr.appendChild( this.getCell( "td", i , strClass_Selected + " " + strClass_Today , blnLink, this.yy, strMounth, strDay) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , strClass_Selected + " " + strClass_Today , blnLink, this.yy, strMounth, strDay) );
					}
				else if (sevendaysaweek == 5 || sevendaysaweek == 6)
					if (blnShow_Booked_Out == 1){
						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, this.yy, strMounth, strDay) );
					}
				else
					if (blnShow_Booked_Out == 1){
						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 , strClass_Today , blnLink, this.yy, strMounth, strDay) );
						}
					} else {
						tr.appendChild( this.getCell( "td", i , strClass_Today , blnLink, this.yy, strMounth, strDay) );
					}
			}

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

			tbody.appendChild( tr );
			return tbody;

		}

		this.getCell = function(tag, str, cssClass, blnLink, strYear, strMonth, strDay) {
			var El = document.createElement( tag );
			if (blnLink == 1){
				El.innerHTML = "<a href=\"javascript:setDate('" + strYear + "', '" + strMonth + "', '" + strDay + "');\" class='link_calendar " + cssClass + "'>" + str + "</a>";
				El.setAttribute("id", strYear + "-" + strMonth + "-" + strDay);
			} 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();
//			}
//		};
//	}
//
//	if (blnOpen_Calendar == 1){
//		addContentLoadListener( function() {
//				document.getElementById("calendar").innerHTML = "";
//				new CalendarJS().init("calendar");
//		} );
//	}
//
//	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){
//		// Pr�fen, ob ein Datum ausgebucht
//		if (strDate_Out[p_strDate] == 1){
//			return "_" + strDate_Out_Status[p_strDate];
//		} else {
//			return "";
//		}
//	}
//

//	function close_Calendar(){
//		window.close();
//	}
//
//	objOpener = opener;

