function geraConcelhos(distrito)
{		
	cleanFreguesia();	
	cleanLocalidade();								
	document.getElementById("concelhos").innerHTML   ="<img src='images/loading.gif' title='A executar..' alt='A executar..'>";
	ajaxpack.getAjaxRequest("lib/ajax/geraConcelhos.php", "distrito="+distrito , GetGeraConcelhos, "xml");
}

function GetGeraConcelhos()
{
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4) { //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1) { //if request was successful or running script locally
			if (myfiletype=="xml") {
				var xml				=	myajax.responseXML;
				//concelhos		= xml.getElementsByTagName("concelho");
				//alert(concelhos);
				//alert(concelhos.length);
				var lista 			=	myajax.responseXML.getElementsByTagName('lista');
				//alert(lista.length);
				
				
				var elemsuc			= xml.getElementsByTagName('sucess');
				//alert(elemsuc);
				
				var sucess			= elemsuc[0].firstChild.nodeValue;
				//alert(sucess);
				
				
				if (sucess == 'true')
				{
					var oSel = document.createElement('select');
					var totConcelhos	= xml.getElementsByTagName('concelho').length;
					
					var oBlank			= document.createElement('option');
					oBlank.innerHTML	= "---";
					oSel.appendChild(oBlank);
					
					for(x=0;x<totConcelhos;x++){
						// create new options
						var oOption 	= document.createElement('option');
						oOption.value	= xml.getElementsByTagName('concelho')[x].getAttribute("id");
						oOption.innerHTML	= xml.getElementsByTagName('concelho')[x].firstChild.nodeValue;
						// append the option
						oSel.appendChild(oOption);
					}
					
					document.getElementById('concelhos').innerHTML = '';
					oSel.name = "concelho";
					oSel.onchange = function() { geraFreguesias(this.value); }
					document.getElementById('concelhos').appendChild(oSel);
				}
				 
				else if (sucess == 'false')
				{
				 	document.getElementById('concelhos').innerHTML = "<select name='concelho' id='concelho' onChange='geraFreguesias(this.value)'></select>";
				}					
			}	
		}
	}
}


function geraNASC_Concelhos(distrito)
{		
	cleanNASC_Freguesia();	
	cleanNASC_Localidade();								
	document.getElementById("NASC_concelhos").innerHTML   ="<img src='images/loading.gif' title='A executar..' alt='A executar..'>";
	ajaxpack.getAjaxRequest("lib/ajax/geraConcelhos.php", "distrito="+distrito , GetGeraNASC_Concelhos, "xml");
}

function GetGeraNASC_Concelhos()
{
	var myajax=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax.readyState == 4) { //if request of file completed
		if (myajax.status==200 || window.location.href.indexOf("http")==-1) { //if request was successful or running script locally
			if (myfiletype=="xml") {
				var xml				=	myajax.responseXML;
				
				//concelhos		= xml.getElementsByTagName("concelho");
				//alert(concelhos);
				//alert(concelhos.length);
				var lista 			=	myajax.responseXML.getElementsByTagName('lista');
				//alert(lista.length);
				
				
				var elemsuc			= xml.getElementsByTagName('sucess');
				//alert(elemsuc);
				var sucess			= elemsuc[0].firstChild.nodeValue;
				//alert(sucess);
				
				
				if (sucess == 'true')
				{
					var oSel = document.createElement('select');
					var totConcelhos	= xml.getElementsByTagName('concelho').length;
					
					var oBlank			= document.createElement('option');
					oBlank.innerHTML	= "---";
					oSel.appendChild(oBlank);
					
					for(x=0;x<totConcelhos;x++){
						// create new options
						var oOption 	= document.createElement('option');
						oOption.value	= xml.getElementsByTagName('concelho')[x].getAttribute("id");
						oOption.innerHTML	= xml.getElementsByTagName('concelho')[x].firstChild.nodeValue;
						// append the option
						oSel.appendChild(oOption);
					}
					
					document.getElementById('NASC_concelhos').innerHTML = '';
					oSel.name = "NASC_concelho";
					oSel.onchange = function() { geraNASC_Freguesias(this.value); }
					document.getElementById('NASC_concelhos').appendChild(oSel);
				}
				 
				else if (sucess == 'false')
				{
				 	document.getElementById('NASC_concelhos').innerHTML = "<select name='NASC_concelho' id='concelho' onChange='geraNASC_Freguesias(this.value)'></select>";
				}					
			}	
		}
	}
}
