update = false;
added = false;
deadline = null;
timeoutID = null;

function idp_live_search_gate(term,people) {
	if (timeoutID == null) {	
		timeoutID = setTimeout("idp_live_search('"+term+"',"+people+")", 500);
	} else {
		clearTimeout(timeoutID);
		timeoutID = setTimeout("idp_live_search('"+term+"',"+people+")", 500);
	}
}

function idp_live_search (term,people) {
	if (people) mode = 'people';
	else mode = 'idp';
	update = true;
	
	if (update) {
		var request = new XMLHttpRequest();
		request.onreadystatechange = function() {
			if (request.readyState == 4) {
				if (request.status == 200) {
					if (added) { //remove old results
						results = document.getElementById('TARGET');
						while ( results.childNodes.length >= 1 ) {
        					results.removeChild(results.firstChild);
    					}
						added = false;
					}
					
					if (term.length > 2) { //only if actual query, longer than 2 chars
						idpArray = (request.responseText).split("::");
						
						results = document.getElementById('TARGET');
						if (idpArray.length > 1) { //only if result
								for (var i in idpArray) {
									if (i != idpArray.length-1) {
										if (people) {
											nameID = idpArray[i].split("##");
											idpDiv = document.createElement("div");
									
											idpDiv.innerHTML = '<strong>'+nameID[0]+'</strong><br />';
											idpDiv.innerHTML += '<a href=\"wdp/entry/'+nameID[1]+'\"><em>More Info</em></a>&nbsp;&nbsp;';
											if(nameID[2] != ' ') idpDiv.innerHTML += 'Phone:'+nameID[2]+'&nbsp;&nbsp;';
											if(nameID[3] != ' ') idpDiv.innerHTML += 'Email:<a href=\"mailto:'+nameID[3]+'\">'+nameID[3]+'</a>';
											idpDiv.innerHTML += '<br /><br />';
										} else {
											nameID = idpArray[i].split("##");
											idpDiv = document.createElement("div");
									
											idpDiv.innerHTML = '<strong>'+nameID[0]+'</strong><br />';
											idpDiv.innerHTML += '<a href=\"idp/entry/'+nameID[1]+'\"><em>More Info</em></a>&nbsp;&nbsp;';
											if(nameID[2] != ' ') idpDiv.innerHTML += 'Phone:'+nameID[2]+'&nbsp;&nbsp;';
											if(nameID[3] != ' ') idpDiv.innerHTML += 'Email:<a href=\"mailto:'+nameID[3]+'\">'+nameID[3]+'</a>';
											idpDiv.innerHTML += '<br /><br />';
										}
										(document.getElementById("TARGET")).appendChild(idpDiv);
									}
								}
						} else { // no results
							idpDiv = document.createElement("div");
									
							idpDiv.innerHTML = '<strong>No Records Found</strong><br />';
							idpDiv.innerHTML += '<a href=\"/idp/idp/advsearch\"><em>Try an advanced search</em></a>';
							(document.getElementById("TARGET")).appendChild(idpDiv);
						} // end no results
						added = true;

					} //end null search term
				} else {
					alert(request.status);
				} //endif
			} //endif
		} //end function callback

		request.open("GET", "/scripts/searchResults.php?mode="+mode+"&term="+term,true);
		request.send(null);
		
		update = false;
	} //endif update
	
	if (highlight >=0) {
		document.getElementById('searchBox').childNodes[highlight].className = 'highlight';
		document.getElementById('searchBox').childNodes[highlight+1].className = (highlight % 2 == 0) ? 'first' : 'second';
		document.getElementById('searchBox').childNodes[highlight-1].className = (highlight % 2 == 0) ? 'first' : 'second';
	}
}