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;

     window.location.href = "/company/" + str_replace(li.selectValue," ","_");
     //alert("The value you selected was: " + sValue);
}

function selectItem(li) {
    findValue(li);
}

function formatItem(row) {
    return row[0] + " <strong>(" + row[1] + ")</strong>";
    //return row[0];
}

function lookupAjax(){
    var oSuggest = $("#CityAjax")[0].autocompleter;

    oSuggest.findValue();

    return false;
}

function lookupLocal(){
    var oSuggest = $("#CityLocal")[0].autocompleter;

    oSuggest.findValue();

    return false;
}

function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

$(document).ready(function() {
    $("#CityAjax").autocomplete("ajax",
        {
            delay:50,
            minChars:2,
            matchSubset:1,
            matchContains:1,
            cacheLength:10,
            onItemSelect:selectItem,
            onFindValue:findValue,
            formatItem:formatItem,
            maxItemsToShow:12
        }
    );


});