/*
Copyright 2007 GroundWork Open Source, Inc. ("GroundWork")
All rights reserved. This program is free software; you can redistribute it and/or modify it under 
the terms of the GNU General Public License version 2 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA 02110-1301, USA.  
*/

function clearSuggest(){
		var popdown = document.getElementById('search_suggest')
		popdown.innerHTML = '';

}

function getRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Browser not supported");
	}
}


var searchReq = getRequestObject();

var global_elemID = "";
//Called from keyup on the search textbox.
//Starts the AJAX request.
function searchSuggest(myElemID,searchType) {
	elemID = myElemID;
	if (searchReq.readyState == 4 || searchReq.readyState == 0) {
		var str = escape(document.getElementById(elemID).value);
		searchReq.open("GET", '/monitor/javascript/searchSuggest.php?search=' + str + '&searchType=' + searchType, true);
		searchReq.onreadystatechange = handleSearchSuggest; 
		searchReq.send(null);
	}		
}

//Called when the AJAX response is returned.
function handleSearchSuggest() {
	if (searchReq.readyState == 4) {
		var ss = document.getElementById('search_suggest')
		ss.innerHTML = '';
		var str = searchReq.responseText.split("\n");
		for(i=0; i < str.length - 1; i++) {
			//Build our element string.  This is cleaner using the DOM, but
			//IE doesn't support dynamically added attributes.
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
			suggest += 'onmouseout="javascript:suggestOut(this);" ';
			suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
			suggest += 'class="suggest_link">' + str[i] + '</div>';
			ss.innerHTML += suggest;
		}
	}
}

//Mouse over function
function suggestOver(div_value) {
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {
	var myElemID = elemID;
	if(value.match('more not shown') == null){
		document.getElementById(myElemID).value = value;
		document.getElementById('search_suggest').innerHTML = '';
	}
	
	
}

