/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


$("#addpackagebtn").click(function () {
    if ($("input[name=number]").val().trim() == "") {
        playSound("error");
        app.toast.show({
            text: "Please fill in a street number.",
            position: "bottom",
            destroyOnClose: true,
            closeTimeout: 1000 * 10
        });
        return;
    }
    if ($("input[name=street]").val().trim() == "") {
        playSound("error");
        app.toast.show({
            text: "Please fill in a street name.",
            position: "bottom",
            destroyOnClose: true,
            closeTimeout: 1000 * 10
        });
        return;
    }
    if ($("input[name=citystate]").val().trim() == "") {
        playSound("error");
        app.toast.show({
            text: "Please fill in a city and state.",
            position: "bottom",
            destroyOnClose: true,
            closeTimeout: 1000 * 10
        });
        return;
    }
    var address = ($("input[name=number]").val() + " " + $("input[name=street]").val()).toUpperCase();
    $.getJSON(geocodeapi, {
        address: address + " " + $("input[name=citystate]").val().toUpperCase()
    }, function (resp) {
        if (resp.status == "OK") {
            packages.push({
                "address": address,
                "coords": [
                    resp.coords[0],
                    resp.coords[1]
                ]
            });
            localStorage.setItem("packages", JSON.stringify(packages));

            playSound("ok");

            app.toast.show({
                text: 'Package Added!<br><span style="font-size: 80%;">' + address + "</span>",
                position: "bottom",
                destroyOnClose: true,
                closeTimeout: 1000 * 2
            });

            if (map != null) {
                map.updatePackageLayer(packages);
            }
        } else {
            playSound("error");
            app.dialog.alert(resp.message, "Error");
        }
    });
});
