// On document load
function domLoad(i){
    var u = navigator.userAgent;
    var e =/*@cc_on!@*/ false;
    var st = setTimeout;
    if (/webkit/i.test(u)) {
        st(function(){
            var dr = document.readyState;
            if (dr == "loaded" || dr == "complete") {
                i();
            } else {
                st(arguments.callee, 10);
            }
        }, 10);
    } else if (((/mozilla/i.test(u) && !(/(compati)/.test(u)))) || (/opera/i.test(u))) {
        document.addEventListener("DOMContentLoaded", i, false);
    } else if (e) {
        (function(){
            var t = document.createElement('doc:rdy');
            try {
                t.doScroll('left');
                i();
                t = null;
            } catch (e) {
                st(arguments.callee, 0);
            }
        })();
    } else {
        window.onload = i;
    }
}

function bannerDemo(){
    if (!document.getElementById('demoBanner')) {
        return false;
    }
    var banner = document.getElementById('demoBanner');
    var button = banner.getElementsByTagName('a')[0];
    
    button.onclick = function(){
        var box = document.getElementById('demoBox');
        var bg = document.getElementById('demoBoxBg');
        
        box.style.display = "block";
        
        bg.style.display = "block";
        
        function showDemo(){
            var demo = document.getElementById('demo');
            demo.style.display = "block";
            document.documentElement.style.overflow = 'hidden';
            document.body.scroll = "no";
        }
        setTimeout(showDemo, 1000);
        return false;
    };
}

function bannerAccordion(){
    /**
     * Three states - start, open and closed
     */
    // Check if afBanner exist
    if (!document.getElementById('afBanner')) {
        return false;
    }
    
    var banner = document.getElementById('afBanner');
    var bannerChildren = banner.getElementsByTagName('div');
    
    // For top and bottom menu, 1 is top, 2 is bottom
    var idFlag = 1;
    
    // Preload background image
    var preloadImage = [];
    preloadImage[0] = new Image;
    preloadImage[0].src = "/caring119static/img_root/bg-banner-menu-sim-closed.jpg";
    preloadImage[1] = new Image;
    preloadImage[1].src = "/caring119static/img_root/bg-banner-menu-invoice-closed.jpg";
    
    for (var i = 0; i < bannerChildren.length; i++) {
        // Continue next loop if element has no class
        if (!bannerChildren[i].className) {
            continue;
        }
        
        // If it has class switch check between className
        switch (bannerChildren[i].className) {
            case "bannerMenu":
                var bannerMenu = bannerChildren[i];
                var defaultHeight = bannerMenu.offsetHeight;
                
                // Set id name for futher reference
                if (idFlag === 1) {
                    bannerMenu.setAttribute('id', 'bannerMenuTop');
                } else {
                    bannerMenu.setAttribute('id', 'bannerMenuBottom');
                }
                
                // Set default height: start state
                bannerMenu.style.height = "160px";
                bannerMenu.style.overflow = "hidden";
                
                break;
                
            case "bannerButton":
                var bannerButton = bannerChildren[i];
                var button = bannerButton.getElementsByTagName('a')[0];
                
                // Set id name for futher reference
                if (idFlag === 1) {
                    bannerButton.setAttribute('id', 'bannerMenuTopButton');
                } else {
                    bannerButton.setAttribute('id', 'bannerMenuBottomButton');
                }
                
                // Set default postion: start state
                bannerButton.style.top = "60px";
                
                // Create Buttons
                var accordBtnImg = document.createElement('img');
                accordBtnImg.setAttribute('src', '/caring119static/img_root/btn-banner-chiuso.gif');
                button.insertBefore(accordBtnImg, button.firstChild);
                
                // Assign button as property for it's menu
                bannerMenu.button = button;
                
                bannerMenu.button.onclick = function(){
                    /**
                     * @Open
                     * locate and find needed elements
                     * set elements to default or at initial value
                     */
                    // Current event parent
                    var bannerMenu = this.parentNode.parentNode;
                    var bannerMenuId = bannerMenu.getAttribute('id');
                    
                    // Top and bottom menu
                    var bannerMenuTop = document.getElementById('bannerMenuTop');
                    var bannerMenuBottom = document.getElementById('bannerMenuBottom');
                    
                    // Fade elements
                    var bannerTop = document.getElementById('bannerTop');
                    var bannerBottom = document.getElementById('bannerBottom');
                    
                    // Set ul navigation as block
                    this.bannerNav.style.display = "block";
                    
                    // Buttons
                    var topButton = document.getElementById('bannerMenuTopButton').getElementsByTagName('a')[0];
                    var bottomButton = document.getElementById('bannerMenuBottomButton').getElementsByTagName('a')[0];
                    var topButtonImg = topButton.getElementsByTagName('img')[0];
                    var bottomButtonImg = bottomButton.getElementsByTagName('img')[0];
                    
                    // Reset menu to start state
                    // Everything here are set to default state
                    if (bannerMenu.offsetHeight == 240) {
                        fade('bannerTop', 0, 5);
                        fade('bannerBottom', 0, 5);
                        
                        animeButton(60, 60);
                        
                        this.bannerNav.style.display = "none";
                        
                        bannerMenuTop.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-sim-start.jpg)";
                        bannerMenuBottom.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-invoice-start.jpg)";
                        
                        topButton.style.color = "#FFFFFF";
                        topButtonImg.setAttribute('src', '/caring119static/img_root/btn-banner-chiuso.gif');
                        bottomButton.style.color = "#FFFFFF";
                        bottomButtonImg.setAttribute('src', '/caring119static/img_root/btn-banner-chiuso.gif');
                    }
                    
                    // Find which menu is been clicked
                    if (bannerMenuId.indexOf('Top') !== -1) {
                        // Set as default height
                        if (bannerMenu.offsetHeight == 240) {
                            constantAnime(bannerMenuId, 160);
                            constantAnime('bannerMenuBottom', 160);
                            
                        // Open top menu and close button menu
                        } else {
                            constantAnime(bannerMenuId, 240);
                            constantAnime('bannerMenuBottom', 80);
                            
                            fade('bannerTop', 1, 5);
                            fade('bannerBottom', 0, 5);
                            
                            animeButton(15, 15);
                            
                            bannerTop.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-sim-open.jpg)";
                            bannerMenuBottom.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-invoice-closed.jpg)";
                            
                            // switch button
                            topButton.style.color = "#FFFFFF";
                            topButtonImg.setAttribute('src', '/caring119static/img_root/btn-banner-aperto.gif');
                            bottomButton.style.color = "#CCCCCC";
                            bottomButtonImg.setAttribute('src', '/caring119static/img_root/btn-banner-chiuso.gif');
                        }
                    } else {
                        // Set as default height
                        if (bannerMenu.offsetHeight == 240) {
                            constantAnime(bannerMenuId, 160);
                            constantAnime('bannerMenuTop', 160);
                            
                        // Open bottom menu and close top menu
                        } else {
                            constantAnime(bannerMenuId, 240);
                            constantAnime('bannerMenuTop', 80);
                            
                            fade('bannerTop', 0, 5);
                            fade('bannerBottom', 1, 5);
                            
                            animeButton(15, 15);
                            
                            bannerMenuTop.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-sim-closed.jpg)";
                            bannerBottom.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-invoice-open.jpg)";
                            
                            // switch button
                            topButton.style.color = "#CCCCCC";
                            topButtonImg.setAttribute('src', '/caring119static/img_root/btn-banner-chiuso.gif');
                            bottomButton.style.color = "#FFFFFF";
                            bottomButtonImg.setAttribute('src', '/caring119static/img_root/btn-banner-aperto.gif');
                        }
                    }
                    
                    return true;
                };
                break;
                
            case "bannerFadeElem":
                var bannerFadeElem = bannerChildren[i];
                var bannerNav = bannerFadeElem.getElementsByTagName('ul')[0];
                bannerMenu.button.bannerNav = bannerNav;
                
                // Set id name for futher reference
                if (idFlag === 1) {
                    bannerFadeElem.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-sim-start.jpg)";
                    bannerNav.setAttribute('id', 'bannerTopNav');
                } else {
                    bannerFadeElem.style.backgroundImage = "url(/caring119static/img_root/bg-banner-menu-invoice-start.jpg)";
                    bannerNav.setAttribute('id', 'bannerBottomNav');
                }
                
                // IE6 bug fix for absolute elements
                bannerFadeElem.style.height = defaultHeight + "px";
                
                bannerNav.style.display = "none";
                idFlag++;
                break;
        }
    }
    
    return true;
}

function fade(element, totalAlpha, speed){
    var elem = document.getElementById(element);
    
    if (elem.fade) {
        clearTimeout(elem.fade);
    }
    
    if (!currentAlpha) {
        if (!elem.style.opacity) {
            elem.style.opacity = 0;
        }
        
        if (!elem.style.MozOpacity) {
            elem.style.MozOpacity = 0;
        }
        
        if (!elem.style.filter) {
            elem.style.filter = "alpha(opacity=0)";
        }
    }
    
    var currentAlpha = elem.style.opacity;
    
    if (currentAlpha == totalAlpha || Math.round(currentAlpha * 100) == (totalAlpha * 100)) {
        clearTimeout(elem.fade);
        return false;
    }
    
    if (currentAlpha < totalAlpha || (currentAlpha * 100) < (totalAlpha * 100)) {
        elem.style.opacity = (currentAlpha * 1) + 0.05;
        elem.style.MozOpacity = (currentAlpha * 1) + 0.05;
        elem.style.filter = "alpha(opacity=" + (Math.ceil(currentAlpha * 100) + 5) + ")";
    }
    
    if (currentAlpha > totalAlpha || (currentAlpha * 100) > (totalAlpha * 100)) {
        elem.style.opacity = (currentAlpha * 1) - 0.1;
        elem.style.MozOpacity = (currentAlpha * 1) - 0.1;
        elem.style.filter = "alpha(opacity=" + (Math.ceil(currentAlpha * 100) - 10) + ")";
    }
    
    
    var repeat = "fade('" + element + "'," + totalAlpha + "," + speed + ")";
    // Speed
    elem.fade = setTimeout(repeat, speed);
}

function animeButton(top, bottom){
    var topButton = document.getElementById('bannerMenuTopButton');
    var bottomButton = document.getElementById('bannerMenuBottomButton');
    
    
    if (!topButton.style.top) {
        var currentTopPos = 0;
    } else {
        var currentTopPos = parseInt(topButton.style.top);
    }
    
    if (!bottomButton.style.top) {
        var currentBottomPos = 0;
    } else {
        var currentBottomPos = parseInt(bottomButton.style.top);
    }
    
    if (currentTopPos < top || currentBottomPos < bottom) {
        currentTopPos += 5;
        currentBottomPos += 5;
    }
    
    if (currentTopPos > top || currentBottomPos > bottom) {
        currentTopPos -= 5;
        currentBottomPos -= 5;
    }
    
    if (Math.round(currentTopPos) === top || Math.round(currentBottomPos) === bottom) {
        clearTimeout(topButton.anime);
        return true;
    }
    
    topButton.style.top = currentTopPos + "px";
    bottomButton.style.top = currentBottomPos + "px";
    
    var repeat = "animeButton(" + top + "," + bottom + ")";
    // Speed
    topButton.anime = setTimeout(repeat, 5);
}

function constantAnime(element, totalHeight){
    var node = document.getElementById(element);
    
    // Clear last action
    if (node.anime) {
        clearTimeout(node.anime);
    }
    
    // Get current height
    if (!node.style.height) {
        var currentHeight = 0;
    } else {
        var currentHeight = parseInt(node.style.height);
    }
    
    // Distance
    if (currentHeight < totalHeight) {
        currentHeight += 10;
    }
    if (currentHeight > totalHeight) {
        currentHeight -= 10;
    }
    
    // Set height
    node.style.height = currentHeight + 'px';
    
    // Stop animation
    if (currentHeight == totalHeight) {
        clearTimeout(node.anime);
        return true;
    } else if (currentHeight === 0) {
        clearTimeout(node.anime);
        return true;
    }
    
    var repeat = "constantAnime('" + element + "'," + totalHeight + ")";
    // Speed
    node.anime = setTimeout(repeat, 5);
}

function preloadImage(parent){
    if (!parent) {
        return false;
    }
    var img = parent.getElementsByTagName('img')[0];
    var imgSrc = img.getAttribute('src');
    
    var preloadImg = document.createElement('img');
    preloadImg.style.display = "none";
    preloadImg.setAttribute('src', imgSrc.replace(/_off/, '_on'));
    
    document.body.appendChild(preloadImg);
}


function accordion(){
    // Get menu
    if (!document.getElementsByTagName('ul')) {
        return false;
    }
    var accordionMenus = document.getElementsByTagName('ul');
    
    // Flag for bottom-border(1 border only)
    var imgFlag = 1;
    
    // Get individual menu
    for (var i = 0; i < accordionMenus.length; i++) {
    
        // Filter menu, if not matched go to next loop
        if (accordionMenus[i].className != "accordionMenu") {
            continue;
        }
        // Filter menu children
        // To next if menu contains no list
        if (!accordionMenus[i].getElementsByTagName('li')) {
            continue;
        }
        
        // Assign menu children to specific menu
        // Thus, each menu has it's own children
        var children = accordionMenus[i].getElementsByTagName('li');
        
        
        // Loop through menu list
        for (var j = 0; j < children.length; j++) {
        
            // Filter class name
            switch (children[j].className) {
                // Assign every list to it's parent menu
                // Since event must be invoke on menu title(accordionTop)
                // All the list are assigned to accordionTop
                // So we could get trace it's properties when invoked by a function
                case "accordionTop":
                    accordionMenus[i].accordionTop = children[j];
                    accordionMenus[i].accordionTop.topBorder = children[j].getElementsByTagName('img')[0];
                    // Specify node: children[j].setAttribute('id', 'accordionMid' + i);
                    accordionMenus[i].accordionTop.i = i;
                    
                    accordionMenus[i].accordionTop.style.height = (accordionMenus[i].accordionTop.offsetHeight + 8) + 'px';
                    accordionMenus[i].accordionTop.style.margin = '0 0 -8px 0';
                    
                    // Preload images
                    preloadImage(children[j]);
                    
                    break;
                    
                case "accordionMid":
                    accordionMenus[i].accordionTop.accordionMid = children[j];
                    // Assign unique id for animation
                    children[j].setAttribute('id', 'accordionMid' + i);
                    // Close all list so they are ready to be opened
                    children[j].style.height = "0px";
                    children[j].style.overflow = "hidden";
                    break;
                    
                case "accordionSub":
                    accordionMenus[i].accordionTop.accordionSub = children[j];
                    accordionMenus[i].accordionTop.accordionSub.subBoderder = children[j].getElementsByTagName('img')[0];
                    
                    // Preload images
                    if (imgFlag == 1) {
                        preloadImage(children[j]);
                        imgFlag++;
                    }
            }
            
        }
        
        // Check menu elements, top, mid and bottom
        if (accordionMenus[i].accordionTop &&
        accordionMenus[i].accordionTop.accordionMid &&
        accordionMenus[i].accordionTop.accordionSub) {
        
            // Set Background
            accordionMenus[i].accordionTop.onmouseover = function(){
                // Exit if menu is opened
                var title = this.getElementsByTagName('h1')[0];
                title.style.color = "#FFFFFF";
                this.topBorder.setAttribute('src', this.topBorder.getAttribute('src').replace(/_off/, '_on'));
                this.accordionSub.subBoderder.setAttribute('src', this.accordionSub.subBoderder.getAttribute('src').replace(/_off/, '_on'));
                return false;
            };
            
            // Set Background
            accordionMenus[i].accordionTop.onmouseout = function(){
                if (this.accordionMid.style.height == 'auto' ||
                parseInt(this.accordionMid.style.height) > 0) {
                    return false;
                }
                var title = this.getElementsByTagName('h1')[0];
                title.style.color = "#003E7A";
                this.topBorder.setAttribute('src', this.topBorder.getAttribute('src').replace(/_on/, '_off'));
                this.accordionSub.subBoderder.setAttribute('src', this.accordionSub.subBoderder.getAttribute('src').replace(/_on/, '_off'));
                return false;
            };
            
            accordionMenus[i].accordionTop.onclick = function(){
                // Close all menu
                var accordionMids = document.getElementsByTagName('li');
                for (var k = 0; k < accordionMids.length; k++) {
                    if (accordionMids[k].className == 'accordionMid') {
                        // Check if height values are in between
                        if (accordionMids[k].getAttribute('id') != ('accordionMid' + this.i) &&
                        (accordionMids[k].style.height == 'auto' ||
                        parseInt(accordionMids[k].style.height) > 0 ||
                        parseInt(accordionMids[k].style.height) == 0)) {
                        
                        
                            var lists = accordionMids[k].parentNode.getElementsByTagName('li');
                            for (var l = 0; l < lists.length; l++) {
                                switch (lists[l].className) {
                                    case 'accordionTop':
                                        var imgTopBorder = lists[l].getElementsByTagName('img')[0];
                                        var title = lists[l].getElementsByTagName('h1')[0];
                                        imgTopBorder.setAttribute('src', imgTopBorder.getAttribute('src').replace(/_on/, '_off'));
                                        title.style.color = "#003E7A";
                                        break;
                                        
                                    case 'accordionSub':
                                        var imgSubBorder = lists[l].getElementsByTagName('img')[0];
                                        imgSubBorder.setAttribute('src', imgSubBorder.getAttribute('src').replace(/_on/, '_off'));
                                        break;
                                }
                            }
                            
                            anime(accordionMids[k].getAttribute('id'), 0);
                            // Set button height
                            this.style.height = '43px';
                            this.style.margin = '0 0 -8px 0';
                        }
                    }
                }
                
                if (this.accordionMid.style.height == '0px') {
                
                    // Set Background
                    this.topBorder.setAttribute('src', this.topBorder.getAttribute('src').replace(/_off/, '_on'));
                    this.accordionSub.subBoderder.setAttribute('src', this.accordionSub.subBoderder.getAttribute('src').replace(/_off/, '_on'));
                    
                    // Set button height
                    this.style.height = '35px';
                    this.style.margin = '0';
                    
                    // Get Height
                    this.accordionMid.style.height = "auto";
                    var totalHeight = this.accordionMid.offsetHeight;
                    this.accordionMid.style.height = "0px";
                    
                    // Open menu
                    anime('accordionMid' + this.i, totalHeight);
                } else {
                    // Close Menu
                    var currentTitle = this.getElementsByTagName('h1')[0];
                    currentTitle.style.color = "#003E7A";
                    this.topBorder.setAttribute('src', this.topBorder.getAttribute('src').replace(/_on/, '_off'));
                    this.accordionSub.subBoderder.setAttribute('src', this.accordionSub.subBoderder.getAttribute('src').replace(/_on/, '_off'));
                    anime('accordionMid' + this.i, 0);
                    
                    // Set button height
                    this.style.height = '43px';
                    this.style.margin = '0 0 -8px 0';
                }
                
                return false;
            };
        }
    }
}

function anime(element, totalHeight){
    var node = document.getElementById(element);
    
    // Clear last action
    if (node.anime) {
        clearTimeout(node.anime);
    }
    
    // Get current height
    if (!node.style.height) {
        var currentHeight = 0;
    } else if (node.style.height == "auto") {
        var currentHeight = node.offsetHeight;
    } else {
        var currentHeight = parseInt(node.style.height);
    }
    
    // Distance
    if (currentHeight < totalHeight) {
        var distance = Math.ceil((totalHeight - currentHeight) / 3);
        currentHeight += distance;
    }
    if (currentHeight > totalHeight) {
        var distance = Math.ceil((totalHeight + currentHeight) / 3);
        currentHeight -= distance;
    }
    
    // Set height
    node.style.height = currentHeight + 'px';
    
    // Stop animation
    if (currentHeight > 0 && totalHeight > 0 && currentHeight == totalHeight) {
        node.style.height = "auto";
        clearTimeout(node.anime);
        return true;
    } else if (currentHeight === 0) {
        clearTimeout(node.anime);
        return true;
    }
    
    var repeat = "anime('" + element + "'," + totalHeight + ")";
    // Speed
    node.anime = setTimeout(repeat, 50);
}

function setBox(parent, element, name, getState, set, choose){
    var nodes = parent.getElementsByTagName(element);
    var flag = 0;
    
    for (var i = 0; i < nodes.length; i++) {
        if (!nodes[i].className && nodes[i].className != name) {
            continue;
        }
        
        // Check box: open
        if (getState) {
            if (nodes[i].style.display == 'block') {
                ++flag;
            }
        } else if (set) {
            // Switch button image
            if (nodes[i].getElementsByTagName('img')[0]) {
                var img = nodes[i].getElementsByTagName('img')[0];
                if (choose == 'open') {
                    img.setAttribute('src', '/caring119static/img_root/btn_espandi_chiudi.gif');
                } else {
                    img.setAttribute('src', '/caring119static/img_root/btn_espandi_apri.gif');
                }
                // Toggle box
            } else {
                if (choose == 'open') {
                    nodes[i].style.display = 'block';
                } else {
                    nodes[i].style.display = 'none';
                }
            }
            // Check box: exist
        } else {
            ++flag;
        }
        
    }
    
    return flag;
}

function toggle(){

    // If no list exit
    if (!document.getElementsByTagName('li')) {
        return false;
    }
    var accordionMids = document.getElementsByTagName('li');
    
    for (var i = 0; i < accordionMids.length; i++) {
        // Find all list with accordionMid class
        if (!accordionMids[i].className &&
        accordionMids[i].className != 'accordionMid') {
            continue;
        }
        var accordionMid = accordionMids[i];
        
        // Flag for creating expandiTutto(quantity: one / each accordionMid)
        var listeFlag = 0;
        
        // List
        var espandiListe = accordionMid.getElementsByTagName('dl');
        
        // bg switch
        var bgFlag = 1;
        for (var j = 0; j < espandiListe.length; j++) {
            // Filter definition list, if not matched go to next loop
            if (!espandiListe[j].className && espandiListe[j].className != 'espandiLista') {
                continue;
            }
            // Assign espandi list
            var espandiLista = espandiListe[j];
            
            // If at least one if found increase listeFlag
            listeFlag++;
            
            // If at least one listeFlag is found create a dynamic button for espandi tutto
            if (listeFlag == 1) {
                // Create button
                var espandiTutto = document.createElement('a');
                espandiTutto.setAttribute('class', 'espandiTutto');
                espandiTutto.setAttribute('href', '#');
                var espandiTuttoImg = document.createElement('img');
                espandiTuttoImg.src = "/caring119static/img_root/btn_espandi_tutto.gif";
                espandiTuttoImg.setAttribute('alt', 'espandi tutto');
                espandiTuttoImg.setAttribute('title', 'espandi tutto');
                espandiTutto.appendChild(espandiTuttoImg);
                
                // Insert button
                espandiLista.parentNode.insertBefore(espandiTutto, espandiLista.parentNode.firstChild);
                
                espandiTutto.espandiTuttoImg = espandiTuttoImg;
                
                listeFlag++;
            }
            
            // Get definition title
            var espandiBtns = espandiLista.getElementsByTagName('dt');
            
            for (var k = 0; k < espandiBtns.length; k++) {
                // Filter definition title, if not matched go to next loop
                if (!espandiBtns[k].className && espandiBtns[k].className != 'espandiBtn') {
                    continue;
                }
                var espandiBtn = espandiBtns[k];
                
                if (bgFlag) {
                    espandiLista.style.backgroundColor = "#F5F5F5";
                    bgFlag--;
                } else {
                    espandiBtn.style.color = "#003E7A";
                    espandiLista.style.backgroundColor = "#FFFFFF";
                    bgFlag++;
                }
                
                // Create image button
                var img = document.createElement('img');
                img.setAttribute('src', '/caring119static/img_root/btn_espandi_apri.gif');
                
                // If title has no button append button
                // This prevents creating multiple buttons
                if (!espandiBtn.getElementsByTagName('img')[0]) {
                    var title = espandiBtn.getElementsByTagName('a')[0];
                    title.appendChild(img);
                    espandiBtn.appendChild(title);
                    
                    // Assign img to it's title
                    espandiBtn.title = title;
                    espandiBtn.img = img;
                }
                
                // Close all boxes: default
                // Remove code block to leave boxes open by default
                var espandiBoxes = espandiLista.getElementsByTagName('dd');
                for (var l = 0; l < espandiBoxes.length; l++) {
                    // Filter definition description, if not matched go to next loop
                    if (!espandiBoxes[l].className && espandiBoxes[l].className != 'espandiBox') {
                        continue;
                    }
                    espandiBoxes[l].style.display = "none";
                    
                    // Assign box to it's title
                    espandiBtn.espandiBox = espandiBoxes[l];
                }
                
                
                espandiBtn.onclick = function(){
                
                    // If it is not none set it as block
                    if (this.espandiBox.style.display != 'none') {
                        // Change button
                        this.img.setAttribute('src', '/caring119static/img_root/btn_espandi_apri.gif');
                        // Toggle
                        this.espandiBox.style.display = 'none';
                    } else {
                        // Chang button
                        this.img.setAttribute('src', '/caring119static/img_root/btn_espandi_chiudi.gif');
                        // Toggle
                        this.espandiBox.style.display = "block";
                    }
                    
                    var parent = this.parentNode.parentNode;
                    
                    var countTitle = setBox(parent, 'dt', 'espandiBox');
                    // Routin filter: count box
                    var countBox = setBox(parent, 'dd', 'espandiBox');
                    // Routin filter: count opened box
                    var countBoxOpened = setBox(parent, 'dd', 'espandiBox', true);
                    
                    // Routin filter: make sure title and box has the same quantity
                    if (countTitle == countBox) {
                        // If opened box is greater than total box
                        // IE: if all the boxes are opened, change button image(espandi tutto)
                        if (countBoxOpened < countBox) {
                            espandiTutto.espandiTuttoImg.setAttribute('src', '/caring119static/img_root/btn_espandi_tutto.gif');
                        } else {
                            espandiTutto.espandiTuttoImg.setAttribute('src', '/caring119static/img_root/btn_chiudi_tutto.gif');
                        }
                    }
                    
                    return false;
                };
            }
            
        }
        
        if (espandiTutto) {
            espandiTutto.onclick = function(){
            
                var parent = this.parentNode;
                // Routin filter: count title
                var countTitle = setBox(parent, 'dt', 'espandiBox');
                // Routin filter: count box
                var countBox = setBox(parent, 'dd', 'espandiBox');
                // Routin filter: count opened box
                var countBoxOpened = setBox(parent, 'dd', 'espandiBox', true);
                
                // Routin filter: make sure title and box has the same quantity
                if (countTitle == countBox) {
                    // If opened box is greater than total box
                    // IE: if all the boxes are opened, close them all
                    // Otherwise open all the boxes
                    if (countBoxOpened >= countBox) {
                        this.espandiTuttoImg.setAttribute('src', '/caring119static/img_root/btn_espandi_tutto.gif');
                        setBox(parent, 'dt', 'espandiBtn', false, true, 'close');
                        setBox(parent, 'dd', 'espandiBox', false, true, 'close');
                    } else {
                        this.espandiTuttoImg.setAttribute('src', '/caring119static/img_root/btn_chiudi_tutto.gif');
                        setBox(parent, 'dt', 'espandiBtn', false, true, 'open');
                        setBox(parent, 'dd', 'espandiBox', false, true, 'open');
                    }
                }
                return false;
            };
        }
    }
}

domLoad(toggle);
domLoad(accordion);
window.onload = bannerAccordion;
domLoad(bannerDemo);
