/*
   Autocalculation of total cost for the choice step
*/
$(document).ready(function() {
   $(".quantity").keyup(function() {
      
      // calc row total
      var _id = ($(this).attr("name")).substring(9, $(this).attr("name").length);
      var _price = $("#price_"+_id).attr("value");
      $("#total_"+_id).attr("value", _price *$(this).attr("value"));
      
      // calc grand total
      var _grandTotal = 0;
      $(".total").each(function() {
         if (($(this).attr("value") != null) && ($(this).attr("value") != ""))
            _grandTotal += parseInt($(this).attr("value"));
      });

      $("#grandtotal").attr("value", _grandTotal);
   })
   
   // fire once to initialize
   $(".quantity").keyup();
});

/*
   Choice form validity check
   
   Errormessages are specified as global variables in procedures/kadobonnen.asp
*/
$(document).ready(function() {
   $("#form_choice").submit(function() {
      if (($("#grandtotal").attr("value") == "") || ($("#grandtotal").attr("value") == "0")) {
         alert(G__NO_ORDER);
         return false;
      }
      
      var bResult = true;
      $(".total").each(function() {
         if (($(this).attr("value") == "NaN") || ($(this).attr("value") < 0)) {
            alert(G__INVALID_ORDER);
            bResult = false;
         }
      });

      return bResult;
   })
});


/*
   input form display delivery fields (or not)
*/
$(document).ready(function() {
   if ($("#delivery_check").attr('checked'))
      $(".delivery").hide();
   else 
      $(".delivery").show();

   // bind toggle to checkbox
   $("#delivery_check").click(function() {
      if ($(this).attr('checked'))
         $(".delivery").hide();
      else 
         $(".delivery").show();
   })
});

/*
   + and -  sign for choice form inputboxes
*/
$(document).ready(function() {
   $(".increment").click(function() {
      var _id = ($(this).attr("id")).substring(10, $(this).attr("id").length);

      // increase
      var _value = $("#quantity_" + _id).attr("value") || 0;
      $("#quantity_" + _id).attr("value", parseInt(_value) +1);
      
      // fire keyup to update the total fields
      $(".quantity").keyup();
   })

   $(".decrement").click(function() {
      var _id = ($(this).attr("id")).substring(10, $(this).attr("id").length);

      // decrease
      var _value = $("#quantity_" + _id).attr("value") || 0;
      if (_value < 1) _value = 1; // hard bottom cap = 0
      $("#quantity_" + _id).attr("value", parseInt(_value) -1);
      
      // fire keyup to update the total fields
      $(".quantity").keyup();
   })
});

/*
   Report onclick overlay
*/
$(document).ready(function() {
   $("table#report tr").click(function() {
      var _id = $(this).attr("id").substring(4, $(this).attr("id").length);
      var _hash = $(this).attr("rel");
      $("#overlay").load("snippets/ajax_orderdetail.asp?id="+_id+ "&hash="+_hash);
      $("#overlay").show();
   });
});
