window.onload = function(){
    var sessionId = sessionStorage.getItem("sessionId");
    var ref = getParameterByName("ref");
    initApp();
    
    if(ref != "" && ref != null){
        document.getElementById("search").value = ref;
    }
    
    var filter = document.getElementById("search").value;
    if(filter == null || filter == undefined){
        filter  = '';
    }
    
    LoadPairs(sessionId);
    LoadOrders(filter, 0,  '', document.getElementById("registerFilter").value, sessionId, '', '', '', '');
    
    document.getElementById('cancelOrder').addEventListener('click', function(){
        console.log(document.getElementById('orderid').value);
        orderCancel(sessionId, document.getElementById('orderid').value);
    });
    
    document.getElementById("iconSearch").addEventListener("click", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    document.getElementById("orderBy").addEventListener("change", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    document.getElementById("registerFilter").addEventListener("change", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    document.getElementById("rangeFilter").addEventListener("change", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    document.getElementById("typeFilter").addEventListener("change", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    document.getElementById("pairFilter").addEventListener("change", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    document.getElementById("statusFilter").addEventListener("change", function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    // Event to input search
    document.getElementById("search").addEventListener("change",function(){
        var filter = document.getElementById("search").value;
        if(filter == null || filter == undefined){
            filter  = '';
        }
        
        //Se desmarca la pagina anterior
        var pSel = document.getElementsByClassName("pSelect");
        if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
        //Colocamos la pagina 1
        var page1 = document.getElementsByClassName("pages")[0];
        if(page1 != undefined){
            page1.classList.add("pSelect");
        }
            
        LoadOrders(filter, 0,  document.getElementById("orderBy").value, document.getElementById("registerFilter").value, sessionId, document.getElementById('rangeFilter').value, document.getElementById('typeFilter').value,document.getElementById('pairFilter').value, document.getElementById('statusFilter').value);
    });
    
    /*funcion del ordenar**/
     
        var orderDirect = document.getElementById("orderDirect");
        if(orderDirect!==null){
            orderDirect.addEventListener("change",function(){
                document.getElementById("orderBy").dispatchEvent(new Event("change"));
            });
        }
};

function showModal(orderId){
    document.getElementById('orderid').value = orderId;
    var modal = document.getElementById("modalconfir");
    modal.style.display = "block";
    window.onclick = function(event) {
        if (event.target == modal) {
            modal.style.display = "none";
        }
    }
}

function LoadOrders(filter, offset, order, numofrecords, sessionid, daterange, type, pairid, status){
    var par = {};
    par.filter = filter;
    par.sessionid = sessionid;
    par.offset = offset;
    par.numofrecords = numofrecords;
    var order = Math.abs(order);
    var dir = document.getElementById("orderDirect").value;
    if(dir=="desc")order="-"+order;
    par.order = order;
    par.daterange = daterange;
    par.type = type;
    par.pairid = pairid;
    par.status = status;
    console.log(par);
    callWS("GET", "orders/list", par, respuesta);
        function respuesta(status, rsp){
            let resp = JSON.parse(rsp);
            var msg = document.getElementById("messageApi");
            switch(status){
                case 200:
                    console.log("respuesta", resp);
                        if (resp.numofrecords === 0){
                            /*Blanquear tabla*/
                            var nodeNoDelete = document.getElementById("noCurrencieDefine");
                            if (nodeNoDelete ){
                                var nodeClone = nodeNoDelete.cloneNode(true);   
                                nodeClone.style.display="none";
                            }
                            var table = document.getElementById("bodyTableCurrencies");
                            table.innerHTML = "";
                            if(nodeClone!==undefined && nodeClone!==null)table.appendChild(nodeClone);
                            /**-hasta aqui-**/
                            document.getElementById("noCurrencieDefine").style.display= "flex";
                            document.getElementById('noCurrencieDefine').style.justifyContent = "center";
                            document.getElementById("msgInTable").innerHTML="No hay ordenes";
                            document.getElementById("paginator").style.display= "none";
                        } else {
                            
                            if(resp.numofrecords <= resp.records.length){
                                document.getElementById("paginator").style.display= "none";    
                            } else {
                                document.getElementById("paginator").style.display= ""; 
                                drawPags(resp.numofrecords, "numbersPaginator",resp.records.length);
                            }
                            /*Blanquear tabla*/
                            var nodeNoDelete = document.getElementById("noCurrencieDefine");
                            if (nodeNoDelete ){
                                var nodeClone = nodeNoDelete.cloneNode(true);   
                                nodeClone.style.display="none";
                            }
                            var table = document.getElementById("bodyTableCurrencies");
                            table.innerHTML = "";
                            if(nodeClone!==undefined && nodeClone!==null)table.appendChild(nodeClone);
                            /**-hasta aqui- **/
                            //Pintar registros
                            paintRows(resp);
                        }
                break;
                
                case 400:
                    msg.innerHTML= resp.errmsg;
                    msg.classList.add("msgError");
                    msg.style.display="block";
                    setTimeout(function() {
                        msg.innerHTML = "";
                        msg.classList.remove("msgError");   
                        msg.style.display="none";
                    }, 3000);
                break;
                    
                case 401:
                    msg.innerHTML="Sesi\u00F3n expirada o inv\u00E1lida, vuelva a iniciar sesi\u00F3n";
                    msg.classList.add("msgError"); 
                    msg.style.display="block";
                    setTimeout(function() {
                        msg.innerHTML = "";
                        msg.classList.remove("msgError"); 
                        myLogout();
                        msg.style.display="none";
                    }, 3000);
                break;
                
                case 403:
                    msg.innerHTML="No posee privilegio para realizar esta operaci\u00F3n";
                    msg.classList.add("msgError"); 
                    msg.style.display="block";
                    /*setTimeout(function() {
                        msg.innerHTML = "";
                        msg.classList.remove("msgError"); 
                        msg.style.display="none";
                    }, 3000);*/
                break;
                
                case 500:
                    msg.innerHTML="Error Interno";
                    msg.classList.add("msgError"); 
                    msg.style.display="block";
                    setTimeout(function() {
                        msg.innerHTML = "";
                        msg.classList.remove("msgError"); 
                        msg.style.display="none";
                    }, 3000);
                break;
                
                default:
                    msg.innerHTML= resp.errmsg;
                    msg.classList.add("msgError"); 
                    msg.style.display="block";
                    setTimeout(function() {
                        msg.innerHTML = "";
                        msg.classList.remove("msgError"); 
                        msg.style.display="none";
                    }, 3000);
            }
        }
}

//Funcion para el Paginado
//Dios santo esto hace falta comentarlo
var pagerNumber = 10;
function drawPags(numofrecords, tbl){
    var pages = Math.ceil( numofrecords / pagerNumber );
    var numbersPaginator = document.getElementById(tbl);
    var lastPag = 0;
    if(numbersPaginator.getElementsByClassName("pSelect").length>0){
        lastPag = numbersPaginator.getElementsByClassName("pSelect")[0].getAttribute("data-page");
    }
    numbersPaginator.innerHTML = "";
    
    var initialPage=0;
    var n=8;
    if (pages>n){  // cambio de formato en caso de mas de n paginas
        initialPage = Math.max((lastPag-Math.ceil(n/2)+2), 1)-1;
    }
    
    // puntitos iniciales cuando son más de 10 página y no se empieza por la primera
    if (pages>n && initialPage !== 0 && initialPage !== 1){
        spanF = document.createElement("div");
        spanF.classList.add("pages");
        spanF.innerHTML = "1 ...";
        spanF.dataset.page= 1;
        spanF.style.cursor="pointer";
        spanF.addEventListener("click", function(){
            //Se desmarca la página anterior
            var pSel = document.getElementsByClassName("pSelect");
            if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
            //Se actualiza la página seleccionada
            this.classList.add("pSelect");
            
            //Se repinta el listado desde el offset seleccionado
                    var filter = document.getElementById("search").value;
            
                    if(filter == null || filter == undefined){
                        filter  = '';
                    }
                    var offset = (pSel[0].getAttribute('data-page')-1)*pagerNumber;
                    var sessionId = sessionStorage.getItem("sessionId");
                    var order = document.getElementById('orderBy').value;
                    var numofrecords = document.getElementById('registerFilter').value;
                    var daterange = document.getElementById('rangeFilter').value;
                    var type = document.getElementById('typeFilter').value;
                    var pairid = document.getElementById('pairFilter').value;
                    var status = document.getElementById('statusFilter').value;
            
                    LoadOrders(filter, offset, order, numofrecords, sessionId, daterange, type, pairid, status);
        });
        numbersPaginator.appendChild(spanF);
        /*
        span = document.createElement("div");
        span.classList.add("pages");
        span.innerHTML = "...";
        numbersPaginator.appendChild(span);*/
        
    }

    //Pintar paginas de table (numeros)
    for(var k=initialPage; k<pages && !(k-initialPage > n-1); k++){
        var p = document.createElement("div");
        p.classList.add("pages");
        p.innerHTML = k+1;
        p.dataset.page= k+1;
        if(lastPag==0 && k==0)p.classList.add("pSelect");//Se selecciona la página 1
        else if(lastPag!==0 && (lastPag-1)==k)p.classList.add("pSelect");
        
        p.addEventListener("click", function(){
            //Se desmarca la página anterior
            var pSel = document.getElementsByClassName("pSelect");
            if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
            //Se actualiza la página seleccionada
            this.classList.add("pSelect");
            
            //Se repinta el listado desde el offset seleccionado
                    var filter = document.getElementById("search").value;
            
                    if(filter == null || filter == undefined){
                        filter  = '';
                    }
                    var offset = (pSel[0].getAttribute('data-page')-1)*pagerNumber;
                    var sessionId = sessionStorage.getItem("sessionId");
                    var order = document.getElementById('orderBy').value;
                    var numofrecords = document.getElementById('registerFilter').value;
                    var daterange = document.getElementById('rangeFilter').value;
                    var type = document.getElementById('typeFilter').value;
                    var pairid = document.getElementById('pairFilter').value;
                    var status = document.getElementById('statusFilter').value;
            
                    LoadOrders(filter, offset, order, numofrecords, sessionId, daterange, type, pairid, status);
        });
        p.style.cursor="pointer";
        numbersPaginator.appendChild(p);
                                
    }
    
    // puntitos si no se ven las útlimas páginas
    if (pages>n && initialPage+n<pages){
        spanL = document.createElement("div");
        spanL.classList.add("pages");
        spanL.innerHTML = "... "+pages;
        spanL.dataset.page= pages;
        spanL.style.cursor="pointer";
        spanL.addEventListener("click", function(){
            //Se desmarca la página anterior
            var pSel = document.getElementsByClassName("pSelect");
            if(pSel.length>0)pSel[0].classList.remove("pSelect");
            
            //Se actualiza la página seleccionada
            this.classList.add("pSelect");
            
            //Se repinta el listado desde el offset seleccionado
                    var filter = document.getElementById("search").value;
            
                    if(filter == null || filter == undefined){
                        filter  = '';
                    }
                    var offset = (pSel[0].getAttribute('data-page')-1)*pagerNumber;
                    var sessionId = sessionStorage.getItem("sessionId");
                    var order = document.getElementById('orderBy').value;
                    var numofrecords = document.getElementById('registerFilter').value;
                    var daterange = document.getElementById('rangeFilter').value;
                    var type = document.getElementById('typeFilter').value;
                    var pairid = document.getElementById('pairFilter').value;
                    var status = document.getElementById('statusFilter').value;
            
                    LoadOrders(filter, offset, order, numofrecords, sessionId, daterange, type, pairid, status);
        });
        numbersPaginator.appendChild(spanL);
    }
    
    
    
    //Asignación de eventos validar que sólo se haga una vez
    if(numbersPaginator.getAttribute("arrowsEvent")=="false"){
        var leftArrow = numbersPaginator.previousElementSibling;
        var rightArrow = numbersPaginator.nextElementSibling;
        //Evento flecha izquierda
        leftArrow.addEventListener("click",function(){
            var pSel = document.getElementsByClassName("pSelect");
            if(pSel.length>0){
                //Si el elemento anterior al seleccionado es una pagina
                if(pSel[0].previousElementSibling.classList.contains("pages")){
                    var last = pSel[0];
                    pSel[0].previousElementSibling.classList.add("pSelect");
                    last.classList.remove("pSelect");
                   //Se repinta el listado desde el offset seleccionado
                    var filter = document.getElementById("search").value;
            
                    if(filter == null || filter == undefined){
                        filter  = '';
                    }
                    var offset = (pSel[0].getAttribute('data-page')-1)*pagerNumber;
                    var sessionId = sessionStorage.getItem("sessionId");
                    var order = document.getElementById('orderBy').value;
                    var numofrecords = document.getElementById('registerFilter').value;
                    var daterange = document.getElementById('rangeFilter').value;
                    var type = document.getElementById('typeFilter').value;
                    var pairid = document.getElementById('pairFilter').value;
                    var status = document.getElementById('statusFilter').value;
            
                    LoadOrders(filter, offset, order, numofrecords, sessionId, daterange, type, pairid, status);
                }
            }
        });
        //Evento flecha derecha
        rightArrow.addEventListener("click",function(){
            var pSel = document.getElementsByClassName("pSelect");
            if(pSel.length>0){
                //Si el elemento anterior al seleccionado es una pagina
                if(pSel[0].nextElementSibling.classList.contains("pages")){
                    pSel[0].nextElementSibling.classList.add("pSelect");
                    pSel[0].classList.remove("pSelect");
                    
                    //Se repinta el listado desde el offset seleccionado
                    var filter = document.getElementById("search").value;
            
                    if(filter == null || filter == undefined){
                        filter  = '';
                    }
                    var offset = (pSel[0].getAttribute('data-page')-1)*pagerNumber;
                    var sessionId = sessionStorage.getItem("sessionId");
                    var order = document.getElementById('orderBy').value;
                    var numofrecords = document.getElementById('registerFilter').value;
                    var daterange = document.getElementById('rangeFilter').value;
                    var type = document.getElementById('typeFilter').value;
                    var pairid = document.getElementById('pairFilter').value;
                    var status = document.getElementById('statusFilter').value;
            
                    LoadOrders(filter, offset, order, numofrecords, sessionId, daterange, type, pairid, status);
                }
            }
        });
        numbersPaginator.setAttribute("arrowsEvent",true);
    }
    
    
}

function paintRows(rows){
    console.log("rows to paint session ", rows)
    var pageInPaginator = document.getElementsByClassName("pSelect")[0];
    if(pageInPaginator){
        pageInPaginator = pageInPaginator.innerHTML;
    }
    var rowCurrencie = document.getElementById("bodyTableCurrencies");
    var filas;
    if (rows.numofrecords > rows.records.length){
        filas = parseInt(rows.numofrecords) - ((parseInt(pageInPaginator) - 1) * rows.records.length);
        if(filas > rows.records.length){
            filas = rows.records.length;
        }
        console.log(filas);
    } else {
        filas = rows.numofrecords
    }
    console.log(filas);
    for (let i=0; i<filas; i++){
        const index = i;
        var divContent= document.createElement("div");
        var div1= document.createElement("div");
        var div2= document.createElement("div");
        var div3= document.createElement("div");
        var div4= document.createElement("div");
        var div5= document.createElement("div");
        var div6= document.createElement("div");
        var div7= document.createElement("div");
        var div8= document.createElement("div");
        var div9= document.createElement("div");
        div1.classList.add("cell1");
        div2.classList.add("cell2");
        div3.classList.add("cell3");
        div4.classList.add("cell4");
        div5.classList.add("cell5");
        div6.classList.add("cell6");
        div7.classList.add("cell7");
        div8.classList.add("cell8");
        div9.classList.add("cell9");
        var divCnt1= document.createElement("div");
        var divCnt2= document.createElement("div");
        var divCnt3= document.createElement("div");
        var divCnt4= document.createElement("div");
        var divCnt5= document.createElement("div");
        var divCnt6= document.createElement("div");
        var divCnt7= document.createElement("div");
        var divCnt8= document.createElement("div");
        var divCnt9= document.createElement("div");
        divCnt1.classList.add("divCellCnt");
        divCnt2.classList.add("divCellCnt");
        divCnt3.classList.add("divCellCnt");
        divCnt4.classList.add("divCellCnt");
        divCnt5.classList.add("divCellCnt");
        divCnt6.classList.add("divCellCnt");
        divCnt7.classList.add("divCellCnt");
        divCnt8.classList.add("divCellCnt");
        divCnt9.classList.add("divCellCnt");
        // fecha
        var dateOrder = document.createElement("span");
        // ref
        var refOrder = document.createElement("span");
        // par
        var parOrder = document.createElement("span");
        // kind
        var kindOrder = document.createElement("span");
        // id bid 
        var userOrder = document.createElement("span");
        // id ask
        var qtyOrder = document.createElement("span");
        //qty
        var priceOrder = document.createElement("span");
        //price
        var totalOrder = document.createElement("span");
        //total
        var filledOrder = document.createElement("span");
        
        dateOrder.disabled=true;
        dateOrder.setAttribute("id", "dateOrder"+index);
        dateOrder.innerHTML = rows.records[i].date.formatted; 
        divCnt1.appendChild(dateOrder);
        div1.appendChild(divCnt1);
        
        refOrder.disabled=true;
        refOrder.setAttribute("id", "refOrder"+index)
        refOrder.innerHTML = rows.records[i].id;
        refOrder.style.cursor = "pointer";
        refOrder.addEventListener("click",function (){
           gotoPage('menu', "exchanges",{"userName": getParameterByName("userName"), "ref": rows.records[i].id}); 
        });
        divCnt2.appendChild(refOrder);
        div2.appendChild(divCnt2);
        
        parOrder.disabled=true;
        parOrder.setAttribute("id", "parOrder"+index)
        parOrder.innerHTML = rows.records[i].pair.dsc;
        divCnt3.appendChild(parOrder);
        div3.appendChild(divCnt3);
        
        kindOrder.disabled=true;
        kindOrder.setAttribute("id", "kindOrder"+index)
        kindOrder.innerHTML = rows.records[i].type.dsc;
        if(rows.records[i].side.id == 1){
            divCnt4.classList.add("red");
        }else if(rows.records[i].side.id == 2){
            divCnt4.classList.add("green");
        }
        divCnt4.appendChild(kindOrder);
        div4.appendChild(divCnt4);
        
        userOrder.disabled=true;
        userOrder.setAttribute("id", "userOrder"+index)
        userOrder.innerHTML = rows.records[i].user.usr;
        divCnt5.appendChild(userOrder);
        div5.appendChild(divCnt5);
        
        qtyOrder.disabled=true;
        qtyOrder.setAttribute("id", "qtyOrder"+index);
        qtyOrder.innerHTML = rows.records[i].qty.formatted;
        divCnt6.appendChild(qtyOrder);
        div6.appendChild(divCnt6);
        
        priceOrder.disabled=true;
        priceOrder.setAttribute("id", "priceOrder"+index);
        priceOrder.innerHTML = rows.records[i].price.formatted;
        divCnt7.appendChild(priceOrder);
        div7.appendChild(divCnt7);
        
        totalOrder.disabled=true;
        totalOrder.setAttribute("id", "totalOrder"+index);
        totalOrder.innerHTML = rows.records[i].total.formatted;
        divCnt8.appendChild(totalOrder);
        div8.appendChild(divCnt8);
        
        filledOrder.disabled=true;
        filledOrder.setAttribute("id", "filledOrder"+index);
        filledOrder.innerHTML = rows.records[i].filled.formatted;
        
        cancelOrder = document.createElement("div");
        item1 = document.createElement('i');
        item2 = document.createElement('i');
        toolTipText = document.createElement("span");
        
        if(rows.records[i].status == 1){
            item1.classList.add("fas","fa-check-square","blue");
            item2.classList.add("fa","fa-ellipsis-v","blue");
        }else if(rows.records[i].status == 0){
            item1.classList.add("far","fa-square","blue");
            item2.classList.add("fa","fa-ellipsis-v","blue");
        }else if(rows.records[i].status == -1){
            item1.classList.add("fas","fa-window-close","red");
            item2.classList.add("fa","fa-ellipsis-v","blue");
        }
        
        
        toolTipText.classList.add('tooltiptext');
        toolTipText.innerHTML = 'Cancelar';
        cancelOrder.setAttribute("id", "p"+index);
        cancelOrder.classList.add("cancelButton");
        if(rows.records[i].status == 0){
            cancelOrder.appendChild(toolTipText);
            cancelOrder.addEventListener("click", function(){
                showModal(rows.records[i].id);
            });
        }
        divCnt9.appendChild(filledOrder);
        cancelOrder.appendChild(item1);
        cancelOrder.appendChild(item2);
        divCnt9.appendChild(cancelOrder);
        div9.appendChild(divCnt9);
        
        divContent.classList.add("rowCurrencie");
        if(i%2 === 0){
             divContent.classList.add("rowImpar");
        }
        divContent.setAttribute("id", "liSession"+index);
        divContent.dataset.id= rows.records[i].id;
        divContent.appendChild(div1);
        divContent.appendChild(div2);
        divContent.appendChild(div3);
        divContent.appendChild(div4);
        divContent.appendChild(div5);
        divContent.appendChild(div6);
        divContent.appendChild(div7);
        divContent.appendChild(div8);
        divContent.appendChild(div9);
        divContent.style.display = "table";
        divContent.style.width = "96%";
        
        rowCurrencie.appendChild(divContent);
    }
    
    for(let j = rows.records.length; j<10; j++){
        var divContent= document.createElement("div");
        var inputName= document.createElement("div");
        var inputSymbol= document.createElement("div");
        var inputStatus= document.createElement("div");
        const index = j;
        
        inputName.classList.add("inputRowCurrencie");
        inputSymbol.classList.add("inputRowCurrencie");
        inputStatus.classList.add("inputRowCurrencie");
        
        divContent.classList.add("rowCurrencie");
        if(j%2 === 0){
             divContent.classList.add("rowImpar");
        }
        divContent.appendChild(inputName);
        divContent.appendChild(inputSymbol);
        divContent.appendChild(inputStatus);
        divContent.style.display = "table";
        divContent.style.width = "96%";
        
        rowCurrencie.appendChild(divContent);
    }
}

function orderCancel(sessionid, orderid){
    var req = {};
    req.sessionid = sessionid;
    req.id = orderid
    
    callWS("GET", "orders/cancel", req, respuesta);
    function respuesta(status, rsp){
        let resp = JSON.parse(rsp);
        var msg = document.getElementById("messageApi");
        document.getElementById("modalconfir").style.display = "none";
        switch(status){
            case 200:
                msg.classList.add("msgSucess");
                msg.innerHTML = "Orden cancelada correctamente";
                msg.style.display = "block";
                
                //Se desmarca la pagina anterior
                var pSel = document.getElementsByClassName("pSelect");
                if(pSel.length>0)pSel[0].classList.remove("pSelect");
        
                //Colocamos la pagina 1
                var page1 = document.getElementsByClassName("pages")[0];
                if(page1 != undefined){
                    page1.classList.add("pSelect");
                }
                
                //Se repinta el listado desde el offset seleccionado
                var filter = document.getElementById("search").value;
            
                if(filter == null || filter == undefined){
                    filter  = '';
                }
                var sessionId = sessionStorage.getItem("sessionId");
                var order = document.getElementById('orderBy').value;
                var numofrecords = document.getElementById('registerFilter').value;
                var daterange = document.getElementById('rangeFilter').value;
                var type = document.getElementById('typeFilter').value;
                var pairid = document.getElementById('pairFilter').value;
                var status = document.getElementById('statusFilter').value;
            
                LoadOrders(filter, 0, order, numofrecords, sessionId, daterange, type, pairid, status);
                setTimeout(function() {
                    msg.classList.remove("msgSucess");
                    msg.innerHTML = "";
                    msg.style.display="none";
                }, 2000);
                break;
            case 401:
                msg.innerHTML="Sesi\u00F3n expirada o inv\u00E1lida, vuelva a iniciar sesi\u00F3n";
                msg.classList.add("msgError"); 
                msg.style.display="block";
                setTimeout(function() {
                    msg.innerHTML = "";
                    msg.classList.remove("msgError"); 
                    myLogout();
                    msg.style.display="none";
                }, 3000);
                break;
            case 402:
                msg.innerHTML="Orden ya cancelada";
                msg.classList.add("msgError");
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);
                break;
            case 403:
                msg.innerHTML="No posee privilegio para realizar esta operaci\u00F3n";
                msg.classList.add("msgError"); 
                msg.style.display="block";
                setTimeout(function() {
                    msg.innerHTML = "";
                    msg.classList.remove("msgError"); 
                    msg.style.display="none";
                }, 3000);
                break;
            case 404:
                msg.innerHTML = "Orden no encontrada";
                msg.classList.add("msgError");
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);
                break;
            case 500:
                msg.innerHTML="Error Ordenes";
                msg.classList.add("msgError");
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);
                break;
            default:
                msg.innerHTML= respuesta.msg;
                msg.classList.add("msgError");
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);
        }
    }
}


function LoadPairs(sessionid){
    var req = {};
    req.sessionid = sessionid;
    
    callWS("GET", "pairs/list", req, respuesta);
    function respuesta(status, rsp){
        let resp = JSON.parse(rsp);
        var pairs = document.getElementById("pairFilter");
        switch(status){
            case 200:
                for (let i=0; i<resp.records.length; i++){
                    var op = document.createElement("option");
                    if(resp.records.length == 0){
                        op.text= "No hay pares definidos";
                        op.disabled = true;
                        op.value = " ";
                        op.selected = true;
                        currencies.appendChild(op);
                    } else {
                        op.text= resp.records[i].dsc;
                        op.value = resp.records[i].id;
                        pairs.appendChild(op);
                    }
                }
                
                
            case 400:
                /*msg.innerHTML=respuesta.msg
                msg.style.color="red";
                msg.style.textAlign="center";
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);*/
                break;
                
            case 500:
                /*msg.innerHTML="Error pares"
                msg.style.color="red";
                msg.style.textAlign="center";
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);
                break;
            default:
                /*msg.innerHTML= respuesta.msg;
                msg.style.color="red";
                msg.style.textAlign="center";
                msg.style.display="block";
                setTimeout(function() {
                    msg.style.display="none";
                }, 3000);*/
        }
    }
}