Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.16 KB | None | 0 0
  1. $('form').submit(function (e) {
  2. "use strict";
  3. var submittedAddress, shipOptions, mismatch, addressString, UPSAddress;
  4. if ($('input[name=valid_address]').val() === '0') {
  5. e.preventDefault();
  6. 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()};
  7. shipOptions = '';
  8. mismatch = 0;
  9. $.when($.ajax({
  10. type: 'POST',
  11. dataType: 'json',
  12. url: 'https://api.katom.com/shipping/address/validate',
  13. data: submittedAddress,
  14. error: function () {
  15. document.getElementById('shippingAddressForm').submit();
  16. },
  17. success: function (result) {
  18. UPSAddress = result;
  19. if (UPSAddress.errors) {
  20. $(UPSAddress.errors).each(function (key, value) {
  21. $('#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">&times;</span><span class="sr-only">Close</span></button>' + value + '</div>');
  22. });
  23. } else {
  24. $(submittedAddress).each(function (key, val) {
  25. 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 />";
  26. shipOptions += val.name + " " + val.address_1 + " ";
  27. if (val.address_2 !== "") {
  28. shipOptions += val.address_2 + " ";
  29. }
  30. shipOptions += val.city + " " + val.state_province_code + " " + val.postal_code + ", " + val.country_code;
  31. shipOptions += "</div></div><div class='clearfix'></div>";
  32. });
  33. var counter = 1;
  34. $(UPSAddress).each(function (key, value) {
  35. var shipClass = (counter < 2) ? " checked": "";
  36. if (value.AddressLine.constructor === Array) {
  37. if (value.AddressLine[0] !== submittedAddress.address_1.toUpperCase()) {
  38. addressString = value.AddressLine[0].split(" ");
  39. 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 />";
  40. shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
  41. shipOptions += "<span class='text-danger'><strong>";
  42. $(addressString).each(function (key, addressVal) {
  43. if (key === 0) {
  44. shipOptions += addressVal + " ";
  45. } else {
  46. shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
  47. }
  48. });
  49. shipOptions += "</strong></span> ";
  50. mismatch += 1;
  51. } else {
  52. addressString = value.AddressLine[0].split(" ");
  53. 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 />";
  54. shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
  55. $(addressString).each(function (key, addressVal) {
  56. if (key === 0) {
  57. shipOptions += addressVal + " ";
  58. } else {
  59. shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
  60. }
  61. });
  62. }
  63. if (value.AddressLine[1] !== submittedAddress.address_2.toUpperCase()) {
  64. shipOptions += "<span class='text-danger'><strong>";
  65. shipOptions += value.AddressLine[1].charAt(0).toUpperCase() + value.AddressLine[1].substring(1).toLowerCase();
  66. shipOptions += "</strong></span> ";
  67. } else {
  68. shipOptions += value.AddressLine[1].charAt(0).toUpperCase() + value.AddressLine[1].substring(1).toLowerCase() + " ";
  69. }
  70. } else {
  71. if (value.AddressLine !== submittedAddress.address_1.toUpperCase()) {
  72. addressString = value.AddressLine.split(" ");
  73. 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 />";
  74. shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
  75. shipOptions += "<span class='text-danger'><strong>";
  76. $(addressString).each(function (key, addressVal) {
  77. if (key === 0) {
  78. shipOptions += addressVal + " ";
  79. } else {
  80. shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
  81. }
  82. });
  83. shipOptions += "</strong></span> ";
  84. mismatch += 1;
  85. } else {
  86. addressString = value.AddressLine.split(" ");
  87. 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 />";
  88. shipOptions += $('[name=shipping_first_name]').val() + " " + $('[name=shipping_last_name]').val() + ", ";
  89. $(addressString).each(function (key, addressVal) {
  90. if (key === 0) {
  91. shipOptions += addressVal + " ";
  92. } else {
  93. shipOptions += addressVal.charAt(0).toUpperCase() + addressVal.substring(1).toLowerCase() + " ";
  94. }
  95. });
  96. }
  97. }
  98. if (value.PoliticalDivision2 !== submittedAddress.city.toUpperCase()) {
  99. shipOptions += "<span class='text-danger'><strong>" + value.PoliticalDivision2.charAt(0).toUpperCase() + value.PoliticalDivision2.substring(1).toLowerCase() + "</strong></span> ";
  100. mismatch += 1;
  101. } else {
  102. shipOptions += value.PoliticalDivision2.charAt(0).toUpperCase() + value.PoliticalDivision2.substring(1).toLowerCase() + " ";
  103. }
  104. if (value.PoliticalDivision1 !== submittedAddress.state_province_code.toUpperCase()) {
  105. shipOptions += "<span class='text-danger'><strong>" + value.PoliticalDivision1 + "</strong></span> ";
  106. mismatch += 1;
  107. } else {
  108. shipOptions += value.PoliticalDivision1 + " ";
  109. }
  110. if (submittedAddress.postal_code.indexOf('-') === 5) {
  111. if (value.PostcodePrimaryLow !== submittedAddress.postal_code.split('-')[0].toUpperCase()) {
  112. shipOptions += "<span class='text-danger'><strong>" + value.PostcodePrimaryLow + "</strong></span>-";
  113. mismatch += 1;
  114. if (value.PostcodeExtendedLow !== submittedAddress.postal_code.split('-')[1].toUpperCase()) {
  115. shipOptions += "<span class='text-danger'><strong>" + value.PostcodeExtendedLow + "</strong></span> ";
  116. mismatch += 1;
  117. } else {
  118. shipOptions += value.PostcodeExtendedLow + " ";
  119. }
  120. } else {
  121. shipOptions += value.PostcodePrimaryLow + " ";
  122. }
  123. } else {
  124. if (value.PostcodePrimaryLow !== submittedAddress.postal_code.toUpperCase()) {
  125. shipOptions += "<span class='text-danger'><strong>" + value.PostcodePrimaryLow + "</strong></span>-";
  126. mismatch += 1;
  127. } else {
  128. shipOptions += value.PostcodePrimaryLow + "-";
  129. }
  130. if (value.PostcodeExtendedLow !== submittedAddress.postal_code.toUpperCase()) {
  131. shipOptions += "<span class='text-danger'><strong>" + value.PostcodeExtendedLow + "</strong></span> ";
  132. mismatch += 1;
  133. } else {
  134. shipOptions += value.PostcodeExtendedLow + " ";
  135. }
  136. }
  137. shipOptions += "</div></div><div class='clearfix'></div>";
  138. counter += 1;
  139. });
  140. }
  141. $(document).on('change', '[name=shipping]', function () {
  142. $('[name=shipping]').parent().parent().removeClass('checked');
  143. $(this).parent().parent().addClass('checked');
  144. });
  145. $('.shipTo').click(function () {
  146. $('.shippingModal').modal('hide');
  147. $('[name=valid_address]').val(1);
  148. var address = UPSAddress[$("[name=shipping]:checked").parent().parent().attr('name')];
  149. if (address.AddressLine.constructor === Array) {
  150. addressString = address.AddressLine[0].split(" ");
  151. var addressTwo = address.AddressLine[1];
  152. $('#shipping_address_2').val(addressTwo);
  153. } else {
  154. addressString = address.AddressLine.split(" ");
  155. }
  156. $('#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());
  157. $('#shipping_city').val(address.PoliticalDivision2.charAt(0).toUpperCase() + address.PoliticalDivision2.substring(1).toLowerCase());
  158. $('#shipping_state').val(address.PoliticalDivision1);
  159. $('#shipping_country').val(address.CountryCode);
  160. $('#shipping_zip').val(address.PostcodePrimaryLow + "-" + address.PostcodeExtendedLow);
  161. document.getElementById('shippingAddressForm').submit();
  162. });
  163. $('.editAddress').click(function () {
  164. $('[name=valid_address]').val(1);
  165. $('.shippingModal').modal('hide');
  166. });
  167. $('.shippingModal').on('hidden.bs.modal', function () {
  168. $('.modal-body div').remove();
  169. });
  170. }
  171. })).then(function () {
  172. if (!UPSAddress.errors) {
  173. if (mismatch > 0) {
  174. $('.modal-body').append(shipOptions);
  175. $('.shippingModal').modal('show');
  176. } else {
  177. $('[name=valid_address]').val(1);
  178. document.getElementById('shippingAddressForm').submit();
  179. }
  180. }
  181. });
  182. } else {
  183. document.getElementById('shippingAddressForm').submit();
  184. }
  185. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement