// Ajax.js
// Michael Beck
// Nov 29th 2005

// to use: simply include this file and pass GetContent(url, divId) 
//         where div id is the div that the content goes in.
//         and url is the url of the content of the div
// note:
// it creates a new XMLHttpRequest for each divId it gets and stores it in an array

// to get form response: GetFormResponse(formID, PostURL, divId)
//         where divId is the div that the response should go in
//         and PostURL is the URL that the form posts to
//         form must be named as <form name="forms[formID]"> format

var http_request = new Array();

function GetContent(url, divId) {
   http_request[divId] = false;
   if (window.XMLHttpRequest) { // Mozilla, Safari,...
      http_request[divId] = new XMLHttpRequest();
      if (http_request[divId].overrideMimeType) {
         http_request[divId].overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
      try {
         http_request[divId] = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try {
            http_request[divId] = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
      }
   }
   if (!http_request[divId]) {
       alert("Giving up :( Cannot create an XMLHTTP instance");
       return false;
   }

   http_request[divId].onreadystatechange = function() {
      if (http_request[divId].readyState == 4) {
         if (http_request[divId].status == 200) {
            // if call is finished then write content
            divContent = http_request[divId].responseText;
            document.getElementById(divId).innerHTML = divContent;
          }
      }
   }

   http_request[divId].open('GET', url, true);
   http_request[divId].send(null);
}


function GetFormResponse(formName, PostURL, divId) {
   var the_form = document.forms[formName];
   var GetString = PostURL+"?";
   for (var loop=0; loop < the_form.elements.length; loop++)  {
      GetString += the_form.elements[loop].name +"=" + the_form.elements[loop].value + "&";
   }
   GetContent(GetString, divId);
}

