/*
 * PHOBS - online booking system (C)2006.
 */
 // Default selection
 var defaultDestination, defaultHotel;
 
// Default vars
var defaultHotelSelect = 'All accommodation:';
var errorMessageDate = 'Please select correct arrival date to proceede.';
var errorMessageHotel = 'Please select desired accommodation from dropdown menu.';

var destinationDefinition = new Array();
var destinationArray = new Array();

/*
 * DEFAULT LIST
 *
 */
// Destinations...
destinationDefinition[0] = new Array('53231', 'Plitvička jezera');

// Array init
for (var i = 0; i<destinationDefinition.length; i++) {destinationArray[i] = new Array();}

// Hotels...
destinationArray[0][0] = new Array('51000d1e0551be7bd091f3bfe0feec1e', 'Hotel Bellevue');
destinationArray[0][1] = new Array('d2ac741e45e4c5855695c45525656ffd', 'Hotel Grabovac');
destinationArray[0][2] = new Array('ac67bad1b60055f096e0fe59b1d988eb', 'Hotel Jezero');
destinationArray[0][3] = new Array('1428673ac3f067e66f0ae03f270d7a3a', 'Hotel Plitvice');

/*
 ***************************************************************************
 * 									Functions 
 *
 */
// Check
function checkReservation() {
	var forma = document.forms['phobs_book'];
	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;}

	var selectedDan = parseInt(forma.elements['check_in_day'].value);
	var selectedMjesec = parseInt(forma.elements['check_in_month'].value);
	var selectedGodina = parseInt(forma.elements['check_in_year'].value);
	
	if (
	    (selectedDan <= todayDan && selectedMjesec <= todayMjesec && selectedGodina <= todayGodina)
	    ||
	    (selectedMjesec < todayMjesec && selectedGodina <= todayGodina)
	    ||
	    (selectedGodina < todayGodina)
	) {
		alert(errorMessageDate);
	} else {
		// Check hotel
		openBooking();
	}

	return false;
}
// Open booking
function openBooking() {
	var forma = document.forms['phobs_book'];
	
	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 defaultPage = '';
	if (forma.elements['company_id']) {
		defaultPage = 'http://www.phobs.net/secure/booking.php';
		defaultPage += '?company_id=' + forma.elements['company_id'].value;
	}
	var win = window.open(defaultPage, '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;
}
// Set hotel list
function setHotelList() {
	var forma = document.forms['phobs_book'];
	var myDestination = forma.elements['destination'];
	var myHotels = forma.elements['hotel'];
	
	// Reset hotel list
	for (var t = 0; t < myHotels.length; t++) {
		while (myHotels.options[t] != null) {
			myHotels.options[t] = null;
		}
	}
	if (myDestination.options[myDestination.selectedIndex].value == '-') {
		myHotels.options[myHotels.length] = new Option(defaultHotelSelect, '-');
		if (destinationArray.length) {
			var newDestination;
			for (var a = 0; a < destinationArray.length; a++) {
				for (b = 0; b < destinationArray[a].length; b++) {
					newDestination = destinationArray[a][b];
					myHotels.options[myHotels.length] = new Option(newDestination[1], newDestination[0]);
				}
			}
		}
	} else {
		// Pick
		var destinationPick;
		for (var i = 0; i<destinationDefinition.length; i++) {
			if (destinationDefinition[i][0] == myDestination.options[myDestination.selectedIndex].value) {
				destinationPick = i;
				break;
			}
		}
		// Hotels
		var hotelList = destinationArray[destinationPick];
		myHotels.options[myHotels.length] = new Option(defaultHotelSelect, '-');
		for (var i = 0; i < hotelList.length; i++) {
			myHotels.options[myHotels.length] = new Option(hotelList[i][1], hotelList[i][0]);
		}
	}
}
// View/cancel
function viewCancelBooking(myLink) {
	var forma = document.forms['phobs_book'];
	forma.elements['view_cancel'].value = '1';
	openBooking();
	forma.elements['view_cancel'].value = '';
	return false;
}
// Setup
function phobsSetupForm() {
	var forma = document.forms['phobs_book'];
	var myDestination = forma.elements['destination'];
	var myHotels = forma.elements['hotel'];

	// Set date
	var arrivalDate = new Date();
	arrivalDate = new Date(arrivalDate.getYear(), arrivalDate.getMonth(), arrivalDate.getDate()+1);
	var dan = arrivalDate.getDate();
	var mjesec = arrivalDate.getMonth();
	var godina = arrivalDate.getFullYear();
	if (godina<999) {godina += 1900;}
	
	var danSelect = forma.elements['check_in_day'];
	var mjesecSelect = forma.elements['check_in_month'];
	var godinaSelect = forma.elements['check_in_year'];
	// Fix godina
	for (var t = 0; t < godinaSelect.length; t++) {
		while (godinaSelect.options[t] != null) {
			godinaSelect.options[t] = null;
		}
	}
	godinaSelect.options[godinaSelect.length] = new Option(godina, godina);
	godinaSelect.options[godinaSelect.length] = new Option((parseInt(godina)+1), (parseInt(godina)+1));
	// date...
	for (var i=0; i < danSelect.length; i++) {
		if (parseInt(danSelect[i].value) == parseInt(dan)) {
			danSelect[i].selected = true;
		}
	}
	for (var i=0; i < mjesecSelect.length; i++) {
		if (parseInt(mjesecSelect[i].value) == (parseInt(mjesec)+1)) {
			mjesecSelect[i].selected = true;
		}
	}
	
	/*
	 * Default selection...
	 */
	// Destination
	if (myDestination) {
		if (defaultDestination && defaultDestination.length && myDestination.length) {
			var myDestinationSelection = false;
			for (var i = 0; i < myDestination.length; i++) {
				if (myDestination[i].value == defaultDestination) {
					myDestination[i].selected = true;
					myDestinationSelection = true;
					break;
				}
			}
			if (myDestinationSelection) {
				setHotelList();
			}
		}
	}
	// Hotel
	if (defaultHotel && defaultHotel.length && myHotels && myHotels.length) {
		var toBreak = false;
		for (var x = 0; x < destinationArray.length; x++) {
			for (var y = 0; y < destinationArray[x].length; y++) {
				if (defaultHotel == destinationArray[x][y][0]) {
					// Get destination
					if (myDestination) {
						if (destinationDefinition && destinationDefinition.length && myDestination.length) {
							for (var i = 0; i < myDestination.length; i++) {
								if (myDestination[i].value == destinationDefinition[x][0]) {
									myDestination[i].selected = true;
									break;
								}
							}
							setHotelList();
						}
					}
					// Set hotel
					for (var i = 0; i < myHotels.length; i++) {
						if (myHotels[i].value == defaultHotel) {
							myHotels[i].selected = true;
							toBreak = true;
							break;
						}
					}
					toBreak = true;
					break;
				}
			}
			if (toBreak) {
				break;
			}
		}
	}
}