/*******************************************************************************
	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
*******************************************************************************/

// blokowanie mozliwosci wyboru dwa razy tego samego pokoju
// id : select w ktorym dokonujemy wyboru


function blockIt(id)
{
	return;
}

// dodanie mozliwosci wyboru kilki rodziajow pokoi


function addrooms()
{	
	//blockIt('roomtypeid');

	var count = document.getElementById('countrooms').value;
	count = parseFloat(count);
	
	var gloabalcount = count;
	
	count += parseFloat(1);
	if (count > 3)
	{
		count = 2;
	}
	if (count > 1)
	{
		document.getElementById('removerooms').style.display = "block";
	}else
	{
		document.getElementById('removerooms').style.display = "none";
	}
		
	document.getElementById('countrooms').value = count;
	
	showrooms(count);
}


function removerooms()
{
	var count = document.getElementById('countrooms').value;
	count = parseFloat(count);
	
	count -= parseFloat(1);
	if (count < 1)
	{
		count = parseFloat(1);
	}
	
	document.getElementById('countrooms').value = count;

	showrooms(count);
}


function 	showrooms(count)
{
	var i;
	for( i = 1; i < 4; i++)
	{
		var rooms  = 'rooms'+i;
		var roomsr = 'roomsr'+i;
		var ind = i > 1 ? 'roomtypeid'+i : 'roomtypeid';

		if (i < count + 1)
		{

			document.getElementById(rooms).style.display = "block";
			document.getElementById(roomsr).style.display = "block";
			
		}else
		{
			document.getElementById(ind).value = 'NULL';
			document.getElementById(rooms).style.display = "none";
			document.getElementById(roomsr).style.display = "none";		
		}
	}
	if (document.getElementById('countrooms').value >1 )
	{
		document.getElementById('removerooms').style.display = "block";
	}else 
	{
		document.getElementById('removerooms').style.display = "none";
	}
	
}


/**
 * Rejestracja formularza wyszukiwania
 */
function BookingSearchHotelFormular( iRootUrl ) {
	/**
	 * Adres URL serwera
	 */
	this.rootUrl = iRootUrl ? iRootUrl : "";
	/**
	 * 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'), this.rootUrl, true );
	/**
	 * 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 = 'http://www.adorohotels.com/index.php?pid=302';
	/**
	 * Akcja, ktora ma byc uruchomina po wyszukiwaniu
	 */
	this.searchResultUrl = 'http://www.adorohotels.com/results.html?';
	this.searchResultUrlForHotel = "http://www.adorohotels.com/hotel.html?";
	
}




/**
 * Funkcja, ktora powinna byc uruchomiona gdy zmienimy date przyjazdu.
 * Jej zadaniem jest zmiana daty wyjazdu i zmiana zakresow
 */
BookingSearchHotelFormular.prototype.calendarSetCheckoutRange = function () {
	// funkcja Date.parseDate jest stworzona w kalendarzyku jscalendar
	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 bookings = this.getBookingsFlag();
	var afsiteid = document.getElementById('siteid');
	if(afsiteid)
	{
		afsiteid = afsiteid.value;
	}
	else
	{
		afsiteid = 1;
	}
	
	var oThis = this;
	cdAJAX.setDefaultParameters({
		group: null,
		unique: true
	});
	cdAJAX.get({
		url: oThis.searchActionUrl,
		parameters : {
			'cityid'       : this.getCityId() ? this.getCityId() : '',
			'regionid'     : this.getRegionId() ? this.getRegionId() : '',
			'countryid'    : this.getCountryId() ? this.getCountryId() : '',
			'placeid'      : this.getPlaceId() ? this.getPlaceId() : '',
			'checkindate'  : this.getCheckin(),
			'checkoutdate' : this.getCheckout(),
			'roomtypeid'   : this.getRoomId(),
			'roomtypeid2'  : this.getRoomId2(),
 			'roomtypeid3'  : this.getRoomId3(),
//			'numofadults'  : this.getNumOfAdults(),
			'countrooms'   : this.getdifrooms(),
			'numofrooms'   : this.getNumOfRooms(),
			'numofrooms2'  : this.getNumOfRooms2(),
			'numofrooms3'  : this.getNumOfRooms3(),
//			'breakfast'    : this.getBreakfastId(),
			'currencyid'   : this.getCurrencyId(),
			'bookings'	   : this.getBookingsFlag(),
			'object_type'  : this.getObjectTypeId(),
			'object_class' : this.getObjectClassId(),
			'afsiteid'	   : afsiteid
		},
		onSuccess : function ( obj ) {
			var query = getQueryResponse();
			oThis.parseQueryResponse( query );
			oThis.goToResultsPage();
			getQueryResponse = null;
		}
	});
	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 ) {
		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( 'countrooms', this.getdifrooms() ) );
	t.assign( new TemplateVar( 'roomtypeid', this.getRoom() ) );
	t.assign( new TemplateVar( 'numofrooms2', this.getNumOfRooms2() ) );
	t.assign( new TemplateVar( 'roomtypeid2', this.getRoom2() ) );
	t.assign( new TemplateVar( 'numofrooms3', this.getNumOfRooms3() ) );
	t.assign( new TemplateVar( 'roomtypeid3', this.getRoom3() ) );
	t.assign( new TemplateVar( 'object_type', this.getObjectType() ) );
	t.assign( new TemplateVar( 'object_class', this.getObjectClass() ) );
	//	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() {
}









/**
 * Zrozumienie odpowiedzi
 */
BookingSearchHotelFormular.prototype.parseQueryResponse = function( query ) {
	this.searchqueryid = query.searchqueryid;
	this.queryid = query.queryid;
}

/** 
 * Domyslna akcja na zakonczenie,
 * przejscie na strone z wynikami wyszukiwania
 * ta funkcja moze byc napdiasana przez afiliacje
 */
BookingSearchHotelFormular.prototype.goToResultsPage = function() {
	var placeid = this.getPlaceId();
	var url = this.searchResultUrl;
	if ( placeid > 0 ) {
		var d = this.getDestinationObject();
		if(d.bookings == 1)
		{	window.location = this.searchResultUrlForHotel + "placeid="+placeid+"&bookings=1&queryid="+this.queryid; return;}
		else
		{	window.location = this.searchResultUrlForHotel + "placeid="+placeid+"&bookings=0&searchqueryid=" + this.searchqueryid; return; }
	}
	window.location  = url + 'queryid='+this.queryid+'&' + 'searchqueryid=' + this.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.getBookingsFlag = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if(d.bookings == 1)
	{ return 1; }
	return 0;
}
BookingSearchHotelFormular.prototype.getCityId = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'city' != d.type ) return null;
	return d.id;
}
BookingSearchHotelFormular.prototype.getRoomId = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'room' != d.type ) return null;
	return d.id;
}

BookingSearchHotelFormular.prototype.getRoomId2 = function() {

	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'room' != d.type ) return null;
	return d.id;
}
BookingSearchHotelFormular.prototype.getRoomId3 = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'room' != d.type ) return null;
	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.getPlaceId = function() {
	var d = this.getDestinationObject();
	if ( ! d ) return null;
	if ( 'place' != d.type ) return null;
	return d.id;
}

BookingSearchHotelFormular.prototype.getRoomId = function() {
	return this._getSelectedValue( document.getElementById('roomtypeid') );
}
BookingSearchHotelFormular.prototype.getRoomId2 = function() {
	return this._getSelectedValue( document.getElementById('roomtypeid2') );
}
BookingSearchHotelFormular.prototype.getRoomId3 = function() {
	return this._getSelectedValue( document.getElementById('roomtypeid3') );
}
BookingSearchHotelFormular.prototype.getRoom = function() {
	return this._getSelectedName( document.getElementById('roomtypeid') );
}
BookingSearchHotelFormular.prototype.getRoom2 = function() {
	return this._getSelectedName( document.getElementById('roomtypeid2') );
}
BookingSearchHotelFormular.prototype.getRoom3 = function() {
	return this._getSelectedName( document.getElementById('roomtypeid3') );
}
BookingSearchHotelFormular.prototype.getNumOfAdults = function() {
	return this._getSelectedValue( document.getElementById('numofadults') );
}
BookingSearchHotelFormular.prototype.getNumOfRooms = function() {
	return this._getSelectedValue( document.getElementById('numofrooms') );
}
BookingSearchHotelFormular.prototype.getdifrooms = function() {
	return document.getElementById('countrooms').value;
}
BookingSearchHotelFormular.prototype.getNumOfRooms2 = function() {
	return this._getSelectedValue( document.getElementById('numofrooms2') );
}
BookingSearchHotelFormular.prototype.getNumOfRooms3 = function() {
	return this._getSelectedValue( document.getElementById('numofrooms3') );
}
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.getObjectTypeId = function() {
	return this._getSelectedValue( document.getElementById('object_type') );
}
BookingSearchHotelFormular.prototype.getObjectClassId = function() {
	return this._getSelectedValue( document.getElementById('object_class') );
}
BookingSearchHotelFormular.prototype.getObjectType = function() {
	return this._getSelectedName( document.getElementById('object_type') );
}
BookingSearchHotelFormular.prototype.getObjectClass = function() {
	return this._getSelectedName( document.getElementById('object_class') );
}


/**
 * 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;
}



/**
 * nadpisanie aktualnego destination
 */
BookingSearchHotelFormular.prototype._setDestination = function( type, id, name, bookings ) {
	this.destination.setOne(type,id,name,bookings);
}
 
 
/**
 * Ustawienia aktualnego miasta w obiekcie destination
 */
BookingSearchHotelFormular.prototype.setCityId = function( cityid,name ) {
	this._setDestination("city",cityid,name,0);
}
/**
 * Ustawienie aktualnego regionu w obiekcie destination
 */
BookingSearchHotelFormular.prototype.setRegionId = function( regionid,name ) {
	this._setDestination("region",regionid,name,0);
}
/**
 * Ustawiene aktualnego panstwa w obiekcie destination
 */
BookingSearchHotelFormular.prototype.setCountryId = function( countryid,name ) {
	this._setDestination("country",countryid,name,0);
}
BookingSearchHotelFormular.prototype.setPlaceId = function( placeid,name,bookings ) {
	this._setDestination("place",placeid,name,bookings);
}





