Don't produce poisons when logged.
[platal.git] / htdocs / javascript / xorg.js
1 /***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
20
21 var is_IE = $.browser.msie;
22
23 // {{{ function getNow()
24 var days = ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi'];
25 var months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin', 'juillet',
26 'août', 'septembre', 'octobre', 'novembre', 'décembre']
27
28 function getNow() {
29 var dt = new Date();
30 var dy = dt.getDay();
31 var mh = dt.getMonth();
32 var wd = dt.getDate();
33 var yr = dt.getYear();
34 if (yr<1000) yr += 1900;
35 var hr = dt.getHours();
36 var mi = dt.getMinutes();
37 if (mi < 10) {
38 mi = '0' + mi;
39 }
40 var se = dt.getSeconds();
41 if (se < 10) {
42 se = '0' + se;
43 }
44
45 $(".date-heure").html(days[dy] + ' ' + wd + ' ' + months[mh] + ' ' + yr + '<br />'
46 + hr + ':' + mi + ':' + se);
47 }
48
49 // }}}
50 // {{{ Search Engine
51
52 function canAddSearchEngine()
53 {
54 if (((typeof window.sidebar == "object") && $.isFunction(window.sidebar.addSearchEngine))
55 || ((typeof window.external == "object") && $.isFunction(window.external.AddSearchProvider))) {
56 return true;
57 }
58 return false;
59 }
60
61 function addSearchEngine()
62 {
63 var searchURI = "http://www.polytechnique.org/xorg.opensearch.xml";
64 if ((typeof window.sidebar == "object") && $.isFunction(window.sidebar.addSearchEngine)) {
65 window.sidebar.addSearchEngine(
66 searchURI,
67 "http://www.polytechnique.org/images/xorg.png",
68 "Annuaire Polytechnique.org",
69 "Academic");
70 } else {
71 try {
72 window.external.AddSearchProvider(searchURI);
73 } catch(e) {
74 alert("Impossible d'installer la barre de recherche");
75 }
76 }
77 }
78
79 // }}}
80
81 /***************************************************************************
82 * POPUP THINGS
83 */
84
85 // {{{ function goodiesPopup()
86
87 var __goodies_active = true;
88
89 var __goodies_ical = {
90 default_title: 'Calendrier iCal',
91 sites: [
92 {'url_prefix': '',
93 'img': 'images/icons/calendar_view_day.gif',
94 'title': 'Calendrier iCal'},
95 {'url_prefix': 'http://www.google.com/calendar/render?cid=',
96 'img': 'images/goodies/add-google-calendar.gif',
97 'title': 'Ajouter à Google Calendar'},
98 {'url_prefix': 'https://www.google.com/calendar/hosted/polytechnique.org/render?cid=',
99 'img': 'images/goodies/add-google-calendar.gif',
100 'title': 'Ajouter à Google Apps / Calendar'}
101 ]
102 };
103
104 var __goodies_rss = {
105 default_title: 'Fils RSS',
106 sites: [
107 {'url_prefix': '',
108 'img': 'images/icons/feed.gif',
109 'title': 'Fil rss'},
110 {'url_prefix': 'http://fusion.google.com/add?feedurl=',
111 'img': 'images/goodies/add-google.gif',
112 'alt': 'Add to Google',
113 'title': 'Ajouter à iGoogle/Google Reader'},
114 {'url_prefix': 'http://www.netvibes.com/subscribe.php?url=',
115 'img': 'images/goodies/add-netvibes.gif',
116 'title': 'Ajouter à Netvibes'},
117 {'url_prefix': 'http://add.my.yahoo.com/content?.intl=fr&url=',
118 'img': 'images/goodies/add-yahoo.gif',
119 'alt': 'Add to My Yahoo!',
120 'title': 'Ajouter à My Yahoo!'}
121 ]
122 };
123
124 function disableGoodiesPopups() {
125 __goodies_active = false;
126 }
127
128 function goodiesPopup(node, goodies) {
129 var text = '<div style="text-align: center; line-height: 2.2">';
130 for (var site in goodies.sites) {
131 var entry = goodies.sites[site];
132 var s_alt = entry["alt"] ? entry["alt"] : "";
133 var s_img = entry["img"];
134 var s_title = entry["title"] ? entry["title"] : "";
135 var s_url = entry["url_prefix"].length > 0 ? entry["url_prefix"] + escape(this.href) : this.href;
136
137 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
138 }
139 text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>';
140
141 var title = node.title ? node.title : goodies.default_title;
142
143 $(node)
144 .mouseover(
145 function() {
146 if (__goodies_active) {
147 return overlib(text, CAPTION, title, CLOSETEXT, 'Fermer', DELAY, 800, STICKY, WIDTH, 150);
148 }
149 }
150 )
151 .mouseout(nd);
152 }
153
154 // }}}
155 // {{{ function auto_links()
156
157 function auto_links() {
158 var url = document.URL;
159 var fqdn = url.replace(/^https?:\/\/([^\/]*)\/.*$/,'$1');
160 var light = (url.indexOf('display=light') > url.indexOf('?'));
161 var resource_page = (url.indexOf('rss') > -1 || url.indexOf('ical') > -1);
162
163 $("a,link").each(function(i) {
164 var node = $(this);
165 var href = this.href;
166 if(!href || node.hasClass('xdx')
167 || href.indexOf('mailto:') > -1 || href.indexOf('javascript:') > -1) {
168 return;
169 }
170 if ((href.indexOf(fqdn) < 0 && this.className.indexOf('popup') < 0) || node.hasClass('popup')) {
171 node.click(function () {
172 window.open(href);
173 return false;
174 });
175 }
176 if (href.indexOf(fqdn) > -1 && light) {
177 href = href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/, "$1?display=light&$2$3");
178 this.href = href;
179 }
180 var rss = href.indexOf('rss') > -1;
181 var ical = href.indexOf('ical') > -1;
182 if (rss || ical) {
183 if (href.indexOf('http') < 0) {
184 href = 'http://' + fqdn + '/' + href;
185 }
186 }
187 if (this.nodeName.toLowerCase() == 'a' && !resource_page) {
188 if (rss && href.indexOf('prefs/rss') < 0 && (href.indexOf('xml') > -1 || href.indexOf('hash'))) {
189 goodiesPopup(this, __goodies_rss);
190 } else if (ical) {
191 goodiesPopup(this, __goodies_ical);
192 }
193 }
194 if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(this.className)) {
195 var w = matches[1], h = matches[2];
196 node.popWin(w, h);
197 }
198 });
199 $('.popup2').popWin(840, 600);
200 $('.popup3').popWin(640, 800);
201 }
202
203
204 // }}}
205
206 /***************************************************************************
207 * Password check
208 */
209
210 // {{{ function checkPassword
211
212 function getType(c) {
213 if (c >= 'a' && c <= 'z') {
214 return 1;
215 } else if (c >= 'A' && c <= 'Z') {
216 return 2;
217 } else if (c >= '0' && c <= '9') {
218 return 3;
219 } else {
220 return 4;
221 }
222 }
223
224 function differentTypes(password) {
225 var prev = 0;
226
227 for (i = 0 ; i < password.length ; ++i) {
228 var type = getType(password.charAt(i));
229 if (prev != 0 && prev != type) {
230 return true;
231 }
232 prev = type;
233 }
234 return false;
235 }
236
237 function passwordStrength(password) {
238 var prop = 0;
239 var prev = 0;
240 var firstType = true;
241 var types = Array(0, 0, 0, 0, 0);
242
243 for (i = 0 ; i < password.length ; ++i) {
244 var type = getType(password.charAt(i));
245 if (prev != 0 && prev != type) {
246 prop += 5;
247 firstType = false;
248 }
249 prop += i;
250 if (types[type] == 0 && !firstType) {
251 prop += 15;
252 }
253 types[type]++;
254 prev = type;
255 }
256 if (password.length < 6) {
257 prop *= 0.75;
258 }
259 if (firstType) {
260 prop *= 0.75;
261 }
262 if (prop > 100) {
263 prop = 100;
264 } else if (prop < 0) {
265 prop = 0;
266 }
267
268 return prop;
269 }
270
271 function checkPassword(box, okLabel) {
272 var password = box.value;
273 var prop = passwordStrength(password);
274
275 if (prop >= 60) {
276 color = "#4f4";
277 bgcolor = "#050";
278 ok = true;
279 } else if (prop >= 35) {
280 color = "#ff4";
281 bgcolor = "#750";
282 ok = true;
283 } else {
284 color = "#f20";
285 bgcolor = "#700";
286 ok = false;
287 }
288 $("#passwords_measure")
289 .stop()
290 .animate({ width: prop + "%",
291 backgroundColor: color
292 }, 750)
293 .parent().stop()
294 .animate({ backgroundColor: bgcolor }, 750);
295 var submitButton = $(":submit[name='" + passwordprompt_submit + "']");
296 if (ok && password.length >= 6 && differentTypes(password)) {
297 submitButton.attr("value", okLabel);
298 submitButton.removeAttr("disabled");
299 } else {
300 submitButton.attr("value", "Mot de passe trop faible");
301 submitButton.attr("disabled", "disabled");
302 }
303 }
304
305 // }}}
306 // {{{ send test email
307
308 function sendTestEmail(token, hruid)
309 {
310 var url = 'emails/test';
311 var msg = "Un email a été envoyé avec succès";
312 if (hruid != null) {
313 url += '/' + hruid;
314 msg += " sur l'adresse de " + hruid + ".";
315 } else {
316 msg += " sur ton addresse.";
317 }
318 $('#mail_sent').successMessage($url + '?token=' + token, msg);
319 return false;
320 }
321
322 // }}}
323
324
325 /***************************************************************************
326 * The real OnLoad
327 */
328
329 $(function() {
330 auto_links();
331 getNow();
332 setInterval(getNow, 1000);
333 $("#quick")
334 .focus(function() {
335 if ($(this).val() === 'Recherche dans l\'annuaire') {
336 $(this).val('');
337 }
338 $("#quick_button").show();
339 })
340 .blur(function() {
341 $("#quick_button").hide();
342 });
343 $("#quick_button").click(function() {
344 if ($("#quick").val() === 'Recherche dans l\'annuaire'
345 || $("#quick").val() === '') {
346 return false;
347 }
348 return true;
349 });
350 });
351
352 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: