
// submit product info form if marked as ready after calculation..
var submitPiForm = function () {
  var $piForm = $("form[name='cart_quantity']");
  if ($piForm.attr('submitready') == 'true') {
    $piForm.submit();
  }

}

$(document).keyup(function(e) {
  if (e.keyCode == 13) {
    if( $("#coverage-input, #length-input").is(".focused") ) {
      $('#pi-amount-calc a').click();
    }
  }   // enter
});

$(document).ready(function(){

  $("#coverage-input, #length-input").focus(function() {
    $(this).addClass('focused');
  }).blur(function() {
    $(this).removeClass('focused');
  });

  // disable submitting form (calculate it first)
  $("input.pi-incartbtn").click(function() {
    if ($("#pi-amount-calc").length) {
      $("form[name='cart_quantity']").attr("submitready", "true");
      $('#pi-amount-calc a').click();
      return false;
    }
  });

  $("form[name='cart_quantity']").submit(function() {
    //$('#pi-amount-calc a').click();
    if ( $("#coverage-input, #length-input").is('.focused') ) {
      return false;
    }
  });

  $('#pi-amount-calc a').click(function() {
    var coverage = $("#coverage-input").val();
    var products_id = $("input[name=products_id]").val();

    var length = $("#length-input").val();
    var width = $("#sel-width").val();

    var qty = $("#qty-input").val();

    var $spinner = $('<div class="calc-spinner" style="background: url(templates/Original/images/loaderform.gif) 50% 50% no-repeat;">&nbsp;</div>');

    $("#pi-amount-calc").after($spinner);

    if (length != null && width != null) {
      if (isNaN(length) || length == 0) {
        $("#length-input").addClass("err");
        $spinner.remove();
      } else {
//        var data = { length: Math.ceil(length), width: width, products_id: products_id };
//        var data = { length: length, width: width, products_id: products_id };
        var data = {products_id: products_id };

        $.getJSON("json_calc.php?length="+length+"&width="+width, data,
          function(res) {
            $("#length-input").attr("value", res.length);
            $("#json-coverage").text("" + res.coverage).show();

            $("#json-total").text("" + res.total);
            $("#json-saveamount").text("" + res.saveamount);
            $("#pi-totals-wrap").show();
            $spinner.remove();
            submitPiForm();
          });

        $("#length-input").removeClass("err");
      }
    }

    if (qty != null) {
      if (isNaN(qty) || qty == 0) {
        $("#qty-input").addClass("err");
        $spinner.remove();
      } else {
        var data = { qty: qty, products_id: products_id };
        $.getJSON("json_calc.php", data,
          function(res) {
            $("#qty-input").attr("value", res.qty);

            $("#json-total").text("" + res.total);
            $("#json-saveamount").text("" + res.saveamount);
            $("#pi-totals-wrap").show();
            $spinner.remove();
            submitPiForm();
          });

        $("#qty-input").removeClass("err");
      }
    }

    if (coverage != null) {
      if (isNaN(coverage) || coverage == 0) {
        $("#coverage-input").addClass("err");
        $spinner.remove();
      } else {
        var data = { coverage: coverage, products_id: products_id };
        $.getJSON("json_calc.php", data,
          function(res) {
            $("#pi-pack-qty").attr("value", res.packs);
            $("#json-packs").text("" + res.packs).show();
  
            $("#json-packs2").text("" + res.packs);
            $("#json-coverage").text("" + res.coverage);
            $("#json-total_m2").text("" + res.total_m2);
  
            $("#json-total").text("" + res.total);
            $("#json-saveamount").text("" + res.saveamount);
            $("#pi-totals-wrap").show();
            $spinner.remove();
            submitPiForm();
          });
  
        $("#coverage-input").removeClass("err");
      }
    }

    return false;
  });
});
