$(document).ready(function() {
//  Выставляем все в дефолт
    if  (! $('#gorod').is('active') ) { $('#gorod').addClass("active"); }
    if  ( $('#oblast').is("active") ) { $('#oblast').removeClass("active"); }
    $('select[name=realtytype] option:first').attr('selected','yes');
    $('#element2').html('Этаж:')
    $('input[name=el2f]').val('1');
    $('select[name=curr] option:first').attr('selected','yes');

//  Обрабатываем нажатие регионов
    $('#oblast').click(function() {
        $('#region').html('<select name="region">\n<option value="Николаевская">Николаевская обл.</optoin>\n<option value="Херсонская">Херсонская обл.</optoin>\n</select>\n');
        $('#gorod').removeClass("active");
        $('#oblast').addClass("active");
    });
    
    $('#gorod').click(function() {
        $('#region').html('<input type="text" name="city" value="Николаев"/>\n');
        $('#oblast').removeClass("active");
        $('#gorod').addClass("active");
    });
//  Обрабатываем поля в зависимости от типа недвижимости
    $('select[name=type]').change(function() {
        var $this = $(this);
            if ($this.val() == 'apartments') {
                $('#element2').html('Этаж:')
                $('input[name=el2f]').val('1');
            }
            if ($this.val() == 'houses') {
                $('#element2').html('Зем. участок (сот.):')
                $('input[name=el2f]').val('0');
            }
            if ($this.val() == 'adminbuildings' || $this.val() == 'offices') {
                $('#element2').html('Зем. участок (сот.):')
                $('input[name=el2f]').val('0');                
            }
    });
// Проверяем задан ли тип недвижимости.
    $('input[name=submitsearch]').click(function() {
        if ($('select[name=type]').val() == '0') {
            alert ('Не задан тип искомой недвижимости');
            return false;
        } else {
            var $params = $('search').serialize();
            $.get('/include/search/', $params);
        }
    })
}); // Document.ready
