$(document).ready(function () {
  $("#versiondescription")
    .find("a")
    .each(function () {
      if (
        $(this).attr("target") == undefined ||
        $(this).attr("target").toLowerCase() != "_parent"
      ) {
        $(this).attr("target", "_blank");
      }
    });

  var isChrome =
    /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
  var isFF =
    /Firefox[\/\s](\d+)/.test(navigator.userAgent) &&
    new Number(RegExp.$1) >= 4;

  // $("#buttonSendFormAndPostForum").click(function (event) {
  //     $("#postonforum").val('true');
  //     $("#sendForm").submit();
  // });

  $("#buttonSendForm").click(function (event) {
    $("#postonforum").val("false");
    $("#sendForm").submit();
  });

  $("#sendForm").submit(function (event) {
    if (
      $("#postonforum").val() == "true" &&
      $("#cke_message iframe").contents().find("body").text().trim() == "" &&
      // ei check img
      CKEDITOR.instances.message.document
        .getBody()
        .getHtml()
        .indexOf("<img src=") < 0 &&
      CKEDITOR.instances.message.document
        .getBody()
        .getHtml()
        .indexOf("<img data-cke-saved-src") < 0
    ) {
      alert(GForumEmprtyMessageAlert);
      event.preventDefault();
      return false;
    }
    existUnsavedData = false;
    parent.postMessage("unsaveddatawassave", "*");
    ShowModalWindow(true);
    $("#buttonSendForm").attr("disabled", "disabled");
    ///$("#buttonSendFormAndPostForum").attr('disabled','disabled');
    for (instance in CKEDITOR.instances) {
      CKEDITOR.instances[instance].updateElement();
    }
    $.ajax({
      url: window.location,
      type: "post",
      data: $("form#sendForm").serialize(),
      success: function (data) {
        var getHTML = $.parseHTML(data);
        if ($(getHTML).find("div#errormessage").length) {
          alert($(getHTML).find("div#errormessage").html());
        } else {
          $("#versiondescription").html(
            $(getHTML).find("#versiondescription").html()
          );
          $("#forummessagelink").html(
            $(getHTML).find("#forummessagelink").html()
          );
        }
        $("#buttonSendForm").prop("disabled", false);
        // $("#buttonSendFormAndPostForum").prop("disabled", false);
      },
    });
    event.preventDefault();
    ShowModalWindow(false);
    parent.postMessage("refreshmenucount", "*");
  });

  if ($("#message").length) {
    var editor = CKEDITOR.replace("message", {
      language: gShortLang,
      // instanceReady
      on: {
        instanceReady: function (ev) {
          var tags = ["p", "ol", "ul", "li"];
          for (var key in tags) {
            ev.editor.dataProcessor.writer.setRules(tags[key], {
              indent: false,
              breakBeforeOpen: false,
              breakAfterOpen: false,
              breakBeforeClose: false,
              breakAfterClose: false,
            });
          }
        },
      },
      // instanceReady end
      toolbar: [
        [
          "Bold",
          "Italic",
          "Underline",
          "Strike",
          "-",
          "Link",
          "Unlink",
          "Image",
        ],
      ],
    });
    CKEDITOR.config.autoParagraph = false;
    CKEDITOR.config.linkShowAdvancedTab = false;
    CKEDITOR.config.linkShowTargetTab = false;
    CKEDITOR.config.DefaultLinkTarget = "_blank";
    CKEDITOR.config.defaultLanguage = gShortLang;
    CKEDITOR.config.language = gShortLang;
    if (typeof gIsReadOnly !== "undefined" && gIsReadOnly) {
      CKEDITOR.config.readOnly = true;
    }

    var chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase());
    var edge = /Edge\/\d./i.test(navigator.userAgent.toLowerCase());

    if (chrome || edge) {
      CKEDITOR.config.extraPlugins = "pastebase64";
    } else {
      CKEDITOR.config.extraPlugins = "pastebase64ie";
    }

    CKEDITOR.instances.message.on("change", function () {
      existUnsavedData = true;
      parent.postMessage("createunsaveddata", "*");
    });
  }

  if ($("input#uav_value").length) {
    $("input#uav_value").change(function () {
      existUnsavedData = true;
      parent.postMessage("createunsaveddata", "*");
    });
  }
  if ($("select#uav_value").length) {
    $("select#uav_value").change(function () {
      existUnsavedData = true;
      parent.postMessage("createunsaveddata", "*");
    });
  }

  $("a").click(function () {
    if (!existUnsavedData || $(this).hasClass("self-event")) {
      return true;
    } else {
      if (confirm(gConfirmsavelesValue)) {
        return true;
      } else {
        event.preventDefault();
        return false;
      }
    }
  });
});
