var Ajax = function() {
this.reset();
this.instanceHandle = Ajax.nextHandle;
Ajax.nextHandle++;
Ajax.instances[this.instanceHandle] = this; }
Ajax.nextHandle = 1;
Ajax.instances = {};
Ajax.releaseAll = function() {
var obj;
for(obj in Ajax.instances)
Ajax.instances[obj].release(); }
Ajax.prototype.reset = function() {
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
} catch(e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xmlhttp = null; } } }
this.xmlhttp = xmlhttp; }
Ajax.prototype.release = function() {
Ajax.instances[this.instanceHandle].abort();
delete Ajax.instances[this.instanceHandle]; }
Ajax.prototype.isAvailable = function() {
return this.xmlhttp !== null ? true : false; }
Ajax.prototype.setOnCompleteCallback = function(context, mtd) {
var obj = this, dispatch = this.dispatcher;
this.xmlhttp.onreadystatechange = function() {
return dispatch.call(obj, context, mtd); } }
Ajax.prototype.dispatcher = function(context, mtd) {
if(this.xmlhttp.readyState != 4)
return;
try {
if(this.xmlhttp.status != 200)
return;
} catch(e) {
return; }
mtd.call(context, this); }
Ajax.prototype.requestGet = function(url, getargs, async) {
url += this.formatArgs(args, true);
if(async == undefined || async !== true)
var async = false;
this.xmlhttp.open("GET", url, async);
this.xmlhttp.send(null);
if(async)
return true;
if(this.xmlhttp.status != 200)
return false;
return true; }
Ajax.prototype.requestPost = function(url, getargs, postargs, async) {
var post = "";
url += this.formatArgs(getargs, true);
post = this.formatArgs(postargs, false);
if(async == undefined || async !== true)
var async = false;
this.xmlhttp.open("POST", url, async);
this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
this.xmlhttp.setRequestHeader("Content-Length", post.length);
this.xmlhttp.setRequestHeader("Connection", "close");
this.xmlhttp.send(post);
if(async)
return true;
if(this.xmlhttp.status != 200)
return false;
return true; }
Ajax.prototype.abort = function() {
this.xmlhttp.abort(); }
Ajax.prototype.getResponseText = function() {
if(this.xmlhttp.readyState != 4)
return null;
return this.xmlhttp.responseText; }
Ajax.prototype.getResponseXML = function() {
if(this.xmlhttp.readyState != 4)
return null;
return this.xmlhttp.responseXML; }
Ajax.prototype.formatArgs = function(args, getmode) {
var i = 0, ret = "", val;
if(args == undefined || args == null)
return ret;
for(key in args) {
if(i++) ret += "&";
if(args[key] === undefined || args[key] === null) val = "";
else if(typeof args[key] == "boolean") val = (args[key] === true ? "true" : "false");
else if(typeof args[key] == "object") {
ret += this.objectToRequest(key, args[key]);
continue; }
else val = encodeURIComponent(args[key]);
ret += encodeURIComponent(key) + "=" + val; }
if((getmode != undefined && getmode == true) && ret.length > 0)
ret = "?" + ret;
return ret; }
Ajax.prototype.objectToRequest = function(name, obj) {
var str = '', i = 0;
if(obj == null)
return str;
for(var k in obj) {
if(i++) str += '&';
str += name + '[' + encodeURIComponent(k) + ']=' + encodeURIComponent(obj[k]); }
return str; }
