1 | //Trobler class
|
---|
2 | function Trobbler(div, active, inactive) {
|
---|
3 | var me = this;
|
---|
4 |
|
---|
5 | me.activate = function() {
|
---|
6 | var aux = document.getElementById(div);
|
---|
7 | aux.setAttribute("class", active);
|
---|
8 | aux.setAttribute("className", active); //IE sucks
|
---|
9 | }
|
---|
10 |
|
---|
11 | me.close = function() {
|
---|
12 | var aux = document.getElementById(div);
|
---|
13 | aux.setAttribute("class", inactive);
|
---|
14 | aux.setAttribute("className", inactive); //IE sucks
|
---|
15 | }
|
---|
16 |
|
---|
17 | }
|
---|
18 |
|
---|
19 | // Read the answer of an URL and write the contents into a 'div'
|
---|
20 |
|
---|
21 | function minimax(url, div) {
|
---|
22 | var me = this;
|
---|
23 |
|
---|
24 | var semaphore = false;
|
---|
25 | var haveSemaphore = false;
|
---|
26 |
|
---|
27 | var trobbler=false;
|
---|
28 | var haveTrobbler=false;
|
---|
29 |
|
---|
30 | me.xhr = false;
|
---|
31 |
|
---|
32 | if (window.XMLHttpRequest) { // Si es Mozilla, Safari etc
|
---|
33 | me.xhr = new XMLHttpRequest()
|
---|
34 | } else {
|
---|
35 | if (window.ActiveXObject) { // pero si es IE
|
---|
36 | try {
|
---|
37 | me.xhr = new ActiveXObject('Microsoft.XMLHTTP')
|
---|
38 | } catch (e) { // en caso que sea una versión antigua
|
---|
39 | try {
|
---|
40 | me.xhr = new ActiveXObject('Msxml2.XMLHTTP')
|
---|
41 | } catch (e){}
|
---|
42 | }
|
---|
43 | } else {
|
---|
44 | return false
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | me.setDiv = function(div_x) {
|
---|
49 | div=div_x;
|
---|
50 | }
|
---|
51 |
|
---|
52 | me.setSemaphore = function(sname) {
|
---|
53 | semaphore=document.getElementById(sname);
|
---|
54 | haveSemaphore = true;
|
---|
55 | if(semaphore.value=='') {
|
---|
56 | semaphore.value='1';
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | me.setTrobbler = function(tdiv,on,off) {
|
---|
61 | trobbler=new Trobbler(tdiv,on,off);
|
---|
62 | haveTrobbler = true;
|
---|
63 | }
|
---|
64 |
|
---|
65 | me.closeTrobbler = function() {
|
---|
66 | if(haveTrobbler) trobbler.close();
|
---|
67 | }
|
---|
68 |
|
---|
69 | me.activateTrobbler = function() {
|
---|
70 | if(haveTrobbler) trobbler.activate();
|
---|
71 | }
|
---|
72 |
|
---|
73 | me.xhr.onreadystatechange = function() {
|
---|
74 | if (me.xhr.readyState == 4 && me.xhr.status==200 ) {
|
---|
75 | // var doc=me.xhr.responseXML;
|
---|
76 | // var body = doc.getElementsByTagName('html')[0];
|
---|
77 | // alert(body.childNodes[3].textContent);
|
---|
78 | // document.getElementById(div).innerHTML=body.childNodes[3].textContent;
|
---|
79 | var text=me.xhr.responseText;
|
---|
80 | if(haveSemaphore) semaphore.value='1';
|
---|
81 | if(haveTrobbler) trobbler.close();
|
---|
82 | if(div) document.getElementById(div).innerHTML=text;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | me.get = function () {
|
---|
87 | if(haveTrobbler) trobbler.activate();
|
---|
88 | //If the semaphore is in 0, request another get in 1 second,
|
---|
89 | //for we are doing something and the result from the other
|
---|
90 | //process would be older than ours
|
---|
91 | if(!haveSemaphore || semaphore.value=='1') {
|
---|
92 | /* Esto es insultante, pero Explorer solo funciona en POST */
|
---|
93 | if(haveSemaphore) semaphore.value='0';
|
---|
94 | me.xhr.open('POST', url, true);
|
---|
95 | //me.xhr.overrideMimeType('text/xml');
|
---|
96 | me.xhr.send(null);
|
---|
97 | } else {
|
---|
98 | setTimeout(function (){ me.get(); }, 300);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | me.post = function (post_x) {
|
---|
103 | if(haveTrobbler) trobbler.activate();
|
---|
104 | //If the semaphore is in 0, request another post in 1 second,
|
---|
105 | //for we are sending something and the result from the other
|
---|
106 | //process would be older than ours
|
---|
107 | post_x=post_x.replace(/\&/g, '&');
|
---|
108 | if(!haveSemaphore || semaphore.value=='1') {
|
---|
109 | if(haveSemaphore) semaphore.value='0';
|
---|
110 | me.xhr.open('POST', url);
|
---|
111 | //me.xhr.overrideMimeType('text/xml');
|
---|
112 | me.xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
---|
113 | me.xhr.send(post_x);
|
---|
114 | } else {
|
---|
115 | setTimeout(function (){ me.post(post_x); }, 300);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | //return me.xhr;
|
---|
120 |
|
---|
121 | }
|
---|