
function select_append(data,sel_id,dots)
{
	if (dots)
	{
		var cdata = {"":"...Не важно..."};    	
	} else
	{
		var cdata = {};
	} 
	for (k in data)
        cdata[k] = data[k];	

    var html = "";
    //html = html+'<option value="-1">-select-</option>';
    for (k in cdata)
    	html = html+'<option value="' + k + '">' + cdata[k] + '</option>'; 

	$('#'+sel_id).empty();
	$('#'+sel_id).append(html);

}
var countrys_loaded = false; 
var regions_loaded = false;
var city_loaded = false;
function country(data)
{
	$('#id_city').empty();
	if (!countrys_loaded)
	{
	    select_append(data,'id_country',false);
		countrys_loaded = true;
	}
}

function region(data)
{
    select_append(data,'id_region',true);
	regions_loaded = true;
}

function region_init(data)
{	
	if (!regions_loaded)
	{
    	select_append(data,'id_region',true);
		regions_loaded = true;
	}
}

function city(data)
{
    select_append(data,'id_city',true);
}

function city_init(data)
{
	if (!city_loaded) {
		select_append(data, 'id_city', true);
		city_loaded = true;
	}
}
$(document).ready(function() 
	{
	$('#id_country').click(function(){$.getJSON("/ajax_countrys", {}, country);});
	$('#id_country').change(function(){$.getJSON("/ajax_regions/"+$('#id_country').val(), {}, region);});
	$('#id_region').change(function(){$.getJSON("/ajax_citys/"+$('#id_region').val(), {}, city);});
	$('#id_region').click(function(){$.getJSON("/ajax_regions/"+$('#id_country').val(), {}, region_init);});
	
	$('#id_city').click(function(){$.getJSON("/ajax_citys/"+$('#id_region').val(), {}, city_init);});
	$("#id_stype").change(function()
						 {if ($("#id_stype").val() == 2)
						 {
						 	$('#rooms_row').show();
						 }else{
						 	$('#rooms_row').hide();
						 }
						  });
	//alert('dd');
	if ($("#id_stype").val() == 2)
	 {
	 	$('#rooms_row').show();
	 }
	
	}
)
