function ElementName_InventoryIDSet(AElement, AInventoryID)
{
  AElement.name = AElement.name.replace(/_Inv(\d+)/g, '_Inv' + AInventoryID);
}

function InventoryElements_NameAdjust(AInventoryID, APrice, AQuantity, AAttributeOptions, AComponentTypeAttribute)
{
  ElementName_InventoryIDSet(APrice, AInventoryID);
  ElementName_InventoryIDSet(AQuantity, AInventoryID);
  if (AComponentTypeAttribute)
    ElementName_InventoryIDSet(AComponentTypeAttribute, AInventoryID);
  for (var j = 0; j < AAttributeOptions.length; j++)
    ElementName_InventoryIDSet(AAttributeOptions[j], AInventoryID);
}

function ProductInventories_ElementsAdjust()
{
  ElementsWidthAdjust('tblProductInventories', true, ['select', 'input']);
}

function ProductAttributeValuesSet(AElement, ALayoutType)
{
  var LspanAttributes = document.getElementsByName(AElement.parentNode.id);
  for (var i = 0; i < LspanAttributes.length; i++)
  {
    var LAttribute = LspanAttributes[i].getElementsByTagName('select')[0];
    if (LAttribute == AElement)
      ProductInventorySet(LAttribute, ALayoutType);
    else
    if (LAttribute.value == '')
    {
      LAttribute.value = AElement.value;
      if (LAttribute.value != AElement.value)
        LAttribute.value = '';
      ProductInventorySet(LAttribute, ALayoutType);
    }
  }
}

function InventoryMappingValueProcess(AKey, AInventoryMappingKeys, AInventoryMappingValues, AAttributeOptions, AUpdateQuantity, APrice, AQuantity, AComponentTypeAttribute)
{
  var LValue;
  for (var i = 0; i < AInventoryMappingKeys.length; i++)
    if (AInventoryMappingKeys[i] == AKey)
    {
      LValue = AInventoryMappingValues[i];
      var LFirstDelimiterIndex = LValue.indexOf('|');
      var LSecondDelimiterIndex = LValue.indexOf('|', LFirstDelimiterIndex + 1);
      var LInventoryID = LValue.substring(0, LFirstDelimiterIndex);
      APrice.value = LValue.substring(LFirstDelimiterIndex + 1, LSecondDelimiterIndex);
      if (AUpdateQuantity)
        if (LValue.substring(LSecondDelimiterIndex + 1, LValue.length) == 'in-stock')
        {
          AQuantity.value = 1;
          AQuantity.readOnly = false;
        }
        else
        {
          AQuantity.value = 'out-of-stock';
          AQuantity.readOnly = true;
        }
      InventoryElements_NameAdjust(LInventoryID, APrice, AQuantity, AAttributeOptions, AComponentTypeAttribute);
      return i;
    }
  return -1;
}

function ProductInventoryPictureSet(AKeyIndex, AInventoryMappingPictures)
{
  var LPKV = '';
  if ((AInventoryMappingPictures) && (AInventoryMappingPictures.length > AKeyIndex) && (AKeyIndex >= 0))
    LPKV = AInventoryMappingPictures[AKeyIndex];

  if (!LPKV)
    return;

  var LThumbnail = document.getElementById('Thumbnail' + LPKV);
  if (!LThumbnail)
    return;

  if ((LThumbnail.tagName == 'IMG') && (LThumbnail.parentNode.tagName == 'A'))
    document.location = LThumbnail.parentNode.href;
  else
    LThumbnail.click();
}

function ProductInventoryPictureSet_ShoppingCart(AKeyIndex, AItemID, AInventoryMappingImages, AImageMaxHeight)
{
  var LImageInfo = '';
  if ((AInventoryMappingImages) && (AInventoryMappingImages.length > AKeyIndex) && (AKeyIndex >= 0))
    LImageInfo = AInventoryMappingImages[AKeyIndex];
  if (!LImageInfo)
    return;

  var LSmallImage = document.getElementById('SmallImage_' + AItemID);
  if (!LSmallImage)
    return;

  var LFirstDelimiterIndex = LImageInfo.indexOf('|');
  var LSecondDelimiterIndex = LImageInfo.indexOf('|', LFirstDelimiterIndex + 1);
  var LImageURL = LImageInfo.substring(0, LFirstDelimiterIndex);
  var LHeight = parseInt(LImageInfo.substring(LFirstDelimiterIndex + 1, LSecondDelimiterIndex));

  LSmallImage.src = LImageURL;
  LSmallImage.height = ((LHeight > 0) && (LHeight <= AImageMaxHeight)) ? LHeight : AImageMaxHeight;
}

function ProductInventorySet(AElement, ALayoutType)
{
  var InventoryIDBad = 0;
  var LProductInventory;
  if (ALayoutType == 'Horizontal')
    LProductInventory = AElement.parentNode.parentNode.parentNode;
  else
    LProductInventory = AElement.parentNode.parentNode.parentNode.parentNode.parentNode;
  var LAttributeOptions = LProductInventory.getElementsByTagName('select');

  var LInventoryProps = LProductInventory.getElementsByTagName('input');
  var LPrice = LInventoryProps[0];
  var LQuantity = LInventoryProps[1];
  var LComponentTypeAttribute = LInventoryProps[2];

  var LKey = LProductInventory.id;
  for (var i = 0; i < LAttributeOptions.length; i++)
    if (LAttributeOptions[i].value == '')
    {
      LPrice.value = '';
      LQuantity.value = '';
      LQuantity.readOnly = true;
      InventoryElements_NameAdjust(InventoryIDBad, LPrice, LQuantity, LAttributeOptions, LComponentTypeAttribute);
      return;
    }
    else
      LKey = LKey + (LKey == '' ? '' : '|') + LAttributeOptions[i].value;

  var LKeyIndex = InventoryMappingValueProcess(LKey, InventoryMappingKeys, InventoryMappingValues, LAttributeOptions, true, LPrice, LQuantity, LComponentTypeAttribute);
  if (LKeyIndex >= 0)
  {
    ProductInventoryPictureSet(LKeyIndex, InventoryMappingPictures);
    return;
  }

  AElement.value = '';
  LPrice.value = 'n/a';
  LQuantity.value = 'n/a';
  LQuantity.readOnly = true;
  InventoryElements_NameAdjust(InventoryIDBad, LPrice, LQuantity, LAttributeOptions, LComponentTypeAttribute);
}

