$(function(){

  const confirm = "confirm"
  $(window).on('load', function() {
    if (location.href.indexOf(confirm) == -1 ){
      // 確認画面では不要な処理
      $('.defaultHide').hide();
      $('.defaultHide').find('input').each(function(idx ,val){
        $(val).prop('disabled' ,true);
      });
      const getClass = $('.kindRadio input:checked').attr('class');
      if (getClass !== undefined ) {
        const replaceName = getClass.replace('radio','menu');
        kindMenuSwitch('.' + replaceName);

      }
    }
  });

    $('.kindRadio input').click(function(){
        const getClass = $(this).attr('class');
        const replaceName = getClass.replace('radio','menu');
        kindMenuSwitch('.' + replaceName);
    });

    function kindMenuSwitch(menuName){
        try {
            $('.defaultHide').hide();
            $('.defaultHide').find('input').each(function(idx ,val){
              $(val).prop('disabled' ,true);
            });

            var show_menuName = menuName.replace( / user-success/g , "" );
            show_menuName = show_menuName.replace( / user-error/g , "" );

            if(show_menuName === '.menuLand'){
                $('.bukkenPlace').text('土地の所在地');
                $('input[name="real_estate_address_form"]').val('土地の所在地');
                $(show_menuName).show();
            }else{
                $('.menuCommon').show();
                $('.bukkenPlace').text('物件の所在地');
                $('input[name="real_estate_address_form"]').val('物件の所在地');
                $(show_menuName).show();

                $('.menuCommon').find('input').each(function(idx ,val){
                  $(val).prop('disabled' ,false);
                });
            }

            $(show_menuName).find('input').each(function(idx ,val){
              $(val).prop('disabled' ,false);
            });
            $('input[name="menuName"]').val(show_menuName);

        } catch (e) {

        }
    }

     // 最初の選択を必須に
     $('.kindRadio li input ').each(function(idx ,val){
        $(val).prop("required" ,true);
     })
     // 必須変化
     $('.kindRadio li input ').on('change',function(){
       $('input').each(function(idx ,val){
         $(val).prop("required" ,false); // 一旦必須外す
       });
       requiredChange($('input[name="client_addr_chk"]')); // お客様住所のみ別扱い
       $('input[name="item_100_151"]').prop("required" ,true); // プライバシーポリシーも別扱い
       $('.required').each(function(idx ,val) {
          $(val).closest('tr').each(function(i ,v) {
              if (!$(v).is(':hidden')) {
                  $(v).find('input').each(function(i2 ,v2) {
                      $(v2).prop("required" ,true);
                  });
                  $('input[name="zip1"],input[name="zip2"]').prop("required" ,false);
              }
         });
       });
     });
     // お客さんの住所　初期必須
     $(window).on('load', function() {
       requiredChange($('input[name="client_addr_chk"]'));
     });

     // 上記住所と同じ場合
     $('input[name="client_addr_chk"]').on('change',function(){
          requiredChange(this);
     })
      $('#button_submit').on('click', function(e){
      });
});

function requiredChange(select) {
    let flg = $(select).prop("checked");
    $('input[name="client_addr1"]').prop("required" ,!flg);
    $('input[name="client_addr2"]').prop("required" ,!flg);
    if (flg) {
        $('input[name="client_zip1"]').prop("value" ,"");
        $('input[name="client_zip2"]').prop("value" ,"");
        $('input[name="client_addr1"]').prop("value" ,"");
        $('input[name="client_addr2"]').prop("value" ,"");
        $('input[name="client_zip1"]').prop("disabled" ,flg);
        $('input[name="client_zip2"]').prop("disabled" ,flg);
        $('input[name="client_addr1"]').prop("disabled" ,flg);
        $('input[name="client_addr2"]').prop("disabled" ,flg);
    } else {
        $('input[name="client_zip1"]').prop("disabled" ,flg);
        $('input[name="client_zip2"]').prop("disabled" ,flg);
        $('input[name="client_addr1"]').prop("disabled" ,flg);
        $('input[name="client_addr2"]').prop("disabled" ,flg);
    }

}
