
function setProductsNamePrice() {
  var priceBreakQtyArr = [];
  var configPrice = 0;
  var configAvailable = true;
  var configAvailabilityDate = '';
  var configQtyArr = [];
  var tdProductsUsed = [];
  var configStock = 0;
  
  // initialize the quantity price break array
  var k;
  for (k = 0; k < qPriceBreaks.length; k++) {
    priceBreakQtyArr[k] = 0.0;
  }
  
  // parse the config options array
  for (var i = 0; i < configProducts.length; i++) {

    var currSelectedProductIndex = parseInt($('input[name="configOpt\\[' + i + '\\]"]:checked').attr('id').split('_')[2], 10);
    
    // before anything else disable the non compatible products of the category
    // and find the index of the first available option in the group so we
    // can move the selection there if the current selection is not available
    var firstAvail = -1;
    var currSelectedIsDisabled = false;
    for (var j = 0; j < configProducts[i].products.length; j++) {
      var failingOnlyWithOptionTitlesArr = []; 
      
      for (k = 0; k < configProducts[i].products[j].only_with_keys.length; k++) {
        var keyIndex = configProducts[i].products[j].only_with_keys[k];
        
        if (keyIndex < tdProductsUsed.length && configProducts[i].products[j].only_with_products[k].indexOf(tdProductsUsed[keyIndex]) == -1) {
          failingOnlyWithOptionTitlesArr.push(configProducts[keyIndex].title);
        }
      }
      
      if (failingOnlyWithOptionTitlesArr.length > 0) {
        // disable actions
        if (currSelectedProductIndex == j) { currSelectedIsDisabled = true; }
        $('#configOpt_' + i + '_' + j).attr('disabled', true);
        $('#configOptAvail_' + i + '_' + j).html('<br/>' + jsPrintf(txtProductNotAvail, failingOnlyWithOptionTitlesArr.join(', ')));
        $('#configOptFirstLine_' + i + '_' + j).addClass('confOptProdNotAvail');
      } else { 
        // enable actions
        if (firstAvail < 0) { firstAvail = j; }
        if ($('#configOpt_' + i + '_' + j).attr('disabled')) {
          $('#configOpt_' + i + '_' + j).removeAttr('disabled');
          $('#configOptAvail_' + i + '_' + j).html('');
          $('#configOptFirstLine_' + i + '_' + j).removeClass('confOptProdNotAvail');
        }
      }
    }
    
    // if we need to hide an option when it is not available
    if (configProducts[i].hide_unavailable == 1) {
      $('#configOption_' + i).toggle(firstAvail >= 0 && firstAvail < configProducts[i].products.length); 
    }
    
    // if currently selected is the "not required"  while the product specifies the option is required
    // it means the option was previously forced
    if (currSelectedProductIndex == configProducts[i].products.length && configProducts[i].is_required == 1) {
      currSelectedIsDisabled = true;
    }
    
    // move the selection to the first available option
    // if there is no available option then set a "not required" flag
    var forceShowNotReq = false;
    if (currSelectedIsDisabled) {
      if (firstAvail < 0) { // means no products are available
        forceShowNotReq = true;
        $('#configOptRow_' + i + '_' + configProducts[i].products.length).show();
        $('#configOpt_' + i + '_' + configProducts[i].products.length).attr('checked', true);
      } else {
        $('#configOpt_' + i + '_' + firstAvail).attr('checked', true);
      }
    }
    
    var selectedProductIndex = parseInt($('input[name="configOpt\\[' + i + '\\]"]:checked').attr('id').split('_')[2], 10);
    var basePrice = 0;
    
    if (selectedProductIndex < configProducts[i].products.length) {
      // set the base price
      basePrice = configProducts[i].products[selectedProductIndex].price * configProducts[i].products[selectedProductIndex].quantity;

      // calculate the quantity price break
      for (k = 0; k < qPriceBreaks.length; k++) {
        priceBreakQtyArr[k] += configProducts[i].products[selectedProductIndex].priceBreaks[k] * configProducts[i].products[selectedProductIndex].quantity;
      }
      
      // calculate the stock
      // since the same product may be used in different options add up the product quantities
      if (configQtyArr[configProducts[i].products[selectedProductIndex].products_id] === undefined) {
        configQtyArr[configProducts[i].products[selectedProductIndex].products_id] = configProducts[i].products[selectedProductIndex].quantity;  
      } else {
        configQtyArr[configProducts[i].products[selectedProductIndex].products_id] += configProducts[i].products[selectedProductIndex].quantity;
      }
      var stockAvail = parseInt(configProducts[i].products[selectedProductIndex].stockQty / configQtyArr[configProducts[i].products[selectedProductIndex].products_id], 10);
      configStock = (i === 0) ? stockAvail : Math.min(configStock, stockAvail); // if i === 0 means that we have no previous value for configStock
      
      // calculate the availability
      var prodAvailDate = configProducts[i].products[selectedProductIndex].availabilityDate;
      if (configStock < 1) {
        configAvailable = false;
      } else if (prodAvailDate > todaysDate) {
        if (configAvailabilityDate === '' || prodAvailDate > configAvailabilityDate) {
          configAvailabilityDate = prodAvailDate;
        }
      }
      
      
      $('#configOptImg_' + i).attr('src', imgFolder + configProducts[i].products[selectedProductIndex].imageName).
                              attr('title', $('#configOptName_' + i + '_' + selectedProductIndex).text()).
                              attr('alt', $('#configOptName_' + i + '_' + selectedProductIndex).text());
    } else {
      $('#configOptImg_' + i).attr('src', imgFolder + imgSpacer).
                              attr('title', '').
                              attr('alt', $('#configOptName_' + i + '_' + selectedProductIndex).text());
    }
      
    // add base price to the total configuration price
    configPrice += basePrice;
    
    // parse the config products array
    for (j = 0; j < configProducts[i].products.length; j++) {
      var lblPrice;

      // is this the selected product
      if (selectedProductIndex == j) {
        lblPrice = txtIncludedInPrice;
        $('[id*=configOptAttrRow_' + i + '_' + j + ']').show();
      } else {
        if (configProducts[i].products[j].price * configProducts[i].products[j].quantity >= basePrice) {
          lblPrice = txtPriceAdd + formatCurrency(configProducts[i].products[j].price * configProducts[i].products[j].quantity - basePrice);
        } else {
          lblPrice = txtPriceSubstract + formatCurrency(basePrice - configProducts[i].products[j].price * configProducts[i].products[j].quantity);
        }
        $('[id*=configOptAttrRow_' + i + '_' + j + ']').hide();
      }
      
      $('#configOptPrice_' + i + '_' + j).html(lblPrice);
    }
    
    // add the product to the top down products used array, push a zero product to signify that no product was selected for that position
    tdProductsUsed[i] = (selectedProductIndex < configProducts[i].products.length) ? configProducts[i].products[selectedProductIndex].products_id : 0;     
    
    // if option not required hide the row
    if (configProducts[i].is_required == 1 && !forceShowNotReq) {
      $('#configOptRow_' + i + '_' + configProducts[i].products.length).hide();
    }
  }
  
  // write the price to the document
  $('#productsPrice').html(formatCurrency(configPrice));
  
  // write the price breaks table to the document
  $('#priceBreaks > tbody').empty();
  var otPriceBreakIndex = 0, currPrice = '', prevPrice = '';
  for (k = 0; k < qPriceBreaks.length; k++) {
    currPrice = formatCurrency(priceBreakQtyArr[k]);
    if (currPrice != prevPrice) {
      $('#priceBreaks > tbody').append(
        '<tr><td valign="top" align="right">&nbsp;' + ((otPriceBreakIndex < otPriceBreaks.length) ? '&gt;&nbsp;' +  otPriceBreaks[otPriceBreakIndex] + '&euro;' : '') + '&nbsp;</td>' +
            '<td valign="top" align="right">&nbsp;' + qPriceBreaks[k] + '&nbsp;</td>' +
            '<td valign="top" align="right">&nbsp;' +  formatCurrency(priceBreakQtyArr[k]) + '&nbsp;</td></tr>' );
      otPriceBreakIndex++;
    }
    prevPrice = currPrice;
  }
  
  // write the availability and the stock
  $('#stockFutureAvailDiv').hide(); $('#stockUnavailDiv').hide(); $('#stockCurrentAvailDiv').hide();
  if (!configAvailable) {
    $('#stockUnavailDiv').show();
  } else if (configAvailabilityDate === '') {
    $('#stockCurrentAvailDiv').show();
    $('#stockCurrentAvailQty').text(configStock);
  } else {
    $('#stockFutureAvailDiv').show(); $('#stockFutureAvailDate').text(configAvailabilityDate);
    $('#stockFutureAvailQty').text(configStock);
  }
  
  // highlight the currently selected products
  $('#productInfoTable input[type=radio]').each( function() {
    $('input[name=' + this.name +'] + label').removeClass('configTableProductLabelCurrent');
    $('input[name=' + this.name +']:checked + label').addClass('configTableProductLabelCurrent');
  });
}


$(document).ready( function() {  
  $('#floatingProductInfoTable').scrollFollow( { container: 'productInfoCell', offset: 8 } );
  
  var i;
  for (i = 0; i < postSelectedOptions.length; i++) {
    $('#' + postSelectedOptions[i]).attr('checked', true);
  }
  
  setProductsNamePrice();
  
  for (i = 0; i < preloadImageArr.length; i++) {
    var preloadedImage  = $('<img />').attr('src', imgFolder + preloadImageArr[i]);
    $('#preloadImagePlaceHolder').append(preloadedImage);
  }
  
  $('#implodeOptions').click( function() {
    $('[id*="configOpt_"]:not(:checked)').closest('tr').addClass('imploadedOption');
    $('#implodeOptions').hide(); $('#explodeOptions').show(); 
  });
  $('#explodeOptions').click( function() {
    $('.imploadedOption').removeClass('imploadedOption');
    $('#explodeOptions').hide(); $('#implodeOptions').show();
  });
  
  $('[id*="configOptRow_"]').hover(
    function() { $(this).addClass("highlight"); },
    function() { $(this).removeClass("highlight"); }
  );
  $('[id*="configOptLbl_"]').hover(
      function() { $(this).addClass("configTableProductLabelHover"); },
      function() { $(this).removeClass("configTableProductLabelHover"); }
  );
  $('[id*="configOptRow_"]').click( function(e) {
    var rb = $(this).find('input');
    if (!rb.attr('disabled')) { 
      rb.attr('checked', true); 
      setProductsNamePrice();  
    }
  });
  $('[id*="configOptProdLink_"]').click( function(e) {
    window.open($(this).attr("href"));
    return false;
  });
});
