WS_waitscreen = true;

window.onload = function(){
    
    // Go back
    document.getElementById('previous-button').addEventListener("click", function(){
        gotoPage('step','withdrawFundsAm',{ 'sessionid' : getParameterByName('sessionid'),'cid' : getParameterByName('cid') })
    });
    
    // Go step 4
    document.getElementById('next-button').addEventListener("click", function(){
        verifyNextButton()
    });
    
    // Go back R
    document.getElementById('previous-buttonR').addEventListener("click", function(){
        gotoPage('step','withdrawFundsAm',{ 'sessionid' : getParameterByName('sessionid'), 'cid' : getParameterByName('cid') })
    });
    
    // Go step 4 R
    document.getElementById('next-buttonR').addEventListener("click", function(){
        verifyNextButton()
    });
    
    // Balance
    document.getElementById('floatButton').addEventListener("click",function(){
        gotoPage('dashboard','home',{ 'sessionid' : getParameterByName('sessionid') })
        
    })
    
    // Session Update
    document.getElementById('expSessionButton').addEventListener("click",function(){
        var req = {
            'sessionid': getParameterByName('sessionid')
        }
        sessionUpdate(req)
    })
    
    // Affiliate
    document.getElementById('affiliate').addEventListener("click",function(){
        gotoPage('preferences','main',{ 'sessionid' : getParameterByName('sessionid') })
    })
    
    // Go to preferences
    document.getElementById('preferences').addEventListener("click", function(){
        gotoPage('preferences','main',{ 'sessionid' : getParameterByName('sessionid') })
    });
    
    // Go to preferences
    document.getElementById('preferencesR').addEventListener("click", function(){
        gotoPage('preferences','main',{ 'sessionid' : getParameterByName('sessionid') })
    });
    
    // Go to balance
    document.getElementById('balance').addEventListener("click", function(){
        gotoPage('balance','balance',{ 'sessionid' : getParameterByName('sessionid') })
    });
    
    // Go to balance
    document.getElementById('balanceR').addEventListener("click", function(){
        gotoPage('balance','balance',{ 'sessionid' : getParameterByName('sessionid') })
    });
    
    document.getElementById('example-element-toggle2').checked = false;
    
    // Bar
    document.getElementById('togglebar').addEventListener("click", function(){
        var el = document.getElementById('addView2')
        document.getElementById('addView2').style.display = 'block'
        setTimeout(function(){ 
            document.getElementById('example-element-toggle2').checked = !document.getElementById('example-element-toggle2').checked;
        }, 15);   
        
    });
    
    // Logout
    document.getElementById('logouticon').addEventListener("click", function(){
        logout();
    });
    
    // Logout
    document.getElementById('logouticon2').addEventListener("click", function(){
        logout();
    });
    
    init()
    
    //Mostrar HTML cargado
    esconderHTML(false);
    
    var waitScreen = document.getElementById("waitScreen");
    document.body.removeChild(waitScreen);
    WS_waitscreen = false;
    document.getElementsByClassName('pageCnt')[0].style.visibility="visible"

    
}

function expiredSession(){
    document.getElementsByClassName('expired')[0].style.display = "table"
    WS_waitscreen = false;
    var seconds = 30;
    var countdown = setInterval(countDown, 1000);
    
    function countDown(){
        seconds--;
        if(seconds == 0){
            if(document.getElementsByClassName('expired')[0].style.display == "table"){
                gotoPage('login','main','');
                clearInterval(countdown);
            }
        }
        if(document.getElementsByClassName('expired')[0].style.display == "none"){
            clearInterval(countdown)
        }
    }
}

function sessionUpdate(req){
    callWS('GET', 'session/update', req, response);
    function response(status, rsp){
        console.log('session/update')
        console.log(JSON.parse(rsp));
        console.log(status) 
        switch(status){
            case 200:
                document.getElementsByClassName('expired')[0].style.display = "none";
                init()
                break;
            case 400:
                break;
            case 401:
                expiredSession();
                break;
            case 402:
                break;
            case 403:
                break;
            case 500:
                break;
            default:
                break;
        }
    }   
}

function init(){
    getEntry(getParameterByName('sessionid'));
}

function getEntry(sessionid){
    callWS("GET", "users/entry", { 'sessionid' : sessionid}, response);
        
    function response(status, rsp){
        switch(status){
            case 200:
                entrydata = (JSON.parse(rsp).entry)
                putName(entrydata);
                var req = {
                    'sessionid': getParameterByName('sessionid'),
                    'paym': 1
                }
                accountsList(req)
                break;
            case 204:
                break;
            case 400:
                break;
            case 401:
                expiredSession();
                break;
            case 500:
                break;
            default:
                break;
        }
    }    
}

function putName(entrydata){
    var name;
    if(entrydata.company){
        if(entrydata.company.name.value=="" || entrydata.company.name.value== null){
            name = entrydata.usr
        }else{
            name = entrydata.company.name.value
        }
    }
    else if(entrydata.person){
        if(entrydata.person.firstname.value=="" || entrydata.person.firstname.value==null){
            name = entrydata.usr
        }else{
            name = entrydata.person.firstname.value + ' '+ entrydata.person.firstlastname.value
        }
    }
    document.getElementById('name').innerHTML = name
    document.getElementById('name2').innerHTML = name
}

function logout(){
    var sessionid = getParameterByName('sessionid')
    var req ={
        'sessionid': sessionid,
    }
    callWS('GET', 'login/logout', req, response);

    function response(status, rsp){
        console.log(JSON.parse(rsp));
        console.log(status)
        switch(status){
            case 200:
                gotoPage('login','main','')
                break;
            case 400:
                break;
            case 401:
                expiredSession();
                break;
            case 500:
                break;
            default:
                break;
        }
    }   
}

function accountsList(req){
    callWS('GET', 'accounts/list', req, response);
    function response(status, rsp){
        console.log('accounts/list')
        console.log(JSON.parse(rsp));
        console.log(status) 
        switch(status){
            case 200:
                loadData(JSON.parse(rsp))
                break;
            case 400:
                break;
            case 401:
                expiredSession();
                break;
            case 402:
                break;
            case 403:
                break;
            case 500:
                break;
            default:
                break;
        }
    }   
}

function loadData(list){
    var tbl;
    var node;
    tbl = document.getElementById('table');
    var node = tbl.children[0].cloneNode(true);
    tbl.innerHTML = "";
    var tmp = null; tmp = node.cloneNode(true);
    if(list.numofrecords == 0){
        var ele = null;
        ele = tmp.cloneNode(true);
        ele.style.display = "none";
        tbl.appendChild(ele);
    }else{
        for (var i = 0; i < list.records.length; i++){
            if(list.records[i].currency.id == getParameterByName('cid')){
                var ele = null;
                ele = tmp.cloneNode(true);
                ele.style.display = "table";
                ele.children[0].children[0].innerHTML = list.records[i].paymentmethod.dsc
                ele.children[1].children[0].innerHTML = list.records[i].details1
                ele.children[2].children[0].children[0].setAttribute('idC',list.records[i].id)
                
                if(tbl.children.length % 2 == 0){
                    ele.classList.add('oddRow')
                }
                tbl.appendChild(ele);   
            }
                        
        }
        buttonsEffect()
    }
}

function buttonsEffect(){
    var buttons = document.getElementsByClassName('crypto');  
    for (i = 0; i < buttons.length; i++) {
        buttons[i].addEventListener("click", function(evt){
            document.getElementById('error').style.display = 'none'
            var buttons = document.getElementsByClassName('crypto'); 
            for (i = 0; i < buttons.length; i++){
                buttons[i].classList.remove('blueButton')
                buttons[i].classList.add('whiteButton')
                buttons[i].classList.remove('accept-button')
                buttons[i].classList.add('cancel-button')
                buttons[i].classList.remove('selectCrypto')
            }
            evt.srcElement.classList.add('blueButton')
            evt.srcElement.classList.remove('whiteButton')
            evt.srcElement.classList.add('accept-button')
            evt.srcElement.classList.remove('cancel-button')
            evt.srcElement.classList.add('selectCrypto')
        });
    }
}

function verifyNextButton(){
    if(document.getElementsByClassName('selectCrypto').length == 0){
        document.getElementById('error').innerHTML = 'Seleccione una cuenta.'
        document.getElementById('error').style.display = 'block'
    }else{
        gotoPage('step','withdrawFundsCo',{ 'sessionid' : getParameterByName('sessionid'), 'cid' : getParameterByName('cid'),
                  'q' : getParameterByName('q'), 'ac' : document.getElementsByClassName('selectCrypto')[0].getAttribute('idc')
        })
    }
}