Close #595.
[platal.git] / htdocs / javascript / ajax.js
1 /***************************************************************************
2 * Copyright (C) 2003-2007 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 Ajax = {
22 xml_client: null,
23 init: false,
24
25 prepare_client: function()
26 {
27 if (!Ajax.init) {
28 if (window.XMLHttpRequest) {
29 Ajax.xml_client = new XMLHttpRequest();
30 } else if (window.ActiveXObject) {
31 try {
32 Ajax.xml_client = new ActiveXObject("Msxml2.XMLHTTP");
33 } catch (e) {
34 Ajax.xml_client = new ActiveXObject("Microsoft.XMLHTTP");
35 }
36 }
37 if (Ajax.xml_client == null) {
38 alert("Ton client ne supporte pas Ajax, nécessaire pour certaines fonctionalités de cette page");
39 }
40 }
41 Ajax.init = true;
42 },
43
44 update_html: function(obj, src, func)
45 {
46 Ajax.prepare_client();
47 if (Ajax.xml_client == null) {
48 return true;
49 }
50 Ajax.xml_client.abort();
51 Ajax.xml_client.onreadystatechange = function()
52 {
53 if(Ajax.xml_client.readyState == 4) {
54 if (Ajax.xml_client.status == 200) {
55 if (obj != null) {
56 document.getElementById(obj).innerHTML = Ajax.xml_client.responseText;
57 }
58 if (func != null) {
59 func(Ajax.xml_client.responseText);
60 }
61 } else if (Ajax.xml_client.status == 403) {
62 window.location.reload();
63 }
64 }
65 };
66 Ajax.xml_client.open ('GET', src, true);
67 Ajax.xml_client.send (null);
68 return false;
69 }
70 }
71
72 var currentTempMessage = 0;
73 function setOpacity(obj, opacity)
74 {
75 opacity = (opacity == 100)?99:opacity;
76 // IE
77 obj.style.filter = "alpha(opacity:"+opacity+")";
78 // Safari < 1.2, Konqueror
79 obj.style.KHTMLOpacity = opacity/100;
80 // Old Mozilla
81 obj.style.MozOpacity = opacity/100;
82 // Safari >= 1.2, Firefox and Mozilla, CSS3
83 obj.style.opacity = opacity/100
84 }
85
86 function _showTempMessage(id, state, back)
87 {
88 var obj = document.getElementById(id);
89 if (currentTempMessage != state) {
90 return;
91 }
92 setOpacity(obj, back * 5);
93 if (back > 0) {
94 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + "," + (back-1) + ")", 100);
95 } else {
96 obj.innerHTML = "";
97 }
98 }
99
100 function showTempMessage(id, message, success)
101 {
102 var obj = document.getElementById(id);
103 obj.innerHTML = (success ? "<img src='images/icons/wand.gif' alt='' /> "
104 : "<img src='images/icons/error.gif' alt='' /> ") + message;
105 obj.style.fontWeight = "bold";
106 obj.style.color = (success ? "green" : "red");;
107 currentTempMessage++;
108 setOpacity(obj, 100);
109 setTimeout("_showTempMessage('" + id + "', " + currentTempMessage + ", 20)", 700);
110 }
111
112 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: