// JavaScript Document
/*
 * PHOBS - online booking system (C)2006.
 */
// Default vars
var errorMessageDate = 'Please select correct arrival date to proceede.';
var errorMessageHotel = 'Please select desired accommodation from dropdown menu.';

// Check
function checkReservation() {
	var forma = document.forms['bookingForm'];
	var myHotels = forma.elements['hotel'];
	// Set today
	var todayDate = new Date();
	var todayDan = parseInt(todayDate.getDate());
	var todayMjesec = parseInt(todayDate.getMonth()) + 1;
	var todayGodina = parseInt(todayDate.getFullYear());
	if (todayGodina<999) {todayGodina += 1900;}
	todayMjesec = todayMjesec.toString();
	if (todayMjesec.length < 2) {
		todayMjesec = '0' + todayMjesec;
	}
	var todayGodinaMjesec = parseInt(todayGodina.toString() + todayMjesec);

	var selectedDan = parseInt(forma.elements['check_in_day'].value);
	var selectedGodinaMjesec = forma.elements['check_in_year_month'].value;
	selectedGodinaMjesec = selectedGodinaMjesec.split('-');
	var selectedYear = selectedGodinaMjesec[0].toString();
	var selectedMjesec = selectedGodinaMjesec[1].toString();
	if (selectedMjesec.length < 2) {
		selectedMjesec = '0' + selectedMjesec;
	}
	selectedGodinaMjesec = parseInt(selectedYear + selectedMjesec);
	
	if (
	    (selectedDan <= todayDan && selectedGodinaMjesec <= todayGodinaMjesec)
	    ||
	    (selectedGodinaMjesec < todayGodinaMjesec)
	) {
		alert(errorMessageDate);
	} else {
		// Check hotel
		if (myHotels.options) {
			if (myHotels.options[myHotels.selectedIndex].value == '-') {
				alert(errorMessageHotel);
			} else {
				// SUBMIT
				openBooking();
			}
		} else {
			openBooking();
		}
	}

	return false;
}
// Open booking
function openBooking() {
	var forma = document.forms['bookingForm'];
	
	var w = 800, h = 600;
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings='height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes,toolbar=no,location=no,status=yes,menubar=no,resizable=yes,dependent=no';
	var win = window.open('http://secure.phobs.net/booking.php?company_id=f588960df50fc10ac58219387ad5d17f', 'phobsBooking', settings);
	if(parseInt(navigator.appVersion) >= 4){win.window.focus();}
	
	forma.target = 'phobsBooking';
	forma.submit();
	
	if (forma.elements['partners_access']) {forma.elements['partners_access'].value = '';}
	return false;
}
// View/cancel
function viewCancelBooking(myLink) {
	var forma = document.forms['bookingForm'];
	forma.elements['view_cancel'].value = '1';
	openBooking();
	forma.elements['view_cancel'].value = '';
	return false;
}
/* 
 * Kalendar
 */
function openKalendar(myTitle, myLang) {
	
	var myIframe = document.getElementById('kalendarFrame');
	var forma = document.forms['bookingForm'];
	if (myIframe && forma) {
		
		var dan = forma.elements['check_in_day'].value;
		var godinaMjesec = new Array();
		godinaMjesec = forma.elements['check_in_year_month'].value.split('-');
		if (dan < 10) {
			dan = '0' + dan.toString();
		}
		if (parseInt(godinaMjesec[1]) < 10) {
			godinaMjesec[1] = '0' + godinaMjesec[1].toString();
		}
		var datum = godinaMjesec[0].toString() + '-' + godinaMjesec[1] + '-' + dan;
		
		var myPage = 'kalendar.php?date=' + datum + '&naslov=' + myTitle + '&akcija=check-in';
		
		myIframe.src = myPage;
		myIframe.style.display = 'block';
		
	}
	return false;
}
function updateKalendar(day, month, year, akcija) {
	var myIframe = document.getElementById('kalendarFrame');
	var forma = document.forms['bookingForm'];
	// Change
	if (myIframe && forma) {
		var checkInDay = forma.elements['check_in_day'];
		var checkInYearMonth = forma.elements['check_in_year_month'];
		
		for(i=0; i<checkInDay.options.length; i++){
			if(parseInt(checkInDay.options[i].value) == parseInt(day)){
				checkInDay.options[i].selected = true;
			}
		}
		
		var godinaMjesec = parseInt(year).toString() + '-' + parseInt(month).toString();
		for(i=0; i<checkInYearMonth.options.length; i++){
			if(checkInYearMonth.options[i].value == godinaMjesec){
				checkInYearMonth.options[i].selected = true;
			}
		}

		myIframe.style.display = 'none';
	}
	return false;
}