//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm03cxFunc2:not('.noevent')", function (e) {
  $('#popafm03cxSearch').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm03cxFunc5:not('.noevent')", function (e) {
  afm03cx.initial();
});
//　F9 決定
$(document).on("click", ".popafm03cxFunc9:not('.noevent')", function (e) {
  $("#popafm03cxSelect").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm03cxFunc12:not('.noevent')", function (e) {
  $('#popafm03cxClose').trigger('popClose');
});

function dspPopupafm03cx($me) {  //表示
  if ($me.attr("readonly") == "readonly" || $me.attr("readonly") == true) {
    return true
  }

  var grp = $me.closest(".rcvdstGrp");
  var rcvdst = grp.find(".rcvdst")[0];

  popup.openPopup("afm03cx", $(grp));
  
  common.setValue($("#afm03cxOrgTitle"), common.getValue($("#topPrgTitle")));
  common.setValue($("#topPrgTitle"),"オンライン識別子検索");


  var id = common.getId( $(rcvdst) );
  $("#afm03cxCd").data("popObjId", id);

  afm03cx.initial();
  $("#afm03cxCd").focus();
  common.setDetailHeight($("#afm03cxDetail"));

  //$("#popafm03cxSearch").trigger("click");
}

$(document).on("click", ".cmdRcvdst", function (e) {
  dspPopupafm03cx($(this));
});

$(document).on("keyup", ".popRcvdst", function (e) {
  if (e.keyCode == 40) {
    dspPopupafm03cx($(this));
  }
});

$(document).on("click", "#popafm03cxSearch", function (e) {
  var me = this;
  // 読込

  if($('input:checkbox[name="afm03cxStp"]:checked').val() == '1') {
    var $stpflg = '1';                          //1=廃止OK
  } else {
    var $stpflg = '0';
  }

  afm03cx.deldetail();  //明細削除

  var sendData = {};
  sendData["popmode"] = "afm03cx";
  sendData["cd"] = $("#afm03cxCd").val();
  //sendData['stpflg'] = $stpflg;

  setComplianceData("search", sendData);

  var me = $(this);
  common.postData(
    common.getApiUrl() + "getPopupSearch.php"
    , sendData
    , function (data, options) { $(me).trigger("regSuccess", data); }
    , function (error, options) { $(me).trigger("regError", error); }
  );

});

$(document).on("focus", "#afm03cxDummy", function(e) {
  $(".popafm03cxFunc2").focus();
});

$(document).on("regSuccess", "#popafm03cxSearch", function (e, data) {
  var list = JSON.parse(data)

  if (!(list != null)) {
    return false;
  }

  $("#afm03cxDetail .detail").remove();
  var el = "";
  for (var i = 0; i < list.length; i++) {
    el += afm03cx.appendRow(list[i]);
  }
  $("#afm03cxDetail").append(el);

  common.setDetailHeight($("#afm03cxDetail"));

  $("#afm03cxDetail .detail").first().find("a").focus();
});
$(document).on("regError", "#popafm03cxSearch", function (e, data) {
  alert(data.msg);
});

$(document).on("dblclick", "#afm03cxDetail .selectRow", function (e) {
  hideToolTip();
  $("#popafm03cxSelect").trigger("click");
});

$(document).on("click", "#popafm03cxSelect", function (e) {
  if (e.bubbles == false) {
    return false;
  }

  var sel = $("#afm03cxDetail .selectRow");
/*
  var selRcvdst = $("#afm03cxDetail .selectRow .rcvdst").text();
*/

  var grp = popup.closePopup("afm03cx");
  var rcvdst = grp.find(".rcvdst")[0];
  var rcvdstnm = grp.find(".rcvdstnm")[0];
  //値をセット
  common.setValue(grp.find(".rcvdst"), common.getValue($(sel).find(".rcvdst")));
  common.setValue(grp.find(".rcvdstnm"), common.getValue($(sel).find(".rcvdstnm")));

//  $(rcvdst).change();
  $(rcvdst).focus();

  afm03cx.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm03cxOrgTitle")));
  $(rcvdst).trigger("afterSelectafm03cx");
});

$("#popafm03cxClose").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }
  var grp = popup.closePopup("afm03cx");
  grp.find(".rcvdst")[0].focus();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm03cxOrgTitle")));
  afm03cx.initial();
  //afm03cx.close();
});

$(document).on("keydown", "#afm03cxDetail .selectRow", function (e) {
  if (e.keyCode == 32) {
    $("#popafm03cxSelect").trigger("click");
    return false;
  }
});

var afm03cx = {
  close: function () {
    this.initial();
    popup.closePopup("afm03cx");
  },

  appendRow: function (d) {
    var n = stringUtil.nvl;

    if(d.stp == null) {
      var row =
      ' <div class="col wp-100 detail"> ' +
      '   <hr> ' +
      '   <div class="col wp-100"> ' +
      '     <a href="#" class="line"> </a> ' +
      '     <div class="item wd-S inputReadOnly rcvdst">' + n(d.rcvdst) + '</div>' +
      '     <div class="item wd-S inputReadOnly cucd ">' + n(d.cucd) + '</div>' +
      '     <div class="item wd-LL inputReadOnly rcvdstnm ">' + n(d.nm) + '</div>' +
      '   </div> ' +
      ' </div> ';
    } else {
      var row =
      ' <div class="col wp-100 detail"> ' +
      '   <hr> ' +
      '   <div class="col wp-100"> ' +
      '     <a href="#" class="line"> </a> ' +
      '     <div class="item wd-S inputReadOnly rcvdst" style="background-color:#FFC8F4;">' + n(d.rcvdst) + '</div>' +
      '     <div class="item wd-S inputReadOnly cucd " style="background-color:#FFC8F4;">' + n(d.cucd) + '</div>' +
      '     <div class="item wd-LL inputReadOnly rcvdstnm " style="background-color:#FFC8F4;">' + n(d.nm) + '</div>' +
      '   </div> ' +
      ' </div> ';
    }

    return row;
  },

  deldetail: function () {
    var el = document.getElementById("afm03cxDetail");
    var n = el.cloneNode(false);
    el.parentNode.replaceChild(n, el);
  },

  initial: function () {
    $("#afm03cxNm").val("");
    $("#afm03cxCd").val("");

    var el = document.getElementById("afm03cxDetail");
    var n = el.cloneNode(false);
    el.parentNode.replaceChild(n, el);

    $("#afm03cxCd").focus();

  },
}
