$(function() {
  if (jQuery.browser.msie || jQuery.browser.opera) {
    $('form').submit(function() {
      if (!document.submitted) {
        document.submitted = true;
        setTimeout(function() {
          document.submitted = false;
        }, 5000);
        return true;
      }
      return false;
    });
  }
  else {
    $('form').submit(function() {
      var submitElems = $(":submit, :image", this);
      setTimeout(function() {
        submitElems.attr('disabled', true);
      }, 1);
      setTimeout(function() {
        submitElems.attr('disabled', false);
      }, 500);
      return true;
    });
  }
  
  // input 要素にフォーカスがある場合に enter を押下しても、submit されないように設定します
  $("input").live("keypress", function(ev) {
	  var enableEnterInputType = ["submit", "reset", "button", "image"];
	  
	  var elem = $(this);
	  var type = elem.attr("type");
	  // enter を有効とする input 要素であれば、true を返し、イベントの処理を継続させます。
	  if (type && $.inArray(type.toLowerCase(), enableEnterInputType) >= 0) {
		  return true;
	  }
	  
	  // それ以外の input 要素は enter キーが押されたかチェックします
	  if (ev.keyCode == 13) {
		  // enterキーが押下された場合
		  ev.preventDefault();
		  return false;
	  }
	  return true;
  });
});

function onSampleSelected(target) {
    var available = parseInt($("#amount_hidden_" + target).html());
    var limit = parseInt($("#amount_hidden_" + target).html());
    var amount = 0;
    $(".list_" + target).each(function() { amount = amount + parseInt($(this).val()); });
    if (limit - amount >= 0) limit = limit - amount;
    else limit = 0;
    if (limit == 0) {
        $(".amount_" + target).html("");
        $("#notice_choice_able_" + target).css("display", "none");
        $("#notice_choice_disable_" + target).css("display", "block");
      }
      else {
        $(".amount_" + target).html(limit + "点");
        $("#notice_choice_able_" + target).css("display", "block");
        $("#notice_choice_disable_" + target).css("display", "none");
      }
    $(".list_" + target).each(function() {
        var value = parseInt($(this).val());
        var max = value;
        if ((value + limit) <= available) max = value + limit;
        $(this).empty();
        for (var i=0;i<max + 1;i++) { $(this).append($("<option></option>").attr("value", i).text(i)); }
        $(this).val(value);
   });
}

