    $(document).ready(function(){
        $('#ow').attr('checked', '').change(function(){
            var is_checked = $(this).attr('checked');
            if(is_checked){
                $('#return_date').hide();
            }
            else{
                $('#return_date').show();
            }
        });

        var input_elements = [
            null,
            $('#ob_orig'),
            $('#ob_dest'),
            $('#date1'),
            $('#date2'),
            $('#ow'),
            $('#yz')
        ];
        function focus_next(self) {
            var index = $(self).attr('tabindex');
            if(++index >= input_elements.length) index = 0;
            input_elements[index].focus();
        }

        $(".ac-city").autocomplete(
            "/city/complete",
            {
                minChars: 3,
                delay: 250,
                mustMatch: true,
                max: 20,
                formatResult: function(row){ return row[1]; },
                width: '180px'
            }
        );

        $('#ob_orig').result(function(){
            setTimeout("$('#ob_dest').focus()", 300);
        });
        $('#ob_dest').result(function(){
            setTimeout("$('#date1').focus()", 300);
        });

        $('.calendar').datepicker({
            nextText: 'Monat&gt;',
            prevText: '&lt;Monat',
            currentText: 'Heute',
            clearText: 'Löschen',
            closeText: 'X',
            yearRange: '-0:+1',
            showOn:    'button',
            buttonImageOnly: true,
            buttonImage: '/static/images/calendar.gif',
            buttonText: 'Kalender',
            changeFirstDay: false,
            doKeyPress: function(e) { return true; },
            dateFormat: 'dd.mm.yy',
            dayNames: [ 'So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa' ],
            monthNames: [ 'Jan', 'Feb', 'Mär', 'Apr', 'Mai',
                'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez' ]
        });

        $('.ac-city').keyup( function(){
            $(this).css('background', 'white');
        });

        $('input.ac-city').change(
            function(){
                var self=this;
                $.ajax({
                    url: '/city/resolve_ajax',
                    dataType: 'HTML',
                    data: 'value=' + $(self).val(),
                    success: function(response){
                        $(self).val(response);
                        focus_next(self);
                    },
                    error: function(){
                        $(self).css('background', '#FED2D6');
                        focus_next(self);
                    }
                });
            }
        );

        $('input.calendar').change( function(){
            var self=this;
            $.ajax({
                url:      '/date/check_before',
                dataType: 'HTML',
                data:     $('.calendar').serialize()
                    + '&which=' + $(self).attr('id'),
                success:   function(response){
                    $(self).css('background', 'white');
                    $(self).val(response);
                    focus_next(self);
                },
                error:     function(){
                    $(self).css('background', '#FED2D6');
                    focus_next(self);
                }
            });
        });

        $("#ob_orig").focus();
        $('#itinerary')
        .attr('action', 'https://skyways.de/flight/search');

        $('#start_btn').click(function(){
            $('#itinerary').submit();
        });

        $('#date2,#ow').keydown(function(e){
            if(e.keyCode == 13){
                $('#itinerary').submit();
            }
        });
        $('#date1').keydown(function(e){
            if(e.keyCode == 13 && $('#ow').attr('checked')){
                $('#itinerary').submit();
            }
        });

    });

