/* Copyright(C) Yumeya,inc 2007/4/26
 *    author: Tomokazu ISHII <office@yumeya.net>
 */

function basketColorSizeTable(form) {
  num = form.COLORSIZERB.length;
  if (!num) {
    if (form.COLORSIZERB.checked) {
      form.item.value = form.COLORSIZERB.value;
      // if (!chkMaxnum(form, form.COLORSIZERB.value)) {
      //     return false;
      // }
      setItemnote(form, form.COLORSIZERB.value);
      return true;
    }
    if (form.item.value != '') {
      return true;
    }
  }
  for (i=0; i < num; i++) {
    if (form.COLORSIZERB[i].checked) {
      form.item.value = form.COLORSIZERB[i].value;
      // if (!chkMaxnum(form, form.COLORSIZERB[i].value)) {
      //     return false;
      // }
      setItemnote(form, form.COLORSIZERB[i].value);
      return true;
    }
  }
  window.alert("表からご希望の商品を選んでください");
  return false;
}

function setItemnote(form, itemcode) {
  color = "";
  size  = "";
  n = form.elements.length;
  for (i=0; i<n; i++) {
    if (form.elements[i].name == ('ITEMNOTE*X*' + itemcode)) {
      color = form.elements[i].value;
    }
    else if (form.elements[i].name == ('ITEMNOTE*Y*' + itemcode)) {
      size = form.elements[i].value;
    }
  }
  str = "";
  if ((color != "") && (size != "")) {
    str = color + '/' + size;
  }
  else if (color != "") {
    str = color;
  }
  else if (size != "") {
    str = size;
  }
  if (str != "") {
    str = '(' + str + ')';
  }
  form.itemnote.value = str;
}

function chkMaxnum(form, itemcode) {
  maxnum   = 0;
  quantity = 0;
  n = form.elements.length;
  for (i=0; i<n; i++) {
    if (form.elements[i].name == ('ITEMNOTE*M*' + itemcode)) {
      maxnum = parseInt(form.elements[i].value);
    }
    else if (form.elements[i].name == 'quantity') {
      quantity = parseInt(form.elements[i].value);
    }
  }
  if ((maxnum != 0) && (maxnum < quantity)) {
      window.alert("おひとり様" + maxnum + "個限定の商品です。お手数ですが数量を変更してください");
      return false;
  }
  return true;
}
