//http://www.hunlock.com/blogs/The_Ultimate_Ajax_Object
function ajaxObject(url, callbackFunction, loadingFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }

  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
	    if (that.AJAX.readyState==1) {
		  that.loading();
		}
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        } 
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
  this.loading = loadingFunction || function () { };
}

/*
var myRequest = new ajaxObject('http://www.somedomain.com/process.php', processData);

function processData(responseText, responseStatus) {
  if (responseStatus==200) {
    alert(responseText);
  } else {
    alert(responseStatus + ' -- Error Processing Request);
  }
}

var myRequest = new ajaxObject('http://www.somedomain.com/process.php');
    myRequest.callback = function(responseText, responseStatus, responseXML) {
      // do stuff here.
    }

Initiating the AJAX request
var myRequest = new ajaxObject('http://www.somedomain.com/process.php', processData);
    myRequest.update('id=1234&color=blue');  // Server is contacted here.


Sending data as a POST in AJAX
var myRequest = new ajaxObject('http://www.somedomain.com/process.php', processData);
    myRequest.update('id=1234&color=blue','POST');


Escape The Data
passName=encodeURIComponent(name);
passAge=encodeURIComponent(age);
passJob=encodeURIComponent(job);
sendString = 'name='+passName+'&age='+passAge+'&job='+passJob;
myRequest.update(sendString,'POST');


updating
if (myRequest.updating) {
  alert('this request is being processed.');
} else {
  alert('this object is idle');
}

if (myRequest.updating) {
  now=new Date();
  alert('This request is '+now-myRequest.updating+' miliseconds old.');
}


Aborting an Ajax Request
myRequest.abort();


Cool Tricks And Tips
myRequest = new ajaxObject('http://somedomain.com/ad.html');
myRequest.callback = function(responseText) {
  document.getElementById('someAdDiv').innerHTML=responseText;
}
myRequest.update();


myRequest = new ajaxObject('http://somedomain.com/javascriptLibrary.js');
myRequest.callback = function(responseText) {
  eval(responseText);
}
myRequest.update();


Multiple Ajax Calls, Ajax Concurrency, and Dynamic Flexibility
function processFeed(responseText) {
  if (responseStatus==200) {
    document.getElementById('feed').innerHTML=responseText;
  }
}

var diggFeed=new ajaxObject('feedHandler.php',processFeed);
var redditFeed=new ajaxObject('feedHandler.php',processFeed);
var dzoneFeed=new ajaxObject('feedHandler.php',processFeed);

<A HREF="" onClick='diggFeed.update("feed=http://www.digg.com/rss/index.xml"); return false;'>Get Digg Feed</A>
<A HREF="" onClick='redditFeed.update("feed=http://reddit.com/.rss"); return false;'>Get Reddit Feed</A>
<A HREF="" onClick='dzoneFeed.update("feed=http://feeds.dzone.com/dzone/frontpage"); return false;'>Get DZone Feed</A>


Changing the calling AJAX URL
var myRequest = new ajaxObject('serverScript.php',callbackFunc);
    myRequest.update();

// Now we're going to change the URL

    myRequest = new ajaxObject('newScript.php', newcallback);
    myRequest.update();


An Array of AJAX Objects
ajaxArray=[];
for (var i=0; i<10; i++) {
  ajaxArray[i] = new ajaxObject('someScript.php',callbackFunc);
}
// Call them all at once!
for (i=0; i<10; i++) {
  ajaxArray[i].update('id='+i;);
}


Addendum
var myRequest = new ajaxObject('http://localhost/AALetters/MergeData.asmx/PutDocument', Curry(OnSaveDoc, CallBack));

myRequest.update(postData, 'Post');

function OnSaveDoc(){
            alert('Executing OnSaveDoc()...');
        }

function CallBack(customHandler, responseText, status, responseXML){
    // Do standard processing then pass execution to dedicated function
    // to handle specific tasks
    if(200 != status){
        alert("Problem");
        return;
    }  
    customHandler();
}

function Curry (fn, scope) {
    var scope = scope || window;
    var args = Array.prototype.slice.call(arguments,2);
    return function() {
        fn.apply(scope, args);
    };
}

*/
