WS_waitscreen = true;
var requiredLevelData = [];
var countryint="";
var countryload=false;
var cntseg = 0;

window.onload = function(){
    
    //El contry load se pone 'true' cuando el entry carga
    //Si pasan 20 seg se cancela.(Previene ciclo infinito)
    countryint = setInterval(function(){
        cntseg = cntseg+1;
        if(cntseg==21){
            clearInterval(countryint); //Si pasaron los 20 s
            cntseg = 0;
        }
        if(countryload){
            clearInterval(countryint);
            getCountries(countries);
            cntseg = 0;
        }
        
    },1000);
    
    // Go to back
    document.getElementById('previous-button').addEventListener("click", function(){
        gotoPage('register','regC', { 'sessionid' : getParameterByName('sessionid') });
    });
    
    // Go to Register Residence Data
    document.getElementById('next-button').addEventListener("click", function(){
        verifyData();
    });
    
    // Select Country
    document.getElementById('country').addEventListener("change", function(){
        //getPhoneCode(document.getElementById('country').value);
    });
    
    // Later
    document.getElementById('later-button').addEventListener("click", function(){
        document.getElementsByClassName('completeLater')[0].style.display = 'table';
    });
    
    var countries;
    // Number format
    /*var number = document.getElementById('number');
    number.addEventListener('keyup', function() {
        numberFormat(number,number.value.charAt(number.value.length-1));
    });*/
    now = new Date();
    nowDate = now.getFullYear() + '-' + (now.getMonth()+1) + '-' + now.getDate()
    // Remove red errors 
    document.getElementsByTagName('body')[0].addEventListener("click", function(){
        if (document.getElementById('firstName').value !== ''){
            noError('firstName','efirstName')
            document.getElementById('efirstName').style.display = 'none'
        }
        if (document.getElementById('firstSurname').value !== ''){
            noError('firstSurname','efirstSurname')
            document.getElementById('efirstSurname').style.display = 'none'
        }
        if (document.getElementById('identif').value !== ''){
            noError('identif','eidentif')
            document.getElementById('eidentif').style.display = 'none'
        }
        if (document.getElementById('number').value !== ''){
            noError('number','enumber')
            document.getElementById('enumber').style.display = 'none'
        }
        if (document.getElementById('country').value !== ''){
            noError('country','ecountry')
            document.getElementById('ecountry').style.display = 'none'
        }
        if (document.getElementById('dateEmission').value !== '' && document.getElementById('dateEmission').value<document.getElementById('dateExpiration').value){
            noError('dateEmission','edateEmission')
            document.getElementById('edateEmission').style.display = 'none'
        }
        
        now = new Date();
        nowDate = now.getFullYear() + '-' + (now.getMonth()+1) + '-' + now.getDate()
        if (document.getElementById('dateExpiration').value !== '' && now.getTime() <= new Date(document.getElementById('dateExpiration').value).getTime()){
            noError('dateExpiration','edateExpiration')
            document.getElementById('edateExpiration').style.display = 'none'
        }
        if (document.getElementById('gender').value !== ''){
            noError('gender','egender')
            document.getElementById('egender').style.display = 'none'
        }
        if (document.getElementById('status').value !== ''){
            noError('status','estatus')
            document.getElementById('estatus').style.display = 'none'
        }
        if (document.getElementById('phone').value !== ''){
            noError('phone','ephone')
            document.getElementById('ephone').style.display = 'none'
        }
        if (document.getElementById('countryB').value !== ''){
            noError('countryB','ecountryB')
            document.getElementById('ecountryB').style.display = 'none'
        }
        if (document.getElementById('american').value !== ''){
            noError('american','eamerican')
            document.getElementById('eamerican').style.display = 'none'
        }
        if (document.getElementById('venezuelan').value !== ''){
            noError('venezuelan','evenezuelan')
            document.getElementById('evenezuelan').style.display = 'none'
        }
        if(document.getElementById('dateB').value !== '' &&  getAge(document.getElementById('dateB').value)>17){
            noError('dateB','edateB')
            document.getElementById('edateB').style.display = 'none'
        }
    }); 
    
    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)
    })

    setLang();
}

var entrydata;

function numberFormat(donde,caracter){
    pat = /[\*,\+,\(,\),\?,\\,\$,\[,\],\^]/
    valor = donde.value
    largo = valor.length
    crtr = true
    if(isNaN(caracter) || pat.test(caracter) == true)
    	{
    	if (pat.test(caracter)==true) 
    		{caracter = "\\" + caracter}
    	carcter = new RegExp(caracter,"g")
    	valor = valor.replace(carcter,"")
    	donde.value = valor
    	crtr = false
    	}
    else
    	{
    	var nums = new Array()
    	cont = 0
    	for(m=0;m<largo;m++)
    		{
    		if(valor.charAt(m) == "." || valor.charAt(m) == " ")
    			{continue;}
    		else{
    			nums[cont] = valor.charAt(m)
    			cont++
    			}
    		}
    	}
    var cad1="",cad2="",tres=0
    if(largo > 3 && crtr == true)
    	{
    	for (k=nums.length-1;k>=0;k--)
    		{
    		cad1 = nums[k]
    		cad2 = cad1 + cad2
    		tres++
    		if((tres%3) == 0)
    			{
    			if(k!=0){
    				cad2 = "." + cad2
    				}
    			}
    		}
    	 donde.value = cad2
    	}
}	


function addOptions(domElement, array) {
    var selector = document.getElementsByName(domElement)[0];
    var firstOopcion = document.createElement("option");
    firstOopcion.disabled = true;
    firstOopcion.text = lang == 'esp' ? 'Selecciona' : 'Select'
    firstOopcion.value =''
    firstOopcion.selected = true
    selector.add(firstOopcion);
    for (var countrie in array.records) {
        if (array.records[countrie].id > 0){
            var opcion = document.createElement("option");
            opcion.text = array.records[countrie].dsc;
            opcion.value = array.records[countrie].id
            selector.add(opcion);
        }
    }
     setTimeout(function(){ 
        if (entrydata.entry.person.birthcountry.id == 0 || entrydata.entry.person.birthcountry.id == null){
            setTimeout(function(){ document.getElementById('countryB').value = "" }, 1000);
        }else{
            document.getElementById('countryB').value = entrydata.entry.person.birthcountry.id
        }
        if (entrydata.entry.person.document.country.id == 0 || entrydata.entry.person.document.country.id == null){
            setTimeout(function(){ document.getElementById('country').value = ""}, 1000);
        }else{
            document.getElementById('country').value = entrydata.entry.person.document.country.id
        }
     }, 1000);
 
}


function getCountries(countries){
    var req = {
        "sessionid": getParameterByName('sessionid')
    }
        
    callWS("GET", "demographics/countries", req, response);
        
    function response(status, rsp){
        switch(status){
            case 200:
                countries = JSON.parse(rsp);
                addOptions("country", countries);
                addOptions("countryB", countries);
                //getEntry(getParameterByName('sessionid'))
                break;
            case 204:
                break;
            case 400:
                // Parametros incorrectos
                break;
            case 401:
                break;
            case 500:
                // Error
                break;
            default:
                break;
        }
    }    
}

function getPhoneCode(number){
    var req = {
        "sessionid": getParameterByName('sessionid')
    }
        
    callWS("GET", "demographics/countries", req, response);
        
    function response(status, rsp){
        switch(status){
            case 200:
                countries = JSON.parse(rsp);
                //document.getElementById('phone').value = countries.records[number].areacode
                break;
            case 204:
                break;
            case 400:
                // Parametros incorrectos
                break;
            case 401:
                expiredSession();
                break;
            case 500:
                // Error
                break;
            default:
                break;
        }
    }    
}

function getEntry(sessionid){
    callWS("GET", "users/entry", { 'sessionid' : sessionid}, response);
        
    function response(status, rsp){
        switch(status){
            case 200:
                entrydata = JSON.parse(rsp)
                if((JSON.parse(rsp).requiredLevel)!==null)requiredLevelData = (JSON.parse(rsp).requiredLevel).split(',')
                levels = JSON.parse(rsp).entry.levels
                console.log(requiredLevelData)
                console.log(JSON.parse(rsp))
                console.log(levels)
                
                document.getElementById('firstName').value = JSON.parse(rsp).entry.person.firstname.value
                setStatus('firstNameIcon',JSON.parse(rsp).entry.person.firstname,'firstName')
                document.getElementById('firstName').setAttribute('dataname',JSON.parse(rsp).entry.person.firstname.update)
                
                document.getElementById('secondName').value = JSON.parse(rsp).entry.person.secondname.value
                setStatus('secondNameIcon',JSON.parse(rsp).entry.person.firstname,'secondName')
                document.getElementById('secondName').setAttribute('dataname',JSON.parse(rsp).entry.person.secondname.update)
                
                document.getElementById('firstSurname').value = JSON.parse(rsp).entry.person.firstlastname.value
                setStatus('firstSurnameIcon',JSON.parse(rsp).entry.person.firstlastname,'firstSurname')
                document.getElementById('firstSurname').setAttribute('dataname',JSON.parse(rsp).entry.person.firstlastname.update)
                
                document.getElementById('secondSurname').value = JSON.parse(rsp).entry.person.secondlastname.value
                setStatus('secondSurnameIcon',JSON.parse(rsp).entry.person.secondlastname,'secondSurname')
                document.getElementById('secondSurname').setAttribute('dataname',JSON.parse(rsp).entry.person.secondlastname.update)
                
                document.getElementById('number').value = JSON.parse(rsp).entry.person.document.dsc.value
                setStatus('numberIcon',JSON.parse(rsp).entry.person.document.dsc,'number')
                document.getElementById('number').setAttribute('dataname',JSON.parse(rsp).entry.person.document.dsc.update)
                
                if(JSON.parse(rsp).entry.person.document.type.id == 0){
                    document.getElementById('identif').value = ''
                }else{
                    document.getElementById('identif').value = JSON.parse(rsp).entry.person.document.type.id   
                }
                setStatus('identifIcon',JSON.parse(rsp).entry.person.document.type,'identif')
                document.getElementById('identif').setAttribute('dataname',JSON.parse(rsp).entry.person.document.type.update)
                
                document.getElementById('country').value = JSON.parse(rsp).entry.person.document.country.id
                setStatus('countryIcon',JSON.parse(rsp).entry.person.document.country,'country')
                document.getElementById('country').setAttribute('dataname',JSON.parse(rsp).entry.person.document.country.update)
                
                if(JSON.parse(rsp).entry.person.document.datecreated.date.substr(0,10) == '1970-01-01'){
                    document.getElementById('dateEmission').value = ''
                }else{
                    document.getElementById('dateEmission').value = JSON.parse(rsp).entry.person.document.datecreated.date.substr(0,10)
                }
                setStatus('dateEmissionIcon',JSON.parse(rsp).entry.person.document.datecreated,'dateEmission')
                document.getElementById('dateEmission').setAttribute('dataname',JSON.parse(rsp).entry.person.document.datecreated.update)
                
                if(JSON.parse(rsp).entry.person.document.dateexpiration.date.substr(0,10) == '1970-01-01'){
                    document.getElementById('dateExpiration').value = ''
                }else{
                    document.getElementById('dateExpiration').value = JSON.parse(rsp).entry.person.document.dateexpiration.date.substr(0,10)
                }
                setStatus('dateExpirationIcon',JSON.parse(rsp).entry.person.document.dateexpiration,'dateExpiration')
                document.getElementById('dateExpiration').setAttribute('dataname',JSON.parse(rsp).entry.person.document.dateexpiration.update)
                
                if (JSON.parse(rsp).entry.person.gender.value == null){
                    document.getElementById('gender').value = ''
                }else{
                    document.getElementById('gender').value = JSON.parse(rsp).entry.person.gender.value
                }
                setStatus('genderIcon',JSON.parse(rsp).entry.person.gender,'gender')
                document.getElementById('gender').setAttribute('dataname',JSON.parse(rsp).entry.person.gender.update)
                
                if (JSON.parse(rsp).entry.person.maritalstatus.id == 0){
                    document.getElementById('status').value = ''
                }else{
                    document.getElementById('status').value = JSON.parse(rsp).entry.person.maritalstatus.id   
                }
                setStatus('statusIcon',JSON.parse(rsp).entry.person.maritalstatus,'status')
                document.getElementById('status').setAttribute('dataname',JSON.parse(rsp).entry.person.maritalstatus.update)
                
                if(JSON.parse(rsp).entry.person.birthdate.date.substr(0,10) == '1970-01-01'){
                    document.getElementById('dateB').value = ''
                }else{
                    document.getElementById('dateB').value = JSON.parse(rsp).entry.person.birthdate.date.substr(0,10)
                }
                setStatus('dateBIcon',JSON.parse(rsp).entry.person.birthdate,'dateB')
                document.getElementById('dateB').setAttribute('dataname',JSON.parse(rsp).entry.person.birthdate.update)
                
                document.getElementById('countryB').value = JSON.parse(rsp).entry.person.birthcountry.id
                setStatus('countryBIcon',JSON.parse(rsp).entry.person.birthcountry,'countryB')
                document.getElementById('countryB').setAttribute('dataname',JSON.parse(rsp).entry.person.birthcountry.update)
                
                document.getElementById('phone').value = JSON.parse(rsp).entry.person.phone.value
                setStatus('phoneIcon',JSON.parse(rsp).entry.person.phone,'phone')
                document.getElementById('phone').setAttribute('dataname',JSON.parse(rsp).entry.person.phone.update)
                
                if(JSON.parse(rsp).entry.person.american.value == null){
                    document.getElementById('american').value = ''
                }else{
                    document.getElementById('american').value = JSON.parse(rsp).entry.person.american.value   
                }
                setStatus('americanIcon',JSON.parse(rsp).entry.person.american,'american')
                document.getElementById('american').setAttribute('dataname',JSON.parse(rsp).entry.person.american.update)
                
                if(JSON.parse(rsp).entry.person.venezuelan.value == null){
                    document.getElementById('venezuelan').value = ''
                }else{
                    document.getElementById('venezuelan').value = JSON.parse(rsp).entry.person.venezuelan.value 
                }
                setStatus('venezuelanIcon',JSON.parse(rsp).entry.person.venezuelan,'venezuelan')
                document.getElementById('venezuelan').setAttribute('dataname',JSON.parse(rsp).entry.person.venezuelan.update)
                
                setLevels(levels)
                
                countryload = true;
                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;
        }
        //Mostrar HTML cargado
        esconderHTML(false);
        //console.log(status)
        //console.log(rsp)
    }    
}

function getAge(dateString) {
  let today = new Date()
  let birthdate = new Date(dateString)
  let age = today.getFullYear() - birthdate.getFullYear()
  let diffMonths = today.getMonth() - birthdate.getMonth()
  if (
    diffMonths < 0 ||
    (diffMonths === 0 && today.getDate() < birthdate.getDate())
  ) {
    age--
  }
  return age
}

function verifyData(){
    var fname = document.getElementById('firstName');
    var sname = document.getElementById('secondName');
    var flastname = document.getElementById('firstSurname');
    var slastname = document.getElementById('secondSurname');
    var doc = document.getElementById('number');
    var doctype = document.getElementById('identif');
    var doccountryid = document.getElementById('country');
    var docissue = document.getElementById('dateEmission');
    var docexpiry = document.getElementById('dateExpiration');
    var gender = document.getElementById('gender');
    var maritalstatus = document.getElementById('status');
    var birthdate = document.getElementById('dateB');
    var birthcountryid = document.getElementById('countryB');
    var phone = document.getElementById('phone');
    var american = document.getElementById('american');
    var venezuelan = document.getElementById('venezuelan');
    
    var req = {
        "sessionid" : getParameterByName('sessionid'),
        "fname": capitalize(fname.value),
        "sname" : capitalize(sname.value),
        "flastname": capitalize(flastname.value),
        "slastname" : capitalize(slastname.value),
        "doc": doc.value,
        "doctype" : doctype.value,
        "doccountryid": doccountryid.value,
        "docissue" : docissue. value,
        "docexpiry": docexpiry.value,
        "gender" : gender. value,
        "maritalstatus": maritalstatus.value,
        "birthdate" : birthdate.value,
        "birthcountryid": birthcountryid.value,
        "phone" : phone.value,
        "american" : american.value,
        "venezuelan" : venezuelan.value,
        "step" : 1
    }
    
    //console.log(req.docissue)
    console.log(birthdate)
    
    now = new Date();
    nowDate = now.getFullYear() + '-' + (now.getMonth()+1) + '-' + now.getDate()
    
    if( (fname.value=='' && requiredLevelData.includes(fname.getAttribute('dataname'))) ||
        (flastname.value=='' && requiredLevelData.includes(flastname.getAttribute('dataname'))) || 
        (doctype.value=='' && requiredLevelData.includes(doctype.getAttribute('dataname'))) || 
        (doc.value=='' && requiredLevelData.includes(doc.getAttribute('dataname'))) || 
        (doccountryid.value=='' && requiredLevelData.includes(doccountryid.getAttribute('dataname'))) || 
        (docissue.value=='' && requiredLevelData.includes(docissue.getAttribute('dataname'))) || 
        (docexpiry.value=='' && requiredLevelData.includes(docexpiry.getAttribute('dataname'))) || 
        (gender.value=='' && requiredLevelData.includes(gender.getAttribute('dataname'))) || 
        (birthdate.value=='' && requiredLevelData.includes(birthdate.getAttribute('dataname'))) || 
        (maritalstatus.value=='' && requiredLevelData.includes(maritalstatus.getAttribute('dataname'))) || 
        (phone.value=='' && requiredLevelData.includes(phone.getAttribute('dataname'))) || 
        (birthcountryid.value=='' && requiredLevelData.includes(birthcountryid.getAttribute('dataname'))) || 
        (maritalstatus.value=='' && requiredLevelData.includes(maritalstatus.getAttribute('dataname'))) ||
        (american.value=='' && requiredLevelData.includes(american.getAttribute('dataname'))) || 
        getAge(birthdate.value)<18 || 
        docissue.value>=docexpiry.value || 
        now.getTime() > new Date(docexpiry.value).getTime() || 
        (venezuelan.value=='' && requiredLevelData.includes(venezuelan.getAttribute('dataname')))){
        
        if (docissue.value>=docexpiry.value){
            error('dateEmission','edateEmission',getLangById('usersUpdateperson1')[lang]);
            document.getElementById('edateEmission').style.display = 'block'
        }
        
        if (now.getTime() > new Date(docexpiry.value).getTime()){
            error('dateExpiration','edateExpiration',getLangById('usersUpdateperson2')[lang]);
            document.getElementById('edateExpiration').style.display = 'block'
        }
        
        if (fname.value=='' && requiredLevelData.includes(fname.getAttribute('dataname'))){
            error('firstName','efirstName',getLangById('usersUpdateperson3')[lang]);
            document.getElementById('efirstName').style.display = 'block'
        }
        
        if(flastname.value=='' && requiredLevelData.includes(flastname.getAttribute('dataname'))){
            error('firstSurname','efirstSurname',getLangById('usersUpdateperson4')[lang]);
            document.getElementById('efirstSurname').style.display = 'block'
        } 
        
        if(doctype.value=='' && requiredLevelData.includes(doctype.getAttribute('dataname'))){
            error('identif','eidentif',getLangById('usersUpdateperson5')[lang]);
            document.getElementById('eidentif').style.display = 'block'
        }
        
        if(doc.value=='' && requiredLevelData.includes(doc.getAttribute('dataname'))){
            error('number','enumber',getLangById('usersUpdateperson6')[lang]);
            document.getElementById('enumber').style.display = 'block'
        }
        
        if(doccountryid.value=='' && requiredLevelData.includes(doccountryid.getAttribute('dataname'))){
            error('country','ecountry',getLangById('usersUpdateperson7')[lang]);
            document.getElementById('ecountry').style.display = 'block'
        }
        
        if(docissue.value=='' && requiredLevelData.includes(docissue.getAttribute('dataname'))){
            error('dateEmission','edateEmission',getLangById('usersUpdateperson8')[lang]);
            document.getElementById('edateEmission').style.display = 'block'
        }
        
        if(docexpiry.value=='' && requiredLevelData.includes(docexpiry.getAttribute('dataname'))){
            error('dateExpiration','edateExpiration',getLangById('usersUpdateperson8')[lang]);
            document.getElementById('edateExpiration').style.display = 'block'
        }
        
         if(gender.value=='' && requiredLevelData.includes(gender.getAttribute('dataname'))){
            error('gender','egender',getLangById('usersUpdateperson9')[lang]);
            document.getElementById('egender').style.display = 'block'
        }
        
        if(birthdate.value=='' && requiredLevelData.includes(birthdate.getAttribute('dataname'))){
            error('dateB','edateB',getLangById('usersUpdateperson8')[lang]);
            document.getElementById('edateB').style.display = 'block'
        }else if(getAge(birthdate.value)<18){
            error('dateB','edateB',getLangById('usersUpdateperson14')[lang]);
            document.getElementById('edateB').style.display = 'block'
        }
        
        if(maritalstatus.value=='' && requiredLevelData.includes(maritalstatus.getAttribute('dataname'))){
            error('status','estatus',getLangById('usersUpdateperson10')[lang]);
            document.getElementById('estatus').style.display = 'block'
        }
        
        if(phone.value=='' && requiredLevelData.includes(phone.getAttribute('dataname'))){
            error('phone','ephone',getLangById('usersUpdateperson11')[lang]);
            document.getElementById('ephone').style.display = 'block'
        }
        
         if(birthcountryid.value=='' && requiredLevelData.includes(birthcountryid.getAttribute('dataname'))){
            error('countryB','ecountryB',getLangById('usersUpdateperson7')[lang]); 
            document.getElementById('ecountryB').style.display = 'block'
        } 
        
        if(american.value=='' && requiredLevelData.includes(american.getAttribute('dataname'))){
            error('american','eamerican',getLangById('usersUpdateperson12')[lang]);
            document.getElementById('eamerican').style.display = 'block'
        }
        
        if(venezuelan.value=='' && requiredLevelData.includes(venezuelan.getAttribute('dataname'))){
            error('venezuelan','evenezuelan',getLangById('usersUpdateperson12')[lang]);
            document.getElementById('evenezuelan').style.display = 'block'
        }
        
        return;  
  
    }else{
        updatePerson(req)    
    }
}

function updatePerson(req){
    callWS("POST", "users/updateperson", req, response);
    //console.log(req)
    //console.log(status)
    function response(status, rsp){
        switch(status){
            case 200:
                gotoPage('register','regPersonRes', { 'sessionid' : getParameterByName('sessionid') })
                break;
            case 204:
                break;
            case 400:
                // Parametros incorrectos
                break;
            case 409:
                // Ya existe esa cedula
                error('number','enumber',getLangById('usersUpdateperson13')[lang]);
                document.getElementById('enumber').style.display = 'block'
                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){
        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'
        }
    }
    
    document.getElementById(nameC).classList.add("inputElement")
    
}

function expiredSession(){
    document.getElementById('waitScreen').style.display = 'none'
    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 setLevels(levels){
    console.log(levels)
    levels1 = levels[1].basicRequired.split(',')
    levels2 = levels[2].basicRequired.split(',')
    levels3 = levels[3].basicRequired.split(',')
    console.log(document.getElementsByClassName('inputElement'))
    console.log(document.getElementsByClassName('inputElement').length)
    for(var i = 0; i < document.getElementsByClassName('inputElement').length ; i++){
        if(levels1.includes(document.getElementsByClassName('inputElement')[i].getAttribute('dataname'))){
            if(!document.getElementsByClassName('inputElement')[i].previousElementSibling.children[0]){
                document.getElementsByClassName('inputElement')[i].previousElementSibling.innerHTML += '<span style="font-size: 90%; color: #7D8B9E;" class="roboto-bold"> (1) </span>'   
            }
        }
        else if(levels2.includes(document.getElementsByClassName('inputElement')[i].getAttribute('dataname'))){
            if(!document.getElementsByClassName('inputElement')[i].previousElementSibling.children[0]){
                document.getElementsByClassName('inputElement')[i].previousElementSibling.innerHTML += '<span style="font-size: 90%; color: #4287CE;" class="roboto-bold"> (2) </span>'   
            }
        }
        else if(levels3.includes(document.getElementsByClassName('inputElement')[i].getAttribute('dataname'))){
            if(!document.getElementsByClassName('inputElement')[i].previousElementSibling.children[0]){
                document.getElementsByClassName('inputElement')[i].previousElementSibling.innerHTML += '<span style="font-size: 90%; color: rgb(1, 42, 63);" class="roboto-bold"> (3) </span>'
            }
        }
    }
}

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','regperson1');
    setWordLanginnerHTML('venezuelanLabel','venezuelanLabel');
    setWordLanginnerHTML('americanLabel','americanLabel');
    setWordLanginnerHTML('firstNameLabel','firstName');
    setWordLanginnerHTML('secondNameLabel','secondName');
    setWordLanginnerHTML('firstSurnameLabel','surname');
    setWordLanginnerHTML('secondNameLabelOptional','optional');
    setWordLanginnerHTML('secondSurnameLabel','secondSurname');
    setWordLanginnerHTML('secondSurnameLabelOptional','optional');
    setWordLanginnerHTML('identifLabel','idLabel');
    setWordLanginnerHTML('numberLabel','number');
    setWordLanginnerHTML('countryLabel','documentIssueCountry');
    setWordLanginnerHTML('dateEmissionLabel','dateOfIssue');
    setWordLanginnerHTML('dateExpirationLabel','expirationDate');
    setWordLanginnerHTML('genderLabel','gender');
    setWordLanginnerHTML('dateBLabel','birthdate');
    setWordLanginnerHTML('countryBLabel','countryOfBirth');
    setWordLanginnerHTML('statusLabel','maritalStatus');
    setWordLanginnerHTML('phoneLabel','phone');
    setWordLanginnerHTML('requiredLevel1','requiredLevel1');
    setWordLanginnerHTML('requiredLevel2','requiredLevel2');
    setWordLanginnerHTML('requiredLevel3','requiredLevel3');
    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');
    setWordLanginnerHTML('expSessionButton','continue');
    
    /*Placeholder*/
    setWordLangPlaceHolder('firstName','firstName');
    setWordLangPlaceHolder('secondName','secondName');
    setWordLangPlaceHolder('firstSurname','surname');
    setWordLangPlaceHolder('secondSurname','secondSurname');
    setWordLangPlaceHolder('number','exampleId');
    setWordLangPlaceHolder('phone','exmpleNumber');
    
    /* Select */
    setWordLanginnerHTML('selectVenezuelan','select');
    setWordLanginnerHTML('selectVenezuelanYes','yes');
    setWordLanginnerHTML('selectVenezuelanNo','no');
    setWordLanginnerHTML('selectAmerican','select');
    setWordLanginnerHTML('selectAmericanYes','yes');
    setWordLanginnerHTML('selectAmericanNo','no');
    setWordLanginnerHTML('selectIDSel','select');
    setWordLanginnerHTML('selectID1','id');
    setWordLanginnerHTML('selectID2','passport');
    setWordLanginnerHTML('selectID3','license');
    setWordLanginnerHTML('selectGenderSel','select');
    setWordLanginnerHTML('male','male');
    setWordLanginnerHTML('female','female');
    setWordLanginnerHTML('selectStatusSel','select');
    setWordLanginnerHTML('selectStatus1','single');
    setWordLanginnerHTML('selectStatus2','married');
    setWordLanginnerHTML('selectStatus3','divorced');
    setWordLanginnerHTML('selectStatus4','widower');
    
    getEntry(getParameterByName('sessionid'));

}



