

function ajax_obj() {
  var req;
  if(typeof XMLHttpRequest != "undefined") {
    req = new XMLHttpRequest();
  } else {
  try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch(e) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(oc) {
      req = null;
    }
  }
  }
  return req;
}

function add_product(id,colour,size) {
  request = ajax_obj();
  if(request != null) {
    request.onreadystatechange = process;
    request.open("GET", buy_url+'?j=1&id='+id+'&colour='+colour+'&size='+size, true);
    request.send(null);
    return 1;
  }
  return 0;
}

function setText(id,value) {
  var el = document.getElementById(id);
  if(el.textContent) { el.textContent = value; return; }
  if(el.innerText)   { el.innerText = value; return; }
  if(el.innerHTML)   { el.innerHTML = value; return; }
  return false;
}

function process() {
  if(request.readyState == 4) {
    if (request.status == 200) {
      res = request.responseText.split("\n");
      setText('subtotal_1',res[0]);
//      setText('subtotal_2',res[0]);
      setText('num_items_1',res[1]);
//      setText('num_items_2',res[1]);
    }
  }
}


function getTarget(e) {
        if (!e) var e = window.event;
        if (e.target) return e.target;
        else if (e.srcElement) targ = e.srcElement;
        if (targ.nodeType != 3) { return targ; }
  return targ.parentNode;
}

function flash(id,counter) {
  var el = document.getElementById(id);
  if(counter == 1) { el.className = ''; return; }
  if(el.className == 'flash') {
    el.className = '';
  } else {
    el.className = 'flash';
  }
  setTimeout("flash('"+id+"',"+(counter-1)+")",flash_interval);
}

function purchaseme(e) {
  if (!e) var e = window.event;
  if(!e || !document.getElementById) return true;
  e.returnValue=false;
  var el = getTarget(e);
  var oid =el.getAttribute('id');
  var id = oid;
  var form = el.form;
  var colour = ''; var size = '';
  var label = labels[id];
  var sub = '';
  if(form['sub']) { sub = form['sub'].value; }
  id += '-'+sub;
  if(form['colour']) {
    colour = form['colour'].options[form['colour'].selectedIndex].value;
    label += ', '+colour;
  }
  id += '-'+colour;
  if(form['size']) {
    size = form['size'].options[form['size'].selectedIndex].value;
    label += ', '+size;
  }
  id += '-'+size;
  if(basket[id]>=max_quantity) { 
    cursor_message(e,'maximum quantity allowed: 20','');
    flash('basket_'+id,flash_count);
    return false; 
  }
  if(!add_product(oid,colour,size)) {
    // don't support ajax, so do it the old-fashioned way...
    return true;
  }
  cursor_message(e,'added 1x '+label+' to basket','');
  add_line(id,label);
  return false;
}

function cursor_message(e,msg,sc) {
  if(e.clientX || e.clientY) {
    posx = e.clientX + document.documentElement.scrollLeft;
    posy = e.clientY + document.documentElement.scrollTop;
  } else if (e.pageX || e.pageY) {
                posx = e.pageX;
                posy = e.pageY;
        } 
        var el = document.createElement('div');
        el.className = 'popup'+sc;
        el.style.position = 'absolute';
        el.style.width = '200px';
        el.style.top = (posy)+'px';
        el.style.left = (posx+20)+'px';
        var id = 'popup_block_'+pb_counter;
        el.setAttribute('id',id);
        pb_counter++;
        el.appendChild(document.createTextNode(msg));
        document.body.appendChild(el);
        setTimeout("hide('"+id+"')",2000);
}

function hide(id) {
  var el = document.getElementById(id);
  el.parentNode.removeChild(el);
}


function add_line(id,label) {
  var el = document.getElementById('basket_'+id);
  if(el) {
    var num = basket[id];
    num+=1;
    basket[id]++;
    el.firstChild.firstChild.data = num;
    return;
  }
  var e = document.createElement('li');
  var sp = document.createElement('span');
  sp.appendChild(document.createTextNode('1'));
  e.appendChild(sp);
  e.appendChild(document.createTextNode(' x '+label));
  e.setAttribute('id','basket_'+id);
  document.getElementById('basket_contents').appendChild(e);
  basket[id] = 1;
  var body = document.getElementsByTagName('body');
  body[0].className = 'full_basket';
}

