var url = null;
var request = null;
var div = null;
var pos = [];

function registerPosition(event) {
  event = event || window.event;
  pos[0] = pointerX(event) - 250;
  pos[1] = pointerY(event) - 140;
}


function ajaxAdd(clp, type, id) {
  // clp is no longer needed, but still accepted for backwards compatability
  var location = document.location.href.split('/').slice(3,7);

  // sometimes the path to the store directory is longer--search to see 
  // if "store" is lower down...
  if (!(location[3].match("store"))) {
    var end=7;
    while (end<20) {
      end++;
      location = document.location.href.split('/').slice(3,end);
      if (location[end-4].match("store")) {
        break;
      }
    }
  }

  // The convention_order site is further down the URL path
  if (document.location.href.match("convention_order")) {
    var location = document.location.href.split('/').slice(3,9);
  }

  if (location[3].indexOf('.html') != -1) {
    location[3] = (location[2] == 'retail') ? 'store' : 'store2';
  }
  location.push('ajax_add.html?' + type + '=' + id);
  url  = '/' + location.join('/');

  try {
    loadChoices();
  }
  catch (e) { codePopup(); }
}

function loadChoices() {
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
        request.onreadystatechange = displayCodes;
        request.open("GET", url, true);
        request.send(null);
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
        if (request) {
            request.onreadystatechange = displayCodes;
            request.open("GET", url, true);
            request.send();
        }
        else {
          codePopup();
        }
    }
}

function pointerX (event) {
  return event.pageX || (event.clientX +
    (document.documentElement.scrollLeft || document.body.scrollLeft));
}

function pointerY (event) {
  return event.pageY || (event.clientY +
    (document.documentElement.scrollTop || document.body.scrollTop));
}

function displayCodes () {
    if (request.readyState == 4) {
        if (request.status == 200) {
          try {
            if (request.responseText.indexOf('<script') != -1) {
              var script = request.responseText.replace(/[\n\r]/g, ' ').replace(/<\/?script[^>]*>/g, '');
              eval(script);
            }
            else {
	            if (! div) {
	              div = document.createElement('DIV');
		            document.body.appendChild(div);
	            }
	            div.innerHTML = request.responseText;
              div.style.display = 'block';
	            div.style.position = 'absolute';
	            div.style.zIndex = '20';
	            div.style.padding = '4px';
	            div.style.backgroundColor = '#efefef';
	            div.style.border = '1px solid #cccccc';
	            div.style.left = pos[0] + 'px';
	            div.style.top = pos[1] + 'px';
            }
          } catch(e) { codePopup(); }
        } else {
          codePopup();
        }
    }
}

function codePopup() {
  window.open(url + '&popup=1', 'add_popup', 'height=300,menubar=no,status=no,toolbar=no,width=400');
}

