
// Array with Cities IDs which has Resedential Districts assigned
var cities_with_rd_arr = [1];

function districtChanged(district_id)
{
	try
	{
		if (typeof municipalities_arr == 'undefined')
		{
			alert("ERROR: municipalities_arr is undefined!");
			return;
		}

		var cities_sel_obj = document.getElementById("city_id");
		cities_sel_obj.options.length = 1;

		var sel_obj = document.getElementById("municipality_id");
		sel_obj.options.length = 1;

		// hide residential districts
		hideResidentialDistricts();

		if (district_id != "")
		{
			for (var i = 0; i < municipalities_arr.length; i++)
			{
				if (parseInt(municipalities_arr[i][2], 10) == parseInt(district_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(municipalities_arr[i][1], municipalities_arr[i][0]);
				}
			}
		}
	} catch (e) {
		alert("ERROR: " + e.message);
	}
}

function districtChangedNoCity(district_id)
{
	try
	{
		if (typeof municipalities_arr == 'undefined')
		{
			alert("ERROR: municipalities_arr is undefined!");
			return;
		}

		var sel_obj = document.getElementById("municipality_id");
		sel_obj.options.length = 1;

		if (district_id != "")
		{
			for (var i = 0; i < municipalities_arr.length; i++)
			{
				if (parseInt(municipalities_arr[i][2], 10) == parseInt(district_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(municipalities_arr[i][1], municipalities_arr[i][0]);
				}
			}
		}
	} catch (e) {
		alert("ERROR: " + e.message);
	}
}

function districtChangedNoRd(district_id)
{
	try
	{
		if (typeof municipalities_arr == 'undefined')
		{
			alert("ERROR: municipalities_arr is undefined!");
			return;
		}

		var cities_sel_obj = document.getElementById("city_id");
		cities_sel_obj.options.length = 1;

		var sel_obj = document.getElementById("municipality_id");
		sel_obj.options.length = 1;

		if (district_id != "")
		{
			for (var i = 0; i < municipalities_arr.length; i++)
			{
				if (parseInt(municipalities_arr[i][2], 10) == parseInt(district_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(municipalities_arr[i][1], municipalities_arr[i][0]);
				}
			}
		}
	} catch (e) {
		alert("ERROR: " + e.message);
	}
}

function districtChangedVisibleRd(district_id)
{
	try
	{
		if (typeof municipalities_arr == 'undefined')
		{
			alert("ERROR: municipalities_arr is undefined!");
			return;
		}

		var sel_obj = document.getElementById("municipality_id");
		sel_obj.options.length = 1;

		if (district_id != "")
		{
			for (var i = 0; i < municipalities_arr.length; i++)
			{
				if (parseInt(municipalities_arr[i][2], 10) == parseInt(district_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(municipalities_arr[i][1], municipalities_arr[i][0]);
				}
			}
		}
	} catch (e) {
		alert("ERROR: " + e.message);
	}
}

function municipalityChanged(municipality_id)
{
	var municipality_id = municipality_id;
	try
	{
		if (typeof cities_arr == 'undefined')
		{
			alert("ERROR: cities_arr is undefined!");
			return;
		}

		var sel_obj = document.getElementById("city_id");
		sel_obj.options.length = 1;

		// hide residential districts
		hideResidentialDistricts();

		if (municipality_id != "")
		{
			for (var i = 0; i < cities_arr.length; i++)
			{
				if (parseInt(cities_arr[i][2], 10) == parseInt(municipality_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(cities_arr[i][1], cities_arr[i][0]);
				}
			}
		}
	}
	catch (e)
	{
		alert("ERROR: " + e.message);
	}
}

function municipalityChangedVisibleRd(municipality_id)
{
	var municipality_id = municipality_id;
	try
	{
		if (typeof cities_arr == 'undefined')
		{
			alert("ERROR: cities_arr is undefined!");
			return;
		}

		var sel_obj = document.getElementById("city_id");
		sel_obj.options.length = 1;

		var rd_sel_obj = document.getElementById("rd_id");
		rd_sel_obj.options.length = 1;

		if (municipality_id != "")
		{
			for (var i = 0; i < cities_arr.length; i++)
			{
				if (parseInt(cities_arr[i][2], 10) == parseInt(municipality_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(cities_arr[i][1], cities_arr[i][0]);
				}
			}
		}
	}
	catch (e)
	{
		alert("ERROR: " + e.message);
	}
}

function cityChanged(city_id)
{
	var city_id = city_id;

	var rd_div_obj = document.getElementById("rd_div");
	var sel_obj = document.getElementById("rd_id");
	var is_rd_div_visible = (rd_div_obj.style.display == "block");
	var is_city_with_rd = is_in_array_numeric(cities_with_rd_arr, city_id);

	try
	{
		if (typeof residential_districts_arr == 'undefined')
		{
			alert("ERROR: residential_districts_arr is undefined!");
			return;
		}

		if (is_rd_div_visible)
		{
			sel_obj.options.length = 1;

			if (!is_city_with_rd)
			{
				changeDivVisibility("rd_div", false);
				return;
			}
		}
		else
		if (is_city_with_rd)
		{
			changeDivVisibility("rd_div", true);
		}

		if (city_id != "")
		{
			for (var i = 0; i < residential_districts_arr.length; i++)
			{
				if (parseInt(residential_districts_arr[i][2], 10) == parseInt(city_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(residential_districts_arr[i][1], residential_districts_arr[i][0]);
				}
			}
		}
	} 
	catch (e)
	{
		alert("ERROR: " + e.message);
	}
}

function cityChangedMulti(city_id_obj)
{
	var selected = new Array();
	for(var i = 0; i < city_id_obj.options.length; i++)
	{       
		if(city_id_obj.options[i].selected)       
		{
			selected.push(city_id_obj.options[i].value);
		}
	}

	if (selected.length == 1)
	{
		cityChanged(selected[0]);
	}
	else
	{
		document.getElementById("rd_id").options.length = 1;
		document.getElementById("rd_div").style.display = 'none';
	}
}

function cityChangedVisibleRd(city_id)
{
	var city_id = city_id;

	var sel_obj = document.getElementById("rd_id");
	var is_city_with_rd = is_in_array_numeric(cities_with_rd_arr, city_id);

	try
	{
		if (typeof residential_districts_arr == 'undefined')
		{
			alert("ERROR: residential_districts_arr is undefined!");
			return;
		}

		sel_obj.options.length = 1;

		if (city_id != "")
		{
			for (var i = 0; i < residential_districts_arr.length; i++)
			{
				if (parseInt(residential_districts_arr[i][2], 10) == parseInt(city_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(residential_districts_arr[i][1], residential_districts_arr[i][0]);
				}
			}
		}
	} 
	catch (e)
	{
		alert("ERROR: " + e.message);
	}
}

function hideResidentialDistricts()
{
	var rd_div_obj = document.getElementById("rd_div");
	if (rd_div_obj.style.display == "block")
	{
		var rd_sel_obj = document.getElementById("rd_id");
		rd_sel_obj.options.length = 1;
		rd_div_obj.style.display = "none";
	}
}

function districtChangedNoRd(district_id)
{
	try
	{
		if (typeof municipalities_arr == 'undefined')
		{
			alert("ERROR: municipalities_arr is undefined!");
			return;
		}

		var cities_sel_obj = document.getElementById("city_id");
		cities_sel_obj.options.length = 1;

		var sel_obj = document.getElementById("municipality_id");
		sel_obj.options.length = 1;

		if (district_id != "")
		{
			for (var i = 0; i < municipalities_arr.length; i++)
			{
				if (parseInt(municipalities_arr[i][2], 10) == parseInt(district_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(municipalities_arr[i][1], municipalities_arr[i][0]);
				}
			}
		}
	} catch (e) {
		alert("ERROR: " + e.message);
	}
}

function municipalityChangedNoRd(municipality_id)
{
	var municipality_id = municipality_id;
	try
	{
		if (typeof cities_arr == 'undefined')
		{
			alert("ERROR: cities_arr is undefined!");
			return;
		}

		var sel_obj = document.getElementById("city_id");
		sel_obj.options.length = 1;

		if (municipality_id != "")
		{
			for (var i = 0; i < cities_arr.length; i++)
			{
				if (parseInt(cities_arr[i][2], 10) == parseInt(municipality_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(cities_arr[i][1], cities_arr[i][0]);
				}
			}
		}
	}
	catch (e)
	{
		alert("ERROR: " + e.message);
	}
}

function districtChangedPublic(district_id)
{
	try
	{
		if (typeof municipalities_arr == 'undefined')
		{
			alert("ERROR: municipalities_arr is undefined!");
			return;
		}

		var sel_obj = document.getElementById("municipality_id");
		sel_obj.options.length = 1;

		if (district_id != "")
		{
			for (var i = 0; i < municipalities_arr.length; i++)
			{
				if (parseInt(municipalities_arr[i][2], 10) == parseInt(district_id, 10))
				{
					sel_obj.options[sel_obj.options.length] = new Option(municipalities_arr[i][1], municipalities_arr[i][0]);
				}
			}
		}
	} catch (e) {
		alert("ERROR: " + e.message);
	}
}
