var allowRegion = false;

function wait ()
{
	byID('wait').style.display = 'block';
}

function okay ()
{
	byID('wait').style.display = 'none';
}

function showPlaces ()
{
	if(byID('town').value == 0)
	{
		byID('places').style.display = 'block';
		showRegions();
		return false;
	}
	
	return true;
}

function showRegions ()
{
	wait();
	byID('cities').style.display = 'none';
	byID('toregions').style.display = 'none';
	call ('places', 'regions');
}

function showCities (id)
{
	wait();
	byID('regions').style.display = 'none';
	call ('places', 'cities&id=' + id);
}

function selectCity (id, title)
{
	byID('places').style.display = 'none';
	var added = document.createElement('option');
	added.text = title; added.value = id;
	try { byID('town').add(added, null); } catch (e) { byID('town').add(added); }
	byID('town').selectedIndex = byID('town').length - 1;
	byID('town').onchange();
}

function fcallback (reply)
{
	var re = reply.split("\n");
	
	switch (re[0])
	{
		case 'regions':
			
			var buf = '';
			
			re[1] = re[1].split('|');
			
			for (r in re[1])
			{
				re[1][r] = re[1][r].split(':');
				if (allowRegion) {
					buf = buf + '<div><a href="#" onclick="selectCity(\''+ re[1][r][0] + '\', \'' + re[1][r][1] + '\');return false;">'
					+ re[1][r][1] + '</a><a href="#" onclick="showCities(\''+ re[1][r][0] + '\');return false;" class="next">→</a></div>';
				} else {
					buf = buf + '<div><a href="#" onclick="showCities(\''+ re[1][r][0] + '\');return false;">' + re[1][r][1] + '</a></div>';
				}
			}
			
			byID('regions').innerHTML = buf;
			byID('regions').style.display = 'block';
			
			okay();
			
			return true;
			
		break;
		
		case 'cities':
			
			var buf = '';
			
			re[1] = re[1].split('|');
			
			for (r in re[1])
			{
				re[1][r] = re[1][r].split(':');
				buf = buf + '<div><a href="#" onclick="selectCity(\''+ re[1][r][1] + '\', \'' + re[1][r][1] + '\');return false;">' + re[1][r][1] + '</a></div>';
			}
			
			byID('cities').innerHTML = buf;
			byID('cities').style.display = 'block';
			byID('toregions').style.display = 'inline';
			
			okay();
			
			return true;
			
		break;
		
		default: return false;
	}
}

document.write('<div id="places" style="display:none"><div id="frame"><div id="regions" class="rsel"></div><div id="cities" class="rsel"></div><a href="#" onclick="showRegions(); return false;" id="toregions">← Выбрать регион</a><img id="wait" src="design/images/progress.gif" alt="Загрузка"/></div><iframe id="iefix2"></iframe></div>');