/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

function sendTelegram() {
    app.dialog.preloader("Sending...");
    apirequest(SETTINGS.apis.sendtelegram, {
        accountnumber: getStorage("accountnumber"),
        accountkey: getStorage("accountkey"),
        message: $("#telegrammessage").val(),
        to: $("#telegramto").val(),
        from: $("#telegramfrom").val(),
        instructions: $("#telegraminstructions").val(),
        usestop: $("#stopperiodcheckbox").is(":checked") ? "1" : "0"
    }, function (resp) {
        app.dialog.close();
        if (resp.status == "OK") {
            app.dialog.alert(resp.msg, "Telegram Sent!");
            sendActionReport("Telegram", "Telegram sent");
        } else if (resp.status == "ERROR") {
            app.dialog.alert(resp.msg, "Error");
            sendErrorReport("Telegram", "Couldn't send telegram", resp.msg);
        }
    }, function (xhr, status, error) {
        app.dialog.close();
        app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
        sendErrorReport("Telegram", "Couldn't send telegram", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
    });
}