// ======================================================================================
// Shows a popUp window with the event search result
//    ListBoxId from where we get the selected items
//    CallerId textBoxId where we insert the result
// ======================================================================================
function OnAssumeEventSearchResult(strListBoxId, strCallerId)
{
	// Get values from Lsitbox
	oListBox = document.getElementById(strListBoxId);
	if(oListBox != null)
	{
		if(oListBox.selectedIndex >= 0)
		{
			//delegate	
			if(window.opener.AssumeEventSearchResult(strCallerId,
				oListBox.options[oListBox.selectedIndex].text,
				oListBox.options[oListBox.selectedIndex].value))
			{
				window.close();
			}
		}
	}
	else if(strListBoxId = "Reset")
	{
		if(window.opener.ResetEventSearchResult(strCallerId))
		{
			window.close();
		}
	}
	else
	{
		alert("Debug:OnAssumeEventSearchResult no listbox");
	}
}

// ======================================================================================
// Shows a popUp window with the event search result
// ======================================================================================
function AssumeEventSearchResult(strCallerId, strText, strValue)
{
	// Set value
	oTextBox = document.getElementById(strCallerId);
	oTextBoxValue = document.getElementById(strCallerId+"HiddenValue");
	if(oTextBox != null)
	{
		oTextBox.value = strText;
		
		if(oTextBoxValue != null)
		{
			oTextBoxValue.value = strValue;
		}
		
		return true;
	}
	else
	{
		alert("Debug:AssumeEventSearchResult Caller not found");
	}
	
	return false;	
}

// ======================================================================================
// Resets the event search result
// ======================================================================================
function ResetEventSearchResult(strCallerId)
{
	// Set value
	oTextBox = document.getElementById(strCallerId);
	oTextBoxValue = document.getElementById(strCallerId + "HiddenValue");
	if(oTextBox != null)
	{
		oTextBox.value = "";
		if(oTextBoxValue != null)
		{
			oTextBoxValue.value = "";
		}
		return true;
	}
	else
	{
		alert("Debug:ResetEventSearchResult Caller not found");
	}
	
	return false;	
}

// ======================================================================================
// Gets the url of the search result
// ======================================================================================
function GetEventSearchResultUrl(strTextFieldId, eLogtype, strFilterBox)
{
	// the sourcelink is needed to extract the prefix of the FieldID
	// then we build the ID for Textfield
	// and get the searchtext
	strFilterText = null;
	strSelectedId = null;
	oTextFieldHiddenValue = null;
	if(strFilterBox != null)
	{
		// Search Box with filtertext
		for(nIdx = 0; nIdx < document.forms[0].elements.length; nIdx++)
		{
			nWithColon = document.forms[0].elements[nIdx].id.lastIndexOf(strFilterBox);
			if(nWithColon >= 0)
			{
				oFilterBox = document.forms[0].elements[nIdx];
				strFilterText = oFilterBox.value;
				break;
			}
		}
	}
	
	// Search for Editfield with partial name <strTextFieldName>
	bFound = false;
	// Complete strTextFieldId
	for(nIdx = 0; nIdx < document.forms[0].elements.length; nIdx++)
	{
		nWithColon = document.forms[0].elements[nIdx].id.lastIndexOf(strTextFieldId);
		if(nWithColon > 0)
		{
			oTextFieldName = document.forms[0].elements[nIdx];
			strTextFieldId = oTextFieldName.id;
			bFound = true;
			break;
		}
	}
	
	var strUrl = "";
	if(bFound)
	{
		oTextFieldHiddenValue = document.getElementById(oTextFieldName.id + "HiddenValue");
		// Get Searchtext from Field
		strSearchText = oTextFieldName.value;
		if(oTextFieldHiddenValue != null)
		{
			strSelectedId = oTextFieldHiddenValue.value;
		}
	
		strUrl = "../Editor/EventSearchResultPage.aspx?" +
			"strCallerId=" + strTextFieldId +
			"&nLogType=" + eLogtype +
			"&strSearchText=" + strSearchText;
		
		if(strFilterText != null && strFilterText != "")
		{
			strUrl += "&strFilterText=" + strFilterText;	
		}
		
		if(strSelectedId != null && strSelectedId != "")
		{
			strUrl += "&strSelectedId=" + strSelectedId;	
		}
	}
	else
	{
		strUrl = "";
		alert('Debug:OpenEventSearchResult Field not found');
	}
	
	return strUrl;
}

// ======================================================================================
// Returns the windoOption for the popUp window
// ======================================================================================
function GetWindowOptions()
{
	return "width=300,height=390," +
		"resizable=false,scrollbars=auto,status=no,statusbar=no" +
		"menubar=no,toolbar=no,location=no,directories=no";
}

// ======================================================================================
// Clear all data from the given list and reset the selectedIndex
// ======================================================================================
function Clear(oList) 
{
	if(oList.options)
	{
		oList.disabled = false;
		for(var i = (oList.options.length - 1); i >= 0; i--)
		{
			oList.options[i]= null;
		}
		oList.selectedIndex = -1;
	}
}
