if (!Prototype) {
	alert("The search combo box script requires the Prototype library, which is not available.");
}

var SearchCombo = {};
var DISPLAY_ROW_COUNT=10;
var CHAR_WIDTH=7;
var DEFAULT_WINDOW_WIDTH=250;
var ESTIMATED_SCROLLBAR_WIDTH = 22;
var DISPLAY_HEIGHT=155;
var HIGHLIGHT_BGCOLOR="#0a246a";
var HIGHLIGHT_COLOR="#ffffff";
var toggleShow=new Image();
var toggleHide=new Image();
var isIE=false;
var winTimeout=null;
var alreadyInit = [];
function initSearchComboBox(controlIdx) {
	if (!alreadyInit[controlIdx]) {
		alreadyInit[controlIdx] = true;
		if (navigator.userAgent.indexOf("Safari")>0) {
			document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).addEventListener("keydown",captureKeyPress,false);
		} else if (navigator.product=="Gecko") {
			document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).addEventListener("keypress",captureKeyPress,false);
		} else {
			document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).attachEvent('onkeydown',captureKeyPress);
			isIE=true;
		}
		LIMIT_RESULTS[controlIdx]=DISPLAY_ROW_COUNT;
		toggleShow.src=button_graphics[controlIdx].SHOWDROPDOWN;
		toggleHide.src=button_graphics[controlIdx].HIDEDROPDOWN;
		resizeToggleSearchButton(controlIdx);
	}
}
function toggleHighlight(highlight,controlIdx,state) {
	if (state) {
		highlight.setAttribute("id","searchhighlight_"+controlIdx);
		highlight.style.backgroundColor=HIGHLIGHT_BGCOLOR;
		highlight.style.color=HIGHLIGHT_COLOR;
		highlight.style.paddingRight="1";
	} else {
		highlight.style.backgroundColor="";
		highlight.style.color="";
		highlight.style.paddingRight="0";
		highlight.removeAttribute("id");
	}
}
function findSearchHighlight(controlIdx) {
	return document.getElementById("searchhighlight_"+controlIdx);
}
function captureKeyPress(event) {
	var srcElement;
	if (event&&event.target) {
		srcElement=event.target;
		if (srcElement.nodeType == 3) {
			srcElement=srcElement.parentNode;
		}
	} else if (window.event) {
		srcElement=window.event.srcElement;
	}
	var controlIdx=srcElement.getAttribute('controlIdx');
	var searchresult=document.getElementById(control_ids[controlIdx].SEARCHRESULT);
	var totalItems;
	if (event.keyCode == Event.KEY_DOWN) {
		if (isDropDownHidden(controlIdx)) {
			showDropDown(controlIdx);
			return;
		}
		highlight=findSearchHighlight(controlIdx);
		if (!highlight) {
			if (document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild!=null) {
				highlight=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.firstChild;
			}
		} else {
			toggleHighlight(highlight,controlIdx,0);
			highlight=highlight.nextSibling;
			if (!highlight) {
				if (document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild!=null) {
					highlight=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.firstChild;
				}
			}
		}
		if (highlight) {
			toggleHighlight(highlight,controlIdx,1);
			totalItems=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.childNodes.length;
			if (totalItems>DISPLAY_ROW_COUNT) {
				if (searchresult.scrollTop>0&&searchresult.scrollTop>highlight.offsetTop) {
					searchresult.scrollTop=0;
				}
				if (Math.abs(highlight.offsetTop-searchresult.scrollTop-searchresult.offsetHeight)<highlight.offsetHeight) {
					searchresult.scrollTop+=highlight.offsetHeight;
				}
			}
		}
		Event.stop(event);
		searchBoxFocus(controlIdx,true);
	} else if (event.keyCode == Event.KEY_UP) {
		if (isDropDownHidden(controlIdx)) {
			showDropDown(controlIdx);
			return;
		}
		highlight=findSearchHighlight(controlIdx);
		if (!highlight) {
			if (document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild!=null) {
				highlight=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.lastChild;
			}
		} else {
			toggleHighlight(highlight,controlIdx,0);
			highlight=highlight.previousSibling;
			if (!highlight) {
				if (document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild!=null) {
					highlight=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.lastChild;
				}
			}
		}
		if (highlight) {
			toggleHighlight(highlight,controlIdx,1);
			totalItems=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.childNodes.length;
			if (totalItems>DISPLAY_ROW_COUNT) {
				if (searchresult.scrollTop<highlight.offsetHeight&&highlight.offsetTop>searchresult.offsetHeight) {
					searchresult.scrollTop=searchresult.scrollHeight-searchresult.offsetHeight;
				}
				if (highlight.offsetTop-searchresult.scrollTop<0) {
					searchresult.scrollTop-=highlight.offsetHeight;
				}
			}
		}
		Event.stop(event);
		searchBoxFocus(controlIdx,true);
	} else if (event.keyCode == Event.KEY_RETURN) {
		submitSearch(controlIdx);
		event.returnValue=false;
		Event.stop(event);
	} else if (event.keyCode == Event.KEY_TAB) {
		highlight=findSearchHighlight(controlIdx);
		if (highlight) {
			submitSearch(controlIdx);
		} else {
			hideDropDown(controlIdx);
		}
		setCaretToStart(document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT));
	} else if (event.keyCode == Event.KEY_ESC) {
		if (isDropDownHidden(controlIdx)) {
			$(control_ids[controlIdx].SEARCHKEYTEXT).value = '';
		} else {
			hideDropDown(controlIdx);
		}
		event.returnValue=false;
		Event.stop(event);
	} else if (event.keyCode == Event.KEY_BACKSPACE || event.keyCode == Event.KEY_DELETE) {
		startSearch(controlIdx,false);
	} else if (event.keyCode == Event.KEY_PAGEDOWN) {
		showDropDown(controlIdx);
		highlight=findSearchHighlight(controlIdx);
		if (highlight) {
			toggleHighlight(highlight,controlIdx,0);
			for(i=0;i<10;i++) {
				if (!highlight.nextSibling) {
					break;
				} else {
					highlight=highlight.nextSibling;
					totalItems=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.childNodes.length;
					if (totalItems>DISPLAY_ROW_COUNT) {
						if (searchresult.scrollTop>0&&searchresult.scrollTop>highlight.offsetTop) {
							searchresult.scrollTop=0;
						}
						if (Math.abs(highlight.offsetTop-searchresult.scrollTop-searchresult.offsetHeight)<highlight.offsetHeight) {
							searchresult.scrollTop+=highlight.offsetHeight;
						}
					}
				}
			}
			toggleHighlight(highlight,controlIdx,1);
			Event.stop(event);
		}
	} else if (event.keyCode == Event.KEY_PAGEUP) {
		showDropDown(controlIdx);
		highlight=findSearchHighlight(controlIdx);
		if (highlight) {
			toggleHighlight(highlight,controlIdx,0);
			for(i=0;i<10;i++) {
				if (!highlight.previousSibling) {
					break;
				} else {
					highlight=highlight.previousSibling;
					totalItems=document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild.childNodes.length;
					if (totalItems>DISPLAY_ROW_COUNT) {
						if (searchresult.scrollTop<highlight.offsetHeight&&highlight.offsetTop>searchresult.offsetHeight) {
							searchresult.scrollTop=searchresult.scrollHeight-searchresult.offsetHeight;
						}
						if (highlight.offsetTop-searchresult.scrollTop<0) {
							searchresult.scrollTop-=highlight.offsetHeight;
						}
					}
				}
			}
			toggleHighlight(highlight,controlIdx,1);
			Event.stop(event);
		}
	} else if (event.keyCode == Event.KEY_HOME || event.keyCode == Event.KEY_END) {
		//do nothing
	} else {
		$$('span.errorText').invoke('hide');
		startSearch(controlIdx,false);
	}
}
function startSearch(controlIdx,isMoreSearch) {
	//Use timeout to allow keypress to propagate before searching.
	if (winTimeout) {
		window.clearTimeout(winTimeout);
	}
	winTimeout = window.setTimeout("performSearch(" + controlIdx + ", " + isMoreSearch + ")", 10);
}
function parseSearchUrl(searchKeyText,top,controlIdx, isMoreSearch) {
	var searchUrl=control_params[controlIdx].SEARCHURL;
	if (searchUrl.indexOf('?')==-1) {
		searchUrl=searchUrl+'?';
	} else {
		searchUrl=searchUrl+'&';
	}
	searchUrl=searchUrl+'top='+top+'&controlIdx='+controlIdx+'&searchKeyText='+encodeURIComponent(searchKeyText);
	var lastSearchKeyValue = '';
	if (isMoreSearch) {
		lastSearchKeyValue = $(control_ids[controlIdx].SEARCHKEYVALUE).value;
	}
	if (lastSearchKeyValue!='') {
		searchUrl=searchUrl+'&lastSearchKeyValue='+lastSearchKeyValue;
	}
	return searchUrl;
}
function performSearch(controlIdx,isMoreSearch) {
	var searchKeyText, searchUrl;
	if (LIMIT_RESULTS[controlIdx]<1) {
		initSearchComboBox(controlIdx);
	}
	if (xmlHttpReq[controlIdx]&&xmlHttpReq[controlIdx].readyState<4) {
		xmlHttpReq[controlIdx].abort();
	}
	searchKeyText = $(control_ids[controlIdx].SEARCHKEYTEXT).value;
	searchKeyText = searchKeyText.replace(/,/g, '').replace(/[\s]+/g, ' ').replace(/^\s/, '');
	searchUrl = parseSearchUrl(searchKeyText, LIMIT_RESULTS[controlIdx], controlIdx, isMoreSearch);
	xmlHttpReq[controlIdx]=getHTTPObject();
	xmlHttpReq[controlIdx].onreadystatechange = function() {
		processReqChange(controlIdx);
	};
	xmlHttpReq[controlIdx].open("GET", searchUrl);
	resetSearchKeyValue(controlIdx);
	lastSearchKeyText[controlIdx] = $(control_ids[controlIdx].SEARCHKEYTEXT).value;
	xmlHttpReq[controlIdx].send(null);
}
function getHTTPObject() {
	if (window.XMLHttpRequest) {
		try {
			xmlhttp=new XMLHttpRequest();
		} catch(ex) {
			xmlhttp=false;
		}
	} else if (window.ActiveXObject) {
		try {
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch(ex) {
			try {
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch(Ex) {
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}
function processReqChange(controlIdx) {
	if (xmlHttpReq[controlIdx].readyState==4) {
		var holderEl = $(control_ids[controlIdx].HOLDER);
		var resultsEl = holderEl.firstChild;
		var moreEl = resultsEl.nextSibling;
		resultsEl.innerHTML=xmlHttpReq[controlIdx].responseText;
		xmlHttpReq[controlIdx]=null;
		if (resultsEl.firstChild!=null) {
			if (resultsEl.firstChild.childNodes.length>DISPLAY_ROW_COUNT) {
				resultsEl.style.height=DISPLAY_HEIGHT+"px";
			} else {
				resultsEl.style.height="auto";
			}
			holderEl.style.display="block";
			showDropDown(controlIdx);
			if (resultsEl.firstChild.tagName=='DIV') {
				var window_width=resultsEl.firstChild.getAttribute('textWidth')*CHAR_WIDTH;
				var doc_width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
				var max_width = doc_width - Position.cumulativeOffset(resultsEl)[0] - ESTIMATED_SCROLLBAR_WIDTH;
				window_width = Math.min(window_width, max_width);
				var totalItems=resultsEl.firstChild.childNodes.length;
				if (window_width>DEFAULT_WINDOW_WIDTH) {
					resultsEl.style.width=window_width+"px";
				} else {
					resultsEl.style.width=DEFAULT_WINDOW_WIDTH+"px";
				}
				if (totalItems==1) {
					var highlight=findSearchHighlight(controlIdx);
					if (highlight) {
						toggleHighlight(highlight,controlIdx,0);
					}
					highlight=resultsEl.firstChild.firstChild;
					toggleHighlight(highlight,controlIdx,1);
				}
				if (resultsEl.firstChild.getAttribute('hasMoreRows')=='True') {
					moreEl.style.display='block';
					moreEl.style.top = (Position.positionedOffset(resultsEl)[1] + resultsEl.offsetHeight) + "px";
					if (isIE) {
						moreEl.style.width=(resultsEl.offsetWidth-2)+"px";
					} else {
						moreEl.style.width=(resultsEl.clientWidth-2)+"px";
					}
				} else {
					moreEl.style.display='none';
				}
			} else {
				resultsEl.style.width=DEFAULT_WINDOW_WIDTH+"px";
				moreEl.style.display='none';
			}
		}
	}
}
function submitSearch(controlIdx) {
	var highlight=findSearchHighlight(controlIdx);
	if (highlight&&highlight.firstChild) {
	    if(document.all){
		    document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).value=highlight.innerText;
		} else {
		    document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).value=highlight.textContent;
		}
		document.getElementById(control_ids[controlIdx].SEARCHKEYVALUE).value=highlight.getAttribute('searchKeyValue');
		lastSearchKeyText[controlIdx]=document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).value;
		searchBoxFocus(controlIdx,false);
		hideDropDown(controlIdx);
	}
	return false;
}
function captureMouseClick(controlIdx,el) {
	var highlight=findSearchHighlight(controlIdx);
	if (highlight) {
		toggleHighlight(highlight,controlIdx,0);
	}
	toggleHighlight(el,controlIdx,1);
	setTimeout("submitSearch("+controlIdx+")",200);
}
function resetSearchKeyValue(controlIdx) {
	if (document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).value!=lastSearchKeyText[controlIdx]) {
		document.getElementById(control_ids[controlIdx].SEARCHKEYVALUE).value='';
		LIMIT_RESULTS[controlIdx]=DISPLAY_ROW_COUNT;
	}
}
function resetSearchComboBox(controlIdx) {
	document.getElementById(control_ids[controlIdx].SEARCHKEYVALUE).value='';
	document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).value='';
	lastSearchKeyText[controlIdx]='';
	closeResults(controlIdx);
}
function closeResults(controlIdx) {
	document.getElementById(control_ids[controlIdx].HOLDER).style.display="none";
	highlight=findSearchHighlight(controlIdx);
	if (highlight) {
		toggleHighlight(highlight,controlIdx,0);
	}
	toggleOverlappingControls(controlIdx,1);
}
function exitControl(controlIdx) {
	if (AllowComboBoxHide[controlIdx]) {
		hideDropDown(controlIdx);
	}
}
function showDropDown(controlIdx) {
	toggleControl(control_ids[controlIdx].HOLDER,1);
	toggleControl(control_ids[controlIdx].SEARCHRESULT,1);
	document.getElementById(control_ids[controlIdx].SEARCHRESULT).style.overflow='auto';
	toggleControl(control_ids[controlIdx].SEARCHFORMORE,1);
	highlight=findSearchHighlight(controlIdx);
	if (highlight) {
		toggleHighlight(highlight,controlIdx,0);
		toggleHighlight(highlight,controlIdx,1);
	}
	toggleGraphic(controlIdx,1);
	toggleOverlappingControls(controlIdx,0);
}
function hideDropDown(controlIdx) {
	toggleControl(control_ids[controlIdx].SEARCHFORMORE,0);
	document.getElementById(control_ids[controlIdx].SEARCHRESULT).style.overflow='hidden';
	toggleControl(control_ids[controlIdx].SEARCHRESULT,0);
	toggleControl(control_ids[controlIdx].HOLDER,0);
	toggleGraphic(controlIdx,0);
	toggleOverlappingControls(controlIdx,1);
}
function isDropDownHidden(controlIdx) {
	if (document.getElementById(control_ids[controlIdx].HOLDER).style.visibility=='hidden') {
		return true;
	} else {
		return false;
	}
}
function toggleControl(divId,divState) {
	var obj=document.getElementById(divId);
	obj.style.visibility=divState?"visible":"hidden";
}
function toggleGraphic(controlIdx,state) {
	if (state) {
		document.getElementById(control_ids[controlIdx].TOGGLEGRAPHIC).src=eval("toggleHide.src");
		document.getElementById(control_ids[controlIdx].TOGGLEDROPDOWN).href="JavaScript:toggleDropDown("+controlIdx+",0);";
	} else {
		document.getElementById(control_ids[controlIdx].TOGGLEGRAPHIC).src=eval("toggleShow.src");
		document.getElementById(control_ids[controlIdx].TOGGLEDROPDOWN).href="JavaScript:toggleDropDown("+controlIdx+",1);";
	}
}
function toggleDropDown(controlIdx,state) {
	if (state) {
		if (document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).value==''||document.getElementById(control_ids[controlIdx].HOLDER).firstChild.firstChild==null) {
			performSearch(controlIdx,false);
		} else {
			showDropDown(controlIdx);
		}
		searchBoxFocus(controlIdx,true);
	} else {
		hideDropDown(controlIdx);
		searchBoxFocus(controlIdx,false);
	}
}
function showMoreResults(controlIdx) {
	LIMIT_RESULTS[controlIdx]=LIMIT_RESULTS[controlIdx]*2;
	startSearch(controlIdx,true);
	searchBoxFocus(controlIdx,false);
}
function toggleOverlappingControls(controlIdx,state) {
	if (control_params[controlIdx].OVERLAPPINGCONTROLS!='') {
		var overlappingControls=control_params[controlIdx].OVERLAPPINGCONTROLS.split(',');
		var i;
		for(i=0;i<overlappingControls.length;i++) {
			toggleControl(overlappingControls[i],state);
		}
	}
}
function resizeToggleSearchButton(controlIdx) {
	document.getElementById(control_ids[controlIdx].TOGGLEGRAPHIC).height=document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT).offsetHeight;
}
function searchBoxFocus(controlIdx,setToEndOfLine) {
	var searchKeyText=document.getElementById(control_ids[controlIdx].SEARCHKEYTEXT);
	searchKeyText.focus();
	if (setToEndOfLine) {
		setCaretToEnd(searchKeyText);
	} else {
		setCaretToStart(searchKeyText);
	}
}
function setCaretToEnd(control) {
	if (control.createTextRange) {
		var range=control.createTextRange();
		range.collapse(false);
		range.select();
	} else if (control.setSelectionRange) {
		control.focus();
		var length=control.value.length;
		control.setSelectionRange(length,length);
	}
}
function setCaretToStart(control) {
	if (control.createTextRange) {
		var range=control.createTextRange();
		range.collapse(true);
		range.select();
	} else if (control.setSelectionRange) {
		control.focus();
		control.setSelectionRange(0,0);
	}
}
function setSearchUrl(controlIdx,newSearchUrl) {
	control_params[controlIdx].SEARCHURL=newSearchUrl;
}
