/******************************************************************************
* File: calendar.js                                                           *
*                                                                             *
* Created    : Feb 13, 2007 by Claudia Egerer                                 *
* Last update: Feb 13, 2007 by Claudia Egerer                                 *
*                                                                             *
* Requires: nothing                                                           *
*                                                                             *
******************************************************************************/

var MONATE = new Array( "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember");
var MONATSTAGE = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var TAGE = new Array( "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag");

var ReturnDate=new Date();
var mylink = null;
var rooms = null;

function closeCalendar(mywin) {
//alert('function closeCalendar(' + mywin.name + ')');
    var mm = 1+this.ReturnDate.getMonth();
    mm = mm < 10 ? "0"+mm : mm;
    var tt = this.ReturnDate.getDate();
    tt = tt<10? "0"+tt : tt;
    var yyyy = this.ReturnDate.getFullYear();
    var x = document.getElementById('calendarReturnField').value;
//alert(window.HuHu);
//    window.opener.document.getElementById(window.HuHu).value= tt+"."+mm+"."+yyyy;
//    window.opener.document.getElementById('termin').value= tt+"."+mm+"."+yyyy;
//	document.location.href='vakanz_zimmer.html?'+tt+'.'+mm+'.'+yyyy;
	if ( myLink.length > 1) document.location.href=myLink + '&hindat='+tt+'.'+mm+'.'+yyyy+ "&rooms=" + rooms;
}


function InitCalendar()
{
        bAllowPast = window.AllowPast; 
	var nodeMonth = document.getElementById("month_list");
	var nodeYear = document.getElementById("year_list");
	while( nodeMonth.hasChildNodes() )
	{
		nodeMonth.removeChild(nodeMonth.firstChild);
	}
	while( nodeYear.hasChildNodes() )
	{
		nodeYear.removeChild(nodeYear.firstChild);
	}
	for ( var i = 0; i < MONATE.length; i++ )
	{
		var newOpt = document.createElement("option");
		var newOptTxt = document.createTextNode( MONATE[i] );
		newOpt.value = i;
		newOpt.appendChild(newOptTxt);
		nodeMonth.appendChild(newOpt);
	}
	var now = new Date(start_year, start_month, start_day);
	var now_date = new Date();
	for ( var i = (bAllowPast)?now.getFullYear()-1:now.getFullYear(); i < now.getFullYear()+4; i++ )
	{
		var newOpt = document.createElement("option");
		var newOptTxt = document.createTextNode( i );
		newOpt.value = i;
		newOpt.appendChild(newOptTxt);
		nodeYear.appendChild(newOpt);
	}
	Set2Month(now.getDate(), now.getMonth(), now.getFullYear() );
	nodeMonth.onclick = function onclick(event){Set2Month(ReturnDate.getDate(),this.value,ReturnDate.getFullYear());};
	nodeYear.onclick  = function onclick(event){Set2Month(ReturnDate.getDate(),ReturnDate.getMonth(),this.value);};
}
function ShowZimmer()
{
	Set2Month( ReturnDate.getDate(), ReturnDate.getMonth() , ReturnDate.getFullYear() );
}

function MonthUp()
{
	var mm = ReturnDate.getMonth();
	var yyyy = ReturnDate.getFullYear();
	mm ++;
	mm ++;
	if (mm > 12){
		yyyy++;
		mm = mm - 12;
	} 
	if (mm < 1){
		yyyy--;
		mm = 12;
	} 
    mm = mm < 10 ? "0"+mm : mm;
    var yy = yyyy.toString();
    yy = yy.substr(2, 2);
// (VS, 14.06.10): 1. des Monats, da WBSCGI jetzt auch mit genauen Datumsangaben umgehen kann	
	document.location.href='handle?action=show_hoteltage&start_date=01'+mm+yy+'&zimmerart='+document.getElementById("zimmerart").value;
}
function MonthDown()
{
	var mm = ReturnDate.getMonth();
	var yyyy = ReturnDate.getFullYear();
	if (mm > 12){
		yyyy++;
		mm = mm-12;
	} 
	if (mm < 1){
		yyyy--;
		mm = 12;
	} 
    mm = mm < 10 ? "0"+mm : mm;
    var yy = yyyy.toString();
    yy = yy.substr(2, 2);
// (VS, 14.06.10): 1. des Monats, da WBSCGI jetzt auch mit genauen Datumsangaben umgehen kann	
	document.location.href='handle?action=show_hoteltage&start_date=01'+mm+yy+'&zimmerart='+document.getElementById("zimmerart").value;
}
function MonthSelect()
{
	mlist = document.getElementById("month_list");
	mm = mlist.options[mlist.selectedIndex].value;
	ylist = document.getElementById("year_list");
	yyyy = ylist.options[ylist.selectedIndex].value;
	mm ++;
	if (mm > 12){
		yyyy++;
		mm = mm - 12;
	} 
	if (mm < 1){
		yyyy--;
		mm = 12;
	} 
    mm = mm < 10 ? "0"+mm : mm;
    var yy = yyyy.toString();
    yy = yy.substr(2, 2);
// (VS, 14.06.10): 1. des Monats, da WBSCGI jetzt auch mit genauen Datumsangaben umgehen kann	
	document.location.href='handle?action=show_hoteltage&start_date=01'+mm+yy;
}
function InArray ( arr, el  ){
	for ( var i = 0; i < arr.length; i ++ ) if ( arr[i] == el ) return true;
      return false 
}
function Set2Month( tt, mm, yyyy)
{
	if (mm == 12){
		yyyy++;
		mm = 0;
	} 
	if (mm < 0){
		yyyy--;
		mm = 11;
	} 
	
	var now = new Date(start_year, start_month, start_day);
	var now_date = new Date();
	now = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	var cal = document.getElementById("inhalt");
        if ( !bAllowPast ) {
     	  if ( yyyy < now.getFullYear() ){ return; }
	  else if ( yyyy == now.getFullYear() && mm < now.getMonth() ) return;
        }
	ReturnDate = new Date(yyyy, mm, tt);
	document.getElementById("month").innerHTML = GetMonat(mm) + "  " + yyyy;
	
	var nFirstDay = (new Date(yyyy,mm,1)).getDay();
	if (nFirstDay == 0 ) nFirstDay = 7;

	{//Anzahl Zeilen im Kalender
	var nRows = GetDaysInMonth(mm,yyyy)-(8-nFirstDay);
	if ( nRows <= 21 )
	{
		nRows = 4;
		document.getElementById("r5").style.display= "none";
		document.getElementById("r6").style.display= "none";
	}
	else if ( nRows <= 28 )
	{
		nRows = 5;
		document.getElementById("r5").style.display= "block";
		document.getElementById("r6").style.display= "none";
	}
	else
	{
		nRows = 6;
		document.getElementById("r5").style.display= "block";
		document.getElementById("r6").style.display= "block";
	}
	}

	for ( var i = 1; i <= nRows; i++ )
	{
		for ( var j = 1; j <= 7; j++ )
		{
			var strClass = "ar-day";
			// if ( j == 6 ) strClass += " ar-sat";
			// else if ( j == 7 ) strClass += " ar-sun";
			var c = document.getElementById("c_"+i+"_"+j)
			nDay = ((j+((i-1)*7))+1-nFirstDay);
			strDay = nDay.toString();
			if ( i == 1 && j < nFirstDay ) c.innerHTML = "&nbsp;";
			else if ( nDay <= GetDaysInMonth(mm,yyyy) )
			{	
				strDay = strDay.length == 2 ? strDay : "0"+strDay;
				c.innerHTML = strDay;
				if ( !bAllowPast && new Date(yyyy,mm,nDay) < now )
				{
					strClass += " ar-past";
					c.onmouseover=null;
					c.onmouseout= null;
					c.onclick= null;
				}
				else if ( yyyy == now_date.getFullYear() && mm == now_date.getMonth() && nDay == now_date.getDate() )
				{
					strClass = "ar-day ar-current";
					if ( links[document.getElementById("zimmerart").value][nDay-1]) {
					if ( links[document.getElementById("zimmerart").value][nDay-1].length > 1) {
						c.style.fontWeight = 'bold';
						c.style.textDecoration = 'underline';
					}
					}
					else
					{
					
						c.style.fontWeight = 'normal';
						c.style.textDecoration = 'none';
					}
					c.onmouseover=function onmouseover(event){this.className = 'ar-dayOv';};
					c.onmouseout= function onmouseout(event){this.className = 'ar-day ar-current';};
					c.onclick= function onclick(event){ReturnDate.setDate(this.innerHTML); dispatch(); closeCalendar(window);};
				}
				else
				{
					if ( links[document.getElementById("zimmerart").value][nDay-1]) {
					if ( links[document.getElementById("zimmerart").value][nDay-1].length > 1) {
						c.style.fontWeight = 'bold';
						c.style.textDecoration = 'underline';
					}
					}
					else
					{
					
						c.style.fontWeight = 'normal';
						c.style.textDecoration = 'none';
					}
					c.onmouseover= function onmouseover(event){this.className = 'ar-dayOv';};
					c.onclick    = function onclick(event){ReturnDate.setDate(this.innerHTML); dispatch(); closeCalendar(window);};
					// if ( j == 6 )
					// {
					// 	c.onmouseout = function onmouseout(event){this.className = 'ar-day ar-sat';};
					// }
					// else if ( j == 7 )
					// {
					// 	c.onmouseout = function onmouseout(event){this.className = 'ar-day ar-sun';};
					// }
	 				// else
					// {
					   c.onmouseout = function onmouseout(event){this.className = 'ar-day';};
                                 if ( ferien[document.getElementById("zimmerart").value] )
                                 {
                                   var f = ferien[document.getElementById("zimmerart").value][yyyy];
                                   if ( f ) 
                                   {
                                      if ( InArray(f[mm], nDay ) ) {
									  strClass += ' ar-ferien';
									  c.onmouseout = function onmouseout(event){this.className = 'ar-day ar-ferien';};
									  // c.style.backgroundColor = '#cc0000';
									  }
                                   }                         
                                 }
					// }
				}			
			}
			else 
			{
				c.innerHTML = "&nbsp;";
				c.onclick = null;
				c.onmouseover = null;
				c.onmouseout = null;
			}

			c.className  = strClass;
		}
	}

	{//Listbox
	        bAllowPast = window.AllowPast;
		var list = document.getElementById("month_list");
		list.value = mm;
		list = document.getElementById("year_list");
		if(bAllowPast) {
		  selection = yyyy - now.getFullYear() + 20;
		} else {
		  selection = yyyy - now.getFullYear();
		}
                list.options[selection].setAttribute('selected', 'true');
	}
	
}

function GetDaysInMonth(month, year)
{
	//alert(year%4);
	if ( month <12 ) return (month==1 && year%4 == 0) ? 29 : MONATSTAGE[month];
	else return 31;
}
function GetMonat(month) { return MONATE[month]; }

function dispatch ()
{
var t = this.ReturnDate.getDate()-1;
myLink = links[document.getElementById("zimmerart").value][t];
rooms = document.getElementById("zimmerart").value;

}









