var region_param = {};

function update_region_info (){
	if (!document.getElementById('choose_region_city_input')){
		return;
	}

	cid = String(document.getElementById(region_param.city_id).value);
	if (cid != ''){
		letter_code = cid;
		if (cid != '0'){
			letter_code = document.getElementById('main_choose_cities_radio_' + cid).getAttribute("letter_code");
			change_radio('main_choose_cities', cid); 
		}
		change_radio('letter', letter_code); 
		if (cid != '0') show_cities(letter_code);
		document.choose_region.city.value=cid;
	}
}

function load_city_from_region (sel) {

	region_value = sel.options[sel.selectedIndex].value;
	document.getElementById("choose_region_region_input").value = region_value;
	country_value = document.choose_region.country.value;
	city_value = document.choose_region.city.value;

	any = document.getElementById('letter_label_0') ? 1 : 0;

	url = "/common/choose_region.php?act=load_regions&region=" + region_value + "&country=" + country_value + "&city=" + city_value + "&show_any_city=" + any;

	document.getElementById('choose_region_loading').style.display = 'inline';

	JsHttpRequest.query(
		url, {},

		function(result, errors) {
            //document.getElementById("debug").innerHTML = errors; 
			if (result){
				document.getElementById('choose_region_city_list').innerHTML = result;
			} else {
				alert(errors);
			}
			hide_div('choose_region_loading');
		},
		true  // do not disable caching
	);
}


function update_common_region(){

	el_city_id = document.getElementById(region_param.city_id);
	el_country_id = document.getElementById(region_param.country_id);
	el_region_id = document.getElementById(region_param.region_id);

	el_region_name = document.getElementById(region_param.region_name);

	country_value = document.choose_region.country.value;
	city_value = document.choose_region.city.value;
	region_value = document.choose_region.region.value;
	region_select = document.getElementById("choose_region_region_select");
	any = document.getElementById('letter_label_0') ? true : false;

	// Чтобы не могли указывать любой город, если мы не позволяем этого
	if (!any && city_value == '0')
	{
		document.choose_region.city.value = '';
		city_value = '';
	}

	if (city_value == '')
	{
		alert('Пожалуйста, выберите город.');
		return 0;
	}

	city_name = '';
	if (city_value != '0') {
		city_name = document.getElementById('main_choose_cities_label_' + city_value).innerHTML;
	}

	el_country_id.value = document.choose_region.country.value;
	el_region_id.value = document.choose_region.region.value;;
	el_city_id.value = city_value;

	var location_name = document.getElementById("choose_region_country_name").innerHTML;
	if (city_name) location_name += " | " + city_name;
	else if (el_region_id.value > 0) location_name += " | " + region_select.options[region_select.selectedIndex].text;
		
	el_region_name.innerHTML =  location_name;
	iBox.hideCommon();
	return 1;

}


function show_cities(letter_code){

	var x=document.getElementsByTagName("div");
	for (var i = 0; i<x.length; i++)
	{
		var el_id = x[i].id;
		if (el_id.match(/^choose_cities_.+$/))
		{
			hide_div(el_id);
		}
	}
	if (letter_code){
		document.getElementById("choose_cities_" + letter_code).style.display="block";
	}
	//if (el) el.value = "";	
}


function update_catalog_region(){

	if (!update_common_region()) return;

	var country = parseInt(document.getElementById(region_param.country_id).value);
	var city = parseInt(document.getElementById(region_param.city_id).value);
	var region = parseInt(document.getElementById(region_param.region_id).value);

	var href = "/" + region_param.catalog_type  + "_catalog/";
	if (city > 0) href += "city" + city;
	else if (region > 0) href += "region" + region;
	else href += "country" + country;
	
	location.href = href + ".html";
	
}


function update_main_region() {

	country_value = document.choose_region.country.value;
	region_value = document.choose_region.region.value;
	city_value = document.choose_region.city.value;

	//alert(city_value); return;
	url = "/common/choose_region.php?act=update_main_region&country=" + country_value + "&city=" + city_value + "&region=" + region_value;

	JsHttpRequest.query(
		url, // backend
		{
		},
		// Function is called when an answer arrives. 
		function(result, errors) {
            //document.getElementById("debug").innerHTML = errors; 
			if (result){
				document.getElementById('main_region_country').innerHTML = result.country_name;
				document.getElementById('main_region_region').innerHTML = !result.city_name && result.region_name ? (" | " + result.region_name) : "";
				document.getElementById('main_region_city').innerHTML = result.city_name ? (" | " + result.city_name) : "";
				iBox.hideCommon();
			} else {
				alert(errors);
			}
		},
		true  // do not disable caching
	);
}


