﻿function displayMenu(passedXML) {   	        
    //load XML file
    var xmlDoc = loadXMLDocument(passedXML);
    langNode = xmlDoc.getElementsByTagName('LANGUAGE');
           
    //select the appropriate language node
    for(i = 0;i < langNode.length;i++){
    
        //if 'LOCALE' attribute is the same as locale defined by page
        if(langNode[i].getAttribute('LOCALE') == locale){                      
            
            //get the value for left menu links
            var leftMenu = langNode[i].getElementsByTagName('LEFT_MENU');
            var leftMenuNode = leftMenu[0].getElementsByTagName('BUTTON');
            
            //get the value for the top menu links
            var topMenu = langNode[i].getElementsByTagName('TOP_MENU');
            var topMenuNode = topMenu[0].getElementsByTagName('BUTTON');
            
            //get the value for the footer links
            var footer = langNode[i].getElementsByTagName('FOOTER');
            var footerNode = footer[0].getElementsByTagName('ITEM');
            
            //get the value the logo section
            var logo = langNode[i].getElementsByTagName('LOGO');
            var logoNode = logo[0].getElementsByTagName('PARTNER');
            
            //set HTML dom left and top menu link elements
            var htmlMenuNav = document.getElementById('navMenu');
                htmlNavMenuList = document.createElement('ul');
                htmlMenuNav.appendChild(htmlNavMenuList);
                            
            var htmlProdMenuNav = document.getElementById('navTop');
            var htmlFooterRow = document.getElementById('footerRow');
            var htmlLogoDiv = document.getElementById('leftMenuLogo');
            
            //elements that will be appended
            var currentImg;
            var currentLi;
            var currentA;
            var currentSpan;

            //display login control
            if (hasQueryString()) {
                if (getQueryArg('username') != undefined) {
                    displayLoginControl(true,langNode[i].getElementsByTagName('LOGINCONTROL'));
                    
                } else {
                    displayLoginControl(false,langNode[i].getElementsByTagName('LOGINCONTROL'));
                }
            } else {
                displayLoginControl(false, langNode[i].getElementsByTagName('LOGINCONTROL'));
            }

            //display partner logo
            document.getElementById('topLoginDiv').appendChild(displayPartnerLogo(logoNode));

            //display left menu link data            
            for (u = 0; u < leftMenuNode.length; u++) {

                //create current <li><a href='link'>text</a></li> structure
                currentLi = document.createElement('li');
                currentA = document.createElement('a');
                currentSpan = document.createElement('span');
                currentA.appendChild(currentSpan);
                currentLi.appendChild(currentA);

                //insert text
                currentSpan.innerHTML = leftMenuNode[u].getElementsByTagName('TEXTE')[0].childNodes[0].nodeValue;

                //insert links
                if (leftMenuNode[u].getElementsByTagName('LINK')[0].childNodes[0].nodeValue != 'null') {
                    currentA.setAttribute('href', leftMenuNode[u].getElementsByTagName('LINK')[0].childNodes[0].nodeValue);
                    currentA.setAttribute('onclick', 'updateActiveTopNav(' + u + ')');
                }

                //creates an entry into navArray
                navArray[u] = currentA;

                //append current list item to leftNav
                htmlNavMenuList.appendChild(currentLi);
            }
            
            /*display top menu link data
            for(u=0;u<topMenuNode.length;u++){
                //create current <a href='link'>text</a> structure
                currentA = document.createElement('a');
                currentSpan = document.createElement('span');
                currentA.appendChild(currentSpan);                          
            
                //insert text and hyperlinks values to links
                currentSpan.innerHTML = topMenuNode[u].getElementsByTagName('TEXTE')[0].childNodes[0].nodeValue;
                currentA.setAttribute('href',topMenuNode[u].getElementsByTagName('LINK')[0].childNodes[0].nodeValue);
                currentA.setAttribute('onclick', 'updateActiveProdNav('+u+')');
                
                //creates an entry into prod menu array
                prodArray[u] = currentA;
                
                //append current list item to leftNav
                htmlProdMenuNav.appendChild(currentA);
                
                
            }*/
                   
            //display footer link data
            for(u=0;u<footerNode.length;u++){
                //create current <a href='link'>text</a> structure
                currentA = document.createElement('a');                          
                
                //insert text and hyperlinks values to links
                currentA.innerHTML = footerNode[u].getElementsByTagName('TEXTE')[0].childNodes[0].nodeValue;
                
                //insert links
                if (footerNode[u].getElementsByTagName('LINK')[0].childNodes[0].nodeValue != 'null') {
                    if (footerNode[u].getElementsByTagName('TEXTE')[0].childNodes[0].nodeValue == 'Privacy policy' || footerNode[u].getElementsByTagName('TEXTE')[0].childNodes[0].nodeValue == 'Politique de confidentialité') {
                        currentA.setAttribute('href', '#');
                        currentA.setAttribute('title', footerNode[u].getElementsByTagName('LINK')[0].childNodes[0].nodeValue);
                        currentA.setAttribute('onclick', 'showPrivacy(this)');
                    } else {
                        currentA.setAttribute('href', footerNode[u].getElementsByTagName('LINK')[0].childNodes[0].nodeValue);
                    }
                }
                
                //append current list item to leftNav
                htmlFooterRow.appendChild(currentA);
            }
        }
    }
}

//display logo control
function displayPartnerLogo(passedXML) {
    var logoDiv = document.createElement('div');

    //for each logo add link containing image
    for (logoCount = 0; logoCount < passedXML.length; logoCount++) {
        var currentA = document.createElement('a');
        currentA.setAttribute('href', passedXML[logoCount].getElementsByTagName('LOGO_LINK')[0].childNodes[0].nodeValue + '?' + global_queryString);
        logoDiv.appendChild(currentA);

        var currentImg = document.createElement('img');
        currentImg.setAttribute('src', passedXML[logoCount].getElementsByTagName('LOGO_IMAGE')[0].childNodes[0].nodeValue);
        currentA.appendChild(currentImg);
    }

    return logoDiv;
}

//display login control
function displayLoginControl(passedAuth, passedXML) {

    //initialize target for login control
    var LOGINDIV_TARGET = document.getElementById('topLoginDiv');

    //flash control if there is an element inside target loop
    while (LOGINDIV_TARGET.hasChildNodes == true) {
        //remove last element from target
        LOGINDIV_TARGET.removeChild(LOGINDIV_TARGET.lastChild);
    }

    //determine which state to display
    if (passedAuth == true) {   //user authenticated, display his name and a logout link
        //get username from query string
        var name = getQueryArg('username');

        //initialize elements
        var loggedInDiv = document.createElement('div');
            loggedInDiv.setAttribute('style', 'height:75px;');
        
        var usernameLabel = document.createElement('label');
        var logoutLink = document.createElement('a');

        //append elements
        loggedInDiv.appendChild(usernameLabel);
        loggedInDiv.appendChild(document.createElement('br'));
        loggedInDiv.appendChild(document.createElement('br'));
        loggedInDiv.appendChild(logoutLink);
        LOGINDIV_TARGET.appendChild(loggedInDiv);

        //set attribute
        usernameLabel.setAttribute('id', 'usernameLabel');
        usernameLabel.setAttribute('style', 'color:white;')
        usernameLabel.innerHTML = passedXML[0].getElementsByTagName('LOGGEDIN')[0].childNodes[0].nodeValue + name;
        logoutLink.setAttribute('onclick', 'logout()');
        logoutLink.setAttribute('href', '#');
        logoutLink.setAttribute('style', 'color:white;');
        logoutLink.innerHTML = passedXML[0].getElementsByTagName('LOGOUT')[0].childNodes[0].nodeValue;
        
    } else {    //user not authenticated display the login control
    //initialize elements        
        var loginControl = '<table style="height:75px;">' +
                                '<tr>' +
                                    '<td align="center">' +
                                        '<label style="color:White">' + passedXML[0].getElementsByTagName('USERNAME_LBL')[0].childNodes[0].nodeValue + '</label>' +
                                    '</td>' +
                                    '<td align="center">' +
                                        '<label style="color:White">' + passedXML[0].getElementsByTagName('PASSWORD_LBL')[0].childNodes[0].nodeValue + '</label>' +
                                    '</td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<td>' +
                                        '<input id="usernameInput" style="width:100px" type="text" title="' + passedXML[0].getElementsByTagName('USERNAME_LBL')[0].childNodes[0].nodeValue + '"/>' +
                                    '</td>' +
                                    '<td>' +
                                        '<input id="passwordInput" style="width:100px" type="password" title="' + passedXML[0].getElementsByTagName('PASSWORD_LBL')[0].childNodes[0].nodeValue + '"/>' +
                                    '</td>' +
                                '</tr>' +
                                '<tr>' +
                                    '<td colspan="2" align="right">' +
                                        '<input id="loginButton" type="button" onclick="authenticate()" value="' + passedXML[0].getElementsByTagName('LOGIN')[0].childNodes[0].nodeValue + '"/>' +
                                    '</td>' +
                                '</tr>' +
                           '</table>';
        LOGINDIV_TARGET.innerHTML = loginControl;
    }
}

//flash menu css class
function flashMenu(){
    //flash navArray
    for(navCount = 0; navCount < navArray.length; navCount++){
        navArray[navCount].removeAttribute('class');
        //navArray[navCount].setAttribute('href', navArray[navCount].getAttribute('href'));

        var cleanLink = getCleanURL(navArray[navCount].getAttribute('href'));
        var updatedLink = cleanLink + '?' + global_queryString;
        navArray[navCount].setAttribute('href', updatedLink);
    }
    
    //flash product menu
    for(prodCount = 0; prodCount < prodArray.length; prodCount++){
        prodArray[prodCount].removeAttribute('class');
        //prodArray[navCount].setAttribute('href', prodArray[navCount].getAttribute('href'));

        var cleanLink = getCleanURL(prodArray[prodCount].getAttribute('href'));
        var updatedLink = cleanLink + '?' + global_queryString;
        prodArray[prodCount].setAttribute('href', updatedLink);
    }
}

function showPrivacy(passedElement) {
    alert(passedElement.getAttribute('title'));
}

//update css active class for menu
function updateActiveTopNav(passedIndex){
    //flash menus
    flashMenu();

    //set selected page link as active
    navArray[passedIndex].setAttribute('class', 'active');
}

function updateActiveProdNav(passedIndex){
    //flash menus
    flashMenu();
    
    //set selected page link as active
    prodArray[passedIndex].setAttribute('class', 'active');
}
