more font size tweaks.
[citadel.git] / webcit / static / authmethods.js
1 /*
2  * Copyright 2010-2017, 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         toggleModal(1);
42 }
43
44
45 /*
46  * tab handler for the login box
47  */
48 function authtoggle(show_which_div) {
49         $('authbox_userpass').style.display = 'none';
50         $('authbox_newuser').style.display = 'none';
51         $('authbox_openid').style.display = 'none';
52         $('authbox_google').style.display = 'none';
53         $('authbox_yahoo').style.display = 'none';
54         $('authbox_aol').style.display = 'none';
55         $('authbox_success').style.display = 'none';
56         $(show_which_div).style.display = 'block';
57 }
58
59
60 /*
61  * Pop out a window for external auth methods
62  * (most of them don't handle inline auth very well)
63  */
64 function do_auth_popout(popout_url) {
65         window.open(popout_url, "authpopout", "status=1,toolbar=0,width=600,height=400");
66 }
67
68
69
70
71 /****************** USERNAME AND PASSWORD ***********************/
72
73 /*
74  * Attempt login with username/password, called from modal dialog
75  */
76 function ajax_try_username_and_password() {
77
78         $('login_errmsg').innerHTML = "";
79         authtoggle('authbox_success');
80         $('ajax_username_password_form').request({
81                 onSuccess: function(ctdlresult) {
82                         if (ctdlresult.responseText.substr(0,1) == '2') {
83                                 window.location = 'pop';
84                         }
85                         else {
86                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
87                                 authtoggle('authbox_userpass');
88                         }
89                 }
90         });
91 }
92
93
94 /*
95  * The user pressed a key while in the username or password box.
96  * Is it the enter/return key?  Submit the form.
97  */
98 function username_and_password_onkeypress(e) {
99         if (window.event) {             /* IE */
100                 keynum = e.keyCode
101         }
102         else if (e.which) {             /* real browsers */
103                 keynum = e.which
104         }
105         if (keynum == 13) {             /* enter/return key */
106                 ajax_try_username_and_password();
107         }
108 }
109
110
111 /****************** REGISTER NEW USER ***********************/
112
113 /*
114  * Attempt to create a new local username/password, called from modal dialog
115  */
116 function ajax_try_newuser() {
117
118         $('login_errmsg').innerHTML = "";
119         $('ajax_newuser_form').request({
120                 onSuccess: function(ctdlresult) {
121                         if (ctdlresult.responseText.substr(0,1) == '2') {
122                                 authtoggle('authbox_success');
123                                 window.location = 'pop';
124                         }
125                         else {
126                                 $('login_errmsg').innerHTML = ctdlresult.responseText.substr(4) ;
127                         }
128                 }
129         });
130 }
131
132
133 /*
134  * The user pressed a key while in the newuser or newpassword box.
135  * Is it the enter/return key?  Submit the form.
136  */
137 function newuser_onkeypress(e) {
138         if (window.event) {             /* IE */
139                 keynum = e.keyCode;
140         }
141         else if (e.which) {             /* real browsers */
142                 keynum = e.which;
143         }
144         if (keynum == 13) {             /* enter/return key */
145                 ajax_try_newuser();
146         }
147 }
148
149
150
151
152 /****************** OPENID ***********************/
153
154 /*
155  * Attempt login with OpenID, called from modal dialog
156  */
157 function ajax_try_openid() {
158         $('login_errmsg').innerHTML = "";
159         openid_url = encodeURI($('ajax_openid_form').elements["openid_url"].value);
160         do_auth_popout("openid_login?openid_url=" + openid_url);
161 }
162
163
164 /*
165  * The user pressed a key while in the openid login box.
166  * Is it the enter/return key?  Submit the form.
167  */
168 function openid_onkeypress(e) {
169         if (window.event) {             /* IE */
170                 keynum = e.keyCode;
171         }
172         else if (e.which) {             /* real browsers */
173                 keynum = e.which;
174         }
175         if (keynum == 13) {             /* enter/return key */
176                 ajax_try_openid();
177                 return false;
178         }
179         return true;
180 }
181
182
183 /****************** GOOGLE ***********************/
184
185 /*
186  * Attempt login with Google, called from modal dialog
187  */
188 function ajax_try_google() {
189         $('login_errmsg').innerHTML = "";
190         openid_url = encodeURI("https://www.google.com/accounts/o8/id");
191         do_auth_popout("openid_login?openid_url=" + openid_url);
192 }
193
194
195 /****************** YAHOO ***********************/
196
197 /*
198  * Attempt login with Yahoo, called from modal dialog
199  */
200 function ajax_try_yahoo() {
201         $('login_errmsg').innerHTML = "";
202         openid_url = encodeURI("http://yahoo.com");
203         do_auth_popout("openid_login?openid_url=" + openid_url);
204 }
205
206
207 /****************** AOL ***********************/
208
209 /*
210  * Attempt login with AOL, called from modal dialog
211  */
212 function ajax_try_aol() {
213         $('login_errmsg').innerHTML = "";
214         openid_url = encodeURI($('ajax_aol_form').elements["aol_screenname"].value);
215         do_auth_popout("openid_login?openid_url=http://openid.aol.com/" + openid_url);
216 }
217
218
219 /*
220  * The user pressed a key while in the AOL login box.
221  * Is it the enter/return key?  Submit the form.
222  */
223 function aol_onkeypress(e) {
224         if (window.event) {             /* IE */
225                 keynum = e.keyCode;
226         }
227         else if (e.which) {             /* real browsers */
228                 keynum = e.which;
229         }
230         if (keynum == 13) {             /* enter/return key */
231                 ajax_try_aol();
232                 return false;
233         }
234         return true;
235 }
236
237