* fix webouqueteditor
[enigma2-plugins.git] / webbouqueteditor / src / web-data / bouqueteditor.js
1 var MODE = { tv : 0, radio : 1};
2 var NAV_SERVICE = { provider : 0, sat : 1, all : 2 };
3 var currentMode = null;
4 var currentNav = null;
5 var protectionPasswordValid = false;
6 var protectionType = null;
7 var protectionConfigured = false;
8 var chachedPin = null;
9
10 //// index marker
11 var currentServicelistIndex = null;
12 var currentBouquetlistIndex = null;
13 var currentProviderServiceList = null;
14 var currentProviderServiceListFiltered = null;
15
16 var currentBouquetRef = null;
17         
18
19 function getSelectedAttr(selectId) {
20
21         var selectList = $(selectId);
22         var selectOptions = selectList.getElementsByTagName('option');
23         var idx = selectList.selectedIndex; 
24         return { selectOptions: selectOptions, idx: idx};
25
26 }
27
28 function moveOptions(selectId, pos) {
29         var ref = "";
30         var index = -1;
31         var selectList = $(selectId);
32         var selectOptions = selectList.getElementsByTagName('option');
33         var idx = selectList.selectedIndex; 
34         if (idx >= 0){
35                 var opt = selectOptions[idx];
36                 selectList.removeChild(opt);
37                 if(pos < selectOptions.length){
38                         selectList.insertBefore(opt, selectOptions[pos]);
39                         ref = unescape(opt.value);
40                         index = pos;
41                 } else {
42                         selectList.appendChild(opt);
43                         ref = unescape(opt.value);
44                         index = selectOptions.length - 1;
45                 }
46         }
47         return { ref: ref , index: index};
48 }
49
50 function setProviderName(provider){
51         if( currentNav != NAV_SERVICE.all){
52                 $("providerServiceHeader").update("All Services of '" + provider + "'");
53         } else {
54                 $("providerServiceHeader").update("All Services");
55         }
56 }
57
58 function setBouquetName(bouquet){
59         $("bouquetServiceHeader").update("Services of Bouquet '" + bouquet + "'");
60 }
61
62 function setNavBackground(element, selected){
63         if(selected)
64                 element.setStyle({background :  "#AAA"});
65         else
66                 element.setStyle({background :  "#485052"});
67 }
68
69 function setMode(mode){
70         currentMode = mode;
71         switch(mode){
72         
73         case MODE.tv:
74                 setNavBackground($("navTv"), true);
75                 setNavBackground($("navRadio"), false);
76                 break;
77                 
78         case MODE.radio:
79                 setNavBackground($("navTv"), false);
80                 setNavBackground($("navRadio"), true);
81                 break;
82         }
83         if (protectionPasswordValid) {
84                 setServiceNav(0);
85                 getBouquets();
86         }
87         else
88                 getProtectionData();
89 }
90
91 function setServiceNav(type){
92
93         if (protectionPasswordValid) {
94                 currentNav = type;
95                 setServiceNavButtons(type);
96                 if(type == NAV_SERVICE.all){
97                         $("contentSelectionList").hide();
98                         setProviderName("");
99                 } else {
100                         $("contentSelectionList").show();
101                 }
102                  
103                 getProviderList(type);
104         }
105         else
106                 getProtectionData();
107 }
108
109 function setServiceNavButtons(type){
110         switch(type){
111                 case NAV_SERVICE.provider:
112                         setNavBackground($("navProv"), true);
113                         setNavBackground($("navSat"), false);
114                         setNavBackground($("navAll"), false);
115                         
116                 break;
117                 case NAV_SERVICE.sat:
118                         setNavBackground($("navProv"), false);
119                         setNavBackground($("navSat"), true);
120                         setNavBackground($("navAll"), false);
121                         
122                 break;
123                 case NAV_SERVICE.all:
124                         setNavBackground($("navProv"), false);
125                         setNavBackground($("navSat"), false);
126                         setNavBackground($("navAll"), true);
127                         
128                 break;
129         }
130 }
131
132 function onServiceSearchFocus(event){
133         if(event.element().value == "Service to search for..."){
134                 event.element().value = '';
135         }
136         event.element().setStyle({color : "#000"});
137         serviceSearch(event);
138 }
139
140 function onServiceSearchBlur(event){
141         if(event.element().value.trim() == ""){
142                 event.element().setStyle({color : "#AAA"});
143                 event.element().value = "Service to search for...";
144         }
145 }
146
147 function serviceSearch(event){
148         var needle = event.element().value.toLowerCase();
149                 
150         //Nur Gecko-basierte Browser k�nnen <option> elemente ausblenden
151         if(Prototype.Browser.Gecko){
152                 var serviceElements = $$('.providerservice');
153                 
154                 for(var i = 0; i < serviceElements.length; i++){
155                         var option = serviceElements[i];
156                         var serviceName = option.readAttribute('data-servicename').toLowerCase();
157                         
158                         if(serviceName.indexOf(needle) == -1 && serviceName != ""){
159                                 option.hide();
160                         } else {                
161                                 option.show();
162                         }
163                 }
164         //f�r alle anderen Anderen Browser muss die Liste komplett neu aufgebaut werden
165         } else {
166                 currentProviderServiceListFiltered = [];
167                 $A(currentProviderServiceList).each(function(service){
168                         var serviceName = service.servicename.toLowerCase();
169                         if(serviceName.indexOf(needle) != -1 || serviceName == ""){
170                                 currentProviderServiceListFiltered[currentProviderServiceListFiltered.length] = service;
171                         }                       
172                 });
173                 
174                 fillProviderServiceList(currentProviderServiceListFiltered);
175         }
176 }
177
178 function fillProviderServiceList(servicelist){
179         var data = { services : servicelist };
180         processTplBouquetEditor('providerservicelist', data, 'contentSelectionSubList');
181 }
182
183 function getCurrentSelectedBouquetlistRef() {
184
185         var selectList = $('bouquetlist');
186         var idx = selectList.selectedIndex; 
187         var current = selectList.options[idx];
188         return unescape(current.value);
189         
190 }
191
192 function getAlternatives(ref){  
193         doRequest('/bouqueteditor/web/getservices?sRef=' + ref, incomingAlternatives, false);
194 }
195
196 function incomingAlternatives(request) {
197         if (request.readyState == 4) {
198                 var servicelist = new BouquetEditorServiceList(getXML(request)).getArray();
199                 var data = {
200                         services : servicelist
201                 };
202                 processTplBouquetEditor('alternativelist', data, 'contentAlternatives');
203         }
204 }
205
206 function removeAlternative(selectObj) {
207         var selectServicelist = $('servicelist');
208         var idxServicelist = selectServicelist.selectedIndex;
209         var selectList = $(selectObj);
210         var selectServicelistOptions = selectList.getElementsByTagName('option');
211         if (selectServicelistOptions.length > 1) {
212                 if (idxServicelist != -1) {
213                         var current_alternative_service_ref = unescape(selectServicelist.options[idxServicelist].value);
214                         var idx = selectList.selectedIndex; 
215                         if ( idx != -1) {
216                                 var ref = unescape(selectList.options[idx].value); 
217                                         var check = confirm("Do you really want to delete the service\n" + selectList.options[idx].text + " from current alternative list?");
218                                 if (check == true)
219                                         doRequest('/bouqueteditor/web/removeservice?sBouquetRef='+ current_alternative_service_ref + '&sRef=' + ref, removeAlternativeServiceCallback, false);
220                         }
221                         else
222                                 alert("No service in alternative-list selected!");
223                 }
224                 else
225                         alert("No service in servicelist selected!");
226         }
227         else
228                 alert("There must be one service in the alternative-list at least!");
229 }
230
231 function removeAlternativeServiceCallback(request) {
232         if (request.readyState == 4) {
233                 var result = new SimpleXMLResult(getXML(request));
234                 simpleResultHandler(result);
235                 if(result.getState()){
236                         var selectList = $('alternativelist');
237                         var idx = selectList.selectedIndex; 
238                         selectList.removeChild(selectList.options[idx]);
239                         var selectOptions = selectList.getElementsByTagName('option');
240                         if ( selectOptions.length != 0) {
241                                 if ( idx > selectOptions.length-1)
242                                         idx = idx - 1;
243                                 selectOptions[idx].selected = true;
244                         }
245                 }
246         }
247 }
248
249 function servicelistChange(element){
250         var idx = element.selectedIndex; 
251         var current = element.options[idx];
252         var hasAlternatives = $(current).readAttribute("data-hasalt");
253         
254         if(hasAlternatives == "1"){
255
256                 $("removeAlternativServicesButton").show();
257                 $("alternativesHead").show();
258                 $("alternatives").show();
259                 
260                 $("contentAlternatives").update('Loading Alternatives... <img src="/bouqueteditor/ajax-loader.gif" />');
261                 
262                 var ref =  unescape(encodeURIComponent(current.value));
263                 getAlternatives(ref);
264                 
265         } else {
266                 $("alternativesHead").hide();
267                 $("alternatives").hide();
268                 $("removeAlternativServicesButton").hide();
269         }
270
271         var hasProtection = $(current).readAttribute("data-hasprotection");
272         var button = $("toggleserviceprotection");
273         if (protectionConfigured) {
274                 if ( protectionType == "0") {
275                         if (hasProtection == "0"){
276                                 button.update("Lock");
277                                 button.show();}
278                         else if (hasProtection == "1"){
279                                 button.update("Unlock");
280                                 button.show();}
281                         else
282                                 button.hide();
283                 } else {
284                         if (hasProtection == "3"){
285                                 button.update("Unlock");
286                                 button.show();}
287                         else if (hasProtection == "4"){
288                                 button.update("Lock");
289                                 button.show();}
290                         else if (hasProtection == "5")
291                                 button.hide();
292                 }
293                         
294                 
295         } else {
296                 button.hide();
297         }
298
299
300
301 }
302
303 function BouquetEditorService(xml, cssclass){   
304         this.servicereference = getNodeContent(xml, 'e2servicereference', '');
305         this.servicename = getNodeContent(xml, 'e2servicename');
306         this.serviceisgroup = getNodeContent(xml, 'e2serviceisgroup');
307         this.serviceismarker = getNodeContent(xml, 'e2serviceismarker');
308         this.serviceisprotected = getNodeContent(xml, 'e2serviceisprotected');
309         
310         this.getServiceReference = function(){
311                 return encodeURIComponent(this.servicereference);
312         };
313         
314         this.getClearServiceReference = function(){
315                 return this.servicereference;
316         };
317                 
318         this.getServiceName = function(){
319                 return this.servicename.replace('&quot;', '"');
320         };
321
322         this.getPrefix = function(){
323                 if (this.serviceisgroup == "1") {
324                         return "* ";}
325
326                 else {
327                         if (this.serviceismarker == "1") {
328                                 return "<------- ";}
329                         else {
330                                 return "";}}
331         };
332
333         this.getSuffix = function(){
334                 if (this.serviceisprotected != "0") {
335                         var returnvalue = '';
336                         switch(this.serviceisprotected){
337                                 case "1":
338                                         returnvalue = '(locked -S-)';
339                                 break;
340                                 case "2":
341                                         returnvalue = '(locked -B-)';
342                                 break;
343                                 case "3":
344                                         returnvalue = '(locked)';
345                                 break;
346                                 case "4":
347                                         returnvalue = '(unlocked -S-)';
348                                 break;
349                                 case "5":
350                                         returnvalue = '(unlocked -B-)';
351                                 break;
352                         }
353                         return returnvalue;}
354                 else {
355                         if (this.serviceismarker == "1") {
356                                 return " ------->";}
357                         else {
358                                 return "";}}
359                 
360         };
361
362         
363         this.setServiceReference = function(sref){
364                 this.servicereference = sref;
365         };
366                 
367         this.setServiceName = function(sname){
368                 this.servicename = sname.replace('&quot;', '"');
369         };
370         
371         if( typeof( cssclass ) == undefined ){
372                 cssclass = 'odd';
373         }
374         
375         this.json = {   
376                         'servicereference' : this.getServiceReference(),
377                         'servicename' : this.getServiceName(),
378                         'serviceisgroup' : this.serviceisgroup,
379                         'prefix' : this.getPrefix(),
380                         'suffix' : this.getSuffix(),
381                         'serviceisprotected' : this.serviceisprotected
382
383         };
384         
385         this.toJSON = function(){
386                 return this.json;
387         };
388 }       
389
390 function BouquetEditorServiceList(xml){
391         this.xmlitems = getNamedChildren(xml, "e2servicelist", "e2service");
392         this.servicelist = [];
393         this.getArray = function(){
394                 if(this.servicelist.length === 0){
395                         var cssclass = 'even';
396                         
397                         for (var i=0;i<this.xmlitems.length;i++){
398                                 cssclass = cssclass == 'even' ? 'odd' : 'even';
399                                 var service = new BouquetEditorService(this.xmlitems.item(i), cssclass).toJSON();
400                                 this.servicelist.push(service);
401                         }
402                 }
403                 return this.servicelist;
404         };
405 }
406
407 function getProviderList(type) {
408
409         url = '';
410         switch (type){
411                 case NAV_SERVICE.provider: 
412                         if (currentMode == MODE.tv) 
413                                 url = '/bouqueteditor/web/getservices?sRef=' + providerTv;
414                         else
415                                 url = '/bouqueteditor/web/getservices?sRef=' +  providerRadio;
416                 break;
417                 case NAV_SERVICE.sat:
418                         url = '/bouqueteditor/web/satelliteslist?mode=' + currentMode;
419                 break;
420                 case NAV_SERVICE.all:
421                         if (currentMode == MODE.tv) 
422                                 url = '/bouqueteditor/web/getservices?sRef=' + allTv;
423                         else
424                                 url = '/bouqueteditor/web/getservices?sRef=' +  allRadio;
425                 break;
426         }
427         if (type == NAV_SERVICE.all)
428                 doRequest(url, incomingProviderServiceList, false);
429         else
430                 doRequest(url, incomingProviderList, false);
431 }
432
433
434 function incomingProviderList(request) {
435         if (request.readyState == 4) {
436                 var servicelist = new BouquetEditorServiceList(getXML(request)).getArray();
437                 var data = {
438                         services : servicelist
439                 };
440                 processTplBouquetEditor('providerlist', data, 'contentSelectionList', processTplIncommingProviderListCallback);
441         }
442 }
443
444 function processTplIncommingProviderListCallback() {
445
446         var selectList = $('providerlist');
447         var selectOptions = selectList.getElementsByTagName('option');
448         if ( selectOptions.length != 0)
449         {
450                 if(currentNav != NAV_SERVICE.all){
451                         selectOptions[0].selected = true;
452                         providerlistChange(selectList);
453                 }               
454         }
455 }
456
457
458 function providerlistChange(selectObj) {
459         
460         var idx = selectObj.selectedIndex; 
461         var current = selectObj.options[idx];
462         var ref =  unescape(encodeURIComponent(current.value));
463         setProviderName(current.innerHTML);
464         doRequest('/bouqueteditor/web/getservices?sRef=' + ref, incomingProviderServiceList, false);
465 }
466
467 function incomingProviderServiceList(request) {
468         if (request.readyState == 4) {
469                 var servicelist = new BouquetEditorServiceList(getXML(request)).getArray();
470                 currentProviderServiceList = servicelist;
471                 var data = {
472                         services : servicelist
473                 };
474                 processTplBouquetEditor('providerservicelist', data, 'contentSelectionSubList');
475         }
476 }
477
478 function addBouquet(){
479         sName=prompt("Name des Bouquets:");
480         if (sName.length){
481                 var selectList = $('bouquetlist');
482                 var selectOptions = selectList.getElementsByTagName('option');
483                 currentBouquetlistIndex =  selectOptions.length ;
484                 doRequest('/bouqueteditor/web/addbouquet?name='+sName+'&mode='+currentMode, bouquetListUpdated, false);
485         }
486 }
487
488 function bouquetListUpdated(request) {
489         if (request.readyState == 4) {
490                 var result = new SimpleXMLResult(getXML(request));
491                 simpleResultHandler(result);
492                 if(result.getState()){                  
493                         getBouquetServiceList();
494                 }
495         }
496 }
497
498
499 function getBouquets() {
500         currentBouquetlistIndex = null;
501         getBouquetServiceList();
502 }
503
504
505 function getBouquetServiceList() {
506
507         if (currentMode == MODE.tv) 
508                 url = '/bouqueteditor/web/getservices?sRef=' + bouquetsTv;
509         else
510                 url = '/bouqueteditor/web/getservices?sRef=' +  bouquetsRadio;
511         doRequest(url, incomingBouquetList, false);
512 }
513
514 function incomingBouquetList(request) {
515         if (request.readyState == 4) {
516
517                 var bouquetlist = new BouquetEditorServiceList(getXML(request)).getArray();
518                 var data = {
519                         bouquets : bouquetlist
520                 };
521                 processTplBouquetEditor('bouquetlist', data, 'contentBouquetList', processTplBouquetListCallback);
522         }
523 }
524
525 function processTplBouquetEditor(tplName, data, domElement, callback){
526         var url = "/bouqueteditor/" +tplName+".htm";
527         doRequest(url, 
528                         function(transport){
529                 incomingProcessTpl(transport, data, domElement, callback);
530         }
531         );
532 }
533
534
535 function processTplBouquetListCallback() {
536         var selectList = $('bouquetlist');
537         var selectOptions = selectList.getElementsByTagName('option');
538         if ( selectOptions.length != 0){
539                 if (currentBouquetlistIndex != null)
540                         selectOptions[currentBouquetlistIndex].selected = true;
541                 else
542                         selectOptions[0].selected = true;
543                 bouquetlistChange(selectList);
544         }
545 }
546
547 function showRequestResult(request) {
548         if (request.readyState == 4) {
549                 var result = new SimpleXMLResult(getXML(request));
550                 simpleResultHandler(result);
551         }
552 }
553
554
555 function moveBouquet(value) {
556         if ( value.ref != "" && value.index != -1)
557                 doRequest('/bouqueteditor/web/movebouquet?sBouquetRef='+ value.ref +'&mode='+ currentMode + '&position=' + value.index, showRequestResult, false);
558 }
559
560
561
562 function moveBouquetOptionsUp(selectId) {
563         var value = getSelectedAttr(selectId);
564         if ( value.idx > 0)
565                 moveBouquet(moveOptions(selectId,value.idx-1));
566 }
567
568 function moveBouquetOptionsDown(selectId) {
569         var value = getSelectedAttr(selectId);
570         if ( (value.idx + 1) < (value.selectOptions.length))
571                         moveBouquet(moveOptions(selectId,value.idx+1));
572 }
573
574
575 function removeBouquet(selectObj) {
576         var selectList = $(selectObj);
577         var idx = selectList.selectedIndex; 
578         var bouqueref = unescape(selectList.options[idx].value); 
579         var check = confirm("Do you really want to delete the bouquet\n" + selectList.options[idx].text + " ?");
580         if (check == true)
581                 doRequest('/bouqueteditor/web/removebouquet?sBouquetRef='+ bouqueref +'&mode=' + currentMode, removeBouquetCallback, false);
582 }
583
584 function removeBouquetCallback(request) {
585         if (request.readyState == 4) {
586                 var result = new SimpleXMLResult(getXML(request));
587                 simpleResultHandler(result);
588                 if(result.getState()){
589                         var selectList = $('bouquetlist');
590                         var idx = selectList.selectedIndex; 
591                         selectList.removeChild(selectList.options[idx]);
592                         var selectOptions = selectList.getElementsByTagName('option');
593                         if ( selectOptions.length != 0) {
594                                 if ( idx > selectOptions.length-1)
595                                         idx = idx - 1;
596                                 selectOptions[idx].selected = true;
597                                 bouquetlistChange(selectList); // Update Servicelist
598                         }
599                 }
600         }
601 }
602
603
604 function bouquetlistChange(selectObj) {
605         var idx = selectObj.selectedIndex; 
606         var current = selectObj.options[idx];
607         var hasProtection = $(current).readAttribute("data-hasprotection");
608         var button = $("togglebouquetprotection");
609         if (protectionConfigured) {
610                 if ( protectionType == "0") {
611                         if (hasProtection == "0"){
612                                 button.update("Lock");
613                                 button.show();}
614                         else if (hasProtection == "2"){
615                                 button.update("Unlock");
616                                 button.show();}
617                         else
618                                 button.hide();
619                 } else {
620                         if (hasProtection == "3"){
621                                 button.update("Unlock");
622                                 button.show();}
623                         else if (hasProtection == "5"){
624                                 button.update("Lock");
625                                 button.show();}
626                 }
627         } else {
628                 button.hide();
629         }
630         currentServicelistIndex = null;
631         var idx = selectObj.selectedIndex; 
632         var current = selectObj.options[idx];
633         var ref = unescape(current.value);
634         setBouquetName(current.innerHTML);
635         getServiceList(ref);
636 }
637
638 function getServiceList(ref) {
639         currentBouquetRef = ref;
640         doRequest('/bouqueteditor/web/getservices?sRef=' + ref, incomingServiceList);
641 }
642
643 function incomingServiceList(request) {
644         if (request.readyState == 4) {
645                 var servicelist = new BouquetEditorServiceList(getXML(request)).getArray();
646                 var data = {
647                         services : servicelist
648                 };
649                 processTplBouquetEditor('servicelist', data, 'contentServicelist', processTplIncomingServiceListCallback);
650         }
651 }
652
653 function processTplIncomingServiceListCallback() {
654         var selectList = $('servicelist');
655         var selectOptions = selectList.getElementsByTagName('option');
656         if ( selectOptions.length != 0) {
657         
658                 if (currentServicelistIndex != null)
659                         selectOptions[currentServicelistIndex].selected = true;
660                 else
661                         selectOptions[0].selected = true;
662                 servicelistChange(selectList);
663         }
664 }
665
666 function moveService(value) {
667         if ( value.ref != "" && value.index != -1) {
668                 var bouqueref = currentBouquetRef;
669                 doRequest('/bouqueteditor/web/moveservice?sBouquetRef='+ bouqueref + '&sRef=' + value.ref + '&mode=' + currentMode + '&position=' + value.index, showRequestResult, false);
670         }
671 }
672
673 function moveServiceOptionsUp(selectId) {
674         var value = getSelectedAttr(selectId);
675         if ( value.idx > 0)
676                 moveService(moveOptions(selectId, value.idx-1));
677 }
678
679 function moveServiceOptionsDown(selectId) {
680         var value = getSelectedAttr(selectId);
681         if ( (value.idx + 1) < (value.selectOptions.length))
682                         moveService(moveOptions(selectId,value.idx+1));
683 }
684
685 function moveServiceOptionsUp5(selectId) {
686         var value = getSelectedAttr(selectId);
687         if ( value.idx > 0){
688                 var pos = -5;
689                 if (value.idx + pos < 0)
690                         pos = 0;
691                 else
692                         pos = value.idx + pos;
693                 moveService(moveOptions(selectId, pos));
694         }
695 }
696
697 function moveServiceOptionsDown5(selectId) {
698         var value = getSelectedAttr(selectId);
699         if ( value.idx != value.selectOptions.length-1){
700                 var pos = 5;
701                 if (value.idx + pos > value.selectOptions.length-1)
702                         pos = value.selectOptions.length -1;
703                 else
704                         pos = value.idx + pos;
705                 moveService(moveOptions(selectId,pos));
706         }
707 }
708
709 function moveServiceOptionsTop(selectId) {
710         var value = getSelectedAttr(selectId);
711         if ( value.idx > 0)
712                 moveService(moveOptions(selectId,0));
713 }
714
715 function moveServiceOptionsBottom(selectId) {
716         var value = getSelectedAttr(selectId);
717         if ( value.idx >= 0 && value.idx != (value.selectOptions.length-1))
718                 moveService(moveOptions(selectId,value.selectOptions.length-1));
719 }
720
721
722
723 function removeAlternativServices(selectObj) {
724         var bouqueref = currentBouquetRef;
725         var selectList = $(selectObj);
726         var idx = selectList.selectedIndex; 
727         if ( idx != -1) {
728                 var ref = unescape(selectList.options[idx].value); 
729                 var check = confirm("Do you really want to delete all allternatives services in service\n" + selectList.options[idx].text + " ?");
730                 if (check == true){
731                         currentServicelistIndex = idx;
732                         var url = '/bouqueteditor/web/removealternativeservices?sBouquetRef=' + bouqueref + "&sRef=" + ref;
733                         doRequest(url, serviceListUpdated, false);
734                 }
735         }
736         else
737                 alert("No service selected!");
738 }
739
740
741 function removeService(selectObj) {
742         var bouqueref = currentBouquetRef;
743         var selectList = $(selectObj);
744         var idx = selectList.selectedIndex; 
745         if ( idx != -1) {
746                 var ref = unescape(selectList.options[idx].value); 
747                 var check = confirm("Do you really want to delete the service\n" + selectList.options[idx].text + " from current selected Bouquet?");
748                 if (check == true)
749                         doRequest('/bouqueteditor/web/removeservice?sBouquetRef='+ bouqueref + '&sRef=' + ref +'&mode=' + currentMode, removeServiceCallback, false);
750         }
751         else
752                 alert("No service selected!");
753 }
754
755 function removeServiceCallback(request) {
756         if (request.readyState == 4) {
757                 var result = new SimpleXMLResult(getXML(request));
758                 simpleResultHandler(result);
759                 if(result.getState()){
760                         var selectList = $('servicelist');
761                         var idx = selectList.selectedIndex; 
762                         selectList.removeChild(selectList.options[idx]);
763                         var selectOptions = selectList.getElementsByTagName('option');
764                         if ( selectOptions.length != 0) {
765                                 if ( idx > selectOptions.length-1)
766                                         idx = idx - 1;
767                                 selectOptions[idx].selected = true;
768                         }
769                         servicelistChange(selectList);
770                 }
771         }
772 }
773
774
775 function addAlternativeService(selectObj) {
776         var bouqueref = currentBouquetRef;
777         var selectList = $(selectObj);
778         var idx = selectList.selectedIndex; 
779         if ( idx != -1){
780                 var ref = unescape(selectList.options[idx].value); // that service ref will be added to the current selected service from servicelist as alternative
781                 var selectServicelist = $('servicelist');
782                 var idxServicelist = selectServicelist.selectedIndex;
783                 if (idxServicelist != -1) {
784                         currentServicelistIndex = idxServicelist;
785                         var current_ref = unescape(selectServicelist.options[idxServicelist].value); // get current  selected ref --> this service will be the alternative
786                         var url = "/bouqueteditor/web/addservicetoalternative?sBouquetRef=" + bouqueref + "&sCurrentRef=" + current_ref + "&sRef=" + ref + "&mode=" + currentMode;
787                         doRequest(url, addAlternativeServiceCallback, false);
788                 }
789                 else
790                         alert ("No service in servicelist selected!");
791         }
792         else
793                 alert ("No service selected!");
794
795 }
796
797 function addAlternativeServiceCallback(request) {
798         if (request.readyState == 4) {
799                 var result = new SimpleXMLResult(getXML(request));
800                 simpleResultHandler(result);
801                 // always reload after adding a alertative, because it could have been created, but the result can be False however
802                 var ref = getCurrentSelectedBouquetlistRef();
803                 getServiceList(ref);
804                 
805         }
806 }
807
808
809 function addServiceToBouquet(selectObj) {
810         var bouqueref = currentBouquetRef;
811         var selectList = $(selectObj);
812         var idx = selectList.selectedIndex;
813         var refServicelist = "";
814                 if ( idx != -1){
815                 var ref = unescape(selectList.options[idx].value);
816                 var selectServicelist = $('servicelist');
817                 var selectServicelistOptions = selectServicelist.getElementsByTagName('option');
818                 if (selectServicelistOptions.length > 0) {
819                         var idxServicelist = selectServicelist.selectedIndex;
820                         currentServicelistIndex = idxServicelist + 1;
821                         if ( idxServicelist != -1 && idxServicelist != selectServicelistOptions.length){
822                                 if ( idxServicelist + 1 < selectServicelistOptions.length){
823                                         refServicelist = unescape(selectServicelist.options[idxServicelist+1].value);
824                                 }
825                         }
826                 }
827                 else
828                         currentServicelistIndex = null;
829                 doRequest('/bouqueteditor/web/addservicetobouquet?sBouquetRef='+ bouqueref + '&sRef=' + ref +'&sRefBefore=' + refServicelist, serviceListUpdated, false);
830         }
831         else
832                 alert ("No service selected!");
833
834 }
835
836
837 function serviceListUpdated(request) {
838         if (request.readyState == 4) {
839                 var result = new SimpleXMLResult(getXML(request));
840                 simpleResultHandler(result);
841                 if(result.getState()){
842                         var ref = getCurrentSelectedBouquetlistRef();
843                         getServiceList(ref);
844                 }
845         }
846 }
847
848
849
850 function addProviderToBouquet(selectObj) {
851         var selectList = $(selectObj);
852         var idx = selectList.selectedIndex; 
853         if ( idx != -1){
854                 var ref =  unescape(encodeURIComponent(selectList.options[idx].value));
855                 doRequest('/bouqueteditor/web/addprovidertobouquetlist?sProviderRef='+ref+'&mode=' + currentMode, addProviderToBouquetCallback, false);
856         }
857         else
858                 alert("No provider selected!");
859
860 }
861
862 function addProviderToBouquetCallback(request) {
863         if (request.readyState == 4) {
864                 var result = new SimpleXMLResult(getXML(request));
865                 simpleResultHandler(result);
866                 if(result.getState()){
867                         var selectList = $('bouquetlist');
868                         var selectOptions = selectList.getElementsByTagName('option');
869                         currentBouquetlistIndex =  selectOptions.length ;
870                         getBouquetServiceList();
871                 }
872         }
873 }
874
875 function ProtectionXMLResult(xml){              
876         try{
877                 this.xmlitems = xml.getElementsByTagName("e2serviceprotection").item(0);
878         } catch (e) {
879                 notify("Error parsing e2serviceprotection: " + e, false);
880         }
881         this.configured = getNodeContent(this.xmlitems, 'e2serviceprotectionconfigured', 'False');
882         this.setupepin = getNodeContent(this.xmlitems, 'e2serviceprotectionsetuppin', '');
883         this.type = getNodeContent(this.xmlitems, 'e2serviceprotectiontype', '');
884         this.servicepinactive = getNodeContent(this.xmlitems, 'e2serviceprotectionsetuppinactive', 'False');
885
886         this.getConfigured = function(){
887                 if(this.configured == 'True'){
888                         return true;
889                 }else{
890                         return false;
891                 }
892         };
893         
894         this.getSetupPin = function(){
895                         return this.setupepin;
896         };
897         
898         this.getProtectionType = function(){
899                         return this.type;
900         };
901         
902         this.getSetupPinActive = function(){
903                 if(this.servicepinactive == 'True'){
904                         return true;
905                 }else{
906                         return false;
907                 }
908         };
909         
910 }
911
912 function incommingProtectionData(request) {
913         if (request.readyState == 4) {
914                 var result = new ProtectionXMLResult(getXML(request));
915                 if(result.getConfigured()){
916                         protectionConfigured = true;
917                         protectionType = result.getProtectionType();
918                         
919                         if (result.getSetupPinActive()){
920                                 chachedPin=prompt('Please enter your setup pin:', '');
921                                 if (result.getSetupPin() == chachedPin) {
922                                         protectionPasswordValid = true;
923                                 }
924                                 else
925                                         alert("Password is wrong!");
926                         }
927                         else
928                                 protectionPasswordValid = true;
929                 }
930                 else
931                         protectionPasswordValid = true;
932                         
933                 if (protectionPasswordValid) {
934                         setServiceNav(0);
935                         getBouquets();
936                 }
937         }
938 }
939
940 function getProtectionData() {
941         url = '/bouqueteditor/web/getprotectionsettings';
942         doRequest(url, incommingProtectionData, false);
943 }
944
945
946 function toggleBouquetProtection(selectObj) {
947         var selectList = $(selectObj);
948         var idx = selectList.selectedIndex; 
949         var bouqueref = unescape(selectList.options[idx].value);
950         currentBouquetlistIndex =  idx;
951         doRequest('/bouqueteditor/web/togglelock?sRef='+ bouqueref +'&password=' + chachedPin, toggleBouquetProtectionCallback, false);
952 }
953
954 function toggleBouquetProtectionCallback(request) {
955         if (request.readyState == 4) {
956                 var result = new SimpleXMLResult(getXML(request));
957                 simpleResultHandler(result);
958                 if(result.getState()){
959                         getBouquetServiceList();
960                 }
961         }
962 }
963
964 function toggleServiceProtection(selectObj) {
965         var selectList = $(selectObj);
966         var idx = selectList.selectedIndex; 
967         var serviceref = unescape(selectList.options[idx].value);
968         currentServicelistIndex =  idx;
969         doRequest('/bouqueteditor/web/togglelock?sRef='+ serviceref +'&password=' + chachedPin, toggleServiceProtectionCallback, false);
970 }
971
972 function toggleServiceProtectionCallback(request) {
973         if (request.readyState == 4) {
974                 var result = new SimpleXMLResult(getXML(request));
975                 simpleResultHandler(result);
976                 if(result.getState()){
977                         var ref = getCurrentSelectedBouquetlistRef();
978                         getServiceList(ref);
979                 }
980         }
981 }
982
983 function createBackup() {
984         var filename=prompt('Please enter filename for backup file:', 'webbouqueteditor_backup');
985         if (filename) {
986                 url = '/bouqueteditor/web/backup?Filename='+filename;
987                 doRequest(url, incommingBackupResult, false);
988         }
989 }
990
991 function incommingBackupResult(request) {
992         if (request.readyState == 4) {
993                 var result = new SimpleXMLResult(getXML(request));
994                 if(result.getState()){
995                         startDownloadBackupFile(result.getStateText());
996                 }
997                 else
998                         simpleResultHandler(result);
999         }
1000 }
1001
1002 function startDownloadBackupFile(file) {
1003         var url =  "/bouqueteditor/tmp/" + file;
1004         window.open(url,'Download');
1005 }
1006
1007
1008 function incommingRestoreResult(request) {
1009         if (request.readyState == 4) {
1010                 var result = new SimpleXMLResult(getXML(request));
1011                 simpleResultHandler(result);
1012                 if(result.getState()){
1013                         setMode(MODE.tv);
1014                 }
1015                 else
1016                         simpleResultHandler(result);
1017         }
1018 }
1019
1020 function renameService(selectObj) {
1021         var bouqueref = currentBouquetRef;
1022         var selectList = $(selectObj);
1023         var idx = selectList.selectedIndex;
1024         var refServicelist = "";
1025         if ( idx != -1) {
1026                 var ref = unescape(selectList.options[idx].value); 
1027         
1028                 var newServicename=prompt('Please enter new servicename for selected service:', selectList.options[idx].text);
1029                 if (newServicename){
1030                         var selectServicelistOptions = selectList.getElementsByTagName('option');
1031                         if (selectServicelistOptions.length > 0) {
1032                                 var idxServicelist = selectList.selectedIndex;
1033                                 currentServicelistIndex = idxServicelist;
1034                                 if ( idxServicelist != -1 && idxServicelist != selectServicelistOptions.length){
1035                                         if ( idxServicelist + 1 < selectServicelistOptions.length){
1036                                                 refServicelist = unescape(selectList.options[idxServicelist+1].value);
1037                                         }
1038                                 }
1039                         }
1040                         else
1041                                 currentServicelistIndex = null;
1042                         doRequest('/bouqueteditor/web/renameservice?sBouquetRef='+ bouqueref + '&sRef=' + ref +'&sRefBefore=' + refServicelist + '&newName=' + newServicename, serviceListUpdated, false);
1043                 }
1044         }
1045         else
1046                 alert ("No service selected!");
1047 }
1048
1049 function renameBouquet(selectObj) {
1050         var selectList = $(selectObj);
1051         var idx = selectList.selectedIndex; 
1052         var bouqueref = unescape(selectList.options[idx].value); 
1053         var newServicename=prompt('Please enter new servicename for selected bouquet:', selectList.options[idx].text);
1054         if (newServicename){
1055                 currentBouquetlistIndex =  idx;
1056                 doRequest('/bouqueteditor/web/renameservice?sRef=' + bouqueref +'&mode=' + currentMode + '&newName=' + newServicename, bouquetListUpdated,false);
1057         }
1058 }
1059
1060
1061 function addMarkerToBouquet(selectObj) {
1062         var bouqueref = currentBouquetRef;
1063         var selectList = $(selectObj);
1064         var markername=prompt('Please enter a marker name:', '');
1065         var refServicelist = "";
1066         if (markername) {
1067                 var selectServicelistOptions = selectList.getElementsByTagName('option');
1068                 if (selectServicelistOptions.length > 0) {
1069                         var idxServicelist = selectList.selectedIndex;
1070                         currentServicelistIndex = idxServicelist + 1;
1071                         if ( idxServicelist != -1 && idxServicelist != selectServicelistOptions.length){
1072                                 if ( idxServicelist + 1 < selectServicelistOptions.length){
1073                                         refServicelist = unescape(selectList.options[idxServicelist+1].value);
1074                                 }
1075                         }
1076                 }
1077                 else
1078                         currentServicelistIndex = null;
1079                 doRequest('/bouqueteditor/web/addmarkertobouquet?sBouquetRef='+ bouqueref + '&Name=' + markername +'&sRefBefore=' + refServicelist, serviceListUpdated, false);
1080         }
1081 }
1082
1083 function fileUpload(form, action_url)
1084 {
1085         if($('file').value.trim() == ""){
1086                 notify("Please select a File to restore!");
1087                 return;
1088         }
1089         // Create the iframe...
1090         var iframe = document.createElement("iframe");
1091         iframe.setAttribute("id","upload_iframe");
1092         iframe.setAttribute("name","upload_iframe");
1093         iframe.setAttribute("width","0");
1094         iframe.setAttribute("height","0");
1095         iframe.setAttribute("border","0");
1096         iframe.setAttribute("style","width: 0; height: 0; border: none;");
1097
1098         // Add to document...
1099         form.parentNode.appendChild(iframe);
1100         window.frames['upload_iframe'].name="upload_iframe";
1101         iframe = $(iframe);
1102
1103
1104         // Add event...
1105         var eventHandler = function(){
1106                 // Message from server...
1107                 var resdoc = "";
1108
1109                 if (iframe.contentDocument) {
1110                                 resdoc = iframe.contentDocument;
1111                 } else if (iframe.contentWindow) {
1112                                 resdoc = iframe.contentWindow.document;
1113                 } else if (iframe.document) {
1114                                 resdoc = iframe.document;
1115                 }
1116
1117                 var result = new SimpleXMLResult(resdoc);
1118
1119                 if (result.getState()){
1120                         doRequest('/bouqueteditor/web/restore?Filename=' + result.getStateText() , incommingRestoreResult, false);
1121                 } else {
1122                         simpleResultHandler(result);
1123                 }
1124                 
1125         try{
1126                         // unregister Eventhandler
1127                         iframe.stopObserving();
1128                         // remove iframe
1129                         iframe.parentNode().removeChild(iframe);
1130                 } catch(e){return;}
1131     };
1132         
1133         iframe.observe("load", eventHandler);
1134         // Set properties of form...
1135         form.setAttribute("target","upload_iframe");
1136         form.setAttribute("action", action_url);
1137         form.setAttribute("method","post");
1138         form.setAttribute("enctype","multipart/form-data");
1139         form.setAttribute("encoding","multipart/form-data");
1140
1141         // Submit the form...
1142         form.submit();
1143
1144         notify("Uploading...");
1145 }