//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm04xFunc2:not('.noevent')", function (e) {
  $('#popafm04xSearch').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm04xFunc5:not('.noevent')", function (e) {
  afm04x.initial();
});
//　F9 決定
$(document).on("click", ".popafm04xFunc9:not('.noevent')", function (e) {
  $("#popafm04xSelect").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm04xFunc12:not('.noevent')", function (e) {
  $('#popafm04xClose').trigger('popClose');
});

$(document).on("focus", "#afm04xDummy", function(e) {
  $(".popafm04xFunc2").focus();
});

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

  var grp = $me.closest(".mcdGrp");
  var mcd = grp.find(".mcd")[0];

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

  common.setValue($("#afm04xOrgTitle"), common.getValue($("#topPrgTitle")));
  common.setValue($("#topPrgTitle"),"仕入先検索");


  var id = common.getId( $(mcd) );
  $("#afm04xCd").data("popObjId", id);

  afm04x.initial();
  $("#afm04xNm").focus();
  common.setDetailHeight($("#afm04xDetail"));

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

$(document).on("click", ".cmdMcd", function (e) {
  dspPopupAfm04x($(this));
});

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

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

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

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

  var sendData = {};
  sendData["popmode"] = "afm04x";
  sendData["nm"] = $("#afm04xNm").val();
  sendData["zip"] = $("#afm04xZip").val();
  sendData["add"] = $("#afm04xAdd").val();
  sendData["tel"] = $("#afm04xTel").val();
  sendData["fax"] = $("#afm04xFax").val();
  sendData["cd"] = $("#afm04xCd").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("regSuccess", "#popafm04xSearch", function (e, data) {
  var list = JSON.parse(data)

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

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

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

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

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

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

  var sel = $("#afm04xDetail .selectRow");
/*
  var selMcd = $("#afm04xDetail .selectRow .mcd").text();
*/

  var grp = popup.closePopup("afm04x");
  var mcd = grp.find(".mcd")[0];
  //値をセット
  common.setValue(grp.find(".mcd"), common.getValue($(sel).find(".mcd")));
  common.setValue(grp.find(".mnm"), common.getValue($(sel).find(".mnm")));
  common.setValue(grp.find(".mknm"), common.getValue($(sel).find(".mknm")));
  common.setValue(grp.find(".mnms"), common.getValue($(sel).find(".mnms")));

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

  afm04x.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm04xOrgTitle")));
  $(mcd).trigger("afterSelectAfm04x");
  common.clearError($(mcd));
});

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

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

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

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

  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="item wd-S   ' + n(d.readcolor) + ' mcd">' + n(d.mcd) + '</div>' +
    '     <div class="item wd-L   ' + n(d.readcolor) + ' mnm longnm">' + n(d.mnm) + '</div>' +
    '     <div class="item wd-M   ' + n(d.readcolor) + ' mknm longnm">' + n(d.mknm) + '</div>' +
    '     <div class="item wd-S   ' + n(d.readcolor) + ' mzip">' + n(d.mzip) + '</div>' +
    '     <div class="item wd-300 ' + n(d.readcolor) + ' madd longnm">' + n(d.madd) + '</div>' +
    '     <div class="item wd-120 ' + n(d.readcolor) + ' mtel">' + n(d.mtel) + '</div>' +
    '     <div class="item wd-120 ' + n(d.readcolor) + ' mfax">' + n(d.mfax) + '</div>' +
    '     <input type="hidden" class="mnms" value="' + n(d.mnms) + '" />' +
    '     <input type="hidden" class="mknms" value="' + n(d.mknms) + '" />' +
    '   </div> ' +
    ' </div> ';

    return row;
  },

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

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

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

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

  },
}
