function DemoSelect2() {
        checkCatGenreList('#form_category_id', '#form_cat_genre_id');
        $('#form_category_id').select2(select2OptPH);
        $('#form_cat_genre_id').select2(select2OptMS);
        $('#form_is_series').select2(select2Opt);
}

function yelp_step_1() {
    // Add custom validation rule
    var customRuleObj = {
        name : 'compare_number',
        errorMessage : "",
        errorMessageKey: 'badCompareNumber'
    };

    customRuleObj.validatorFunction = function(value, el, config, language, form) {
        var compElem = $(el).data('validation-compare-element');
        var compOperation = $(el).data('validation-compare-operation') ? $(el).data('validation-compare-operation') : "<=";
        var compRegExpStr = $(el).data('validation-regexp');
        var compElemValidationOptional = $(compElem).data('validation-optional') || false;

        if (compRegExpStr) {
            var compRegExp = new RegExp(compRegExpStr, 'ig');
            if (!compRegExp.test($(el).val())){
                this.errorMessage = language.badCustomVal;
                return false;
            }
        }

        if (compElem && !$(compElem).is(":disabled")) {
            if (!$(compElem).val() && compElemValidationOptional) {
                return true;
            }
            var val = parseFloat($(el).val());
            var compVal = parseFloat($(compElem).val());
            if ((compOperation == '<=' && !(val <= compVal)) || (compOperation == '>=' && !(val >= compVal))) {
                this.errorMessage = "{{ 'End date can not be less than the start date'|trans }}";
                return false;
            }
        }

        return true;
    };

    $.formUtils.addValidator(customRuleObj);

    $.validate(conf);

    LoadSelect2Script(DemoSelect2);

    $(document).on('change', '#form_category_id', function () {
        checkCatGenreList('#form_category_id', '#form_cat_genre_id');
        $('#form_category_id, #form_cat_genre_id').validate(function(valid, elem) {
            if (valid) {
                conf.unhighlight(elem, 'error');
            } else {
                conf.highlight(elem, 'error');
            }
        });
    });

    $(document).on('change', '#form_cat_genre_id', function () {
        $(this).validate(function(valid, elem) {
            if (valid) {
                conf.unhighlight(elem, 'error');
            } else {
                conf.highlight(elem, 'error');
            }
        });
    });

    $(document).on('blur focus', '#form_name, #form_year', function (e) {
        var _this = $('#form_name');
        _this.next('div').removeClass('bg-danger').html('&nbsp;').hide();

        if ($.trim(_this.val()) == '' && $.trim($('#form_o_name').val()) == '') {
            $('#form_kinopoisk_id').removeAttr('readonly');
        } else {
            $('#form_kinopoisk_id').attr('readonly', 'readonly');
        }
        if ($.trim(_this.val()) == '') {
            return;
        }

        ajaxPostSend('{{ app.request_context.baseUrl }}/{{ app.controller_alias }}/check-name', {name: _this.val(), year: $.trim($('#form_year').val()), 'id<>': $("#form_id").val()}, false, false);

    });

    if ($.trim($('#form_o_name').val()) == '') {
        $('#form_kinopoisk_id').removeAttr('readonly');
    } else {
        $('#form_kinopoisk_id').attr('readonly', 'readonly');
    }

    $(document).on('change keyup', '#form_o_name', function (e) {
        if ($.trim($(this).val()) == '' && $.trim($('#form_name').val()) == '') {
            $('#form_kinopoisk_id').removeAttr('readonly');
        } else {
            $('#form_kinopoisk_id').attr('readonly', 'readonly');
        }
        checkAutocompliteButton('#form_o_name', '#form_kinopoisk_id');
    });

    $(document).on('change keyup', '#form_kinopoisk_id', function (e) {
        checkAutocompliteButton('#form_kinopoisk_id', '#form_o_name');
    });

    $(document).on('change', '[id^="form_autocomplete_provider_"]', function(e){

        var label = $('label[for="form_autocomplete_provider"]');

        label.text($(this).val() == 'tmdb' ? "{{ 'TMDB ID'|trans }}" : "{{ 'Kinopoisk ID'|trans }}" );
        $('i.i-hint', label.next()).attr('title', $(this).val() == 'tmdb' ? "{{ 'ID of movie on the TMDB'|trans }}": "{{ 'ID of movie on the Kinopoisk'|trans }}" ).tooltip('fixTitle').tooltip('setContent');
    });

    $("#autocomplete_button_container").on('click', 'button', function (e) {
        e.stopPropagation();
        e.preventDefault();

        var sendData = {
            data: 0,
            provider: $("[id^='form_autocomplete_provider']").filter(':checked').val() || false,
            type: ($("#form_is_series").val() === '1'?'tv':'movie')
        };

        var searchBy;

        if (($('#form_o_name').val() && $('#form_o_name').val() != $('#form_o_name').data('oldvalue'))) {
            $('#form_o_name').data('oldvalue', $('#form_o_name').val());
            sendData.data = $('#form_o_name').val();
            searchBy = 'name';
        } else {
            sendData.data = ($('#form_kinopoisk_id').val() ? $('#form_kinopoisk_id').val() : $('#form_o_name').val());
            searchBy = ($('#form_kinopoisk_id').val() ? 'id' : 'name');
        }


        if (!sendData.provider || sendData.provider === ''){
            JSErrorModalBox({'msg': "{{ 'First, select the search service'|trans }}"});
        } else if (!sendData.data || sendData.data === '') {
            JSErrorModalBox({'msg': "{{ 'First enter the name or the movie id'|trans }}"});
        } else {
            if ($("#modalbox > div").css('display') != 'none') {
                $("#modalbox > div").hide();
            }
            $("#modalbox").show();
            JSshowModalBox();
            ajaxPostSend('{{ app.request_context.baseUrl }}/{{ app.controller_alias }}/get-kinopoisk-info-by-' + searchBy, sendData, false, false, true);
        }
    });

    checkIsSeries();

    $(document).on("change", "#form_is_series", function () {
        checkIsSeries();
    });

    var currID = $("#form_id").val(), currOName = $("#form_o_name").val();
    if (!currID && currOName) {
        var addMovieData = localStorage.getItem('add_movie_data') || '[]';
        addMovieData = JSON.parse(addMovieData);
        for (var i in addMovieData){
            if (addMovieData[i] instanceof Object && addMovieData[i].o_name && addMovieData[i].o_name === currOName) {
                $("#form_id").val(addMovieData[i].id);
                break;
            }
        }
    }
}

var cat_genre_list = {
    {% for key, g_item in app['preparedCatGenre'] %}
    '{{ key }}': [{% for g_in_item in g_item %}{
        'id': '{{ g_in_item['id'] }}',
        'title': '{{ g_in_item['title'] }}'
    }{% if not loop.last %}, {% endif %}{% endfor %}]{% if not loop.last %}, {% endif %}
    {% endfor %}
};

var category_list = {
    {% for g_item in app['catGenres'] %}
    '{{ g_item['id'] }}': '{{ g_item['category_alias'] }}'{% if not loop.last %}, {% endif %}
    {% endfor %}
};

function checkCatGenreList(parent, child) {
    var selAlias = category_list[$(parent).val()];
    var selChildData = $(child).val();
    $(child).empty();
    if (typeof(cat_genre_list[selAlias]) != 'undefined') {
        $.each(cat_genre_list[selAlias], function (index, value) {
            $(child).append('<option value="' + value.id + '" ' + ($.inArray(value.id, selChildData) != -1 ? 'selected="selected"' : '') + '>' + value.title + '</option>');
        });
    }
    $(child).select2(select2OptMS);
}

function checkAutocompliteButton(master, slave) {
    var buttonContainer = $(slave).parent('div').next();
    if ($.trim($(master).val()) != '') {
        buttonContainer.find('button').attr('disabled', 'disabled').removeClass('btn-success');
        buttonContainer.find('i').removeClass('txt-success').removeClass('fa-check').addClass('txt-danger').addClass('fa-ban');
    } else {
        buttonContainer.find('button').removeAttr('disabled').addClass('btn-success');
        buttonContainer.find('i').removeClass('txt-danger').removeClass('fa-ban').addClass('txt-success').addClass('fa-check');
    }
}

function checkIsSeries(){
    if ($("#form_is_series").val() === '1') {
        $(".uniseries").removeClass('uniseries').addClass('serial');
        $("#form_year").addClass("add-video-year");
        $("#form_year_end").removeAttr("disabled");
    } else {
        $(".serial").removeClass('serial').addClass('uniseries');
        $("#form_year").removeClass("add-video-year");
        $("#form_year_end").attr("disabled", "disabled");
    }
}