var oSearchDiv = null;
var arrOptionTexts = new Array();
var arrOptionValues = new Array();
var sSearchText = '';
var oTmpTimer = null;
var g_bSubstringSearch;
var g_oSelect;
var sSelectClassName;

//SELECT: clicked
function jsSearch_onFocus(oSelect) {
	//Clear global search string
	sSearchText = '';
	
	//Save width, so doesn't resize when no elements showing
	oSelect.style.width = oSelect.offsetWidth + 'px';

	//create search DIV
	if (oSearchDiv == null) {
		oSearchDiv = document.createElement('DIV');
		document.body.appendChild(oSearchDiv);
		oSearchDiv.className = 'jsSearchText';
		oSearchDiv.unselectable = 'on';
		oSearchDiv.style.left = GetABSOffsetLeft(oSelect);
		oSearchDiv.style.top = GetABSOffsetTop(oSelect) - oSearchDiv.offsetHeight;
	}
	oSearchDiv.innerHTML = 'Search:';

	oSelect.classNameOld = oSelect.className;
}

function GetABSOffsetTop(o) {
	var i = o.offsetTop;
	var p = o.offsetParent;
	while(p!=null) {
		i += p.offsetTop;
		p = p.offsetParent;
	}
	return i;
}
function GetABSOffsetLeft(o) {
	var i = o.offsetLeft;
	var p = o.offsetParent;
	while(p!=null) {
		i += p.offsetLeft;
		p = p.offsetParent;
	}
	return i;
}

/*
SELECT: letter typed
- add / delete keystroke from search string
- clear Select
- search select
*/

//Exit if DEL or BS pressed
function jsSearch_onKeyDown() {
	if (event.keyCode == 8) {
		event.keyCode = 0;
	} else if (event.keyCode == 13) {
		event.keyCode = 0;
		//event.keyCode = 9; //Enter pressed - tab to next control
	}
}

//Automatically filters out control type keystrokes
function jsSearch_onKeyPress(oSelect, bSubstringSearch) {
	//exit if ESC, DEL or BS pressed
	if ((event.keyCode == 27) || (event.keyCode == 8) || (event.keyCode == 13)) {
		return;
	}

	//add alphanumeric character to global search string for DIV
	var s = String.fromCharCode(event.keyCode); 
	sSearchText += s;

	//do the SELECT filter
	g_bSubstringSearch = bSubstringSearch;
	g_oSelect = oSelect;
	jsSearch_DoSearch();
}

function jsSearch_onKeyUp() {
	//backspace or delete if needs to
	if ((event.keyCode == 8) && (arrOptionTexts.length > 0)) {
		//Ctrl + backspace: delete whole search string
		if (event.ctrlKey) {
			sSearchText = '';
		//backspace or delete: just delete last character
		} else {
			sSearchText = sSearchText.substring(0, sSearchText.length - 1);
		}
		jsSearch_DoSearch();
	}
}

function jsSearch_DoSearch2() {
	var i = 0;
	var j = 0;
	var sValue = sSearchText.toLowerCase();
	var iValueLen = sValue.length;
	var oSelect = g_oSelect;
	
	clearInterval(oTmpTimer);
	oTmpTimer = null;
	
	//if Arrays empty, load from OPTION's
	if (arrOptionTexts.length == 0) {
		jsSearch_LoadArrays(oSelect);
	}

	//clear all OPTION's from SELECT
	oSelect.innerHTML = ''; //slower to use "while (oSelect.options.length > 0) oSelect.options.remove(0);"

//put matching OPTION's back into SELECT
	//match anywhere in string
	if (g_bSubstringSearch) {
		for (i = 0; i < arrOptionValues.length; i++) {
			if (arrOptionTexts[i].toLowerCase().indexOf(sValue) >= 0) {
				var oOption = document.createElement("OPTION");
				oOption.text = arrOptionTexts[i];
				oOption.value = arrOptionValues[i];
				oSelect.add(oOption);
			}
		}

	//matching start only
	} else {
		for (i = 0; i < arrOptionValues.length; i++) {
			if (arrOptionTexts[i].toLowerCase().substring(0, iValueLen) == sValue) {
				var oOption = document.createElement("OPTION");
				oOption.text = arrOptionTexts[i];
				oOption.value = arrOptionValues[i];
				oSelect.add(oOption);
			}
		}
	}

	//show search string DIV
	oSearchDiv.style.display = '';
	oSearchDiv.innerHTML = 'Search: <B>' + sSearchText + '</B><img src="/arhg/js/caret.gif" align=absbottom>';

	//update SELECT class name
	oSelect.className = 'jsSearchFocus';
}

function jsSearch_DoSearch() {
	//"Please wait..."
	if(oSearchDiv==null) return;
	
	if (g_oSelect.options.length > 100) {
		//asdf
		oSearchDiv.innerHTML = 'Searching, please wait...';
	}
	
	if (oTmpTimer == null) {
		setInterval('jsSearch_DoSearch2()',0);
	}
}


//load OPTION's from SELECT into 2 arrays
function jsSearch_LoadArrays(oSelect) {
	//load array
	for (var i = 0; i < oSelect.options.length; i++) {
		arrOptionTexts[i] = oSelect.options[i].text;
		arrOptionValues[i] = oSelect.options[i].value;
	}
}


/*
SELECT: lost focus
- repopulate whole select
- select same option
*/
function jsSearch_onBlur(oSelect) {
	//hide search DIV
	if(oSearchDiv==null) return;
	oSearchDiv.style.display = 'none';

	//if SELECT was typed in to, repopulate
	//if (oSelect.className != sSelectClassName) {
	//if (oSelect.className != 'jsSearchNoFocus') {
		//update SELECT class
		//oSelect.className = sSelectClassName;
		//oSelect.className = 'jsSearchNoFocus';
	
	oSelect.className = oSelect.classNameOld;

	if (arrOptionValues.length > 0) {
		var sValue = oSelect.value;
		oSelect.innerHTML = '';

		//repopulate whole SELECT from 2 arrays
		for (i = 0; i < arrOptionValues.length; i++) {
			var oOption = document.createElement("OPTION");
			oOption.text = arrOptionTexts[i];
			oOption.value = arrOptionValues[i];
			oSelect.add(oOption);

			//select same OPTION
			if (arrOptionValues[i] == sValue) {
				oSelect.options[i].selected = true;
			}
		}
		arrOptionTexts.length = 0;
		arrOptionValues.length = 0;
		var sSearchText = '';
	}
	//oSearchDiv = null;
}