function selectByValS(id, val)
{
	if (val) $('#'+id+' option').each(function(i, el){
		if (el.value==val) {
			$('#'+id).get(0).selectedIndex=i
		}
	});
}

function onChangeDistrictS()
{
	onChangeLocationS();
}

function onChangeLocationS(aLocation)
{
	$('#swindow_location').jqm({
		trigger: false
		,onShow: function(h){
			$('#scountry, #sregion, #scity, #sdistrict').attr({'disabled': 'disabled', 'selectedIndex': 0});
			$('#sdistrict').hide();
			h.w.show();
			var loc=aLocation.split('-');
			selectByValS('scountry', loc[0]);
			$('#scountry').removeAttr('disabled');
			onChangeLocationCountryS(loc);
		}
	}).jqmShow();
}

function onChangeLocationCountryS(loc)
{
	$('#sdistrict').hide();
	$('#sregion, #scity').attr({'selectedIndex': 0, 'disabled': 'disabled'});
	var country_id=parseInt($('#scountry').val());
	if (country_id<=0) return;
		$('#sregion')
		.empty()
		.append('<option value="0">'+lang['loading']+'</option>')
		.attr('selectedIndex', 0);	
	/*$.ajaxSetup({beforeSend: function(){
		$('#sregion')
		.empty()
		.append('<option value="0">'+lang['loading']+'</option>')
		.attr('selectedIndex', 0);
	}});*/
	$.get('/ajax/slocation/', {'country_id': country_id}, function(data) {
		$('#sregion').replaceWith(data);
		loc && selectByValS('region', loc[1]);
		$('#sregion').removeAttr('disabled');
		loc && loc[1] && onChangeLocationRegionS(loc);
	});
}

function onChangeLocationRegionS(loc)
{
	$('#sdistrict').hide();
	//$('#sdistrictLabel').hide();
	$('#scity').attr({'selectedIndex': 0, 'disabled': 'disabled'});
	var region_id=parseInt($('#sregion').val());
	if (region_id<=0) return;
		$('#scity')
		.empty()
		.append('<option value="0">'+lang['loading']+'</option>')
		.attr('selectedIndex', 0);	
	/*$.ajaxSetup({beforeSend: function(){
		$('#scity')
		.empty()
		.append('<option value="0">'+lang['loading']+'</option>')
		.attr('selectedIndex', 0);
	}});*/
	$.get('/ajax/slocation/', {'region_id': region_id}, function(data) {
		$('#scity').replaceWith(data);
		loc && selectByValS('city', loc[2]+'-'+loc[3]);
		$('#scity').removeAttr('disabled');
		loc && loc[2] && onChangeLocationCityS(loc);
	});
}

function onChangeLocationCityS(loc)
{
	var city_location=$('#scity').val().split('-'),
	city_id=parseInt(city_location[0]),
	is_districted=parseInt(city_location[1] || 0);
	$('#sdistrict').attr({'selectedIndex': 0, 'disabled': 'disabled'});

	if (is_districted)
	{
		$('#sdistrict').show();
		//$('#sdistrictLabel').show();
	}
	else
	{
		$('#sdistrict').hide();
		//$('#sdistrictLabel').hide();
		return;
	}
	if (city_id<=0) return;
		$('#sdistrict')
		.empty()
		.append('<option value="0">'+lang['loading']+'</option>')
		.attr('selectedIndex', 0);	
	/*$.ajaxSetup({beforeSend: function(){
		$('#sdistrict')
		.empty()
		.append('<option value="0">'+lang['loading']+'</option>')
		.attr('selectedIndex', 0);
	}});*/
	$.get('/ajax/slocation/', {'city_id': city_id}, function(data) {
		$('#sdistrict').replaceWith(data);
		loc && loc[3]>0 && $('#metro').val()>0 && selectByValS('district', $('#metro').val());
		$('#sdistrict').show().removeAttr('disabled');
		//$('#sdistrictLabel').show();
	});

}

function selectLocationS()
{
	if ($('#scountry')[0].selectedIndex>0)
	{
		var clone = $("<select id='myselectbox' name='geo' tabindex='2'><option value='0'>Другое...</option></select>");
		clone.append($('#scountry option[value='+$('#scountry').val()+']').clone().attr('value', function(i, el)
		{
			if($('#sregion')[0].selectedIndex==0)
			{
				this.selected='selected';
				this.defaultSelected=true;
			}
			else
			{
				this.selected=false;
				this.defaultSelected=false;
			}

			this.text += lang['any-city'];
			return this.value+'-0-0-0';
		}
		));

		if($('#sregion')[0].selectedIndex>0)
		{
			clone.append($('#sregion option[value='+$('#sregion').val()+']').clone().attr('value', function(i, el)
			{
				if($('#scity')[0].selectedIndex==0)
				{
					this.selected='selected';
					this.defaultSelected=true;
				}
				else
				{
					this.selected=false;
					this.defaultSelected=false;
				}

				this.text += lang['any-city'];
				return $('#scountry').val()+'-'+this.value+'-0-0';
			}
			));
			if($('#scity')[0].selectedIndex>0)
			{
				if ($('#sdistrict')[0].selectedIndex>0)
				{
					clone.append($('#scity option[value='+$('#scity').val()+']').clone().attr('value', function(i, el)
					{
						this.selected=false;
						this.defaultSelected=false;
						this.text += lang['any-district'];
						var cityval = this.value.split('-');
						return $('#scountry').val()+'-'+$('#sregion').val()+'-'+cityval[0]+'-0';
					}
					));
					clone.append($('#sdistrict option[value!=0]').clone().attr('value', function(i, el)
					{
						if (this.value==$('#sdistrict').val()) {
							this.selected='selected';
							this.defaultSelected=true;
						}
						else
						{
							this.selected=false;
							this.defaultSelected=false;
						}
						var cityval = $('#scity').val().split('-');
						return $('#scountry').val()+'-'+$('#sregion').val()+'-'+cityval[0]+'-'+this.value;
					}
					));
				}
				else
				{
					clone.append($('#scity option[value!=0]').clone().attr('value', function(i, el)
					{
						if (this.value==$('#scity').val())
						{
							this.selected='selected';
							this.defaultSelected=true;
						}
						else
						{
							this.selected=false;
							this.defaultSelected=false;
						}
						var cityval = this.value.split('-');
						return $('#scountry').val()+'-'+$('#sregion').val()+'-'+cityval[0]+'-0';
					}
					));
				}
			}
			else
			{
				clone.append($('#scity option[value!=0]').clone().attr('value', function(i, el)
				{
					this.selected=false;
					this.defaultSelected=false;
					var cityval = this.value.split('-');
					return $('#scountry').val()+'-'+$('#sregion').val()+'-'+cityval[0]+'-0';
				}
				));
			}
		}
		else
		{
			clone.append($('#sregion option[value!=0]').clone().attr('value', function(i, el)
			{
				this.selected=false;
				this.defaultSelected=false;
				this.text += lang['any-city'];
				return $('#scountry').val()+'-'+this.value+'-0-0';
			}
			));
		}
		$('#myselectbox').replaceWith(clone);
		var scityval = $('#scity').val().split('-');
		selectByValS('myselectbox', $('#scountry').val()+'-'+$('#sregion').val()+'-'+scityval[0]+'-'+$('#sdistrict').val());
		$.post('/ajax/geo_list/', {geo_search_list: $('#scountry').val()+'-'+$('#sregion').val()+'-'+scityval[0]+'-'+$('#sdistrict').val()});
		selectedIndex=$('#myselectbox')[0].selectedIndex;
		location_id=$('#myselectbox').val();
		$('#myselectbox_input').remove();
		$('#myselectbox_container').remove();
		$('#myselectbox').selectbox();
		$('#scountry option[value=0]').attr('selected', 'selected');
		$('#sregion option[value=0]').attr('selected', 'selected');
		$('#scity option[value=0]').attr('selected', 'selected');
		$('#sdistrict option[value=0]').attr('selected', 'selected');
		$('#swindow_location').jqmHide();
	}
	else $('#serr_block').text('Внимание! Для успешного завершения операции Вам необходимо выбрать страну!');
	return true;
}

function changeDistrictS(onoff)
{
	var act=onoff;
	if (typeof(act)=='undefined') {
		act=$('#sdistrict').val()>0;
	}
	if (act) {
		if (typeof(onoff)=='undefined') {
			$('#metro').val($('#sdistrict').val());
			var si=$('#sdistrict')[0].selectedIndex;
			$('#metro_link').text($('#sdistrict option').eq(si).text());
		}
		$('#metro_box').show();
	} else {
		if (typeof(onoff)!='undefined') {
			$('#metro_box').hide();
		} else {
			var loc=location_id.split('-');
			if (!loc[3] || loc[3]<=0) {
				$('#metro_box').hide();
			} else {
				$('#metro_box').show();
			}
			$('#metro').val(0);
			$('#metro_link').text(lang['select-district']);
		}
	}
}
