var MouseX0 = 0;
var MouseX  = 0;
var MouseY0 = 0;
var MouseY  = 0;



//if(window.Event) document.captureEvents(Event.MOUSEMOVE);
window.onload        = SaveMouseXY;
document.onmousemove = SaveMouseXY;

function SaveMouseXY (e) {
  if(navigator.appName.substring(0,1) == "N") {
    MouseX0 = 0; MouseY0 = e.mouseX;
    MouseX = e.pageX;
    MouseY = e.pageY;
  }
  else {
    MouseX0 = window.event.x;
    MouseY0 = window.event.y;
    MouseX = document.body.scrollLeft + MouseX0;
    MouseY = document.body.scrollTop  + MouseY0;
  }
}
function kalender_show () {
  document.getElementById("kalender_hiddenbox").style.visibility = "visible";
  document.getElementById("kalender_hiddenbox").style.left = MouseX+"px";
  document.getElementById("kalender_hiddenbox").style.top  = MouseY+"px";
}
// *************************************************************



var cal=[];
for (var i=0;i<6;i++)
  	cal[i]=[];
	
setToday();

function setTableKalender()
{
	tdKalender=document.getElementById("td_kalender");
	tableKalender=document.getElementById("table_kalender");
	if (tableKalender)
		tdKalender.removeChild(tableKalender);
	inner='<table id="table_kalender" class="text_label1"><tr><td></td><td>M</td><td>D</td><td>M</td><td>D</td><td>F</td><td>S</td><td>S</td></tr>';
	id=0;
  	for (w=1; w<7; w++) 
	{
		inner+='<tr><td><span id="woche'+w+'"></span></td>';
		for (d=0; d<7; d++)
		{
			inner+='<td align="center"><a class="kalender_link" id="'+id+'">00</a></td>';
			id++;
		}
		inner+="</tr>";
	}
	tdKalender.innerHTML=inner+"</table>";
}

function setToday() 
{
	var now   = new Date();
	var day   = now.getDate();
	var month = 0;
	var year  = 1950;
	this.focusDay = day;
	
	document.formular.month.selectedIndex = month;
	document.formular.year.selectedIndex = year-1930;
	displayCalendar(month, year);
}


function isFourDigitYear(year) 
{
	if (year.length != 4) 
	{
		alert ("Sorry, the year must be four-digits in length.");
		document.formular.year.select();
		document.formular.year.focus();
	}
	else
	{
		return true; 
	}
}

function selectDate() 
{
	var year  = document.formular.year.value;
	if (isFourDigitYear(year)) 
	{
		var day   = 0;
		var month = document.formular.month.selectedIndex;
		displayCalendar(month, year);
    }
}



function setPreviousYear() 
{
	var year  = document.formular.year.value;
	if (isFourDigitYear(year)) 
	{
		var day   = 0;
		var month = document.formular.month.selectedIndex;
		year--;
			document.formular.year.value = year;
			displayCalendar(month, year);
   }
}

function setPreviousMonth() 
{
	var year  = document.formular.year.value;
	if (isFourDigitYear(year)) 
	{
		var day   = 0;
		var month = document.formular.month.selectedIndex;
		if (month == 0) 
		{
			month = 11;
			if (year > 1000) 
			{
				year--;
					document.formular.year.value = year;
			}
		}
		else
		{
			month--;
		}
		{
			document.formular.month.selectedIndex = month;
			displayCalendar(month, year);
		}
	}
}


function setNextMonth() 
{
	var year  = document.formular.year.value;
	if (isFourDigitYear(year)) 
	{
		var day   = 0;
		var month = document.formular.month.selectedIndex;
		if (month == 11) 
		{
			month = 0;
			year++;
				document.formular.year.value = year;
		}
		else
		{
			month++; 
		}
		{
			document.formular.month.selectedIndex = month;
			displayCalendar(month, year);
		}
   }
}


function setNextYear() 
{
	var year = document.formular.year.value;
	if (isFourDigitYear(year)) 
	{
		var day = 0;
		var month = document.formular.month.selectedIndex;
		year++;
		{
			document.formular.year.value = year;
			displayCalendar(month, year);
		}
   }
}

function fBuildCal(y,m) {
  m=parseInt(m,10);
  var days=[31,31,(y%4==0&&y%100!=0||y%400==0)?29:28,31,30,31,30,31,31,30,31,30,31];
  var dCalDate=new Date(y,m,1);
  var iDayOfFirst=dCalDate.getDay();
  if (--iDayOfFirst<0)
  {
	iDayOfFirst=6;
  }
  var iOffsetLast=days[m]-iDayOfFirst+1;
  var iDate=1;
  var iNext=1;
  for (var d=0;d<7;d++)
	cal[0][d]=(d<iDayOfFirst)?[m-1,-(iOffsetLast+d)]:[m,iDate++];
  for (var w=1;w<6;w++)
  	for (var d=0;d<7;d++)
		cal[w][d]=(iDate<=days[m+1])?[m,iDate++]:[m+1,-(iNext++)];
  return cal;
}


function number_woche(y,m)
{
  m=parseInt(m,10);
  if (m==0)
  	return 1;
  var dCalDate=new Date(y,m,1);
  var dCalDate1=new Date(y-1,11,31);
  var dCalDate2=new Date(y,0,1);
  
  return Math.ceil(   ((Date.parse(dCalDate)-Date.parse(dCalDate1))/24/60/60/1000 + ((dCalDate2.getDay()==0) ? 6 : dCalDate2.getDay()-1)    )/7);
}



function displayCalendar(m,y) {
  document.formular.month.selectedIndex = m;
  document.formular.year.selectedIndex = y-1930;
  setTableKalender();
  m = parseInt(m);
  y = parseInt(y);
  var c=fBuildCal(y,m);
  var erst_woche=number_woche(y,m);
  var el_woch=[];
  el_woch=document.getElementById("td_kalender").getElementsByTagName("SPAN");
  for (var i=0; i<el_woch.length; i++)
  	if (el_woch[i].id.substr(0,5)=="woche")
	{
		el_woch[i].innerHTML="("+erst_woche+")"; 
		erst_woche = erst_woche+1;
		el_woch[i].style.color="#666666"
	}
  var gCellSet=[];
  gCellSet=document.getElementById("td_kalender").getElementsByTagName("A");
  var d;
 for (var week=0; week<6; week++)
	for (var day=0; day<7; day++) {
		m=c[week][day][0];
		d=c[week][day][1];
		with (gCellSet[(7*week)+day]) {
			if (d<0) {
				style.color="silver";
				d=-d;
			}else{
				style.color=(day==6)?"red":(day==5)?"red":"black";
			}
			
			href="javascript:kalender_user_function("+d+","+((m<0) ? 12 : ((m==12)?1:(m+1)))+","+((m<0) ? (y-1) : ((m==12)?y+1:y))+");";
			innerHTML=d;
		}
	}
	if (top.setMonat)
		top.setMonat();
}



