var AJAX_ConnectionObjects = [];
var AJAX_TransportStates = [
"Uninitialized",
"Loading",
"Loaded",
"Interactive",
"Complete"
];
function AJAXConnection()
{
this.conn = null;
this.data = null;
this.xml = null;
this.contentType = null;
this.error = null;
this._initialize = AJAX_InitializeConnectionObject;
this._method = null;
this._processedData = null;
this._throwError = AJAX_Error;
this._clearIntv = AJAX_ClearIntervals;
this._rstate = 0;
this._rstateIntv = null;
this._rstateonchange = AJAX_ReadyStateChange;
this._async = null;
this._indice = null;
this._loading = false;
this._timeoutIntv = null;
this._timeoutCount = 0;
this.timeout = 30;
this.isLoading = AJAX_isLoading;
this.checkConnection = AJAX_CheckConnection;
this.onTimeout = null;
this._loadingIntv = null;
this._doWhileLoading = AJAX_WhileLoading;
this.whileLoading = null;
this.whileLoadingMS = 100;
this.onReadyStateChange = null;
this.onError = null;
this.onSuccess = null;
this.onBadStatusCode = null;
this.open = AJAX_OpenConnection;
this.abort = AJAX_Abort;
this.send = AJAX_Send;
this.sendForm = AJAX_SendForm;
this.sendObject = AJAX_SendObject;
this.transportState = AJAX_GetTransportState;
this.destroy = AJAX_Destroy;
this.processData = AJAX_ProcessData;
this._initialize();
}
function AJAX_InitializeConnectionObject()
{
if (window.ActiveXObject) {
try {
this.conn = new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch(e) {
try {
this.conn = new ActiveXObject("MSXML2.XMLHTTP");
}
catch(e) {
try {
this.conn = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) { }
}
}
}
else if (window.XMLHttpRequest) {
this.conn = new XMLHttpRequest();
}
if (!this.conn) {
this._throwError("Could Not Initialize Connection Object");
}
else {
this._indice = AJAX_ConnectionObjects.length;
AJAX_ConnectionObjects[this._indice] = this;
}
}
function AJAX_OpenConnection(method, url, async, uid, pass)
{
if (!this.conn) return;
if (async == undefined || async == null) async = false;
this._async = async;
this._method = method.toLowerCase();
if (this._processedData) {
if (url.indexOf("?") > -1)
url += "&"+ this._processedData;
else
url += "?"+ this._processedData;
}
try {
if (uid)
this.conn.open(method, url, async, uid, pass);
else
this.conn.open(method, url, async);
this._open = true;
if (this.contentType)
this.conn.setRequestHeader("Content-Type", this.contentType);
else if (method.toLowerCase() == "post") {
this.conn.setRequestHeader(
"Content-Type",
"application/x-www-form-urlencoded; charset=UTF-8"
);
}
}
catch(e) {
this._throwError(e);
return(false);
}
return(true);
}
function AJAX_Send(data)
{
if (!this._open) return;
this._rstate = 0;
this._loading = true;
if (this._async) {
if (this._rstateIntv)
clearInterval(this._rstateIntv);
this._rstateIntv = setInterval("AJAX_ConnectionObjects["+ this._indice +"]._rstateonchange()", 1);
if (typeof this.whileLoading == "function") {
if (this._loadingIntv)
clearInterval(this._lodingIntv);
this._loadingIntv = setInterval("AJAX_ConnectionObjects["+ this._indice +"]._doWhileLoading()", this.whileLoadingMS);
}
if (this.timeout) {
if (this._timeoutIntv)
clearInterval(this._timeoutIntv);
this._timeoutCount = 0;
this._timeoutIntv = setInterval("AJAX_ConnectionObjects["+ this._indice +"].checkConnection()", 1000);
}
}
try {
if (this._processedData) {
if (data) data += "&"+ this._processedData;
else data = this._processedData;
}
if (data) this.conn.send(data);
else this.conn.send(null);
}
catch(e) {
this._throwError(e);
}
if (!this._async) {
this.data = this.conn.responseText;
this.xml = this.conn.responseXML;
this._loading = false;
this._open = false;
if (this.conn.status == 200) {
if (typeof this.onSuccess == "function")
this.onSuccess();
}
else {
if (typeof this.onBadStatusCode == "function")
this.onBadStatusCode();
else {
this.error = "HTTP Request Status Code is "+ this.conn.status +" ("+ this.conn.statusText +")";
if (typeof this.onError == "function")
this.onError();
}
}
}
}
function AJAX_SendForm(formNode, async, oFormButton)
{
if (formNode) {
var data = "", numElements, len, i, x;
numElements = formNode.elements.length;
for (i = 0; i < numElements; i++) {
switch(formNode.elements[i].tagName) {
case "INPUT" :
switch(formNode.elements[i].type) {
case "text" :
data += ((data != "") ? "&" : "")+ escape(formNode.elements[i].name) +"="+ escape(formNode.elements[i].value);
break;
case "checkbox" :
case "radio" :
if (formNode.elements[i].checked)
data += ((data != "") ? "&" : "")+ escape(formNode.elements[i].name) +"="+ escape(formNode.elements[i].value);
break;
}
break;
case "SELECT" :
if (formNode.elements[i].multiple) {
len = formNode.elements[i].options.length;
for (x = 0; x < len; x++) {
if (formNode.elements[i].options[x].selected)
data += ((data != "") ? "&" : "")+ escape(formNode.elements[i].name) +"="+ escape(formNode.elements[i].options[x].value);
}
}
else if (formNode.elements[i].selectedIndex > -1)
data += ((data != "") ? "&" : "")+ escape(formNode.elements[i].name) +"="+ escape(formNode.elements[i].options[formNode.elements[i].selectedIndex].value);
break;
case "TEXTAREA" :
data += ((data != "") ? "&" : "")+ escape(formNode.elements[i].name) +"="+ escape(formNode.elements[i].value);
}
}
if (oFormButton) {
data += ((data != "") ? "&" : "");
if (oFormButton.name != "")
data += escape(oFormButton.name);
else
data += "Submit";
data += "="+ escape(oFormButton.value);
}
if (formNode.method.toLowerCase() == "get")
this.processData(data);
if (this.open(formNode.method, formNode.action, async))
this.send(data);
}
else this._throwError("Form Node is Invalid or Not Supplied");
}
function AJAX_SendObject(obj)
{
return(null);
}
function AJAX_isLoading()
{
return( this._loading );
}
function AJAX_ReadyStateChange()
{
if (this.conn.readyState != this._rstate) {
this._rstate = this.conn.readyState;
if (this.conn.readyState == 4) {
clearInterval(this._rstateIntv);
if (this._timeoutIntv)
clearInterval(this._timeoutIntv);
this.data = this.conn.responseText;
this.xml = this.conn.responseXML;
this._loading = false;
this._open = false;
this._clearIntv();
if (this.conn.status == 200) {
if (typeof this.onSuccess == "function")
this.onSuccess();
}
else {
if (typeof this.onBadStatusCode == "function")
this.onBadStatusCode();
else {
this.error = "HTTP Request Status Code is "+ this.conn.status +" ("+ this.conn.statusText +")";
if (typeof this.onError == "function")
this.onError();
}
}
}
if (typeof this.onReadyStateChange == "function")
this.onReadyStateChange();
}
}
function AJAX_CheckConnection()
{
if (!this._loading) {
clearInterval(this._timeoutIntv);
}
else if (++this._timeoutCount > this.timeout) {
this._clearIntv();
if (typeof this.onTimeout == "function") {
this.onTimeout();
}
else {
this._loading = false;
this._open = false;
this._throwError("Connection Timed Out After: "+ this.timeout +" seconds and no 'onTimeout' event is declared");
}
}
}
function AJAX_ProcessData(data, doHtmlEncode)
{
if (doHtmlEncode) {
var new_data = "", i, len;
var arr = data.split("&");
len = arr.length;
for (i = 0; i < len; i++) {
if (i > 0)
new_data += "&";
new_data += escape(arr[0]) +"="+ escape(arr[1]);
}
data = new_data;
}
if (this._processedData)
this._processedData += "&"+ data;
else
this._processedData = data;
}
function AJAX_WhileLoading()
{
if (this._loading)
this.whileLoading();
else
clearInterval(this._loadingIntv);
}
function AJAX_Abort()
{
this.conn.abort();
this._loading = false;
this._open = false;
}
function AJAX_GetTransportState()
{
return( AJAX_TransportStates[this._rstate] );
}
function AJAX_Error(message)
{
this._loading = false;
this._open = false;
this._clearIntv();
this.error = message;
if (typeof this.onError == "function")
this.onError();
else
throw message;
}
function AJAX_ClearIntervals()
{
clearInterval(this._rstateIntv);
clearInterval(this._loadingIntv);
clearInterval(this._timeoutIntv);
}
function AJAX_Destroy()
{
this._cleatIntv();
AJAX_ConnectionObjects[this._indice] = null;
}