function ajax(url, div, addTo) {
  /* self-contained AJAX module
    Martin Latter Sep '07, revised Feb '08; */
  var debug = true; // * MAKE THIS FALSE ON LIVE SITE *
  url = encodeURI(url); // escape any bad characters in url string

  /* add extra function arguments and processing above this message */
  /*************************************************************************/ 
  var xmlHttp = createXHR();
  xmlHttp.open("GET", url, true);
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
      if (xmlHttp.status == 200 || xmlHttp.status == 304) {
        if(addTo) { document.getElementById(div).innerHTML += xmlHttp.responseText; }
        else { document.getElementById(div).innerHTML = xmlHttp.responseText; }
      }
      else {
        if (debug) {
          alert("An error occurred: " + xmlHttp.statusText);
        }
      }
    }
  };
  xmlHttp.send(null);
}

/*************************************************************************/
function createXHR() {
    /* sub function to create cross browser AJAX object  - amalgamation of different techniques: Wrox and O'Reilly
      tested in IE6 (6.0.2900.2180), IE7, Safari (Mac), Opera 9, Firefox Mac/Win */
  if (typeof XMLHttpRequest != "undefined") {
    return new XMLHttpRequest();
  }
  else if (window.ActiveXObject) {
    try {
      var xmlHttp = new ActiveXObject("MSXML2.XMLHttp");
        // O'Reilly: specific version of MSXML not necessary, saves Wrox IE6 trouble
      return xmlHttp;
    }
    catch (xhrError) {
      alert("AJAX functionality is not supported (or trusted ActiveX has been disabled) in this version of Internet Explorer.");
    }
  }
}
/*************************************************************************/

function ajaxPost(url,div,form) {
  values = DisplayFormValues(form);
//alert('Values' + values);  
  var debug = true; // * MAKE THIS FALSE ON LIVE SITE *
  url = encodeURI(url); // escape any bad characters in url string

  var xmlHttp = createXHR();
  xmlHttp.open("POST", url, true);
  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
      if (xmlHttp.status == 200 || xmlHttp.status == 304) {
        document.getElementById(div).innerHTML = xmlHttp.responseText;
      }
      else {
        if (debug) {
          alert("An error occurred: " + xmlHttp.statusText);
        }
      }
    }
  };
  xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  xmlHttp.setRequestHeader("Content-length", values.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(values);



  /**
   * Encode the property name/value pairs of an object as if they were from
   * an HTML form, using application/x-www-form-urlencoded format
   */
/*
  HTTP.encodeFormData = function(data) {
    var pairs = [];
    var regexp = /%20/g; // A regular expression to match an encoded space

    for(var name in data)
    {
      var value = data[name].toString();
        // Create a name/value pair, but encode name and value first
        // The global function encodeURIComponent does almost what we want,
        // but it encodes spaces as %20 instead of as "+". We have to
        // fix that with String.replace()
      var pair = encodeURIComponent(name).replace(regexp,"+") + '=' + encodeURIComponent(value).replace(regexp,"+");
      pairs.push(pair);
    }

    return pairs.join('&');
      // Concatenate all the name/value pairs, separating them with &
  };
*/
}


function DisplayFormValues(form) {
  var regexp = /%20/g; // A regular expression to match an encoded space
  var str = '';
  var testing = 'Testing:';
  var elem = document.getElementById(form).elements;
  for(var i = 0; i < elem.length; i++) {
    var k = elem[i].name;
    if(k) {    
      var t = elem[i].type;
      var v = elem[i].value;
      testing += "\n" + t + " " + k + " : " + v;
      if(t == 'checkbox') { // SPECIAL CHECK FOR CHECKBOX ODDITIES - MAY NEED ONE FOR RADIO BUTTONS TOO
        var check = elem[i].checked;
        testing += check;
        if(check) { v = v; }
        else { v = '0'; }
      }
      str += "&" + encodeURIComponent(k).replace(regexp,"+") + "=" + encodeURIComponent(v).replace(regexp,"+"); 
   }
  }
//alert(testing);  
  return str.substr(1) ;
}

