3f55bdef28eea25944e30bd44c37bd8330e93a3a
[evergreen-equinox.git] / Open-ILS / web / opac / skin / kcls / js / sidebar.js
1 /* set up the colors in the sidebar 
2         Disables/Enables certain components based on various state data */
3
4 attachEvt("common", "init", initSideBar);
5 //attachEvt("common", "init", setSidebarLinks);
6 //attachEvt("common", "unload", sidebarTreesFree );
7
8 function prevRResults() {
9         return buildOPACLink({ page : RRESULT });
10 }
11
12 function prevMResults() {
13         return buildOPACLink({ page : MRESULT });
14 }
15
16 function initSideBar() {
17         var page = findCurrentPage();
18
19         if( page == MRESULT ) 
20                 unHideMe($("sidebar_results_wrapper"));
21
22         if( page == RRESULT ) {
23                 unHideMe($("sidebar_results_wrapper"));
24                 unHideMe(G.ui.sidebar[MRESULT]);
25                 if( getRtype() == RTYPE_MRID )
26                         $("sidebar_title_group_results").setAttribute("href", prevMResults());
27                 else hideMe($("sidebar_title_group_results").parentNode);
28         }
29
30         /* if( page == RDETAIL ) {
31                 unHideMe($("sidebar_results_wrapper"));
32                 unHideMe(G.ui.sidebar[MRESULT]);
33                 if(getRtype())
34                         $("sidebar_title_results").setAttribute("href", prevRResults());
35                 unHideMe(G.ui.sidebar[RRESULT]);
36
37                 if( getRtype() == RTYPE_MRID )
38                         $("sidebar_title_group_results").setAttribute("href", prevMResults());
39                 else hideMe($("sidebar_title_group_results").parentNode);
40         } */
41
42         unHideMe(G.ui.sidebar[page]);
43         addCSSClass(G.ui.sidebar[page], "sidebar_item_active");
44
45         /* if we're logged in, show it and replace the Login link with the Logout link */
46         if(grabUser()) {
47                 //G.ui.sidebar.username_dest.appendChild(text(G.user.usrname()));
48                 //unHideMe(G.ui.sidebar.logoutbox);
49                 unHideMe(G.ui.sidebar.logged_in_as);
50                 hideMe(G.ui.sidebar.loginbox);
51         }
52
53         if(G.ui.sidebar.login) G.ui.sidebar.login.onclick = initLogin;
54         //if(G.ui.sidebar.logout) G.ui.sidebar.logout.onclick = doLogout; 
55
56         if(isXUL()) hideMe( G.ui.sidebar.logoutbox );
57 }
58
59 /* sets up the login ui components */
60 var loginBoxVisible = false;
61 function loginDance() {
62         var rem = $('remember_me');
63         if(rem) rem = rem.checked; else rem = false;
64         rem = rem?"persist":"opac";
65         
66         if(doLogin(true, rem)) {
67
68                 if(!strongPassword( G.ui.login.password.value ) ) {
69
70                         cookieManager.write(COOKIE_SES, "");
71                         hideMe($('login_box'));
72                         unHideMe($('change_pw_table'));
73                         $('change_pw_current').focus();
74                         $('change_pw_button').onclick = changePassword;
75                         setEnterFunc($('change_pw_2'), changePassword);
76
77                 } else {
78                         loggedInOK();
79                 }
80         }
81 }
82
83 function loggedInOK() {
84         showCanvas();
85         //G.ui.sidebar.username_dest.appendChild(text(G.user.usrname()));
86         //unHideMe(G.ui.sidebar.logoutbox);
87         unHideMe(G.ui.sidebar.logged_in_as);
88         hideMe(G.ui.sidebar.loginbox);
89         runEvt( 'common', 'loggedIn');
90         
91         var org = G.user.prefs[PREF_DEF_LOCATION];
92     // KCLS wants the defautl search location to be the consortium
93     //if(!org) org = G.user.home_ou();
94     if(!org) org =  globalOrgTree.id();
95
96         var depth = G.user.prefs[PREF_DEF_DEPTH];
97         if(! ( depth && depth <= findOrgDepth(org)) ) 
98                 depth = findOrgDepth(org);
99
100         runEvt( "common", "locationChanged", org, depth);
101 }
102
103
104 function changePassword() {
105
106         var pc = $('change_pw_current').value;
107         var p1 = $('change_pw_1').value;
108         var p2 = $('change_pw_2').value;
109
110         if( p1 != p2 ) {
111                 alert($('pw_no_match').innerHTML);
112                 return;
113         }
114
115         if(!strongPassword(p2, true) ) return;
116
117         var req = new Request(UPDATE_PASSWORD, G.user.session, p2, pc );
118         req.send(true);
119         if(req.result()) {
120                 alert($('pw_update_successful').innerHTML);
121                 loggedInOK();
122         }
123 }
124
125 var pwRegexSetting;
126 function strongPassword(pass, alrt) {
127
128     /* first, let's see if there is a configured regex */
129     if(!pwRegexSetting) {
130         var regex = fetchOrgSettingDefault(G.user.home_ou(), 'global.password_regex');
131         if(regex) {
132             if(pass.match(new RegExp(regex))) {
133                 return true;
134             } else {
135                 if(alrt)
136                        alert($('pw_not_strong').innerHTML);
137                 return false;
138             }
139         }
140     }
141
142     /* no regex configured, use the default */
143
144         var good = false;
145
146         do {
147
148                 if(pass.length < 7) break;
149                 if(!pass.match(/.*\d+.*/)) break;
150                 if(!pass.match(/.*[A-Za-z]+.*/)) break;
151                 good = true;
152
153         } while(0);
154
155         if(!good && alrt) alert($('pw_not_strong').innerHTML);
156         return good;
157 }
158
159 function initLogin() {
160     if(G.ui.login.button) G.ui.login.button.onclick = function(){$('login_form_submit').click();};
161     if(dojo.isIE) {
162         if(G.ui.login.username) G.ui.login.username.onkeydown = function(evt) {if(userPressedEnter(evt)) $('login_form_submit').click();};
163         if(G.ui.login.password) G.ui.login.password.onkeydown = function(evt) {if(userPressedEnter(evt)) $('login_form_submit').click();};
164     }
165
166    //if(findCurrentPage()==MYOPAC) {
167         swapCanvas(G.ui.login.box);
168         try{G.ui.login.username.focus();}catch(e){}
169     //}
170
171     G.ui.login.cancel.onclick = function(evt) { 
172         showCanvas();
173         runEvt("common", "loginCanceled");
174     };
175
176         if(findCurrentPage() == MYOPAC) 
177                 G.ui.login.cancel.onclick = goHome;
178 }
179
180 function setSidebarLinks() {
181         G.ui.sidebar.home_link.setAttribute("href", buildOPACLink({page:HOME}));
182         G.ui.sidebar.advanced_link.setAttribute("href", buildOPACLink({page:ADVANCED}));
183         G.ui.sidebar.myopac_link.setAttribute("href", buildOPACLink({page:MYOPAC}, false, true));
184 }
185
186 function sidebarTreesFree() {
187         return;
188         removeChildren($(subjectSidebarTree.rootid));
189         removeChildren($(authorSidebarTree.rootid));
190         removeChildren($(seriesSidebarTree.rootid));
191         subjectSidebarTree = null;
192         authorSidebarTree = null;
193         seriesSidebarTree = null;
194 }
195
196
197
198
199 /* --------------------------------------------------------------------------------- */
200 /* Code to support GALILEO links for PINES.  Fails gracefully
201 /* --------------------------------------------------------------------------------- */
202 attachEvt('common', 'init', buildEGGalLink);
203 function buildEGGalLink() {
204
205         /* we're in a lib, nothing to do here */
206         if( getOrigLocation() ) return;
207         if(!$('eg_gal_link')) return;
208
209         //var link = 'http://demo.galib.uga.edu/express?pinesid=';
210         var link = 'http://www.galileo.usg.edu/express?pinesid=';
211         if(grabUser()) {
212                 $('eg_gal_link').setAttribute('href', link + G.user.session);
213                 return;
214         }
215
216         $('eg_gal_link').setAttribute('href', 'javascript:void(0);');
217         $('eg_gal_link').setAttribute('target', '');
218         $('eg_gal_link').onclick = function() {
219                 /* we're not logged in.  go ahead and login */
220                 detachAllEvt('common','locationChanged');
221                 detachAllEvt('common','loggedIn');
222                 attachEvt('common','loggedIn', function() { goTo(link + G.user.session); })
223                 initLogin();
224         };
225 }
226 /* --------------------------------------------------------------------------------- */
227
228