function SC_QuantityValidate(AForm)
{
  for (var i = 0; i < AForm.elements.length; i++)
  {
    var LElement = AForm.elements[i];
    if ((LElement.id.substr(0, 8) == 'Quantity') && !FieldValidate(LElement, fvtInt, 'Quantity', '', true))
      return false;
  }
  return true;
}

function ImageCopyrightHide()
{
  var LdvCopyrightProtection = ElementGet('dvCopyright');
  if (LdvCopyrightProtection)
  {
    LdvCopyrightProtection.style.display = 'none';
    showSelectBoxes(ElementGet('dvCopyright'));
  }
}

function  ImageCopyrightPositionAdjust(AElement)
{
  var LdvCopyright = ElementGet('dvCopyright');
  LdvCopyright.style.display = 'block';

  var LScrollSize = 14;
  var LTop = ElementAbsTopGet(AElement);
  var LLeft = ElementAbsLeftGet(AElement);
  
  if ((LTop + AElement.offsetHeight - document.body.scrollTop + LdvCopyright.offsetHeight + LScrollSize) < document.body.offsetHeight)
    LTop = LTop + AElement.offsetHeight;
  else 
    LTop = LTop - LdvCopyright.offsetHeight;

  if ((LLeft + LdvCopyright.offsetWidth - document.body.scrollLeft + LScrollSize) > document.body.offsetWidth)
    LLeft = document.body.offsetWidth - LdvCopyright.offsetWidth + document.body.scrollLeft - LScrollSize;

  LdvCopyright.style.top = LTop;
  LdvCopyright.style.left = LLeft;
}

function ImageCopyrightShow(AElement, ACopyright)
{
  ElementGet('dvCopyrightContent').innerHTML = ACopyright;
  ImageCopyrightPositionAdjust(AElement);
  hideSelectBoxes(ElementGet('dvCopyright'), AElement.scrollLeft);
}

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()
{
  var LProductInventories = document.getElementById('tblProductInventories');
  if (!LProductInventories)
    return;

  var LSelects = LProductInventories.getElementsByTagName('select');
  for (var i = 0; i < LSelects.length; i++)
  {
    LSelects[i].style.width = LSelects[i].parentNode.parentNode.clientWidth - (LProductInventories.cellPadding * 2);
    if (LSelects[i].options.length == 2)
      LSelects[i].value = LSelects[i].options[1].value;
  }

  var LInputs = LProductInventories.getElementsByTagName('input');
  for (var i = 0; i < LInputs.length; i++)
    if ((LInputs[i].parentNode.tagName == 'TD') && (LInputs[i].parentNode.style.display != 'none'))
      LInputs[i].style.width = LInputs[i].parentNode.clientWidth - (LProductInventories.cellPadding * 2);
}

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);
}

function SC_TopMessageShow()
{
  var LMessage = ElementGet('spMessage');
  if ((LMessage) && (LMessage.style.display == 'none'))
    LMessage.style.display = 'inline';
}

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_ShoppingCart(AElement, AInventoryMappingKeys, AInventoryMappingValues, AInventoryMappingImages, AKeySchema, AItemID, AImageMaxHeight)
{
  SC_TopMessageShow();

  if (!(AInventoryMappingKeys))
    return;

  var LPrice = document.getElementById('inpPrice_' + AItemID);
  var LQuantity = document.getElementById('Quantity_' + AItemID);
  var LspanPrice = document.getElementById('Price_' + AItemID);
  var LAttributeOptionIDs = AKeySchema.match(/Attr_(\d+)_(\d+)/g);

  var LKey = '';
  var LAttributeOptions = new Array();

  document.getElementsByName('IsDirty')[0].value = 'true';

  for (var i = 0; i < LAttributeOptionIDs.length; i++)
  {
    LAttributeOptions[i] = document.getElementById(LAttributeOptionIDs[i]);
    if (LAttributeOptions[i].value == '')
    {
      LspanPrice.innerHTML = 'n/a';
      LPrice.value = '';
      return;
    }
    else
      LKey = LKey + (LKey == '' ? '' : '|') + LAttributeOptions[i].value;
  }

  var LKeyIndex = InventoryMappingValueProcess(LKey, AInventoryMappingKeys, AInventoryMappingValues, LAttributeOptions, false, LPrice, LQuantity);
  if (LKeyIndex >= 0)
  {
    LspanPrice.innerHTML = LPrice.value;
    ProductInventoryPictureSet_ShoppingCart(LKeyIndex, AItemID, AInventoryMappingImages, AImageMaxHeight);
    return;
  }

  alert("This product doesn't have an inventory with current set of attributes");
  AElement.value = '';
  LPrice.value = 'n/a';
  LspanPrice.innerHTML = 'n/a';
}

function PrePaidSupportPageOpen(AFixedDomain)
{
  Window_openNew('http://support.' + AFixedDomain +'/default.asp?S=J1&DocNo=H05120001', 880, 800);
}