function scrollDown(n, i) {
	if (i == null) i = 0;
	self.scrollBy(0, 10);
	if (n > 10) self.setTimeout("scrollDown(" + (n - 10) + ")", i);
}
function submitForm(x) {
	document.mainForm.submit();
}
var infoWin;
function showInfo(url) {
	showSizableInfo(url, 660, 580);
}

function showSizableInfo(url, width, height) {
if (infoWin && navigator.userAgent.indexOf("MSIE") != -1) infoWin.close();
	infoWin = window.open(url+'&popup=true','tui_details','width='+width+',height='+height+',resizable=yes,scrollbars=yes');
	if (navigator.userAgent.indexOf("MSIE") == -1 ) infoWin.focus();
}

function setMainName() {
   window.name = "tui_main";
}
day = new Array("Mo","Di","Mi","Do","Fr","Sa","So");
month = new Array("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");
chosen = new Date();
start = new Date();
field = new Object();
fday   = new Object();
fmonth = new Object();

window.onError=handle_error;
function handle_error()
{
	msg = "\nEs ist ein JavaScript-Fehler aufgetreten.";
	alert(msg);
	return true;
}
function spawnCal(myField) {
	field = myField;
	ds = myField.value;
	start = new Date(ds);
	if (isNaN(start)) {
		start = new Date();
		if (ds.length > 0) {
			start.setDate(ds.substring(0, ds.indexOf('.')));
			ds = ds.substring(ds.indexOf('.') + 1);
			start.setMonth(ds.substring(0, ds.indexOf('.')) - 1);
			ds = ds.substring(ds.indexOf('.') + 1);
			start.setFullYear(ds.substring(0, ds.length));
		}
	}
	if (isNaN(start))
		start = new Date();
	calWin = window.open("", "calWindow", "width=310,height=400");
	calDoc = calWin.document;
	chosen = start;
	updateCal(start, field);
	calWin.focus();
}

function spawnCal2(dayField, monthField, months) {

	// Array vacations ist in externer Datei definiert
	//fieldVacations = vacations;
	field = null;
	fday = dayField;

	// 12 is default if month undefined or zero
	if ((! isNaN(months)) && (months > 0)) {
		numberOfMonths = months;
	}
	else {
		numberOfMonths = 12;
	}

	fmonth = monthField;
	ds  = fday.options[fday.selectedIndex].value + "." + fmonth.options[fmonth.selectedIndex].value;
	start = new Date(ds);
	if (isNaN(start)) {
		start = new Date();
		if (ds.length > 0) {
			start.setDate(ds.substring(0, ds.indexOf('.')));
			ds = ds.substring(ds.indexOf('.') + 1);
			start.setMonth(ds.substring(0, ds.indexOf('.')) - 1);
			ds = ds.substring(ds.indexOf('.') + 1);
			start.setFullYear(ds.substring(0, ds.length));
		}
	}
	if (isNaN(start))
		start = new Date();
	// Initialwert fuer ausgewaehltes Bundesland auf -1 (kein Bundesland ausgewaehlt)
	selectedCountry = -1;
	// Kalenderfenster oeffnen
	calWin = window.open("", "calWindow", "width=305,height=240");
	calDoc = calWin.document;
	chosen = start;
	updateCal(start, field);
	calWin.focus();
}

function chooseDate(doc) {
	chosen.setTime(doc.calForm.dateBox.options[doc.calForm.dateBox.selectedIndex].value);
	//selectedCountry = doc.calForm.country.options[doc.calForm.country.selectedIndex].value;
	//vacationArray = new Array();
	if (selectedCountry > -1) {
		// Aufbereitung der Ferientage fuer das ausgewaehlte Bundesland
		for (var i = 1; i < fieldVacations[selectedCountry].length; i++) {
			vacationDate = new String(fieldVacations[selectedCountry][i]);
			// Tag ermitteln
			var vacationDay = vacationDate.substring(0, vacationDate.indexOf('.'));
			// Monat ermitteln
			var vacationMonth = vacationDate.substring(vacationDate.indexOf('.') + 1, vacationDate.lastIndexOf('.'));
			// Jahr ermitteln
			var vacationYear = vacationDate.substring(vacationDate.lastIndexOf('.') + 1);
			// wenn Jahreszahl zweistellig, dann erweitere auf 20yy
			if (vacationYear < 100) {
				vacationYear = "20" + vacationYear;
			}
			// Datum erstellen
			vacationArray[i - 1] = new Date(vacationYear, vacationMonth - 1, vacationDay);
		}
	}
	updateCal(chosen, field);
}

function dateString(day, win) {
	chosen.setDate(day);
	d = chosen.getDate();
	d = d < 10 ? '0' + d : d;
	m = chosen.getMonth() + 1;
	m = m < 10 ? '0' + m : m;
	if (field != null)
	{
		field.value = d + "." + m + "." + chosen.getFullYear();
		win.close();
		return;
	}
	if (fday != null)
	{
		fdaycnt = 0;
		foundfield = 0;
 		osday = -1;
		ffday = -1;
		osmonth = -1;
		ffmonth = -1;

		for (fdaycnt=0;fdaycnt<fday.options.length;fdaycnt++)
		{
			opt = fday.options[fdaycnt];
			if (opt.selected == true) {osday = fdaycnt;}
			if (opt.value == chosen.getDate()) {ffday = fdaycnt;}
		}
		fmonthcnt = 0;
		for (fmonthcnt=0;fmonthcnt<fmonth.options.length;fmonthcnt++)
		{
			opt = fmonth.options[fmonthcnt];
			vall = "" + (chosen.getMonth() + 1)+"."+chosen.getFullYear();
			if (opt.selected == true) {osmonth = fmonthcnt;}
			if (opt.value.indexOf(vall)==0) {ffmonth = fmonthcnt;}
		}

		if ((ffday != -1) && (ffmonth != -1))
		{
			fday.options[osday].selected = false;
			fday.options[ffday].selected = true;
			fmonth.options[osmonth].selected = false;
			fmonth.options[ffmonth].selected = true;
		}
		win.close();
		return;
	}
	win.close();
}

function updateCal(chosen, field) {

	calDoc.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional>');
	calDoc.writeln('<html>');
	calDoc.writeln('<head>');
	calDoc.writeln('	<title>Kalender</title>');
	calDoc.writeln('	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
	calDoc.writeln('	<link rel="stylesheet" href="includes/kalender.css" type="text/css">');
	calDoc.writeln('</head>');
	calDoc.writeln('<body bgcolor="#FFFFFF" text="#000000">');
	calDoc.writeln('	<form name="calForm">');
	calDoc.writeln('								<table width="260" border="0" cellspacing="0" cellpadding="1">');
	calDoc.writeln('									<tr>');
	calDoc.writeln('										<td class="formularRahmen">');
	calDoc.writeln('											<table width="280" border="0" cellpadding="0" cellspacing="0" class="formularRahmen">');
  calDoc.writeln('												<tr>');
	calDoc.writeln('													<td height="28" class="kastengrau">&nbsp;&nbsp; <select name="dateBox" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif" onchange="opener.chooseDate(self.document)">');

	today = new Date();
	optionDate = new Date();
	// show selectbox with month and year
	for (i = 0; i < numberOfMonths; i++) {
		optionDate.setMonth((today.getMonth() + i) % 12);
		optionDate.setYear(today.getFullYear() + (today.getMonth() + i) / 12);
		calDoc.writeln('<!-- ' + optionDate.getMonth() + '-->');
		var optionDateString = "" + month[optionDate.getMonth()] + " " + optionDate.getFullYear();
		calDoc.write('<option value = "' + optionDate.getTime() + '"');
		if ((optionDate.getMonth() == chosen.getMonth()) && (optionDate.getFullYear() == chosen.getFullYear())) {
			calDoc.write(' selected');
		}
		calDoc.writeln('>' + optionDateString + '</option>');
	}
	calDoc.writeln('</select>');

	calDoc.writeln('													</td>');
	calDoc.writeln('												</tr>');

	calDoc.writeln('												<tr>');
	calDoc.writeln('												<td align="center" class="formularBg">');
	calDoc.writeln('													<table width="240" border="0" cellpadding="0" cellspacing="0" class="formularBg">');
	calDoc.writeln('														<tr height="20">');

	for (i=0; i<day.length; i++) {
		calDoc.writeln('														<td height="22" align="center" class="headlineblack">' + day[i] + '</td>');
	}
	calDoc.writeln('														</tr>');

	calDoc.writeln('														<tr height="28">');

	iter = chosen;
	iter.setDate(1);
	diff = iter.getDay();
	if (diff > 0)
		iter = new Date(iter.getTime() - 86400000 * (diff - 1));
	else
		iter = new Date(iter.getTime() - 86400000 * (diff + 6));
	// erstelle Tabelle mit den Tagen
	for (i=0; i<42; i++) {
		bgc = "formularBg";
		// checken ob Bundesland ausgewaehlt
		
		/*
		if (selectedCountry > -1) {
			// fuer alle Ferienintervalle pruefen
			for (var j = 0; j < vacationArray.length; j = j + 2) {
				if ((iter.getTime() >= vacationArray[j].getTime()) && (iter.getTime() < (vacationArray[j+1].getTime() + 86400000))) {
					bgc = "grauerBg";
				}
			}
		}*/
		// wenn aktuelles Datum, Hintergrund aendern
		//if (iter.getDate() == today.getDate() && iter.getMonth() == today.getMonth() && iter.getFullYear() == today.getFullYear())
		//	bgc = "#ff9999";

		calDoc.writeln('<td height="22" align="center" class="' + bgc + '">');

		if (iter.getMonth() == chosen.getMonth()) {
		 	calDoc.writeln('<a href="javascript:opener.dateString(' + iter.getDate() + ', self)">');
			//if (iter.getDay() == 0)
			//	calDoc.writeln('<span class="text3">')
			//if (iter.getDay() == 0)
			//	calDoc.writeln('</font>')
		}
		//else {
		//	calDoc.writeln('<font color="#aaaaaa">');
		//}

		calDoc.writeln(iter.getDate());
		if (iter.getMonth() == chosen.getMonth())
		 	calDoc.writeln('</a>');
		//else
		// calDoc.writeln('</span>');
		calDoc.write('</td>');

		if (i % 7 == 6)
			calDoc.writeln('</tr><tr>');
		iter.setDate(iter.getDate() + 1);
	}

	calDoc.writeln('														</tr>');
	calDoc.writeln('													</table>');
	calDoc.writeln('													<table width="260" border="0" cellspacing="0" cellpadding="0">');
	calDoc.writeln('														<tr>');
	calDoc.writeln('															<td height="30"><font color="#000000">Bitte klicken Sie auf das gew&uuml;nschte Datum.</font></td>');
	calDoc.writeln('														</tr>');
	calDoc.writeln('														<tr>');

	// Select-Box fuer Bundeslaender erstellen
//
	//calDoc.writeln('															<td height="30">&nbsp;');
	//calDoc.writeln('<select name="country" onChange="opener.chooseDate(self.document)" style="font-size: 12px; font-family: Arial, Helvetica, sans-serif" tabindex="1">');

	//calDoc.writeln('<option value = "-1"> W&auml;hlen Sie ein Bundesland');
	//for (var i = 0; i < fieldVacations.length; i++) {
		//calDoc.write('<option ');
		// ausgewaehltes Bundesland soll bei Neuaufbau selektiert werden
		//if (i == selectedCountry) {
		//	calDoc.write('selected ');
		//}
		//calDoc.writeln('value = "' + i + '"> ' + fieldVacations[i][0]);
	//}

	//calDoc.writeln('																</select>');//
	calDoc.writeln(' 															</td>');
	calDoc.writeln('														</tr>');
	calDoc.writeln('													</table>');
	calDoc.writeln('												</td>');
	calDoc.writeln('											</tr>');
	calDoc.writeln('										</table>');
	calDoc.writeln('									</td>');
	calDoc.writeln('								</tr>');
	calDoc.writeln('							</table>');
	calDoc.writeln('	</form>');
	calDoc.writeln('</body>');
	calDoc.writeln('</html>');
	calDoc.close();
};

function showWaitPage() {
    var DHTML = (document.getElementById || document.all || document.layers);
    if (!DHTML) return;

    if (document.getElementById) {
      document.getElementById("standard").style.visibility = "hidden";
      document.getElementById("wait").style.visibility = "visible";
    } else if (document.all) {
      document.all["standard"].style.visibility = "hidden";
      document.all["wait"].style.visibility = "visible";
    } else if (document.layers) {
    	// Netscape 4.7x
			// do nothing (leave the wait page invisible because of some problems:
			//             layers and stylesheets do not seem to work together correctly)
    }
    self.scrollTo(0, 0);
}

function checkSubmitted() {
	if (formSubmitted == false) {
		formSubmitted = true;
		return true;
	} else {
		return false;
	}
}

