Pressing enter/return in the openid url box submits the form
[citadel.git] / webcit / static / authmethods.js
1 /*
2  * Copyright 2010-2011, the Citadel Team
3  * Licensed under the GPL V3
4  *
5  * JavaScript functions which handle various authentication methods.
6  */
7
8
9
10 /****************** COMMON CODE ***********************/
11
12
13 /*
14  * Are we logged in right now?
15  */
16 function IsLoggedIn() {
17         if ($('is_logged_in').innerHTML == "yes") {
18                 return 1;
19         }
20         else {
21                 return 0;
22         }
23 }
24
25
26 /*
27  * Wrapper script to require logging in before completing an action
28  */
29 function GetLoggedInFirst(destination_url) {
30
31         /* If logged in already, go directly to the destination. */
32         if (IsLoggedIn()) {
33                 window.location = decodeURIComponent(destination_url);
34                 return;
35         }
36
37         p = 'push?url=' + destination_url;
38         new Ajax.Request(p, { method: 'get' } );
39
40         /* If not logged in, go modal and ask the user to log in first. */
41         new Ajax.Updater(
42                 'md-content',
43                 'do_template?template=get_logged_in',
44                 {
45                         method: 'get',
46                         onSuccess: function() {
47                                 toggleModal(1);
48                         }
49                 }
50         );
51 }
52
53
54 /*
55  * tab handler for the login box
56  */
57 function authtoggle(show_which_div) {
58         $('authbox_userpass').style.display = 'none';
59         $('authbox_newuser').style.display = 'none';
60         $('authbox_openid').style.display = 'none';
61         $(show_which_div).style.display = 'block';
62 }
63
64
65 /*
66  * Pop out a window for external auth methods
67  * (most of them don't handle inline auth very well)
68  */
69 function do_auth_popout(popout_url) {
70         window.open(popout_url, "authpopout", "status=1,toolbar=0,width=600,height=400");
71 }
72
73
74
75
76 /****************** USERNAME AND PASSWORD ***********************/
77
78 /*
79  * Attempt login with username/password, called from modal dialog
80  */
81 function ajax_try_username_and_password() {
82
83         $('login_errmsg').innerHTML = "";
84         $('ajax_username_password_form').request({
85                 onSuccess: function(ctdlresult) {
86                         if (ctdlresult.responseText.substr(0,1) == '2') {
87                                 window.location = 'pop';
88                         }
89                         else {
90                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
91                         }
92                 }
93         });
94 }
95
96
97 /*
98  * The user pressed a key while in the username or password box.
99  * Is it the enter/return key?  Submit the form.
100  */
101 function username_and_password_onkeypress(e) {
102         if (window.event) {             /* IE */
103                 keynum = e.keyCode
104         }
105         else if (e.which) {             /* real browsers */
106                 keynum = e.which
107         }
108         if (keynum == 13) {             /* enter/return key */
109                 ajax_try_username_and_password();
110         }
111 }
112
113
114 /****************** REGISTER NEW USER ***********************/
115
116 /*
117  * Attempt to create a new local username/password, called from modal dialog
118  */
119 function ajax_try_newuser() {
120
121         $('login_errmsg').innerHTML = "";
122         $('ajax_newuser_form').request({
123                 onSuccess: function(ctdlresult) {
124                         if (ctdlresult.responseText.substr(0,1) == '2') {
125                                 window.location = 'pop';
126                         }
127                         else {
128                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
129                         }
130                 }
131         });
132 }
133
134
135 /*
136  * The user pressed a key while in the newuser or newpassword box.
137  * Is it the enter/return key?  Submit the form.
138  */
139 function newuser_onkeypress(e) {
140         if (window.event) {             /* IE */
141                 keynum = e.keyCode;
142         }
143         else if (e.which) {             /* real browsers */
144                 keynum = e.which;
145         }
146         if (keynum == 13) {             /* enter/return key */
147                 ajax_try_newuser();
148         }
149 }
150
151
152
153
154 /****************** OPENID ***********************/
155
156 /*
157  * Attempt login with OpenID, called from modal dialog
158  */
159 function ajax_try_openid() {
160         $('login_errmsg').innerHTML = "";
161         openid_url = encodeURI($('ajax_openid_form').elements["openid_url"].value);
162         do_auth_popout("openid_login?openid_url=" + openid_url);
163 }
164
165
166 /*
167  * The user pressed a key while in the openid login box.
168  * Is it the enter/return key?  Submit the form.
169  */
170 function openid_onkeypress(e) {
171         if (window.event) {             /* IE */
172                 keynum = e.keyCode;
173         }
174         else if (e.which) {             /* real browsers */
175                 keynum = e.which;
176         }
177         if (keynum == 13) {             /* enter/return key */
178                 ajax_try_openid();
179         }
180 }
181
182