var prods;

if (isNaN(items[0][2])) {
  prods = new OrderedVector(size_comp);
  //prods = new Array();
} else {
  prods = new OrderedVector(numeric_comp);
  //prods = new Array();
}

for (var i = 0; i < items.length; i++) {
  var size = items[i][2];
  if (!isNaN(parseInt(size))) {
    size = ' ' + size;  // The string is needed in the event that the size is strictly numeric, so it is not interpreted as an array index
  }
  var color = items[i][1], prod_rn = items[i][0];

  if ( prods[size] == null ) {
    prods.push(size);
    prods[size] = new Array();
  }

  if ( prods[size][color] == null) {
    prods[size].push(color);
    prods[size][color] = prod_rn;
  }

}

function setSizeMenu(theForm) {

  if (gl_colors.length > 1) {
    clearMenu(theForm.color);
  }

  var sizeMenu = theForm.size;

  if (gl_sizes.length > 1) {

    clearMenu(sizeMenu);

    var sizeOpts = sizeMenu.options;

    for ( var i = 0; i < prods.length; i++ ) {
      sizeOpts[sizeOpts.length] = new Option(prods.getElemAt(i), prods.getElemAt(i));
    }
  } else {
    setColorMenu(sizeMenu);
  }
}

function setColorMenu(eventElem) {

  if (gl_colors.length <= 1) {
    eventElem.form.qty.focus();
    return;
  }

  clearMenu(eventElem.form.color);

  if (eventElem.selectedIndex == 0) {
    eventElem.form.color.selectedIndex = 0;
    return;
  }

  if (gl_sizes.length > 1) {
    size = eventElem[eventElem.selectedIndex].value;
  } else {
    size = eventElem.value;
  }

  var opts = eventElem.form.color.options;

  for ( var i = 0; i < prods[size].length; i++ )	{
    opts[opts.length] = new Option(prods[size][i], prods[size][i]);
  }

  with(eventElem.form.color) {
    selectedIndex = 0;
    focus();
  }

}

function clearMenu( menu ) {
  var opts = menu.options;

  for (var i = opts.length - 1; i>0; --i) {
    opts[i]=null;
  }
}

function initSize() {
  if (gl_sizes.length > 1) {
    if (gl_colors.length > 1) {
      isFirst = "First, ";
    } else {
      isFirst = "";
    }
    document.writeln('<select size="1" name="size" onChange="setColorMenu(this)">');
    document.writeln('<option>' + isFirst + 'Choose Size</option>');
    for (var i = 0; i < gl_sizes.length; i++) {
      document.writeln('<option>' + gl_sizes[i] + '</option>');
    }
    document.writeln('</select>');
  } else if (gl_sizes.length == 1) {
    document.writeln('<input type="hidden" name="size" value="' + gl_sizes[0] + '">');
    if (gl_sizes[0] == 'ONE SIZE FITS ALL') return;
    document.writeln('<font face="Arial" size="2"><b>Size: ' + gl_sizes[0] + '</b></font>');
  } else {
    document.writeln('<input type="hidden" name="size" value="">');
  }
}

function initColor() {
  if (gl_colors.length > 1) {
    if (gl_sizes.length > 1) {
      isSecond = "Then, ";
    } else {
      isSecond = "";
    }
    document.writeln('<select size="1" name="color" onChange="if (this.selectedIndex > 0) { this.form.qty.focus(); }">');
    document.writeln('<option>' + isSecond + 'Choose Color</option>');
    for (var i = 0; i < gl_colors.length; i++) {
      // Write in enough option tags as placeholders for Netscape
      document.writeln('<option></option>');
    }
    document.writeln('</select>');
  } else if (gl_colors.length == 1) {
    document.writeln('<input type="hidden" name="color" value="' + gl_colors[0] + '">');
    document.writeln('<font face="Arial" size="2"><b>Color: ' + gl_colors[0] + '</b></font>');
  } else {
    document.writeln('<input type="hidden" name="color" value="">');
  }
}

function setProduct(aForm) {
  if (aForm.size.selectedIndex == 0) { alert("Please select a size"); aForm.size.focus(); return false; }
  if (aForm.color.selectedIndex == 0) { alert("Please select a color"); aForm.color.focus(); return false; }

  var qty = aForm.qty.value;

  var size, color;

  if (gl_sizes.length > 1) {
    size = aForm.size[aForm.size.selectedIndex].value;
  } else {
    size = aForm.size.value;
  }

  if (gl_colors.length > 1) {
    color = aForm.color[aForm.color.selectedIndex].value;
  } else {
    color = aForm.color.value;
  }

  var p = new String(prods[size][color]);

  var found = false; // needed so that we can zero out an items that occur in the form
                     // after the selected item

  for (var i = 0; i < aForm.elements.length; i++) {
	  
    if (aForm.elements[i].name == "catEntryId_" + p) {
      aForm.elements[i + 1].value = qty;
      found = true;
    } else {
      if (aForm.elements[i].name.substring(0, 11) == "catEntryId_") {
        //not to clash with size/color catentryIds the hemming starts at a safe value 9901
        //To skip the CatEntryId_ related to hemming the follow if condition. 
        if (aForm.elements[i].name.substring(0, 13) == "catEntryId_99") {
	         aForm.elements[i + 1].value = 0;
	    }
      }
    }
  }

  if (!found) {
    alert("Couldn't find a matching product.");
  }

  return found;
}

