﻿//export button onclick function to export files
function Export(tabName, type, hash) {
    var queryString = "q=" + query;
    $("#query-strings input").each(function(i) {
        queryString += "&" + $(this).attr("id") + "=" + $(this).attr("value");
    });
    
    if (USER_ROLE == "guest" /*|| tabName == "control-adinfo"*/) {
        window.location = "/research/exportFiles.aspx?tab=" + tabName + "&exportType=" + type + "&h=" + hash + "&" + queryString;
        HideExportProgress();
    }
    else {
        $.ajax({
            url: "/research/exportFiles.aspx?tab=" + tabName + "&exportType=" + type + "&h=" + hash + "&" + queryString,
            cache: false,
            success: function(html) {
                if (html == "[exceeded]") {
                    alert("Your export limit for today has been reached\r\nExport limit will be reset everyday at 12:00 AM Eastern Time");
                    HideExportProgress();
                }
                else
                    ShowExportProgress();
            },
            error: function(x, t, e) {
                alert("An error has occured\r\nPlease try again later");
                HideExportProgress();
            }
        });
    }
}

//Cancels Exporting
function KillExport() {
    stopExportTimer();
    
    $.ajax({
        url: "/research/tab.aspx?name=control-tasks&task=kill-thread",
        cache: false,
        success: function(html) {
            HideExportProgress();
        }
    });
}

//Direct File Download
function DownloadFile() {
    var curValue = $('.quota-header li:last').text();
    $('.quota-header li:last').text("Updating...");
    $.ajax({
        url: "/research/tab.aspx?name=control-tasks&task=current-export",
        cache: false,
        success: function(html) {
            $('.quota-header li:last').text(html); // refreshes the export counter on top of the page
            window.location = "/research/tab.aspx?name=control-tasks&task=download-file";
        },
        error: function(x, t, e) {
            $('.quota-header li:last').text(curValue);
        }
    });
}

//Loads progress windows at the bottom of the page via jquery ajax during export progress
function ShowExportProgress() {
    SetExportWaiting();

    if ($(".export-progress-popup").is(":hidden")) {
        $(".export-progress-popup").slideDown("slow");
    }
    
    startExportTimer();
}

function HideExportProgress() {
    $(".export-progress-popup").hide();
    stopExportTimer();
}

function SetExportWaiting() {
    $(".export-progress-popup").html("<div style='padding: 5px'>Please wait...</div>");
}

function GetProgressBox() {
    $.ajax({
        url: "/research/tab.aspx?name=control-progress",
        cache: false,
        success: function(html) {
            if (html.search("No Exports") != -1) {
                HideExportProgress();
            }
            else {
                $(".export-progress-popup").html(html);
                $(".export-progress-popup .cancel").each(function(i) {
                    $(this).bind("click", function() {
                        SetExportWaiting();
                        KillExport();
                    });
                });
                $(".export-progress-popup .cancel-button").each(function(i) {
                    $(this).bind("click", function() {
                        SetExportWaiting();
                        KillExport();
                    });
                });

                $(".export-progress-popup .download").each(function(i) {
                    $(this).attr("href", "javascript:DownloadFile()");
                });

                if (html.indexOf('download-file') != -1) {
                    HideExportProgress();
                }
            }
        },
        error: function(x, t, e) {
            stopExportTimer();
        }
    });
}

function startExportTimer() {
    $(".export-progress-popup").everyTime("10s", "export-progress", function() {
        GetProgressBox();
    });
}

function stopExportTimer() {
    $(".export-progress-popup").stopTime("export-progress");
}