var xmlUtil = new XMLUtil();
var bd = new BrowserDetection();
var http;
var winRet = /\r\n$/;
var unxRet = /\n$/;
var domainRe = /http:\/\/(.+?)\//;
var domain = domainRe.exec(document.location.href)[1];
var currentRow = -1;
var resultArray = new Array();
var currentTerm = null;
var key;

function handleKeyPress(e)
{
	// IE
	if(window.event)
	{
		key = e.keyCode;
	}
	// Moz
	else
	{
		key = e.which;
	}

	// Scrolling
	if ((key == 38 || key == 40) && resultArray.length > 0)
	{					
		if (key == 38)
		{
			if (currentRow != 0)
			{
				inRow(currentRow - 1);
			}
		}
		else
		{
			if (currentRow != (resultArray.length - 1))
			{
				inRow(currentRow + 1);
			}					
		}
		return;
	}
	
	if (key == 8 || (key >= 48 && key <= 90))
	{
		var searchTerm = document.getElementById('hotel_location').value;
		if (searchTerm.length > 0)
		{
			setTimeout('searchMxna("'+searchTerm+'")', 200);
		}
		else
		{
			populateResults('');
		}
	}

	// Escape
	if (key == 27)
	{
		close();
		return;
	}
}

function showWaitPage()
{
	document.getElementById('container').style.display = 'none';
	document.getElementById('WaitPage').style.display = 'block'
}

function searchMxna(hint)
{
	if ( hint != document.getElementById('hotel_location').value )
	{
		return;
	}

	http = xmlUtil.getXMLHTTPObject();
	http.open('GET', 'http://'+domain+'/hotelLocations?'+escape(hint), false);
	http.send(null);
	handleSearchResults();
}

function handleSearchResults()
{
	if (http.readyState == xmlUtil.COMPLETE && http.status == 200)
	{
		var res = http.responseText;
		resultArray = new Array();
		
			if (res.search(winRet) != -1)
			{
				res = res.substring(0, res.length - 2);
			}
			else if (res.search(unxRet) != -1)
			{
				res = res.substring(0, res.length - 1);	
			}
			
		var html = new String();
		if (res.length > 0)
		{
			resultArray = res.split('\n');
			html += '<div id="suggestion_table">';
			for (var i = 0; i < resultArray.length; ++i)
			{
				var term = resultArray[i];
				html += '<div class="row" id="row_'+i+'" onMouseOver="inRow('+i+')" onClick="gotoMxna();">'+term+'</div>';
			}
			html += '</div>';
		}
		else
		{
			html = '<strong>No suggestions found.</strong>';	
		}
		populateResults(html);
		inRow(0);
	}
}

function populateResults(val)
{
	document.getElementById('hotelForm').style.display = 'none';
	document.getElementById('suggestion').style.backgroundColor = 'white';
	replaceHTML('suggestion', val);
}

function acClose() { close() }

function close()
{
	document.getElementById('hotelForm').style.display = 'block';
	document.getElementById('suggestion').style.backgroundColor = '';
	replaceHTML('suggestion', '');	
}

function changeRowColor(id, fgColor, bgColor)
{
	document.getElementById('row_'+id).style.backgroundColor = bgColor;
}

function inRow(id)
{
	if (currentRow != -1)
	{
		changeRowColor(currentRow, 'green', '#fff');
	}
	currentRow = id;
	currentTerm = resultArray[currentRow];
	changeRowColor(currentRow, '#fff', '#90ACC8');

	if (key != 8)
	{
		autoComplete();
	}
}

function autoComplete()
{
	if (bd.isSafari)
	{
		return;	
	}

	var textInput = document.getElementById('hotel_location');
	var startVal = textInput.value;
	textInput.value = currentTerm;


	if (!bd.isIE)
	{
		textInput.setSelectionRange(startVal.length, currentTerm.length);
	}
	else
	{
        var range = textInput.createTextRange();
		range.moveStart("character", startVal.length);
		range.moveEnd("character", currentTerm.length);
		range.select();	
	}
}
