
function locChildWithId ( obj, id ) {

	for ( var x = 0 ; x < obj.childNodes.length ; x++ ) {
		var cnode, node = obj.childNodes[x];
		if ( node.id == id )
			return node;
		else if ( cnode = locChildWithId(node,id) )
			return cnode;			
	}

}

function locChildWithClass ( obj, className ) {

	for ( var x = 0 ; x < obj.childNodes.length ; x++ ) {
		var cnode, node = obj.childNodes[x];
		if ( node.className == className )
			return node;
		else if ( cnode = locChildWithClass(node,className) )
			return cnode;			
	}

}


function getAndShowFileOn ( url, elementId, onCompleteHandler ) {

	return new Ajax.Request ( url, {
		method: 'get',
		parameters: null,
		encoding: 'UTF-8',
		onComplete: function (response) {
			if ( elementId != null )
				document.getElementById(elementId).innerHTML = response.responseText;
			if ( onCompleteHandler != null )
				onCompleteHandler(response);
		}
	} );

}


// Get municipality widget

function showMunList ( selectmun ) {

	var
		widget = document.getElementById('widConcelho'),
		commands = locChildWithClass(widget,'commands_sapo') || locChildWithClass(widget,'commands'),
		widDiv = locChildWithClass(widget,'formlocal'),
		list;

	widDiv.id = "concelho_formlocal";

	// Show municipalities on list

	getAndShowFileOn("/xml/municipalities.html",'concelho_formlocal',function() { if(selectmun) selectMun(selectmun); commands.style.display = "none"; widDiv.style.display = "block"; } );

}


// Hide municipalitites list

function hideMunList ( ) {

	var
		widget = document.getElementById('widConcelho'),
		commands = locChildWithClass(widget,'commands_sapo') || locChildWithClass(widget,'commands'),
		widDiv = locChildWithClass(widget,'formlocal');

	widDiv.innerHTML = "";
	commands.style.display = "block";
	widDiv.style.display = "none";

}


// Set municipality

function setMunicipality ( ) {

	var
		obj = (document.getElementsByName("pickconcelho"))[0];

	if ( obj == null )
		return null;

	location.href = 'http://noticias.sapo.pt/local/'+obj.value+'/autarquicas/';

}


