/*
 * Renders results from services
 */
function renderResults(parent, resultsXML, EventManager) {
	var title = resultsXML.getAttribute('title');
	var rows = resultsXML.getElementsByTagName('row');
	var header = resultsXML.getElementsByTagName('header')[0];

	if(rows.length > LAYER_MAXIMUM_OBJECTS) { alert(SERVICE_OVERLOAD_MESSAGE); return true; }
	var titleElement = document.createElement('h3');
	if(title) {
		titleElement.appendChild(document.createTextNode(title));
	}
	parent.appendChild(titleElement);

	var resultsTable = document.createElement('table');
	var resultsBody = document.createElement('tbody');
	resultsTable.appendChild(resultsBody);

	if(header) {
		var headerElms = header.getElementsByTagName('col');
		
		var headerRow = document.createElement('tr');
		var headerSpace = document.createElement('td');
		headerSpace.className='RowId';
		headerRow.appendChild(headerSpace);
		for(var i = 0; i < headerElms.length; i++) {
			var val = headerElms[i].getAttribute('value');
			var cell = document.createElement('th');
			cell.appendChild(document.createTextNode(val));
			headerRow.appendChild(cell);
		}
		resultsBody.appendChild(headerRow);
	}
	for(var i = 0; i < rows.length; i++) {
		var resultsRow = document.createElement('tr');

		var rowTitle = rows[i].getAttribute('value');
		if(!rowTitle) {
			rowTitle = i+1;
		}
		var rowId = document.createElement('td');
		rowId.className = 'RowId';
		rowId.appendChild(document.createTextNode(rowTitle));

		resultsRow.appendChild(rowId);
		var cols = rows[i].getElementsByTagName('col');
		for(var c = 0; c < cols.length; c++) {
			var resultsCell = document.createElement('td');
			var cellType = cols[c].getAttribute('type');
			var valueParent = resultsCell;
			if(!cellType) {
				valueParent.appendChild(document.createTextNode(cols[c].getAttribute('value')));
			} else if(cellType.match(/pan/i) || cellType.match(/zoomto/i)) {
				var panLink = document.createElement('a');
				var panX = cols[c].getAttribute('x');
				var panY = cols[c].getAttribute('y');
				var pVal = cols[c].getAttribute('label');
				if(pVal == null) { pVal = 'Point of Interest'; }
				/*panLink.href = 'javascript:jumpToXY("'+pVal+'",'+panX+','+panY+');';
				panLink.href = 'javascript:opener.Events["'+window.name+'"].changePointScale(new Point('+panX+','+panY+'), 8)';*/
				panLink.href = 'javascript:function nil() { return; }';
				panLink.x = panX;
				panLink.y = panY;
				panLink.onclick = function () {
					var modCirc = new Circle();
					modCirc.Center = new Point(this.x, this.y);
					modCirc.Radius = 4;
					modCirc.Markup = true;
					EventManager.triggerEvent('navigate', modCirc);
				}
				panLink.appendChild(document.createTextNode(cols[c].getAttribute('value')));
				valueParent.appendChild(panLink);
			} else if(!cellType.match(/image/i) && cols[c].getAttribute('href')) {
				var panLink = document.createElement('a');
				panLink.target = "_blank";
				panLink.href = unescape(cols[c].getAttribute('href'));
				panLink.appendChild(document.createTextNode(cols[c].getAttribute('value')));
				valueParent.appendChild(panLink);
			} else if(cellType.match(/internal/i)) {
				var panLink = document.createElement('a');
				panLink.href = 'javascript:'+cols[c].getAttribute('command');
				panLink.appendChild(document.createTextNode(cols[c].getAttribute('value')));
				valueParent.appendChild(panLink);
			} else if(cellType.match(/image/i)) {
				var w = cols[c].getAttribute('width');
				var h = cols[c].getAttribute('height');
				var src = cols[c].getAttribute('src');
				var href = cols[c].getAttribute('href');

				var newImg = document.createElement('img');
				if(w) { newImg.width = w; }
				if(h) { newImg.height = h; }
				newImg.src = src;
				if(href) {
					var link = document.createElement('a');
					link.href = href;
					link.target = "_blank";
					link.appendChild(newImg);
					valueParent.appendChild(link);
				} else {
					valueParent.appendChild(newImg);
				}
			} else {
				valueParent.appendChild(document.createTextNode(cols[c].getAttribute('value')));
			}
			resultsRow.appendChild(valueParent);
		}
		resultsBody.appendChild(resultsRow);
	}
	
	parent.appendChild(resultsTable);
}


