
/* last edit: 2011-06-21 */

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 = '';

  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);
  $.post(url, content, function(data) { update_Cart(data); }); // ajax call
}

function update_Cart(resp) {
  var resp;

  resp = eval('(' + resp + ')');
  emacs.show_cart(resp);
  updateInProgress = false;
}

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 (resp) {
  var el;

  resp.total = parseInt(resp.total) / 100;
//  document.getElementById("shopping_cart").innerHTML = 'Checkout/View Cart: $' + resp.total.toFixed(2);
  el = document.getElementById("shopping_cart");
  if (el) {
    el.innerHTML = 'Checkout/View Cart: $' + resp.total.toFixed(2);
  } else if (opener && opener.emacs) {
    opener.emacs.show_cart(resp.part,resp.op,resp.items,Math.round(resp.total * 100));
  }
  if ($('#cart_items')) {
    if (resp.items === '') resp.items = '0';
    $('#cart_items').html(resp.items);
  }
  if ($('#cart_total')) {
    if (resp.total === '' || resp.total === 0) resp.total = 0;
    $('#cart_total').html(resp.total).formatCurrency({ symbol:'' });
  }
  return false;
}

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

  if (updateInProgress) return false;
  if (el === '') {
    info.partNo = '';
    info.price = '';
    info.qty = '';
    info.op = '';
  } else {
    updateInProgress = true;
    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 ) {
      minQty = parseInt(minQty);
      if (isNaN(minQty)) minQty = 1;
      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 === 0) {
      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 listRequest;
var updateInProgress = false;

var emacs = new My_emacs();

listRequest = CreateRequest();

$(document).ready(function() {
  emacs.updateCart('');
} );

