Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $('form').submit(function (e) {
- "use strict";
- var submittedAddress, shipOptions, mismatch, addressString, UPSAddress;
- if ($('input[name=valid_address]').val() === '0') {
- e.preventDefault();
- submittedAddress = {name: $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ",", address_1: $('[name=shipping_address]').val(), address_2: $('[name=shipping_address_2]').val(), city: $('[name=shipping_city]').val(), state_province_code: $('[name=shipping_state]').val(), postal_code: $('[name=shipping_zip]').val(), country_code: $('[name=shipping_country]').val()};
- shipOptions = '';
- mismatch = 0;
- $.when($.ajax({
- type: 'POST',
- dataType: 'json',
- url: 'https://api.katom.com/shipping/address/validate',
- data: submittedAddress,
- error: function () {
- document.getElementById('shippingAddressForm').submit();
- },
- success: function (result) {
- UPSAddress = result;
- if (UPSAddress.errors) {
- $(UPSAddress.errors).each(function (key, value) {
- $('#shippingAddress .panel-body').prepend('<div class="alert alert-danger alert-dismissible" role="alert"><button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>' + value + '</div>');
- });
- } else {
- $(submittedAddress).each(function (key, val) {
- shipOptions += "<div class='suggested'><div class='col-md-1'><input type='radio' name='shipping' value='suggested1'></div><div class='col-md-11'><strong>Original Address:</strong><br />";
- shipOptions += val.name + " " + val.address_1 + " ";
- if (val.address_2 !== "") {
- shipOptions += val.address_2 + " ";
- }
- shipOptions += val.city + " " + val.state_province_code + " " + val.postal_code + ", " + val.country_code;
- shipOptions += "</div></div><div class='clearfix'></div>";
- });
- var counter = 1;
- $(UPSAddress).each(function (key, value) {
- var shipClass = (counter < 2) ? " checked": "";
- if (value.AddressLine.constructor === Array) {
- if (value.AddressLine[0] !== submittedAddress.address_1.toUpperCase()) {
- addressString = value.AddressLine[0].split(" ");
- shipOptions += "<div class='suggested" + shipClass + "' name='" + key + "'><div class='col-md-1'><input type='radio' name='shipping' value='suggested1'" + shipClass + "></div><div class='col-md-11'><strong>Suggested Address:</strong><br />";
- shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
- shipOptions += "<span class='text-danger'><strong>";
- $(addressString).each(function (key, addressVal) {
- if (key === 0) {
- shipOptions += addressVal + " ";
- } else {
- shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
- }
- });
- shipOptions += "</strong></span> ";
- mismatch += 1;
- } else {
- addressString = value.AddressLine[0].split(" ");
- shipOptions += "<div class='suggested" + shipClass + "' name='" + key + "'><div class='col-md-1'><input type='radio' name='shipping' value='suggested1'" + shipClass + "></div><div class='col-md-11'><strong>Suggested Address:</strong><br />";
- shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
- $(addressString).each(function (key, addressVal) {
- if (key === 0) {
- shipOptions += addressVal + " ";
- } else {
- shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
- }
- });
- }
- if (value.AddressLine[1] !== submittedAddress.address_2.toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>";
- shipOptions += value.AddressLine[1].charAt(0).toUpperCase() + value.AddressLine[1].substring(1).toLowerCase();
- shipOptions += "</strong></span> ";
- } else {
- shipOptions += value.AddressLine[1].charAt(0).toUpperCase() + value.AddressLine[1].substring(1).toLowerCase() + " ";
- }
- } else {
- if (value.AddressLine !== submittedAddress.address_1.toUpperCase()) {
- addressString = value.AddressLine.split(" ");
- shipOptions += "<div class='suggested" + shipClass + "' name='" + key + "'><div class='col-md-1'><input type='radio' name='shipping' value='suggested1'" + shipClass + "></div><div class='col-md-11'><strong>Suggested Address:</strong><br />";
- shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
- shipOptions += "<span class='text-danger'><strong>";
- $(addressString).each(function (key, addressVal) {
- if (key === 0) {
- shipOptions += addressVal + " ";
- } else {
- shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
- }
- });
- shipOptions += "</strong></span> ";
- mismatch += 1;
- } else {
- addressString = value.AddressLine.split(" ");
- shipOptions += "<div class='suggested" + shipClass + "' name='" + key + "'><div class='col-md-1'><input type='radio' name='shipping' value='suggested1'" + shipClass + "></div><div class='col-md-11'><strong>Suggested Address:</strong><br />";
- shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
- $(addressString).each(function (key, addressVal) {
- if (key === 0) {
- shipOptions += addressVal + " ";
- } else {
- shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
- }
- });
- }
- }
- if (value.PoliticalDivision2 !== submittedAddress.city.toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>" + value.PoliticalDivision2.charAt(0).toUpperCase() + value.PoliticalDivision2.substring(1).toLowerCase() + "</strong></span> ";
- mismatch += 1;
- } else {
- shipOptions += value.PoliticalDivision2.charAt(0).toUpperCase() + value.PoliticalDivision2.substring(1).toLowerCase() + " ";
- }
- if (value.PoliticalDivision1 !== submittedAddress.state_province_code.toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>" + value.PoliticalDivision1 + "</strong></span> ";
- mismatch += 1;
- } else {
- shipOptions += value.PoliticalDivision1 + " ";
- }
- if (submittedAddress.postal_code.indexOf('-') === 5) {
- if (value.PostcodePrimaryLow !== submittedAddress.postal_code.split('-')[0].toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>" + value.PostcodePrimaryLow + "</strong></span>-";
- mismatch += 1;
- if (value.PostcodeExtendedLow !== submittedAddress.postal_code.split('-')[1].toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>" + value.PostcodeExtendedLow + "</strong></span> ";
- mismatch += 1;
- } else {
- shipOptions += value.PostcodeExtendedLow + " ";
- }
- } else {
- shipOptions += value.PostcodePrimaryLow + " ";
- }
- } else {
- if (value.PostcodePrimaryLow !== submittedAddress.postal_code.toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>" + value.PostcodePrimaryLow + "</strong></span>-";
- mismatch += 1;
- } else {
- shipOptions += value.PostcodePrimaryLow + "-";
- }
- if (value.PostcodeExtendedLow !== submittedAddress.postal_code.toUpperCase()) {
- shipOptions += "<span class='text-danger'><strong>" + value.PostcodeExtendedLow + "</strong></span> ";
- mismatch += 1;
- } else {
- shipOptions += value.PostcodeExtendedLow + " ";
- }
- }
- shipOptions += "</div></div><div class='clearfix'></div>";
- counter += 1;
- });
- }
- $(document).on('change', '[name=shipping]', function () {
- $('[name=shipping]').parent().parent().removeClass('checked');
- $(this).parent().parent().addClass('checked');
- });
- $('.shipTo').click(function () {
- $('.shippingModal').modal('hide');
- $('[name=valid_address]').val(1);
- var address = UPSAddress[$("[name=shipping]:checked").parent().parent().attr('name')];
- if (address.AddressLine.constructor === Array) {
- addressString = address.AddressLine[0].split(" ");
- var addressTwo = address.AddressLine[1];
- $('#shipping_address_2').val(addressTwo);
- } else {
- addressString = address.AddressLine.split(" ");
- }
- $('#shipping_address').val(addressString[0] + " " + addressString[1].charAt(0).toUpperCase() + addressString[1].substring(1).toLowerCase() + " " + addressString[2].charAt(0).toUpperCase() + addressString[2].substring(1).toLowerCase());
- $('#shipping_city').val(address.PoliticalDivision2.charAt(0).toUpperCase() + address.PoliticalDivision2.substring(1).toLowerCase());
- $('#shipping_state').val(address.PoliticalDivision1);
- $('#shipping_country').val(address.CountryCode);
- $('#shipping_zip').val(address.PostcodePrimaryLow + "-" + address.PostcodeExtendedLow);
- document.getElementById('shippingAddressForm').submit();
- });
- $('.editAddress').click(function () {
- $('[name=valid_address]').val(1);
- $('.shippingModal').modal('hide');
- });
- $('.shippingModal').on('hidden.bs.modal', function () {
- $('.modal-body div').remove();
- });
- }
- })).then(function () {
- if (!UPSAddress.errors) {
- if (mismatch > 0) {
- $('.modal-body').append(shipOptions);
- $('.shippingModal').modal('show');
- } else {
- $('[name=valid_address]').val(1);
- document.getElementById('shippingAddressForm').submit();
- }
- }
- });
- } else {
- document.getElementById('shippingAddressForm').submit();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement