$(document).ready(
	function()
	{
		var group = new Array(5);
		var isLoad = false;
		var isLoading = false;
		
		$("select[name=id_gr]").change(function () {
			var grId = $("select[name=id_gr] option:selected").val();
			var options = '';
			
			if ( grId > 0 ) {
				for (var i = 0; i < group[grId].length; i++) {
					options += '<option value="' + group[grId][i].value + '">' + group[grId][i].text + '</option>';
				}

				$("select[name=id_br]").html(options);
				$("select[name=id_br] option:first").attr('selected', 'selected');
			}
			else {
				$("select[name=id_br]").html('<option value="0">wybierz grupê</option>');
				("select[name=id_br] option:first").attr('selected', 'selected');
			}
		}); 
		
		jQuery.buildSelect = function(grId, brId) {
			if ( grId > 0 ) {
				var options = '';
				
				for (var i = 0; i < group[grId].length; i++) {
					options += '<option value="' + group[grId][i].value + '">' + group[grId][i].text + '</option>';
				}
				
				$("select[name=id_br]").html(options);
				
				if ( brId > 0 ) {
					$("select[name=id_br]").val(brId);
				}
			}
		};
		
		jQuery.getSectors = function(grId, brId) {
			if ( !isLoad ) {
				isLoading = true;
				
				$.get("files/sectors.xml", function(xml) { 
			        $(xml).find('grupa').each(function() {
						var grupa = $(this).attr('id');
						var count = 0;
						
						group[grupa] = new Array();
						group[grupa][0] = new Option("wybierz bran¿ê", 0);
						
						$(this).find('branza').each(function() {
							var brId = $(this).attr('id');
							var brName = $(this).text();
							
							group[grupa][count + 1] = new Option(brName, brId);
							count++;
						});
					});
					$.buildSelect(grId, brId);
					isLoading = false;
			    });
				isLoad = true;
			}
		};
		
		//$.getSectors();
	});