var DataSearch_InputBox;
var DataSearch_ValueBox;
var DataSearch_Param1Box;
var DataSearch_DescLabel;
var DataSearch_SearchType;
var DataSearch_MinLen;
var DataSearch_ListTable;
//var DataSearch_ListBacking;
var DataSearch_SearchBusy = false;
var DataSearch_SearchPending = false;
var DataSearch_IncludeIntl = false;
var DataSearch_FrameCount = 1;

var bSetPUPoint = false;
var bSetDOPoint = false;

function DataSearch_InputOnFocus(sInputID, sValueID, sDescID, sSearchType, iMinLen, bIncludeIntl, sParam1ID) {
	DataSearch_Setup(sInputID, sValueID, sDescID, sSearchType, iMinLen, bIncludeIntl, sParam1ID);
	if (iMinLen == 0)
		DataSearch_Init();
}

function DataSearch_Setup(sInputID, sValueID, sDescID, sSearchType, iMinLen, bIncludeIntl, sParam1ID) {
	DataSearch_ListTable = document.getElementById('DataSearch_tblList');
	//DataSearch_ListBacking = document.getElementById('DataSearch_ifrList');
	DataSearch_InputBox = document.getElementById(sInputID);
	DataSearch_ValueBox = document.getElementById(sValueID);
	DataSearch_SearchType = sSearchType;
	DataSearch_MinLen = iMinLen;
	
	bSetPUPoint = false;
	bSetDOPoint = false;
	switch (sValueID) {
		case 'txtPULandmarkValue':
			bSetPUPoint = true;
			break;
		case 'txtDOLandmarkValue':
			bSetDOPoint = true;
			break;
	}
	
	if (sDescID == '') {
		DataSearch_DescLabel = null;
	}
	else {
		if (sDescID == '') {
			DataSearch_DescLabel = null;
		}
		else {
			DataSearch_DescLabel = document.getElementById(sDescID);
		}
	}
	if (bIncludeIntl == true) {
		DataSearch_IncludeIntl = true;
	}
	else {
		DataSearch_IncludeIntl = false;
	}
	if (sDescID == '') {
		DataSearch_DescLabel = null;
	}
	else {
		if (sParam1ID == '') {
			DataSearch_Param1Box = null;
		}
		else {
			DataSearch_Param1Box = document.getElementById(sParam1ID);
		}
	}
}

function DataSearch_InputKeyDown(e) {
	var iMax = 0;
	var iIndex = 0;
	var iKeyCode = 0;
	var oTBody;
	
	if (e && e.which) {   
		iKeyCode = e.which; 
	}
	else {  
		iKeyCode = e.keyCode;  
	}  

	switch (iKeyCode) {
		case 9:
			//Tab
			DataSearch_SelectValue();
			if (e && e.which) {   
				e.which = 0; 
			}
			else {  			
			//	e.keyCode = 0;  
			}  
			break;
		case 13:
			//Enter
			if (DataSearch_ListTable.style.display == 'none') {
				DataSearch_Init();
			}
			else {
				DataSearch_SelectValue();
			}
			if (e && e.which) {   
				e.which = 0; 
			}
			else {  
				e.keyCode = 0;  
			}  
			break;
		case 27:
			//Esc
			DataSearch_CloseList();
			break;
		case 38:
			//Up Arrow
			if (DataSearch_ListTable.style.display != 'none') {
				oTBody = DataSearch_ListTable.firstChild;
				iIndex = parseInt(oTBody.firstChild.firstChild.firstChild.nodeValue);
				if (iIndex > 1) {
					iIndex--;
					DataSearch_HighlightRow(oTBody, iIndex);
					if (e && e.which) {   
						e.which = 0; 
					}
					else {  
						e.keyCode = 0;  
					}  
				}
			}
			break;
		case 40:
			//Down Arrow
			if (DataSearch_ListTable.style.display != 'none') {
				oTBody = DataSearch_ListTable.firstChild;
				iIndex = parseInt(oTBody.firstChild.firstChild.firstChild.nodeValue);
				iMax = oTBody.childNodes.length - 1;
				if (iIndex < iMax) {
					iIndex++;
					DataSearch_HighlightRow(oTBody, iIndex);
					if (e && e.which) {   
						e.which = 0; 
					}
					else {  
						e.keyCode = 0;  
					}  
				}
			}
			break;			
	}
	return false;
}

function DataSearch_InputKeyUp(e) {
	var iKeyCode = 0;
	
	if (e && e.which) {   
		iKeyCode = e.which; 
	}
	else {  
		iKeyCode = e.keyCode;  
	}  

	switch (iKeyCode) {
		case 9:
			if (e && e.which) {   
				e.which = 0; 
			}
			else {  
				e.keyCode = 0;  
			}  
		case 13:
			if (e && e.which) {   
				e.which = 0; 
			}
			else {  
				e.keyCode = 0;  
			}  
		case 27:
		case 37:
		case 38:
		case 39:
		case 40:
			break;
		default:
			DataSearch_Init();
	}
	return false;
}

function DataSearch_InputOnBlur() {
	DataSearch_SelectValue();
}

function DataSearch_MouseOver(e) {
	var oElement;
	var oCell;
	var oTBody;
	var oRow;
	var iCurIndex;
	
	if (e && e.target) {   
		oElement = e.target;  
	}
	else {  
		oElement = e.srcElement;  
	}  
	
	switch (oElement.tagName) {
		case 'TD':
			oCell = oElement;
			oRow = oCell.parentNode;
			break;
		case 'TR':
			oRow = oElement;
			break;
		default:
			oRow = null;
	}
	
	if (oRow != null) {
		oTBody = oRow.parentNode;
		iCurIndex = oRow.rowIndex;

		DataSearch_HighlightRow(oTBody, iCurIndex);
	}
}

function DataSearch_MouseDown(e) {
	DataSearch_SelectValue();
}

function DataSearch_Init() {
	var sSearchText = '' + DataSearch_InputBox.value;
	var sSearchParam1 = ''
	var oForm = document.getElementById('DataSearch_frmSubmit');
	var iLeft = GetAbsoluteLeft(DataSearch_InputBox);
	var iTop = GetAbsoluteTop(DataSearch_InputBox);

	DataSearch_ValueBox.value = '';

	if (sSearchText.length >= DataSearch_MinLen) {
		if (DataSearch_DescLabel != null) {
			DataSearch_DescLabel.innerHTML = 'Searching...';
		}
		
		if (DataSearch_Param1Box != null) {
			sSearchParam1 = DataSearch_Param1Box.value;
		}

		DataSearch_ListTable.style.left = iLeft;
		DataSearch_ListTable.style.top = iTop + DataSearch_InputBox.offsetHeight;
		
		//DataSearch_ListBacking.style.left = iLeft;
		//DataSearch_ListBacking.style.top = iTop + DataSearch_InputBox.offsetHeight;
	
		oForm.SearchType.value = DataSearch_SearchType;
		oForm.SearchText.value = sSearchText;
		oForm.SearchParam1.value = sSearchParam1;
		oForm.SearchLimit.value = '10';
		if (DataSearch_IncludeIntl == true) {
			oForm.IncludeIntl.value = 'Y';
		}
		else {
			oForm.IncludeIntl.value = 'N';
		}
		DataSearch_FrameCount++;
		if (DataSearch_FrameCount > 5) {
			DataSearch_FrameCount = 1;
		}
		oForm.target = 'DataSearch_ifrSubmit' + DataSearch_FrameCount;
		if (DataSearch_SearchBusy == true) {
			DataSearch_SearchPending = true;
		}
		else {
			DataSearch_SearchBusy = true;
			oForm.submit();
		}
	}
	else {
		if (DataSearch_DescLabel != null) {
			DataSearch_DescLabel.innerHTML = '';
		}

		DataSearch_ReturnResult('');
	}
}

function DataSearch_ReturnResult(sSearchResult) {
	DataSearch_SearchBusy = false;
	if (DataSearch_SearchPending == true) {
		DataSearch_SearchPending = false;
		DataSearch_Init();
	}
	else {
		DataSearch_ProcessResult(sSearchResult);
	}
}
	
function DataSearch_ProcessResult(sSearchResult) {
	var oTBody;
	var oRow;
	var oCell;
	var sValue = '';
	var sText = '';
	var i;
	var aItems;
	var sItem;
	var aItemAtribs;
	var iSize = 0;

	if (DataSearch_DescLabel != null) {
		DataSearch_DescLabel.innerHTML = '';
	}

	if (DataSearch_ListTable.childNodes.length > 0) {
		oTBody = DataSearch_ListTable.firstChild;
		while (oTBody.childNodes.length > 0) {
			oTBody.removeChild(oTBody.childNodes[0]);
		}
		while (DataSearch_ListTable.childNodes.length > 0) {
			DataSearch_ListTable.removeChild(DataSearch_ListTable.childNodes[0]);
		}
	}

	if (sSearchResult != '') {
		aItems = sSearchResult.split('^');
		if (aItems.length > 1) {
			oTBody = document.createElement("TBODY");
			DataSearch_ListTable.appendChild(oTBody);						
			
			oRow = document.createElement("TR");
			oRow.style.display = 'none';
			
			oTBody.appendChild(oRow);
			
			oCell = document.createElement("TD");
			oCell.appendChild(document.createTextNode('1'));
			oCell.colSpan = 2;
			oRow.appendChild(oCell);

			for (i=1; i<aItems.length; i++) {
				sItem = aItems[i];
				aItemAtribs = sItem.split('~');
				sValue = aItemAtribs[0];
				sText = aItemAtribs[1];
				
				oRow = document.createElement("TR");
				if (i == 1) {
					oRow.className = 'DataSearch_SelectedRow';
				}
				else {
					oRow.className = 'DataSearch_NormalRow';
				}
				
				oTBody.appendChild(oRow);
				
				oCell = document.createElement("TD");
				oCell.innerHTML = sValue;
				//oCell.appendChild(document.createTextNode(sValue));
				oCell.style.display = 'none';
				oRow.appendChild(oCell);
				
				oCell = document.createElement("TD");
				oCell.innerHTML = sText;
				//oCell.appendChild(document.createTextNode(sText));
				oCell.noWrap = true;
				oRow.appendChild(oCell);
				
				iSize++;
			}
		}
	}	
	else{
		DataSearch_CloseList();
  }	
	
	if (iSize < 1) {
		DataSearch_ListTable.style.display = 'none';	
		//DataSearch_ListBacking.style.display = 'none';
	}
	else {
		DataSearch_ListTable.style.display = 'block';
		HideOverlappedSelects(DataSearch_ListTable);
		//DataSearch_ListBacking.style.display = 'block';

		//DataSearch_ListBacking.style.width = DataSearch_ListTable.offsetWidth;
		//DataSearch_ListBacking.style.height = DataSearch_ListTable.offsetHeight;
	}
}

function DataSearch_HighlightRow(oTBody, iCurIndex) {
	var oCurRow;
	var oDataRow;
	var iLastIndex;
	var oLastRow;

	if (iCurIndex > 0) {
		oDataRow = oTBody.firstChild;
							
		iLastIndex = parseInt(oDataRow.firstChild.firstChild.nodeValue);
		if (iLastIndex > 0) {
			oLastRow = oTBody.childNodes[iLastIndex];
			oLastRow.className = 'DataSearch_NormalRow';
		}

		oCurRow = oTBody.childNodes[iCurIndex];
		oCurRow.className = 'DataSearch_SelectedRow';

		oDataRow.firstChild.firstChild.nodeValue = '' + iCurIndex;
	}
}

function DataSearch_SelectValue() {
	var oTBody;
	var iIndex = 0;
	var sValue = '';
	var sText = '';
	var sDesc = '';
	var aVal;
	var sAddr;
	var aValue;
	var sLandmarkID;

	if (DataSearch_ListTable.style.display != 'none') {
		if (DataSearch_ListTable.childNodes.length > 0) {
			oTBody = DataSearch_ListTable.firstChild;
			if (oTBody.childNodes.length > 0) {
				iIndex = parseInt(oTBody.firstChild.firstChild.firstChild.nodeValue);
			}
		}
		if (iIndex > 0) {
			sValue = DataSearch_ListTable.firstChild.childNodes[iIndex].firstChild.firstChild.nodeValue;
			sText = DataSearch_ListTable.firstChild.childNodes[iIndex].lastChild.firstChild.nodeValue;			
			sDesc = GetLocationDesc(sValue);
			DataSearch_InputBox.value = sText;
			DataSearch_ValueBox.value = sValue;
/*
			if (bSetPUPoint == true) {
				if (sValue == '') {
					sLandmarkID = '';
				}
				else {
					aValue = sValue.split('|');
					sLandmarkID = aValue[0];
				}
				//GetPUPoints(sLandmarkID, '');
			}
			if (bSetDOPoint == true) {
				if (sValue == '') {
					sLandmarkID = '';
				}
				else {
					aValue = sValue.split('|');
					sLandmarkID = aValue[0];
				}
				//GetDOPoints(sLandmarkID, '');
			}
*/			
		}
		DataSearch_CloseList();
		if (DataSearch_DescLabel != null) {
			DataSearch_DescLabel.innerHTML = sDesc;
		}
	}
}

function DataSearch_CloseList() {
	DataSearch_ListTable.style.display = 'none';
	ShowHiddenSelects();
	//DataSearch_ListBacking.style.display = 'none';
}
