aboutsummaryrefslogtreecommitdiff
path: root/static/juandelacosa.js
blob: 5552f9f6185857d0a9febffed8573db112644e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
$(function() {
  var info = $('#info');
  var infoHead = $('#info>h1');
  var infoAlert = $('#info>div');
  var main = $('#main');
  var noSuchAccount = $('#noSuchAccount');
  var password = $('#password');
  var passwordMessage = $('#passwordMessage');
  var resetPassword = $('#resetPassword');

  document.title = window.location.hostname + ' - ' + 'Juan De La Cosa';

  (function whoAmI() {
    $.ajax({
      url: "whoAmI",
      method: "GET",
      success: function(login) {
        info.hide();
        main.show();
        $('#login').val(login);
        setTimeout(whoAmI, 60 * 1000);
      },
      error: function(jqXHR, textStatus, errorThrown) {
        main.hide();
        if ((404 == jqXHR.status) && ('' != jqXHR.responseText)) {
          infoHead.text('Your account does not exist');
          infoAlert.html('Your login would be <strong>' + jqXHR.responseText +
            '</strong>, but it does not exist in the database.');
          infoAlert.removeClass().addClass('alert alert-info');
          setTimeout(whoAmI, 60 * 1000);
        } else {
          infoHead.text('An error has occured');
          infoAlert.text((0 == jqXHR.readyState) ?
            'Service unavailable' : errorThrown);
          infoAlert.removeClass().addClass('alert alert-danger');
          setTimeout(whoAmI, 10 * 1000);
        }
        info.show();
      }
    })
  })();

  function showPasswordMessage(succ, msg, done) {
    if (succ) {
      passwordMessage.removeClass().addClass('alert alert-success');
    } else {
      passwordMessage.removeClass().addClass('alert alert-danger');
    }
    passwordMessage.fadeIn();
    passwordMessage.text(msg);
    setTimeout(function() {
      passwordMessage.fadeOut();
      if (typeof done == 'function') done();
    }, 5 * 1000);
  };

  resetPassword.click(function() {
    $.ajax({
      url: "resetMyPassword",
      method: "POST",
      error: function(jqXHR, textStatus, errorThrown) {
        resetPassword.prop('disabled', true);
        showPasswordMessage(false, (0 == jqXHR.readyState) ?
          'Service unavailable' : errorThrown,
          function() {
            resetPassword.prop('disabled', false)
          });
      },
      success: function(newpwd) {
        resetPassword.hide();
        password.val(newpwd);
        password.show();
        password.select();

        showPasswordMessage(true, 'Password changed.');

        setTimeout(function() {
          password.val('');
          password.hide();
          resetPassword.show();
        }, 10 * 1000);
      }
    });
  })
});