//===================================================================*/
/* イベント
//===================================================================*/
//　F2 表示
$(document).on("click", ".popafm036xFunc2:not('.noevent')", function (e) {
  $('#popafm036xSearch').trigger('click');
});
//　F5 クリア
$(document).on("click", ".popafm036xFunc5:not('.noevent')", function (e) {
  afm036x.initial();
});
//　F9 決定
$(document).on("click", ".popafm036xFunc9:not('.noevent')", function (e) {
  $("#popafm036xSelect").trigger("click");
});
//　F12 メニュー/戻る
$(document).on("click", ".popafm036xFunc12:not('.noevent')", function (e) {
  $('#popafm036xClose').trigger('popClose');
});

$(document).on("focus", "#afm036xDummy", function(e) {
  $(".popafm036xFunc2").focus();
});

function dspPopupAfm036x($me) {  //表示

    if ($me.attr("readonly") == "readonly" || $me.attr("readonly") == true) {
      return true
    }

    var grp = $me.closest(".cshcdGrp");
    var cshcd = grp.find(".cshcd")[0];

    var cucd = grp.find(".cucd")[0];
    var cunm = grp.find(".cunm")[0];

    if(stringUtil.isEmpty(common.getValue($(cucd)))) {
      common.addError($(cucd), "得意先を入力してください。", null);
      return false;
    }

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

    common.setValue($("#afm036xOrgTitle"), common.getValue($("#topPrgTitle")));
    common.setValue($("#topPrgTitle"),"得意先センター検索");

    var id = common.getId( $(cshcd) );
    $("#afm036xCd").data("popObjId", id);

    afm036x.initial();
    $("#afm036xNm").focus();

    common.setValue($("#afm036xCcd"),common.getValue($(cucd)));
    common.setValue($("#afm036xLblcnm"),common.getValue($(cucd))+':'+common.getValue($(cunm)));

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

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

$(document).on("click", ".cmdCshcd", function (e) {
  dspPopupAfm036x($(this));
});
$(document).on("keyup", ".popCshcd", function (e) {
  if (e.keyCode == 40) {
    dspPopupAfm036x($(this));
  }
});
$(document).on("click", "#popafm036xSearch", function (e) {
  var me = this;
  // 読込

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

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

  var sendData = {};
  sendData["popmode"] = "afm036x";
  sendData["cnm"] = $("#afm036xCnm").val();
  sendData["ccd"] = $("#afm036xCcd").val();
  sendData["cd"] = $("#afm036xCd").val();
  sendData["nm"] = $("#afm036xNm").val();
  sendData["zip"] = $("#afm036xZip").val();
  sendData["add"] = $("#afm036xAdd").val();
  sendData["tel"] = $("#afm036xTel").val();
  sendData["fax"] = $("#afm036xFax").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", "#popafm036xSearch", function (e, data) {
  var list = JSON.parse(data)

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

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

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

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

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

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

  var sel = $("#afm036xDetail .selectRow");
/*
  var selCucd = $("#afm036xDetail .selectRow .cucd").text();
  var selCuscd = $("#afm036xDetail .selectRow .cuscd").text();
*/

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

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

  afm036x.initial();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm036xOrgTitle")));
  $(cshcd).trigger("afterSelectAfm036x");
});

$("#popafm036xClose").on("popClose", function (e) {
  if (e.bubbles == false) {
    return false;
  }

  var grp = popup.closePopup("afm036x");
  grp.find(".cshcd")[0].focus();

  common.setValue($("#topPrgTitle"), common.getValue($("#afm036xOrgTitle")));
  afm036x.initial();

//  afm036x.close();
});

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

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

  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-070 inputReadOnly cshcd">' + n(d.cshcd) + '</div>' +
    '     <div class="item wd-250 inputReadOnly cshnm ">' + n(d.cshnm) + '</div>' +
    '     <div class="item wd-070 inputReadOnly cszip">' + n(d.cszip) + '</div>' +
    '     <div class="item wd-400 inputReadOnly csad longnm">' + n(d.csad) + '</div>' +
    '     <div class="item wd-100 inputReadOnly cstel">' + n(d.cstel) + '</div>' +
    '     <div class="item wd-100 inputReadOnly csfax">' + n(d.csfax) + '</div>' +
    '   </div> ' +
    ' </div> ';

    return row;
  },

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

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

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

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

  },
}
