You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HelenaExpressApp/www/assets/js/rates.js

213 lines
8.9 KiB
JavaScript

/*
* 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/.
*/
var itemTypeCharacteristics = {
"Parcel": {weight: true, size: true, lbs: true},
// USPS
"Card": {weight: false, size: false, lbs: false},
"Letter": {weight: true, size: false, lbs: false},
"Flat": {weight: true, size: false, lbs: true},
"FlatRateEnvelope": {weight: false, size: false, lbs: false},
"FlatRateLegalEnvelope": {weight: false, size: false, lbs: false},
"FlatRatePaddedEnvelope": {weight: false, size: false, lbs: false},
"SmallFlatRateBox": {weight: false, size: false, lbs: false},
"MediumFlatRateBox": {weight: false, size: false, lbs: false},
"LargeFlatRateBox": {weight: false, size: false, lbs: false},
"LargeFlatRateBoxAPOFPO": {weight: false, size: false, lbs: false},
"RegionalRateBoxA": {weight: true, size: false, lbs: true},
"RegionalRateBoxB": {weight: true, size: false, lbs: true},
// UPS
"UPSLetter": {weight: true, size: false, lbs: true},
"UPSExpressBox": {weight: true, size: false, lbs: true},
"Pak": {weight: true, size: false, lbs: true},
"Tube": {weight: true, size: false, lbs: true},
"SmallExpressBox": {weight: true, size: false, lbs: true},
"MediumExpressBox": {weight: true, size: false, lbs: true},
"LargeExpressBox": {weight: true, size: false, lbs: true},
// FedEx
"FedExEnvelope": {weight: true, size: false, lbs: true},
"FedExBox": {weight: true, size: false, lbs: true},
"FedExPak": {weight: true, size: false, lbs: true},
"FedExTube": {weight: true, size: false, lbs: true},
"FedExSmallBox": {weight: true, size: false, lbs: true},
"FedExMediumBox": {weight: true, size: false, lbs: true},
"FedExLargeBox": {weight: true, size: false, lbs: true},
"FedExExtraLargeBox": {weight: true, size: false, lbs: true}
};
function updateRateForm() {
if ($("#to_country").val() == "" || $("#to_country").val() == "US") {
$(".citystateboxes").css("display", "none");
$("#zipcodelookuplink").css("display", "");
$("#ziptext").html("ZIP Code");
$("#to_zip").attr("inputmode", "numeric");
$("#to_zip").attr("required", "true");
$("#to_zip").attr("placeholder", "ZIP Code");
} else {
$(".citystateboxes").css("display", "");
$("#zipcodelookuplink").css("display", "none");
$("#ziptext").html("Postal Code");
$("#to_zip").attr("inputmode", "text");
$("#to_zip").removeAttr("required");
$("#to_zip").attr("placeholder", "Postal Code");
}
$("#itemweight").css("display", "none");
$("#itemsize").css("display", "none");
$("#itemweightlbs").css("display", "none");
if ($("#itemType").val() == "") {
// no selection, leave all boxes hidden
return;
}
if (typeof itemTypeCharacteristics[$("#itemType").val()] == 'undefined') {
// not in list of item type info
$("#itemweight").css("display", "");
$("#itemsize").css("display", "");
$("#itemweightlbs").css("display", "");
return;
}
var chars = itemTypeCharacteristics[$("#itemType").val()];
if (chars.weight) {
// show weight box for ounces
$("#itemweight").css("display", "");
} else {
$("#weightLbs").val("0");
$("#weightOz").val("0");
}
if (chars.lbs) {
// show weight box for pounds
$("#itemweightlbs").css("display", "");
} else {
$("#weightLbs").val("0");
}
if (chars.size) {
$("#itemsize").css("display", "");
}
}
$("#app").on("click change", "#itemType", updateRateForm);
$("#app").on("click change", "#to_country", updateRateForm);
function initRateForm() {
$("#itemType").val("");
updateRateForm();
}
function getRates() {
app.dialog.preloader("Loading...");
apirequest(
SETTINGS.apis.rates,
{
from_street1: $("#from_street1").val(),
from_zip: $("#from_zip").val(),
to_street1: $("#to_street1").val(),
to_zip: $("#to_zip").val(),
to_city: $("#to_city").val(),
to_state: $("#to_state").val(),
to_country: $("#to_country").val(),
itemType: $("#itemType").val(),
length: $("input[name=length]").val(),
width: $("input[name=width]").val(),
height: $("input[name=height]").val(),
weightLbs: $("#weightLbs").val(),
weightOz: $("#weightOz").val()
},
function (resp) {
app.dialog.close();
if (resp.status == "OK") {
if (resp.rates.length == 0) {
resp.rates = false;
}
if (resp.hints.length == 0) {
resp.hints = false;
}
$("#rateResultPopup").html(compiledPages.rateresult(resp));
app.popup.open("#rateResultPopup");
sendActionReport("Rates", "Got rates", $("#from_zip").val() + " -> " + $("#to_zip").val());
} else {
app.dialog.alert(resp.message, "Error");
sendErrorReport("Rates", "Couldn't get rates", resp.message);
}
},
function (xhr) {
app.dialog.close();
try {
var error = $.parseJSON(xhr.responseText);
if (error && typeof error.msg != 'undefined') {
app.dialog.alert(error.msg, "Error");
sendErrorReport("Rates", "Couldn't get rates", error.msg);
} else {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
sendErrorReport("Rates", "Couldn't get rates", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
} catch (ex) {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
sendErrorReport("Rates", "Couldn't get rates", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
}, "GET");
}
function doZipCodeLookup(street, city, state) {
if (street == "") {
app.dialog.alert("You need to enter a street address.", "ZIP Code Lookup");
return;
}
if (city == "") {
app.dialog.alert("You need to enter a city.", "ZIP Code Lookup");
return;
}
if (state == "") {
app.dialog.alert("You need to select a state.", "ZIP Code Lookup");
return;
}
app.dialog.preloader("Searching...");
apirequest(
SETTINGS.apis.zipcodelookup,
{
street: street,
city: city,
state: state
},
function (resp) {
app.dialog.close();
if (resp.status == "OK") {
app.popup.close();
if (resp.address.dpv_confirmed) {
// If an exact deliverable match was found, fill in the verified street address
$("#to_street1").val(resp.address.address);
}
$("#to_zip").val(resp.address.zip);
app.input.checkEmptyState('#to_street1');
app.input.checkEmptyState('#to_zip');
app.dialog.alert("The correct ZIP Code is " + resp.address.zip, "ZIP Code Lookup");
} else {
app.dialog.alert(resp.msg, "Error");
sendErrorReport("ZIP Lookup", "Couldn't get ZIP Code", resp.msg);
}
},
function (xhr) {
app.dialog.close();
try {
var error = $.parseJSON(xhr.responseText);
if (error && typeof error.msg != 'undefined') {
app.dialog.alert(error.msg, "Error");
sendErrorReport("ZIP Lookup", "Couldn't get ZIP Code", error.msg);
} else {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
sendErrorReport("ZIP Lookup", "Couldn't get ZIP Code", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
} catch (ex) {
app.dialog.alert("There's a server or network problem. Check your Internet connection or try again later.", "Error");
sendErrorReport("ZIP Lookup", "Couldn't get ZIP Code", "Server/network problem: " + xhr.status + ": " + xhr.statusText);
}
}, "GET");
}