﻿//Autocomplete
function lookup(sender, popupId, url, queryStringFieldName) {
	var inputString = sender.value;	
	if (inputString.length == 0) {
		// Hide the suggestion box.
		$(popupId).fadeOut(250);
	}
	else if(inputString.length > 2) {
	$.post(url + "?" + queryStringFieldName + "=" + inputString, null, function(data) {
		if (data.length > 0) {
			$(popupId).css({ "left": $(sender)[0].offsetLeft + 20 + "px" }).fadeIn(250);
			//populate the suggestions box with results returned from the page
			$("#autoSuggestionsList", $(popupId)).html(data);

			//Add the click event and hightlight the current text
			$("#autoSuggestionsList li", $(popupId)).each(function() {
				$(this).html("<strong>" + this.innerText.substring(0, inputString.length) + "</strong>" + this.innerText.substring(inputString.length));
				$(this).click(function() {
					$(sender).val($(this).text());
					$(popupId).fadeOut(250);
				});
			});
		}
		else { 
			$(popupId).fadeOut(250);
		}
	});
	}
}