var zoink = 0;

var intResTimeslot = 0;

function update_res_time() {
   $('#res_time_slot_' + intResTimeslot).hide();
   intResTimeslot = $('#res_time').val();
   $('#res_time_slot_' + intResTimeslot).show();
}


/* **************************************************
   Following code was copied from kadobonnen.js and
   adjusted for the webshop
  ************************************************** */

// auto calculation
$(document).ready(function() {
   $(".quantity").keyup(function() {

      // calc row total
      var _id = ($(this).attr("name")).substring(9, $(this).attr("name").length);
      var _price = parseFloat($("#price_"+_id).attr("value").replace(",", "."));
      _price = _price *$(this).attr("value");
      _price = _price.toFixed(2);
      $("#total_"+_id).attr("value", _price.toString().replace(".", ","));

      // update server side shopping cart through ajax
      $.get("/templates/snippets/ajax_updatecart.asp?id=" +_id+ "&value="+$(this).attr("value") + "&rnd="+(new Date()).getTime());

   });

   // 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;
   });
});


/*
   + 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);

      // update server side shopping cart through ajax
      //$.get("/templates/snippets/ajax_updatecart.asp?id=" +_id+ "&value="+_value);

      // 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);

      // update server side shopping cart through ajax
      //$.get("/templates/snippets/ajax_updatecart.asp?id=" +_id+ "&value="+_value);

      // fire keyup to update the total fields
      $(".quantity").keyup();
   });
});


/*
   extra person select
*/
jQuery(document).ready(function() {
   jQuery('.extrapersonselect').change(function(){
      showPersonBoxes(jQuery(this));
   });
   function showPersonBoxes(selectBox){
      var numvisible = selectBox.val() - 1;
      jQuery('.extrapersonblocks').children().slice(0, numvisible).show();
      jQuery('.extrapersonblocks').children().slice(numvisible).hide();
   }
   showPersonBoxes(jQuery('.extrapersonselect'));
});


/*
   Reservation time
*/
jQuery(document).ready(function() {
   jQuery('select.res_time').change(function(){
      var startTime = jQuery('select.res_time option[value="'+jQuery('select.res_time').val()+'"]').attr('rel');
      var endTime = jQuery('select.res_time_leave option[value="'+jQuery('select.res_time_leave').val()+'"]').attr('rel');
      if(startTime){
         if(!endTime || parseFloat(startTime) > parseFloat(endTime) - 4){
            jQuery('select.res_time_leave').val(jQuery('select.res_time_leave option[rel="' + (parseFloat(startTime) + 4) + '"]').attr('value'));
         }
      }
   });
   jQuery('select.res_time_leave').change(function(){
      var startTime = jQuery('select.res_time option[value="'+jQuery('select.res_time').val()+'"]').attr('rel');
      var endTime = jQuery('select.res_time_leave option[value="'+jQuery('select.res_time_leave').val()+'"]').attr('rel');
      if(endTime){
         if(!startTime || parseFloat(startTime) > parseFloat(endTime) - 4){
            jQuery('select.res_time').val(jQuery('select.res_time option[rel="' + (parseFloat(endTime) - 4) + '"]').attr('value'));
         }
      }
   });
});




