function openCalendar(formElement){
    var url = "calendar.html?formname=form1&amp;formelement=" + formElement;
    var calendarWindow = window.open(url, "calendar", "toolbar=no,left=700,top=400,width=200,height=150,status=no,scrollbars=no,resize=no,menubar=no");
    calendarWindow.focus();
}

function formatDate(date){
    var day = date.slice(0, 2);
    var month = date.slice(3, 5);
    var year = date.slice(6, 10);
    var newDate = month + '/' + day + '/' + year;
    document.getElementById('DateIn').value = newDate;
}

function getCurrentDate(){
    var today = new Date();
    var currentDay = today.getDate();
    var currentMonth = today.getMonth() + 1;
    var currentYear = today.getFullYear();
    
    if (currentDay < 10) {
        currentDay = '0' + currentDay;
    }
    if (currentMonth < 10) {
        currentMonth = '0' + currentMonth;
    }
    
    var currentDate = currentDay + '.' + currentMonth + '.' + currentYear;
    var currentDateEnglish = currentMonth + '/' + currentDay + '/' + currentYear;
    document.getElementById('date_german').value = currentDate;
    document.getElementById('DateIn').value = currentDateEnglish;
}

function jumpToUrl(URL){
    var parameters = window.location.search;
    if (parameters != "") {
        var month = parameters.match(/\d\d\d\d\d\d\d\d/);
        var view = parameters.match(/week|day|month/);
        if (view == 'week' || view == 'day' || view == 'month') {
            var page = month != null ? URL + '&tx_cal_controller[getdate]=' + month + '&tx_cal_controller[view]=' + view : URL + '&tx_cal_controller[view]=month';
            window.open(page);
        }
    }
    else {
        var page = URL + '&tx_cal_controller[view]=month';
        window.open(page);
    }
}


function changeMonth(month) {
	var url = window.location.search;
	if(url.match(/\d\d\d\d\d\d\d\d/)) {
		url = url.replace(/\d\d\d\d\d\d\d\d/, month);
	}
	else {
		url += '&tx_cal_controller[getdate]='+month;
	}
	
	if(url.search(/\?/) == -1) {
		url = url.replace('&', 'index.php?');
	}
	
	var view = url.match(/week|day|month/);
	url = url.replace(/week|day|month/, 'month');
	
	urlParts = url.split('?');
	popUpUrl = 'http://www.schlossfuschlresort.at/hotels/index.php?id=253&'+urlParts[1];
	
	window.location.href = url;
	
	if (!url.match(/category/)) {
		window.open(popUpUrl+'&tx_cal_controller[category][0]=5');
	}
	else {
		window.open(popUpUrl);
	}
}

function change_url() {
	new_category = document.getElementsByName("tx_cal_controller[category][]")[0].value;
	old_url = window.location.href;
	category_position = old_url.search(/category/);
	if(category_position != -1) {
		if(old_url[category_position-19] == "?") {
			category_position = category_position + 13;
			old_category = old_url.substring(category_position, category_position+2);
			if(old_category[1] == "&") {
				old_category = old_url.substring(category_position, category_position+1);
			}
			urlParts = old_url.split("?tx_cal_controller[category][0]="+old_category);
			if(urlParts[1][0] != '&') {
				charToAdd = '&';
				urlParts[1] = charToAdd.concat(urlParts[1]);
			}
			new_url = urlParts[0]+"?tx_cal_controller[category][0]="+new_category+urlParts[1];

			new_urlParts = new_url.split('?');
			popUpUrl = 'http://www.schlossfuschlresort.at/hotels/index.php?id=253&'+new_urlParts[1];

			window.open(popUpUrl);

			window.location.href = new_url;
		} else {
			category_position = category_position + 13;
			old_category = old_url.substring(category_position, category_position+2);
			if(old_category[1] == "&") {
				old_category = old_url.substring(category_position, category_position+1);
			}
			urlParts = old_url.split("&tx_cal_controller[category][0]="+old_category);
			if(urlParts[1][0] != '&') {
				charToAdd = '&';
				urlParts[1] = charToAdd.concat(urlParts[1]);
			}
			new_url = urlParts[0]+"&tx_cal_controller[category][0]="+new_category+urlParts[1];

			new_urlParts = new_url.split('?');
			popUpUrl = 'http://www.schlossfuschlresort.at/hotels/index.php?id=253&'+new_urlParts[1];

			window.open(popUpUrl);

			window.location.href = new_url;
		}
	} else if(category_position == -1) {
		if(old_url.indexOf("?") == -1) {
			new_url = old_url+"?tx_cal_controller[category][0]="+new_category;
		} else {
			new_url = old_url+"&tx_cal_controller[category][0]="+new_category;
		}

		new_urlParts = new_url.split('?');
		popUpUrl = 'http://www.schlossfuschlresort.at/hotels/index.php?id=253&'+new_urlParts[1];

		window.open(popUpUrl);

		window.location.href = new_url;
	}
}