integrated AutoTime partly. it's not visible for now.
[enigma2-plugins.git] / webinterface / src / web-data / timer.js
1 // $Header$
2
3 // TimerEdit variables:
4 var addTimerEditFormObject = new Object();
5 addTimerEditFormObject["TVListFilled"] = 0;
6 addTimerEditFormObject["RadioListFilled"] = 0;
7 addTimerEditFormObject["deleteOldOnSave"] = 0;
8 addTimerEditFormObject["eventID"] = 0;
9 days = [];
10 days[0] = 'mo';
11 days[1] = 'tu';
12 days[2] = 'we';
13 days[3] = 'th';
14 days[4] = 'fr';
15 days[5] = 'sa';
16 days[6] = 'su';
17
18 // Timer
19 function addTimerByID(sRef,eventID,justplay){
20         if(parentPin(sRef)) {
21                 debug("addTimerByID\neventID: "+eventID);
22                 doRequest(url_timeraddbyeventid+"?sRef="+sRef+"&eventid="+eventID+"&justplay="+justplay, incomingTimerAddResult, false);        
23         }
24 }
25 function incomingTimerAddResult(request){
26         debug("onTimerAdded");
27         if(request.readyState == 4){
28                 var addresult = new SimpleXMLResult(getXML(request));
29                 if(addresult.getState()){
30                         //timer was add
31                         loadTimerList();
32                 }else{
33                         messageBox("Timer Error","your Timer could not be added!\nReason: "+addresult.getStateText());
34                 }
35         }               
36 }
37 function loadTimerList(){
38         doRequest(url_timerlist, incomingTimerList, false);     
39 }
40
41 function incomingTimerList(request){
42         if(request.readyState == 4){
43                 var timers = new TimerList(getXML(request)).getArray();
44                 debug("have "+timers.length+" timer");
45                 var ns_tplTimerListHeader = {
46                         'page_title': 'Timerlist',
47                         'name1': 'Channel',
48                         'name2': 'Name',
49                         'repeated': 'Repeated',
50                         'duration': 'Time running',
51                         'start': 'Start time',
52                         'end': 'End time',
53                         'event': 'Event',
54                         'after_event': 'After event',
55                         'options': 'Options'                    
56                 };
57                 listerHtml = RND(tplTimerListHeader, ns_tplTimerListHeader);
58                 var aftereventReadable = new Array ('Nothing', 'Standby', 'Deepstandby/Shutdown');
59                 var justplayReadable = new Array('record', 'zap');
60                 for ( var i = 0; i <timers.length; i++){
61                         var timer = timers[i];
62                         var beginDate = new Date(Number(timer.getTimeBegin())*1000);
63                         var endDate = new Date(Number(timer.getTimeEnd())*1000);
64                         var namespace = {       
65                                 'servicereference': timer.getServiceReference(),
66                                 'servicename': quotes2html(timer.getServiceName()),
67                                 'title': quotes2html(timer.getName()),
68                                 'description': quotes2html(timer.getDescription()),
69                                 'descriptionextended': quotes2html(timer.getDescriptionExtended()),
70                                 'begin': timer.getTimeBegin(),
71                                 'beginDate': beginDate.toLocaleString(),
72                                 'end': timer.getTimeEnd(),
73                                 'endDate': endDate.toLocaleString(),
74                                 'state': timer.getState(),
75                                 'duration': Math.ceil((timer.getDuration()/60)),
76                                 'repeated': timer.getRepeated(),
77                                 'repeatedReadable': repeatedReadable(timer.getRepeated()),
78                                 'justplay': timer.getJustplay(),
79                                 'justplayReadable': justplayReadable[Number(timer.getJustplay())],
80                                 'afterevent': timer.getAfterevent(),
81                                 'aftereventReadable': aftereventReadable[Number(timer.getAfterevent())],
82                                 'disabled': timer.getDisabled(),
83                                 'onOff': timer.getToggleDisabledIMG(),
84                                 'color': timer.getColor(),
85                                 'delTimer_FUNCTION': "delTimer(\'"+timer.getServiceReference()+"\',\'"+timer.getTimeBegin()
86                                         +"\',\'"+timer.getTimeEnd()+"\',\'"+quotes2html(timer.getServiceName())
87                                         +"\',\'"+quotes2html(timer.getName())+"\',\'"+quotes2html(timer.getDescription())
88                                         +"\',incomingTimerDelResult)",
89                                 'sendToggleTimerDisable_FUNCTION': "sendToggleTimerDisable(\'"+timer.getJustplay()+"\',\'"
90                                         +timer.getTimeBegin()+"\',\'"+timer.getTimeEnd()+"\',\'"+timer.getRepeated()
91                                         +"\',\'"+timer.getServiceReference()+"\',\'"+quotes2html(timer.getName())
92                                         +"\',\'"+quotes2html(timer.getDescription())+"\',\'"+timer.getAfterevent()
93                                         +"\',\'"+timer.getDisabled()+"\')",
94                                 'loadTimerFormSeconds_FUNCTION': "loadTimerFormSeconds(\'"+timer.getJustplay()+"\',\'"
95                                         +timer.getTimeBegin()+"\',\'"+timer.getTimeEnd()+"\',\'"+timer.getRepeated()
96                                         +"\',\'"+timer.getServiceReference()+"\',\'"+quotes2html(timer.getName())
97                                         +"\',\'"+quotes2html(timer.getDescription())+"\',\'"+timer.getAfterevent()+"\',1)"
98                         };
99                         listerHtml += RND(tplTimerListItem, namespace);
100                 }
101                 listerHtml += tplTimerListFooter;
102                 $('BodyContent').innerHTML = listerHtml;
103                 setBodyMainContent('BodyContent');
104         }
105 }
106 function repeatedReadable(num) {
107         num = Number(num);
108         if(num == 0) {
109                 return "One Time";
110         }
111         
112         var html = "";
113         var Repeated = new Object();
114         Repeated["Mo-Su"] =127;
115         Repeated["Su"] =    64;
116         Repeated["Sa"] =    32;
117         Repeated["Mo-Fr"] = 31;
118         Repeated["Fr"] =    16;
119         Repeated["Th"] =     8;
120         Repeated["We"] =     4;
121         Repeated["Tu"] =     2;
122         Repeated["Mo"] =     1;
123         
124         for(rep in Repeated) {
125                 if(rep.toString() != "extend") {
126                         var check = Number(Repeated[rep]);
127                         if(check <= num) {
128                                 num -= check;
129                                 if(html == "") {
130                                         html += rep.toString();
131                                 } else {
132                                         html = rep.toString()+","+html;
133                                 }
134                         }
135                 }
136         }
137         return html;
138 }
139
140 function colorTimerListEntry (state) {
141         if (state == 0) {
142                 return "000000";
143         } else if(state == 1) {
144                 return "00BCBC";
145         } else if(state == 2) {
146                 return "9F1919";
147         } else {
148                 return "00BCBC";
149         }
150 }
151 function delTimer(sRef,begin,end,servicename,title,description,readyFunction){
152         debug("delTimer: sRef("+sRef+"),begin("+begin+"),end("+end+"),servicename("+servicename+"),title("+title+"),description("+description+")");
153         Dialog.confirm(
154                 "Selected timer:<br>"
155                 +"Channel: "+servicename+"<br>"
156                 +"Title: "+title+"<br>"
157                 +"Description: "+description+"<br>"
158                 +"Are you sure that you want to delete the Timer?",
159                  {windowParameters: {width:300, className: windowStyle},
160                         okLabel: "delete",
161                         buttonClass: "myButtonClass",
162                         cancel: function(win) {debug("delTimer cancel confirm panel")},
163                         ok: function(win) { 
164                                                             debug("delTimer ok confirm panel"); 
165                                                             doRequest(url_timerdelete+"?sRef="+sRef+"&begin="+begin+"&end="+end, readyFunction, false);
166                                                             return true;
167                                                           }
168                         }
169         );
170 }
171
172 function incomingTimerDelResult(request){
173         debug("onTimerDeleted");
174         if(request.readyState == 4){
175                 var delresult = new SimpleXMLResult(getXML(request));
176                 debug("Lade liste");
177                 loadTimerList();
178         }               
179 }
180 function loadTimerFormNow() {
181         var now = new Date();
182         addTimerEditFormObject["syear"] = now.getFullYear();
183         addTimerEditFormObject["smonth"] = now.getMonth() + 1;
184         addTimerEditFormObject["sday"] = now.getDate();
185         addTimerEditFormObject["shour"] = now.getHours();
186         addTimerEditFormObject["smin"] = now.getMinutes();
187
188         var     plusTwoHours = new Date(now.getTime() + ((120 *60)*1000) );
189         addTimerEditFormObject["eyear"] = plusTwoHours.getFullYear();
190         addTimerEditFormObject["emonth"] = plusTwoHours.getMonth() + 1;
191         addTimerEditFormObject["eday"] = plusTwoHours.getDate();
192         addTimerEditFormObject["ehour"] = plusTwoHours.getHours();
193         addTimerEditFormObject["emin"] = plusTwoHours.getMinutes();
194                 
195         addTimerEditFormObject["justplay"] = "record";
196         addTimerEditFormObject["channel"] = "";
197         addTimerEditFormObject["channelName"] = "";
198         addTimerEditFormObject["channelSort"] = "tv";
199         addTimerEditFormObject["name"] = "";
200         addTimerEditFormObject["description"] = "";
201         addTimerEditFormObject["repeated"] = 0;
202         addTimerEditFormObject["afterEvent"] = "0";
203         addTimerEditFormObject["deleteOldOnSave"] = 0;
204         
205         addTimerEditFormObject["beginOld"] = 0;
206         addTimerEditFormObject["endOld"] = 0;
207         
208         
209         debug("loadTimerFormNow 2");
210         loadTimerFormChannels();
211 }
212
213 function loadTimerFormSeconds(justplay,begin,end,repeated,channel,channelName,name,description,afterEvent,deleteOldOnSave,eit) {
214         debug('justplay:'+justplay+' begin:'+begin+' end:'+end+' repeated:'+repeated+' channel:'+channel+' name:'+name+' description:'+description+' afterEvent:'+afterEvent+' deleteOldOnSave:'+deleteOldOnSave);
215         var start = new Date(Number(begin)*1000);
216         addTimerEditFormObject["syear"] = start.getFullYear();
217         addTimerEditFormObject["smonth"] = start.getMonth() + 1;
218         addTimerEditFormObject["sday"] = start.getDate();
219         addTimerEditFormObject["shour"] = start.getHours();
220         addTimerEditFormObject["smin"] = start.getMinutes();
221         
222         var     stopp = new Date(Number(end)*1000);
223         addTimerEditFormObject["eyear"] = stopp.getFullYear();
224         addTimerEditFormObject["emonth"] = stopp.getMonth() + 1;
225         addTimerEditFormObject["eday"] = stopp.getDate();
226         addTimerEditFormObject["ehour"] = stopp.getHours();
227         addTimerEditFormObject["emin"] = stopp.getMinutes();
228         
229         addTimerEditFormObject["justplay"] = String(justplay);
230         addTimerEditFormObject["channel"] = decodeURIComponent(String(channel));
231         addTimerEditFormObject["channelName"] = String(channelName);
232         addTimerEditFormObject["channelSort"] = "";
233         addTimerEditFormObject["name"] = String(name);
234         addTimerEditFormObject["description"] = String(description);
235         addTimerEditFormObject["repeated"] = Number(repeated);
236         addTimerEditFormObject["afterEvent"] = Number(afterEvent);
237         
238         debug(justplay+"|"+begin+"|"+end+"|"+repeated+"|"+channel+"|"+name+"|"+description+"|"+afterEvent);
239
240         addTimerEditFormObject["deleteOldOnSave"] = Number(deleteOldOnSave);
241         addTimerEditFormObject["beginOld"] = Number(begin);
242         addTimerEditFormObject["endOld"] = Number(end);
243         
244         addTimerEditFormObject["eventID"] = Number(eit);
245         
246         loadTimerFormChannels();
247 }
248
249 // startin to load for TV
250 function loadTimerFormChannels() {
251         if(addTimerEditFormObject["TVListFilled"] == 1 && addTimerEditFormObject["RadioListFilled"] == 1) {
252                 loadTimerForm();
253         } else if(addTimerEditFormObject["TVListFilled"] == 1 && addTimerEditFormObject["RadioListFilled"] == 0) {
254                 addTimerListFormatTV();
255         } else {
256                 var favorites = '1%3A7%3A1%3A0%3A0%3A0%3A0%3A0%3A0%3A0%3AFROM%20BOUQUET%20%22userbouquet.favourites.tv%22%20ORDER%20BY%20bouquet'
257                 doRequest(url_getServices+favorites, addTimerListFormatTV, false);
258         }
259 }
260
261 function addTimerListFormatTV(request) {
262         if(addTimerEditFormObject["RadioListFilled"] == 0) {
263                 if(request.readyState == 4){
264                         var services = new ServiceList(getXML(request)).getArray();
265                         var tv = new Object();
266                         for ( var i = 0; i < services.length ; i++){
267                                 var reference = services[i];
268                                 tv[reference.servicereference] = reference.servicename;
269                         }
270                         addTimerEditFormObject["TVListFilled"] = 1;
271                         addTimerEditFormObject["TVList"] = tv;
272                 }
273         }
274         if(addTimerEditFormObject["RadioListFilled"] == 1) {
275                 loadTimerForm()
276         } else {
277                 var favorites = '1%3A7%3A1%3A0%3A0%3A0%3A0%3A0%3A0%3A0%3AFROM%20BOUQUET%20%22userbouquet.favourites.radio%22%20ORDER%20BY%20bouquet';
278                 doRequest(url_getServices+favorites, addTimerListFormatRadio, false);
279         }
280 }
281 function addTimerListFormatRadio(request) {
282         if(request.readyState == 4){
283                 var services = new ServiceList(getXML(request)).getArray();
284                 var radio = new Object();
285                 for ( var i = 0; i < services.length ; i++){
286                         var reference = services[i];
287                         radio[reference.servicereference] = reference.servicename;
288                 }
289                 addTimerEditFormObject["RadioListFilled"] = 1;
290                 addTimerEditFormObject["RadioList"] = radio;
291         }
292         loadTimerForm();
293 }
294
295 function loadTimerForm(){
296
297         var Action = new Object();
298         Action["0"] = "Record";
299         Action["1"] = "Zap";
300         
301         var Repeated = new Object();
302         Repeated["1"] =  "mo";
303         Repeated["2"] = "tu";
304         Repeated["4"] =  "we";
305         Repeated["8"] =  "th";
306         Repeated["16"] = "fr";
307         Repeated["32"] = "sa";
308         Repeated["64"] = "su";
309         Repeated["31"] = "mf";
310         Repeated["127"] ="ms";
311         
312         var AfterEvent = new Object();
313         AfterEvent["0"] = "Nothing";
314         AfterEvent["1"] = "Standby";
315         AfterEvent["2"] = "Deepstandby/Shutdown";
316         
317         addTimerEditFormObject["name"] = (typeof(addTimerEditFormObject["name"]) != "undefined") ? addTimerEditFormObject["name"] : "";
318         addTimerEditFormObject["name"] = (addTimerEditFormObject["name"] == "") ? " " : addTimerEditFormObject["name"];
319
320         addTimerEditFormObject["description"] = (typeof(addTimerEditFormObject["description"]) != "undefined") ? addTimerEditFormObject["description"] : "";
321         addTimerEditFormObject["description"] = (addTimerEditFormObject["description"] == "") ? " " : addTimerEditFormObject["description"];
322
323         var channelObject = addTimerEditFormObject["TVList"];
324         if(     addTimerEditFormObject["channelSort"] == "tv") {
325                 // already set
326         } else if( addTimerEditFormObject["channelSort"] == "radio") {
327                 channelObject = addTimerEditFormObject["RadioList"];
328         } else {
329                 var found = 0;
330                 for( element in addTimerEditFormObject["TVList"]) {
331                         if( element == addTimerEditFormObject["channel"]) {
332                                 found = 1;
333                                 break;
334                         }
335                 }
336                 if(found == 0) {
337                         for( element in addTimerEditFormObject["RadioList"]) {
338                                 if( element == addTimerEditFormObject["channel"]) {
339                                         channelObject = addTimerEditFormObject["RadioList"];
340                                         found = 1;
341                                         break;
342                                 }
343                         }
344                 }
345                 if(found == 0) {
346                         addTimerEditFormObject["TVList"][addTimerEditFormObject["channel"]] = addTimerEditFormObject["channelName"];
347                 }
348         }
349         var dashString = "------";
350         channelObject[dashString] = "- Bouquets -";
351         var listeNeu = new ServiceList(getXML(doRequestMemory[url_getServices+encodeURIComponent(bouqet_tv)])).getArray();
352         if(addTimerEditFormObject["channelSort"] == "radio") {
353                 debug("weiter");
354                 listeNeu = new ServiceList(getXML(doRequestMemory[url_getServices+encodeURIComponent(bouqet_radio)])).getArray();
355         }
356         for (i = 1; i < listeNeu.length; i++) {
357                 var element = listeNeu[i];
358                 channelObject[String(dashString+i)] = "---";
359                 channelObject[element.getServiceReference()] = element.getServiceName();
360         }
361         var namespace = {       
362                                 'justplay': addTimerFormCreateOptionList(Action, addTimerEditFormObject["justplay"]),
363                                 'syear': addTimerFormCreateOptions(2008,2010,addTimerEditFormObject["syear"]),
364                                 'smonth': addTimerFormCreateOptions(1,12,addTimerEditFormObject["smonth"]),
365                                 'sday': addTimerFormCreateOptions(1,31,addTimerEditFormObject["sday"]),
366                                 'shour': addTimerFormCreateOptions(0,23,addTimerEditFormObject["shour"]),
367                                 'smin': addTimerFormCreateOptions(0,59,addTimerEditFormObject["smin"]),
368                                 'eyear': addTimerFormCreateOptions(2008,2010,addTimerEditFormObject["eyear"]),
369                                 'emonth': addTimerFormCreateOptions(1,12,addTimerEditFormObject["emonth"]),
370                                 'eday': addTimerFormCreateOptions(1,31,addTimerEditFormObject["eday"]),
371                                 'ehour': addTimerFormCreateOptions(0,23,addTimerEditFormObject["ehour"]),
372                                 'emin': addTimerFormCreateOptions(0,59,addTimerEditFormObject["emin"]),
373                                 'channel': addTimerFormCreateOptionList(channelObject, addTimerEditFormObject["channel"]),
374                                 'name': addTimerEditFormObject["name"],
375                                 'description': addTimerEditFormObject["description"],
376                                 'repeated': addTimerFormCreateOptionListRepeated(Repeated, addTimerEditFormObject["repeated"]),
377                                 'deleteOldOnSave': addTimerEditFormObject["deleteOldOnSave"],
378                                 'channelOld': addTimerEditFormObject["channel"],
379                                 'beginOld': addTimerEditFormObject["beginOld"],
380                                 'endOld': addTimerEditFormObject["endOld"],
381                                 'afterEvent': addTimerFormCreateOptionList(AfterEvent, addTimerEditFormObject["afterEvent"]),
382                                 'eventID': addTimerEditFormObject["eventID"]
383                 };
384         var listerHtml = RND(tplAddTimerForm, namespace);
385         $('BodyContent').innerHTML = listerHtml;
386
387         // Empty some stuff, but keep others to have the performance
388         var tmp1 = addTimerEditFormObject["RadioList"];
389         var tmp2 = addTimerEditFormObject["TVList"];
390         addTimerEditFormObject = new Object();
391         addTimerEditFormObject["deleteOldOnSave"] = 0;
392         addTimerEditFormObject["RadioList"] = tmp1;
393         addTimerEditFormObject["TVList"] = tmp2;
394         addTimerEditFormObject["TVListFilled"] = 1;
395         addTimerEditFormObject["RadioListFilled"] = 1;
396 }
397
398 function addTimerFormCreateOptions(start,end,number) {
399         var html = '';
400         for(i = start; i <= end; i++) {
401                 var txt = (String(i).length == 1) ? "0" + String(i) : String(i);
402                 var selected =  (i == Number(number)) ? "selected" : " ";
403                 var namespace = {
404                         'value': i,
405                         'txt': txt,
406                         'selected': selected };
407                 html += RND(tplAddTimerFormOptions, namespace);
408         }
409         return html;
410 }
411 function addTimerFormCreateOptionList(object,selected) {
412         html = '';
413         var found = 0;
414         for(var element in object) {
415                 var txt = String(object[element]);
416                 var sel = " ";
417                 if(element == selected) {
418                         found = 1;
419                         sel = "selected";
420                 }
421                 var namespace = {
422                         'value': element,
423                         'txt': txt,
424                         'selected': sel };
425                 if(element != "extend") {
426                         html += RND(tplAddTimerFormOptions, namespace);
427                 }
428         }
429         if(found == 0) {
430                 var namespace = {
431                         'value': element,
432                         'txt': txt,
433                         'selected': sel };
434         }
435         return html;
436 }
437
438 function timerFormExtendChannellist(bouqet) {
439         var listeTV = new ServiceList(getXML(doRequestMemory[url_getServices+encodeURIComponent(bouqet_tv)])).getArray();
440         var listeRadio = new ServiceList(getXML(doRequestMemory[url_getServices+encodeURIComponent(bouqet_radio)])).getArray();
441         found = 0;
442         for(i = 0; i < listeTV.length; i++) {
443                 var element = listeTV[i];
444                 if(String(element.getServiceReference()) == bouqet) {
445                         found = 1;
446                         break;
447                 }
448         }
449         if(found == 0) {
450                 for(i = 0; i < listeRadio.length; i++) {
451                         var element = listeTV[i];
452                         if(String(element.getServiceReference()) == bouqet) {
453                                 found = 1;
454                                 break;
455                         }
456                 }
457         }
458         if(found == 1) {
459                 servicereftoloadepgnow = bouqet;
460                 if(typeof(loadedChannellist[servicereftoloadepgnow]) == "undefined") {  
461                         doRequest(url_getServices+servicereftoloadepgnow, incomingTimerFormExtendChannellist, true);
462                 } else {
463                         incomingTimerFormExtendChannellist();
464                 }
465         }
466 }
467 function incomingTimerFormExtendChannellist(request) {
468         var services = null;
469         if(typeof(loadedChannellist[servicereftoloadepgnow]) != "undefined"){
470                 services = loadedChannellist[servicereftoloadepgnow];
471         } else if(request.readyState == 4) {
472                 services = new ServiceList(getXML(request)).getArray();
473                 loadedChannellist[servicereftoloadepgnow] = services;
474                 debug("got "+services.length+" Services");
475         }
476         var attachLater = new Object();
477         if(services != null) {
478                 debug("incomingTimerFormExtendChannellist " + services.length);
479                 var selected = $('channel').selectedIndex;
480                 for(j = selected; j < $('channel').options.length; j++) {
481                         if($('channel').options[j].value == servicereftoloadepgnow) {
482                                 j++;
483                                 for(var i = 0; i < services.length ; i++) {
484                                         var reference = services[i];
485                                         var newEntry = new Option(reference.getServiceName(), reference.getServiceReference(), false, true);
486                                         if(typeof($('channel').options[j]) != "undefined") {
487                                                 attachLater[String($('channel').options[j].value)] = $('channel').options[j].text;
488                                         }
489                                         $('channel').options[j] = newEntry;
490                                         j++;
491                                 }
492                         }
493                         break;
494                 }
495                 for(x in attachLater) {
496                         var newEntry = new Option(attachLater[x], x, false, true);
497                         if(x != "extend") {
498                                 $('channel').options[$('channel').options.length] = newEntry;
499                         }
500                 }
501                 $('channel').options[selected].selected = true;
502                 
503         }
504 }
505
506 function addTimerFormChangeTime(which) {
507         var start = new Date( $('syear').value, ($('smonth').value -1), $('sday').value, $('shour').value, $('smin').value, 0);
508         var end = new Date($('eyear').value, ($('emonth').value -1), $('eday').value, $('ehour').value, $('emin').value, 0);
509 //      debug("("+start+")(" + end+")");
510
511         if(start.getTime() > end.getTime()) {
512                 opponent = (which.substr(0,1) == 's') ? 'e' +  which.substr(1, which.length -1) : 's' +  which.substr(1, which.length -1) ;
513                 $(opponent).value = $(which).value;
514         }
515         var all = new Array('year','month','day','hour','min');
516         for(i=0; i < all.length; i++) {
517                 if(which.substr(1, which.length -1) == all[i]) {
518                         addTimerFormChangeTime(which.substr(0,1) + all[i+1] );
519                         break;
520                 }
521         }
522 }
523 function addTimerFormChangeType() {
524         var selected = ($('tvradio').checked == true) ? addTimerEditFormObject["TVList"]: addTimerEditFormObject["RadioList"];
525         for (i = $('channel').options.length; i != 0; i--) {
526                 $('channel').options[i - 1] = null;
527         }
528         var i = -1;
529         for(element in selected) {
530                 if(element != "extend") {
531                         i++;
532                         $('channel').options[i] = new Option(selected[element]);
533                         $('channel').options[i].value = element;
534                 }
535         }
536 }
537 function addTimerFormCreateOptionListRepeated(Repeated,repeated) {
538         var num = Number(repeated);
539         var html = "";
540         var html2 = "";
541         var list = new Array(127, 64, 32, 31, 16, 8, 4, 2, 1);
542         for(i = 0; i < list.length; i++) {
543                 var txt = String(Repeated[String(list[i])]);
544                 if( String(Repeated[String(list[i])]) == "mf") {
545                         txt = "Mo-Fr";
546                 } else if( String(Repeated[String(list[i])]) == "ms") {
547                         txt = "Mo-Su";
548                 } else {
549                         txt = txt.substr(0,1).toUpperCase() + txt.substr(1,1);
550                 }
551                 var checked = " ";
552                 if(num >=  list[i]) {
553                         num -= list[i];
554                         checked = "checked";
555                 }
556                 var namespace = {
557                         'id': Repeated[String(list[i])],
558                         'name': Repeated[String(list[i])],
559                         'value': list[i],
560                         'txt': txt,
561                         'checked': checked };
562                 if(String(Repeated[String(list[i])]) == "mf" || String(Repeated[String(list[i])]) == "ms") {
563                         html2 = RND(tplAddTimerFormCheckbox, namespace) + html2;
564                 } else {
565                         html = RND(tplAddTimerFormCheckbox, namespace) + html;
566                 }
567         }
568         return html + html2;
569 }
570 function sendAddTimer() {
571         debug("sendAddTimer" + "parentChannel:" +$('channel').value);
572         
573         if(parentPin($('channel').value)) {
574                 var beginD = new Date(ownLazyNumber($('syear').value), (ownLazyNumber($('smonth').value) - 1), ownLazyNumber($('sday').value), ownLazyNumber($('shour').value), ownLazyNumber($('smin').value));
575                 var begin = beginD.getTime()/1000;
576                 
577                 var endD = new Date(ownLazyNumber($('eyear').value), (ownLazyNumber($('emonth').value) - 1), ownLazyNumber($('eday').value), ownLazyNumber($('ehour').value), ownLazyNumber($('emin').value));
578                 var end = endD.getTime()/1000;
579
580                 var repeated = 0;
581                 if( $('ms').checked ) {
582                         repeated = 127;
583                 } else if($('mf').checked) {
584                         repeated = 31;
585                         if($('sa').checked) {
586                                 repeated += ownLazyNumber($('sa').value);
587                         }
588                         if($('su').checked) {
589                                 repeated += ownLazyNumber($('su').value);
590                         }
591                 } else {
592                         var check = new Array('mo', 'tu', 'we', 'th', 'fr');
593                         for(i = 0; i < check.length; i++) {
594                                 if($(check[i]).checked) {
595                                         repeated += Number($(check[i]).value);
596                                 }
597                         }
598                 }
599         
600                 var descriptionClean = ($('descr').value == " " || $('descr').value == "N/A") ? "" : $('descr').value;
601                 var nameClean = ($('name').value == " " || $('name').value == "N/A") ? "" : $('name').value;
602                 
603                 var neverString = "[0-9a-zA-Z\-_\.\!\(\)&=\+$,;\?/:\\\ ]*";
604                 if(descriptionClean != descriptionClean.match(neverString) ||
605                         nameClean != nameClean.match(neverString)) {
606                         alert("Please only use "+neverString+" in the name and the description field");
607                         return;
608                 }
609
610                 var repeated = 0;
611                 if($('ms').checked) {
612                         repeated = ownLazyNumber($('ms').value);
613                 } else if($('mf').checked) {
614                         repeated = ownLazyNumber($('mf').value);
615                         if($('su').checked) {
616                                 repeated += ownLazyNumber($('su').value);
617                         }
618                         if($('sa').checked) {
619                                 repeated += ownLazyNumber($('sa').value);
620                         }
621                 } else {
622                         for(i = 0; i < days.length; i++){
623                                 if($(days[i]).checked) {
624                                         repeated += ownLazyNumber($(days[i]).value);
625                                 }
626                         }
627                 }
628                 //addTimerByID(\'%(servicereference)\',\'%(eventid)\',\'False\');
629                 doRequest(url_timerchange+"?"+"sRef="+($('channel').value).replace("&quot;", '"')+"&begin="+begin
630                   +"&end="+end+"&name="+escape(nameClean)+"&description="+escape(descriptionClean)
631                   +"&afterevent="+$('after_event').value+"&eit=0&disabled=0"
632                   +"&justplay="+ownLazyNumber($('justplay').value)+"&repeated="+repeated
633                   +"&channelOld="+$('channelOld').value
634                   +"&beginOld="+$('beginOld').value+"&endOld="+$('endOld').value
635                   +"&eventID"+$('eventID').value
636                   +"&deleteOldOnSave="+ownLazyNumber($('deleteOldOnSave').value), incomingTimerAddResult, false);
637         }
638 }
639
640 function cleanTimerListNow(){
641         debug("cleanTimerListNow pushed");
642         doRequest(url_timerlist, incomingCleanTimerListNow, false);     
643 }
644 function incomingCleanTimerListNow(request) {
645         if(request.readyState == 4){
646                 var timers = new TimerList(getXML(request)).getArray();
647                 debug("have "+timers.length+" timer");
648                 for ( var i = 0; i <timers.length; i++){
649                         var timer = timers[i];
650                         debug(timer.getState() + " " + quotes2html(timer.getName()));
651                         if(timer.getState() != 0 && timer.getState() != 2) {
652                                 delTimer(timer.getServiceReference(),timer.getTimeBegin(),timer.getTimeEnd()
653                                         ,quotes2html(timer.getServiceName()),quotes2html(timer.getName()),quotes2html(timer.getDescription()),incomingJustDoNothing);
654                         }
655                 }
656         }
657 }
658 function incomingJustDoNothing(request){
659         debug("just do nothing");
660 }
661 function sendToggleTimerDisable(justplay,begin,end,repeated,channel,name,description,afterEvent,disabled){
662         disabled = (ownLazyNumber(disabled) == 0) ? 1 : 0;
663         
664         var descriptionClean = (description == " " || description == "N/A") ? "" : description;
665         var nameClean = (name == " " || name == "N/A") ? "" : name;
666
667         doRequest(url_timerchange+"?"+"sRef="+channel.replace("&quot;", '"')+"&begin="+begin
668          +"&end="+end+"&name="+escape(nameClean)+"&description="+escape(descriptionClean)
669          +"&afterevent="+afterEvent+"&eit=0&disabled="+disabled
670          +"&justplay="+justplay+"&repeated="+repeated
671          +"&channelOld="+channel
672          +"&beginOld="+begin+"&endOld="+end
673          +"&deleteOldOnSave=1", incomingTimerAddResult, false);
674 }
675
676
677 // AUTOTIMER
678 function loadAutoTimerList(){
679         debug("loading AutoTimer");
680         doRequest(url_autotimerlist, incomingAutoTimerList, false);
681 }
682
683 var autotimers = null;
684 function incomingAutoTimerList(request){
685         if(request.readyState == 4){
686                 autotimers = new AutoTimerList(getXML(request)).getArray();
687                 debug("have "+autotimers.length+" timer");
688                 var ns_tplTimerListHeader = {
689                         'page_title': 'Auto timerlist',
690                         'name1': 'Name',
691                         'name2': 'Match',
692                         'repeated': 'Repeated',
693                         'duration': 'Time running',
694                         'start': 'Start time',
695                         'end': 'End time',
696                         'event': 'Event',
697                         'after_event': 'After event',
698                         'options': 'Options'                    
699                 };
700                 listerHtml = RND(tplTimerListHeader, ns_tplTimerListHeader);
701                 var aftereventReadable = new Array ('Nothing', 'Standby', 'Deepstandby/Shutdown');
702                 var justplayReadable = new Array('record', 'zap');
703                 for ( var i = 0; i <autotimers.length; i++){
704                         var timer = autotimers[i];
705 //                      var beginDate = new Date(Number(timer.getTimeBegin())*1000);
706 //                      var endDate = new Date(Number(timer.getTimeEnd())*1000);
707                         var namespace = {       
708                                  'servicereference': timer.getServices()
709                                 ,'servicename': quotes2html(timer.getName())
710                                 ,'title': quotes2html(timer.getMatch())
711                                 ,'description': quotes2html(timer.getMatch())
712                                 ,'justplay': timer.getJustplay()
713                                 ,'justplayReadable': timer.getJustplay()
714                                 ,'repeated': timer.getCounter()
715                                 ,'repeatedReadable': timer.getCounter()
716                                 ,'afterevent': timer.getAfterevent()
717                                 ,'onOff': timer.getToggleDisabledIMG()
718                                 ,'begin': timer.getTimespanbegin()
719                                 ,'beginDate': timer.getTimespanbegin()
720                                 ,'end': timer.getTimespanend()
721                                 ,'endDate': timer.getTimespanend()
722                                 ,'sendToggleTimerDisable_FUNCTION': "sendToggleAutoTimerDisable("+i+")"
723                                 ,'delTimer_FUNCTION': "delAutoTimer("+i+")"
724                                 ,'loadTimerFormSeconds_FUNCTION': "loadAutoTimer("+i+")"
725                                 ,'color': '000000'
726 /*                              'descriptionextended': quotes2html(timer.getDescriptionExtended()),
727                                 'state': timer.getState(),
728                                 'duration': Math.ceil((timer.getDuration()/60)),
729                                 
730                                 'aftereventReadable': aftereventReadable[Number(timer.getAfterevent())],
731                                 'disabled': timer.getDisabled(),
732                                 'onOff': timer.getToggleDisabledIMG(),
733                                 'color': timer.getColor()*/
734                         };
735                         listerHtml += RND(tplTimerListItem, namespace);
736                 }
737                 listerHtml += tplTimerListFooter;
738                 $('BodyContent').innerHTML = listerHtml;
739                 setBodyMainContent('BodyContent');
740         }
741 }
742
743 function sendToggleAutoTimerDisable(element) {
744         timers = autotimers[Number(element)];
745         debug("sendToggleAutoTimerDisable["+element+"]: "+timers.getName());
746         
747         doRequest(url_autotimertoggledisable+"?name="+timer.getName()+"&match="+timer.getMatch()
748                 , loadAutoTimerList, false);
749 }
750
751 function delAutoTimer(element) {
752         timers = autotimers[Number(element)];
753         debug("delAutoTimer["+element+"]: "+timers.getName());
754         Dialog.confirm(
755                 "Selected timer:<br>"
756                 +"Name: "+quotes2html(timer.getName())+"<br>"
757                 +"Match: "+quotes2html(timer.getMatch())+"<br>"
758                 +"Are you sure that you want to delete the AutoTimer?",
759                  {windowParameters: {width:300, className: windowStyle},
760                         okLabel: "delete",
761                         buttonClass: "myButtonClass",
762                         cancel: function(win) {debug("delAutoTimer cancel confirm panel")},
763                         ok: function(win) { 
764                                                             debug("delAutoTimer ok confirm panel"); 
765                                                             doRequest(url_autotimerdelete+"?name="+timer.getName()+"&match="+timer.getMatch(), loadAutoTimerList, false);
766                                                             return true;
767                                                           }
768                         }
769         );
770 }
771
772 function loadAutoTimer(element) {
773         timer = autotimers[Number(element)];
774         debug("loadAutoTimer["+element+"]: "+timer.getName());
775         
776         var Action = new Object();
777         Action["0"] = "Record";
778         Action["1"] = "Zap";
779         
780         var Repeated = new Object();
781         Repeated["1"] =  "mo";
782         Repeated["2"] = "tu";
783         Repeated["4"] =  "we";
784         Repeated["8"] =  "th";
785         Repeated["16"] = "fr";
786         Repeated["32"] = "sa";
787         Repeated["64"] = "su";
788         Repeated["31"] = "mf";
789         Repeated["127"] ="ms";
790         
791         var AfterEvent = new Object();
792         AfterEvent["0"] = "Nothing";
793         AfterEvent["1"] = "Standby";
794         AfterEvent["2"] = "Deepstandby/Shutdown";
795         
796         var beginDate = timer.getTimespanbegin().split(":");
797         var endDate =   timer.getTimespanend().split(":");
798         
799         
800         var namespace = {       
801                                  'justplay': addTimerFormCreateOptionList(Action, timer.getJustplay)
802                                 ,'shour': addTimerFormCreateOptions(0,23,beginDate[0])
803                                 ,'smin': addTimerFormCreateOptions(0,59,beginDate[1])
804                                 ,'ehour': addTimerFormCreateOptions(0,23,endDate[0])
805                                 ,'emin': addTimerFormCreateOptions(0,59,endDate[1])
806                                 ,'name': timer.getName()
807                                 ,'description': timer.getMatch()
808                                 ,'repeated': addTimerFormCreateOptions(0,99,timer.getCounter())
809                                 ,'deleteOldOnSave': addTimerEditFormObject["deleteOldOnSave"]
810                                 ,'afterEvent': addTimerFormCreateOptionList(AfterEvent, timer.getAfterevent())
811                                 ,'timers_element': element
812                 };
813         var listerHtml = RND(tplAddAutoTimerForm, namespace);
814         $('BodyContent').innerHTML = listerHtml;
815 }
816
817 function sendAddAutoTimer() {
818         debug("sendAddAutoTimer");
819 }