function preencheConcelhos(concelhos,id)
{
	var html = '';
		
	html += '<select id="' + id + '" name="' + id + '">';
	html += '<option value="%" selected="true">Todos</option>';
	if(_Idioma=="EN") html=html.replace("Todos","All");
	
	for (j = 0; j < concelhos.length; j++)
	{
		html += '<option value="' + concelhos[j] + '">' + concelhos[j] + '</option>';
	}

	html += '</select>';
	
	$("DIV_" + id).innerHTML = html
}



function preencheCategoria(nomeObj)
{
	var html = '';
	var temas = arvoreTemas();

	html += '<select id="' + nomeObj + '" name="' + nomeObj + '" onchange="preencheSubCategoria(\'' + nomeObj + '\')">';
	html += '<option value="" selected="true">Escolha uma opção</option>';
	if(_Idioma=="EN") html=html.replace("Escolha uma opção","Select an option");
	
	for (j = 0; j < temas.length; j++)
	{
		for (i = 0; i < temas[j].length; i++)
		{
			if (temas[j][i])
			{
				if (temas[j][i][3] != undefined)
				{
					if (temas[j][i][3].length == 2)
					{
						html += '<option value="' + temas[j][i][3] + '">' + temas[j][i][1] + '</option>';		
					}
				}
			}
		}
	}

	html += '</select>';
	
	$("DIV_"+nomeObj).innerHTML = html
}


function preencheSubCategoria(nomeObj)
{
	var html = '';
	var cat = $(nomeObj).value;
	var temas = arvoreTemas();
	var disable = (cat == "") ? "disabled" : "" ;
	
	html += '<select id="' + nomeObj + '_Sub" name="' + nomeObj + '_Sub" ' + disable + ' >';
	html += '<option value="" selected="true">Escolha uma opção</option>';
	if(_Idioma=="EN") html=html.replace("Escolha uma opção","Select an option");
		
	for (j = 0; j < temas.length; j++)
		for (i = 0; i < temas[j].length; i++)
			if (temas[j][i])
				if (temas[j][i][3] != undefined)
					if (temas[j][i][3].length == 2)
						if (temas[j][i][3] == cat)
							for (k = 4; k < temas[j][i].length; k++)
								html += '<option value="' + temas[j][i][k][1][3] + '">' + temas[j][i][k][1][0] + '</option>';

	html += '</select>';
	
	$("DIV_" + nomeObj + "_Sub").innerHTML = html
}

function pesquisarPI()
{
	//obter dados da pesquisa!
	//fechar div de resultados
	IDT_MOUSE = { "clientX":480, "clientY": 295 };
	fxPesqRes.hide();

	var oSelConc = $('Sel_Pesq_Concelho');
	
	var oSel = $('Sel_Pesq_Cat');
	if (oSel.value == "") {
		//alert("Por favor, selecione uma categoria para efectuar a pesquisa.");
		var sMsg = "Por favor, selecione uma categoria para efectuar a pesquisa.";
		var sTit = "Erro";
		if(_Idioma=="EN") {
		    sMsg = "Please select a category to search in.";
		    sTit = "Error";
		}
		doTooltip(IDT_MOUSE, sTit, sMsg);
		oSel.focus();
		return;
	}
	
	var oSelSub = $('Sel_Pesq_Cat_Sub');
	if (oSelSub.value == "") {
		//alert("Por favor, selecione uma subcategoria para efectuar a pesquisa.");
		var sMsg = "Por favor, selecione uma subcategoria para efectuar a pesquisa.";
		var sTit = "Erro";
		if(_Idioma=="EN") {
		    sMsg = "Please select a subcategory to search in.";
		    sTit = "Error";
		}
		doTooltip(IDT_MOUSE, sTit, sMsg);
		oSelSub.focus();
		return;
	}

	var oTxt = $('Sel_Pesq_Txt');
	
	//Ligar o tema correspondente na árvore
	//alert("ligar o tema: " + oSelSub.options[oSelSub.selectedIndex].text);
	ligarTema(oSelSub.options[oSelSub.selectedIndex].text)

	//Actualiza o objecto de persistencia
	mMapa.persist.updatePesquisaPI(escape(oSelConc.value),oSelSub.value,oTxt.value,'');
	mMapa.persist.updatePesquisaProximidade('','','','','','');

	//executar a pesquisa
	pedeMapa('PESQUISARPI',{"concelho":escape(oSelConc.value),"cat":oSel.value,"subcat":oSelSub.value,"txt":escape(oTxt.value)});
}

function limparMapa()
{
	mMapa.persist.updatePesquisaPI('','','','','');
	mMapa.persist.updatePesquisaProximidade('','','','','','');
	updateVisibleLayers();
	pedeMapa('');
}

function pesquisarProximidade()
{
	//categoria válida?
	IDT_MOUSE = { "clientX":480, "clientY": 295 };
	var oSel = $('Sel_PesqProx_Cat');
	var oSelSub = $('Sel_PesqProx_Cat_Sub');
	var oBff = $('valorProximidade');
	
	if (oSel.selectedIndex == "") {
		//alert("Por favor, selecione uma categoria para efectuar a pesquisa.");
		var sMsg = "Por favor, selecione uma categoria para efectuar a pesquisa.";
		var sTit = "Erro";
		if(_Idioma=="EN") {
		    sMsg = "Please select a category to search in.";
		    sTit = "Error";
		}
		doTooltip(IDT_MOUSE, sTit, sMsg);
		oSel.focus();
		return;
	}
	
	if (oSelSub.value == "") {
		//alert("Por favor, selecione uma subcategoria para efectuar a pesquisa.");
		var sMsg = "Por favor, selecione uma subcategoria para efectuar a pesquisa.";
		var sTit = "Erro";
		if(_Idioma=="EN") {
		    sMsg = "Please select a subcategory to search in.";
		    sTit = "Error";
		}
		doTooltip(IDT_MOUSE, sTit, sMsg);
		oSelSub.focus();
		return;
	}

	//Ligar o tema correspondente na árvore
	ligarTema(oSelSub.options[oSelSub.selectedIndex].text)

	//Actualiza o objecto de persistencia
	mMapa.persist.updatePesquisaProximidade(xPoint,yPoint,oSelSub.value,oBff.value,'');
	//mMapa.persist.updatePesquisaPI('','','','','');
	
	//executar a pesquis
	pedeMapa('PESQUISARPROXIMIDADE',{"cat":oSel.value,"subcat":oSelSub.value,"bff":oBff.value,"xPnt":xPoint,"yPnt":yPoint});
}


function marcarPI(idsig,x,y)
{

	pedeMapa('MARCARPI',{"idsig":idsig,"x":x,"y":y});

}

//DC - Ago08 - Actualizado para se clicar sobre todo o div e não só na img
function togglePesquisa(obj,control)
{
	if ($(control).style.display == 'none')
	{
	    $("img_"+arguments[1]).src = 'imagens/minus.png';
		//obj.src = 'imagens/minus.png';
		$(control).style.display = 'block';
	}
	else
	{
	    $("img_"+arguments[1]).src = 'imagens/plus.png';
		//obj.src = 'imagens/plus.png';
		$(control).style.display = 'none';
	}

	for (i = 2; i < arguments.length; i++)
	{
		$("img_"+arguments[i]).src = 'imagens/plus.png';
		$(arguments[i]).style.display = 'none';
	}

}

function pesquisarSL()
{
	var oTxt = $('Sel_Pesq_Txt_SL');
	
	//executar a pesquisa
	pedeMapa('PESQUISARSL',{"txt":escape(oTxt.value)});
}

function pesquisarR()
{
	var oSel = $('Sel_PesqR_Concelho');
	var oTxt = $('Sel_Pesq_Txt_R');
	
	//executar a pesquisa
	pedeMapa('PESQUISARR',{"concelho":escape(oSel.value),"txt":escape(oTxt.value)});
}
//DC - pesquisar um ponto coordenado Ago08
function pesquisarSC(sistCoords)
{
    var dX; var dY;
    if(sistCoords == "EPSG:4326")
    {
	    dX = $('XWGS84').value;
	    dY = $('YWGS84').value;
	}
	if(sistCoords == "EPSG:27492")
    {
	    dX = $('XD73HG').value;
	    dY = $('YD73HG').value;
	}
	if(dX=="" || dY=="")
	{
	    //alert("Por favor introduza um par de coordenadas X,Y ou Lon,Lat...");
	    var sMsg = "Por favor introduza um par de coordenadas X,Y ou Lon,Lat...";
		var sTit = "Erro";
		if(_Idioma=="EN") {
		    sMsg = "Please enter coordinates X,Y or Lon,Lat to search for...";
		    sTit = "Error";
		}
	    janelaTxt(sTit, sMsg, 350, 250);
	    return;
	}
	
	//executar a pesquisa
	pedeMapa('PESQUISARSC',{"sistCoords":escape(sistCoords),"x":dX,"y":dY});
}


function textBoxKey(e,opcao)
{
	if (!e) { e = window.event; } keyCode = (e.keyCode) ? e.keyCode : e.which;
		
	if (keyCode == 13) //tecla enter
	{
		if (opcao == "R") pesquisarR();
		
		else if (opcao == "SL") pesquisarSL();

		else if  (opcao == "PI") pesquisarPI();
		
		else if (opcao == "SCD73HG") pesquisarSC("EPSG:27492");
		
		else if (opcao == "SCWGS84") pesquisarSC("EPSG:4326");
	}
}

