function pad(integer) {
  return String("0" + integer).slice(-2);
}

$(function() {
  if ($('#dev-user-switcher').length) {
    $('#dev-user-switcher').show();
    
    $('#dev-user-switcher select').change(function() {
      if ($(this).val() != '') {
        if ($(this).val() == 'new_yearbook') {
          $.ajax({
            url: '/newyearbook',
            success: function(data) {
              var re = /<input name="token_newyearbook_form" type="hidden" value="([a-z0-9]+)">/
              var tmp = data.match(re);
              var token = tmp[1];
              
              var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
              var date = new Date();
              
              $.ajax({
                url: '/newyearbook',
                type: 'POST',
                data: {
                  form_id: 'newyearbook_form',
                  token_newyearbook_form: token,
                  group_name: 'Test Yearbook: ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds()) + ', ' + pad(date.getDate()) + ' ' + months[date.getMonth()],
                  town_city: 'Cambridge',
                  group_type: 'other',
                  people: 100,
                  captcha: 'captcha',
                  name: 'Test Editor',
                  email: 'testeditor' + Math.round(date.getTime() / 1000) + '@allyearbooks.dev',
                  password: 'password',
                  year: 2012
                },
                success: function(data) {
                  var re = /href="(\/wizard\/([0-9]+)\/skip)"/;
                  var matches = data.match(re);
                  
                  window.location = matches[1];
                }
              });
            }
          });
        }
        else if ($(this).val() == 'uid') {
          var uid = window.prompt("What is the user ID?", "");
          location.href = '/yearbook/' + test_yearbook_id + '/emulate_user/' + uid + '/brochureware';
        }
        else {
          location.href = '/yearbook/' + test_yearbook_id + '/emulate_user/' + $(this).val() + '/brochureware';
        }
      }
    });
  }
});

function infopack_sending_form() {
  $('#address').keyup(update_address);
}

function update_address() {
  $('#address-label').text($.trim($('#address').val()));
}
