function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else 
		var sValue = li.selectValue;
		var encodedInputString=sValue.replace("&", "%26");

	//alert("The value you selected was: " + sValue);
	window.location = "product.listing.php?name="+encodedInputString;
}

function selectItem(li) {
	findValue(li);
}
$(document).ready(function(){
	//Header search
	$("#name").autocomplete("search.cfm",{
		delay:10,
		minChars:1,
		onItemSelect:selectItem,
		onFindValue:findValue,
		maxItemsToShow:20
	});
	//Home page
	$("#NameSearch input").autocomplete("search.cfm",{
		delay:10,
		minChars:1,
		onItemSelect:selectItem,
		onFindValue:findValue,
		maxItemsToShow:20
	});
	//Here
	$("#homePageSearch input").autocomplete("search.cfm",{ 
		delay:10, 
		minChars:1, 
		width:200, 
		onItemSelect:selectItem, 
		onFindValue:findValue, 
		maxItemsToShow:20 });
	// VALIDATE DRUG SEARCH
	$('#HeadSearch').submit(function() {
		var searchWord = $("input#name").val();
			if (searchWord.length < 3) {
				alert('You must enter at least 3 characters to do a search.');
				return false;
				}				
	});

    // VALIDATE DRUG SEARCH
    $("#NameSearch").validate({
        rules: {
            name: {
                required: true,
                minlength: 3
            }
        },
        messages: {
            name: {
                required: "Enter a drug name",
                minlength: "Minimum 3 characters"
            }
        }
    });
});
