    var nnul = 0;
    var startobj = 0;
    var servers;
    var origin_servers;
    var sdata;
    var ct;
    var settime;
    var stmp = '';
    var finished = [];
    let try_count = 0;
    const MAX_RETRY = 150;
    const CHECK_FILE = 0;
    const SAVE_FILE = 1;
    const RECHECK_FILE = 2;

    function setresponse(res) {
        servers = res.split("*");
        origin_servers = servers;
        ct = servers.length;
        section = "";
        console.log("count section ", ct);
        for (section_no = 1; section_no <= ct; section_no++) {
            section += "<div id='section_" + section_no + "' class='section'></div>";
        }
        document.getElementById("presult").innerHTML = section;
        $(".btn-download, .btn-save").prop("disabled", true);
    }

    function addskip(spart_no) {
    }

    function next_section(current_section) {
        current_section = current_section || 0;
        for (var idx = current_section; idx < ct; idx++) {
            if (servers[idx] != null) {
                return idx + 1;
            }
        }
        return '';
    }

    function get_section_from_server(server_no) {
        for (index = 0; index < origin_servers.length; index++) {
            if (origin_servers[index] == server_no) {
                let section_no = index + 1;
                return section_no
            }
        }
        return 0;
    }

    function pulldata() {
        onpro_multi = ct;
        finished = 0;
        action_type = 0;
        part_no = 1;
        key = document.getElementById("key").value;

        $('.tprogress').show('slow');
        clearTimeout(settime);
        if (action_type == SAVE_FILE)
            $('.pstatus').text('Saving data...');
        else if (action_type == RECHECK_FILE)
            $('.pstatus').text('Reload data...');
        else if (action_type == CHECK_FILE)
            $('.pstatus').text('Receive data...');

        for (section_no = 1; section_no <= ct; section_no++) {
            console.log("call multi get key", key," section", section_no)
            setTimeout(() => {
                multiget(0, key, part_no, section_no);
            }, section_no*500); 
        }
    }

    function reloaddata(active_servers) {
        console.log("reload data trigger");
        $(".tprogress").show("slow");
        $(".pstatus").text("Reload data...");
        origin_servers = servers;
        servers = active_servers;
        ct = servers.length;
        ga('send', 'event', "File>ReloadData", "start");
        getdata(SAVE_FILE);
    }

    function report_error(key) {
        $.ajax({
            type: "POST",
            url: homeUrl + "developer/report_error",
            data: {
                key: key,
                a3g4d21h4k: readCookie('k55b1n5f8')
            },
            cache: false,
            success: function(res) {
            }
        });
    }

    function resume_check(key, section) {
        console.log("trigger resume check",section);
        $('.tprogress').show('slow');
        $('.pstatus').text('Processing...');
        $.ajax({
            type: "GET",
            url: serverurl + `req2/resume_section?key=${key}&section=${section}&ext=${ext}`,
            cache: false,
            success: function(res) {
                if (res == "success" || res.length > 30 ) {
                    console.log("resume check success");
                    let check_file = 0;
                    // if (section == 1)
                    //     getdata(check_file);
                    // resume check file if after this section available
                    $(".pstatus").text("Receive data...");
                    document.getElementById(
                      "section_" + section
                    ).innerHTML += res;
                    let next_section_no = next_section(section);
                    if (next_section_no != '') {
                        setTimeout(function () {
                            resume_check(key, next_section_no);
                        }, 500);
                    } else {
                        $('.tprogress').hide('slow');
                        $('.pstatus').text("Selesai dalam " + ((Date.now() - tstart) / 1000) + " detik");
                        ga('send', 'event', "File>GetData", "finish", key + "_" + section + " in " +((Date.now() - tstart) / 1000) + "s");
                        settime = setTimeout(function() {
                            $('.pstatus').text("");
                        }, 5000);
                    }
                    ga('send', 'event', "File>ResumeCheck", "success", section);
                } else {
                    console.log("resume check fail");
                    setTimeout(() => {
                        resume_check(key, section);
                    }, 3000);
                }
            }
        });
    }

    function getdata(action_type, key, part_no, section_no) {
        part_no = part_no || 1;
        key = key || document.getElementById("key").value;
        if (part_no == 1) {
            $('.tprogress').show('slow');
            clearTimeout(settime);
            if (action_type == SAVE_FILE)
                $('.pstatus').text('Saving data...');
            else if (action_type == RECHECK_FILE)
                $('.pstatus').text('Reload data...');
            else if (action_type == CHECK_FILE)
                $('.pstatus').text('Receive data...');
        }
        if (section_no == null) {
            section_no = 1;
            // Reset section html
            if (action_type == SAVE_FILE || action_type == RECHECK_FILE) {
                stmp = '';
            }
        }
        console.log("request get data section no", section_no)
        $.ajax({
            type: "POST",
            url: homeUrl + "upload/get_data_new",
            data: {
                key: key,
                part: part_no,
                section: section_no,
                a3g4d21h4k: readCookie('k55b1n5f8')
            },
            cache: false,
            success: function(res) {
                ga('send', 'event', "File>GetData", "success", part_no);
                if (res == "on progress" || res.indexOf("502 Bad Gateway") > -1 || res.indexOf("Gateway Timeout") > -1) {
                    if (try_count < MAX_RETRY) {
                        return setTimeout(() => {
                            console.log("get data retry", ++try_count);
                            ga('send', 'event', "File>GetData", "retry", key + "_" + section_no + "_" + part_no, try_count);
                            getdata(action_type, key, part_no, section_no);
                        }, 5000);
                    } else {
                        ga('send', 'event', "File>GetData", "exceeded retry limit", key + "_" + section_no + "_" + part_no);
                        alert("Gagal! Server tidak merespon permintaan anda.. \nJika kendala terulang kembali, laporkan kepada teknisi kami.\nTerima kasih");
                        $('.tprogress').hide('slow');
                        try_count = 0;
                        $('.pstatus').text("");
                        onpro = 0;
                    }
                }
                if (res != '' && startobj != 1)
                    startobj = 1;
                if (res == '' && startobj == 1)
                    nnul++;
                else
                    part_no++;
                if (res != "15082015" && res != null) {
                    // If is not clearly finish
                    if (res.indexOf("Request Timeout") == -1 && res != 'skip') {
                        if (action_type == 1 || action_type == 2) {
                            stmp += res;
                        } else {
                            document.getElementById("section_" + section_no).innerHTML += res.replace("15082015","");
                            addIndex(section_no);
                        }
                        return getdata(action_type, key, part_no, section_no);
                    } else {
                        addskip(section_no);
                        report_error(key);
                        ga('send', 'event', "File>GetData", "skip", key + "_" + section_no + "_" + part_no);
                        onpro = 0;
                        alert("Skip!!!");
                    }
                } else {
                    if (action_type == SAVE_FILE || action_type == RECHECK_FILE) {
                        if (stmp != '' && document.getElementById("section_" + section_no) != null) {
                            document.getElementById("section_" + section_no).innerHTML = stmp.replace("15082015","");
                        }
                    }
                    let next_section_no = next_section(section_no);
                    if (next_section_no != '') {
                        if (action_type == SAVE_FILE || action_type == RECHECK_FILE) {
                            // Reset Section Temporary Store
                            stmp = '';
                        }
                        // resume check file if after this section available
                        // let after_this_section = next_section(next_section_no);
                        // if (after_this_section != '') {
                        //     setTimeout(function () {
                        //         resume_check(key, after_this_section);
                        //     }, 500);
                        // }
                        return setTimeout(function () {
                            getdata(action_type, key, 1, next_section_no);
                        }, 1500);
                    } else {
                        $('.tprogress').hide('slow');
                        try_count = 0;
                        $('.pstatus').text("Selesai dalam " + ((Date.now() - tstart) / 1000) + " detik");
                        ga('send', 'event', "File>GetData", "finish", key + "_" + section_no + "_" + part_no + " in " +((Date.now() - tstart) / 1000) + "s");
                        settime = setTimeout(function() {
                            $('.pstatus').text("");
                        }, 5000);
                        onpro = 0;
                        if (action_type == SAVE_FILE) {
                            servers = origin_servers;
                            ct = servers.length;
                        }
                    }
                }
            },
            error: function(e) {
                report_error(key);
                ga('send', 'event', "File>GetData", "error", key + "_" + section_no + "_" + part_no);
                alert("Gagal! Server tidak merespon permintaan anda.. \nJika kendala terulang kembali, laporkan kepada teknisi kami.\nTerima kasih");
                $('.tprogress').hide('slow');
                try_count = 0;
                $('.pstatus').text("");
                onpro = 0;
            }
        });
    }

    function multiget(action_type, key, part_no, section_no, retry =0, error_retry =0) {
        part_no = part_no || 1;
        key = key || document.getElementById("key").value;
        if (section_no == null) {
            section_no = 1;
            // Reset section html
            if (action_type == SAVE_FILE || action_type == RECHECK_FILE) {
                stmp = '';
            }
        }
        console.log("multi get section", section_no, "part", part_no)
        $.ajax({
            type: "POST",
            url: homeUrl + "upload/get_data_new",
            data: {
                key: key,
                part: part_no,
                section: section_no,
                a3g4d21h4k: readCookie('k55b1n5f8')
            },
            cache: false,
            success: function (res) {
                console.log("multi get section success", section_no, "part", part_no)
                ga('send', 'event', "File>GetData", "success", part_no);
                if (res == "on progress" || res.indexOf("502 Bad Gateway") > -1) {
                    if (retry < MAX_RETRY) {
                        ++retry;
                        return setTimeout(() => {
                            console.log("get data retry", retry);
                            ga('send', 'event', "File>GetData", "retry", key + "_" + section_no + "_" + part_no, retry);
                            getdata(action_type, key, part_no, section_no, retry);
                        }, 2500);
                    } else {
                        ga('send', 'event', "File>GetData", "exceeded retry limit", key + "_" + section_no + "_" + part_no);
                        alert("Gagal! Server tidak merespon permintaan anda.. \nJika kendala terulang kembali, laporkan kepada teknisi kami.\nTerima kasih");
                        retry = 0;
                        onpro_multi--;
                        if (onpro_multi == 0) {
                            $('.tprogress').hide('slow');
                            $('.pstatus').text("selesai dalam " + ((Date.now() - tstart) / 1000) + " detik");
                            ga('send', 'event', "File>GetData", "finish With Error", key + "_" + section_no + "_" + part_no + " in " + ((Date.now() - tstart) / 1000) + "s");
                            settime = setTimeout(function () {
                                $('.pstatus').text("");
                            }, 5000);
                        }
                    }
                }
                if (res != '' && startobj != 1)
                    startobj = 1;
                if (res == '' && startobj == 1)
                    nnul++;
                else
                    part_no++;
                if (res != "15082015" && res != null) {
                    // If is not clearly finish
                    if (res.indexOf("Request Timeout") == -1 && res != 'skip') {
                        if (action_type == 1 || action_type == 2) {
                            stmp += res;
                        } else
                            document.getElementById("section_" + section_no).innerHTML += res.replace("15082015", "");
                        return setTimeout(() => {
                            getdata(action_type, key, part_no, section_no, retry);
                        }, 1500);
                    } else {
                        addskip(section_no);
                        report_error(key);
                        ga('send', 'event', "File>GetData", "skip", key + "_" + section_no + "_" + part_no);
                        alert("Skip section "+section_no+"!!!");
                        onpro_multi--;
                    }
                } else {
                    if (action_type == SAVE_FILE || action_type == RECHECK_FILE) {
                        if (stmp != '' && document.getElementById("section_" + section_no) != null) {
                            document.getElementById("section_" + section_no).innerHTML = stmp.replace("15082015", "");
                        }
                    }
                    console.log("section", section_no, "is finish");
                    onpro_multi--;
                    finished.push(section_no);
                    if (onpro_multi == 0) {
                        $('.tprogress').hide('slow');
                        $('.pstatus').text("selesai dalam " + ((Date.now() - tstart) / 1000) + " detik");
                        ga('send', 'event', "File>GetData", "finish", key + "_" + section_no + "_" + part_no + " in " + ((Date.now() - tstart) / 1000) + "s");
                        settime = setTimeout(function () {
                            $('.pstatus').text("");
                        }, 5000);
                        console.log("all finish!");
                        if (action_type == SAVE_FILE) {
                            servers = origin_servers;
                            ct = servers.length;
                        }
                    }
                }
            },
            error: function (e) {
                report_error(key);
                ga('send', 'event', "File>GetData", "error", key + "_" + section_no + "_" + part_no);
                alert("Gagal! Server tidak merespon permintaan anda.. \nJika kendala terulang kembali, laporkan kepada teknisi kami.\nTerima kasih");
                if (onpro_multi == 0) {
                    $('.tprogress').hide('slow');
                    $('.pstatus').text("selesai dalam " + ((Date.now() - tstart) / 1000) + " detik");
                    ga('send', 'event', "File>GetData", "finish With Error", key + "_" + section_no + "_" + part_no + " in " + ((Date.now() - tstart) / 1000) + "s");
                    settime = setTimeout(function () {
                        $('.pstatus').text("");
                    }, 5000);
                } else if (error_retry < MAX_ERROR_RETRY) {
                    error_retry++;
                    return setTimeout(() => {
                        console.log("get data error retry", error_retry);
                        ga('send', 'event', "File>GetData", "error retry", key + "_" + section_no + "_" + part_no, error_retry);
                        getdata(action_type, key, part_no, section_no, retry, error_retry);
                    }, 2500);
                } else {

                }
            }
        });
    }

    function enddata(key) {
        key = key || document.getElementById("key").value;
        var nnul = 0;
        var startobj = 0;
        $.ajax({
            type: "POST",
            url: "upload/end_data",
            data: {
                key: key,
                a3g4d21h4k: readCookie('k55b1n5f8')
            },
            cache: false,
            success: function(res) {},
            error: function() {
                ga('send', 'event', "File>EndData", "error");
                alert("Gagal! Server tidak merespon permintaan anda.. \nJika kendala terulang kembali, laporkan kepada teknisi kami.\nTerima kasih");
            }
        });
    }