/********************************************************************************************/
/* AHAH functions by Phil Ballard                                                           */
/* This code is intended for study purposes.                                                */
/* You may use these functions as you wish, for commercial or non-commercial applications,  */
/* but please note that the author offers no guarantees to their usefulness, suitability or */
/* correctness, and accepts no liability for any losses caused by their use.                */
/********************************************************************************************/



function callAHAH(url, pageElement, callMessage, errorMessage) {

     document.getElementById(pageElement).innerHTML = callMessage;
     try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req = false;
         } 
       } 
     }
     req.onreadystatechange = function() {responseAHAH(pageElement, errorMessage);};
     req.open("GET",url,true);
     req.send(null);
  }

function responseAHAH(pageElement, errorMessage) {
   var output = '';
   if(req.readyState == 4) {
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(pageElement).innerHTML = output;
         } else {
         document.getElementById(pageElement).innerHTML = errorMessage+"\n"+output;
         }
      }
  }


/*
  http://www.openjs.com/articles/ajax/ahah_asynchronous_html_over_http/
  Anonymous at 28 May, 2009 09:56
  I've modified the ahah function to allow the POST method via a standard
  HTML form, and thought I'd share, to maybe save others some of the
   head-banging-on-the-wall I went through...
   
   The idea here is to design your <form> as you would any other, with a
    slight tweak to the action of your form:


  <form method="post" id="yourform" 
  action="javascript:ahahPost('your-url.php', 'targetdiv', 'yourform');">


  Now add form elements as normal and design it as you wish, and add a 
  regular old submit button as usual. The function iterates through your form,
  gathers up it's various elements' values, forms the appropriate http request 
  and fires it off, with the results coming back to your targetdiv like the 
  normal ahah function.

  I've tested this with the standard form elements - text, textarea,
  checkboxes (you'll see they needed a little workaround else they all appear checked),
  radio buttons, etc. I'm hoping this works for submission of files but have not
  tested that yet... at first glance I assume it might but there might be something
  missing yet to support file uploads.
 */

function ahahPost(url,target,formid) {

  var params = '';
  var elem = document.getElementById(formid).elements;
  
  for(var i = 0; i < elem.length; i++) {
    if (elem[i].type == "checkbox") {
      if (elem[i].checked) {
        params += elem[i].name + '=' + elem[i].value + '&';
      }
    }
    else {
      params += elem[i].name + '=' + elem[i].value + '&';
    }
  }
  
  params = params.slice(0,params.length-1);
  
  //document.getElementById(target).innerHTML = 'loading data...';
  document.getElementById(target).innerHTML = '<img src=/ahah/spinny.gif border=0>';
  
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    //req.onreadystatechange = function() {ahahDone(target);};
    req.onreadystatechange = function() {responseAHAH(target, "Error");};
    req.open("POST", url, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", params.length);
    req.setRequestHeader("Connection", "close");
    req.send(params);
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req) {
      //req.onreadystatechange = function() {ahahDone(target);};
      req.onreadystatechange = function() {responseAHAH(target, "Error");};
      req.open("POST", url, true);
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      req.setRequestHeader("Content-length", params.length);
      req.setRequestHeader("Connection", "close");
      req.send(params);
    }
  }
} 