/* Minification failed. Returning unminified contents.
(1,16): run-time error CSS1031: Expected selector, found '='
(1,16): run-time error CSS1025: Expected comma or open brace, found '='
(68,10): run-time error CSS1031: Expected selector, found 'GUID('
(68,10): run-time error CSS1025: Expected comma or open brace, found 'GUID('
(78,10): run-time error CSS1031: Expected selector, found 'ConfirmationModal('
(78,10): run-time error CSS1025: Expected comma or open brace, found 'ConfirmationModal('
 */
var FormHelper = {
    ApplyModelStateErrorListToForm: function (ModelStateErrors, FormElement, FeedbackElement) {
        if (!Array.isArray(ModelStateErrors)) {
            return;
        }
        if (!(FormElement instanceof jQuery)) {
            FormElement = $(FormElement);
        }
        if (!(FeedbackElement instanceof jQuery)) {
            FeedbackElement = $(FeedbackElement);
        }

        FormElement.find(".is-invalid").removeClass("is-invalid");

        var message = "";
        ModelStateErrors.forEach(function (err) {
            var input = FormElement.find("input[name='" + err.Key + "'],select[name='" + err.Key + "'],textarea[name='" + err.Key + "']");
            if (input.attr("type") == "checkbox" && input.parent().is("label")) {
                input.parent().addClass("field-validation-error");
            }
            else {
                input.addClass("is-invalid");
            }
            message += err.Value + "<br>";
        });
        FeedbackElement.html(message);
        if (message != "") {
            FeedbackElement.slideDown();
            if (FeedbackElement.hasClass("alert")) {
                FeedbackElement.removeClass("alert-success");
                FeedbackElement.addClass("alert-danger");
            }
        }
    },
    ConvertMailgunBatchResponsesToRawHtmlDisplay: function(MailgunBatchResponsList) {
        var rawHtml = "";
        var HasErrors = false;
        MailgunBatchResponsList.forEach(function (r, i) {
            if (r.BatchCount == 0) HasErrors = true;
            if (!r.StatusCode.toString().match(/2\d\d/)) HasErrors = true;
            if (r.ErrorMessage) HasErrors = true;

            var contentMessage = "";
            if (r.Content) {
                contentMessage = r.Content.message || "";
            }
            rawHtml += "<strong>Batch #" + (i + 1) + " (" + r.BatchCount + " Recipients):</strong> ";
            rawHtml += "Status Code " + r.StatusCode;
            if (r.ErrorMessage) {
                rawHtml += ": <span class='text-danger'>" + r.ErrorMessage + "</span> ";
            }
            else {
                rawHtml += ". ";
            }
            if (contentMessage) {
                rawHtml += "Message: \"" + contentMessage + "\" ";
            }
            rawHtml += "<br>";
        });
        rawHtml = "<h5>" + (HasErrors ? "Completed with Errors..." : "Success") + "</h5>" + rawHtml;
        return {
            RawHTML: rawHtml,
            HasErrors: HasErrors
        };
    }
}

function GUID() {
    function s4() {
        return Math.floor((1 + Math.random()) * 0x10000)
            .toString(16)
            .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}

// Confirmation Modal
function ConfirmationModal(title, body, OnConfirm, OnCancel, isLarge) {
    isLarge = isLarge || false;
    var modalID = GUID();
    var rawHtml = "<div class='modal fade' id='" + modalID + "'><div class='modal-dialog " + (isLarge ? "modal-lg" : "") + "'><div class='modal-content'>";
    rawHtml += "<div class='modal-header'><h5 class='modal-title'>" + title + "</h5><button type='button' class='close' data-dismiss='modal'>&times;</button></div>";
    rawHtml += "<div class='modal-body'><p>" + body + "</p></div>";
    rawHtml += "<div class='modal-footer'>";
    rawHtml += "<button type='button' class='Button_Confirm btn btn-primary auto-width'>Confirm</button>";
    rawHtml += "<button type='button' class='Button_Cancel btn btn-secondary auto-width'>Cancel</button>";
    rawHtml += "</div>";
    rawHtml += "</div></div></div>"
    $("body").append(rawHtml);
    if (!OnConfirm) {
        $("#" + modalID + " .Button_Confirm").hide();
        $("#" + modalID + " .Button_Cancel").text("Close");
    }
    $("#" + modalID + " .Button_Confirm").click(function () {
        $("#" + modalID).modal("hide");
        if (OnConfirm) {
            OnConfirm();
        }
    });
    $("#" + modalID + " .Button_Cancel").click(function () {
        $("#" + modalID).modal("hide");
    });
    $("#" + modalID).on("hidden.bs.modal", function () {
        if (OnCancel) {
            OnCancel();
        }
        $("#" + modalID).remove();
    });
    $("#" + modalID).modal("show");
    return modalID;
}

