/**
 * @author luksoft
 */
	var branches = new Hash();
	var locations = new Hash();
	function dodajLokalizacje(pow, wojId){
		var valueP = wojId;
		var txtP = $(('OgloszenieWojewodztwoLbl_' + wojId)).innerHTML;
		
		if (pow && $(pow)) {
			var valueD = $(pow).value;
			var selector = 'label[for="' + pow + '"]';
			var labels = $$(selector);
			var txtD = '';
			if (labels) {
				var label = labels[0];
				txtD = label.innerHTML;
			}
			locations.each(function(pair){
				var id = pair.key;
				var ids = id.split('_', 2);
				if(!ids[1] && wojId == ids[0]){
					usunLokalizacje(id);
				}
			});
		}
		else{
			var txtD = 'całe województwo';
			var valueD = '';
			locations.each(function(pair){
				var id = pair.key;
				var ids = id.split('_', 2);
				if(ids[1] && wojId == ids[0]){
					usunLokalizacje(id);
				}
			});
		}
		var labelTxt = txtP + ' ' + txtD;
		var id = valueP + '_' + valueD;

		var list = $('OgloszeniePowiatyLista');
		if(!locations.get(id)){
			var liId = 'OgloszeniePowiatyLista' + id;
			var a = '<a href="#" onclick="usunLokalizacje(\'' + id  + '\'); return false;">[usuń]</a> ';
			var li = new Element('li', {id : liId}).update(a + labelTxt);
			list.appendChild(li);
			locations.set(id, labelTxt);
		}
		else{
			usunLokalizacje(id);
		}
	}
	
	function usunLokalizacje(id){
		var liId = 'OgloszeniePowiatyLista' + id;
		$(liId).remove();
		locations.unset(id);
		var ids = id.split('_', 2);
		if(!ids[1]){
			var woj = $(('OgloszenieWojewodztwo_' + ids[0]));
			if(woj && woj.checked){
				woj.checked = false;
			}
		}
		else{
			var pow = $(('OgloszeniePowiaty' + ids[1]));
			if(pow && pow.checked){
				pow.checked = false;
			}
		}
	}
	
	function dodajBranze(bra, parId){
		var valueP = parId;
		var txtP = $(('OgloszenieBranzaLbl_' + parId)).innerHTML;
		
		if (bra && $(bra)) {
			var valueD = $(bra).value;
			var selector = 'label[for="' + bra + '"]';
			var labels = $$(selector);
			var txtD = '';
			if (labels) {
				var label = labels[0];
				txtD = label.innerHTML;
			}
			branches.each(function(pair){
				var id = pair.key;
				var ids = id.split('_', 2);
				if(!ids[1] && parId == ids[0]){
					usunBranze(id);
				}
			});
		}
		else{
			var txtD = 'cała branża';
			var valueD = '';			
			branches.each(function(pair){
				var id = pair.key;
				var ids = id.split('_', 2);
				if(ids[1] && parId == ids[0]){
					usunBranze(id);
				}
			});
		}
		var labelTxt = txtP + ' ' + txtD;
		var id = valueP + '_' + valueD;

		var list = $('OgloszenieBranzeLista');
		if(!branches.get(id)){
			var liId = 'OgloszenieBranzeLista' + id;
			var a = '<a href="#" onclick="usunBranze(\'' + id  + '\'); return false;">[usuń]</a> ';
			var li = new Element('li', {id : liId}).update(a + labelTxt);
			list.appendChild(li);
			branches.set(id, labelTxt);
		}
		else{
			usunBranze(id);
		}
	}
	
	function usunBranze(id){
		var liId = 'OgloszenieBranzeLista' + id;
		$(liId).remove();
		branches.unset(id);
		var ids = id.split('_', 2);
		if(!ids[1]){
			var par = $(('OgloszenieBranza_' + ids[0]));
			if(par && par.checked){
				par.checked = false;
			}
		}
		else{
			var bra = $(('OgloszenieBranze' + ids[1]));
			if(bra && bra.checked){
				bra.checked = false;
			}
		}
	}
	
    function doSubmit(){
        var result = true;
        
        if (result) {
            locations.each(function(pair){
                var input = new Element('input', {
                    type: 'hidden',
                    name: 'data[Ogloszenie][lokalizacje_lista][]',
                    value: pair.key
                });
                $('Szukaj').appendChild(input);
            });
        }
        if (result) {
            branches.each(function(pair){
                var input = new Element('input', {
                    type: 'hidden',
                    name: 'data[Ogloszenie][branze_lista][]',
                    value: pair.key
                });
                $('Szukaj').appendChild(input);
            });
        }
        return result;
    }
	
