function getHttpLoc(target) {
  var a, httploc, n;

  // return URL of current location - trim trailing '#'
  httploc = new String(document.location);
  if (httploc.charAt(httploc.length-1) === "#") {
    httploc = httploc.substring(0, httploc.length-1);
  }
  if (httploc.charAt(httploc.length-1) === "/") {
    httploc = httploc.substring(0, httploc.length-1);
  }
  a = httploc.split('/');
  for (n = 7; n < a.length; n++) {
    a[n] = '';
  }
  if (a[5] !== target) {
    a[5] = target;
  }
  httploc = a.join('/');
  for (n = httploc.length; n > 1 && httploc.charAt(n-1) === '/'; n--) ;
  if (n) {
    httploc = httploc.substring(0,n);
  }
  return httploc;
}

function CreateRequest() {
  var request = null;

  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }
  if (request === null)
    alert("Error creating request object!");
  return request;
}

function updateCart(cartInfo) {
  var a, httploc, url, wid;
  var content = '';

  if (cartRequest.readyState !== 0) return;
  httploc = getHttpLoc('emacs.ajax');
  a = httploc.split('/');
//a[6] is the session ID and it's expected to be there!
  wid = document.getElementById('wid').value;
  if (wid) {
    a[6] = wid;
  }
  a[7]='shopping_cart';
  a[8]='';
  url = a.join('/');
  content='partNo='+encodeURIComponent(cartInfo.partNo);
  content += '&price='+encodeURIComponent(cartInfo.price);
  content += '&qty='+encodeURIComponent(cartInfo.qty);
  content += '&op='+encodeURIComponent(cartInfo.op);
  cartRequest.open("POST", url, true);
  cartRequest.onreadystatechange = update_Cart;
  cartRequest.send(content);
}

function update_Cart() {
  var resp;

  if (cartRequest.readyState === 4) {
    if (cartRequest.status === 200) {
      resp = cartRequest.responseText.split(',');
      emacs.show_cart(resp[0],resp[1],resp[2],resp[3]);
      cartRequest = CreateRequest();
    }
  }
}

function update_List() {
  var resp;

  if (listRequest.readyState === 4) {
    if (listRequest.status === 200) {
      resp = listRequest.responseText.split(',');
      // don't really need to do anything... just get ready for the next request.
      listRequest = CreateRequest();
    }
  }
}

function changeLocation (url) {
  window.location = url;
}

function My_emacs() {
}

My_emacs.prototype.show_cart = function (part,op,items,total) {
  var el;

  total = parseInt(total) / 100;
//  document.getElementById("shopping_cart").innerHTML = 'Checkout/View Cart: $' + total.toFixed(2);
  el = document.getElementById("shopping_cart");
  if (el) {
    el.innerHTML = 'Checkout/View Cart: $' + total.toFixed(2);
  } else {
    opener.emacs.show_cart(part,op,items,total * 100);
  }
  return false;
}

My_emacs.prototype.updateCart = function (el) {
  var info = new Object;
  var partNo;
  var el2, qtyEl, minQty;

  partNo = el.name.substring(3,99);
  info.partNo = partNo;
  info.price = document.getElementById('pr_' + partNo).innerHTML;
  qtyEl = document.getElementsByName('p_' + partNo)[0];
  info.qty = qtyEl.value;
  minQty = qtyEl.getAttribute('min_qty');
  if (minQty !== null ) {
    if (info.qty === '') {
      info.qty = minQty;
    }
    if(info.qty < (minQty * 1)) {
      alert("This item requires a minimum purchase of " + minQty + ".");
      qtyEl.value = '';
      return false;
    }
  }
  if (info.qty === '') {
    info.qty = 1;
  }
  qtyEl.value = info.qty;
  if (info.price.charAt(0) === '$') {
    info.price = info.price.substring(1,999);
  }
  info.price = Math.round(info.price * 100);
  if (!info.price) {
    info.price = 0;
  }
  if (el.title === 'add to cart') {
    el.title = 'remove from cart';
    el.src = 'http://www.4netresults.com/eco_images/deletecartIcon.gif';
    info.op = 'add';
  } else {
    el.title = 'add to cart';
    el.src = 'http://www.4netresults.com/eco_images/addcartIcon2.gif';
    info.op = 'del';
  }
  if (info.op === 'del') {
    qtyEl.value='';
    qtyEl.disabled = false;
  } else {
    qtyEl.disabled = true;
  }
  if (document.getElementById("detail")) {
    el2 = opener.document.getElementsByName(el.name)[0];
    if (el2) {
      el2.title = el.title;
      el2.src = el.src;
      qtyEl = opener.document.getElementsByName('p_' + partNo)[0];
      if (info.op === 'del') {
        qtyEl.value='';
        qtyEl.disabled = false;
      } else {
        qtyEl.value = info.qty;
        qtyEl.disabled = true;
      }
    }
  }
  updateCart(info);
  return false;
}

My_emacs.prototype.selectItem = function (el) {
  var op, partnbr;
  var a, httploc, url;
  var content = '';

  partnbr = el.name.substr(3,999);
  op = (el.checked) ? 'add' : 'del';
  if (listRequest.readyState !== 0) return;
  httploc = getHttpLoc('emacs.ajax');
  a = httploc.split('/');
//a[6] is the session ID and it's expected to be there!
  a[7]='select_list';
  a[8]='';
  url = a.join('/');
  content='partno='+encodeURIComponent(partnbr);
  content += '&op='+encodeURIComponent(op);
  listRequest.open("POST", url, true);
  listRequest.onreadystatechange = update_List;
  listRequest.send(content);
  return false;
}

var cartRequest;
var listRequest;

var emacs = new My_emacs();

cartRequest = CreateRequest();
listRequest = CreateRequest();
