WS_waitscreen = true;

window.onload = function(){
    var usr = 'hola'
    // Call Entry
    setLang()
    
    
    // Go to Register Transactional Data
    document.getElementById('previous-button').addEventListener("click", function(){
        gotoPage('register','regCompanyTr',{ '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" : 7
        }
        updateCompany(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:
                
                if(lang == 'esp'){
                    var meses = new Array ("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");
                    var f=new Date();
                    
                    
                    document.getElementById('textStatement').innerHTML= 'Yo, ' + JSON.parse(rsp).entry.company.partners[0].name.value + 
                    ' titular de la Cédula de Identidad Nro. ' + JSON.parse(rsp).entry.company.partners[0].document.value +
                    ' y de este domicilio' 
                    
                    if(JSON.parse(rsp).entry.company.street.value){
                        document.getElementById('textStatement').innerHTML += ' ' +JSON.parse(rsp).entry.company.street.value
                        if(JSON.parse(rsp).entry.company.city.dsc){
                            document.getElementById('textStatement').innerHTML += ', en ' + JSON.parse(rsp).entry.company.city.dsc
                        }
                        if(JSON.parse(rsp).entry.company.country.dsc){
                            document.getElementById('textStatement').innerHTML +=', ' + JSON.parse(rsp).entry.company.country.dsc
                        }
                    }
                    document.getElementById('textStatement').innerHTML +='. En mi condición de Representante Legal de la empresa ' + JSON.parse(rsp).entry.company.name.value +
                    ' identificada con el número de registro de información fiscal RIF '
                    + JSON.parse(rsp).entry.company.document.dsc.value + '. <br/><br/>'
                    + 'Declaro bajo Fe de Juramento: que todos los datos y documentos suministrados son ciertos y fehacientes, igualmente autorizo suficientemente a la empresa Asesoria' +
                    ' Financiera .IO, C.A., a efecto de verificar la veracidad de lo aquí declarado.' +
                    '<br/><br/> A los '+f.getDate()+' días del mes de '+meses[f.getMonth()]+' del año '+f.getFullYear()+'.';   
                }else{
                    var meses = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
                    var f=new Date();
                    document.getElementById('textStatement').innerHTML= 'I, ' + JSON.parse(rsp).entry.company.partners[0].name.value +
                    ' I, Cristian Marquina holder of the Identity Card No. ' + JSON.parse(rsp).entry.company.partners[0].document.value +
                    ' and address' 
                    
                    if(JSON.parse(rsp).entry.company.street.value){
                        document.getElementById('textStatement').innerHTML += ' ' +JSON.parse(rsp).entry.company.street.value
                        if(JSON.parse(rsp).entry.company.city.dsc){
                            document.getElementById('textStatement').innerHTML += ', en ' + JSON.parse(rsp).entry.company.city.dsc
                        }
                        if(JSON.parse(rsp).entry.company.country.dsc){
                            document.getElementById('textStatement').innerHTML +=', ' + JSON.parse(rsp).entry.company.country.dsc
                        }
                    }
                    document.getElementById('textStatement').innerHTML +='. In my capacity as Legal Representative of the ' + JSON.parse(rsp).entry.company.name.value + " company " +
                    ' identified with the tax information registration number RIF '
                    + JSON.parse(rsp).entry.company.document.dsc.value + '. <br/><br/>'
                    + 'I declare under Oath: that all the data and documents provided are true and reliable, I also sufficiently authorize the company Asesoria Financiera .IO, 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()+'.';
                
                }
                
                
                document.getElementsByClassName('pageCnt')[0].style.visibility="visible"
                // 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 updateCompany(req){
    callWS("POST", "users/updatecompany", 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;
        }
    }  
}

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('regcompany1','regcompany1');
    setWordLanginnerHTML('regcompany2','regcompany2');
    setWordLanginnerHTML('regcompany3','regcompany3');
    setWordLanginnerHTML('regcompany4','regcompany4');
    setWordLanginnerHTML('regcompany5','regcompany5');
    setWordLanginnerHTML('regcompany6','regcompany6');
    setWordLanginnerHTML('regcompany7','regcompany7');
    setWordLanginnerHTML('regTitle','regcompany7');
    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'));

}

