var extended_search;
var search_city = '0_0_0';
var arCityIDWhereMetro = [4400, 4962];

if (typeof($) == 'undefined')
{
	function $(element)
	{
		var elements = new Array();
		
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
		
			if (arguments.length == 1)
				return element;
		
			elements.push(element);
		}
		
		return elements;
	}
}


// изменяет id метро в скрытом инпуте
function selectMetroStation(Select, hf)
{
    if (typeof(Select) == 'undefined') {
        return;
    }
	var hiddenField = $(hf);

	if (hiddenField)
	{
		if (Select.options)
			metro_id = Select.options[Select.selectedIndex].value
		else
			metro_id = Select;
		
		hiddenField.value = metro_id;
	}
}


function hasMetroByCityID(cityID)
{
	for (var i = 0, n = arCityIDWhereMetro.length; i < n; i++)
	{
		if (arCityIDWhereMetro[i] == cityID)
		{
			return true;
		}
	}
	
	return false;
}


function changeCity()
{
	if ((document.forms)&&(document.forms.msearch))
	{
		var Select = document.msearch.s_c;
		var Option = Select.options[Select.selectedIndex];
		
		if (Option.value == '1_1_1')
		{
			if (navigator.appName.indexOf("WebTV") != '-1')
				window.location = 'search_city.phtml';
			else
			{
				var arSC = search_city.split('_');
				var get = '';
				if ((arSC[0] > 0) && (arSC[1] > 0) && (arSC[2] > 0) && hasMetroByCityID(arSC[2])) get = 'country_id=' + arSC[0] + '&region_id=' + arSC[1] + '&action=city&city_id=' + search_city;
				if ((arSC[0] > 0) && (arSC[1] > 0) && (arSC[2] == 0 || !hasMetroByCityID(arSC[2]))) get = 'country_id=' + arSC[0] + '&action=region&region_id=' + [arSC[0], arSC[1]].join('_');
				
				wopen('search_city.phtml?'+get, 'SearchCity', 850, 400, 'yes', 'yes', 0);
			}
			Select[0].selected = true;
		}
		else {
			search_city = Option.value;
			var arSC = search_city.split('_');
			var MetroBox = $('MetroBox');
			
			if (MetroBox)
			{
				if (hasMetroByCityID(arSC[2]))
				{
					clearMetro();
				}
				else
					clearMetro(true);
			}
		}
		
	}

}


function SelectMetroLink()
{
	var Select = document.msearch.s_c;
	var arSC = search_city.split('_');
	var get = get = 'country_id=' + arSC[0] + '&region_id=' + arSC[1] + '&action=city&city_id=' + search_city;
	wopen('search_city.phtml?'+get, 'SearchCity', 850, 400, 'yes', 'yes', 0);
}


// аддит город в селект
function addSearchCity(vvalue, vtext, metro_id)
{
	selectMetroStation(metro_id, 's_m');

    var fcity = document.forms.msearch.elements.s_c;
    var flength = fcity.length;
    var fcurrent = flength-1;
    
    if (metro_id > 0)
    {
		var arVT = vtext.split(', ');
		
		$('MetroLink').innerHTML = 'м. ' + arVT[arVT.length-1];
		vtext = arVT.splice(0, arVT.length-1).join(', ');
		vvalue = vvalue.split('_').splice(0, 3).join('_');

		$('MetroBox').style.display = '';
    }
    else
    	clearMetro(true);
    
  	 for (var i = 0; i < flength; i++)
    {
        if (fcity.options[i].value == vvalue)
        {
            fcity.options[i].selected = true;
            return 0;
        }
    }
    
    fcity.options[fcurrent].text = vtext;
    fcity.options[fcurrent].value = vvalue;
    fcity.options[fcurrent].selected = true;
    
    fcity.options[flength] = new Option('Выбрать другой город...', '1_1_1');


    return true;
}


function checkGender()
{
	if (extended_search)
	{
		checkGenderExtended();
	}
}


function clearMetro(hide)
{
	$('s_m').value = 0;
	$('MetroLink').innerHTML = 'Выбрать станцию метро';
	$('MetroBox').style.display = (hide ? 'none' : '');
}

