function geo_block(state, prefix)
{
    var geo;

    geo = document.getElementById(prefix + 'geo_country');
    if (geo) geo.disabled = state;

    geo = document.getElementById(prefix + 'geo_city');
    if (geo) geo.disabled = state;

    geo = document.getElementById(prefix + 'geo_region');
    if (geo) geo.disabled = state;
}

function geo_update_selectbox(geo_id, selectbox_id, prefix, after_update)
{
    prefix = prefix ? prefix + '_' : '';

    var req = new MyJsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            if (req.responseText) {
                alert(req.responseText);
            }
            if (req.responseJS) {
                if (req.responseJS.regions) {
                    geo_clear_selectbox(prefix + 'geo_city');
                    geo_create_options(req.responseJS.regions, prefix + 'geo_region');
                } else if (req.responseJS.cities) {
                    geo_create_options(req.responseJS.cities, prefix + 'geo_city');
				} else if (req.responseJS.countries) {
					geo_clear_selectbox(prefix + 'geo_region');
					geo_clear_selectbox(prefix + 'geo_city');
					geo_create_options(req.responseJS.countries, prefix + 'geo_country');
				}
                geo_block(false, prefix);
            }
			if (after_update)
				after_update();
        }
    }
    req.caching = true;

    if (selectbox_id == prefix + 'geo_country') {

        if (geo_id == '') {
            geo_clear_selectbox(prefix + 'geo_region');
            geo_clear_selectbox(prefix + 'geo_city');
			if (after_update)
				after_update();
        } else {
            geo_block(true, prefix);
            geo_clear_selectbox(prefix + 'geo_region');
            geo_clear_selectbox(prefix + 'geo_city');
            req.open('GET', '/pub/geo_load.php', true);
            req.send({ country: geo_id});
        }

    } else if (selectbox_id == prefix + 'geo_region') {

        if (geo_id == '') {
            geo_clear_selectbox(prefix + 'geo_city');
			if (after_update)
				after_update();
        } else {
            geo_block(true, prefix);
            geo_clear_selectbox(prefix + 'geo_city');
            req.open('GET', '/pub/geo_load.php', true);
            req.send({ region: geo_id});
        }

    } else if (selectbox_id == prefix + 'geo') {
		geo_clear_selectbox(prefix + 'geo_country');
		geo_clear_selectbox(prefix + 'geo_region');
        geo_clear_selectbox(prefix + 'geo_city');
		req.open('GET', '/pub/geo_load.php', true);
        req.send({ geo: 1});
	}
}

function geo_create_options(geo, id)
{
    var selectbox = $(id);
    if (selectbox)
    {
		var size = $H(geo).size();
        selectbox.options[0].selected = true;
        selectbox.options.length = size + 1;

		if (size == 0) return;
		
        var j = 1;
        for (var i in geo) with (selectbox.options[j])
        {
            text  = geo[i];
            value = i;
            j++;
        }
    }
}

function geo_clear_selectbox(id)
{
    selectbox = document.getElementById(id);
    if (selectbox)
    {
        selectbox.options.length = 1;
        selectbox.options[0].selected = true; //for ie
    }
}
