
//////////////////////////////////////////////////////////
//Returns query results when you type text in the query textbox
function UpdateContent(inputText, targetListBoxId, action)
{
	var keyCode = event.keyCode;

	if(keyCode == 38)  
		ScrollUpListBox(targetListBoxId);  //When up or down arrow is pressed
	else if(keyCode == 40)
		ScrollDownListBox(targetListBoxId); //When up or down arrow is pressed
	else
	{
		var targetListBox = document.getElementById(targetListBoxId);	
		if (inputText.length < 3)
		{
			targetListBox.innerText = null;
			return;
		}
		
		inputedText = inputText;
		
		var xmlHttp = GetXmlHttpObject();
		var url = AjaxRepositoryUrl;
	
		url += "?action=" + action;
		url += "&paramter=" + inputText;
		xmlHttp.onreadystatechange = function() { ProcessResponse(xmlHttp, inputText, targetListBox); };
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
//Ajax Callback
function ProcessResponse(xmlHttp, inputedText, targetListBox)
{
	if (xmlHttp.readyState ==4 || xmlHttp.readyState == "complete")
	{ 
		PopulateListBox(xmlHttp.responseText, inputedText, targetListBox);
	} 
}

function PopulateListBox(rawText, inputedText, targetListBox)
{
	var rows = rawText.split('$');
	
	if (rows.length < 1)
		return;

	targetListBox.innerText = null;

	if (rows.length == 1)
	{
		var option = document.createElement('option');
		option.value = '';
		option.innerText = 'No Entities Found for: ' + inputedText;

		targetListBox.appendChild(option);
	}

	for (var i = 0; i < rows.length; i++)
	{
		var row = rows[i];
		var index = row.indexOf('|');
		if (index > 0)
		{
			var option = document.createElement('option');
			option.value = row.substring(0, index) + ':' + row.substring(index + 1, row.length);
			option.innerText = row.substring(index + 1, row.length);

			targetListBox.appendChild(option);
		}
	}
	
	targetListBox.selectedIndex = 0;
}

//////////////////////////////////////////////////////

function UpdateContentEntity(inputTextBox, targetListBoxId, isAbstractCheckBoxId, action)
{
	var inputText = inputTextBox.value;
	
	SetWaittingMessage(targetListBoxId);
	
	SetInputEntityVisiblity(inputText, targetListBoxId);

	var targetListBox = document.getElementById(targetListBoxId);	
	
	if (inputText.length < 1)
	{
		targetListBox.innerText = null;
		return;
	}
	
	inputedText = inputText;
	
	var isAbstract = document.getElementById(isAbstractCheckBoxId).checked;
	
	var xmlHttp = GetXmlHttpObject();
	var url = AjaxRepositoryUrl;

	url += "?action=" + action;
	url += "&paramter=" + inputText.replace('&', '%26');
	url += "&isAbstract=" + (isAbstract ? 1 : 0);
	xmlHttp.onreadystatechange = function() { ProcessResponseEntity(xmlHttp, inputText, inputTextBox, targetListBox); };
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}
//Callback function
function ProcessResponseEntity(xmlHttp, inputedText, inputTextBox, targetListBox)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		if (!IsSessionTimeOut(xmlHttp.responseText))
		{
			if (inputedText != inputTextBox.value)
				return;
			
			PopulateListBoxEntity(xmlHttp.responseText, inputedText, targetListBox);
			ProcessDone();
		}
	} 
}

function PopulateListBoxEntity(rawText, inputedText, targetListBox)
{
	var rows = rawText.split('$');
	
	if (rows.length < 1)
		return;

	targetListBox.innerText = null;
	
	if (rows.length == 1)
	{
		var option = document.createElement('option');
		option.value = '';
		option.innerText = 'No Entities Found for: ' + inputedText;

		targetListBox.appendChild(option);
	}

	for (var i = 0; i < rows.length; i++)
	{
		var row = rows[i];
		var index = row.indexOf('|');
		if (index > 0)
		{
			var option = document.createElement('option');
			option.value = row.substring(0, index);
			option.innerText = row.substring(index + 1, row.length - 2);
			if (row.substring(row.length - 1, row.length) == '1')
				option.style.color = "#FF33FF";
				
			targetListBox.appendChild(option);
		}
	}
	
	targetListBox.selectedIndex = 0;
}

///////////////////////////////////////////////////


function ScrollUpListBox(listBoxId)
{
	var select = document.getElementById(listBoxId);
	var selectedIndex = select.selectedIndex;
	if(selectedIndex > 0)
		select[selectedIndex].previousSibling.selected = "selected";
}

function ScrollDownListBox(listBoxId)
{
	var select = document.getElementById(listBoxId);
	var selectedIndex = select.selectedIndex;
	
	if(selectedIndex >= 0 && select[selectedIndex].nextSibling != null)
		select[selectedIndex].nextSibling.selected = 'selected';
}

function InputTextBoxKeyDown(targetListBoxId)
{
	var keyCode = event.keyCode;

	var targetListBox = document.getElementById(targetListBoxId);	
	
	if(keyCode == 38)
		ScrollUpListBox(targetListBoxId);
	else if(keyCode == 40)
		ScrollDownListBox(targetListBoxId);
	else if(keyCode == 13)
	{
		if (targetListBox.value == null || targetListBox.value == "" || targetListBox.value == "0")
		{
			event.returnValue = false;
			return;
		}
		
		searchFeedback();
	}	
}

function SetWaittingMessage(listBoxId)
{
	var listBox = document.getElementById(listBoxId);
	
	listBox.innerText = null;
	var option = document.createElement('option');
	option.value = '';
	option.innerText = 'Retrieving Entities...';

	listBox.appendChild(option);
}



//////////////////////////////////////////////////////////

function UpdatePosition(elementId, positionX, positionY, action)
{
	window.status = "Update position ...";
	
	if (UpdateFingerprint != null)
		UpdateFingerprint();
	
	var xmlHttp = GetXmlHttpObject();
	var url = AjaxRepositoryUrl;
	
	url += "?action=" + action;
	url += "&paramter=" + elementId + '|' + positionX + '|' + positionY;
	xmlHttp.onreadystatechange = function() { ProcessUpdatePositionResponse(xmlHttp); };
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function ProcessUpdatePositionResponse(xmlHttp)
{
	if (xmlHttp.readyState ==4 || xmlHttp.readyState == "complete")
		if (!IsSessionTimeOut(xmlHttp.responseText))
			ProcessDone();
}

//////

function UpdateControlPanelStatus(elementId, status)
{
	window.status = "Update control panel status ...";
	
	var xmlHttp = GetXmlHttpObject();
	var url = AjaxRepositoryUrl;
	
	url += "?action=ControlPanelStatus";
	url += "&paramter=" + elementId + '|' + status;
	xmlHttp.onreadystatechange = function() { ProcessUpdateControlPanelStatusResponse(xmlHttp); };
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function ProcessUpdateControlPanelStatusResponse(xmlHttp)
{
	if (xmlHttp.readyState ==4 || xmlHttp.readyState == "complete")
		if (!IsSessionTimeOut(xmlHttp.responseText))
			ProcessDone();
}

//////////////////////////////////////////////////////

var HideSessionTimeOutAlert = false;

function IsSessionTimeOut(response)
{
	if(response.indexOf("<title>login</title>") > -1)
	{
		if (!HideSessionTimeOutAlert)
		{
			HideSessionTimeOutAlert = true;
			alert("Your session has expired.\nYou will now be logged out");
			window.navigate("http://www.intellectspace.com");
		}
		
		return true;
	}
	else
		return false;
}

function ProcessDone()
{
	window.status = "Done"; 
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

var AjaxRepositoryUrl = 'AjaxRepository.aspx';

