var coleccEnOrden = new Array();
coleccEnOrdenIdx = 1;

function addColeccEnOrden(nombreColecc) {
	coleccEnOrden[nombreColecc] = coleccEnOrdenIdx;
	coleccEnOrdenIdx++;
}
addColeccEnOrden("Cartografia");
addColeccEnOrden("Cartografia_1000");
addColeccEnOrden("Cartografia_5000");
addColeccEnOrden("Fotos_1927");
addColeccEnOrden("Fotos_1956");
addColeccEnOrden("Fotos_1979");
addColeccEnOrden("Fotos_Catastro");
addColeccEnOrden("Fotos_Fototeca_CHE");
addColeccEnOrden("Fotos_Nucleos2006");
addColeccEnOrden("Fotos_Nucleos2009");
addColeccEnOrden("Fotos_Nucleos2010");
addColeccEnOrden("Fotos_Zaragoza1965");
addColeccEnOrden("Fotos_Zaragoza1967");
addColeccEnOrden("Ortofoto_SigOleicola");
addColeccEnOrden("Ortofoto_1997_2007");
addColeccEnOrden("Ortofoto_2006");
addColeccEnOrden("Ortofoto_2007");
addColeccEnOrden("Ortofoto_2008");
addColeccEnOrden("Ortofoto_2009");
addColeccEnOrden("Satelite_Landsat");
addColeccEnOrden("Planos_Altimetria");
addColeccEnOrden("Planos_Archivo");
addColeccEnOrden("Planos_Conjunta");
addColeccEnOrden("Planos_Historicos");
addColeccEnOrden("Planos_Planimetria");
addColeccEnOrden("Planos_Poblaciones");
addColeccEnOrden("Planos_Urbanos");
addColeccEnOrden("Planos_Zaragoza1971");
addColeccEnOrden("Medio Ambiente");
addColeccEnOrden("Minas");

//////////////////////////////////////////////////////////////////

resultadoGFI_sitar = new Array();
resultadoGFI_sitar["count"] = 0;

listaColecc = new Array();
listaColeccCount = 0;

function resetResultadosAnterioresGFI() {
	resultadoGFI_sitar = new Array();
	resultadoGFI_sitar["count"] = 0;

	listaColecc = new Array();
	listaColeccCount = 0;

	for (var jj = 0; jj < gfiCount; jj++) {
		gfiList[jj]["camposRespuesta"] = new Array();
		gfiList[jj]["camposRespuesta"]["count"] = 0;
		gfiList[jj]["valoresRespuesta"] = new Array();
		gfiList[jj]["valoresRespuesta"]["count"] = 0;
	}

}

	function sinRespuestaGFI() {
			//processResultQueryGFI_sitarDescarga(strResGFI);
		currentQueriedGFI++;
		doNextQuery();
	}

var t_timeout;

function sendGFIQuery(urlGFI){
	if (debugOn) {
		window.open(urlGFI);
	}
	http = getHTTPObject();
	if ((http != null)) {
					//alert(urlGFI);
		http.open("GET", urlGFI);

		if (urlGFI.indexOf(urlGFI_SITAR) != -1) {
			http.onreadystatechange = parseResultGFIQuery_sitar;
		} else {
			http.onreadystatechange = parseResultGFIQuery;
		}
		isWorking = true;
		//muestraLayerLoading();

		t_timeout = setTimeout("sinRespuestaGFI()", 15000);

		http.send(null);
	}
}

function parseResultGFIQuery(){
	if (http.readyState == 4) {
		if (http.status == 200) {
			clearTimeout(t_timeout);

			var result = http.responseText;
				//alert("RESULTADO:" + result);
			processResultQueryGFI(result);
			currentQueriedGFI++;
			doNextQuery();
		} else {
			clearTimeout(t_timeout);
			sinRespuestaGFI();
		}
	}
}

function parseResultGFIQuery_sitar2(){
	if (http.readyState == 4) {
		if (http.status == 200) {
			clearTimeout(t_timeout);

			var result = http.responseText;
				//alert("RESULTADO:" + result);
			processResultQueryGFI_sitarDescarga(result, true);

			var idContent = getResultsGFIHTML_sitar();
			resultsWin.setcontent(idContent);
			resultsWin.settitle("Datos descargables sobre (" + RoundDecimal(xCoordUsr, 2) + ", " + RoundDecimal(yCoordUsr, 2) + ")");
			resultsWin.show();

			updateAcetateMarca(xCoordUsr, yCoordUsr);

			ocultaLayerLoading();
		} else {
			clearTimeout(t_timeout);
			sinRespuestaGFI();
		}
	}
}

function parseResultGFIQuery_sitar(){
	if (http.readyState == 4) {
		if (http.status == 200) {
			clearTimeout(t_timeout);

			var result = http.responseText;
				//alert("RESULTADO:" + result);
			processResultQueryGFI_sitarDescarga(result, false);
			currentQueriedGFI++;
			doNextQuery();
		} else {
			clearTimeout(t_timeout);
			sinRespuestaGFI();
		}
	}
}

function processResultQueryGFI(theReply) {
	//TODO : Use XML DOM parser instead to parse the response

	var selectedData = "";
	var endpos = 1;
	reply = theReply;

	resultNum = 0;
	var pos = reply.indexOf("<FIELDS ",endpos);
	while (pos != -1) {
		startpos = pos + 8;
		endpos = reply.indexOf('"/>',startpos);
		selectedData = reply.substring(startpos, endpos);

		if(selectedData != ""){
			var fValue1 = getFieldValues(selectedData);
			var fName1 = getFieldNames(selectedData);

			gfiList[currentQueriedGFI]["camposRespuesta"][resultNum] = fName1;
			gfiList[currentQueriedGFI]["valoresRespuesta"][resultNum] = fValue1;

			gfiList[currentQueriedGFI]["camposRespuesta"]["count"]++;
			gfiList[currentQueriedGFI]["valoresRespuesta"]["count"]++;

			resultNum++;
		}

		reply = reply.substring(endpos);
		pos = reply.indexOf("<FIELDS ",1);
	}

	//alert("relleno otro " + currentQueriedGFI);

}

function calculaDistanciaCentroide(laX, laY, centroideX,centroideY) {
	var xD = Math.abs(laX - centroideX);
	var yD = Math.abs(laY - centroideY);
	var theDistance = Math.sqrt(Math.pow(xD,2) + Math.pow(yD,2));

	return theDistance;
}

function processResultQueryGFI_sitarDescarga(theReply, avoidUpdateGFIList){
	//TODO : Use XML DOM parser instead to parse the response

	var selectedData = "";
	var endpos = 1;
	var reply = theReply;
	resultNumber = 0;

	var pos = reply.indexOf("<FIELDS ",endpos);
	while (pos!=-1) {
		resultadoGFI_sitar["count"] = resultadoGFI_sitar["count"] + 1;
		startpos = pos + 8;
		endpos = reply.indexOf('"/>',startpos);
		selectedData = reply.substring(startpos,endpos);

		if(selectedData != ""){
			var fValue1 = getFieldValuesGFI(selectedData);
			var fName1 = getFieldNamesGFI(selectedData);

			var colecc = getResultGFI(fName1, fValue1, ".COLECCION");
			var estaGeorref = getResultGFI(fName1, fValue1, ".GEORREF");
			var _xMin = replaceChar(getResultGFI(fName1, fValue1, ".X1"), ",", ".");
			var _yMin = replaceChar(getResultGFI(fName1, fValue1, ".Y1"), ",", ".");
			var _xMax = replaceChar(getResultGFI(fName1, fValue1, ".X2"), ",", ".");
			var _yMax = replaceChar(getResultGFI(fName1, fValue1, ".Y2"), ",", ".");

			var puntoMedioFotogramaX = (_xMin*1) + ((_xMax - _xMin) / 2);
			var puntoMedioFotogramaY = (_yMin*1) + ((_yMax - _yMin) / 2);

			if (colecc != "") {
				if (! resultadoGFI_sitar[colecc]) {
					resultadoGFI_sitar[colecc] = new Array();
					resultadoGFI_sitar[colecc]["count"] = 0;

					listaColecc[listaColeccCount] = colecc;
					listaColeccCount++;
				}

				idxResult = resultadoGFI_sitar[colecc]["count"];

				resultadoGFI_sitar[colecc][idxResult] = new Array();
				resultadoGFI_sitar[colecc][idxResult]["camposRespuesta"] = fName1;
				resultadoGFI_sitar[colecc][idxResult]["valoresRespuesta"] = fValue1;

				var distanc = calculaDistanciaCentroide(puntoMedioFotogramaX, puntoMedioFotogramaY, xCoordUsr, yCoordUsr);
				resultadoGFI_sitar[colecc][idxResult]["distanciaCentroide"] = distanc;

				resultadoGFI_sitar[colecc][idxResult]["deltaX"] = 0;
				resultadoGFI_sitar[colecc][idxResult]["deltaY"] = 0;

				resultadoGFI_sitar[colecc]["count"]++;
			}
			resultNumber++;
		}

		reply = reply.substring(endpos);
		pos = reply.indexOf("<FIELDS ",1);

	}

	gfiList[currentQueriedGFI]["camposRespuesta"]["count"] = resultadoGFI_sitar["count"];
	gfiList[currentQueriedGFI]["valoresRespuesta"]["count"] = resultadoGFI_sitar["count"];

	//updateResultsGFI();
	//updateResultsGFI_SITAR();

}

function getResultsGFIHTML(i, j, k, l) {
	if ( (i==0) && (j==0) && (k==1)) {
		return getResultsGFIHTML_sitar();
	} else {
		return getResultsGFIHTML_generico(i, j, k, l);
	}
}

function ordenaColeccion(theCollection, startIdx) {
	var firstIdx = startIdx;
	var idxMinimum = getMinimunDistance(resultadoGFI_sitar[theCollection], firstIdx);
	var auxCR = resultadoGFI_sitar[theCollection][firstIdx]["camposRespuesta"];
	var auxVR = resultadoGFI_sitar[theCollection][firstIdx]["valoresRespuesta"];
	var auxDC = resultadoGFI_sitar[theCollection][firstIdx]["distanciaCentroide"];
	resultadoGFI_sitar[theCollection][firstIdx]["camposRespuesta"] = resultadoGFI_sitar[theCollection][idxMinimum]["camposRespuesta"];
	resultadoGFI_sitar[theCollection][firstIdx]["valoresRespuesta"] = resultadoGFI_sitar[theCollection][idxMinimum]["valoresRespuesta"];
	resultadoGFI_sitar[theCollection][firstIdx]["distanciaCentroide"] = resultadoGFI_sitar[theCollection][idxMinimum]["distanciaCentroide"];
	resultadoGFI_sitar[theCollection][idxMinimum]["camposRespuesta"] = auxCR;
	resultadoGFI_sitar[theCollection][idxMinimum]["valoresRespuesta"] = auxVR;
	resultadoGFI_sitar[theCollection][idxMinimum]["distanciaCentroide"] = auxDC;
	firstIdx++;
	if (firstIdx < resultadoGFI_sitar[theCollection]["count"]) {
		ordenaColeccion(theCollection, firstIdx);
	}
}

function getMinimunDistance(colecc, firstIdx) {
	var minimumDistanceValue = 999999;
	var minimumDistanceIndex = firstIdx;
	for (var resIdx = firstIdx; resIdx < colecc["count"]; resIdx++) {
		if (! isNaN(colecc[resIdx]["distanciaCentroide"])) {
			if (colecc[resIdx]["distanciaCentroide"] < minimumDistanceValue) {
				minimumDistanceIndex = resIdx;
				minimumDistanceValue = colecc[resIdx]["distanciaCentroide"];
			}
		}
	}
	return minimumDistanceIndex;
}

function ordenaCandidatos() {
	for (var colIdx = 0; colIdx < listaColeccCount; colIdx++) {
		theCollection = listaColecc[colIdx];
		ordenaColeccion(theCollection, 0);
	}
}

function getNextMinimum(oldMinimum) {
	var candidatoIdx = -1;
	var candidatoValue = 9999999;
	for (var colIdx = 0; colIdx < listaColeccCount; colIdx++) {
		if (listaAuxColecc[colIdx] <= candidatoValue) {
			if (listaAuxColecc[colIdx] > oldMinimum) {
				candidatoIdx = colIdx;
				candidatoValue = listaAuxColecc[colIdx]
			}
		}
	}
	return candidatoIdx;
}

var listaAuxColecc = new Array();
function ordenaColecciones() {
	var max = 9999;
	var posic = max;
	for (var colIdx = 0; colIdx < listaColeccCount; colIdx++) {
		theCollection = listaColecc[colIdx];
		posic = posic++;
			// si no tiene posicion, le genero un numero grande mayor a todos
		if (coleccEnOrden[theCollection]) {
			posic = coleccEnOrden[theCollection];
		} else {
			posic = max++;
		}
		listaAuxColecc[colIdx] = posic;
	}
	var currentMinimum = -1;
	var listaColeccAux = new Array();
	for (var colIdx = 0; colIdx < listaColeccCount; colIdx++) {
		var currentMinimumIdx = getNextMinimum(currentMinimum);
		currentMinimum = listaAuxColecc[currentMinimumIdx];
		listaColeccAux[colIdx] = listaColecc[currentMinimumIdx];
	}
	listaColecc = listaColeccAux;
}

function getResultsGFIHTML_sitar() {
	ordenaCandidatos();
	ordenaColecciones();

	var idContent = '';

	idContent += "<table class='search_map' width='100%' cellpadding='0' cellspacing='2'>";
	if (listaColeccCount == 0) {
		idContent += '<tr><td class="normal item" align="left">No hay datos</td></tr>';
	} else {
		idContent += '<tr class="item header">';
		idContent += '<td align="center" colspan="6">';
		idContent += '<input id="mantenerExtension" type="checkbox" checked alt="Mantener extensión actual del mapa">Mantener extensión actual del mapa</input>'; 
		idContent += '</td>';
		idContent += '</tr>';
		idContent += '<tr class="item header">';
		idContent += '<td align="center" colspan="6">';
		idContent += '</td>';
		idContent += '</tr>';
		for (var colIdx = 0; colIdx < listaColeccCount; colIdx++) {
			theCollection = listaColecc[colIdx];

			idContent += '<tr class="itemBig header blanco">';
			idContent += '<td align="left" colspan="6">';
			idContent += getCabeceraCollectionHTML(theCollection, colIdx);
			idContent += '</td>';
			idContent += '</tr>';

			idContent += '<tr>';
			idContent += '<td>';

			idContent += "<table id='colecc" + colIdx + "Table' style='display:none;visibility:hidden;table-layout:fixed;' class='search_map' width='100%' cellpadding='0' cellspacing='2'>";
			idContent += '<tr class="itemBig negro fondoGrisClaro">';
			idContent += '<td align="center" width="18%"><b>Nombre</b></td>';
			idContent += '<td align="center" width="14%"><b>Ver</b></td>';
			idContent += '<td align="center" width="14%"><b>Descargar</b></td>';
			idContent += '<td align="center" width="18%"><b>Fecha</b></td>';
			idContent += '<td align="center" width="18%"><b>Escala</b></td>';
			idContent += '<td align="center" width="18%"><b>Tama&ntilde;o de pixel</b></td>';
			//idContent += '<td align="center" width="20%"><b>Distancia a centroide</b></td>';
			idContent += '</tr>';

			var evenOdd;
			for (var resIdx = 0; resIdx < resultadoGFI_sitar[theCollection]["count"]; resIdx++) {
				if((resIdx%2) == 0) {
					evenOdd = "oddRow";
				}	else {
					evenOdd = "evenRow";
				}
				idContent += getItemGFI_descarga(theCollection, resIdx, false, evenOdd);
			}
			idContent += "</table>";
			idContent += '</td>';
			idContent += '</tr>';
		}
	}
	idContent += "</table>";

	return idContent;
}

function getCabeceraCollectionHTML(theCollection, colIdx) {
	var idContent = "";
	idContent += "<table width='100%' cellpadding='0' cellspacing='0'>";
	idContent += '<tr class="itemBig header blanco">';
	idContent += '<td align="left">';
	idContent += "<a href='javascript:toggleZoneCarpeta(\"colecc" + colIdx + "\", \"información de " + theCollection + "\")' title='Mostrar información de " + theCollection + "' id='colecc" + colIdx + "Link'>";
	idContent += "<img src='imagesTOC/old_icon_closed.gif' border='0'></img></a>";

	if (mdListURL[theCollection]) {
		idContent += '<a href="' + mdListURL[theCollection]  + '" title="Ver Leyenda y Metadato" target="_new"><img class="imgToc" border="0" src="imagesTOC/icon_layerMD.gif"/></a>';
	} else {
		idContent += '<img class="imgToc" onmousedown="alert(\'Este contenido no está catalogado\');" border="0" src="imagesTOC/icon_layerNO_MD.gif"/>';
	}
	idContent += theCollection + ' (' + resultadoGFI_sitar[theCollection]["count"] + ')';

	idContent += '</td>';
	idContent += '<td align="right">';

	var distancMejorCandidato = resultadoGFI_sitar[theCollection][0]["distanciaCentroide"];
		// como estan ordenados, sera el primero de la lista
	if (! isNaN(distancMejorCandidato)) {
		idContent += getItemGFI_descarga(theCollection, 0, true);
	}
	idContent += '</td>';
	idContent += '</tr>';
	idContent += '</table>';

	return idContent;
}

function getItemGFI_descarga(theCollection, resIdx, esCompacto, evenOdd) {
	var idContent = "";
	var fNam = resultadoGFI_sitar[theCollection][resIdx]["camposRespuesta"];
	var fVal = resultadoGFI_sitar[theCollection][resIdx]["valoresRespuesta"];

	var theName = getResultGFI(fNam, fVal, ".NAME");
	var estaGeorref = replaceChar(getResultGFI(fNam, fVal, ".GEORREF"), ",", ".");

	var capa = getResultGFI(fNam, fVal, ".CAPA");
	var servic = getResultGFI(fNam, fVal, ".SERVICIO");
	var locat = getResultGFI(fNam, fVal, ".LOCATION");
	var ruta = getResultGFI(fNam, fVal, ".PATH");
	var fecha = getResultGFI(fNam, fVal, ".FECHA");
	var escala = getResultGFI(fNam, fVal, ".ESCALA");
	var tamPx = getResultGFI(fNam, fVal, ".TAM_PIXEL");
	var formatos = getResultGFI(fNam, fVal, ".DESCARGAS");
	var formatosLista = formatos.split(",");
	var tamannos = getResultGFI(fNam, fVal, ".TAMANO");
	var tamannosLista = tamannos.split(",");

	var _xMin = replaceChar(getResultGFI(fNam, fVal, ".X1"), ",", ".");
	var _yMin = replaceChar(getResultGFI(fNam, fVal, ".Y1"), ",", ".");
	var _xMax = replaceChar(getResultGFI(fNam, fVal, ".X2"), ",", ".");
	var _yMax = replaceChar(getResultGFI(fNam, fVal, ".Y2"), ",", ".");

	var _deltaX = resultadoGFI_sitar[theCollection][resIdx]["deltaX"];
	var _deltaY = resultadoGFI_sitar[theCollection][resIdx]["deltaY"];

	//var distanc = resultadoGFI_sitar[theCollection][resIdx]["distanciaCentroide"];

	var altText = "Ver en cartoteca "  + theName + " (" + theCollection + ")";

	if (esCompacto) {
		if (locat != "") {
			idContent += '<a class="itemBlancoEnlace" align="right" title="' + altText + '" href="javascript:selectedImage(\'' + locat + '\',\'' + servic + '\',\'' + capa + '\',\'' + _xMin + '\',\'' + _yMin + '\',\'' + _xMax + '\',\'' + _yMax + '\',\'' + theCollection + '\', false, ' + (estaGeorref=='S') + ',' + _deltaX + "," + _deltaY + ',' + resIdx + ',' + escala + ',\'' + fecha +  '\')">Ver</a>&nbsp;';
		}
		//idContent += '&nbsp;&nbsp;&nbsp;<a class="itemBlancoEnlace" align="right" title="Descargar" href="' + ruta + '">Descargar</a>';
	} else {
		idContent += '<tr class="normal item ' + evenOdd + '">';
		idContent += '<td class="normal item" align="center">' + theName + '</td>';

		idContent += '<td class="normal item" align="center">';
		if (locat != "") {
			idContent += '<a class="normal itemEnlace" title="' + altText + '" href="javascript:selectedImage(\'' + locat + '\',\'' + servic + '\',\'' + capa + '\',\'' + _xMin + '\',\'' + _yMin + '\',\'' + _xMax + '\',\'' + _yMax + '\',\'' + theCollection + '\', false, ' + (estaGeorref=='S') + ',' + _deltaX + "," + _deltaY + ',' + resIdx + ',' + escala + ',\'' + fecha + '\')">';
			idContent += '<img border="0" src="images/verMini.png" title="' + altText + '"></a>&nbsp;';
		}
		idContent += '</td>';

		idContent += '<td class="normal item" align="center">';
		if (ruta != "&nbsp;") {
			for (var i = 0; i < formatosLista.length; i++) {
				var laRuta = ruta + theName + formatosLista[i];
				var rutaImg = "i/ico_default.png";
				var formatoTxt = "";
				switch (formatosLista[i]) {
					case ".dgn.zip": { rutaImg = "i/DGN.png"; formatoTxt = "DGN"; break; } 
					case ".dxf.zip": {	rutaImg = "i/DXF.jpg"; formatoTxt = "DXF"; break; }
					case ".dwg.zip": { rutaImg = "i/DWG.png"; formatoTxt = "DWG"; break; }
					case ".shp.zip": { rutaImg = "i/SHP.png"; formatoTxt = "Shapefile"; break; }
					case ".dgn": { rutaImg = "i/DGN.png"; formatoTxt = "DGN"; break; }
					case ".dxf": { rutaImg = "i/DXF.jpg"; formatoTxt = "DXF"; break; }
					case ".dwg": { rutaImg = "i/DWG.png"; formatoTxt = "DWG"; break; }
			 		case ".shp": { rutaImg = "i/SHP.png"; formatoTxt = "Shapefile"; break; }
			 		case ".gml": { rutaImg = "i/GML.png"; formatoTxt = "GML"; break; }
					case ".kml": { rutaImg = "i/KML.png"; formatoTxt = "KML"; break; }
					case ".kmz": { rutaImg = "i/KMZ.png"; formatoTxt = "KMZ"; break; }
					case ".jpg": { rutaImg = "i/JPG.png"; formatoTxt = "JPG"; break; }
					case ".jpg.zip": { rutaImg = "i/JPG.png"; formatoTxt = "JPG"; break; }
					case ".png": { rutaImg = "i/PNG.png"; formatoTxt = "PNG"; break; }
					case ".gif": { rutaImg = "i/GIF.png"; formatoTxt = "GIF"; break; }
					case ".tif": { rutaImg = "i/TIF.png"; formatoTxt = "TIF"; break; }
					case ".ecw": { rutaImg = "i/ECW.png"; formatoTxt = "ECW"; break; }
					case ".mdt": { rutaImg = "i/MDT.png"; formatoTxt = "MDT"; break; }
					case ".asc": { rutaImg = "i/ASC.png"; formatoTxt = "ASC"; break; }
					case ".avi": { rutaImg = "i/AVI.png"; formatoTxt = "AVI"; break; }
					case ".mpg": { rutaImg = "i/MPG.png"; formatoTxt = "MPG"; break; }
					case ".dbf": { rutaImg = "i/DBF.png"; formatoTxt = "DBF"; break; }
					case ".xls": { rutaImg = "i/XLS.png"; formatoTxt = "XLS"; break; }
					case ".mdb": { rutaImg = "i/MDB.png"; formatoTxt = "MDB"; break; }
					case ".csv": { rutaImg = "i/CSV.png"; formatoTxt = "CSV"; break; }
					case ".tab": { rutaImg = "i/TAB.png"; formatoTxt = "TAB"; break; }
					case ".pdf": { rutaImg = "i/PDF.png"; formatoTxt = "PDF"; break; }
					case ".doc": { rutaImg = "i/DOC.png"; formatoTxt = "DOC"; break; }
					case ".odf": { rutaImg = "i/ODF.png"; formatoTxt = "ODF"; break; }
					case ".xml": { rutaImg = "i/XML.png"; formatoTxt = "XML"; break; }
					case ".txt": { rutaImg = "i/TXT.png"; formatoTxt = "Texto"; break; }
					case ".zip": { rutaImg = "i/ZIP.png"; formatoTxt = "ZIP"; break; }
				}

				var tam = "";
				if (tamannosLista[i]) {
					if (tamannosLista[i] != "&nbsp;") {
						tam = " (" + tamannosLista[i] + ")";
					}
				}
				
				idContent += '<a class="normal itemEnlace" align="right" title="Descargar en formato ' + formatoTxt + tam + '" href="#" >';
				idContent += '<img border="0" src="' + rutaImg + '" alt="Descargar ' + theName + " (" + theCollection + ')" onclick="window.open(\'' + laRuta + '\')"></a>&nbsp;&nbsp;';
			}
		} else {
			idContent += '-';
		}
		idContent += '</td>';

		idContent += '<td class="normal item" align="center">' + fecha + '</td>';
		idContent += '<td class="normal item" align="center">' + escala + '</td>';
		idContent += '<td class="normal item" align="center">' + tamPx + '</td>';

	
		/*
			if (estaGeorref == 'S') {
				idContent += '<td class="normal item" align="center">' + RoundDecimal(distanc, 2) + ' metros</td>';
			} else {
				idContent += '<td class="normal item" align="center">N/A</td>';
			}
		*/
		idContent += '</tr>';
	}
	return idContent;
}

function getResultsGFIHTML_generico(i, j, k, l) {
	var idContent = '';

	idContent += "<table class='search_map' width='100%' cellpadding='0' cellspacing='2'>";


	var idx = restrictions[i][j][k][l]["posicion"];

			if (gfiList[idx]["camposRespuesta"]["count"]>0) {
				idContent += '<tr class="itemBig negro fondoGrisClaro">';
				idContent += '<td align="center" width="35%"><b>Nombre de la v&iacute;a</b></td>';
				idContent += '<td align="center" width="7%"><b>N&uacute;mero</b></td>';
				idContent += '<td align="center" width="20%"><b>Referencia catastral</b></td>';
				idContent += '<td align="center" width="20%"><b>Coord X</b></td>';
				idContent += '<td align="center" width="20%"><b>Coord Y</b></td>';
				idContent += '</tr>';

				var evenOdd;


				for (var kk = 0; kk < gfiList[idx]["camposRespuesta"]["count"]; kk++) {

					fNam = gfiList[idx]["camposRespuesta"][kk];
					fVal = gfiList[idx]["valoresRespuesta"][kk];

					if((kk%2) == 0) {
						evenOdd = "oddRow";
					}	else {
						evenOdd = "evenRow";
					}

					var elNomVia = getResultGFI(fNam, fVal, "CARTO.T04_DIRCAT.NOMBRE_VIA");
					var elNum = getResultGFI(fNam, fVal, "CARTO.T04_DIRCAT.NUMERO");
					var laRefCat = getResultGFI(fNam, fVal, "CARTO.T04_TROIDES.REFCAT");
					var laX = getResultGFI(fNam, fVal, "CARTO.T04_TROIDES.XCEN");
					var laY = getResultGFI(fNam, fVal, "CARTO.T04_TROIDES.YCEN");

					idContent += '<tr class="normal item ' + evenOdd + '">';

					idContent += '<td class="normal item" align="center">' + elNomVia + '</td>';
					idContent += '<td class="normal item" align="center">' + elNum + '</td>';
					idContent += '<td class="normal item" align="center">' + laRefCat + '</td>';
					idContent += '<td class="normal item" align="center">' + laX + '</td>';
					idContent += '<td class="normal item" align="center">' + laY + '</td>';

					idContent += '</tr>';
				}
			}



	idContent += "</table>";

	return idContent;
}


function selectedImage(_locat, _servicio, _capa, _bboxXMin, _bboxYMin, _bboxXMax, _bboxYMax, _theCollection, hideResultsBack, _isGeorref, _deltaX, _deltaY, _resIdx, _escala, _fecha) {
	var str = urlCartoteca + "?LOCAT=" + _locat;
	str += "&SERVICIO=" + _servicio;
	str += "&CAPA=" + _capa;

	if (document.getElementById('mantenerExtension').checked) {
		if (getMapScale(false) > _escala) {
			str += "&BBOXFOTOGRAMA=" + xCoordUsr + ":" + yCoordUsr + ":" + xCoordUsr + ":" + yCoordUsr;
			str += "&ESCALA=" + _escala; 
		} else {
			str += "&BBOXFOTOGRAMA=" + minx + ":" + miny + ":" + maxx + ":" + maxy;
		}
	} else {
		str += "&BBOXFOTOGRAMA=" + _bboxXMin + ":" + _bboxYMin + ":" + _bboxXMax + ":" + _bboxYMax;
	}
	str += "&COLECCION=" + _theCollection;
	str += "&ISGEORREF=" + _isGeorref;
	str += "&FECHA=" + _fecha;

	window.open(str);
}

function getResultGFI(fName1, fValue1, nomCampo) {
	idx = -1;
			// buscar la posicion del campo
	for (var ii=0;ii<fName1.length;ii++) {
		nombreCampo = fName1[ii];

		if (nombreCampo.indexOf(nomCampo) != -1) {
			idx = ii;
		}
	}
	if (idx != -1) {
		return corrigeCaracteresExtranos(fValue1[idx]);
	} else {
		return "";
	}
}

// get a list of field names from the returned record
function getFieldNamesGFI(recordString) {
	var theStuff = new String(recordString);
	var theList = theStuff.split('" ');
	var fName1 = new Array();
	for (var f=0;f<theList.length;f++) {
		var v = theList[f].split('="');
		fName1[f] = v[0];
	}
	return fName1;

}
// get a list field values from the returned record
function getFieldValuesGFI(recordString) {
	var theStuff = new String(recordString);
	var theList = theStuff.split('" ');
	var fValue1 = new Array();
	for (var f=0;f<theList.length;f++) {
		var v = theList[f].split('="');
		if ((v[1]=="") || (v[1]==null)) v[1] = "&nbsp;";
		fValue1[f] = v[1];
	}
	return fValue1;
}

