// Provide suggestions for Places
function RemoteSuggestions()
{
	if (typeof XMLHttpRequest != "undefined")
	{
		this.http = new XMLHttpRequest();
	} else if (typeof ActiveXObject != "undefined")
	{
		this.http = new ActiveXObject("MSXML2.XmlHttp");
	}
//	this.http = new AJAXObject();
}

function NormalizeValue(val)
{
	var retval = val;
	var CharsToRemove = new Array('\'', '.', ',', '-', '"', '’');
	
//	var CharsToReplace = "àâäáéèêëìîïòôöùûüçñ";

	var CharsToReplace = new Array();
	CharsToReplace[0] = String.fromCharCode(224); 
	CharsToReplace[1] = String.fromCharCode(226);
	CharsToReplace[2] = String.fromCharCode(228);
	CharsToReplace[3] = String.fromCharCode(225);
	CharsToReplace[4] = String.fromCharCode(233);
	CharsToReplace[5] = String.fromCharCode(232);
	CharsToReplace[6] = String.fromCharCode(234);
	CharsToReplace[7] = String.fromCharCode(235);
	CharsToReplace[8] = String.fromCharCode(236);
	CharsToReplace[9] = String.fromCharCode(238);
	CharsToReplace[10] = String.fromCharCode(239);
	CharsToReplace[11] = String.fromCharCode(242);
	CharsToReplace[12] = String.fromCharCode(244);
	CharsToReplace[13] = String.fromCharCode(246);
	CharsToReplace[14] = String.fromCharCode(249);
	CharsToReplace[15] = String.fromCharCode(251);
	CharsToReplace[16] = String.fromCharCode(252);
	CharsToReplace[17] = String.fromCharCode(231);
	CharsToReplace[18] = String.fromCharCode(241);
	
	var ReplacementChars = "aaaaeeeeiiiooouuucn";

	var MultiReplacementChars = new Array('oe', 'ss', 'AE', 'ae', 'Th', 'th');
	var MultiCharsToReplace = new Array();
	MultiCharsToReplace[0] = String.fromCharCode(156); 
	MultiCharsToReplace[1] = String.fromCharCode(223);
	MultiCharsToReplace[2] = String.fromCharCode(198);
	MultiCharsToReplace[3] = String.fromCharCode(230);
	MultiCharsToReplace[4] = String.fromCharCode(222);
	MultiCharsToReplace[5] = String.fromCharCode(254);
	
	for (var i=0; i < CharsToRemove.length; i++)
	{
		retval = retval.replace(CharsToRemove[i],"");
	}

	for (var i=0; i < CharsToReplace.length; i++)
	{
		retval = retval.replace(CharsToReplace[i],ReplacementChars.charAt(i));
	}

	for (var i=0; i < MultiCharsToReplace.length; i++)
	{
		retval = retval.replace(MultiCharsToReplace[i],MultiReplacementChars[i]);
	}

	return retval;
}
// Request suggestions for the given autosuggest control
// Param1: oAutoSuggestControl - The autosuggest control to provide suggestions for
// Param2: bTypeAhead - if true then do type-ahead
RemoteSuggestions.prototype.requestSuggestions = function (oAutoSuggestControl, bTypeAhead)
{
	var oHttp = this.http;

	// is there already a live request?
	if (oHttp.readyState != 0)
	{
		// Yes, if we can still use a subset of the suggestions we got last then do it,
		// else cancel it.
		
		if (oAutoSuggestControl.retrievedAll.length > 0 && oAutoSuggestControl.userInput.length >= oAutoSuggestControl.minChars)
		{
		
		//alert(oAutoSuggestControl.userInput);
			// We retrieved a "complete" set of suggestions, but...
			// are they still valid for the current user input?
			var userInputLC = oAutoSuggestControl.userInput.toLowerCase();
			
			var normalizedUserInputLC = NormalizeValue(oAutoSuggestControl.userInput.toLowerCase());
						
			if (userInputLC.indexOf(oAutoSuggestControl.retrievedAll.toLowerCase()) == 0)
			{
				var aSuggestions = [];
				for (var i=0; i < oAutoSuggestControl.suggestions.length; i++)
				{
					//if there is a delimiter...then we have a normalized value being passed in from the service that we will use to show the right list values in the display box.
					var delimiterPos = oAutoSuggestControl.suggestions[i].indexOf("^");
					if(delimiterPos > 0)
					{
						if (oAutoSuggestControl.suggestions[i].toLowerCase().substring(delimiterPos+1).indexOf(normalizedUserInputLC) == 0)
						{
							aSuggestions.push(oAutoSuggestControl.suggestions[i]);
						}
					}
					else
					{
						if (oAutoSuggestControl.suggestions[i].toLowerCase().indexOf(userInputLC) == 0)
						{
							aSuggestions.push(oAutoSuggestControl.suggestions[i]);
						}
					}
				}
				oAutoSuggestControl.autosuggest(aSuggestions, bTypeAhead);
				return;
			}
		}
		oHttp.abort();
	}

	if (oAutoSuggestControl.userInput.length >= oAutoSuggestControl.minChars)
	{
	// build the URL
	var sURL = oAutoSuggestControl.url + "&q=" + encodeURIComponent(oAutoSuggestControl.userInput) + "&type=" + oAutoSuggestControl.type;

	try
	{
	oHttp.open("get", sURL , true);
	}
	catch(err)
	{
	}
	
	oHttp.onreadystatechange = function ()
	{
		try
		{
		if (oHttp.readyState == 4)
		{
			if(oHttp.status == 200)
			{
					var foundSuggestions = false;
					res = oHttp.responseXML.documentElement;
					if (res != null)
					{
						obj = res.getElementsByTagName('complete')[0];
						if (obj != null && obj.firstChild != null)
						{
							if (obj.firstChild.data == "true")
							{
								oAutoSuggestControl.retrievedAll = oAutoSuggestControl.userInput;
							} else
							{
								oAutoSuggestControl.retrievedAll = "";
							}
						}
						obj = res.getElementsByTagName('suggestions')[0];
						if (obj != null && obj.firstChild != null)
						{
							foundSuggestions = true;
							oAutoSuggestControl.suggestions = obj.firstChild.data.split("|");
							oAutoSuggestControl.autosuggest(oAutoSuggestControl.suggestions, bTypeAhead);
						}
					}
					if (foundSuggestions == false)
					{
						oAutoSuggestControl.autosuggest(null, bTypeAhead);
					}
				}
			}
		}    
		catch(err)
		{
		//alert(err);
		}
	}
	oHttp.send(null);
	}
}