function array_contains(arr, val)
{
    for (var i=0; i<arr.length; i++)
    {
        if (arr[i] == val)
            return true;
    }
    
    return false;
}

function station_img(num)
{
	var comboid = '#select_station_' + num;
	var selectedValue = $(comboid).attr('selectedIndex');
	if (selectedValue == 0)
	{
		$('#station_remove_' + num).hide();
		return;
	}
	
	$('#station_remove_' + num).show();	
}

function district_img(num)
{
	var comboid = '#select_district_' + num;
	var selectedValue = $(comboid).attr('selectedIndex');
	if (selectedValue == 0)
	{
		$('#district_remove_' + num).hide();
		return;
	}
	
	$('#district_remove_' + num).show();	
}

function hide_controls()
{
        $('#select_station_0').html("<option value=\"0\">Выберите город</option>");
        $('#select_station_1').html("");
        $('#select_station_2').html("");
        $('#select_station_3').html("");
        
        $('#divStation1').hide();
        $('#divStation2').hide();
        $('#divStation3').hide();
        
        $('#select_district_0').html("<option value=\"0\">Выберите город</option>");
        $('#select_district_1').html("");
        $('#select_district_2').html("");
        $('#select_district_3').html("");
        $('#divDistrict1').hide();
        $('#divDistrict2').hide();
        $('#divDistrict3').hide();
}

function hide_control()
{
        $('#select_station').html("<option value=\"0\">Выберите город</option>");
        $('#select_district').html("<option value=\"0\">Выберите город</option>");
}

function show_station_controls(json, select_current, comma_seperated_station_ids)
{
   if ($('#select_city').val() == '0')
   return;
    
   var result_html_for_first = "";
   var result_html_for_others = "";                    
   var station_ids = new Array();
   
   if (select_current) 
        station_ids = comma_seperated_station_ids;

   for (var i=0; i<json.stations.length; i++)
    {
        if (json.stations[i].id == 0)
        {
            result_html_for_first += '<option value="' + json.stations[i].id + '" ' + 'id="id' + i + '"' + '>' + 'Не имеет значения' + '</option>';
            result_html_for_others += '<option value="' + json.stations[i].id + '" ' + 'id="id' + i + '"' + '>' + 'Не указано' + '</option>';
        } else {
            result_html_for_first += '<option value="' + json.stations[i].id + '" id="id' + i + '"' + '>' + json.stations[i].name + '</option>';
            result_html_for_others += '<option value="' + json.stations[i].id + '" id="id' + i + '"' + '>' + json.stations[i].name + '</option>';
        }
    }

    $('#select_station_0').html(result_html_for_first);
    $('#select_station_1').html(result_html_for_others);
    $('#select_station_2').html(result_html_for_others);
    $('#select_station_3').html(result_html_for_others);
        
    for (var i=0; i<station_ids.length; i++)
    {
		var selectedid = '#id'+station_ids[i];
		var comboid = '#select_station_' + i;
		var divid = '#divStation' + i;
		var imgid = '#station_remove_'  + i;

		var asynccommand = "$(\"" + comboid + " option[value='" + station_ids[i] + "']\").attr(\"selected\", \"selected\");";
		asynccommand = asynccommand + "$('"+divid+"').show();";

		if (station_ids[i] == 0)
		  asynccommand = asynccommand + "$('"+imgid+"').hide();";
		else
  		  asynccommand = asynccommand + "$('"+imgid+"').show();";
		
		setTimeout(asynccommand,1);
    }
    
    $('#divStation' + station_ids.length).show();
}


function show_station_control(json, select_current, station_id)
{
   if ($('#select_city').val() == '0')
    return;
    
   var result_html = "";

   for (var i=0; i<json.stations.length; i++)
    {
        if (json.stations[i].id == 0)
        {
            result_html += '<option value="' + json.stations[i].id + '" ' + 'id="id' + i + '"' + '>' + 'Не имеет значения' + '</option>';
        } else {
            result_html += '<option value="' + json.stations[i].id + '" id="id' + i + '"' + '>' + json.stations[i].name + '</option>';
        }
    }

    $('#select_station').html(result_html);

    if (station_id != -1)
    {
		var asyncCommand = "$(\"#select_station option[value='" + station_id + "']\").attr(\"selected\", \"selected\");";
		setTimeout(asyncCommand, 1);
    }
}

function remove_station_control(num)
{
	var lastdiv;
	var lastdivindex = 0;
	var lastdivvalue;
	
	for (var i=num; i<4; i++)
	{
		var comboid = '#select_station_' + i;
		
		if (i<3)
		{
			var nextcomboid = '#select_station_' + (i + 1);
			var next_selectedvalue = $(nextcomboid).attr('selectedIndex');
			setTimeout("$('" + comboid + "').attr('selectedIndex', '"+next_selectedvalue+"');" + " station_img('"+i+"');",1);
	    }

		if ($('#divStation'+i).is(':visible'))
		{
			lastdiv = '#divStation' + i;
			lastdivindex = i;
		}
	}
	
	var comboid = '#select_station_' + lastdivindex;
	if (lastdivindex > 0)
	{
			if (num != lastdivindex && (lastdivindex != 3 || $(comboid).attr('selectedIndex') == 0))
			{
				$(lastdiv).hide();
			}
			
			setTimeout("$('" + comboid + "').attr('selectedIndex', '0');" + " station_img('"+lastdivindex+"');",1);
	}
}

function show_district_controls(json, select_current, comma_seperated_station_ids)
{
    if ($('#select_city').val() == '0')
    return;
    
   var result_html_for_first = "";
   var result_html_for_others = "";
   var district_ids = new Array();
   
    if (select_current) 
        district_ids = comma_seperated_station_ids;
        
   for (var i=0; i<json.districts.length; i++)
    {
        if (json.districts[i].id == 0)
        {
            result_html_for_first += '<option value="' + json.districts[i].id + '" id="id' + i + '"' + '>' + 'Не имеет значения' + '</option>';
            result_html_for_others += '<option value="' + json.districts[i].id  + '" id="id' + i + '"' + '>' + 'Не указано' + '</option>';
        } else {
            result_html_for_first += '<option value="' + json.districts[i].id + '" id="id' + i + '"' + '>' + json.districts[i].name + '</option>';
            result_html_for_others += '<option value="' + json.districts[i].id + '" id="id' + i + '"' +  '>' + json.districts[i].name + '</option>';
        }
    }
    
    $('#select_district_0').html(result_html_for_first);
    $('#select_district_1').html(result_html_for_others);
    $('#select_district_2').html(result_html_for_others);
    $('#select_district_3').html(result_html_for_others);
    
    for (var i=0; i<district_ids.length; i++)
    {
		var selectedid = '#id'+district_ids[i];
		var comboid = '#select_district_' + i;
		var divid = '#divDistrict' + i;
		var imgid = '#district_remove_'  + i;

		var asynccommand = "$(\"" + comboid + " option[value='" + district_ids[i] + "']\").attr(\"selected\", \"selected\");";		
		asynccommand = asynccommand + "$('"+divid+"').show();";

		if (district_ids[i] == 0)
		  asynccommand = asynccommand + "$('"+imgid+"').hide();";
		else
  		  asynccommand = asynccommand + "$('"+imgid+"').show();";
		
		setTimeout(asynccommand,1);
    }
    
    $('#divDistrict' + district_ids.length).show();
}


function remove_district_control(num)
{
	var lastdiv;
	var lastdivindex = 0;
	var lastdivvalue;
	
	for (var i=num; i<4; i++)
	{
		var comboid = '#select_district_' + i;
		
		if (i<3)
		{
			var nextcomboid = '#select_district_' + (i + 1);
			var next_selectedvalue = $(nextcomboid).attr('selectedIndex');
			setTimeout("$('" + comboid + "').attr('selectedIndex', '"+next_selectedvalue+"');" + " district_img('"+i+"');",1);
	    }

		if ($('#divDistrict'+i).is(':visible'))
		{
			lastdiv = '#divDistrict' + i;
			lastdivindex = i;
		}
	}
	
	var comboid = '#select_district_' + lastdivindex;
	if (lastdivindex > 0)
	{
			if (num != lastdivindex && (lastdivindex != 3 || $(comboid).attr('selectedIndex') == 0))
			{
				$(lastdiv).hide();
			}
			
			setTimeout("$('" + comboid + "').attr('selectedIndex', '0');" + " district_img('"+lastdivindex+"');",1);
	}
}

function show_district_control(json, select_current, district_id)
{
   if ($('#select_city').val() == '0')
    return;
    
   var result_html = "";

   for (var i=0; i<json.districts.length; i++)
    {
        if (json.districts[i].id == 0)
        {
            result_html += '<option value="' + json.districts[i].id + '" ' + 'id="id' + i + '"' + '>' + 'Не имеет значения' + '</option>';
        } else {
            result_html += '<option value="' + json.districts[i].id + '" id="id' + i + '"' + '>' + json.districts[i].name + '</option>';
        }
    }

    $('#select_district').html(result_html);

    if (district_id != -1)
    {
		var asyncCommand = "$(\"#select_district option[value='" + district_id + "']\").attr(\"selected\", \"selected\");";
		setTimeout(asyncCommand, 1);
    }
}

function on_station_district_controls_city_selected(stations_url, districts_url)
{
    $('#divStation0').hide();
    $('#divDistrict0').hide();
    $('#divStationsLoading').show();
    $('#divDistrictsLoading').show();

    $.post(stations_url + "/" + $('#select_city').val(), 
           {},
           function(json)
           {
                show_station_controls(json, false, "");
                $('#divStation0').show();
                $('#divStationsLoading').hide();
				station_img(0);
           },
           'json');
    
    $.post(districts_url + "/" + $('#select_city').val(), 
           {},
           function(json)
           {
                show_district_controls(json, false, "");
                $('#divDistrict0').show();
                $('#divDistrictsLoading').hide();
				district_img(0);
           },
           'json');
    
}

function init_station_district_controls_ajax(stations_url, districts_url)
{
    $('#select_city').change
    (
        function()
        {
            hide_controls();
            
            if ($('#select_city').val() != '0')
            {
				on_station_district_controls_city_selected(stations_url, districts_url);
            }
        }
    )

    $('#select_station_0').change(function(){$('#divStation1').show(); station_img(0); $('station_remove_1').hide();})
    $('#select_station_1').change(function(){$('#divStation2').show(); station_img(1); $('station_remove_2').hide();})
    $('#select_station_2').change(function(){$('#divStation3').show(); station_img(2); $('station_remove_3').hide();})
    $('#select_station_3').change(function(){station_img(3);})

    $('#select_district_0').change(function(){$('#divDistrict1').show(); district_img(0); $('district_remove_1').hide();})
    $('#select_district_1').change(function(){$('#divDistrict2').show(); district_img(1); $('district_remove_2').hide();})
    $('#select_district_2').change(function(){$('#divDistrict3').show(); district_img(2); $('district_remove_3').hide();})
    $('#select_district_3').change(function(){district_img(3);})
}


function init_station_district_control_ajax(stations_url, districts_url)
{
    $('#select_city').change
    (
        function()
        {
            hide_control();
            
            if ($('#select_city').val() != '0')
            {
                $('#divStation').hide();
                $('#divDistrict').hide();
                $('#divStationLoading').show();
                $('#divDistrictLoading').show();

                $.post(stations_url + "/" + $('#select_city').val(), 
                       {},
                       function(json)
                       {
                            show_station_control(json, false, -1);
                            $('#divStation').show();
                            $('#divStationLoading').hide();
                       },
                       'json');
                
                $.post(districts_url + "/" + $('#select_city').val(), 
                       {},
                       function(json)
                       {
                            show_district_control(json, false, -1);
                            $('#divDistrict').show();
                            $('#divDistrictLoading').hide();
                       },
                       'json');
            }
        }
    )
}
