var iframeContainer;

function bookingSubmit(form, link_url) {
	var date_from, date_to;
	var params = $H();
	
	date_from = $('date_from').getValue();
	
	date_from_splitted = date_from.split(".");

	params.set('ARR_DAY', date_from_splitted[0]);
	params.set('ARR_MONTH', (date_from_splitted[1].replace(/^[0]+/g,"")-1));
	params.set('ARR_YEAR', date_from_splitted[2]);
	params.set('NIGHTS', $('nights').getValue());
	params.set('ADULTS', $('adults').getValue());
	params.set('UNITS', 1);

			
	if (typeof(link_url) === 'undefined') {	
		url = $(form).readAttribute('action') + '&' +params.toQueryString();
	}
	else {
		url = link_url+$('current_language').value;
	}
	
	if($('iframeTomas')) {
		iframe = $('iframeTomas');
		iframe.writeAttribute({ 'src': url });
		showIframe();
	}
	else {
		
		var height = document.viewport.getHeight() - 200;
	
		iframeContainer = new Element('div', { 'id': 'iframeTomasContainer', 'style': 'height: ' + height + 'px;', 'style': 'width: 600px;' });
		iframe = new Element('iframe', { 'src': url, 'id': 'iframeTomas', 'class': 'tomas', 'style': 'height: ' + height + 'px;', 'marginheight': '0', 'marginwidth': '0', 'frameborder': '0', 'scrolling': 'auto' });
		closeButton = '<a class="iframeTomasCloseButton" href="javascript:void(0);" onclick="hideIframe()"><img src="templates/tannenhof/img/close.png" alt="Close" /></a>';
		
		iframeContainer.insert(closeButton);
		iframeContainer.insert(iframe);
		
		showIframe();
	}
}

function showIframe() {
	// new instance of prototype-overlay
	popup = new Overlay();
	popup.show(iframeContainer, undefined, { 'afterhide': removeIframe });
	
	// lookfs if the esc key is pressed and closes the current Popup if yes.
	window.onkeydown = function(e) {
		if (!e) {
			e = window.event;
		}
		if (e.keyCode == 27) {
			hideIframe();
		}
	}
}

function hideIframe() {
	popup.hide(iframeContainer);
}

function removeIframe() {
	iframeContainer.remove();
}

function getDateTo(date_from, nights) {
	var parts = date_from.split('.');
	var day, month, year;
	
	day = parseInt(parts[0]);
	month = parseInt(parts[1]);
	year = parseInt(parts[2]);
	
	nights = parseInt(nights);
	
	var date_to = new Date(year, month-1, day);
	var timestamp = parseInt(date_to.getTime());
	
	var date_to = new Date((timestamp + (nights * 86400 * 1000)));
	
	day = date_to.getDate();
	month = parseInt(date_to.getMonth())+1;
	year = date_to.getFullYear();
	
	return formatNumber(day) + "." + formatNumber(month) + "." + year;
}

function formatNumber(number) {
	if (number < 10) {
		number = "0" + number;
	}
	
	return number;
}
