﻿// http://docs.jquery.com/Plugins/Autocomplete


//
//  given a text input with a class of "location" give them a lookup of locations
//  given another input with an attribute of "locationIdFor" put the id of the original input control
//
function applyLocationAutosuggest() {
    $("input[type='text']", _crudManager.dialogSelector).format();
    var url = _repository.getActionUrl("Location/Find");
    $('input.location').autocomplete(url, {
        formatItem: formatItem,
        formatResult: formatItem
    }).result(autocompleteResult);
}

function formatItem(item) {
    var itemArray = jsonDecode(item);
    if (itemArray.length > 0)
        return jsonDecode(item)[0].LocationName;
    else
        return null;
}

function autocompleteResult(event, data, formatted) {
    var TargetId = "[locationIdFor=" + event.currentTarget.id + "]";
    $(TargetId).val(jsonDecode(data)[0].LocationId);
}
