
function ecommerce_updateLevel(widget)
{
  var parentCode=widget.id.replace(/^add_to_cart_([^_]*).*$/,'$1');
  var level=widget.options[widget.selectedIndex].getAttribute('level').split('!',2);
  var el=$('level_'+parentCode);
  if (el) {
    el.className='stock '+level[0];
    img = el.getElementsByTagName('img')[0]
    img.src = img.src.replace(/[^/]*.jpg$/,level[0]+'.jpg');
    img.alt = level[1];
  }
}

function ecommerce_updatePrice(widget)
{
  var parentCode=widget.id.replace(/^add_to_cart_([^_]*).*$/,'$1');
  var price=widget.options[widget.selectedIndex].getAttribute('price');
  var el=$$('#price_'+parentCode+' .price')[0];
  if (el) {
    el.innerHTML = price;
  }
  var el=$$('#price_'+parentCode+' .custprice')[0];
  if (el) {
    var price=widget.options[widget.selectedIndex].getAttribute('custprice');
    if (el) {
      el.innerHTML = price;
    }
  }
}

function ecommerce_updateSimilarProducts(widget)
{
  var parentCode=widget.id.replace(/^add_to_cart_([^_]*).*$/,'$1');
  var stockCode = widget.options[widget.selectedIndex].getAttribute('value');
  $$('.similar_products.'+parentCode).each(function(el){
    el.hide()
  });
  $$('.similar_products').each(function(el){
    //Can't do this 'if' as part of the selector as prototype bombs if the stock code has unicode chars in it
    if (el.hasClassName(stockCode)) el.show();
  });
}

function ecommerce_updateStockistSearch(widget)
{
  var parentCode=widget.id.replace(/^add_to_cart_([^_]*).*$/,'$1');
  var stockCode = widget.options[widget.selectedIndex].getAttribute('value');
  var el=$$('#stockist_search_'+parentCode+' .search_code')[0];
  if (el) {
    el.value = stockCode;
  }
}

function ecommerce_updateChildDesc(widget)
{
  var parentCode=widget.id.replace(/^add_to_cart_([^_]*).*$/,'$1');
  var desc = widget.options[widget.selectedIndex].getAttribute('desc');
  $$('.child_description_'+parentCode).each(function(el){
    el.innerHTML = desc;
  });
}

function ecommerce_update_add_to_cart(widget)
{
  if (widget.options[widget.selectedIndex].getAttribute('price')) {
    ecommerce_updateLevel(widget);
    ecommerce_updatePrice(widget);
    ecommerce_updateStockistSearch(widget);
    ecommerce_updateSimilarProducts(widget);
    ecommerce_updateChildDesc(widget);
  }else{
    $$('.multimenu.'+widget.id.replace(/^add_to_cart_/,'')).each(function(e){e.hide(); e.disable()});
    child = $(widget.id+'_'+widget.selectedIndex)
    child.show().enable();
    ecommerce_update_add_to_cart(child);
  }
}

function update_current_address()
{
  var dropdown = $('ecommerce_addresses_dropdown');
  var address_boxes = $$('.current_address');
  if (dropdown && address_boxes) {
    var address = dropdown.options[dropdown.selectedIndex].text.replace(/,/g, ",\n");
    address_boxes.each(function(box) {
      box.value = address;
    });
  }
}

function setup_address()
{
  update_current_address();
  if ($('ecommerce_addresses_dropdown')) $('ecommerce_addresses_dropdown').onchange = update_current_address;
}

function setup_shoppinglist()
{
  if (!$$('.ecommerce_trade_shopping_list')[0]) return;
  $$('.ecommerce_trade_shopping_list tr.warn').each(function(e){
    esl_open_product(e);
  });
  
  Event.observe($$('.ecommerce_trade_shopping_list')[0], 'click', click_shoppinglist);
}

function click_shoppinglist(ev)
{
  var el = Event.findElement(ev, 'tr');
  if (!el) return;
  esl_toggle_children(el, 'toggle');
}

function esl_toggle_children(parent, action)
{
  var id = parent.id.replace(/^esl/,'.ecommerce_trade_shopping_list .parent');
  $$(id).each(function(el){
    if (action == 'toggle')
      el.toggle();
    else if (action == 'hide')
      el.hide();
    else if (action == 'show')
      el.show();

    if (!el.visible()) {
      esl_toggle_children(el, 'hide'); 
    }
  });
}

function esl_open_product(product_element)
{
  var parent_name = $w(product_element.className).find(function(n){ return n.startsWith('parent_')});
  esl_toggle_children(product_element, 'show');
  if (!parent_name) return;
  esl_open_product($(parent_name.replace(/^parent_/, 'esl_')));
}

function setup_keycodes()
{
  if (!$('ecommerce_keycode_dropdown')) return;
  Event.observe($('ecommerce_keycode_type'), 'change', change_keycode);
}

function change_keycode(ev)
{
  type = $F(ev.element());
  $$('#ecommerce_keycode_dropdown select').each(function(el){
    if (el.id == 'ecommerce_keycode_type') return;
    if (el.getAttribute('ekk_type') == type) {
      el.show();
      el.enable();
      el.selectedIndex = 0;
    }else{
      el.hide(); 
      el.disable();
    }
  });
}

function setup_cart()
{
  if ($$('#ecommerce_cart input[type=text]')) {
    $$('#ecommerce_cart input[type=text]').each(function(el){
      Event.observe(el, 'change', show_update_cart_message);
    })
  }
}

function show_update_cart_message()
{
  if ($('cart_update_required')) {
    $('cart_update_required').show();
  }
}

Event.observe(window, 'load', setup_address);
Event.observe(window, 'load', setup_shoppinglist);
Event.observe(window, 'load', setup_keycodes);
Event.observe(window, 'load', setup_cart);



