
var td_strana = _gel('td_strana');
var td_oblast = _gel('td_oblast');
var td_gorod = _gel('td_gorod');
var td_raion = _gel('td_raion');
var region = _gel('region');
var strana, oblast, gorod, raion;

var strana_value_def=301;
var oblast_value_def=0;	//	Алматы 665
var gorod_value_def=0;
var raion_value_def=0;
var geo_select_id;
var search_form;
var changes = false;
function change_region(t){

	search_form = _gel('search_form');
	
	if (search_form.style.display!='none') {
		search_form.style.display='none';
		_gel('geo_table').style.display=IE?'block':'table';
		
	} else {
		search_form.style.display='block';
		_gel('geo_table').style.display='none';
	}
	t.blur();
	geo_select();
	if (changes) {
		document.location = '?rgn='+_m([raion_value, gorod_value, oblast_value, strana_value]);
		return false;
	}
	return changes;
}

function _m(a) {
	for(i in a) {
		if (a[i] > 0) {
			return a[i];
		}
	}
}

function getValuesFromCookies(){
	strana_value = getCookie('strana_value');	if (typeof(strana_value)=='undefined') strana_value = strana_value_def;
	oblast_value = getCookie('oblast_value');	if (typeof(oblast_value)=='undefined') oblast_value = oblast_value_def;
	gorod_value  = getCookie('gorod_value');	if (typeof(gorod_value) =='undefined') gorod_value = gorod_value_def;
	raion_value  = getCookie('raion_value');	if (typeof(raion_value) =='undefined') raion_value = raion_value_def;
}

function geo_select(){
	
	
	geo_select_id = _gel('geo_select_id');
	
	getValuesFromCookies();
	
	strana_selected = false;
	oblast_selected = false;
	gorod_selected = false;
	raion_selected = false;
	
	if (typeof(geo[strana_value])=='undefined') return;
	
	html='';
	
	if (raion_value>0) {
		html='';
		raion_selected = true;
	} else if (gorod_value>0){
		gorod_selected = true;
		for(i in geo[strana_value][oblast_value][gorod_value]){
			i = geo[strana_value][oblast_value][gorod_value][i];
			html+='<br><nobr><input type="Radio" onclick="changes=true; setCookie(\'raion_value\','+i+',365,\'/\');geo_select();return;" name="s" id="s'+i+'" '+(raion_value==i?' checked':'')+'><label for="s'+i+'">'+geo_values[strana_value][i]+'</label></nobr>';
			if (raion_value==i) { raion_selected = true; gorod_selected = false;}
		}
	} else if (oblast_value>0){
		oblast_selected = true;
		for(i in geo[strana_value][oblast_value]){
			html+='<br><nobr><input type="Radio" onclick="changes=true; setCookie(\'gorod_value\','+i+',365,\'/\');geo_select();return;" name="s" id="s'+i+'" '+(gorod_value==i?' checked':'')+'><label for="s'+i+'">'+geo_values[strana_value][i]+'</label></nobr>';
			if (gorod_value==i) { gorod_selected = true; oblast_selected = false; }
		}
	} else if (strana_value>0){	
		strana_selected = true;
		for(i in geo[strana_value]){
			html+='<br><nobr><input type="Radio" onclick="changes=true; setCookie(\'oblast_value\','+i+',365,\'/\');geo_select();return;" name="s" id="s'+i+'" '+(oblast_value==i?' checked':'')+'><label for="s'+i+'">'+geo_values[strana_value][i]+'</label></nobr>';
			if (oblast_value==i) { oblast_selected = true; strana_selected = false; }
		}
	}
	
	geo_select_html = '';
	if (strana_value>0)	geo_select_html += '<nobr><input type="Radio" name="s" id="s0" onclick="changes=true; setCookie(\'oblast_value\',0,365,\'/\');setCookie(\'gorod_value\',0,365,\'/\');setCookie(\'raion_value\',0,365,\'/\');geo_select();return;"'+(strana_selected?' checked':'')+'><label for="s0">'+geo_values[strana_value][strana_value]+'</label></nobr><br>';
	if (oblast_value>0)	geo_select_html += '<nobr><input type="Radio" name="s" id="o0" onclick="changes=true; setCookie(\'gorod_value\',0,365,\'/\');setCookie(\'raion_value\',0,365,\'/\');geo_select();return;"'+(oblast_selected?' checked':'')+'><label for="o0">'+geo_values[strana_value][oblast_value]+'</label></nobr><br>';
	if (gorod_value>0)	geo_select_html += '<nobr><input type="Radio" name="s" id="g0" onclick="changes=true; setCookie(\'raion_value\',0,365,\'/\');geo_select();return;"'+(gorod_selected?' checked':'')+'><label for="g0">'+geo_values[strana_value][gorod_value]+'</label></nobr><br>';
	if (raion_value>0)	geo_select_html += '<nobr><input type="Radio" name="s" id="r0"'+(raion_selected?' checked':'')+'><label for="r0">'+geo_values[strana_value][raion_value]+'</label></nobr><br>';
	
	if (html) {
		geo_select_html += html;
	} 
	
	geo_select_id.innerHTML = geo_select_html;
		
	setRegion();

}

function setRegion(){
	
	getValuesFromCookies();
	
	region_html = geo_values[strana_value][strana_value];
	
		 if (raion_value>0) region_html = geo_values[strana_value][gorod_value] + ', ' + geo_values[strana_value][raion_value];
	else if (gorod_value>0) region_html += ', ' + geo_values[strana_value][gorod_value];
	else if (oblast_value>0)  region_html = geo_values[strana_value][oblast_value];
	
	_gel('region').innerHTML = region_html;
}


setRegion();



