/*
 * window: Window to use for displaying the calendar
 * mode: 0: Freetext complete date; 1: Combobox complete date; 2: Comboboxes Date, Month/Year (Departure Date)
 *       3: Comboboxes Date, Month/Year (Return Date)
 * formname: Name of form to reflect choices in the calendar
 * formelements: Array of names of form elements to reflect choices in the calendar
 *              if mode 0: one element (input field)
 *              if mode 1: one element (combobox)
 *              if mode 2 or 3: two elements (comboboxes in sequence date, month/year;
 *                           optional other comboboxes for calendar validation in sequence date, month, year)
 * originalChosen: date which was chosen on the original web page
 * chosen: date which defines the month to display
 * numberOfMonth: Number of months to display (starting with the current month)
 * validateCalendar: Indicates whether a service method is to include after a date was selected.
 *                   The service method can influence the contents of other form elements.
 * selectedArea: Index of the area currently selected
 * pattern: display pattern
 * submit: Indicates whether to submit the calendar launching form after a date was selected in the calendar.
 * alwaysOnTop (optional):  Indicates if the popup should alwyas be on top. If alwaysOnTop is true
 *							onblur="self.focus();opener.blur();" is added to the body tag
 * chosen2: return date which defines the month to display
 * originalChosen2: return date which was chosen on the original web page
 */
function generateCal(window, mode, formname, formelements, originalChosen, chosen, numberOfMonths, validateCalendar, 
					 selectedArea, pattern, submit, alwaysOnTop, chosen2, originalChosen2) {
					 				 	
	if(!document.getElementById("calDocDiv")){				 				 	
		calDoc = document.createElement("div");			
	}else{
		calDoc = document.getElementById("calDocDiv");	
	}

	myX = getX(document.getElementsByName(formelements[0])[0]) - 120;
	myY = getY(document.getElementsByName(formelements[0])[0]) - 20;

	if ((myX < 0) || (myY < 0)) {
		var f = document.forms[formname];
		var fe = null;
		for (var i = 0;i < f.elements.length;i++) {
			if (f.elements[i].name == formelements[0]) {
				fe = f.elements[i];
				break;
			}
		}
		if (fe) {
			myX = getX(fe) - 100;
			myY = getY(fe) - 20;
		}
	}
	isMSIE6 = (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf('MSIE 6.0') != -1) ? true : false;
	
	if(isMSIE6){
		iframe = document.getElementById("calIframe");
		if(!iframe){
			iframe = document.createElement("iframe");
		}
		iframe.setAttribute("id", "calIframe");
		iframe.setAttribute("frameborder", "0");		
		iframe.style.visiblity = "hidden";
		iframe.style.zIndex = '4999';
		iframe.style.border = "0";
		iframe.style.position = 'absolute';
		iframe.style.top = myY + 'px';
		iframe.style.left = myX + 'px';		
		iframe.style.width = '180px'
		iframe.style.height = '440px';	
		calDoc.style.filter = "Shadow(color=#ff0000, direction=135)";	
		document.getElementsByTagName('body')[0].appendChild(iframe);					
		calDoc.style.background = "#fff";


		calDoc.style.padding = "0";		
		calDoc.style.border = "1px solid #c0c0c0";
	
	}		
	
	calDoc.setAttribute("id", "calDocDiv");	
	calDoc.style.position = 'absolute';
	calDoc.style.zIndex = '5000';
	calDoc.style.top = myY + 'px';
	calDoc.style.left = myX + 'px';
	calDoc.style.width = "180px";
	calDoc.style.height = "440px";
	calDoc.style.display = 'none';
	calDoc.style.margin = '15px';
	if(!isMSIE6){
		calDoc.style.padding = "5px 5px 5px 5px";	
	}
	
	calDoc.setAttribute("class", "calDocShadow");
	calDoc.setAttribute("className", "calDocShadow");	
	
	document.getElementsByTagName('body')[0].appendChild(calDoc);		

	
	var calContent = "";
	calDoc.writeln = function(code){
		calContent += code;
	}
	
	calDoc.write = function(code){
		calContent += code;
	}	
		
	// *********************************************************************************************************************
	// *********************************************************************************************************************
	// Calendar for the departure
	// *********************************************************************************************************************
	// *********************************************************************************************************************

	var selectedMonth;
	var selectedYear;

	var elements = formelements;
	
	// prepare holidays
	var vacationArray = new Array();

	if(document.cookie && selectedArea < 0){
		selectedArea = document.cookie;
		if(isNaN(selectedArea)){			
			parts = selectedArea.split(";");
			if(!isNaN(parts[0])){
				selectedArea = parts[0];
			}
		}
	}
	
	if (selectedArea > - 1) {
		
		if(navigator.cookieEnabled){
			document.cookie = selectedArea;
		}
				
		// Prepare holidays for the selected area
		for (var i = 1; i < vacations[selectedArea].length; i++) {
			var vacationDate = new String(vacations[selectedArea][i]);
			// Compute day
			var vacationDay = vacationDate.substring(0, vacationDate.indexOf('.'));
			// Compute month
			var vacationMonth = vacationDate.substring(vacationDate.indexOf('.') + 1, vacationDate.lastIndexOf('.'));
			// Compute year
			var vacationYear = vacationDate.substring(vacationDate.lastIndexOf('.') + 1);
			// if year has only 2 characters: prefix with "20"
			if (vacationYear < 100) {
	            vacationYear = "20" + vacationYear;
			}
			// Create date
			vacationArray[i - 1] = new Date(vacationYear, vacationMonth - 1, vacationDay);
		}
		
	}

	var today = new Date();
	
	myArrivalDay = originalChosen2.getDate();
	myArrivalMonth = originalChosen2.getMonth();
	myArrivalYear = originalChosen2.getFullYear();
	
	myDepartureDay = originalChosen.getDate();
	myDepartureMonth = originalChosen.getMonth();
	myDepartureYear = originalChosen.getFullYear();

	calDoc.writeln('<div id="genCalCloseCal" onclick="hideCal()" style="float:right;margin-right:20px;">Fenster schliessen</div>');

	calDoc.writeln('<form name="calForm" style="float: left; width: 175px;">');
	
	calDoc.writeln('<div id="genCalPopup" class="departure">');
	calDoc.writeln('<div id="genCalPopupHeadline">' + calendarTitle + '</div>');
	calDoc.writeln('<div id="genCalPopupMonthSelCellHeader">' + calendarDeparture + '</div>');
	// show selectbox with month and year
	calDoc.writeln('<div id="genCalPopupMonthSelCell"><div id="genCalPopupMonthPrev" title="' + calendarMonthPrevTitle + '" onclick="select = document.getElementById(\'genCalPopupMonthSel\'); if (select.selectedIndex > 0) { select.selectedIndex= select.selectedIndex-1;');
	calDoc.writeln('calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());};');
	calDoc.writeln('"></div>');
	calDoc.writeln('<select name="monthBox" onchange="');
	calDoc.writeln('calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());');	
	calDoc.writeln('" id="genCalPopupMonthSel" class="genCalPopupSel">');
	
	var optionDate = new Date();
	// preset day to first day because of overrun of last days on some months:
	// starting with (today) 31.10 and after adding 1 month we get 31.11 (non-existant date), therefore
	// we get not november as month but dezember (bugs 42339, 45402)!!!
	optionDate.setDate(1);
	for (i = 0; i < numberOfMonths; i++) {
		selectedMonth = (today.getMonth() + i) % 12;
		optionDate.setMonth(selectedMonth);
		selectedYear = today.getFullYear() + (today.getMonth() + i)/12;
		optionDate.setYear(selectedYear);
		var optionDateString = "" + calendarMonths[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('<div id="genCalPopupMonthNext" title="' + calendarMonthNextTitle + '" onclick="select = document.getElementById(\'genCalPopupMonthSel\'); if (select.selectedIndex < select.length-1) { select.selectedIndex=select.selectedIndex+1; ');
	calDoc.writeln('calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());};');
	calDoc.writeln('"></div></div>');


	calDoc.writeln('<div id="genCalPopupCalendar">');
	
	calDoc.writeln('<div id="genCalPopupDayNames">');
	for (i = 0; i < calendarDays.length; i++) {
		calDoc.writeln('<div class="genCalPopupDayName">' + calendarDays[i] + '</div>');
	}
	calDoc.writeln('</div>');

	calDoc.writeln('<div id="genCalPopupDays"><div class="genCalPopupWeek">');

	var iter = new Date(chosen.getFullYear(), chosen.getMonth(), 1);
	
	var diff = iter.getDay();

	// Weeks start with monday
	if (diff > 0)
		iter = new Date(iter.getTime() - 86400000 * (diff - 1));
	else
		iter = new Date(iter.getTime() - 86400000 *(diff + 6));

	// create days
	for (i = 0; i < 42; i++) {
		var bgc = "genCalPopupDay";
		
		// check if special vacations are selected
		if (selectedArea > - 1) {
			// check all intervals
			for (var j = 0; j < vacationArray.length; j = j + 2) {
				if ((iter.getTime() >= vacationArray[j].getTime())
						&& (iter.getTime() < (vacationArray[j + 1].getTime() + 86400000))) {
				   bgc = "genCalPopupVacationDay";
				}
			}
		}

		var isChosen = false;
		
		if ((originalChosen != null)
			&& (iter.getFullYear() == originalChosen.getFullYear())
			&& (iter.getMonth() == originalChosen.getMonth())
			&& (iter.getDate() == originalChosen.getDate())) {
			isChosen = true;
		}
		
		var isToday = false;
		if ((iter.getFullYear() == today.getFullYear())
			&& (iter.getMonth() == today.getMonth())
			&& (iter.getDate() == today.getDate())) {
			isToday = true;
		}
		
		var id = '';
		if (isToday && isChosen) {
			id = ' id="genCalPopupDayTodayChosen"';
		} else if (isToday) {
			id = ' id="genCalPopupDayToday"';
		} else if (isChosen) {
			id = ' id="genCalPopupDayChosen"';
		}
		
		calDoc.writeln('<div class="' + bgc + '"' + id + '>');
		
		var call = null;
		var isInThePast = false;
		
		if ((iter.getFullYear() == today.getFullYear())
			&& (iter.getMonth() == today.getMonth())
			&& (iter.getDate() < today.getDate())) {
			isInThePast = true;
		}
		
		if (!isInThePast) {
			if (iter.getMonth() == chosen.getMonth()) {
				var element = formelements[0];
				call = 'setDepartureDate(' + iter.getDate() + ',' + iter.getMonth() + ',' + iter.getFullYear() + ', this.parentNode); return false;';
			}
		}
		
		if (call != null) {
			calDoc.write('<a class="genCalPopupDayLink" href="#" onClick="' + call);
			if (submit) {
				calDoc.write('document.forms[\'' + formname + '\'].submit();');
			}
			calDoc.write('">');
		}
		
		calDoc.write(iter.getDate());
		
		if (call != null) {
			calDoc.writeln('</a>');
		}
		
		calDoc.write('</div>');
		
		if (i % 7 == 6) {
			calDoc.writeln('</div><div class="genCalPopupWeek">');
		}
		iter.setDate(iter.getDate() + 1);
	}

	calDoc.writeln('</div></div></div>');
	calDoc.writeln('<div id="genCalPopupInstruction">' + calendarInstruction + '.</div>');
	calDoc.writeln('</div>');
	calDoc.writeln('</form>');


	// *********************************************************************************************************************
	// *********************************************************************************************************************
	// Calendar for the return
	// *********************************************************************************************************************
	// *********************************************************************************************************************
	var selectedMonth2;
	var selectedYear2;
	
	calDoc.writeln('<form name="calForm2" style="float: left; width: 175px;">');
	calDoc.writeln('<div id="genCalPopup">');
	calDoc.writeln('<div id="genCalPopupHeadline">' + calendarTitle + '</div>');
	calDoc.writeln('<div id="genCalPopupMonthSelCellHeader">' + calendarArrival + '</div>');
	// show selectbox with month and year
	calDoc.writeln('<div id="genCalPopupMonthSelCell"><div id="genCalPopupMonthPrev" title="' + calendarMonthPrevTitle + '" onclick="select = document.getElementById(\'genCalPopupMonthSel2\'); if (select.selectedIndex > 0){select.selectedIndex= select.selectedIndex-1;');
	calDoc.writeln('calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());};');	
	calDoc.writeln('"></div>');
	calDoc.writeln('<select name="monthBox2" onchange="');
	calDoc.writeln('calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());');	
	calDoc.writeln('" id="genCalPopupMonthSel2" class="genCalPopupSel">');
	
	var optionDate2 = new Date();
	// preset day to first day because of overrun of last days on some months:
	// starting with (today) 31.10 and after adding 1 month we get 31.11 (non-existant date), therefore
	// we get not november as month but dezember (bugs 42339, 45402)!!!
	optionDate2.setDate(1);
	for (i = 0; i < numberOfMonths; i++) {
		selectedMonth2 = (today.getMonth() + i) % 12;
		optionDate2.setMonth(selectedMonth2);
		selectedYear2 = today.getFullYear() + (today.getMonth() + i)/12;
		optionDate2.setYear(selectedYear2);
		var optionDateString = "" + calendarMonths[optionDate2.getMonth()] + " " + optionDate2.getFullYear();
		calDoc.write('<option value = "' + optionDate2.getTime() + '"');
		if ((optionDate2.getMonth() == chosen2.getMonth()) && (optionDate2.getFullYear() == chosen2.getFullYear())) {
			calDoc.write(' selected');
		}
		calDoc.writeln('>' + optionDateString + '</option>');
	}
	calDoc.writeln('</select>');
	
	calDoc.writeln('<div id="genCalPopupMonthNext" title="' + calendarMonthNextTitle + '" onclick="select = document.getElementById(\'genCalPopupMonthSel2\'); if (select.selectedIndex < select.length-1) { select.selectedIndex=select.selectedIndex+1; calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());}; ');	
	calDoc.writeln('"></div></div>');
	
	calDoc.writeln('<div id="genCalPopupCalendar2">');
	
	calDoc.writeln('<div id="genCalPopupDayNames">');
	for (i = 0; i < calendarDays.length; i++) {
		calDoc.writeln('<div class="genCalPopupDayName">' + calendarDays[i] + '</div>');
	}
	calDoc.writeln('</div>');

	calDoc.writeln('<div id="genCalPopupDays"><div class="genCalPopupWeek">');
	
	var iter = new Date(chosen2.getFullYear(), chosen2.getMonth(), 1);
	var diff = iter.getDay();
	
	// Weeks start with monday
	if (diff > 0)
		iter = new Date(iter.getTime() - 86400000 * (diff - 1));
	else
		iter = new Date(iter.getTime() - 86400000 *(diff + 6));

	// create days
	for (i = 0; i < 42; i++) {
		var bgc = "genCalPopupDay";
		
		// check if special vacations are selected
		if (selectedArea > - 1) {
			// check all intervals
			for (var j = 0; j < vacationArray.length; j = j + 2) {
				if ((iter.getTime() >= vacationArray[j].getTime())
						&& (iter.getTime() < (vacationArray[j + 1].getTime() + 86400000))) {
				   bgc = "genCalPopupVacationDay";
				}
			}
		}

		var isChosen = false;
		
		if ((originalChosen2 != null)
			&& (iter.getFullYear() == originalChosen2.getFullYear())
			&& (iter.getMonth() == originalChosen2.getMonth())
			&& (iter.getDate() == originalChosen2.getDate())) {
			isChosen = true;
		}
		
		var isToday = false;
		if ((iter.getFullYear() == today.getFullYear())
			&& (iter.getMonth() == today.getMonth())
			&& (iter.getDate() == today.getDate())) {
			isToday = true;
		}
		
		var id = '';
		if (isToday && isChosen) {
			id = ' id="genCalPopupDayTodayChosen2"';
		} else if (isToday) {
			id = ' id="genCalPopupDayToday"';
		} else if (isChosen) {
			id = ' id="genCalPopupDayChosen2"';
		}
		
		calDoc.writeln('<div class="' + bgc + '"' + id + '>');
		
		var call = null;
		var isInThePast = false;
		
		if ((iter.getFullYear() == today.getFullYear())
			&& (iter.getMonth() == today.getMonth())
			&& (iter.getDate() < today.getDate())) {
			isInThePast = true;
		}
		
		if (!isInThePast) {
			if (iter.getMonth() == chosen2.getMonth()) {
				var element = formelements[0];
				call = 'setArrivalDate(' + iter.getDate() + ',' + iter.getMonth() + ',' + iter.getFullYear() + ', this.parentNode); return false;';
			}
		}
		
		if (call != null) {
			calDoc.write('<a class="genCalPopupDayLink" href="#" onClick="' + call);
			if (submit) {
				calDoc.write('document.forms[\'' + formname + '\'].submit();');
			}
			calDoc.write('">');
		}
		
		calDoc.write(iter.getDate());
		
		if (call != null) {
			calDoc.writeln('</a>');
		}
		
		calDoc.write('</div>');
		
		if (i % 7 == 6) {
			calDoc.writeln('</div><div class="genCalPopupWeek">');
		}
		iter.setDate(iter.getDate() + 1);
	}

	calDoc.writeln('</div></div></div>');
	calDoc.writeln('<div id="genCalPopupInstruction">' + calendarInstruction + '.</div>');
	calDoc.writeln('</div>');
	
	calDoc.writeln('<div id="genCalPopupVacationCell">');
	calDoc.writeln('<div id="genCalPopupVacationLabel">' + calendarHolidays + ':</div>');
	// special areas
	calDoc.writeln('<select name="areas" style="clear:both" onChange="');
	calDoc.writeln('calendarChooseMonth(self,' + mode + ',\'' + formname  + '\', \'' + formelements + '\' , new Date(' + myDepartureYear + ',' + myDepartureMonth + ',' + myDepartureDay + ',0,0,0,0).getTime(),document.calForm.monthBox.options[document.calForm.monthBox.selectedIndex].value,' + numberOfMonths + ',' + validateCalendar + ',document.calForm2.areas.options[document.calForm2.areas.selectedIndex].value,\'' + pattern + '\',' + submit + ', document.calForm2.monthBox2.options[document.calForm2.monthBox2.selectedIndex].value, new Date(' + myArrivalYear + ',' + myArrivalMonth + ',' + myArrivalDay + ',0,0,0,0).getTime());');	
	calDoc.writeln('" id="genCalPopupStateSel" class="genCalPopupSel">');
	calDoc.writeln('<option value = "-1">' + calendarChooseArea);
	
	savedValue = "";
	if(navigator.cookieEnabled && document.cookie){
		parts = document.cookie.split(";");
		savedValue = parts[0];				
	}
	if(!savedValue){
		savedValue = selectedArea;
	}

	if(isNaN(savedValue)){
		parts = savedValue.split(";");
		savedValue = parts[0];
	}
	for (var i = 0; i < vacations.length; i++) {
		calDoc.write('<option ');
		// selected area must be reselected upon regeneration
		if (i == savedValue) {
			calDoc.write('selected ');
		}
		calDoc.writeln('value = "' + i + '"> ' + vacations[i][0]);
	}
	calDoc.writeln('</select>');
	calDoc.writeln('<input type="button" name="SelectValues" id="dateButton" style="clear:both;margin-top:5px" value="' + calendarSelectValues + '" onClick="javascript:setDateToFreeTextField(\'' + formname + '\',\'' + formelements[1] + '\',myArrivalDay , myArrivalMonth, myArrivalYear,\'' + pattern + '\');setDateToFreeTextField(\'' + formname + '\',\'' + formelements[0] + '\',myDepartureDay , myDepartureMonth, myDepartureYear,\'' + pattern + '\'); hideCal()">');
	calDoc.writeln('</div>');
	calDoc.writeln('</form>');

	
	calDoc.innerHTML = calContent;
	calDoc.style.display = 'block';
};

