// AJAX and HTTP objects
// --
// AJAX wrapper written by Andy H - ovine by design - www.ovine.net
// *** Feel free to use, but would appreciate if you keep this
// *** message and web address intact.

function getHTTPObject() 
{
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	return req;
}


function getHTTPObject2() 
{
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
   		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
  {
	try {
		xmlhttp = new XMLHttpRequest();
		xmlhttp.overrideMimeType("text/xml"); 
	} catch (e) {
		xmlhttp = false;
	}
  }
  return xmlhttp;
}


function AJAX(pUri)
{
	// generate teh HTTP object
	this.http = getHTTPObject();
	this.uri = pUri;	
	
	this.propkey = new Array();
	this.propval = new Array();
	this.proptype = new Array();
	this.addProp = AJAX_addProp;
	this.getProp = AJAX_getProp;
	this.clearProp = AJAX_clearProp;
	this.execute = AJAX_execute;
	this.getXML = AJAX_getXML;
	this.close = AJAX_close;
}

// set key = start value of type (0= for receive only, 1= for send and receive)
function AJAX_addProp(pkey, pval, ptype)
{
	var t=0;
	// first ensure property not already defined
	for (t=0; t<this.propkey.length; t++)
	{
		if (this.propkey[t]==pkey)
			break;
	}

	this.propkey[t]=pkey;
	this.propval[t]=pval;
	this.proptype[t]=ptype;
}

function AJAX_getProp(pkey)
{
	var t=0;
	var found=false;
	// find property key
	for (t=0; t<this.propkey.length; t++)
	{
		if (this.propkey[t]==pkey)
		{
			found = true;
			break;
		}
	}
	if (found)
	{
		return this.propval[t];
	} else {
		return "";
	}
	
}

function AJAX_clearProp()
{
	delete this.propkey;
	delete this.propval;
	delete this.proptype;
	
	this.propkey = new Array();
	this.propval = new Array();
	this.proptype = new Array();
}

function AJAX_execute(pGo)
{
	var plist="";
	var t=0;
	
	for (t=0; t<this.propkey.length; t++)
		if (this.proptype[t] == 1)
			plist += escape(this.propkey[t]) +"="+ escape(this.propval[t]);

	this.http.onreadystatechange = pGo;
	this.http.open("GET", this.uri+'?'+plist, true);
	this.http.send("");
	

}
function AJAX_close()
{
	// fix for IE browsers
	delete this.http;
	this.http = getHTTPObject();
}

function AJAX_getXML()
{
	var plist="";
	var t=0;
	if (this.http.readyState == 4) 
	{
		var xmlDocument = this.http.responseXML;
		// populate our properties
		for (t=0; t<this.propkey.length; t++)
		{
			try { this.propval[t] = xmlDocument.getElementsByTagName(this.propkey[t]).item(0).firstChild.data; } catch(e) { this.propval[t] = 'Error';}
		}
	}
	return this.http.readyState;
}