arrAttributeIDs = new Array();
arrAttributeNames = new Array();
arrAttributeSelectables = new Array();
arrAttributeValueIDs = new Array();
arrAttributeValueValues = new Array();
arrAttributeValueStocks = new Array();
arrAttributeValueAvailableDates = new Array();

arrAttributeProductIDs = new Array();
arrAttributeProductCombos = new Array();
arrAttributeProductAttributeSummarys = new Array();
arrAttributeProductImages = new Array();
arrAttributeProductZoomImages = new Array();
arrAttributeProductImagePopUpLinks = new Array();
arrAttributeProductMediumImages = new Array();
arrAttributeProductThumbMiniImages = new Array();
arrAttributeProductPrices = new Array();
arrAttributeProductPricesIncVAT = new Array();
arrAttributeProductStocks = new Array();
arrAttributeProductAvailableDates = new Array();

arrAttributeProductPricesRRP = new Array();
arrAttributeProductPricesWAS = new Array();

arrAttributeProductarrProductImagesImages = new Array();
arrAttributeProductarrProductImagesImagePopUpLinks = new Array();
arrAttributeProductarrProductImagesMediumImages = new Array();
arrAttributeProductarrProductImagesThumbMiniImages = new Array();
arrAttributeProductarrProductImagesZoomImages = new Array();

function getCurrentCombo(attributekey)
{
  var x;
  var nextid;
  var selectedvalue;

  arrAttributeCombo = '';
  strIDLog = '';
  selectedvalue = '';
  for(x = 0; x < arrAttributeIDs.length; x++)
  {
    if (x < attributekey)
    {
      nextid = 'attribute_'+x;
      if (document.getElementById(nextid))
      {
        //alert(document.getElementById(nextid).value);
        selectedvalue = document.getElementById(nextid).value;
        if (selectedvalue != 0 && selectedvalue != '')
        {
          if (arrAttributeCombo != '')
          {
            arrAttributeCombo += '_';
          }
          arrAttributeCombo += selectedvalue;
          strIDLog += '//' + nextid;
        }
      }
    }
  }

  //alert('getCurrentCombo for ' + attributekey + ': ' + arrAttributeCombo + ', matched at ' + strIDLog);

  return arrAttributeCombo;
}

function chooseOptions()
{
  var x;
  var nextid;
  var selectedvalue;

  for(x = 0; x < arrAttributeIDs.length; x++)
  {
    nextid = 'attribute_'+x;
    if (document.getElementById(nextid))
    {
      selectedvalue = document.getElementById(nextid).value;
      if (selectedvalue != 0 && selectedvalue != '')
      {
      }
      else
      {
        alert('Please choose a '+arrAttributeNames[x]);
        break;
      }
    }
  }

  return false;
}

function cleanFrom(attributekey)
{
  var x;
  var nextid;
  //alert(document.getElementById('addtobasket_enabled'));
  if (document.getElementById('addtobasket_enabled'))
  {
    document.getElementById('addtobasket_enabled').style.display = 'none';
  }
  if (document.getElementById('addtobasket_disabled'))
  {

    document.getElementById('addtobasket_disabled').style.display = '';
    document.getElementById('addtobasket_disabled').onclick = function() { return chooseOptions() };
  }

  for(x = arrAttributeIDs.length-1; x > attributekey; x--)
  {
    nextid = 'attribute_'+x;
    if (document.getElementById(nextid))
    {
      removeAllOptions(x);

      //if (arrAttributeValueIDs[x].length > 1)
      //{
        if (blnOptionsShowNames)
        {
          addOption(x,0,strOptionsNamePrepend+arrAttributeNames[x]);
        }
        else
        {
          addOption(x,0,'');
        }
      //}

      document.getElementById(nextid).disabled = true;
    }
  }
}

function changeTopPriceBreaks()
{
  var str_attribute_header_row = '';
  var str_attribute_row = '';
  var done_price_breaks = new Array();
  var break_is_valid = true;

  for (x = 0; x < arrAttributeProductCombos.length; x++) {
    if (arrAttributeCombo == arrAttributeProductCombos[x])
    {
     //get price break headers if they exist
      for (y = 0; y < arrPriceBreaks[x].length; y++) {
        break_is_valid = true;
        for (i=0;i<done_price_breaks.length;i++) {
          if (done_price_breaks[i] == arrPriceBreaks[x][y]) break_is_valid = false;
        }
        if (break_is_valid == true) {
          done_price_breaks[y] = arrPriceBreaks[x][y];
          if (document.getElementById('price_break' + (y + 1) + '_header')) {
            //build up row html
            str_attribute_header_row += '<th style="text-align:center">' + document.getElementById('price_break' + (y + 1) + '_header').innerHTML + '</th>';
          }
          if (document.getElementById('attribute_price_' + arrPriceBreaks[x][y])) {
            //build up row html
            str_attribute_row += '<td class="price_break1 price" style="text-align:center">' + document.getElementById('attribute_price_' + arrPriceBreaks[x][y]).innerHTML + '</td>';
          }
        }
      }
      if (document.getElementById('top_price_break_header_row'))
      {
        $('#top_price_break_header_row').html(str_attribute_header_row);
      }
      if (document.getElementById('top_price_break_row'))
      {
        $('#top_price_break_row').html(str_attribute_row);
      }
    }
  }
}




function populateOptions(attributekey, blnUpdatePrice)
{
  //alert(attributekey);
  //alert('populateOptions:' + attributekey);
  var x;
  var y;
  var z;
  var alreadyadded;
  var select;
  var id = 'attribute_'+attributekey;
  var nextid;
  var selectedvalue;
  var arrAttributeCombo;
  var partcombo;
  var arrAttributeTheseValuesIDs = new Array();
  var arrAttributeTheseValuesValues = new Array();
  var arrAttributeTheseValuesStocks = new Array();
  var arrAttributeTheseValuesStockDates = new Array();
  var arrAttributeTheseValues;

  cleanFrom(attributekey);

  arrAttributeCombo = getCurrentCombo(attributekey);

  if (attributekey < arrAttributeIDs.length)
  {



    //alert(arrAttributeCombo);
    arrAttributeCombo += '_';
    if (arrAttributeCombo != '_')
    {
      //alert('current combo '+arrAttributeCombo);
    }
    //Work out possible options for this list
    if (attributekey == 0)
    {
      for(y = 0; y < arrAttributeValueIDs[attributekey].length; y++)
      {
        //alert('adding '+arrAttributeValueIDs[attributekey][y]);
        arrAttributeTheseValuesIDs[arrAttributeTheseValuesIDs.length] = arrAttributeValueIDs[attributekey][y];
        arrAttributeTheseValuesValues[arrAttributeTheseValuesValues.length] = arrAttributeValueValues[attributekey][y];
        arrAttributeTheseValuesStocks[arrAttributeTheseValuesStocks.length] = arrAttributeValueStocks[attributekey][y];

        if(arrAttributeValueAvailableDates[attributekey] != undefined && [attributekey][y] != undefined)
        {
          arrAttributeTheseValuesStockDates[arrAttributeTheseValuesStocks.length] = arrAttributeValueAvailableDates[attributekey][y];
        }
      }
    }
    else
    {
      for(x = 0; x < arrAttributeProductCombos.length; x++)
      {
        partcombo = arrAttributeProductCombos[x].substr(0,arrAttributeCombo.length);




        if (arrAttributeCombo == partcombo)
        {
          //alert(arrAttributeProductMediumImages[x]);

          if (document.getElementById('product_medium_image') && arrAttributeProductMediumImages[x] && document.getElementById('product_medium_image').src != arrAttributeProductMediumImages[x])
          {
            document.getElementById('product_medium_image').src = arrAttributeProductMediumImages[x];
            if (document.getElementById('product_medium_link') && arrAttributeProductImagePopUpLinks[x])
            {
              document.getElementById('product_medium_link').href = arrAttributeProductImagePopUpLinks[x];
            }
          }

          //product_zoom_image
          if (document.getElementById('product_zoom_image') && arrAttributeProductZoomImages[x] && document.getElementById('product_zoom_image').href != arrAttributeProductZoomImages[x])
          {
            document.getElementById('product_zoom_image').href = arrAttributeProductZoomImages[x];
          }
          else
          {
            document.getElementById('product_zoom_image').href = "#";
          }

          arrProductThumbImages = new Array();
          arrProductThumbLinks = new Array();
          arrProductZoomImages = new Array();
          if (document.getElementById('product_thumb_image_0') && arrAttributeProductThumbMiniImages[x])
          {
            arrProductThumbImages[0] = arrAttributeProductMediumImages[x];
            arrProductThumbLinks[0] = arrAttributeProductImagePopUpLinks[x];
            arrProductZoomImages[0] = arrAttributeProductZoomImages[x];
            document.getElementById('product_thumb_image_0').src = arrAttributeProductThumbMiniImages[x];

            if(arrAttributeProductarrProductImagesThumbMiniImages[x].length > 0)
            {
              document.getElementById('product_thumb_0').style.display = '';
            }
            else
            {
              document.getElementById('product_thumb_0').style.display = 'none';
            }
            //alert(arrAttributeProductThumbMiniImages[x]);
          }

          if ($('#product_thumb_images') && arrAttributeProductarrProductImagesThumbMiniImages[x])
          {
            for(y = 0; y < arrAttributeProductarrProductImagesThumbMiniImages[x].length; y++)
            {
              if ($('#product_thumb_image_'+(y+1)))
              {
                $('#product_thumb_image_'+(y+1)).attr("src", arrAttributeProductarrProductImagesThumbMiniImages[x][y]);
              }
              if ($('#product_thumb_'+(y+1)))
              {
                $('#product_thumb_'+(y+1)).show();
              }

            }


            for(y = arrAttributeProductarrProductImagesImages[x].length; y < 12; y++)
            {
              if ($('#product_thumb_'+(y+1)))
              {
                $('#product_thumb_'+(y+1)).hide();
              }
            }
            for(y = 0; y < arrAttributeProductarrProductImagesImagePopUpLinks[x].length; y++)
            {
              arrProductThumbLinks[y+1] = arrAttributeProductarrProductImagesImagePopUpLinks[x][y];
            }
            for(y = 0; y < arrAttributeProductarrProductImagesMediumImages[x].length; y++)
            {
              arrProductThumbImages[y+1] = arrAttributeProductarrProductImagesMediumImages[x][y];
            }
            //alert("here");
            for(y = 0; y < arrAttributeProductarrProductImagesZoomImages[x].length; y++)
            {
              arrProductZoomImages[y+1] = arrAttributeProductarrProductImagesZoomImages[x][y];
            }
            setThumbs();
          }



          arrAttributeTheseValues = arrAttributeProductCombos[x].split('_');

          for(y = 0; y < arrAttributeValueIDs[attributekey].length; y++)
          {
            if (arrAttributeValueIDs[attributekey][y] == arrAttributeTheseValues[attributekey])
            {
              //alert('adding '+arrAttributeValueValues[attributekey][y]);
              alreadyadded = false;
              for(z = 0; z < arrAttributeTheseValuesIDs.length; z++)
              {
                if (arrAttributeTheseValuesIDs[z] == arrAttributeValueIDs[attributekey][y])
                {
                  alreadyadded = true;
                }
              }
              if (alreadyadded == false)
              {
                arrAttributeTheseValuesIDs[arrAttributeTheseValuesIDs.length] = arrAttributeValueIDs[attributekey][y];
                arrAttributeTheseValuesValues[arrAttributeTheseValuesValues.length] = arrAttributeValueValues[attributekey][y];
                if (arrAttributeValueStocks[attributekey][y] == 'N' || arrAttributeProductStocks[x] == 'N')
                {
                  arrAttributeTheseValuesStocks[arrAttributeTheseValuesStocks.length] = 'N';
                }
                else
                {
                  arrAttributeTheseValuesStocks[arrAttributeTheseValuesStocks.length] = 'Y';
                }
              }
            }
          }
        }
      }
    }


    select = document.getElementById(id);
    if (select)
    {
      removeAllOptions(attributekey);

      if (arrAttributeTheseValuesIDs.length > 1)
      {
        if (blnOptionsShowNames)
        {
          addOption(attributekey,0,strOptionsNamePrepend+arrAttributeNames[attributekey]);
        }
        else
        {
          addOption(attributekey,0,'');
        }
      }

      for(x = 0; x < arrAttributeTheseValuesIDs.length; x++)
      {
        var text = arrAttributeTheseValuesValues[x];
        if (arrAttributeTheseValuesStocks[x] == 'N')
        {
          if(arrAttributeTheseValuesStockDates[x] != undefined)
          {
            text += arrAttributeTheseValuesStockDates[x];
          }
          else
          {
           text += strOptionsOutOfStock;
          }
        }
        //alert('About to add ' + attributekey + ' with: ' + arrAttributeTheseValuesIDs[x] + '/' + text)
        addOption(attributekey, arrAttributeTheseValuesIDs[x], text);
      }

      //when changing attributes, need to run populate options possibly and change price breaks table near description
      select.onchange = function() { selectedOption(attributekey,this.selectedIndex);changeTopPriceBreaks() };
      select.disabled = false;

      if (select.options.length == 1)
      {
        selectOption(attributekey,0);
      }
    }
  }
  else
  {
    //alert('B, combo length ' + arrAttributeProductCombos.length + ', ' + arrAttributeCombo);
    //No Options
    if(arrAttributeCombo == '' && arrAttributeProductCombos.length == 0)
    {
      if (document.getElementById('addtobasket_disabled'))
      {
        document.getElementById('addtobasket_disabled').style.display = 'none';
      }
      if (document.getElementById('addtobasket_enabled'))
      {
        document.getElementById('addtobasket_enabled').style.display = '';
      }
    }

    for(x = 0; x < arrAttributeProductCombos.length; x++)
    {
      if (arrAttributeCombo == arrAttributeProductCombos[x])
      {
        //alert('else, product '+arrAttributeProductIDs[x]+' selected');
        //Show buy button etc.
        if (document.getElementById('product_attribute_summary'))
        {
          document.getElementById('product_attribute_summary').innerHTML = arrAttributeProductAttributeSummarys[x];
        }
        if (document.getElementById('product_price'))
        {
          if (arrPriceBreaks[x].length > 0)
          {
            //alert("a");
            var strMainProductPrice = '';
            var strMainProductPriceIncVAT = '';
            var iPointer = $("#quantity").val();
            if (iPointer > 1)
            {
              while ($('#price_break_' + iPointer).length == 0 && iPointer > 0)
              {
                iPointer--;
              }
            }
            for (y = 0; y < arrPriceBreaks[x].length; y++)
            {
              //alert(arrPriceBreaks[x][y]);
              if (document.getElementById('price_break_' + arrPriceBreaks[x][y]))
              {
                //alert(arrAttributeProductPrices[x][arrPriceBreaks[x][y]]);
                document.getElementById('price_break_' + arrPriceBreaks[x][y]).innerHTML = arrAttributeProductPrices[x][arrPriceBreaks[x][y]];

                var strDisplay = 'style="display:none;"';
                if(arrPriceBreaks[x][y] == iPointer)
                {
                  strDisplay = '';
                }
                strMainProductPrice = strMainProductPrice + '<span id="price_break_' + arrPriceBreaks[x][y] + '" class="price_break" ' + strDisplay + '>' + arrAttributeProductPrices[x][arrPriceBreaks[x][y]] + '</span>';
                if (arrAttributeProductPricesIncVAT.length > 0) strMainProductPriceIncVAT = strMainProductPriceIncVAT + '<span id="price_break_' + arrPriceBreaks[x][y] + '" class="price_break" ' + strDisplay + '>' + arrAttributeProductPricesIncVAT[x][arrPriceBreaks[x][y]] + '</span>';
              }

              if(document.getElementById('attribute_rrp_price_' + arrPriceBreaks[x][y]))
              {
                //alert(arrAttributeProductPricesRRP[x][arrPriceBreaks[x][y]]);
                document.getElementById('attribute_rrp_price_' + arrPriceBreaks[x][y]).innerHTML = 'RRP: '+arrAttributeProductPricesRRP[x][arrPriceBreaks[x][y]];
              }

              if(document.getElementById('attribute_was_price_' + arrPriceBreaks[x][y]))
              {
                document.getElementById('attribute_was_price_' + arrPriceBreaks[x][y]).innerHTML = 'WAS: '+arrAttributeProductPricesWAS[x][arrPriceBreaks[x][y]];
              }

            }
            if (arrAttributeProductPricesIncVAT.length > 0)
            {
              //alert(strMainProductPrice);
              document.getElementById('product_price').innerHTML = strMainProductPrice + '<p id="price_inc">(' + strMainProductPriceIncVAT + ' inc. vat)</p>';
            }
            else
            {
              //alert(strMainProductPrice);
              document.getElementById('product_price').innerHTML = strMainProductPrice;
            }
          }
          else
          {

            document.getElementById('product_price').innerHTML = arrAttributeProductPrices[x][1];
          }
        }
        if(document.getElementById('attribute_link'))
        {
          document.getElementById('attribute_link').href = arrAttributeProductLinks[x];
        }
        if(document.getElementById('attribute_max_quantity'))
        {
          document.getElementById('attribute_max_quantity').value = arrAttributeMaximumQuantities[x];
        }
        if(document.getElementById('attribute_min_quantity'))
        {
          document.getElementById('attribute_min_quantity').value = arrAttributeMinimumQuantities[x];
        }
        if(document.getElementById('attribute_code'))
        {
          document.getElementById('attribute_code').innerHTML = arrAttributeProductModels[x];
        }
        if (document.getElementById('product_image') && arrAttributeProductImages[x])
        {
          document.getElementById('product_image').src = arrAttributeProductImages[x];
        }
        if (document.getElementById('product_medium_image') && arrAttributeProductMediumImages[x])
        {
          document.getElementById('product_medium_image').src = arrAttributeProductMediumImages[x];
          if (document.getElementById('product_medium_link') && arrAttributeProductImagePopUpLinks[x])
          {
            document.getElementById('product_medium_link').href = arrAttributeProductImagePopUpLinks[x];
          }
        }
        arrProductThumbImages = new Array();
        arrProductThumbLinks = new Array();
        arrProductZoomImages = new Array();
        if (document.getElementById('product_thumb_image_0') && arrAttributeProductThumbMiniImages[x])
        {
          arrProductThumbImages[0] = arrAttributeProductMediumImages[x];
          arrProductThumbLinks[0] = arrAttributeProductImagePopUpLinks[x];
          arrProductZoomImages[0] = arrAttributeProductZoomImages[x];
          document.getElementById('product_thumb_image_0').src = arrAttributeProductThumbMiniImages[x];

          if(arrAttributeProductThumbMiniImages[x].length > 0)
          {
            document.getElementById('product_thumb_0').style.display = '';
          }
          else
          {
            document.getElementById('product_thumb_0').style.display = 'none';
          }
        }

        if ($('#product_thumb_images') && arrAttributeProductarrProductImagesThumbMiniImages[x])
        {
          for(y = 0; y < arrAttributeProductarrProductImagesThumbMiniImages[x].length; y++)
          {
            if ($('#product_thumb_image_'+(y+1)))
            {
              $('#product_thumb_image_'+(y+1)).attr("src", arrAttributeProductarrProductImagesThumbMiniImages[x][y]);
            }
            if ($('#product_thumb_'+(y+1)))
            {
              $('#product_thumb_'+(y+1)).show();
            }

          }
          for(y = arrAttributeProductarrProductImagesImages[x].length; y < 12; y++)
          {
            if ($('#product_thumb_'+(y+1)))
            {
              $('#product_thumb_'+(y+1)).hide();
            }
          }
          for(y = 0; y < arrAttributeProductarrProductImagesImagePopUpLinks[x].length; y++)
          {
            arrProductThumbLinks[y+1] = arrAttributeProductarrProductImagesImagePopUpLinks[x][y];
          }
          for(y = 0; y < arrAttributeProductarrProductImagesMediumImages[x].length; y++)
          {
            arrProductThumbImages[y+1] = arrAttributeProductarrProductImagesMediumImages[x][y];
          }
          for(y = 0; y < arrAttributeProductarrProductImagesZoomImages[x].length; y++)
          {
            arrProductZoomImages[y+1] = arrAttributeProductarrProductImagesZoomImages[x][y];
          }

          setThumbs();
        }
        if (document.getElementById('product_id'))
        {
          document.getElementById('product_id').value = arrAttributeProductIDs[x];
        }

        if($('#wishlist_link'))
        {
          $('#wishlist_link').attr("href", "/wishlist/add/"+arrAttributeProductIDs[x]);
        }

        if(arrAttributeProductStocks[x] == 'Y')
        {
          if (document.getElementById('addtobasket_disabled'))
          {
            document.getElementById('addtobasket_disabled').style.display = 'none';
          }
          if (document.getElementById('addtobasket_enabled'))
          {
            document.getElementById('addtobasket_enabled').style.display = '';
          }
        }
      }
    }
  }

  if (blnUpdatePrice == 1)
  {
    refreshCurrency();
    refreshVat();
  }
}

function removeAllOptions(attributekey)
{
  var x;
  var elSelect;
  var strId = 'attribute_'+attributekey;

  elSelect = document.getElementById(strId);

  if (elSelect)
  {
    for(x = elSelect.options.length-1; x >= 0; x--)
    {
      removeOption(attributekey,x);
    }
  }
}

function removeOption(attributekey,valuekey)
{
  var elSelect;
  var id = 'attribute_'+attributekey;

  elSelect = document.getElementById(id);

  if (elSelect)
  {
    if (elSelect.options.length == 1)
    {
      elSelect.remove(elSelect.length - 1);
    }
    else
    {
      elSelect.remove(valuekey);
    }
  }
}

function addOption(attributekey,value,text)
{
  //alert('adding '+text+' to '+attributekey);
  var elSelect;
  var strId = 'attribute_'+attributekey;

  elSelect = document.getElementById(strId);

  if (elSelect)
  {
    //alert("Asked for ID: " + strId + "\nActually got: " + elSelect.id + "(" + elSelect.name + ")")
    var newoption = document.createElement('option');
    newoption.text = text;
    newoption.value = value;
    document.body.insert
    elSelect.add(newoption, newoption.selectedIndex);
  }
}

function selectOption(attributekey,valuekey)
{
  var elSelect;
  var strId = 'attribute_'+attributekey;

  elSelect = document.getElementById(strId);

  if (elSelect)
  {
    elSelect.selectedIndex = valuekey;
    selectedOption(attributekey,valuekey);
  }
  else
  {
    //alert('selectOption: Unable to find ' + strId + ' to add in ' + attributekey + ' and ' + valuekey);
  }
}

function selectedOption(attributekey,valuekey)
{
  var elSelect;
  var strId = 'attribute_'+attributekey;

  elSelect = document.getElementById(strId);

  if (elSelect)
  {
    if(elSelect.value != 0 && elSelect.value != '')
    {
      populateOptions(attributekey+1);
    }
    else
    {
      cleanFrom(attributekey);
    }
  }
  else
  {
    //alert('selectedOption: Unable to find ' + strId + ' to add in ' + attributekey + ' and ' + valuekey);
  };
}
