function fill_regions()
{
    country_id = $('country').value;

    if (country_id > 0)
    {
        region_list = $('region');

        region_list.options[0]      = new Option("загрузка...", 0);
        region_list.selectedIndex   = 0;

        var url     = "/lib/region/getlist/";
        var data    = "country_id=" + country_id;
        var myAjax  = new Request({url: url, onComplete: bind_region_list, onRequest: initAd, onSuccess: closeAd, onFailure: fail}).send(data); 
    }
}

function bind_region_list(json)
{
    json = strToJson(json);
    list = json.list;

    fill_select("region", list, "--");
}

function fill_cities()
{
    region_id = $('region').value;

    if (region_id > 0)
    {
        city_list = $('city');

        city_list.options[0] = new Option("загрузка...", 0);
        city_list.selectedIndex = 0;

        var url     = "/lib/city/getlist/";
        var data    = "region_id=" + region_id;
        var myAjax  = new Request({url: url, onComplete: bind_city_list, onRequest: initAd, onSuccess: closeAd, onFailure: fail}).send(data); 
    }

}

function bind_city_list(json)
{
    json = strToJson(json);
    list = json.list;

    fill_select("city", list, "--");
}

function county_add_new()
{
    hide_element($('country'));
    hide_element($('a_new_country'));

    hide_element($('region'));
    hide_element($('a_new_region'));

    hide_element($('city'));
    hide_element($('a_new_city'));
    

    show_element($('new_country'));
    show_element($('a_country_list'));
    $('country_state').value    = 1;
    $('region_state').value     = 1;
    $('city_state').value       = 1;

    show_element($('new_region'));
    show_element($('new_city'));
}

function county_get_list()
{
    show_element($('country'));
    show_element($('a_new_country'));

    show_element($('region'));
    show_element($('a_new_region'));

    show_element($('city'));
    show_element($('a_new_city'));
    
    
    $('new_country').value      = '';
    $('country_state').value    = 0;
    $('region_state').value     = 0;
    $('city_state').value       = 0;

    hide_element($('new_country'));
    hide_element($('a_country_list'));

    hide_element($('new_region'));
    hide_element($('new_city'));
}

function region_add_new()
{
    hide_element($('region'));
    hide_element($('a_new_region'));

    hide_element($('city'));
    hide_element($('a_new_city'));
    

    show_element($('new_region'));
    show_element($('a_region_list'));
    $('region_state').value     = 1;
    $('city_state').value       = 1;

    show_element($('new_city'));
}

function region_get_list()
{
    show_element($('region'));
    show_element($('a_new_region'));

    show_element($('city'));
    show_element($('a_new_city'));
    
    
    $('new_region').value       = '';
    $('region_state').value     = 0;
    $('city_state').value       = 0;
    hide_element($('new_region'));
    hide_element($('a_region_list'));

    hide_element($('new_city'));
}


function city_add_new()
{
    hide_element($('city'));
    hide_element($('a_new_city'));  

    show_element($('new_city'));
    show_element($('a_city_list'));
    $('city_state').value = 1;
}

function city_get_list()
{
    show_element($('city'));
    show_element($('a_new_city'));
    
   
    $('new_city').value = '';
    $('city_state').value = 0;
    hide_element($('new_city'));
    hide_element($('a_city_list'));
}

function change_btn_status()
{
    if ($('btnRegister') != null && $('agree') != null)
    {
        $('btnRegister').disabled = !$('agree').checked;
    }
}

function bind_handlers()
{
    if ($('country') != null)
    {
        $('country').addEvent('change', fill_regions);
    }

    if ($('region') != null)
    {
        $('region').addEvent('change', fill_cities);
    }


    if ($('agree') != null)
    {
        if ($('btnRegister') != null)
        {
            $('btnRegister').disabled = true;
        }

        $('agree').addEvent('click', change_btn_status);
    }
}

addDOMLoadEvent(bind_handlers);

