Forward-port self-serve password reset implementation from rel_1_6
[working/Evergreen.git] / Open-ILS / web / opac / skin / default / js / password_reset.js
1 dojo.require('dojo.parser');
2 dojo.require('dijit.Dialog');
3 dojo.require('dijit.form.Button');
4 dojo.require('dijit.form.TextBox');
5
6 dojo.requireLocalization("openils.opac", "opac");
7 opac_strings = dojo.i18n.getLocalization("openils.opac", "opac");
8
9 dojo.addOnLoad(function() {
10
11     // Create the password reset dialog
12     var pwResetFormDlg = createResetDialog();
13     dojo.parser.parse();
14
15     // Connect the buttons to submit / cancel events that override
16     // the default actions associated with the buttons to do
17     // pleasing Ajax things
18     dojo.connect(dijit.byId("cancelButton"), "onClick", function(event) {
19         event.preventDefault();
20         event.stopPropagation();
21         pwResetFormDlg.hide();
22     });
23     dojo.connect(dijit.byId("submitButton"), "onClick", function(event) {
24         event.preventDefault();
25         event.stopPropagation();
26         var xhrArgs = {
27             form: dojo.byId("requestReset"),
28             handleAs: "text",
29             load: function(data) {
30                 pwResetFormDlg.hide();
31                 passwordSubmission(opac_strings.PWD_RESET_SUBMIT_SUCCESS);
32             },
33             error: function(error) {
34                 pwResetFormDlg.hide();
35                 passwordSubmission(opac_strings.PWD_RESET_SUBMIT_ERROR);
36             }
37         }
38         var deferred = dojo.xhrPost(xhrArgs);
39     });
40     dojo.place("<tr><td colspan='2' align='center'><a class='classic_link' id='pwResetLink' onClick='dijit.byId(\"pwResetFormDlg\").show();'</a></td></tr>", "login_tbody");
41     dojo.query("#pwResetLink").attr("innerHTML", opac_strings.PWD_RESET_FORGOT_PROMPT);
42
43 });
44
45 function passwordSubmission( msg ) {
46     var responseDialog = new dijit.Dialog({
47         title: opac_strings.PWD_RESET_RESPONSE_TITLE,
48         style: "width: 35em"
49     });
50     responseDialog.startup();
51     var requestStatusDiv = dojo.create("div", { style: "width: 30em" });
52     var requestStatusMsg = dojo.create("div", { innerHTML: msg }, requestStatusDiv);
53     var okButton = new dijit.form.Button({
54         id: "okButton",
55         type: "submit",
56         label: opac_strings.OK
57     }).placeAt(requestStatusDiv);
58     responseDialog.attr("content", requestStatusDiv);
59     responseDialog.show();
60     dojo.connect(dijit.byId("okButton"), "onClick", responseDialog, "hide");
61 }
62
63 function createResetDialog() {
64     var pwResetFormDlg = new dijit.Dialog({
65         id: "pwResetFormDlg",
66         title: opac_strings.PWD_RESET_FORM_TITLE,
67         style: "width: 35em"
68     });
69     pwResetFormDlg.startup();
70
71     // Instantiate the form
72     var pwResetFormDiv = dojo.create("form", { id: "requestReset", style: "width: 30em", method: "post", action: "/opac/password/en-US" });
73     dojo.create("p", { innerHTML: opac_strings.PWD_RESET_SUBMIT_PROMPT }, pwResetFormDiv);
74     var pwResetFormTable = dojo.create("table", null, pwResetFormDiv);
75     var pwResetFormTbody = dojo.create("tbody", null, pwResetFormTable);
76     var pwResetFormRow = dojo.create("tr", null, pwResetFormTbody);
77     var pwResetFormCell = dojo.create("td", null, pwResetFormRow);
78     var pwResetFormLabel = dojo.create("label", null, pwResetFormCell);
79     dojo.attr(pwResetFormCell, { innerHTML: opac_strings.BARCODE_PROMPT });
80     pwResetFormCell = dojo.create("td", null, pwResetFormRow);
81     var barcodeText = new dijit.form.TextBox({
82         name: "barcode"
83     }).placeAt(pwResetFormCell);
84     pwResetFormRow = dojo.create("tr", {}, pwResetFormTbody);
85     pwResetFormCell = dojo.create("td", {}, pwResetFormRow);
86     dojo.attr(pwResetFormCell, { innerHTML: opac_strings.USERNAME_PROMPT });
87     pwResetFormCell = dojo.create("td", {}, pwResetFormRow);
88     var usernameText = new dijit.form.TextBox({
89         name: "username"
90     }).placeAt(pwResetFormCell);
91     dojo.create("br", null, pwResetFormDiv);
92     var submitButton = new dijit.form.Button({
93         id: "submitButton",
94         type: "submit",
95         label: opac_strings.SUBMIT_BUTTON_LABEL
96     }).placeAt(pwResetFormDiv);
97     var cancelButton = new dijit.form.Button({
98         id: "cancelButton",
99         type: "cancel",
100         label: opac_strings.CANCEL_BUTTON_LABEL
101     }).placeAt(pwResetFormDiv);
102
103     // Set the content of the Dialog to the pwResetForm
104     pwResetFormDlg.attr("content", pwResetFormDiv);
105     return pwResetFormDlg;
106 }
107