/* File: ajax.js
 * Author: Jason Charney (bushidohacks@gmail.com)
 * Created: April 22, 2006
 * Description: Ajax (Asynchronous JavaScript and XML) uses JavaScript
 *	and XML (More or less, HTML or PHP) to create web applications
 *	that behave more like desktop applications.
 *	Some of the codes in this file were borrowed from
 *	"AJAX for Dummies" by Steve Holzner, PhD.
 *	Pick up a copy of "AJAX for Dummies" at your local bookstore.
 */
/* ATTENTION MSIE USERS
 * As of April 11, 2006, MSIE can no longer use ActiveX due to a patent
 * infringment lawsuit the Eolas Corporation won against Microsoft.
 * Microsoft then released a patch that disabled ActiveX in their
 * Microsoft products, including Internet Explorer.  Users who recently
 * installed the "Eolas Patch" may have noticed that MSIE behaves strangely
 * when they access webpages that use AJAX, Java, Flash, or Quicktime.
 * If you are having difficulty accessing webpages that have enhanced
 * content, you should browse the Microsoft website for a work-around patch.
 * Alternatively, You can install Firefox as your broswer and sod off
 * MSIE altogether.  The alternative would certainly be better.
 */
/*
 * Variables
 * divID - a name of a dive section that data will be transfered
 * dataSrc - The URL data will be transfered from
 * dataDest - The URL data will be transfered to
 * callback - a called function that has two variables
 */
/* Methods
 * getData  - a method that reads data from a file
 * postData - a method that writes data to a file
 * disp - a frequently used callback function that inserts code in a div section
 */

function disp(divID,dataSrc){document.getElementById(divID).innerHTML = dataSrc;};

function getData(divID,dataSrc,callback){
 var XMLHttpRequestObject = false;
 if(window.XMLHttpRequest){	XMLHttpRequestObject = new XMLHttpRequest();}	// Firefox
 else if(window.ActiveXObject){	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");}	//MSIE
 if(XMLHttpRequestObject){
  XMLHttpRequestObject.open("GET",dataSrc);
  XMLHttpRequestObject.onreadystatechange = function()
  {
   if(XMLHttpRequestObject.readyState == 4){
    if(XMLHttpRequestObject.status == 200){
     callback(divID,XMLHttpRequestObject.responseText);
    }
    else if(XMLHttpRequestObject.status == 404){
     disp(div,"ERROR! 404 - Could not find " + dataSrc + ".");
    }
    delete XMLHttpRequestObject;
    XMLHttpRequestObject = null;
   }
  }
  XMLHttpRequestObject.send(null);
 }
}

function postData(divID,dataDest,data,callback){
 var XMLHttpRequestObject = false;
 if(window.XMLHttpRequest){	XMLHttpRequestObject = new XMLHttpRequest();}	// Firefox
 else if(window.ActiveXObject){	XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");}	//MSIE
 if(XMLHttpRequestObject){
  XMLHttpRequestObject.open("POST",dataDest);
  XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  XMLHttpRequestObject.onreadystatechange = function()
  {
   if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
    callback(divID,XMLHttpRequestObject.responseText);
    delete XMLHttpRequestObject;
    XMLHttpRequestObject = null;
   }
  }
  XMLHttpRequestObject.send(data);
 }
}

