$(document).ready(function() {

    var frm$ = $('.st-booking-form');
    var summary$ = $('.st-booking-summary');
    var travelerCountDd$ = $('#booking_request_travelerCount');
    var travelers$ = $('.st-traveler');
    var travelerFields$ = travelers$.find('input,select');

    frm$.find('input, select').change(function() {
        var tmp = location.href.split('?');
        var tmp2 = tmp[0].split('/');
        tmp2.pop();
        var url = tmp2.join('/') + '/berechne-gesamtpreis';
        if (tmp[1])
        {
            url += '?'+ tmp[1];
        }

        $.ajax({
            url: url,
            type: 'post',
            data: frm$.serialize()

        }).then(function(r) {
            summary$.html(r);

        }, function() {
            summary$.html('Aufgrund eines Fehlers konnte kein Angebot ermittelt werden.');
        })
    });

    function updateTravelers()
    {
        var travelerCount = parseInt(travelerCountDd$.val());
        travelers$.hide();
        travelerFields$.prop('required', false);
        for (var i = 1; i <= travelerCount; ++i)
        {
            $('.st-traveler-'+ i).show().find('input,select').prop('required', true);
        }
    }
    travelerCountDd$.change(updateTravelers);
    updateTravelers();

});