* Adding getXML(request) to fix missing request.responseXML in IE
[enigma2-plugins.git] / webinterface / src / web-data / tools.js
1 var url_getvolume = '/web/vol?set=info'; 
2 var url_setvolume = '/web/vol?set=set'; // plus new value eq. set=set15
3 var url_volumeup = '/web/vol?set=up';
4 var url_volumedown = '/web/vol?set=down';
5 var url_volumemute = '/web/vol?set=mute';
6
7 var url_epgservice = "/web/epgservice?ref="; // plus serviceRev
8 var url_epgsearch = "/web/epgsearch?search="; // plus serviceRev
9 var url_epgnownext = "/web/epgnownext?ref="; // plus serviceRev
10
11 function getHTTPObject( ){
12     var xmlHttp = false;
13             
14     // try to create a new instance of the xmlhttprequest object        
15     try{
16         // Internet Explorer
17         if( window.ActiveXObject ){
18             for( var i = 5; i; i-- ){
19                 try{
20                     // loading of a newer version of msxml dll (msxml3 - msxml5) failed
21                     // use fallback solution
22                     // old style msxml version independent, deprecated
23                     if( i == 2 ){
24                         xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" );    
25                     }
26                     // try to use the latest msxml dll
27                     else{
28                         
29                         xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP." + i + ".0" );
30                     }
31                     break;
32                 }
33                 catch( excNotLoadable ){                        
34                     xmlHttp = false;
35                 }
36             }
37         }
38         // Mozilla, Opera und Safari
39         else if( window.XMLHttpRequest ){
40             xmlHttp = new XMLHttpRequest();
41         }
42     }
43     // loading of xmlhttp object failed
44     catch( excNotLoadable ){
45         xmlHttp = false;
46     }
47     return xmlHttp ;
48 }
49
50
51 function debug(text){
52 //      document.getElementById('BodyContentDebugbox').innerHTML += "DEBUG: "+text+"<br>";
53 }
54
55 function showhide(id){
56         o = document.getElementById(id).style;
57         o.display = (o.display!="none")? "none":"";
58 }
59
60 function set(what, value){
61         //debug(what+"-"+value);
62         element = document.getElementById(what);
63         element.innerHTML = value;
64         //$('scriptzone').innerHTML = ""; // deleting set() from page, to keep the page short and to save memory
65 }
66
67 function getXML(request){
68         if (document.implementation && document.implementation.createDocument){
69                 debug("using responseXML");
70                 var xmlDoc = request.responseXML
71         }
72         else if (window.ActiveXObject){
73                 debug("Creating XML for IE");
74                 var xmlInsert = document.createElement('xml');
75
76                 xmlInsert.setAttribute('innerHTML',request.responseText);
77                 xmlInsert.setAttribute('id','_MakeAUniqueID');
78                 document.body.appendChild(xmlInsert);
79                 xmlDoc = document.getElementById('_MakeAUniqueID');
80                 document.body.removeChild(document.getElementById('_MakeAUniqueID'));
81         } else {
82                 debug("Your Browser Sucks!");
83         }
84         return xmlDoc;
85 }
86
87 function zap(li){
88         var request = getHTTPObject();
89         var url = "/web/zap?ZapTo=" + escape(li.id);
90         //debug("requesting "+url);
91         new Ajax.Request( url,
92                         {
93                                 method: 'get' 
94                                 
95                         });
96                 
97 }
98
99 function openWindow(t, inner, w, h){
100                         id = new Date().toUTCString()
101                         
102                         //debug(id);
103                         var win = new Window(id, {className: "alphacube", title: t, width:w, height:h});
104                         win.getContent().innerHTML = inner;
105                         win.setDestroyOnClose();
106                         win.showCenter();
107                         debug("opening Window: "+t);
108 }
109
110 function messageBox(t, m){
111         Dialog.alert(m, {windowParameters: {title: t, className: "alphacube", width:200}, okLabel: "Close"});
112 }
113 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
114 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
115 //++++ EPG functions                               ++++
116 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
117 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
118 var EPGList = Class.create();
119 EPGList.prototype = {
120         //contructor
121         initialize: function(){
122                 debug("init class EPGList");
123         },
124         getBySearchString: function(string){
125                 debug("requesting "+ url_epgsearch+string);
126                 new Ajax.Request( url_epgsearch+string,
127                         {
128                                 method: 'get', 
129                                 onComplete: this.incomingEPGrequest
130                         });
131                 
132         },
133         getByServiceReference: function(serviceRef){
134                 new Ajax.Request(url_epgservice+serviceRef,
135                         {
136                                 method: 'get', 
137                                 onComplete: this.incomingEPGrequest
138                         });
139                 
140         },
141         renderTable: function(epglist){
142                 debug("rendering Table with "+epglist.length+" events");
143                 var html="<table width='100%'>";
144                 for (var i=0; i < epglist.length; i++){
145                         try{
146                                 var item = epglist[i];
147                                 html +="<tr  bgcolor='gray'>";
148                                 html +="<td>"+item.getTimeDay()+"</td>";
149                                 html +="<td>"+item.getTimeStartString()+"</td>";
150                                 html +="<td>"+item.getTimeEndString()+"</td>";
151                                 html +="<td>"+item.getTitle()+"</td>";
152                                 html +="<td>"+item.getDescription()+"</td>";
153                                 html +="<td>"+item.getDescriptionExtended()+"</td>";
154                                 html +="<td>"+(item.getDuration()/60000)+" min.</td>";
155                                 html +="<td>"+item.getServiceName()+"</td>";
156                                 html +="</tr>";
157                         } catch (blubb) {
158                                 //debug("Error rendering: "+blubb);
159                         }
160                 }
161                 
162                 html +="</table>";
163                 //element.innerHTML = html;
164                 openWindow("Electronic Program Guide", html, 900, 500);
165                 
166         },
167         incomingEPGrequest: function(request){
168                 debug("incoming request" +request.readyState);          
169                 if (request.readyState == 4)
170                 {
171                         var EPGItems = getXML(request).getElementsByTagName("e2eventlist").item(0).getElementsByTagName("e2event");
172                         
173                         debug("have "+EPGItems.length+" e2events");
174                         if(EPGItems.length > 0){
175                         
176                                 epglist = new Array();
177                                 for(var i=0; i < EPGItems.length; i++){         
178                                         epglist.push(new EPGEvent(EPGItems.item(i)));
179                                 }
180                                 debug("Calling prototype.renderTable(epglist)");
181                                 EPGList.prototype.renderTable(epglist);
182                                 
183                         } else {
184                                 messageBox('No Items found!', 'Sorry but i could not find any EPG Content containing your search value');
185                         }
186                         
187                 }
188         }
189         
190 }
191
192 function EPGEvent(element){     
193         // parsing values from xml-element
194         try{
195                 this.eventID= element.getElementsByTagName('e2eventid').item(0).firstChild.data;
196                 this.startTime= element.getElementsByTagName('e2eventstart').item(0).firstChild.data;
197                 this.duration= element.getElementsByTagName('e2eventduration').item(0).firstChild.data;
198                 this.title= element.getElementsByTagName('e2eventtitle').item(0).firstChild.data;
199                 this.description= element.getElementsByTagName('e2eventdescription').item(0).firstChild.data;
200                 this.descriptionE= element.getElementsByTagName('e2eventdescriptionextended').item(0).firstChild.data;
201                 this.serviceRef= element.getElementsByTagName('e2eventservicereference').item(0).firstChild.data;
202                 this.serviceName= element.getElementsByTagName('e2eventservicename').item(0).firstChild.data;
203         } catch (bullshit) {
204                 //debug("Bullshit is:"+bullshit);
205         }
206         this.getEventId = function ()
207         {
208                 return this.eventID;
209         }
210         this.getTimeStart = function ()
211         {
212                 var date = new Date(parseInt(this.startTime)*1000);
213                 return date;
214         }
215         this.getTimeStartString = function ()
216         {
217                 var h = this.getTimeStart().getHours();
218                 var m = this.getTimeStart().getMinutes();
219                 if (m < 10){
220                         m="0"+m;
221                 }
222                 return h+":"+m;
223         }
224         this.getTimeDay = function ()
225         {
226                 var Wochentag = new Array("So", "Mo", "Di", "Mi", "Do", "Fr", "Sa");
227                 var wday = Wochentag[this.getTimeStart().getDay()];
228                 var day = this.getTimeStart().getDate();
229                 var month = this.getTimeStart().getMonth()+1;
230                 var year = this.getTimeStart().getFullYear();
231                 
232                 return wday+".&nbsp;"+day+"."+month+"."+year;
233         }
234         this.getTimeEnd = function ()
235         {
236                 var date = new Date((parseInt(this.startTime)+parseInt(this.duration))*1000);
237                 return date;
238         }
239         this.getTimeEndString = function ()
240         {
241                 var h = this.getTimeEnd().getHours();
242                 var m = this.getTimeEnd().getMinutes();
243                 if (m < 10){
244                         m="0"+m;
245                 }
246                 return h+":"+m;
247         }
248         this.getDuration = function ()
249         {
250                 return  new Date(parseInt(this.duration)*1000);
251         }
252         this.getTitle = function ()
253         {
254                 return this.title;
255         }
256         this.getDescription = function ()
257         {
258                 return this.description;
259         }
260         this.getDescriptionExtended = function ()
261         {
262                 return this.descriptionE;
263         }
264         this.getServiceReference = function ()
265         {
266                 return this.serviceRef;
267         }
268         this.getServiceName = function ()
269         {
270                 return this.serviceName.replace(" ","&nbsp;");
271         }
272 }//END class EPGEvent
273
274 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
275 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
276 //++++ GUI functions                               ++++
277 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
278 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
279 var currentBodyMainElement = null
280
281 function setBodyMainContent(newelementname){
282         newelement =document.getElementById(newelementname);
283         if(currentBodyMainElement != null){
284                 currentBodyMainElement.style.display = "none";
285                 
286         }
287         newelement.style.display = "";
288         currentBodyMainElement = newelement;
289 }
290 function doRequest(url,readyFunction){
291         
292         http_request =getHTTPObject();
293         if(readyFunction){
294                 http_request.onreadystatechange = readyFunction;
295         }
296         http_request.open('GET', url, true);
297         http_request.send(null);
298         return http_request;
299 }
300 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
301 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
302 //++++ volume functions                            ++++
303 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
304 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
305
306
307 function getVolume()
308 {
309         doRequest(url_getvolume,handleVolumeRequest);
310 }
311 function setVolume(newvalue)
312 {
313                 doRequest(url_setvolume+newvalue,handleVolumeRequest);
314 }
315 function volumeUp()
316 {
317                 doRequest(url_volumeup,handleVolumeRequest);
318 }
319 function volumeDown()
320 {
321                 doRequest(url_volumedown,handleVolumeRequest);  
322 }
323 function volumeMute()
324 {
325                 doRequest(url_volumemute,handleVolumeRequest);
326 }
327 function handleVolumeRequest(){
328         if (http_request.readyState == 4) {
329                 var a = http_request.responseText.split("\n");
330                 var newvalue = a[1];
331                 var mute=a[2];
332                 set("Volume_Current",newvalue);
333                 for (var i = 1; i <= 10; i++)
334                 {
335                         if ( (newvalue/10)>=i){
336                                 $("volume"+i).src = "/webdata/gfx/led_on.png";
337                         }else{
338                                 $("volume"+i).src = "/webdata/gfx/led_off.png";
339                         }
340                 }
341                 if (mute == "notmuted"){
342                         $("speaker").src = "/webdata/gfx/speak_on.png";
343                 }else{
344                         $("speaker").src = "/webdata/gfx/speak_off.png";
345                 }
346         }       
347 }
348 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
349 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
350 //++++ Bouqet managing functions                   ++++
351 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
352 //+++++++++++++++++++++++++++++++++++++++++++++++++++++
353 function loadRootTVBouqet(){
354         //rootB = '/web/fetchchannels?ServiceListBrowse='+escape('1:7:1:0:0:0:0:0:0:0:(type == 1) FROM BOUQUET "bouquets.tv" ORDER BY bouquet');
355         rootB = '/web/fetchchannels?ServiceListBrowse='+escape('1:7:1:0:0:0:0:0:0:0:(type == 1) || (type == 17) || (type == 195) || (type == 25)  FROM BOUQUET &quot;bouquets.tv&quot; ORDER BY bouquet');
356
357         loadRootBouqet(rootB);
358 }
359 function loadRootBouqet(rootB){
360         
361         t = getHTTPObject();
362         t.onreadystatechange = incomingResult;
363         t.open('GET', rootB, true);
364         t.send(null);
365         
366 }
367
368 function incomingResult(){
369         if((t.readyState == 4) && (t.status == 200)) {
370         // perfekt!
371                 
372                 var b = getXML(t).getElementsByTagName("e2servicelist").item(0).getElementsByTagName("e2service");
373
374                 bouquets = new Array();
375                 for ( var i=0; i < b.length; i++){
376                         bRef = b.item(i).getElementsByTagName('e2servicereference').item(0).firstChild.data;
377                         bName = b.item(i).getElementsByTagName('e2servicename').item(0).firstChild.data;
378                         
379                         bu = new Array(bName,bRef);
380                         bouquets.push(bu)
381                 }
382                 refreshSelect(bouquets);
383         } else {
384         // die Anfrage enthielt Fehler;
385         // die Antwort war z.B. 404 (nicht gefunden)
386         // oder 500 (interner Server-Fehler)
387         }
388         
389 }
390
391 // to add the bouqetts to the list
392 function refreshSelect(arraybouqet){
393         
394         sel = document.getElementById("accordionMenueBouqetContent");
395         // options neu eintragen
396         html = "<table>";
397                 for ( var i = 0 ; i < arraybouqet.length ; i++ ){
398                 if(arraybouqet[i][0] && arraybouqet[i][1]){
399                         html+="<tr><td>";
400                         html+="<a  onclick=\"bouqetSelected(this); setBodyMainContent('BodyContentChannellist');\" id='";
401                         html+= arraybouqet[i][1];
402                         html+="'>";
403                         html+= arraybouqet[i][0];
404                         html+="</a>";
405                         html+="</td></tr>";
406                 }
407         }
408         html+="</table>";
409         sel.innerHTML=html;
410         refreshChannellist(arraybouqet[0][0],arraybouqet[0][1]);
411 }
412
413 //++++++++++++++++++++++
414 function bouqetSelected(element){
415         refreshChannellist(element.value,element.id)
416 }
417
418 function refreshChannellist(bname,bref){
419         urlx = '/web/fetchchannels?ServiceListBrowse='+escape(bref);
420         w =getHTTPObject();
421         w.onreadystatechange = incomingChannellist;
422         w.open('GET', urlx, true);
423         w.send(null);   
424 }
425                         
426 function incomingChannellist(){
427         if(w.readyState == 4){
428
429                 services = getXML(w).getElementsByTagName("e2servicelist").item(0).getElementsByTagName("e2service");
430                 
431                 listerHtml      = '<table border="0" cellpadding="0" cellspacing="0" class="BodyContentChannellist">\n';
432                 listerHtml += '<thead class="fixedHeader">\n';
433                 listerHtml += '<tr>\n';
434                 listerHtml += '<th colspan="2" style="color: #FFFFFF;">ServiceList</th>\n';
435                 listerHtml += '<th style="text-align: right;" style="color: #FFFFFF;">'
436                 listerHtml += '<form onSubmit="new EPGList().getBySearchString(document.getElementById(\'searchText\').value); return false;">';
437                 listerHtml += '<input type="text" id="searchText" onfocus="this.value=\'\'" value="EPG suchen"/>';
438                 listerHtml += '</form>'
439                 listerHtml += '</tr>\n';
440                 listerHtml += '</thead>\n';
441                 listerHtml += '<tbody class="scrollContent">\n';
442                 
443                 debug("got "+services.length+" Services");
444                 
445                 for ( var i = 0; i < (services.length ); i++){
446                         sRef = services.item(i).getElementsByTagName('e2servicereference').item(0).firstChild.data;
447                         sName = services.item(i).getElementsByTagName('e2servicename').item(0).firstChild.data;
448
449                         listerHtml += '<tr><td onclick="zap(this)" id="';
450                         listerHtml += sRef;
451                         listerHtml += '">\n';
452                         listerHtml += sName;
453                         listerHtml += '</td>\n';
454                         listerHtml += '<td onclick="new EPGList().getByServiceReference(this.id);" id="';
455                         listerHtml += sRef;
456                         listerHtml += '" >EPG</td>\n';
457                         listerHtml += '<td><a target="blank" href="stream.m3u?ref=';
458                         listerHtml += sRef;
459                         listerHtml += '\">Stream</a></td>\n';
460                         listerHtml += '</tr>\n';
461                 }
462                 
463                 listerHtml += "</tbody></table>\n";
464                 document.getElementById('BodyContentChannellist').innerHTML = listerHtml;
465                 setBodyMainContent('BodyContentChannellist');
466         }
467 }
468
469
470
471
472