<script>
//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popaf254x1Func2:not('.noevent')", function (e) {
  $('#popaf254x1Search').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popaf254x1Func5:not('.noevent')", function (e) {
  af254x1.initial();
});
//　F9 決定
$(document).on("click", ".popaf254x1Func9:not('.noevent')", function (e) {
  $("#popaf254x1Select").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popaf254x1Func12:not('.noevent')", function (e) {
  $('#popaf254x1Close').trigger('popClose');
});

$(document).on("focus", "#af254x1Dummy", function(e) {
  $(".popaf254x1Func2").focus();
});

$(document).on("click", ".cmdAf254x1", function (e) {
  dspPopupAf254x1($(this),'0');
});

$(document).on("keyup", ".popAf254x1", function (e) {
  if (e.keyCode == 40) {
    dspPopupAf254x1($(this),'0');
  }
});

$(document).on("click", ".popup-af254x1", function (e) {
  dspPopupAf254x1($(this),'1');
});

//$(document).on("click", ".popup-af254x1", function (e) {
//    var grp = $(this).closest(".af254x1popup");
function dspPopupAf254x1($me,flg) {
    //var grp = $(".contents.af2000x .header .af254x1popup");
    var grp = $me.closest(".af254x1popup");
    var frmh = grp.closest("#headerTop");

    popup.openPopup("af254x1", $(grp));

    af254x1.initial();

    common.setValue($("#af254x1OrgTitle"), common.getValue($("#topPrgTitle")));
    common.setValue($("#topPrgTitle"),"転送伝票番号検索");

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

    //表示前チェックで1伝票番号で複数伝票あった時
    if(flg == '1') {
      common.setValue($("#af254x1Vseq"),common.getValue(grp.find(".vseq")));
      $("#popaf254x1Search").trigger("click");
    }

}
//});

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

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

  var sendData = {};
  
  sendData["popmode"] = "af254x1";
  sendData["dat"] = $("#af254x1Dat").val();
  sendData["spktcd1"] = $("#af254x1Spktcd1").val();
  sendData["spktcd2"] = $("#af254x1Spktcd2").val();
  sendData["jan"] = $("#af254x1Jan").val();

  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("regSuccess", "#popaf254x1Search", function (e, data) {
  var list = JSON.parse(data);

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

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

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

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

$(document).on("dblclick", "#af254x1Detail .selectRow", function (e) {
  $("#popaf254x1Select").trigger("click");
});

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

  var sel = $("#af254x1Detail .selectRow");

  var selVseq = $("#af254x1Detail .selectRow .vseq").text();
  var selCuiseq = $("#af254x1Detail .selectRow .cuiseq").val();
  var selSpktcd1 = $("#af254x1Detail .selectRow .spktcd1").text();
  var selSpktnm1 = $("#af254x1Detail .selectRow .spktnm1").text();
  var selSpktcd2 = $("#af254x1Detail .selectRow .spktcd2").text();
  var selSpktnm2 = $("#af254x1Detail .selectRow .spktnm2").text();

  var grp = popup.closePopup("af254x1");
  var frmh = grp.closest(".header");

  var vseq = grp.find(".vseq")[0];
  var cuiseq = grp.find(".cuiseq")[0];
  var spktcd1 = grp.find(".spktcd1")[0];
  var spktnm1 = grp.find(".spktnm1")[0];
  var spktcd2 = grp.find(".spktcd2")[0];
  var spktnm2 = grp.find(".spktnm2")[0];
  $(vseq).val(selVseq);
  $(cuiseq).val(selCuiseq);
  $(spktcd1).val(selSpktcd1);
  $(spktnm1).val(selSpktnm1);
  $(spktcd2).val(selSpktcd2);
  $(spktnm2).val(selSpktnm2);
  
  af254x1.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#af254x1OrgTitle")));
  $(vseq).trigger("afterSelectaf254x1");
});

$("#popaf254x1Close").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }
  common.setValue($("#topPrgTitle"), common.getValue($("#af254x1OrgTitle")));
  af254x1.initial();
  af254x1.close();
});

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

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

  appendRow: function (d) {
    var n = stringUtil.nvl;
    var row =
      ' <div class="col wp-100 detail"> ' +
      '   <hr> ' +
      '   <div class="col wp-100"> ' +
      '     <a href="#" class="line"> </a> ' +
      '     <div class="col wp-100"> ' +
      '       <div class="item wd-100   inputReadOnly vseq">' + n(d.vseq) + '</div>' +
      '       <div class="item wd-kbn   inputReadOnly vlin number">' + n(d.vlin) + '</div>' +
      '       <div class="item wd-date  inputReadOnly dat">' + n(d.dat) + '</div>' +
      '       <div class="item wd-SS    inputReadOnly spktcd1">' + n(d.spktcd1) + '</div>' +
      '       <div class="item wd-M     inputReadOnly spktnm1">' + n(d.spktnm1) + '</div>' +
      '       <div class="item wd-jan   inputReadOnly jan">' + n(d.jan) + '</div>' +
      '       <div class="item wd-kbn   inputReadOnly gdcls9nm">' + n(d.gdcls9nm) + '</div>' +
      '       <div class="item wd-gdjnm inputReadOnly gdjnm">' + n(d.gdjnm) + '</div>' +
      '       <div class="item wd-S     inputReadOnly mvpno number">' + common.addFigure(n(d.mvpno)) + '</div>' +
      '       <div class="item wd-date  inputReadOnly dat1">' + n(d.dat1) + '</div>' +
      '     </div> ' +
      '     <div class="col wp-100"> ' +
      '       <div class="col " style="width:221px;" >&nbsp;</div> ' +
      '       <div class="item wd-SS    inputReadOnly spktcd2">' + n(d.spktcd2) + '</div>' +
      '       <div class="item wd-M     inputReadOnly spktnm2">' + n(d.spktnm2) + '</div>' +
      '       <div class="col " style="width:142px;" >&nbsp;</div> ' +
      '       <div class="item wd-gdjnm inputReadOnly gdjstnd">' + n(d.gdjstnd) + '</div>' +
      '     </div> ' +
      '     <input type="hidden" class="cuiseq" value="' + n(d.cuiseq) + '"> '+
      '   </div> ' +
      ' </div> ';

    return row;
  },

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

  initial: function () {
    var main = $(".af254x1").find(".header_popaf254x1");
    common.clearInputData(main);
    common.clearAllError();

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

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

  },
}
</script>
