/*
 * 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/.
 */

$("#app").on("submit", "#moneyorderForm", function (e) {
    e.preventDefault();
    verifyMoneyOrder($('#moneyorderserial').val());
    return false;
});

function verifyMoneyOrder(serial) {
    if (typeof serial != "string" || serial == "" || /^[0-9]{3,8}$/.test(serial) == false) {
        app.dialog.alert("Enter a valid Helena Express money order serial number.", "Whoops!");
        return;
    }
    app.dialog.preloader("Working...");
    apirequest(SETTINGS.apis.moneyorderverify, {
        serial: serial
    }, function (resp) {
        app.dialog.close();
        if (resp.status == "OK") {
            app.dialog.alert("Amount: $" + resp.amount
                    + "<br>Issued: " + resp.issued_date + " UTC"
                    + (resp.mo_status == "" ? "" : "<br>Status: " + resp.mo_status),
                    "Money Order #" + resp.serial);
        } else {
            app.dialog.alert(resp.msg, "Error");
        }
    }, function (error) {
        app.dialog.close();
        app.dialog.alert("Connection or server error. Try again later.", "Whoops!");
        sendErrorReport("Money Order", "Verification");
    });
}