jQuery(function($) {
    $('#ruth_industry')
        .val('')
        .change(function() {
            var url = $('#ruth_ajaxurl').val();
            if (!url) {
                return true;
            }

            var industry_id = $(this).val();
            if (!industry_id) {
                return true;
            }

            var url = url + industry_id;

            $.ajax({
                url: url,
                data: { 'portalId': 'klima-sucht-schutz' },
                dataType: 'jsonp',
                success: function(data) {
                    if (data.html.length) {
                        $('#ruth_category').html(data.html).removeAttr('disabled');
                    } else {
                        $('#ruth_category').html('<option value="">&nbsp;</option>').attr('disabled', 'disabled');
                    }
                },
                error: function(obj, textStatus, errorThrown) {
                    // error handling
                }
            });

            return true;
        });

    $('form:not(.ruth_val_form)').find('#ruth_category').attr('disabled', 'disabled');

    $('#ruth_teaser dt').click(function() {
        $(this).parent().find('.ruth_active').removeClass('ruth_active');
        $(this).addClass('ruth_active').next().addClass('ruth_active');
    });

    $('.ruth_val_form').submit(function() {
        var emptyPattern = /^\s*$/;
        var $zip = $(this).find('input.ruth_zip');

        if (emptyPattern.test($zip.val())) {
            alert('Bitte geben Sie eine Postleitzahl an.');
            $zip.focus();
            return false;
        }

        var $industry = $(this).find('#ruth_industry');
        if ($industry.length && emptyPattern.test($industry.val())) {
            alert('Bitte w' + String.fromCharCode(228) + 'hlen Sie eine Branche aus.');
            $industry.focus();
            return false;
        }

        var $category = $(this).find('#ruth_category');
        if ($category.length && emptyPattern.test($category.val())) {
            alert('Bitte w' + String.fromCharCode(228) + 'hlen Sie eine Qualifikation aus.');
            $category.focus();
            return false;
        }
    });

});

