$(function()
{
	$('#selection_tabs .header_1').click(function()
	{
		document.getElementById('selection_tabs').className = 'corner-blue-header_1 clearfix';
		document.getElementById('medical_cabinets_list').style.display = 'inline';
		document.getElementById('infrastructures_list').style.display = 'none';
		document.getElementById('selection-list-by-cabinets').style.display = 'inline';
		document.getElementById('selection-list-by-infrastructures').style.display = 'none';
		document.getElementById('main_submit_button').value = 'medical_cabinets';
		document.getElementById('main_submit_button').innerHTML = 'Выбрать';
		return false;
	});

	$('#selection_tabs .header_2').click(function()
	{
		document.getElementById('selection_tabs').className = 'corner-blue-header_2 clearfix';
		document.getElementById('medical_cabinets_list').style.display = 'none';
		document.getElementById('infrastructures_list').style.display = 'inline';
		document.getElementById('selection-list-by-cabinets').style.display = 'none';
		document.getElementById('selection-list-by-infrastructures').style.display = 'inline';
		document.getElementById('main_submit_button').value = 'infrastructures';
		document.getElementById('main_submit_button').innerHTML = 'Выбрать';
		return false;
	});
});

function process_selection_by_cabinets()
{
	checked_cabinets = Array();
	i = 1;
	cnt = 0;
	visible_count = 0;
	while (document.getElementById('medical_cabinets_' + i) != undefined)
	{
		if (document.getElementById('medical_cabinets_' + i).checked)
		{
			checked_cabinets[cnt] = document.getElementById('medical_cabinets_ids_' + i).value;
			cnt++;
		}
		i++;
		if (i > 500) break;
	}
	i = 1;
	if (cnt == 0)
	{
		while (document.getElementById('sanatorium_by_medical_cabinets_' + i) != undefined)
		{
			document.getElementById('sanatorium_by_medical_cabinets_' + i).style.display = 'inline';
			visible_count++;
			i++;
			if (i > 500) break;
		}
	}
	else
	{
		while (document.getElementById('sanatorium_by_medical_cabinets_' + i) != undefined)
		{
			coincidences = 0;
			for (pos in checked_cabinets)
			{
				if (cab_links[i][checked_cabinets[pos]] != undefined) { coincidences++; }
			}
			if (coincidences == cnt)
			{
				document.getElementById('sanatorium_by_medical_cabinets_' + i).style.display = 'inline';
				visible_count++;
			}
			else
			{
				document.getElementById('sanatorium_by_medical_cabinets_' + i).style.display = 'none';
			}
			i++;
			if (i > 500) break;
		}
	}
	if (visible_count == 0)
	{
		document.getElementById('not_found_by_medical_cabinets').style.display = 'inline';
	}
	else
	{
		document.getElementById('not_found_by_medical_cabinets').style.display = 'none';
	}
}

function process_selection_by_infrastructures()
{
	checked_infrastructures = Array();
	i = 1;
	cnt = 0;
	visible_count = 0;
	while (document.getElementById('infrastructures_' + i) != undefined)
	{
		if (document.getElementById('infrastructures_' + i).checked)
		{
			checked_infrastructures[cnt] = document.getElementById('infrastructures_ids_' + i).value;
			cnt++;
		}
		i++;
		if (i > 500) break;
	}
	i = 1;
	if (cnt == 0)
	{
		while (document.getElementById('sanatorium_by_infrastructures_' + i) != undefined)
		{
			document.getElementById('sanatorium_by_infrastructures_' + i).style.display = 'inline';
			visible_count++;
			i++;
			if (i > 500) break;
		}
	}
	else
	{
		while (document.getElementById('sanatorium_by_infrastructures_' + i) != undefined)
		{
			coincidences = 0;
			for (pos in checked_infrastructures)
			{
				if (inf_links[i][checked_infrastructures[pos]] != undefined) { coincidences++; }
			}
			if (coincidences == cnt)
			{
				document.getElementById('sanatorium_by_infrastructures_' + i).style.display = 'inline';
				visible_count++;
			}
			else
			{
				document.getElementById('sanatorium_by_infrastructures_' + i).style.display = 'none';
			}
			i++;
			if (i > 500) break;
		}
	}
	if (visible_count == 0)
	{
		document.getElementById('not_found_by_infrastructures').style.display = 'inline';
	}
	else
	{
		document.getElementById('not_found_by_infrastructures').style.display = 'none';
	}
}
