﻿        window.addEvent('domready', function(){

	        var urlx = encodeURI('http://www.operaen.no/');
	        var nosimple = ['1132'];
	        
	        var login = ($('entaLogin') == null) ? false : true;
	        if (login) {
	            getEntaLogin('');
	        }
	        
	        var baboo = ($('EntaBasketXS') == null) ? false : true;
	        if (baboo) {
	            getBasket();
	        }

	        var elboo = ($('EventList') == null) ? false : true;
	        if (elboo) {
                var shoid = ($('EventList').get('title') == null) ? '' : $('EventList').get('title');
	            getEventsByShow(shoid);
	        }
	        
	        var elxboo = ($('EventListXL') == null) ? false : true;
	        if (elxboo) {
                var filterby = ($('EventListXL').get('title') == null) ? '' : $('EventListXL').get('title');
                if (filterby == ''){
                    var tod = new Date();
                    filterby = 'filterby=monthyear=' +  (tod.getMonth()+1+100).toString().substring(1,3) + tod.getFullYear().toString();
                }
                filterby += ' AND ShoID not in(64,784)';
	            getEventListXL(filterby.replace(' ', '%20'));
	        }
	        
	        var caboo = ($('Calendar') == null) ? false : true;
	        if (caboo) {
	            getCalendar();
	        }
	        
			// getEventsToday();
            function getCalendar()
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnocalendar', 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('Calendar').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('Calendar').adopt(html);
                        //Set the event handlers.
                        enumTriggers();
                    },
                    //onFailure: function() {$('Calendar').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnocalendar</p>');}//
                    onFailure: function() {$('Calendar').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('ProductionListXS').innerHTML = ('<img src="images/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            function getCalendar(qs)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnocalendar&'+qs, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('Calendar').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('Calendar').adopt(html);
                        enumTriggers();
                    },
                    //onFailure: function() {$('Calendar').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnocalendar&'+qs+'</p>');}//
                    onFailure: function() {$('Calendar').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('Calendar').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            function getEventsToday()
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnoevetinybydate_en', 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('ProductionListXS').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('ProductionListXS').adopt(html);
						getEventsByDateTriggers();
                    },
                    //onFailure: function() {$('ProductionListXS').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnoevetinybydate</p>');}//
                    onFailure: function() {$('ProductionListXS').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('ProductionListXS').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            function getEventsByDate(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnoevetinybydate_en&fromdate='+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('ProductionListXS').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('ProductionListXS').adopt(html);
						getEventsByDateTriggers();
                    },
                    //onFailure: function() {$('ProductionListXS').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnoevetinybydate_en</p>');}//
                    onFailure: function() {$('ProductionListXS').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('ProductionListXS').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            function getEventsByShow(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnoevetinybyshow_en&shoid='+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('EventList').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('EventList').adopt(html);
						getEventsByShowTriggers();
                    },
                    //onFailure: function() {$('EventList').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnoevetinybyshow_en&shoid='+id+'</p>');}//
                    onFailure: function() {$('EventList').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('EventList').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            function getEventListXL(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnoevefull_en&rows=0&'+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('EventListXL').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('EventListXL').adopt(html);
						getEventListXLTriggers();
                    },
                    //onFailure: function() {$('EventListXL').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnoevefull_en&rows=0'+id+'</p>');}//
                    onFailure: function() {$('EventListXL').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('EventListXL').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }

            function getPriceBands(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnopricebands_en&perindex='+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('Prices').set('text', '');
                        $('PriceBands').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('PriceBands').adopt(html);
                        var prg = ($('PriceBands').getFirst('div').get('id') == null) ? '' : $('PriceBands').getFirst('div').get('id');
                        if (prg != '') {
                            getPrices('perindex=' + id + '&mclid' + prg);
                        } else {
                            $('Prices').set('text', '');
                        }
						//getEventListXLTriggers();
                    },
                    //onFailure: function() {$('PriceBands').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnopricebands_en&perindex='+id+'</p>');}//
                    onFailure: function() {$('PriceBands').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('PriceBands').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            
            function getPrices(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnoprices_en&'+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('Prices').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('Prices').adopt(html);
                        if ($('Prices').innerHTML == ''){
                            $('Prices').set('text', 'Ingen rabatter tilgjengelig for nettsalg.');
                        }
						OfferBestSeats();
                    },
                    //onFailure: function() {$('Prices').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnopricebands_en&perindex='+id+'</p>');}//
                    onFailure: function() {$('Prices').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('Prices').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            
            function getBestSeats(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnoofferbestseats_en&'+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('Prices').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('Prices').adopt(html);
                        if ($('Prices').innerHTML != '0'){
                            window.location=('http://www.operaen.no/Default.aspx?ID=27382&QL=GBasicBasket.aspx');
                        } else {
                            $('Prices').set('text', 'Kunne ikke legge ønsket kombinasjon av billetter i handlekurven! Vennligst klikk på forestilling igjen, og forsøk å endre dine angitte kriterier.');
                        }
						//OfferBestSeats();
                    },
                    //onFailure: function() {$('Prices').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnoofferbestseats_en&'+id+'</p>');}//
                    onFailure: function() {$('Prices').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('Prices').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
            
            function getBasket()
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnotinybasket_en', 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('EntaBasketXS').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('EntaBasketXS').adopt(html);
						getTinyBasketTriggers();
                    },
                    //onFailure: function() {$('EntaBasketXS').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnotinybasket_en</p>');}//
                    onFailure: function() {$('EntaBasketXS').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('EntaBasketXS').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }

            function getEntaLogin(id)
            {
                var req = new Request.HTML({url:urlx + 'bpElementProvider.aspx?template=dnocustomerlogon_en&'+id, 
                    onSuccess: function(html) {
                        //Clear the text currently inside the results div.
                        $('entaLogin').set('text', '');
                        //Inject the new DOM elements into the results div.
                        $('entaLogin').adopt(html);
						getEntaLoginTriggers();
                    },
                    //onFailure: function() {$('entaLogin').set('text', '<p>Cant load: '+ urlx + 'bpElementProvider.aspx?template=dnocustomerlogon_en</p>');}//
                    onFailure: function() {$('entaLogin').set('text', '<p>Server er opptatt: Vennligst forsøk på nytt!/Server busy: please try again!</p>');}
                });
                $('entaLogin').innerHTML = ('<img src="Files/System/Searching.gif" alt="Leter i databasen" />');
                req.send();
            }
	// --
            function enumTriggers()
            {
                $('Calendar').getElements('td').addEvent('click',function(e){
                    e.stop();
                   var id = (this.get('id') == null) ? '' : this.get('id');
				   $('Calendar').getElements('td').set('style', '');
				   this.set('style', 'color: White; background-color: rgb(247, 95, 19)');
				   getEventsByDate(id);
                });
                $('Calendar').getElements('th').addEvent('click',function(e){
                    e.stop();
                   var id = (this.get('id') == null) ? '' : this.get('id');
                   getEventlist(id);
                   //var name = this.get('name');
                   //document.URL=('http://billettsystem.billettportalen.no/bp/test.aspx?'+id);
                });
                $('Calendar').getElements('span').addEvent('click',function(e){
                    e.stop();
                    var id = (this.get('id') == null) ? '' : this.get('id');
                    getCalendar('fromdate='+id);
                });
            }

            function getEventsByDateTriggers()
            {
                $('ProductionListXS').getFirst('div').getElements('div').addEvent('click',function(e){
                    e.stop();
                    var id = (this.get('id') == null) ? '' : this.get('id');
                    
                    // Reset all prev. Style settings.
                    $('ProductionListXS').getFirst('div').getElements('div').set('style', '');
                    
                    // Does the performance have Seating Plan access?
                    var x = id.indexOf('{1}');
	                var noseatplan = (x == -1) ? true : false;
	                
                    // Is simple booking allowed?
                    var simplebooking = true;
                    nosimple.each(function(item, index){
                        if (item == per) {simplebooking=false;}
                    });
                    
                    // Check if PriceBands and Prices exists on the page.
	                var pr = ($('PriceBands') == null) ? false : true;
	                var pb = ($('Prices') == null) ? false : true;

	                if (pr && pb && noseatplan && simplebooking) {//(pb && pr && simplebooking) {
                        var per = (this.get('title') == null) ? '' : this.get('title');
                        this.set('style', 'background-color: #ff6600; color: #FFFFFF;');
                        getPriceBands(per);
	                } else {
					    id = id.replace('{0}', 'EventPrices.aspx').replace('{1}', 'EventFlashPlan.aspx').replace('+', '&');
					    window.location=('http://www.operaen.no/Default.aspx?ID=27382&'+id)
	                }
                });
            }
            
            function getEventsByShowTriggers()
            {
                $('EventList').getFirst('div').getElements('div').addEvent('click',function(e){
                    e.stop();
                    var id = (this.get('id') == null) ? '' : this.get('id');
                    
                    // Reset all prev. Style settings.
                    $('EventList').getFirst('div').getElements('div').set('style', '');
                    
                    // Does the performance have Seating Plan access?
                    var x = id.indexOf('{1}');
	                var noseatplan = (x == -1) ? true : false;
	                
                    // Is simple booking allowed?
                    var simplebooking = true;
                    nosimple.each(function(item, index){
                        if (item == per) {simplebooking=false;}
                    });
                    
                    // Check if PriceBands and Prices exists on the page.
	                var pr = ($('PriceBands') == null) ? false : true;
	                var pb = ($('Prices') == null) ? false : true;

	                if (pr && pb && noseatplan && simplebooking) {//(pb && pr && simplebooking) {
                        var per = (this.get('title') == null) ? '' : this.get('title');
                        this.set('style', 'background-color: #ff6600; color: #FFFFFF;');
                        getPriceBands(per);
	                } else {
					    id = id.replace('{0}', 'EventPrices.aspx').replace('{1}', 'EventFlashPlan.aspx').replace('+', '&');
					    window.location=('http://www.operaen.no/Default.aspx?ID=27382&'+id)
	                }
                });
            }

            function getEventListXLTriggers()
            {
                $('EventListXL').getElements('span').addEvent('click',function(e){
                    e.stop();
                    var id = (this.get('id') == null) ? '' : this.get('id');
					id = id.replace('{0}', 'EventPrices.aspx').replace('{1}', 'EventFlashPlan.aspx').replace('+', '&');
					window.location=('http://www.operaen.no/Default.aspx?ID=27382&'+id)
                });
            }

            function OfferBestSeats()
            {
                $('offertickets').addEvent('click',function(e){
                    e.stop();
                    var tics = 0;
                    var ticstr = '';
                    $('Prices').getElements('input').each(function(item, index){
    	                var val = (item.get('value') == null) ? '' : item.get('value');
    	                if (val != '') {
    	                    tics = tics + item.get('value')*1;
    	                    ticstr += '|' + item.get('value') + item.get('id')
    	                }
                    });
                    var per = ($('Prices').getFirst('div').get('id') == null) ? '' : $('Prices').getFirst('div').get('id');
                    var prg = ($('PriceBands').getFirst('div').get('id') == null) ? '' : $('PriceBands').getFirst('div').get('id');
                    if (tics>0) {
                        getBestSeats('perindex='+per+'&seatstr='+ticstr.substring(1,ticstr.length)+'&mclid='+prg);
                    } else {
                        alert('Angi antall billetter!');
                    }
                });
            }

            function getEntaLoginTriggers()
            {
                $('entaLogin').getElements('div').addEvent('click',function(e){
                    e.stop();
                    var id = (this.get('id') == null) ? '' : this.get('id');
                    if (id == 'userlogin'){
                        getEntaLogin('emuemail='+$('userid').get('value')+'&emupwd='+$('userpwd').get('value'));
                    }
                    if (id == 'userlogout'){
                        getEntaLogin('agtrefno=0');
                    }
                    if (id == 'useredit'){
					    window.location=('http://www.operaen.no/Default.aspx?ID=27382&QL=GNewEmailLogon.aspx')
                    }
                    if (id == 'usernew'){
					    window.location=('http://www.operaen.no/Default.aspx?ID=27382&QL=GNewEmailLogon.aspx')
                    }
                    if (id == 'usersendpwd'){
					    window.location=('http://www.operaen.no/Default.aspx?ID=27382&QL=GPasswordRequest.aspx')
                    }
                    if (id == 'userbookings'){
					    window.location=('http://www.operaen.no/Default.aspx?ID=27382&QL=GBookingList.aspx')
                    }
                });
            }

            function getTinyBasketTriggers()
            {
                $('EntaBasketXS').getElements('a').addEvent('click',function(e){
                    e.stop();
					window.location=('http://www.operaen.no/Default.aspx?ID=27382&QL=GBasicBasket.aspx')
                });
            }
            
});
