function search_store() {
  $('#new_store').find('input').val('');
  $('#new_store').hide();
  $('#stores, #store, #store_search_container').show();

  $.ajax({
    url: '/stores?zip='+ $('#zip_search').val() + '&carrier=' + $('#carrier_search').val(),
    success: function(response) {
      $('#stores').html(response);

      $('#account_store_id').change(function() {
        if ($('#account_store_id').val() != '' && $('#account_store_id').val() != null && typeof $('#account_store_id').val() != 'undefined') {
          $('.tmobile_personnel_number').hide();
          $.ajax({
            url: '/stores/' + $('#account_store_id').val(),
            success: function(response) {
              $('#store').html(response);
              // Display P# if this is a T-Mobile store
              var carriers = $('.carriers').text().split(',');
              if ($.inArray('tmobile', carriers) != -1) {
                $('.tmobile_personnel_number').show();
              }
            }
          });
        } else {
          $('#store').html("");
        }
      });

      if ($('#store_id').val() != '') {
        $('#account_store_id').val($('#store_id').val());
      }

      $('#account_store_id').change();

      // My Store is Not Listed
      $('#show_new_store').click(function() {
        $('#new_store').show();
        $('#stores, #store, #store_search_container, #carrier_search_container').hide();
        $(this).hide();
        $('#account_store_id').val('');
      });
    }
  });
}

$(document).ready(function() {
  $('input').val($(this).attr('value'));
  $('#search_store').button();
  $('#signup_cancel').click(function() { location.href='/'; });

  $('#carrier_search').change(function () {
    var $carrier = $(this);
    if ($carrier.val() == '' || $carrier.val() == null || typeof $carrier.val() == 'undefined') {
      $('#stores, #store, #store_search_container').hide();
    } else {
      if ($('#store_search_container').is(':hidden')) {
        $('#store_search_container').show();
        $('#search_store').css({width: '52px'});
        $('#search_store > .text').css({width: '38px'});
      }
      if ($('#stores').is(':visible')) {
        search_store();
      }
    }
  });

  $('#search_store').click(search_store);

  if ($('#store_id').val() != '' && $('#store_id').val() != null && typeof $('#store_id').val() != 'undefined') {
    $('#stores, #store, #store_search_container').show();
    $.ajax({
      url: '/stores/' + $('#store_id').val(),
      success: function(response) {
        $('#store').html(response);
        $('#zip_search').val($('#store_zip').text());
        $('#search_store').click();
        $('#account_store_id').val($('#store_id').val());
      }
    });
  }

  $submitted = false;
  $('.submit').click(function (event) {
    if ( $submitted ) { return false; }
    if ($('#signup').length !== 0 && $('#new_store:visible').length !== 0) {
      var valid = true;

      $('#new_store').find('input').each(function () {
        var errorSelector = '#' + $(this).attr('id') + '_error';
        if ($(errorSelector).length !== 0) {
          if ($(this).val() != '') {
            $(errorSelector).hide();
          }
          else {
            $(errorSelector).show();
            valid = false;
          }
        }
      });

      if (valid) {
        $(this).find('.text').html('Registering...').width('77px').parent().width('90px').css('cursor', 'normal');
        $('#signup').submit();
        $submitted = true;
      }
      else {
        // Not supported in this version of jQuery?
        //event.stopImmediatePropagation();
        return false;
      }

    }
    else {
      $(this).find('.text').html('Registering...').width('77px').parent().width('90px').css('cursor', 'default');
      $('#signup').submit();
      $submitted = true;
    }
  });

  // Hitting enter while in the store zip field
  $('#zip_search').keypress(function(e) {
    if (e.which == 13) {
      e.preventDefault();
      search_store();
    }
  });
});


