$(document).ready(function () {
    $('#country_id').change(function () {
        var country_id = $(this).val();
        if (country_id == '0') {
            $('#region_id').html('');
            $('#region_id').attr('disabled', true);
            return(false);
        }
        $('#region_id').attr('disabled', true);
        $('#region_id').html('<option>загрузка...</option>');
        var url = 'index.php?tpl=main/region';
        $.get(
            url,
            "country_id=" + country_id,
            function (result) {
                if (result.type == 'error') {
                    alert('error');
                    return(false);
                }
                else {
                    var options = '';
                    $(result.regions).each(function() {
                        options += '<option value="' + $(this).attr('id') + '">' + $(this).attr('title') + '</option>';
                    });
                    $('#region_id').html(options);
                    $('#region_id').attr('disabled', false);
                }
            },
            "json"
        );
    });
	
    $('#region_id').change(function () {
        var region_id = $(this).val();
        if (region_id == '0') {
            $('#place_id').html('');
            $('#place_id').attr('disabled', true);
            return(false);
        }
        $('#place_id').attr('disabled', true);
        $('#place_id').html('<option>загрузка...</option>');
        var url = 'index.php?tpl=main/region';
        $.get(
            url,
            "country_id=" + region_id,
            function (result) {
                if (result.type == 'error') {
                    alert('error');
                    return(false);
                }
                else {
                    var options = '';
                    $(result.regions).each(function() {
                        options += '<option value="' + $(this).attr('id') + '">' + $(this).attr('title') + '</option>';
                    });
                    $('#place_id').html(options);
                    $('#place_id').attr('disabled', false);
                }
            },
            "json"
        );
    });
    $('#classificator_id').change(function () {
        var region_id = $(this).val();
        if (region_id == '0') {
            $('#classificator_type').html('');
            $('#classificator_type').attr('disabled', true);
            return(false);
        }
        $('#classificator_type').attr('disabled', true);
        $('#classificator_type').html('<option>загрузка...</option>');
        var url = 'index.php?tpl=main/rielty';
        $.get(
            url,
            "classificator_id=" + region_id,
            function (result) {
                if (result.type == 'error') {
                    alert('error');
                    return(false);
                }
                else {
                    var options = '';
                    $(result.regions).each(function() {
                        options += '<option value="' + $(this).attr('id') + '">' + $(this).attr('title') + '</option>';
                    });
                    $('#classificator_type').html(options);
                    $('#classificator_type').attr('disabled', false);
                }
            },
            "json"
        );
		var orenda = document.getElementById('orenda');
		if ($(this).val() == 2) {orenda.value=81;}
		if ($(this).val() == 3) {orenda.value=146;}
		if ($(this).val() == 4) {orenda.value=214;}
    });
});
