function getData(obj) {
	// finds all TDs inside obj
	var arrTD = obj.getElementsByTagName('TD');
	// assigns TD value to form field
	document.getElementById('searchstring').value = arrTD[0].innerHTML.replace(/^\s+/g,"");
	// hides the DIV
	document.getElementById('searchstringSuggestItems').style.display = 'none';
}

function htmlFormat(arr) {
	// formats arr as an HTML table
	var output = '<table class="suggestList"><tbody>';
	for (var i=0;i<arr.length;i++) {
		output = output + '<tr onmouseover="this.style.backgroundColor=0xeeeeee;" onmouseout="this.style.backgroundColor=0xffffff;" onclick="getData(this)">' + '<td>' + arr[i] + '</td>' + '</tr>';
	}
	output = output + '</tbody></table>';
	return output;
}
	
function doSearch(searchString) {
	var sugItems = document.getElementById('searchstringSuggestItems');
	
	if(searchString.length>0) {
		// creates XMLHttpRequest object
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		// request countries.cfm passing searchString as an URL parameter
		req.open("GET", "./search_suggest.cfm?search=" + searchString, true);
		req.send(null);
		
		// gives the request object an event handler
		req.onreadystatechange = function() {
		//alert(req.readyState);
		//alert(req.status);
			if ((req.readyState == 4) && (req.status == 200)) {
				// creates an array from returned list
				var arr = req.responseText.split(',');
				
				if (arr.length) {
					// formats array as an HTML table and shows the DIV
					sugItems.innerHTML = htmlFormat(arr);
					sugItems.style.display = 'block';
				}
				// No items found? Hides the DIV
				else sugItems.style.display = 'none';
				return;
			}
		}
	}
	
	// Empty searchString? Hides the DIV
	else sugItems.style.display = 'none';
	return;
}	
