﻿var xmlhttp

function showHint(str) {
	if (str.length==0)
	  { document.getElementById("txtHint").innerHTML=""; return; }
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	  { alert ("Your browser does not support XMLHTTP!"); return; } 
	var url="cfsuggest.cfm";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged() {
	if (xmlhttp.readyState==4)
	  { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; document.getElementById('txtHint').style.display='inline'; }
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest)
	  {
	  	// code for IE7+, Firefox, Chrome, Opera, Safari
	  	return new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  	// code for IE6, IE5
	  	return new ActiveXObject("Microsoft.XMLHTTP");
	  }
	return null;
}

function CFSHide() { 
	document.getElementById('txtHint').style.display='none';
	document.getElementById('txt1').value = document.getElementById('baseSearch').value;
}

function arrowKeys(curKey) {
	var evtobj=window.event? event : curKey //distinguish between IE's explicit event object (window.event) and Firefox's implicit.
	if (evtobj.keyCode == 38) {
		// up key pressed
		divList = document.getElementById('txtHint').getElementsByTagName('div');
		curSel = 'none';
		for (i = 0; i < divList.length; i++) {
			if (divList[i].style.backgroundColor!= '') {
				curSel = i;
			}
			divList[i].style.backgroundColor = '';
		}
		if (curSel == 'none') {
			divList[0].style.backgroundColor = '#B5D5FF';
			document.getElementById('txt1').value = divList[0].getElementsByTagName('a')[0].name;
		} else if (curSel == divList.length) {
			divList[curSel].style.backgroundColor = '#B5D5FF';
			document.getElementById('txt1').value = divList[i].getElementsByTagName('a')[0].name;
		} else {
			divList[curSel-1].style.backgroundColor = '#B5D5FF';
			document.getElementById('txt1').value = divList[curSel-1].getElementsByTagName('a')[0].name;
		}
	} else if (evtobj.keyCode == 40) {
		// down key pressed
		divList = document.getElementById('txtHint').getElementsByTagName('div');
		curSel = 'none';
		for (i = 0; i < divList.length; i++) {
			if (divList[i].style.backgroundColor != '') {
				curSel = i;
			}
			divList[i].style.backgroundColor = '';
		}
		if (curSel == 'none') {
			divList[0].style.backgroundColor = '#B5D5FF';
			document.getElementById('txt1').value = divList[0].getElementsByTagName('a')[0].name;
		} else if (curSel == divList.length) {
			divList[curSel].style.backgroundColor = '#B5D5FF';
			document.getElementById('txt1').value = divList[i].getElementsByTagName('a')[0].name;
		} else {
			divList[curSel+1].style.backgroundColor = '#B5D5FF';
			document.getElementById('txt1').value = divList[curSel+1].getElementsByTagName('a')[0].name;
		}
		document.getElementById('txt1').value = divList[i].getElementsByTagName('a')[0].name;
	} else {
		showHint(document.getElementById('txt1').value);
		document.getElementById('baseSearch').value = document.getElementById('txt1').value;
	}
}

function clearArrow() {
	divList = document.getElementById('txtHint').getElementsByTagName('div');
	for (i = 0; i < divList.length; i++) {
		divList[i].style.backgroundColor = '';
	}
	document.getElementById('txt1').value = document.getElementById('baseSearch').value;
}

function checkHighlight() {
	if (document.getElementById('txtHint').style.display != 'none') {
		divList = document.getElementById('txtHint').getElementsByTagName('div');
		for (i = 0; i < divList.length; i++) {
			if (divList[i].style.backgroundColor != '') {
				document.getElementById('txt1').value = divList[i].getElementsByTagName('a')[0].name;
			}
		}
	}
	return true;
}
