let reservationLanguageFieldName = 'api_language';
let reservationCategoryIdFieldName = 'category_id';
let reservationCategoryItemIdFieldName = 'category_item_id';
let reservationCategoryItemPluralFieldName = 'category_item_plural';
let reservationCategoryItemSingularFieldName = 'category_item_singular';
let reservationTempReservationIdFieldName = 'temp_reservation_id';
let reservationTempReservationRowIdFieldName = 'temp_reservation_row_id';
let reservationMaxAmountSelectableFieldName = 'max_amount_selectable';
let reservationInputLanguage;
let reservationInputCategoryId;
let reservationInputCategoryItemId;
let reservationInputCategoryItemPlural;
let reservationInputCategoryItemSingular;
let reservationInputTempReservationId;
let reservationInputTempReservationRowId;
let reservationMaxAmountSelectable;
let reservationInputTypeOfVisitorOption;
let reservationInputDate;
let reservationInputPeriod;
let reservationInputTime;
let reservationInputNumberOfPersons;
let reservationInputNumberOfPersonsInGroup;
let reservationInputNumberOfPersonsInGroupRow;
let reservationInputNumberOfPersonsRow;
let reservationInputQuantity;
let reservationInputQuantityRow;
let reservationTimeSlots;
$j(function(){
reservationInputLanguage = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationLanguageFieldName);
reservationInputCategoryId = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationCategoryIdFieldName);
reservationInputCategoryItemId = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationCategoryItemIdFieldName);
reservationInputCategoryItemPlural = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationCategoryItemPluralFieldName);
reservationInputCategoryItemSingular = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationCategoryItemSingularFieldName);
reservationInputTempReservationId = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationTempReservationIdFieldName);
reservationInputTempReservationRowId = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationTempReservationRowIdFieldName);
reservationMaxAmountSelectable = getJQueryElementAndReturnUndefinedIfNotFound('#'+reservationMaxAmountSelectableFieldName);
reservationInputTypeOfVisitorOption = getJQueryElementAndReturnUndefinedIfNotFound('#type_of_visitor_option');
reservationInputDate = getJQueryElementAndReturnUndefinedIfNotFound('#date');
reservationInputPeriod = getJQueryElementAndReturnUndefinedIfNotFound('#period');
reservationInputTime = getJQueryElementAndReturnUndefinedIfNotFound('#time');
reservationInputNumberOfPersons = getJQueryElementAndReturnUndefinedIfNotFound('#number_of_persons');
reservationInputNumberOfPersonsInGroup = getJQueryElementAndReturnUndefinedIfNotFound('#number_of_persons_in_group');
reservationInputNumberOfPersonsInGroupRow = getJQueryElementAndReturnUndefinedIfNotFound('#divnumber_of_persons_in_group');
reservationInputNumberOfPersonsRow = getJQueryElementAndReturnUndefinedIfNotFound('#divnumber_of_persons');
reservationInputQuantity = getJQueryElementAndReturnUndefinedIfNotFound('#quantity');
reservationInputQuantityRow = getJQueryElementAndReturnUndefinedIfNotFound('#divquantity');
hideJqueryElement(reservationInputNumberOfPersonsInGroupRow);
hideJqueryElement(reservationInputNumberOfPersonsRow);
hideJqueryElement(reservationInputQuantityRow);
getReservationStep1TimeSlots();
});
function getActiveReservationTimeSlot() {
if(reservationTimeSlots === undefined || reservationTimeSlots.length === 0 || reservationInputTime === undefined) {
return undefined;
}
let selectedValue = getNumberValueFromJQueryElement(reservationInputTime);
if(isNaN(selectedValue) || (empty(selectedValue) && selectedValue !== 0)){
return undefined;
}
let object;
for(let key in reservationTimeSlots){
object = reservationTimeSlots[key];
if(object === undefined){
continue;
}
if(object.key !== selectedValue){
continue;
}
return object;
}
return undefined;
}
function getReservationStep1TimeSlots()
{
if(
reservationInputLanguage === undefined ||
reservationInputCategoryId === undefined ||
reservationInputCategoryItemId === undefined ||
reservationInputTempReservationId === undefined ||
reservationInputDate === undefined
){
//Required inputs not found
return;
}
let dataObject = {
category_id: reservationInputCategoryId.val(),
category_item_id: reservationInputCategoryItemId.val(),
temp_reservation_id: reservationInputTempReservationId.val(),
date: reservationInputDate.val(),
language: reservationInputLanguage.val()
}
if(
empty(dataObject.category_id) ||
empty(dataObject.category_item_id) ||
empty(dataObject.temp_reservation_id) ||
empty(dataObject.date) ||
empty(dataObject.language)
)
{
//Required inputs are empty
return;
}
if(reservationInputPeriod !== undefined){
//Only add the reservation period if the input is found
let periodId = reservationInputPeriod.val();
if(empty(periodId)){
//The input is found but is empty, stop executing because it's a required field if the input is available.
return;
}
dataObject.reservation_period_id = periodId;
}
if(reservationInputTempReservationRowId !== undefined){
//Reservation row is input found
let reservationRowId = reservationInputTempReservationRowId.val();
if(!empty(reservationRowId)){
//Only add it if there is a value set, otherwise we don't need to send it along with the call
dataObject.temp_reservation_row_id = reservationRowId;
}
}
//Clear both the time and number selection because the call might take a while and we don't want to user to be able to select old data.
clearSelect(reservationInputTime);
setInputDataForNumbersSelection();
executeAjaxCall(baseUrl+resourceFolder+'ajax/reservation/getReservationCategoryItemTimeSlots.php', handleGetReservationStep1TimeSlotsResponse, 'POST', dataObject);
}
function handleGetReservationStep1TimeSlotsResponse(call)
{
if(call.status === jsonAjaxCallStatusOK)
{
reservationTimeSlots = call.data;
setReservationTimeSlots();
}
else
{
alertJsonError(call);
}
}
function setInputDataForNumberOfPersons(numberOfPersons)
{
if(reservationInputNumberOfPersons === undefined){
return;
}
if(numberOfPersons === 0){
hideJqueryElement(reservationInputNumberOfPersonsRow);
return;
}
showJqueryElement(reservationInputNumberOfPersonsRow);
if(numberOfPersons === -1){
if(reservationMaxAmountSelectable === undefined){
numberOfPersons = 1;
} else {
numberOfPersons = getNumberValueFromJQueryElement(reservationMaxAmountSelectable);
}
}
let singular= '';
let plural = '';
if(reservationInputCategoryItemSingular !== undefined){
singular = reservationInputCategoryItemSingular.val();
}
if(reservationInputCategoryItemPlural !== undefined){
plural = reservationInputCategoryItemPlural.val();
}
let options = [{
key : '',
value: ''
}];
let suffix = singular;
for(let index = 1; index <= numberOfPersons; index++){
options.push({
key : index,
value: index+'x '+suffix
});
if(index === 1){
suffix = plural;
}
}
fillSelect(reservationInputNumberOfPersons, options, true);
}
function setInputDataForNumberOfPersonsInGroup(numberOfGroups)
{
if(reservationInputNumberOfPersonsInGroup === undefined){
return;
}
if(numberOfGroups === 0){
hideJqueryElement(reservationInputNumberOfPersonsInGroupRow);
return;
}
showJqueryElement(reservationInputNumberOfPersonsInGroupRow);
numberOfGroups = getNumberValueFromJQueryElement(reservationMaxAmountSelectable);
let singular= '';
let plural = '';
if(reservationInputCategoryItemSingular !== undefined){
singular = reservationInputCategoryItemSingular.val();
}
if(reservationInputCategoryItemPlural !== undefined){
plural = reservationInputCategoryItemPlural.val();
}
let options = [{
key : '',
value: ''
}];
let suffix = singular;
for(let index = 1; index <= numberOfGroups; index++){
options.push({
key : index,
value: index+'x '+suffix
});
if(index === 1){
suffix = plural;
}
}
fillSelect(reservationInputNumberOfPersonsInGroup, options, true);
}
function setInputDataForNumbersSelection()
{
clearSelect(reservationInputNumberOfPersonsInGroup);
clearSelect(reservationInputNumberOfPersons);
clearSelect(reservationInputQuantity);
hideJqueryElement(reservationInputNumberOfPersonsInGroupRow);
hideJqueryElement(reservationInputNumberOfPersonsRow);
hideJqueryElement(reservationInputQuantityRow);
let reservationTimeSlot = getActiveReservationTimeSlot();
if(reservationTimeSlot === undefined)
{
return;
}
//undefined needs to be a string here because typeof returns a string
if(typeof reservationTimeSlot.number_of_items !== 'undefined'){
setInputDataForQuantity(reservationTimeSlot.number_of_items);
return;
}
if(typeof reservationTimeSlot.number_of_persons !== 'undefined'){
if(reservationInputNumberOfPersonsInGroup !== undefined){
setInputDataForNumberOfPersonsInGroup(reservationTimeSlot.number_of_persons);
return;
}
setInputDataForNumberOfPersons(reservationTimeSlot.number_of_persons);
}
}
function setInputDataForQuantity(quantity)
{
if(reservationInputQuantity === undefined){
return;
}
if(quantity === 0){
hideJqueryElement(reservationInputQuantityRow);
return;
}
showJqueryElement(reservationInputQuantityRow);
}
function setReservationTimeSlots() {
if(reservationInputTime === undefined){
return;
}
fillSelect(reservationInputTime, reservationTimeSlots, true);
setInputDataForNumbersSelection();
}