var obra_oberta = false;

function get_obra_info(id_obra){
	var aux_taula = false;
	if(obra_oberta == id_obra){
		document.getElementById("taula_"+obra_oberta).style.display = "none";
		document.getElementById("img"+obra_oberta).src = "/imatges/icon_action_open.gif";
		obra_oberta = false;
	}else{
		if(obra_oberta != false){
			document.getElementById("taula_"+obra_oberta).style.display = "none";
			document.getElementById("img"+obra_oberta).src = "/imatges/icon_action_open.gif";
		}
		obra_oberta = id_obra;
		if(document.getElementById("taula_"+obra_oberta).rows.length == 0){
			var url = "/obra_info.php?obra="+id_obra;;
			make_request(url,obra_info_resp,"GET",null);
		}else{
			document.getElementById("taula_"+obra_oberta).style.display = "block";
		}
		document.getElementById("img"+obra_oberta).src = "/imatges/icon_action_close.gif";
	}
}

function obra_info_resp(){
	if(http_request.readyState == 4){
		if(http_request.status == 200){
			var xmldoc = http_request.responseXML;
			var aux_taula = document.getElementById("taula_"+obra_oberta);
			var aux_tipus;
			var aux_indret;
			var aux_localitat;
			var aux_data;
			var aux_fila;
			var aux_label;
			//TODO
			var x_obra = xmldoc.getElementsByTagName("obra")[0];//puto IE
			for(var i = 1; i<x_obra.childNodes.length; i++){
				if(x_obra.childNodes[i].childNodes[4].hasChildNodes()){
					//Actuacio privada
					if(x_obra.childNodes[i].childNodes[3].hasChildNodes())
						aux_data = x_obra.childNodes[i].childNodes[3].firstChild.data;
					else
						aux_data = " ";
					aux_label = "Actuació privada el "+aux_data;
				}else{
					if(x_obra.childNodes[i].childNodes[0].hasChildNodes())
						aux_tipus = x_obra.childNodes[i].childNodes[0].firstChild.data;
					else
						aux_tipus = " ";
					if(x_obra.childNodes[i].childNodes[1].hasChildNodes())
						aux_indret = x_obra.childNodes[i].childNodes[1].firstChild.data;
					else
						aux_indret = " ";
					if(x_obra.childNodes[i].childNodes[2].hasChildNodes())
						aux_localitat = x_obra.childNodes[i].childNodes[2].firstChild.data;
					else
						aux_localitat = " ";
					if(x_obra.childNodes[i].childNodes[3].hasChildNodes())
						aux_data = x_obra.childNodes[i].childNodes[3].firstChild.data;
					else
						aux_data = " ";
					aux_label = "" + aux_tipus + ". " + aux_indret + " a " + aux_localitat + " el " + aux_data +"";
				}
				aux_fila = aux_taula.insertRow(i-1);
				aux_fila.insertCell(0).appendChild(document.createTextNode(aux_label));
			}
			aux_taula.style.display = "block";
		}else{
			alert("problema en la peticio. Recarrega la plana");
		}
	}
}

