function refreshPricesStockAutoAttach () {
  for (i = 0; selectx = document.getElementsByTagName("*")[i]; i++) {
    if (hasClass(selectx, 'select-attribute')) {
      selectx.onchange = function () { refreshPrices(); }
    }
  }
  refreshPrices();
  pricediv = document.getElementById('PriceDiv');
  setInterval('PriceFloat()', 100);
}

function refreshPrices () {
  var attributes='';
  var forbidden='';
  var categories='';
  var surcharge=baseprice;
  var stock_toggle=true;
  var forbidden_toggle = false;
  for (i = 0; selectx = document.getElementsByTagName("*")[i]; i++) {
  if (!hasClass(selectx, 'select-attribute')) {continue;}
  if (hasClass(selectx, 'form-radio') && !selectx.checked) {continue;}
  vid=selectx.name.replace(/edit\[variations\]\[/,"");
  vid=vid.replace(/\]/,"");
  attribute=variations[vid]['attributes'][selectx.value];
  surcharge+=parseFloat(attribute['surcharge']);
  if (forbidden && attribute['forbidden_category']){forbidden+=','+attribute['forbidden_category'];}
  else if (attribute['forbidden_category']){forbidden=attribute['forbidden_category'];}
  if (categories && attribute['category']){categories+=','+attribute['category'];}
  else if (attribute['category']){categories=attribute['category'];}
  if (attribute['image']){document.getElementById('img_num_' + vid ).src = attribute['image'];}
  else {document.getElementById('img_num_' + vid ).src = var_image[vid];}
  if (stock_toggle) {stock = parseInt(attribute['stock']);stock_toggle = false;}
  else {
    if (stock > parseInt(attribute['stock']) || (stock == -1 && stock < parseInt(attribute['stock'])) ) {stock = parseInt(attribute['stock']);}
  }
  }
  forbidden=forbidden.split(',');
  categories=categories.split(',');
  f_len = forbidden.length;
  c_len = categories.length;
  for (f=0; f<f_len; f++) {
    for (c=0; c<c_len; c++) {
      if (categories[c]==forbidden[f] && categories[c]!='') {
        stock= 0;
        forbidden_toggle = true;
        break;
      }
    }
  }
  if (forbidden_toggle) {document.getElementById('edit-newprice').value = message_forbidden;}
  else {
    if (stock != 0) {
      document.getElementById('edit-newprice').value = HTTPGet(url_ajax + 'permutation_price;'+ surcharge, this.receive, this) ;
    }
    else {document.getElementById('edit-newprice').value = message_sold_out;}
  }
  for (i = 0; button = document.getElementsByName('op')[i]; i++) {
    if (hasClass(button, 'addtocart')) {button.disabled = (!stock);}
  }
}

function PriceFloat() {
  pricediv.style.width = '180px';
  pricediv.style.top = (window.pageYOffset + 200) + 'px';
  pricediv.style.left = (window.innerWidth -20- parseInt(pricediv.style.width)) + 'px';
}

if (isJsEnabled()) {
  addLoadEvent(refreshPricesStockAutoAttach);
}