$(document).ready(function () {
    var size = window.top.allowUploadFileSize;
    var errorUploadSize = window.top.errorExceptionsUploadSize;
    $(".ImageBox").dropzone({
        url: "/file/post",
        init: function () {
            var objDropzone = this;
            objDropzone.on("complete", function (file, done) {

                if (file.size > size) {
                    $("#fileError").text(errorUploadSize);
                    objDropzone.removeAllFiles();
                    return;
                }
                else if (!(file.type == "image/png" || file.type == "image/jpeg" || file.type == "image/jpg")) {
                    $("#fileError").text(window.top.msgExtension1 + " (.jpg , .jpeg , .png) " + window.top.msgExtension2);

                    objDropzone.removeAllFiles();
                    return;
                }

                $(".ImageBox").css("display", "none");
                $(".FinalImageBox").css("display", "block");
                $("#uplaodedImg").attr("src", file.dataURL);
                $("#lblFileName").text(file.name);
                $("#imgSize").text(Math.round(file.size / 1024) + "KB");
                $("#fileError").text("");
                objDropzone.removeAllFiles();
            });
        },
        paramName: "file",
        uploadMultiple: false,
        maxFiles: 1,
    });
    $("#userImageUploader").change(function (event) {
        if (this.files.length > 0) {
            var file = event.target.files[0];
            var reader = new FileReader();
            reader.readAsDataURL(file);
            reader.onload = function () {
                $("#uplaodedImg").fadeIn().attr("src", reader.result);
                $(".ImageBox").css("display", "none");
                $(".FinalImageBox").css("display", "block");
                $("#lblFileName").text(file.name);
                $("#imgSize").text(Math.round(file.size / 1024) + "KB");
            };
        }
    });
});
function resetUpload() {
    $(".ImageBox").css("display", "block");
    $(".FinalImageBox").css("display", "none");
    $("#uplaodedImg").attr("src", "");
    $("#lblFileName").text("");
    $("#imgSize").text("");
    $("#fileError").text("");
}
function save() {
    var data = getJsonValue("");
    var error = checkValidation("");
    if (error == "") {
        var code = getQuery("code");
        if (code)
            data["Code"] = code;
        data["FileImage"] = $('#uplaodedImg').attr("src");
        ajax({
            loading: true,
            url: "GeneralApi/" + (code ? "UpdateBulletin" : "InsertBulletin"),
            data: { news: data },
            success: function (result) {
                showMsgModal(2, code ? window.top.editedSuccessfully : window.top.savedSuccessfully);
                refreshParent("refresh();");
                closeTopModal();
            }
        });
    }
    else {
        showMsgModal(0, error, error);
    }
}

