/*******************************************************************************
	EasyWEB	5

	@Copyright 	Synerway Sp. z o. o. http://www.synerway.pl/
			All rights reserved

	@Author		Antoni Jakubiak <a.jakubiak@synerway.pl>

	@Description	Obsluga formularza
*******************************************************************************/


function showHotel(placeid,searchqueryid)
{
var sortby = document.getElementById('sortby').value;
window.location = "hotel.html?placeid="+placeid+"&searchqueryid="+searchqueryid+"&sortby="+sortby;
}


/**
 * Rejestracja formularza wyszukiwania
 */
function BookingSearchHotelFormular() {
	/**
	 * Ja byc ja
	 */
	var oThis = this;
	/**
	 * zbudowanie selektorow z data
	 */
	this.checkin = new DateSelect( 'checkindate' );
	this.checkout = new DateSelect( 'checkoutdate' );
	this.checkin.setOnchangeCallback( function() {
		oThis.calendarSetCheckoutRange();
	} );
	/**
	 * Gdzie chcesz jechac
	 */
	this.destination = new DestinationAutoComplete( document.getElementById('destination') );
	/**
	 * Liczba osob, liczba pokoi
	 */
	this.noanor = new NumOfAdultsNumOfRooms( 'numofadults', 'numofrooms', 4 );
	/**
	 * Przycisk submitujacy
	 */
	document.getElementById('bookingsearchformSubmitButton').onclick = function() {
		oThis.submit();
	}
	/**
	 * Akcja, ktora ma byc uruchomiona w celu wyszukiwania
	 */
	this.searchActionUrl = 'index.php?pid=302';
	/**
	 * Akcja, ktora ma byc uruchomina po wyszukiwaniu
	 */
	this.searchResultUrl = 'results.html?';
}




/**
 * Funkcja, ktora powinna byc uruchomiona gdy zmienimy date przyjazdu.
 * Jej zadaniem jest zmiana daty wyjazdu i zmiana zakresow
 */
BookingSearchHotelFormular.prototype.calendarSetCheckoutRange = function () {
	var d = Date.parseDate( this.checkin.get(), "%Y-%m-%d" );
	d.setDate( d.getDate() + 1 );
	var d2 = new Date( d );
	// maksymalna liczba dni na ktore moze byc zrobiona rezerwacja
	d2.setDate( d.getDate() + 49 );
	this.checkout.set( d.print( "%Y-%m-%d" ) );
	this.checkout.setRange( d.print("%Y-%m-%d" ), d2.print("%Y-%m-%d") );
}

/**
 * Wysylanie formularza
 *	wysylanie formularza poprzez funkcje AJAX, POST
 *	funkcja ta powinna zwrocic identyfikator wynikow wyszukiwania
 *	po otrzymaniu identyfikatora wyniko wyszukiwania przekierowuje przegladarke na adres URL,
 *	ktory potrafi wyswietlic wyniki wyszukiwania
 *
 */
BookingSearchHotelFormular.prototype.submit = function() {
	if ( ! this.validate() ) {
		return false;
	}
	var oThis = this;

	var loc = 'index.php?pid=306';
	loc += '&placeid=' + (this.getPlaceId() ? this.getPlaceId() : '');
	loc += '&cityid=' + (this.getCityId() ? this.getCityId() : '');
	loc += '&regionid=' + (this.getRegionId() ? this.getRegionId() : '');
	loc += '&countryid=' + (this.getCountryId() ? this.getCountryId() : '');
	loc += '&checkindate=' + this.getCheckin();
	loc += '&checkoutdate=' + this.getCheckout();
	loc += '&numofadults=' + this.getNumOfAdults();
	loc += '&numofrooms=' + this.getNumOfRooms();
	loc += '&breakfast=' + this.getBreakfastId();
	loc += '&currencyid=' + this.getCurrencyId();
	loc += '&searchresulturl=' + this.searchResultUrl;
	window.location = loc;
	/**
	 * Good Bye AJAX!
	advAJAX.post({
		url: oThis.searchActionUrl,
		parameters : {
			'placeid'      : this.getPlaceId() ? this.getPlaceId() : '',
			'cityid'       : this.getCityId() ? this.getCityId() : '',
			'regionid'     : this.getRegionId() ? this.getRegionId() : '',
			'countryid'    : this.getCountryId() ? this.getCountryId() : '',
			'checkindate'  : this.getCheckin(),
			'checkoutdate' : this.getCheckout(),
			'numofadults'  : this.getNumOfAdults(),
			'numofrooms'   : this.getNumOfRooms(),
			'breakfast'    : this.getBreakfastId(),
			'currencyid'   : this.getCurrencyId()
		},
		onSuccess : function ( obj ) {
//			alert( obj.responseText );
			oThis.parseQueryResultsAndRedirect( obj.responseXML );
		}
	});
	this.displayWaitForResultsBanner();
	*/
}
/**
 * Sprawdzenie poprawnosci w formularzu wyszukiwania
 *	Funkcja zwraca true, gdy wszystko jest poprawnie.
 *	Jezeli byl blad to zwraca false oraz wyswietla komunikat o bledzie
 */
BookingSearchHotelFormular.prototype.validate = function() {
	var d = this.getDestinationObject();
	if ( ! d ) {
		// jezeli nie wybrano celu, to moze jest wpisacy obiekt dla ktorego mamy szukac ofert
		placeId = this.getPlaceId();
		cityId = this.getCityId();
		if (placeId == 'NULL' || !(placeId > 0)) {
			if (cityId == 'NULL' || !(cityId > 0)) {
				alert( getText( 'ah:bookingsearchform:PROSZE_WYBRAC_CEL_PODROZY' ) );
				document.getElementById('destination').focus();
				return false;
			}
		}
	}
	// TODO, podobnie mozna by sie bylo zabawic z checkin < checkout, checkin + 50 >= checkout, numofaduls ^ numofrooms
	// ale na to na razie nie mam checi
	return true;
}

/**
 * Wyswietlenie informacji, ze oczekujemy na wyniki wyszukiwania
 */
BookingSearchHotelFormular.prototype.displayWaitForResultsBanner = function() {
	// kontenery
	var container = document.getElementById('bookingsearchformContainer');
	var waitContainer = document.getElementById('bookingsearchformWaitContainer');
	// ukrycie formularza
	// zabawa z zmiennymi
	var t = new Template();
	t.assign( new TemplateVar( 'checkin', this.getCheckin() ) );
	t.assign( new TemplateVar( 'checkout', this.getCheckout() ) );
	t.assign( new TemplateVar( 'destination', this.getDestination() ) );
	t.assign( new TemplateVar( 'numofadults', this.getNumOfAdults() ) );
	t.assign( new TemplateVar( 'numofrooms', this.getNumOfRooms() ) );
	t.assign( new TemplateVar( 'breakfast', this.getBreakfast() ) );
	t.assign( new TemplateVar( 'currency', this.getCurrency() ) );

	if ( ! this.waitHtml ) {
		this.waitHtml = waitContainer.innerHTML;
	}
	waitContainer.innerHTML = t.parse( this.waitHtml );

	// ukrycie i pokazanie formularza
	container.style.display = 'none';
	waitContainer.style.display = 'block';

}

/**
 * Ukrycie banerka z informacja ze oczekujemy na wynik wyszukiwania
 *	ponowne wyswietlenie formularza
 *	byc moze wlasnie byl blad
 */
BookingSearchHotelFormular.prototype.hideWaidForResultsBanner = function() {
}









/**
 * Przetworzenie obiektu XML zawierajacego wyniki wyszukiwania
 * Przekierowanie, na strone z wynikami wyszukiwania
 */
BookingSearchHotelFormular.prototype.parseQueryResultsAndRedirect = function( xml ) {
	var searchqueryid = xml.getElementsByTagName( 'searchquery' ).item(0).getAttribute( 'searchqueryid' );
	window.location  = this.searchResultUrl + 'searchqueryid=' + searchqueryid;
}

/**
 * Gettery
 */
BookingSearchHotelFormular.prototype.getCheckin = function() {
	return this.checkin.get();
}
BookingSearchHotelFormular.prototype.getCheckout = function() {
	return this.checkout.get();
}
BookingSearchHotelFormular.prototype.getDestinationObject = function() {
	return this.destination.get();
}
BookingSearchHotelFormular.prototype.getDestination = function() {
	var d = this.getDestinationObject();
	if ( d ) return d.name;
}
BookingSearchHotelFormular.prototype.getCityId = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return document.getElementById('cityid').value;
	if ( 'city' != d.type ) return document.getElementById('cityid').value;
	return d.id;
}
BookingSearchHotelFormular.prototype.getRegionId = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'region' != d.type ) return null;
	return d.id;
}
BookingSearchHotelFormular.prototype.getCountryId = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'country' != d.type ) return null;
	return d.id;
}
BookingSearchHotelFormular.prototype.getNumOfAdults = function() {
	return this._getSelectedValue( document.getElementById('numofadults') );
}
BookingSearchHotelFormular.prototype.getNumOfRooms = function() {
	return this._getSelectedValue( document.getElementById('numofrooms') );
}
BookingSearchHotelFormular.prototype.getCurrencyId = function() {
	return this._getSelectedValue( document.getElementById('currency') );
}
BookingSearchHotelFormular.prototype.getCurrency = function() {
	return this._getSelectedName( document.getElementById('currency') );
}
BookingSearchHotelFormular.prototype.getBreakfastId = function() {
	return this._getSelectedValue( document.getElementById('breakfast') );
}
BookingSearchHotelFormular.prototype.getBreakfast = function() {
	return this._getSelectedName( document.getElementById('breakfast') );
}
BookingSearchHotelFormular.prototype.getPlaceId = function() {
	return document.getElementById('placeid').value;
}

/**
 * Zwraca wybrana wartosc dla selecta
 */
BookingSearchHotelFormular.prototype._getSelectedValue = function ( selectElement ) {
	if ( ! selectElement ) return;
	return selectElement.options[ selectElement.selectedIndex ].value;
}
BookingSearchHotelFormular.prototype._getSelectedName = function ( selectElement ) {
	if ( ! selectElement ) return;
	return selectElement.options[ selectElement.selectedIndex ].text;
}

