Enter/return key submits form in the new user/password dialog
[citadel.git] / webcit / static / authmethods.js
1 /*
2  * Copyright 2010, the Citadel Team
3  * Licensed under the GPL V3
4  *
5  * JavaScript functions which handle various authentication methods.
6  */
7
8
9 /*
10  * Are we logged in right now?
11  */
12 function IsLoggedIn() {
13         if ($('is_logged_in').innerHTML == "yes") {
14                 return 1;
15         }
16         else {
17                 return 0;
18         }
19 }
20
21
22
23 /*
24  * Wrapper script to require logging in before completing an action
25  */
26 function GetLoggedInFirst(destination_url) {
27
28         /* If logged in already, go directly to the destination. */
29         if (IsLoggedIn()) {
30                 window.location = decodeURIComponent(destination_url);
31                 return;
32         }
33
34         p = 'push?url=' + destination_url;
35         new Ajax.Request(p, { method: 'get' } );
36
37         /* If not logged in, go modal and ask the user to log in first. */
38         new Ajax.Updater(
39                 'md-content',
40                 'do_template?template=get_logged_in',
41                 {
42                         method: 'get',
43                         onSuccess: function() {
44                                 toggleModal(1);
45                         }
46                 }
47         );
48 }
49
50
51 /*
52  * Attempt login with username/password, called from modal dialog
53  */
54 function ajax_try_username_and_password() {
55
56         $('login_errmsg').innerHTML = "";
57         $('ajax_username_password_form').request({
58                 onSuccess: function(ctdlresult) {
59                         if (ctdlresult.responseText.substr(0,1) == '2') {
60                                 window.location = 'pop';
61                         }
62                         else {
63                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
64                         }
65                 }
66         });
67 }
68
69 /*
70  * The user pressed a key while in the username or password box.
71  * Is it the enter/return key?  Submit the form.
72  */
73 function username_and_password_onkeypress(e) {
74         if (window.event) {             /* IE */
75                 keynum = e.keyCode
76         }
77         else if (e.which) {             /* real browsers */
78                 keynum = e.which
79         }
80         if (keynum == 13) {             /* enter/return key */
81                 ajax_try_username_and_password();
82         }
83 }
84
85
86 /*
87  * tab handler for the login box
88  */
89 function authtoggle(show_which_div) {
90         $('authbox_userpass').style.display = 'none';
91         $('authbox_openid').style.display = 'none';
92         $(show_which_div).style.display = 'block';
93 }