var termsArray;
//var currItem = -1;
var currItem = new Array();

function checkDefaultValue(fieldId, defaultValue)
{
	var field = document.getElementById(fieldId);
	
	if ( (field) && (field.value == defaultValue) )
	{
		field.value = '';
	}
}

function noenter(event, destinationNode) 
{
	if (event.keyCode == 13)
	{
		if ((currItem[destinationNode] != null) && (currItem[destinationNode] > -1))
		{
			return false;
		}
	}
	return true;
}

function selectWord(termIndex, fieldId, destinationNode) 
{
	var field = document.getElementById(fieldId);
	if (field)
	{
		var fieldValue = field.value;
		
		var valueSplitted = fieldValue.split(' ');
		fieldValue = '';
		
		for(var i=0;i<valueSplitted.length - 1;i++)
		{
			if (i > 0)
			{
				fieldValue += ' ';
			}
			
			fieldValue += valueSplitted[i];
		}

		if (fieldValue.length > 0)
		{
			fieldValue += ' ';
		}

		fieldValue += termsArray[termIndex];

		field.value = fieldValue;
		field.focus();
	}
		
	clearNode(destinationNode);
}

function showWords(event, field, urlWords, destinationNode)
{
	var items = null;
	var node = document.getElementById(destinationNode);
	if (node)
	{
		items = node.getElementsByTagName('li');
	}
	if (currItem[destinationNode] == null)
	{
		currItem[destinationNode] = -1;
	}
	
	var isCharacterKey = true;
	
	if (event.keyCode == 40)
	{
		// ARROW DOWN
		isCharacterKey = false;
		if (items)
		{
			currItem[destinationNode]++;
			if (currItem[destinationNode] >= items.length)
			{
				currItem[destinationNode] = 0;
			}
		}
	}
	else if (event.keyCode == 38)
	{
		// ARROW UP
		isCharacterKey = false;
		if (items)
		{
			currItem[destinationNode]--;
			if (currItem[destinationNode] < 0)
			{
				currItem[destinationNode] = items.length - 1;
			}
		}		
	}
	else if (event.keyCode == 13) 
	{
		// RETURN
		isCharacterKey = false;
		if (currItem[destinationNode] > -1)
		{
			if ( (termsArray) && (termsArray[currItem[destinationNode]]) )
			{
				selectWord(currItem[destinationNode], field.id, destinationNode);
			}			
		}
		
		currItem[destinationNode] = -1;
	}
	else if (event.keyCode == 27) 
	{
		// ESCAPE
		isCharacterKey = false;
		items = null;
		currItem[destinationNode] = -1;
		clearNode(destinationNode);
	}

	
	if (isCharacterKey)
	{
		urlWords += '?texto=' + encodeURIComponent(field.value);
		var ajaxCall = new AjaxCall('GET', true);
		doAjaxCallShowWords(ajaxCall, urlWords, destinationNode, field);
	}
	else
	{
		if (items)
		{			
			for (var a=0;a<items.length;a++) 
			{
				if (items[a].className == 'sel')
				{
					items[a].className = '';
				}
			}
			if (currItem[destinationNode] > -1)
			{
				items[currItem[destinationNode]].className = 'sel';		
			}		
		}
	}
}

function doAjaxCallShowWords(ajaxCall, serverUrl, destinationNode, field)
{	 
	var completeCall = new CompleteCall(writeWords, destinationNode, field);
	ajaxCall.execute(serverUrl, completeCall, true);
} 

function writeWords(response, destinationNode, field)
{
	var html = '';
	
	var terms = response;
	
	if (terms)
	{
		var termTags = terms.getElementsByTagName('term');
		if ( (termTags) && (termTags.length > 0) )
		{
			termsArray = new Array();
			
			html += '<ul>';
			
			for(var i=0;i<termTags.length;i++)
			{
				var term = termTags[i];

				termsArray[i] = term.firstChild.nodeValue;
				
				if (term)
				{
					html += '<li><a href="javascript:selectWord(' + i + ', \'' + field.id + '\', \'' + destinationNode + '\');">' + termsArray[i] + '</a></li>';
				}
			}
			
			html += '</ul>';
		}
	}

	if (html != '')
	{
		var node = document.getElementById(destinationNode);
		if (node)
		{
			node.innerHTML = html;
			node.style.display = 'block';
		}
	}
	else
	{
		clearNode(destinationNode);
	}
}

function clearNode(nodeId)
{
	var node = document.getElementById(nodeId);
	if (node)
	{
		node.style.display = 'none';
	}
}
