jQuery(document).ready(function($) {
  
  // Format all legacy US zip codes to include only 5-digits
  $('form input[id*="postcode"]').each(function() {
    var locale = $('form select[name*="country"] option:selected').val();
    if (locale=="US") {
      var zip = $('form input[id*="postcode"]').val();
      var new_zip = zip.substr(0, 5);
      $('form input[id*="postcode"]').val(new_zip);
    }
  });
     
  // Account address book 
  $('#add_address form input#postcode').attr("maxlength", 5);
  $('#add_address form select[name="country_id"]').change(function(){
    $('#add_address form select[name="country_id"] option:selected').each(function() {
      var country = $(this).val();
      if (country=="US") {
       $('#add_address form input#postcode').attr("maxlength", 5);
       var zip = $('#add_address form input#postcode').val();
       var new_zip = zip.substr(0, 5);
       $('#add_address form input#postcode').val(new_zip);
      } else {
        $('#add_address form input#postcode').attr("maxlength", 100);
      }
    });
  });
  
  // Shipping cost estimator
  $('form#shipping-zip-form input#postcode').attr("maxlength", 5);
  $('form#shipping-zip-form select#country').change(function(){
    $('form#shipping-zip-form select#country option:selected').each(function() {
      var country = $(this).val();
      if (country=="US") {
       $('form#shipping-zip-form input#postcode').attr("maxlength", 5);
       var zip = $('form#shipping-zip-form input#postcode').val();
       var new_zip = zip.substr(0, 5);
       $('form#shipping-zip-form input#postcode').val(new_zip);
      } else {
        $('form#shipping-zip-form input#postcode').attr("maxlength", 100);
      }
    });
  });
  
  // Checkout - Billing
  $('form#co-billing-form input[id*="postcode"]').attr("maxlength", 5);
  $('form#co-billing-form select[id*="country_id"]').change(function(){
    $('form#co-billing-form select[id*="country_id"] option:selected').each(function() {
      var country = $(this).val();
      if (country=="US") {
       $('form#co-billing-form input[id*="postcode"]').attr("maxlength", 5);
       var zip = $('form#co-billing-form input[id*="postcode"]').val();
       var new_zip = zip.substr(0, 5);
       $('form#co-billing-form input[id*="postcode"]').val(new_zip);
      } else {
        $('form#co-billing-form input[id*="postcode"]').attr("maxlength", 100);
      }
    });
  });

  // Checkout - Shipping
  $('form#co-shipping-form input[id*="postcode"]').attr("maxlength", 5);
  $('form#co-shipping-form select[id*="country_id"]').change(function(){
    $('form#co-shipping-form select[id*="country_id"] option:selected').each(function() {
      var country = $(this).val();
      if (country=="US") {
       $('form#co-shipping-form input[id*="postcode"]').attr("maxlength", 5);
       var zip = $('form#co-shipping-form input[id*="postcode"]').val();
       var new_zip = zip.substr(0, 5);
       $('form#co-shipping-form input[id*="postcode"]').val(new_zip);
      } else {
        $('form#co-shipping-form input[id*="postcode"]').attr("maxlength", 100);
      }
    });
  });
});
