1function sendStellariumRequest(buttonId, serverUrl, password) {
2    var content = document.getElementById(buttonId).dataset.content;
3    if (serverUrl.charAt(serverUrl.length - 1) === '/') {
4        serverUrl = serverUrl.slice(0, -1);
5    }
6    var url = serverUrl + '/api/main/focus';
7    var xhr = new XMLHttpRequest();
8    xhr.open('POST', url);
9    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
10    if (password != "") {
11        xhr.setRequestHeader('Authorization', 'Basic ' + btoa(':' + password));
12    }
13
14    xhr.onerror = function(e) {
15        alert("Cannot connect to Stellarium. Check the server URL and if Stellarium is running the HTTP control plugin server.");
16    };
17    xhr.onload = function() {
18        switch (xhr.status) {
19            case 200:
20                var response = xhr.responseText;
21                if (response === "false") {
22                    alert("Target "+content+" not found");
23                }
24                break;
25            case 401:
26                alert("Password incorrect");
27                break;
28            default:
29                alert("Unable to communicate with Stellarium");
30                break;
31        }
32    };
33
34    var formBody = [];
35    formBody.push("mode=zoom");
36    formBody.push("target=" + encodeURIComponent(content));
37    formBody = formBody.join("&");
38    xhr.send(formBody);
39}
40