// -------------------------------------
// calendarscript	
//
// needs lib_neu.js and calendar css
// and the table
// params for creating
// 1. pickup formelement
// 2. return formelement
//
// params for initializing
// 1. element to read out (1 or 2) or 0
// 2. element to update
// 3. additional element to update
// 4 + 5. x and y 
//
// author:    jw
// created: 
// 
// modified:  17.04.2003
// for:       modified for sixty	
//
// modified:  30.06.2003
// for:       IE 5.1.bug with innerHTML
//            fixed by using dom methods
//
// -------------------------------------

var dName   =  new Array("Mo","Di","Mi","Do","Fr","Sa","So");
var monName	=  new Array ("JANUAR","FEBRUAR","M&Auml;RZ","APRIL","MAI","JUNI","JULI","AUGUST","SEPTEMBER","OKTOBER","NOVEMBER","DEZEMBER");
var footer  =  "Kalender schlie&szlig;en";
var add_day =  1;

function makeCal(formS, formE)
{
	this.outForm    =  new Array();
	this.outForm[1] =  formS;
	this.outForm[2] =  formE;
	this.posDiv		=  posDiv;
	this.initCal	=  initCal;
	this.nextMonth	=  nextMonth;
	this.pastMonth	=  pastMonth;	
	this.writeCal	=  writeCal;
	this.wrForm     =  wrForm;
	this.dplCal     =  dplCal;	
}

function posDiv() {	
	dRefS("calendar").left =  this.calAktX + ((dom && !op6)? "px" : "");
	dRefS("calendar").top  =  this.calAktY + ((dom && !op6)? "px" : "");	
}	

function initCal(rdIdx, akIdx, adIdx, x, y) {
		
	this.calAktForm	=  akIdx;
	this.caladdForm	=  adIdx;
	this.calAktX    =  x;	
	this.calAktY    =  y;

	// today
	var today       =  new Date();
	var todayMon    =  today.getMonth();
	var todayYear   =  (today.getYear() < 1000)? today.getYear() + 1900 : today.getYear();
	var todayDate   =  today.getDate();
	// uhrzeitbereinigt
	this.todayDate  =  new Date(todayYear, todayMon, todayDate);
	this.todayTime  =  this.todayDate.getTime();	
	
	var date        =  rdIdx? this.outForm[rdIdx].value : 0;	
	var reg			=	/^([0-9]{1,2})[\.\,]([0-9]{1,2})[\.\,]([0-9]{2,4})$/;
	
	if(reg.exec(date)) {
		var datArr   =  reg.exec(date);
		datArr[3]    =  (datArr[3].length == 2)? "20" + datArr[3] : datArr[3];
		datArr[2]    =  (datArr[2] > 0 )? datArr[2] - 1 : 11;
		this.aktDate =  new Date(datArr[3], datArr[2], datArr[1]);
	} else {
		this.aktDate =	this.todayDate;
	}
	
	// aktuell
	this.aktMonth =  this.aktDate.getMonth();
	this.aktYear  =  (this.aktDate.getYear() < 1000)? this.aktDate.getYear() + 1900 : this.aktDate.getYear();
	this.aktTime  =  this.aktDate.getTime();	
	
	// positionieren und schreiben			 				 		
	this.writeCal();
	this.posDiv();
	this.dplCal(1);
}

// Old browser: creating complete table string and writing it into "#calendar"
// Dom-browser: appending month/days to the table cells
function writeCal() {	

	var cnt     =  1;
	// erster Tag im Monat
	firstDay    =  new Date(this.aktYear, this.aktMonth, 1);
	firstDayId  =  firstDay.getDay();
	firstDayId  =  (firstDayId > 0)? firstDayId - 1 : 6;         // Sonntag hier siebter Tag
	// x Tage im Monat
	nextMonth   =  new Date(this.aktYear, this.aktMonth + 1 ,1); // erster Tag naechster Monat
	monthDur    =  nextMonth - (1000*60*60*23);                  // einen Tag abziehen
	lastDay     =  new Date(monthDur);                           // Datum
	lastDayDate =  lastDay.getDate();	
	

	// setting month, year and dayNames
	if(dom) {
	
		var day_content, day_cnt, past_content, temp, tempTime;	
		
		// month
		if (dRef("monthTd").firstChild) dRef("monthTd").removeChild(dRef("monthTd").firstChild);
		mon_nam            =  document.createTextNode(monName[this.aktMonth] + String.fromCharCode(160) +  this.aktYear);	
		mon_span           =  document.createElement("span");
		mon_span.className =  "scf";			
		mon_span.appendChild(mon_nam);				
		dRef("monthTd").appendChild(mon_span);
		
		// day names, first remove, after set
		// write this only the first time		
		if (!dRef("dayName0").firstChild)
			for(var k = 0; k < dName.length; k++) {			
			akt_day =  document.createTextNode(dName[k]);
			dRef("dayName" + k).appendChild(akt_day);			
		}
		
		// day fields
		for (var i = 0; i < 42; i++) {
		
			// delete past day fields if available
			if (dRef("day" + i).firstChild) dRef("day" + i).removeChild(dRef("day" + i).firstChild);	
		    
			// past or future
			temp     =  new Date(this.aktYear, this.aktMonth, cnt);	
			tempTime =  temp.getTime();		
			
			// past
			if (tempTime < this.todayTime) {
				day_content         =  document.createTextNode(cnt);
			// today or future
			} else {
				day_cnt             =  document.createTextNode(cnt);			
				day_content         =  document.createElement("a");
				day_content.setAttribute("href","#");
				day_content.onclick =  new Function("cal.wrForm(" + this.calAktForm + "," + cnt + ")");
				day_content.appendChild(day_cnt);			
			
				if (tempTime == this.todayTime)    day_content.className =  "scf";
				else if (tempTime == this.aktTime) day_content.className =  "gcf";
				else                               day_content.className =  "norm";				
			}	
			
			// add to the dom					
			if(firstDayId <= i && cnt <= lastDayDate) {
				dRef("day" + i).appendChild(day_content);
				cnt++;
			} else {
				// do a nonBreakingSpace a little more complicated because 
				// IE5.0 / Mac has problems with "String.fromCharCode(160)"
				var leer_gif =  document.createElement("img");	
				leer_gif.setAttribute("src","/common/all/gfx/leer.gif");
				leer_gif.setAttribute("width","1");	
				leer_gif.setAttribute("height","15");	
				dRef("day" + i).appendChild(leer_gif);
			}			
		}	
	// old browsers: making a string and write it into the document
	} else {
	
		var txt, aTag, table;	
		// month and daynames
		table =  '<table cellspacing="1" cellpadding="1" border="0"><tr>';
		table += '<td class="dayTd" width="18"><A class="norm" href="#" onclick="cal.pastMonth(); return false;">&lt;&lt;</A><\/td>';
		table += '<td class="dayTd" colspan="5" align="center" width="90"><span class="scf">' + monName[this.aktMonth]+ "&nbsp;" + this.aktYear + '<\/span><\/td>';
		table += '<td class="dayTd" align="right" width="18"><A class="norm" href="#" onclick="cal.nextMonth(); return false;">&gt;&gt;<\/A><\/td><\/tr><tr>';	
		for(var k in dName) table += '<td id="dayName' + k + '" class="dayName" align="right" width="18">' + dName[k] + '<\/td>';
		table += '<\/tr><tr>';

		for(var i = 0; i < 42; i++) {		
			// past or future
			var temp     =  new Date(this.aktYear, this.aktMonth, cnt);	
			var tempTime =  temp.getTime();
		
			// past
			if(tempTime < this.todayTime) {
				txt     =  cnt;  
			// today or future
			} else if(tempTime == this.todayTime) {
				aTag	=	'<a class="scf" href="#" onclick="cal.wrForm(' + this.calAktForm + ', ' + cnt + '); return false;">';
				txt		=	aTag + cnt + '<\/a>';
			} else if(tempTime == this.aktTime) {
				aTag	=	'<a class="gcf" href="#" onclick="cal.wrForm(' + this.calAktForm + ', ' + cnt + '); return false;">';
				txt		=	aTag + cnt + '<\/a>';
			} else {
				aTag	=	'<a class="norm" href="#" onclick="cal.wrForm(' + this.calAktForm + ', ' + cnt + '); return false;">';
				txt		=	aTag + cnt + '<\/a>';
			}	
		
			// writing (or making string for nc4x) day-fields						
			if(firstDayId <= i && cnt <= lastDayDate) {
				table += '<td' + ((i + 1)%7 == 0? ' class="sun"' : ' class="dayTd"') + ' align="right">' + txt + '<\/td>';
				table += (i + 1)%7 == 0? '<\/tr>' + (i < 41? '<tr>' : ''): '';
				cnt++;
			} else {
				table += '<td' + ((i + 1)%7 == 0? ' class="sun"' : ' class="dayTd"') + '>&nbsp;<\/td>';
				table += (i + 1)%7 == 0? '<\/tr>' + (i < 41? '<tr>' : ''): '';
			} 		
		}
		// finishing table-string and writing it
		table += '<tr><td class="dayTd" colspan="7" align="center"><a href="#"  class="norm" onclick="cal.dplCal(0)">' + footer + '<\/a><\/td><\/tr><\/table>';
		writeDiv (dRef('calendar'), table);
	}
}

function nextMonth() {

		if (this.aktMonth < 12) this.aktMonth += 1;
		if (this.aktMonth == 12) {
				this.aktYear += 1;
				this.aktMonth = 0;
		}
		this.writeCal();		
}

function pastMonth() {
		if (this.aktMonth > -1) this.aktMonth -= 1;
		if (this.aktMonth == -1) {
				this.aktYear -= 1;
				this.aktMonth = 11;
		}
		this.writeCal();
}

var arrivalSet = 0;;
function wrForm(index, id) {

	var depDate                 =  new Date(this.aktYear,this.aktMonth, parseInt(id) +add_day);
	var depDay                  =  depDate.getDate();
	var depMonth                =  depDate.getMonth();
	var depYear                 =  depDate.getYear();
	if (depYear < 1000) depYear += 1900;
	
	this.outForm[index].value   =  id + "." + (this.aktMonth + 1) + "." + this.aktYear;
	if(this.caladdForm) this.outForm[this.caladdForm ].value =  depDay + "." + (depMonth + 1) + "." + depYear;
//		if(this.caladdForm && !arrivalSet) this.outForm[this.caladdForm ].value =  depDay + "." + (depMonth + 1) + "." + depYear;
	arrivalSet = 1;
	this.dplCal(0);
}

function dplCal(stat) {
	dRefS("calendar").visibility =  stat? "visible" : "hidden";
}