//
// Scripts for Calendar generation and handling
//
var month_array = new Array("Januar","Februar","März","April","Mai","Juni",
                            "Juli","August","September","Oktober","November",
														"Dezember");
var now = new Date();

var actMonth=now.getMonth();
var actYear=now.getYear();

var Calendar = new Array();

function GenerateCalendar(month,year) {
  var cal=Calendar.length;
  Calendar[cal]=new Array();
	Calendar[cal].Month=month;
	Calendar[cal].Year=year;
	return cal;
}

function WriteCalendar(calID,hDoc,lbutton,rbutton) {
  var i,j;
  hDoc.write("<form name=date_list>");
	hDoc.write("<table bgcolor=silver><tr><td width=20>");
	if (lbutton)
	  hDoc.write("<input type=button onclick=preMonth("+calID+") value='<'> </input>");
		
  hDoc.write("</td>");
	hDoc.write("<td align=center id=c"+calID+"title width=150>"+month_array[now.getMonth()]+
	           " "+now.getYear()+"</td>");
	hDoc.write("<td width=20>");
  if (rbutton)
    hDoc.write("<input type=button onclick=nextMonth("+calID+") value='>'> </input>");
	hDoc.write("</td></tr>");
	
	//Write Matrix
	hDoc.write("<tr><td colspan=3><center>");
  hDoc.write("<table bgcolor=white border=0 cellspacing = 0 cellpading = 0 width=100%>");
	hDoc.write("<tr bgcolor=gray align=center>");
  hDoc.write("<td><font color=silver>Mo</font></td>"+
	           "<td><font color=silver>Di</td>"+
						 "<td><font color=silver>Mi</td>"+
						 "<td><font color=silver>Do</td>"+
						 "<td><font color=silver>Fr</td>"+
						 "<td><font color=silver>Sa</td>"+
						 "<td><font color=silver>So</td>");
  hDoc.write("</tr><tr>");
  for(j=0;j<6;j++) {
    for(i=0;i<7;i++) {
      hDoc.write("<td align=center id=c"+calID+"d"+i+"r"+j+
			           " onclick='onDateSelected("+calID+","+i+","+j+")'></td>");
    }
    hDoc.write("</tr>");
  }
  hDoc.write("</table>");
	
  hDoc.write("</center></td></tr></table>");
	hDoc.write("</form>");
	set_cal(calID,Calendar[calID].Month, Calendar[calID].Year);

}

function set_cal(calID,month,year) {
   begin_day = new Date (year,month,1);
   begin_day_date = begin_day.getDay();
   end_day = new Date (year,month+1,1);
   count_day = (end_day - begin_day)/1000/60/60/24;
	 if (count_day>31)
	   count_day=31;
	 //change title
	 column_name=eval("c"+calID+"title");
	 column_name.innerText=month_array[month]+" "+year;
	 //change table
   input_table(calID,begin_day_date,count_day);
}

function input_table(calID,begin,count) {
  var i,j;
  init(calID);
  j=0;
  if (begin!=0){i=begin-1;}else{i=6}
  for (c=1;c<count+1;c++) {
    colum_name = eval("c"+calID+"d"+i+"r"+j);
		//highligth now
    if ((now.getDate() == c) && 
		    (Calendar[calID].Month == now.getMonth())&&
				(Calendar[calID].Year == now.getYear())) {
		  colum_name.style.backgroundColor = "red";
			colum_name.style.color = "white";
		};
    colum_name.innerText = c;
    i++;
    if (i==7){i=0;j++;}
  }
}

function init(calID) {
  var i,j;
  for(j=0;j<6;j++) {
    for(i=0;i<7;i++) {
      colum_name = eval("c"+calID+"d"+i+"r"+j);
      colum_name.innerText =  "-";
      colum_name.style.backgroundColor ="";
      colum_name.style.color ="";
    }
  }
}

//
// Events
//
/*
function change_month(sel_month) {
  actMonth=sel_month;
  set_cal(actMonth,actYear);
}
function change_year(sel_year) {
  actYear = sel_year.value;
  set_cal(actMonth,actYear);
}
*/
function nextMonth(calID) {
  Calendar[calID].Month++;
	if (Calendar[calID].Month>11) {
	  Calendar[calID].Month=0;
		Calendar[calID].Year++;
	}
  set_cal(calID,Calendar[calID].Month,Calendar[calID].Year);
}

function preMonth(calID) {
  Calendar[calID].Month--;
	if (Calendar[calID].Month<0) {
	  Calendar[calID].Month=11;
		Calendar[calID].Year--;
	}
  set_cal(calID,Calendar[calID].Month,Calendar[calID].Year);
}


//function onDateSelected(calID,x,y) {
//  alert(calID+" "+x+" "+y);
//}


