﻿function styledPopupClose() {
    document.getElementById("styled_popup").style.display = "none";
};



//Checks or unchecks all gridview checkboxes based on the master gridview checkbox
function SelectAllClearAllCheckboxes() {
    $("#tGridView :checkbox:not([id$='_TITLECBX'])").each(function() {
        $(this).attr("checked", $("#tGridView :checkbox[id$='_TITLECBX']").is(":checked"));
    });
};

//Builds a '^' delimited string of the FDA Recall IDs of all selected recalls
function GetSelectedRecalls() {
    var ids = "";
    //Grab all checked checkboxes besides the master checkbox
    $("#tGridView :checkbox:not([id$='_TITLECBX'])[checked]").each(function() {
        //Grab the hidden field input next to it.
        $(this).next(":hiddenfield").each(function() {
            //Grab the Recall ID from the hidden field.
            ids += "^" + $(this).attr("value");
        });
    });
    return ids;
};


//Posts to the DocumentHandler.ashx to handle the print functionality.
function PrintDeviceRecalls() {
    var action = $("#aspnetForm").attr("action");
    var target = $("#aspnetForm").attr("target");

    $("#selectedRecalls").attr("value", GetSelectedRecalls());
    $("#aspnetForm").attr("action", "/DocumentHandler.ashx?mod=PrintFDADeviceRecalls");
    $("#aspnetForm").attr("target", "_blank");
    $("#aspnetForm").submit();
    $("#aspnetForm").attr("action", action.toString());
    $("#aspnetForm").attr("target", target.toString());
};


//Toggle the visibility of the routing reports on the device recalls print document.
function ToggleRoutingReports() {
    var cbx = $("#cbxRoutingReports");
    $("div[class='divRoutingReport']").each(function() {
        if ($(cbx).attr("checked") == true)
            $(this).attr("style", "display:block;");
        else
            $(this).attr("style", "display:none;");
    });
};

//Posts to the DocumentHandler.ashx to handle the showing of a single recall when clicking on the recall title link.
function ShowSingleRecall(recallID) {
    var action = $("#aspnetForm").attr("action");
    var target = $("#aspnetForm").attr("target");

    $("#selectedRecalls").attr("value", "^" + recallID.toString());
    $("#aspnetForm").attr("action", "/DocumentHandler.ashx?mod=PrintFDADeviceRecalls");
    $("#aspnetForm").attr("target", "_blank");
    $("#aspnetForm").submit();
    $("#aspnetForm").attr("action", action.toString());
    $("#aspnetForm").attr("target", target.toString());
};

/*Handle the email and print button toggling on the initial page load.*/
function pageLoad() {
    //Disable the print button
    //This has to be done here instead of the control because the 
    //button's onclientclick script won't render otherwise
    var printButton = $(":submit[id$='btnPrint']");
    printButton.attr("disabled", "disabled");
    //Loop through every checkbox in the gridview
    $("#tGridView :checkbox").each(function() {
        //Hook up a callback function for their click event.
        $(this).click(function() {
            var enabled = false;
            var emailButton = $(":submit[id$='btnEmail']");

            //Loop through every checkbox in the gridview to see if it is checked.
            $("#tGridView :checkbox:not([id$='_TITLECBX'])").each(function() {
                //If one is checked enable the email and print buttons
                if ($(this).attr("checked") == true) {
                    emailButton.removeAttr("disabled");
                    emailButton.removeClass("emailButtonAsLinkButtonDisabled");
                    emailButton.addClass("emailButtonAsLinkButton");
                    printButton.removeAttr("disabled");

                    printButton.removeClass("printButtonAsLinkButtonDisabled");
                    printButton.addClass("printButtonAsLinkButton");
                    enabled = true;
                    return;
                }
            });
            //If no checkboxes were checked, disable the email and print buttons
            if (enabled == false) {
                emailButton.attr("disabled", "disabled");
                emailButton.removeClass("emailButtonAsLinkButton");
                emailButton.addClass("emailButtonAsLinkButtonDisabled");
                printButton.attr("disabled", "disabled");
                printButton.removeClass("printButtonAsLinkButton");
                printButton.addClass("printButtonAsLinkButtonDisabled");
            }
        });
    });
};