var docHTML ;
var dateField ;
var arrayDays ;
var arrayMonth ;
var posX, posY;
var layerIDGlobal ;

window.onload = initCalendar ;

function initCalendar() {
	if (document.captureEvents) document.captureEvents(Event.MOUSEUP) ;
	document.onmouseup = initCalendarCoordinates ;
}

function initCalendarCoordinates (e) {
	// IE4
	if (document.all) {
		posX = event.clientX ;
		posY = event.clientY ;
	}
	else {
	// Netscape und W3C compatible
		posX = parseInt(e.pageX+10) ;
		posY = parseInt(e.pageY) ;
	}
}

function showCalendar(layerID, sField) {

// Der Kalender wird aktiviert oder deaktiviert
	layerIDGlobal = layerID ;
	dateField = sField ;
	if (document.getElementById) {
	// W3C compatible
		if (document.getElementById(layerID).style.visibility == "visible") document.getElementById(layerID).style.visibility = "hidden" ;
		else { 
			document.getElementById(layerID).style.visibility = "visible" ;
			document.getElementById(layerID).style.left = parseInt(posX)+10 ;
			document.getElementById(layerID).style.top = parseInt(posY);
		}
	}
	else if (document.layers) {
	// Netscape compatible
		if (document.layers[layerID].visibility == "visible" | document.layers[layerID].visibility == "show") document.layers[layerID].visibility = "hide" ;
		else { 
			document.layers[layerID].visibility = "show" ;
			document.layers[layerID].left = posX ;
			document.layers[layerID].top = posY ;
		}
	}
	else if (document.all) {
	// IE4 und  IE5 compatible
		if (document.all[layerID].style.visibility == "visible") document.all[layerID].style.visibility = "hidden" ;
		else { 
			document.all[layerID].style.visibility = "visible" ;
			document.all[layerID].style.left = parseInt(posX)+10 ;
			document.all[layerID].style.top = parseInt(posY) ;
			
		}
	}
	
}	
function drawCalendar(cYear, cMonth) {
// Der Kalender wird gezeichnet
	var myMonth = buildCalendar(cYear, cMonth)
	var showMonth = arrayMonth[parseInt(cMonth)] ;
	docHTML = "<table>" ;

//	docHTML = docHTML + "<tr>" ;
//	docHTML = docHTML + "<td colspan=7 class='headingTopic'>Bitte Tag auswählen</td>"
//	docHTML = docHTML + "</tr>" ;
	
	docHTML = docHTML + "<tr>" ;
	docHTML = docHTML + "<td class=cellTable><a class=arrowTable href='' onclick='changeCalendar(" + parseInt(cYear-1) + ", " + parseInt(cMonth) + ") ; return false ; '><<</a></td>" ;
	docHTML = docHTML + "<td class=cellTable><a class=arrowTable href='' onclick='changeCalendar(" + (cMonth==0 ? parseInt(cYear-1) : parseInt(cYear)) + ", " + (cMonth==0 ? 11 : parseInt(cMonth-1)) + ") ; return false ; '><</a></td>" ;
	docHTML = docHTML + "<td colspan=3 align=center><font class=headingText>" + showMonth + " " + cYear + "</font></td>" ;
	docHTML = docHTML + "<td class=cellTable><a class=arrowTable href='' onclick='changeCalendar(" + (cMonth==11 ? parseInt(cYear+1) : parseInt(cYear)) + ", " + (cMonth==11 ? 0 : parseInt(cMonth+1)) + ") ; return false ; '>></a></td>" ;
	docHTML = docHTML + "<td class=cellTable><a class=arrowTable href='' onclick='changeCalendar(" + parseInt(cYear+1) + ", " + parseInt(cMonth) + ") ; return false ; '>>></a></td>" ;
	docHTML = docHTML + "</tr>" ;
	docHTML = docHTML + "<tr>" ;	
	for (w=0; w<7; w++) {
		docHTML = docHTML + "<td><font class=headingDays>" + myMonth[0][w] + "</font></td>" ;
	}
	docHTML = docHTML + "</tr>" ;
	for (w=1; w<7; w++) {
		docHTML = docHTML + "<tr>" ;
		for (d=0; d<7; d++) {
			docHTML = docHTML + "<td class=cellTable>" ;
			if ( !isNaN(myMonth[w][d]) ) {
				docHTML = docHTML + "<a class=bodyDays href='' onclick='clickCalendar(" + myMonth[w][d] + ", " + parseInt(cMonth+1) + ", " + cYear + ") ; return false ; '>" + myMonth[w][d] + "</a>" ;
			}
			else docHTML = docHTML + "<font class=bodyDays>&nbsp;</font>" ;
			docHTML = docHTML + "</td>" ;
		}
		docHTML = docHTML + "</tr>" ;
	}
	docHTML = docHTML + "</table>" ;
	return docHTML;
}
function buildCalendar(aYear, aMonth) {
// Diese Funktion bestimmt eine Monats-Matrix
	var tMonth = new Array() ;
	var tDate1 = new Date(aYear, aMonth, 1) ;
	var tDate28 = new Date(aYear, aMonth, 28) ;
	var tDate29 = new Date(aYear, aMonth, 29) ;
	var tDate30 = new Date(aYear, aMonth, 30) ;
	var tDate31 = new Date(aYear, aMonth, 31) ;
	var tFirstDay = tDate1.getDay() ;
	var tDays = 0 ;
	if ( tDate31.getMonth() == tDate1.getMonth() ) tDays = tDate31.getDate() ;
	else if ( tDate30.getMonth() == tDate1.getMonth() ) tDays = tDate30.getDate() ;
	else if ( tDate29.getMonth() == tDate1.getMonth() ) tDays = tDate29.getDate() ;
	else if ( tDate28.getMonth() == tDate1.getMonth() ) tDays = tDate28.getDate() ;
	var tVar = 1 ;
	
	tMonth[0] = arrayDays ;
	tMonth[1] = new Array(7) ; 
	tMonth[2] = new Array(7) ; 
	tMonth[3] = new Array(7) ; 
	tMonth[4] = new Array(7) ; 
	tMonth[5] = new Array(7) ; 
	tMonth[6] = new Array(7) ; 
	if (tFirstDay == 0) {
		tMonth[1][6] = tVar ;
		tVar++ ;
	}
	else { 
		for (d=tFirstDay-1; d<7; d++) {
			tMonth[1][d] = tVar ;
			tVar++ ;
		}
	}
	for (w=2; w<7; w++) {
		for (d=0; d<7; d++) {
			if (tVar <= tDays) {
				tMonth[w][d] = tVar ;
				tVar++ ;	
			}
		}
	}
	return tMonth ;
	
}
function clickCalendar(sDay, sMonth, sYear) {
// Ein bestimmtes Datum wird ausgewählt
	if (sDay < 10) sDay = "0" + sDay ;
	if (sMonth < 10) sMonth = "0" + sMonth ;
	document.forms[0].elements[dateField].value = sDay + "." + sMonth + "." + sYear ;
	showCalendar(layerIDGlobal, "") ;
}

function changeCalendar(nYear, nMonth) {
// Monat oder Jahr des Kalenders werden geändert
	if (document.getElementById) {
	// W3C compatible
		drawCalendar(nYear, nMonth) ;
		document.getElementById(layerIDGlobal).innerHTML = docHTML ;
	}
	else if (document.layers) {
	// Netscape compatible
		document.layers["DatePicker"].document.open() ;
		drawCalendar(nYear, nMonth) ;
		document.write("<div class='layerBackground'>")
		document.write(docHTML) ;
		document.write("</div>")
		document.layers[layerIDGlobal].document.close() ;
	}
	else if (document.all) {
	// IE4 und  IE5 compatible
		drawCalendar(nYear, nMonth) ;
		document.all[layerIDGlobal].innerHTML = docHTML ;
	}
}



function UpdateFromFieldCalendar(layerID, sMonth, sYear) {
// Ein bestimmter Monat wird ausgewählt

layerIDGlobal = layerID ;

if (sMonth.substr(0,1)=="0")
{
sMonth = sMonth.substr(1,1)
}


//alert ("layer: "+ layerID);
//alert ("für: "+ sMonth + "." + sYear);

changeCalendar(parseInt(sYear), parseInt(sMonth)-1) ;
}



function makedate(feld) {

	window.status="";

	d1  = String(feld.value.substr(0,1));
	d2  = String(feld.value.substr(1,1));
	d3  = String(feld.value.substr(2,1));
	d4  = String(feld.value.substr(3,1));
	d5  = String(feld.value.substr(4,1));
	d6  = String(feld.value.substr(5,1));
	d7  = String(feld.value.substr(6,1));
	d8  = String(feld.value.substr(7,1));
	d9  = String(feld.value.substr(8,1));
	d10 = String(feld.value.substr(9,1));


	if (feld.value.length <=3 && feld.value.length !=0) 
	{ 
		feld.focus() ;
		alert ("Bitte mindestens 4 Zeichen (ttmm) \nz.B. 0209 für 02.09. eingeben!") ;
	}


	if (feld.value.length >3) 
	
	{
		var mydate = new Date();
		var tmpdate = new Date();
		var strtmp;
        var myJahr = mydate.getFullYear();
		if (d2 == ".") {feld.value = "0" + d1+d2+d3+d4+d5+d6+d7+d8+d9; makedate(feld);}
		if (d3 == "." && d5 == ".") {feld.value = d1+d2+d3+"0"+d4+d5+d6+d7+d8+d9; makedate(feld);}
		if (feld.value.length ==4 && d3 ==".") {feld.value = d1+d2+d3+"0"+d4+"."; makedate(feld);}
		if (feld.value.length ==4) {feld.value = d1+d2+"."+d3+d4+"."; makedate(feld);} 
		if (feld.value.length ==5 && d3 ==".") {feld.value = d1+d2+d3+d4+d5+"."; makedate(feld);}
		if (feld.value.length ==6 && d4 != "." && d3 != "." && d5 != ".") {feld.value = d1+d2+"."+d3+d4+"."+"20"+d5+d6; makedate(feld);}
		if (feld.value.length ==6 && d5 == ".") {feld.value = d1+d2+"."+d3+d4+d5+"200"+d6; makedate(feld);}
		if (feld.value.length ==6 && d3 == "." && d6 == ".") {feld.value = d1+d2+d3+d4+d5+d6+myJahr; makedate(feld);}
		if (feld.value.length ==7 && d3== "." && d6 == ".") {feld.value = d1+d2+d3+d4+d5+d6+"200"+d7; makedate(feld);}
		if (feld.value.length ==8 && d3== "." && d6 == ".") {feld.value = d1+d2+d3+d4+d5+d6+"20"+d7+d8; makedate(feld);}
		if (d3 != "." && d6 != "."){feld.focus(); feld.select();}
		if (feld.value.length != 10){feld.focus(); feld.select();}
		
		// wie viele Tage? - Gültiges Datum
		
				
		//alert ("Datumstring :" +d1+d2+d3+" D4: "+d4+ "D5: " + String(d5) +d6+d7+d8+d9+d10);
		
		if (d1=="0")	{
		var aDay = parseInt(d2) ;
		}
		else
		{
		var aDay = parseInt(d1+d2) ;
		}
		
		if (d4=="0")	{
		var aMonth = parseInt(d5);
		}
		else
		{
		var aMonth = parseInt(d4+d5);
		}
		
		var aYear = parseInt(d7+d8+d9+d10) ;
		
		//alert ("aDay:"+String(aDay) + "\naMonth:"+String(aMonth) +"\naYear:"+String(aYear));
		
		if ( aMonth >12 ) 
		{
			aMonth=12;
		}
		
				
		if (aMonth == 1 ||  aMonth == 3 || aMonth == 5 || aMonth == 7 || aMonth == 8 || aMonth == 10 || aMonth == 12 ) 
		{
			if ( aDay>31 ) {aDay=31;}
		}
		
		
		if (aMonth == 4 ||  aMonth == 6 || aMonth == 9 || aMonth == 11 ) 
		{
			if ( aDay>30 ) {aDay=30;}
		}
		
		
		if (aMonth == 2 ) 
		{
		
			var tDate1 = new Date(aYear, aMonth-1, 1) ;
			var tDate28 = new Date(aYear, aMonth-1, 28) ;
			var tDate29 = new Date(aYear, aMonth-1, 29) ;
			var tDate30 = new Date(aYear, aMonth-1, 30) ;
			var tDate31 = new Date(aYear, aMonth-1, 31) ;
			var tFirstDay = tDate1.getDay() ;
		
			var tDays = 0 ;
		
			if ( tDate31.getMonth() == tDate1.getMonth() ) tDays = tDate31.getDate() ;
			else if ( tDate30.getMonth() == tDate1.getMonth() ) tDays = tDate30.getDate() ;
			else if ( tDate29.getMonth() == tDate1.getMonth() ) tDays = tDate29.getDate() ;
			else if ( tDate28.getMonth() == tDate1.getMonth() ) tDays = tDate28.getDate() ;
		
					
			if ( aDay>tDays ) 
			{
				aDay=tDays;
			}
			
	
		
		}
	

			
		if ( aDay<10 ) 
			{	
		
				if ( aMonth<10 ) 
				{
				strtmp = "0" + String(aDay) + "." + "0" + String(aMonth) + "." + d7+d8+d9+d10;
				}
				else
				{	
				strtmp = "0" + String(aDay) + "." + String(aMonth) + "." + d7+d8+d9+d10;
				}	

			}
			
			else
			
			{
				
				if ( aMonth<10 ) 
				{
				strtmp = String(aDay) + "." + "0" + String(aMonth) + "." + d7+d8+d9+d10;
				}
				else
				{	
				strtmp = String(aDay) + "." + String(aMonth) + "." + d7+d8+d9+d10;
				}	
				
			}	
			//alert("dateok");
			return(strtmp);
		
		
		
			
	}

}
