function showpopup(dest, post){
  if(typeof XMLHttpRequest == "undefined") return;
  if(typeof xhreq == "undefined"){
    xhreq = new XMLHttpRequest();
  };
  if(!xhreq) return false;

  //Prepare the help container
  hidepopup(); //Just in case we missed a close event somewhere
  var h = document.createElement('div');
  h.id = 'floatingpopup';
  h.style.position = 'absolute';
  h.style.backgroundColor = '#fffff0';
  h.style.left = '50px';
  h.style.right = '50px';
  h.style.top = '50px';
  h.style.bottom = '50px';
  h.style.overflow = 'scroll';
  h.style.border = '2px #831B1A solid';
  h.style.zIndex = 9;
  h.style.padding = '3px';
  i = document.createElement('img');
  i.src = loadingicon.src;
  i.style.display = 'block';
  i.style.position = 'absolute';
  i.style.top = '50%';
  i.style.left = '50%';
  h.appendChild(i);
  document.getElementsByTagName('body')[0].appendChild(h);
  
  if(typeof(post) == "undefined"){
    xhreq.open("GET", dest, true);
    post = null;
  }else{
    xhreq.open("POST", dest, true);
    xhreq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    postdata = '';
    if(typeof post != "string"){
      post = recursivepostvars(post, 'test=test');
    };
  };

  xhreq.setRequestHeader("Accept", "application/aja-reply");
  xhreq.onreadystatechange=function() {
    if(xhreq.readyState==4){
      if(typeof h == "undefined") h = document.getElementById('floatingpopup');
      if(h){
        h.innerHTML = xhreq.responseText;
      };
    }
  }
  xhreq.send(post);
  return true;
};
function hidepopup(){
  var h = document.getElementById('floatingpopup');
  if(h){
    h.parentNode.removeChild(h);
  };
};

function recursivepostvars(parentele, vars){
    for(var i=0;i<parentele.childNodes.length;i++){
      var ele = parentele.childNodes[i];
      if(typeof ele.tagName == "undefined") continue;
      switch(ele.tagName.toLowerCase()){
        case "input":
          switch(ele.type.toLowerCase()){
            case "checkbox":
              if(!ele.checked) vars = vars + "&" + escape(ele.name) + "=!" . escape(ele.value);
              else vars = vars + "&" + escape(ele.name) + "=" . escape(ele.value);
              break;
            case "submit":
              if(ele.name && ele.name.length > 0) vars = vars + "&" + escape(ele.name) + "=" . escape(ele.value);
              break;
            case "reset":
              break;
            default:
              vars = vars + "&" + escape(ele.name) + "=" + escape(ele.value)
          };
          break;
        case "textarea":
          vars = vars + "&" + escape(ele.name) + "=" + escape(ele.value)
          break;
        case "select":
          vars = vars + "&" + escape(ele.name) + "=" + escape(ele.options[ele.selectedIndex].value);
          break;
        case "div":
          vars = vars + recursivepostvars(ele, '');
          break;
      };
    };
    return vars;
};


loadingicon = new Image(); loadingicon.src = './images/loading.gif';

