WS_waitscreen = true;


window.onload = function(){
    // Call Entry
    setLang()
    
    
    // Go to Register Transactional Data
    document.getElementById('previous-button').addEventListener("click", function(){
        gotoPage('register','regPersonTr',{ 'sessionid' : getParameterByName('sessionid') })
    });
    
     document.getElementById('next-button').addEventListener("click", function(){
        verify()
    });
    
    /*document.getElementsByTagName('body')[0].addEventListener("click", function(){
        console.log()
    }); */
    
    // Remove red errors 
    document.getElementsByClassName('switch')[0].addEventListener("click", function(){
        noError('checkbox','echeckbox')
        document.getElementById('echeckbox').style.display = 'none'
    }); 
    
    // Later
    document.getElementById('later-button').addEventListener("click", function(){
        document.getElementsByClassName('completeLater')[0].style.display = 'table'
    });
    
    document.getElementById('completeLaterAccept').addEventListener("click", function(){
        gotoPage('dashboard','home', { 'sessionid' : getParameterByName('sessionid') })
    });
    
    document.getElementById('completeLaterCancel').addEventListener("click", function(){
        document.getElementsByClassName('completeLater')[0].style.display = 'none'
    });
    
    // Session Update
    document.getElementById('expSessionButton').addEventListener("click",function(){
        var req = {
            'sessionid': getParameterByName('sessionid')
        }
        sessionUpdate(req)
    })
    
    //Mostrar HTML cargado
    esconderHTML(false);
    
}

function verify(){
    if (document.getElementById('checkbox').checked){
        var req = {
            "sessionid" : getParameterByName('sessionid'),
            "step" : 6
        }
        updatePerson(req);
    }else{
        error('checkbox','echeckbox',getLangById('usersUpdateperson1')[lang]);
        document.getElementById('echeckbox').style.display = 'block'
    }

}

function getEntry(sessionid){
    console.log('entry')
    callWS("GET", "users/entry", { 'sessionid' : sessionid}, response);
        
    function response(status, rsp){
        switch(status){
            case 200:
                var f=new Date();
                if(lang == 'esp'){
                    var meses = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
                    document.getElementById('textStatement').innerHTML="Yo "+JSON.parse(rsp).entry.person.firstname.value+' ';
                
                    if( JSON.parse(rsp).entry.person.secondname.value != null){
                        document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + JSON.parse(rsp).entry.person.secondname.value + ' ';
                    }
                    
                    document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + JSON.parse(rsp).entry.person.firstlastname.value;
                    
                    if( JSON.parse(rsp).entry.person.secondlastname.value != null){
                        document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + ' ' + JSON.parse(rsp).entry.person.secondlastname.value;
                    }
                    
                    document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML +', titular de la '+
                                                                        JSON.parse(rsp).entry.person.document.type.dsc;
                    if(JSON.parse(rsp).entry.person.document.type.dsc=='cédula'){
                        document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + ' de identidad'
                    }
                    document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + ' Nro. ' +
                                                                        JSON.parse(rsp).entry.person.document.dsc.value /*', domiciliado en la Calle '+
                                                                        JSON.parse(rsp).entry.person.street.value + ', en '+
                                                                        JSON.parse(rsp).entry.person.city.dsc + ', ' +
                                                                        JSON.parse(rsp).entry.person.country.dsc + ', declaro bajo fe de juramento que todos los datos y documentos suministrados son ciertos y fehacientes. Igualmente autorizo a la empresa Asesoría Financiera C.A. a efectos de verificar la veracidad de lo aquí declarado.'
                                                                        + '<br/><br/> A los '+f.getDate()+' días del mes de '+meses[f.getMonth()]+' de '+f.getFullYear()*/
                    document.getElementsByClassName('pageCnt')[0].style.visibility="visible"
                    
                    if(JSON.parse(rsp).entry.person.street.value){
                        document.getElementById('textStatement').innerHTML += ', domiciliado en la Calle '+ JSON.parse(rsp).entry.person.street.value
                    }
                    if(JSON.parse(rsp).entry.person.city.dsc){
                        document.getElementById('textStatement').innerHTML += ', en '+ JSON.parse(rsp).entry.person.city.dsc
                    }
                    if(JSON.parse(rsp).entry.person.country.dsc){
                        document.getElementById('textStatement').innerHTML += ', en '+ JSON.parse(rsp).entry.person.country.dsc
                    }
                    document.getElementById('textStatement').innerHTML += ', declaro bajo fe de juramento que todos los datos y documentos suministrados son ciertos y fehacientes. Igualmente autorizo a la empresa Asesoría Financiera C.A. a efectos de verificar la veracidad de lo aquí declarado.'
                                                                        + '<br/><br/> A los '+f.getDate()+' días del mes de '+meses[f.getMonth()]+' de '+f.getFullYear()+'.';
                       
                }else{
                    var meses = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
                    document.getElementById('textStatement').innerHTML="I "+JSON.parse(rsp).entry.person.firstname.value+' ';
                
                    if( JSON.parse(rsp).entry.person.secondname.value != null){
                        document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + JSON.parse(rsp).entry.person.secondname.value + ' ';
                    }
                    
                    document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + JSON.parse(rsp).entry.person.firstlastname.value;
                    
                    if( JSON.parse(rsp).entry.person.secondlastname.value != null){
                        document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + ' ' + JSON.parse(rsp).entry.person.secondlastname.value;
                    }
                    
                    document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML +', holder of '+
                                                                        JSON.parse(rsp).entry.person.document.type.dsc + ' No. '
                    if(JSON.parse(rsp).entry.person.document.type.dsc=='cédula'){
                        //document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML + ' de identidad'
                    }
                    document.getElementById('textStatement').innerHTML = document.getElementById('textStatement').innerHTML +
                                                                        JSON.parse(rsp).entry.person.document.dsc.value
                    document.getElementsByClassName('pageCnt')[0].style.visibility="visible"
                    
                    if(JSON.parse(rsp).entry.person.street.value){
                        document.getElementById('textStatement').innerHTML += ', domiciled at '+ JSON.parse(rsp).entry.person.street.value
                    }
                    if(JSON.parse(rsp).entry.person.city.dsc){
                        document.getElementById('textStatement').innerHTML += ', in '+ JSON.parse(rsp).entry.person.city.dsc
                    }
                    if(JSON.parse(rsp).entry.person.country.dsc){
                        document.getElementById('textStatement').innerHTML += ', '+ JSON.parse(rsp).entry.person.country.dsc
                    }
                    document.getElementById('textStatement').innerHTML += ', declare under oath that all the data and documents provided are true and reliable. I also authorize the company Asesoría Financiera C.A. in order to verify the veracity of what is stated here.'
                                                                        + '<br/><br/> On the '+f.getDate()+'th day of the month of '+meses[f.getMonth()]+' '+f.getFullYear()+'.';
                                                                    
                }
                // Window Load
                var waitScreen = document.getElementById("waitScreen");
                document.body.removeChild(waitScreen);
                WS_waitscreen = false;
                break;
            case 204:
                break;
            case 400:
                // Parametros incorrectos
                break;
            case 401:
                expiredSession();
                break;
            case 500:
                // Error
                break;
            default:
                break;
        }
    }    
}

function verifyData(){
    var req = {
        "sessionid" : getParameterByName('sessionid'),
        "step" : 6
    }
    if( amomonthlyop.value == '' ){
        error('amount','eamount','Introduzca un monto.');
        document.getElementById('eamount').style.display = 'block'
    }else{
        updatePerson(req)   
    }

}

function updatePerson(req){
    callWS("POST", "users/updateperson", req, response);
    function response(status, rsp){
        switch(status){
            case 200:
                // Ok
                gotoPage('dashboard','home',{'sessionid' : getParameterByName('sessionid')})
                break;
            case 204:
                break;
            case 400:
                // Parametros incorrectos
                break;
            case 401:
                expiredSession();
                break;
            case 500:
                // Error
                break;
            default:
                break;
        }
    }  
}

// name A: classIcon
// name B : BD
// nameC: nameInput
function setStatus(nameA,nameB,nameC){
    if(nameB.valid == -1){
        document.getElementById(nameA).classList.add("iconRed")
        document.getElementById(nameA).classList.add("fas")
        document.getElementById(nameA).classList.add("fa-times-circle")
    }
    else if (nameB.valid == 0){
        document.getElementById(nameA).classList.add("iconYellow")
        document.getElementById(nameA).classList.add("fas")
        document.getElementById(nameA).classList.add("fa-pause-circle")
    }
    else if (nameB.valid == 1){
        console.log('entro',nameC)
        document.getElementById(nameA).classList.add("iconGreen")
        document.getElementById(nameA).classList.add("fas")
        document.getElementById(nameA).classList.add("fa-check-circle")
        document.getElementById(nameC).disabled = true;
        document.getElementById(nameC).style.cursor = 'default'
        if(document.getElementById(nameC).tagName != 'SELECT'){
            document.getElementById(nameC).style.backgroundColor = 'white'
        }
    }
}

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"
                break;
            case 400:
                break;
            case 401:
                expiredSession();
                break;
            case 402:
                break;
            case 403:
                break;
            case 500:
                break;
            default:
                break;
        }
    }   
}

function setLang(){
    
    lang = localStorage.getItem('lang');
    
    /* inner HTML */
    setWordLanginnerHTML('regperson1','regperson1');
    setWordLanginnerHTML('regperson2','regperson2');
    setWordLanginnerHTML('regperson3','regperson3');
    setWordLanginnerHTML('regperson4','regperson4');
    setWordLanginnerHTML('regperson5','regperson5');
    setWordLanginnerHTML('regperson6','regperson6');
    setWordLanginnerHTML('regTitle','regperson6');
    setWordLanginnerHTML('confirm','confirm');
    setWordLanginnerHTML('data','data');
    setWordLanginnerHTML('previous-button','goBack');
    setWordLanginnerHTML('later-button','completeThen');
    setWordLanginnerHTML('next-button','continue');
    setWordLanginnerHTML('ts','ts');
    setWordLanginnerHTML('completeLater','completeLater');
    setWordLanginnerHTML('completeLaterAccept','completeThen');
    setWordLanginnerHTML('completeLaterCancel','cancel');
    setWordLanginnerHTML('expired1','expired1');
    setWordLanginnerHTML('expired2','expired2');
    
    getEntry(getParameterByName('sessionid'));

}
