useSuggest = 0;

function initSuggest(searchbox,formname,searchtype) {
	suggestSelect = document.getElementById('kwlookup');
	suggestDiv =  document.getElementById('kwlookupdiv');
	suggestInput = document.getElementById(searchbox);		
	suggestForm = eval('document.' + formname);
	
	if (searchtype == 'fullTextSearch') {
		useSuggest = 0;
		suggestDiv.style.visibility = 'hidden';
	} else {
		useSuggest = 1;
	}	
	
	suggestSelect = document.getElementById('kwlookup');
	suggestDiv =  document.getElementById('kwlookupdiv');
	suggestInput = document.getElementById(searchbox);		
	suggestForm = eval('document.' + formname);
	si = 0;
	
	var fp = findPos(suggestInput);
	suggestDiv.style.left = fp[0] + 'px';
	suggestDiv.style.top = fp[1] + suggestInput.offsetHeight + 'px';
	
	if (searchbox == 'searchInputA') {
		suggestSelect.style.width = suggestInput.style.width;
		suggestSelect.style.fontSize = '11px';
	} else if (searchbox == 'searchInputB') { 
		suggestSelect.style.width = suggestInput.style.width;	
		suggestSelect.style.fontSize = '12px';
	}
}

function choosekw(val) {
	if (typeof(val) == 'string') {
		suggestInput.value = val;
	}
	suggestDiv.style.visibility = 'hidden';
	document.body.style.cursor = "wait";
	suggestForm.submit();
}

function checksearchkeys(e) {
	if (!e) e=window.event;
	switch(e.keyCode){
		case 37: case 38: case 39:  return 0;
		case 40: //down
			if (suggestDiv.style.visibility == 'visible') {
				suggestInputValueOld = suggestInput.value;
				suggestSelect.focus();
				suggestSelect.selectedIndex=0;
				suggestInput.value = suggestSelect.options[0].value;
				return 0;
			}
		}
	return 1;
}

function checkselectkeys(e) {
	if (!e) {e=window.event;}
	switch(e.keyCode){
		case 37: case 39:  break;
		case 38: case 27:
			if ((e.keyCode == 27) || (suggestSelect.selectedIndex==0 && si == 0)) {
				suggestSelect.selectedIndex=-1;
				suggestInput.value = suggestInputValueOld;
				suggestDiv.style.visibility = 'hidden';
				suggestInput.focus();
			} else {
				si = suggestSelect.selectedIndex;
				suggestInput.value = suggestSelect.options[si].value;
			}	
			break;
		case 40:
			si = suggestSelect.selectedIndex;
			suggestInput.value = suggestSelect.options[si].value;
			break;
		case 13: 
			choosekw();		
			break;
	}
}

function doseek(val,e) {
	if (!useSuggest) {
		return;
	}
	if (!val.length) {
		suggestDiv.style.visibility = 'hidden';
		return;	
	}	
	if (!checksearchkeys(e)) {
		return;
	}
	
	AjaxRequest.post(
	  		{
	    		'url':'/shared/components/keywordlookup.cfm'
	    		,'parameters':{'inputValue':escape(val)}
	    		,'onSuccess':function(req) { processResult(req.responseText); }
	    		,'onError':function(req){ processError(req.responseText); }
	    		,'timeout':6000
	    	}
		);
}	

function processResult(resp){
	var respAr = resp.split(",");
	var respArlen = respAr.length;
	if (resp.length) {
		suggestDiv.style.visibility = 'visible';
	} else {
		suggestDiv.style.visibility = 'hidden';
	}
	suggestSelect.innerHTML = '';
	if (respArlen > 6) {
		suggestSelect.size = 6;
	} else {
		suggestSelect.size = respArlen;
	}
	for (x=0;x<respArlen;x++) {
		suggestSelect.options[x] = new Option(respAr[x],respAr[x],false,false);
	}
}

function processError(resp,nodeid) {
	if (nodeid) {
		document.getElementById(nodeid).style.backgroundColor = 'FF0000';
	}
	alert('Error!\n'+resp);
	if (nodeid) {
		setTimeout("clearbkgd('" + nodeid + "')", 2000);
		document.getElementById(nodeid).focus();
	}
}	

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

