$(document).ready(function() {
  jQuery.ajax({
      headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      },
      type: "GET",
      url: "/cart-drawer/status",
      dataType: 'json',
      success: function(response) {
          if(typeof response.status != 'undefined' && response.status != false) {

              if(typeof response.script_url != 'undefined') {
                var s = document.createElement("script");
                s.type = "text/javascript";
                s.src = response.script_url;
                $("head").append(s);
              }

              $('a').click(function(event) {
                  if($(this).hasClass('header-cart-icon') || $(this).hasClass('head_cart') || ($(this).attr('href') != 'undefined' && $(this).attr('href') == '/cart' && typeof $(this).attr('aria-expanded') == 'undefined')){
                    event.preventDefault();
                    window.localStorage.setItem('add_to_cart', false)
                    $("#drawer-demo").drawer('show');
                  }
              });

              $($("[action='/cart/add']").find("button")).click(function(){
                window.localStorage.setItem('add_to_cart', true);
                if((typeof $(this).attr('type') != 'undefined' && $(this).attr('type') == 'submit') || $(this).hasClass('addtocart-js')) {
                  if($('#prpage-cart-term-conditions-checkbox') && $("#prpage-cart-term-conditions-checkbox").prop('checked') == false) {
                        return true;
                  } else {
                    $(this).removeClass('disabled');
                    $(this).prop("disabled",false);
                    $("#drawer-demo").drawer('show');
                  }
                }
              });

              $('.header-icon1').click(function(event) {
                  event.preventDefault();
                  window.localStorage.setItem('add_to_cart', false)
                  $("#drawer-demo").drawer('show');
              })
          }
      }
  });
});