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