function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();
var http2 = createRequestObject();

function carregarConcelhos(distrito, idTipoEntidade) {
  http.open('get', 'ajax.php?action=concelhos&idDistrito='+distrito);
  http.onreadystatechange = function() {
		if(http.readyState == 4){
			var combo = document.getElementById('concelho');
			combo.options.length = 0;
			var response = http.responseText;
			var items = response.split("*");
			var count = items.length;
			for (var i=0;i<count;i++){
				var options = items[i].split("%");
				combo.options[i] = new Option(options[0],options[1]);
			}


		}		
	}
  http.send(null);
  http2.open('get', 'ajax.php?action=entidades&idDistrito='+distrito+'&idTipoEntidade='+idTipoEntidade);
  http2.onreadystatechange = function() {
		if(http2.readyState == 4){
			var response = http2.responseText;
			if (response.length > 0) {
				document.getElementById("listentidades").innerHTML=response;
			}
		}		
	}
  http2.send(null);

}

function carregarConcelhos2(distrito) {
//    http.open('get', 'ajax.txt'); //simular php...
  http.open('get', 'ajax.php?action=concelhos&idDistrito='+distrito);
  http.onreadystatechange = function() {
		if(http.readyState == 4){
			var combo = document.getElementById('concelho2');
			combo.options.length = 0;
			var response = http.responseText;
			var items = response.split("*");
			var count = items.length;
			for (var i=0;i<count;i++){
				var options = items[i].split("%");
				combo.options[i] = new Option(options[0],options[1]);
			}
		}		
	}
  http.send(null);
}
function carregarEntidConcelho(concelho, idTipoEntidade) {
//    http.open('get', 'ajax.txt'); //simular php...
  http2.open('get', 'ajax.php?action=entidades&idConcelho='+concelho+'&idTipoEntidade='+idTipoEntidade);
  http2.onreadystatechange = function() {
		if(http2.readyState == 4){
			var response = http2.responseText;
			if (response.length > 0) {
				document.getElementById("listentidades").innerHTML=response;
			}
		}		
	}
  http2.send(null);
}

function carregarDistrito(id,nome) {
	//alert(id + nome);
	for (xi=0;xi<=document.pesq_biblioteca.distrito.length;xi++) {
		if (document.pesq_biblioteca.distrito.options[xi].value==id) {
			document.pesq_biblioteca.distrito.selectedIndex=xi;
			carregarConcelhos(id, idTipoEntidade);
			break;
		}
	}	
}