var dlg = null;

function addPerson(uids) {
    var userid = uids[0];
    $.getJSON(base + 'user.ivp/GetUserNameById', { userid: userid, date: new Date().toString() }, function(fio) {
    $('#customfield8').val(fio);
    });
    $.getJSON(base + 'user.ivp/GetUserTelById', { userid: userid, date: new Date().toString() }, function(tel) {
    $('#customfield9').val(tel);
    });
    return false;
}

$(function() {
if ($('#customfield8').size() > 0) {
    //$('#customfield8').autocomplete(base + 'user.ivp/findusers/', { minChars: 3 }).result(addRequester);
    $('#customfield8').devbridgeAutocomplete({
        serviceUrl: 'user.ivp/FindServiceUsersJson/',
        minChars: 1,
        params: { serviceId: $('#serviceid').val() },
        autoClearInputOnSelect: true,
        onSelect: addRequester
    });
    }
});

function selectRequester() {
    var serviceid = $('#serviceid').val();
    var url = 'user.ivp/select?selectmode=SINGLE&tb_mode=serviceuser&tb_serviceid=' + serviceid;
    dlg = window.openDialog(url, 'selectpersons', { width: 900, height: 650 });
    dlg.callback = usersCallback;
    return false;
}

function usersCallback(result) {
    var uids = result["selectedIds"];
    addPerson(uids);
}

function addRequester(suggestion) {
    var name = suggestion.value;
    var id = suggestion.data.id;
    $('#customfield8').val(name);
    $.getJSON(base + 'user.ivp/GetUserTelById', { userid: id, date: new Date().toString() }, function (tel) {
        $('#customfield9').val(tel);
    });
    return false;
}
