Move header javascript to xorg.js.
[platal.git] / htdocs / javascript / xorg.js
1 /***************************************************************************
2 * Copyright (C) 2003-2010 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 // {{{ dynpost()
81
82 function dynpost(action, values)
83 {
84 var form = document.createElement('form');
85 form.action = action;
86 form.method = 'post';
87
88 $('body').get(0).appendChild(form);
89
90 for (var k in values) {
91 var input = document.createElement('input');
92 input.type = 'hidden';
93 input.name = k;
94 input.value = values[k];
95 form.appendChild(input);
96 }
97
98 form.submit();
99 }
100
101
102 function dynpostkv(action, k, v)
103 {
104 var dict = {};
105 dict[k] = v;
106 dynpost(action, dict);
107 }
108
109 // }}}
110 // {{{ function RegExp.escape()
111
112 RegExp.escape = function(text) {
113 if (!arguments.callee.sRE) {
114 var specials = [
115 '/', '.', '*', '+', '?', '|',
116 '(', ')', '[', ']', '{', '}',
117 '\\', '^' , '$'
118 ];
119 arguments.callee.sRE = new RegExp(
120 '(\\' + specials.join('|\\') + ')', 'g'
121 );
122 }
123 return text.replace(arguments.callee.sRE, '\\$1');
124 }
125
126 // }}}
127
128 /***************************************************************************
129 * POPUP THINGS
130 */
131
132 // {{{ function popWin()
133
134 function popWin(theNode, w, h) {
135 window.open(theNode.href, '_blank',
136 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+w+',height='+h);
137 return false;
138 }
139
140 // }}}
141 // {{{ function goodiesPopup()
142
143 var __goodies_active = true;
144
145 var __goodies_ical = {
146 default_title: 'Calendrier iCal',
147 sites: [
148 {'url_prefix': '',
149 'img': 'images/icons/calendar_view_day.gif',
150 'title': 'Calendrier iCal'},
151 {'url_prefix': 'http://www.google.com/calendar/render?cid=',
152 'img': 'images/goodies/add-google-calendar.gif',
153 'title': 'Ajouter à Google Calendar'},
154 {'url_prefix': 'https://www.google.com/calendar/hosted/polytechnique.org/render?cid=',
155 'img': 'images/goodies/add-google-calendar.gif',
156 'title': 'Ajouter à Google Apps / Calendar'}
157 ]
158 };
159
160 var __goodies_rss = {
161 default_title: 'Fils RSS',
162 sites: [
163 {'url_prefix': '',
164 'img': 'images/icons/feed.gif',
165 'title': 'Fil rss'},
166 {'url_prefix': 'http://fusion.google.com/add?feedurl=',
167 'img': 'images/goodies/add-google.gif',
168 'alt': 'Add to Google',
169 'title': 'Ajouter à iGoogle/Google Reader'},
170 {'url_prefix': 'http://www.netvibes.com/subscribe.php?url=',
171 'img': 'images/goodies/add-netvibes.gif',
172 'title': 'Ajouter à Netvibes'},
173 {'url_prefix': 'http://add.my.yahoo.com/content?.intl=fr&url=',
174 'img': 'images/goodies/add-yahoo.gif',
175 'alt': 'Add to My Yahoo!',
176 'title': 'Ajouter à My Yahoo!'}
177 ]
178 };
179
180 function disableGoodiesPopups() {
181 __goodies_active = false;
182 }
183
184 function goodiesPopup(node, goodies) {
185 var text = '<div style="text-align: center; line-height: 2.2">';
186 for (var site in goodies.sites) {
187 var entry = goodies.sites[site];
188 var s_alt = entry["alt"] ? entry["alt"] : "";
189 var s_img = entry["img"];
190 var s_title = entry["title"] ? entry["title"] : "";
191 var s_url = entry["url_prefix"].length > 0 ? entry["url_prefix"] + escape(this.href) : this.href;
192
193 text += '<a href="' + s_url + '"><img src="' + s_img + '" title="' + s_title + '" alt="' + s_alt + '"></a><br />';
194 }
195 text += '<a href="https://www.polytechnique.org/Xorg/Goodies">Plus de bonus</a> ...</div>';
196
197 var title = node.title ? node.title : goodies.default_title;
198
199 $(node)
200 .mouseover(
201 function() {
202 if (__goodies_active) {
203 return overlib(text, CAPTION, title, CLOSETEXT, 'Fermer', DELAY, 800, STICKY, WIDTH, 150);
204 }
205 }
206 )
207 .mouseout(nd);
208 }
209
210 // }}}
211 // {{{ function auto_links()
212
213 function auto_links() {
214 var url = document.URL;
215 var fqdn = url.replace(/^https?:\/\/([^\/]*)\/.*$/,'$1');
216 var light = (url.indexOf('display=light') > url.indexOf('?'));
217 var resource_page = (url.indexOf('rss') > -1 || url.indexOf('ical') > -1);
218
219 $("a,link").each(
220 function(i) {
221 node = $(this);
222 href = this.href;
223 if(!href || node.hasClass('xdx')
224 || href.indexOf('mailto:') > -1 || href.indexOf('javascript:') > -1) {
225 return;
226 }
227 if ((href.indexOf(fqdn) < 0 && this.className.indexOf('popup') < 0) || node.hasClass('popup')) {
228 node.click(function () { window.open(this.href); return false; });
229 }
230 if (href.indexOf(fqdn) > -1 && light) {
231 href = href.replace(/([^\#\?]*)\??([^\#]*)(\#.*)?/, "$1?display=light&$2$3");
232 this.href = href;
233 }
234 rss = href.indexOf('rss') > -1;
235 ical = href.indexOf('ical') > -1;
236 if (rss || ical) {
237 if (href.indexOf('http') < 0) {
238 href = 'http://' + fqdn + '/' + href;
239 }
240 }
241 if (this.nodeName.toLowerCase() == 'a' && !resource_page) {
242 if (rss && href.indexOf('prefs/rss') < 0 && (href.indexOf('xml') > -1 || href.indexOf('hash'))) {
243 goodiesPopup(this, __goodies_rss);
244 } else if (ical) {
245 goodiesPopup(this, __goodies_ical);
246 }
247 }
248 if(matches = (/^popup_([0-9]*)x([0-9]*)$/).exec(this.className)) {
249 var w = matches[1], h = matches[2];
250 node.click(function() { return popWin(this, w, h); });
251 }
252 }
253 );
254 $('.popup2').click(function() { return popWin(this, 840, 600); });
255 $('.popup3').click(function() { return popWin(this, 640, 800); });
256 }
257
258
259 // }}}
260
261
262 /***************************************************************************
263 * Password check
264 */
265
266 // {{{ function checkPassword
267
268 function getType(c) {
269 if (c >= 'a' && c <= 'z') {
270 return 1;
271 } else if (c >= 'A' && c <= 'Z') {
272 return 2;
273 } else if (c >= '0' && c <= '9') {
274 return 3;
275 } else {
276 return 4;
277 }
278 }
279
280 function differentTypes(password) {
281 var prev = 0;
282
283 for (i = 0 ; i < password.length ; ++i) {
284 var type = getType(password.charAt(i));
285 if (prev != 0 && prev != type) {
286 return true;
287 }
288 prev = type;
289 }
290 return false;
291 }
292
293 function passwordStrength(password) {
294 var prop = 0;
295 var prev = 0;
296 var firstType = true;
297 var types = Array(0, 0, 0, 0, 0);
298
299 for (i = 0 ; i < password.length ; ++i) {
300 var type = getType(password.charAt(i));
301 if (prev != 0 && prev != type) {
302 prop += 5;
303 firstType = false;
304 }
305 prop += i;
306 if (types[type] == 0 && !firstType) {
307 prop += 15;
308 }
309 types[type]++;
310 prev = type;
311 }
312 if (password.length < 6) {
313 prop *= 0.75;
314 }
315 if (firstType) {
316 prop *= 0.75;
317 }
318 if (prop > 100) {
319 prop = 100;
320 } else if (prop < 0) {
321 prop = 0;
322 }
323
324 return prop;
325 }
326
327 function checkPassword(box, okLabel) {
328 var password = box.value;
329 var prop = passwordStrength(password);
330
331 if (prop >= 60) {
332 color = "#4f4";
333 bgcolor = "#050";
334 ok = true;
335 } else if (prop >= 35) {
336 color = "#ff4";
337 bgcolor = "#750";
338 ok = true;
339 } else {
340 color = "#f20";
341 bgcolor = "#700";
342 ok = false;
343 }
344 $("#passwords_measure")
345 .stop()
346 .animate({ width: prop + "%",
347 backgroundColor: color
348 }, 750)
349 .parent().stop()
350 .animate({ backgroundColor: bgcolor }, 750);
351 var submitButton = $(":submit[name='" + passwordprompt_submit + "']");
352 if (ok && password.length >= 6 && differentTypes(password)) {
353 submitButton.attr("value", okLabel);
354 submitButton.removeAttr("disabled");
355 } else {
356 submitButton.attr("value", "Mot de passe trop faible");
357 submitButton.attr("disabled", "disabled");
358 }
359 }
360
361 // }}}
362
363
364 /***************************************************************************
365 * The real OnLoad
366 */
367
368 $(document).ready(function() {
369 auto_links();
370 getNow();
371 setInterval(getNow, 1000);
372 $("#quick")
373 .focus(function() {
374 if ($(this).val() === 'Recherche dans l\'annuaire') {
375 $(this).val('');
376 }
377 $("#quick_button").show();
378 })
379 .blur(function() {
380 $("#quick_button").hide();
381 });
382 $("#quick_button").click(function() {
383 if ($("#quick").val() === 'Recherche dans l\'annuaire'
384 || $("#quick").val() === '') {
385 return false;
386 }
387 return true;
388 });
389 });
390
391 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: