var Types = new Array();
var PicRed = false;

$A(document.getElementsByTagName("script")).findAll( 
	function(s)
	{
		return (s.src && s.src.match(/citySelect\.js(\?.*)?$/))
    }
).each( 
	function(s)
	{
		var path = s.src.replace(/citySelect\.js(\?.*)?$/,'');
		var includes = s.src.match(/\?.*type=([a-z,]*)/);

		(includes ? includes[1] : 'search,reg').split(',').each(
			function(include) 
			{
				//include = 'search';
				Types[0] = '04';
				Types[1] = '05';
				if (include == 'reg')
				{
					Types[0] = '02';
					Types[1] = '03';
				}
			}
		);
	}
);

Event.observe('country','change',   function(event){ rebuild('country',Types[0],'region');});
Event.observe('region',	'change',   function(event){ rebuild('region',Types[1],'city');});
Event.observe('city',	'change',	function(event){ choose(); } );

function city_select()
{
	if (arguments.length)
	{
		PicRed = true;
	}
	if ($('city_id').value == 'search')
	{
		$('CityDiv').style.display = 'inline';
		return 1;
	}
}
			
function city_unselect()
{
	if ( $('city_id').value == 'search')
	{
		$('city_id').options[0].selected = true;
	}
	$('CityDiv').style.display = 'none';
}

function choose()
{
	if ($('city').value == 0)
	{
		return;
	}
	if ($('city').value == '-1')
	{
		any_city();
		return;
	}
	country = $('country').value;
	region = $('region').value;
	city = $('city').value;
	val = country + "," + region + "," + city;
	country_name = $('country')[$('country').selectedIndex].text;
	City_Name = $('city')[$('city').selectedIndex].text;
	txt = country_name + ", " + City_Name;
	RebuildCity(txt,val);
	return;
}


function rebuild(place,tip,child)
{
	if (place == 'country')
	{
		$('rid').style.display = 'inline';
		$('city').length = 0;
		$('city').disabled = true;
		$('city').style.width = 'auto';

		if ($('country').value != 0)
		{
			$('region').disabled = false;
		}
		else
		{
			$('region').length = 0;
			$('region').disabled = true;
			$('region').style.width = 'auto';
			$('rid').style.display = 'none';
		}
	}
	if (place == 'region')
	{
		$('cid').style.display = 'inline';
		if ($('region').value != 0)
		{
			if ($('region').value == '-1')
			{
				return any_region();
			}

			$('city').disabled = false;
		}
		else
		{
			$('city').length = 0;
			$('city').disabled = true;
			$('city').style.width = 'auto';
			$('cid').style.display = 'none';
		}
	}

	if ($(place).value != 0)
	{
		new Ajax.Request
		(
			BaseUrl, 
			{
				parameters: "act=city&tip="+tip+"&"+place+"="+$F(place), 
				onComplete:  function(data) { Load(data,child);		}
			}
		);
	}
}

function any_region()
{

	country = $('country').value;
	region = 0;
	city = 0;
	val = country + "," + region + "," + city;
	txt = $('country')[$('country').selectedIndex].text;

	$('city').length = 0;
	$('city').disabled = true;
	$('city').style.width = 'auto';
	$('cid').style.display = 'none';

	RebuildCity(txt,val);
	return;
}

function any_city()
{

	country = $('country').value;
	region = $('region').value;;
	city = 0;
	val = country + "," + region + "," + city;
	country_name = $('country')[$('country').selectedIndex].text;
	region_name  = $('region')[$('region').selectedIndex].text;
	txt = country_name + ", " + region_name;
	RebuildCity(txt,val);
	return;
}


function Load(data,place)
{
	$('cid').style.display = 'none';
	$('rid').style.display = 'none';
	Response = data.responseText.split('|');
	$(place).length = 0;
	
	for (i=0; i<Response.length; i++)
	{
		opt = Response[i].split(',');
		NewSelect(place,opt[0],opt[1]);
		if (opt[2])
		{
			eval("$('"+place+"')["+i+"]."+opt[2]);
		}
	}
}

function NewSelect(id,val,txt)
{
		if (document.createElement)
		{
			if (txt && val)
			{
				var newOpt = document.createElement("OPTION");
				newOpt.text = txt;
				newOpt.value = val;
				($(id).options.add) ? $(id).options.add(newOpt) : $(id).add(newOpt, null);
			}
		}
		else
		{
			$(id).options[i] = new Option(txt, val, false, false);
		}
}

function RebuildCity(txt,val)
{
	city_unselect();

	len = $('city_id').length - 1;
	$('city_id')[len].value = val;
	$('city_id')[len].text = txt;
	NewSelect('city_id','search','Поискать другой...');
	$('city_id')[len].selected = true;
	$('city_id')[len].style.color = '';
	$('city_id')[len+1].style.color = '#df0505';

	if (PicRed != false)
	{
		picred('');
	}
}

function NewSelect(id,val,txt)
{
		if (document.createElement)
		{
			if (txt && val)
			{
				var newOpt = document.createElement("OPTION");
				newOpt.text = txt;
				newOpt.value = val;
				($(id).options.add) ? $(id).options.add(newOpt) : $(id).add(newOpt, null);
			}
		}
		else
		{
			$(id).options[i] = new Option(txt, val, false, false);
		}
}