var _ms_XMLHttpRequest_ActiveX = "";
document.writeln('<div id="AJAX_DIV1234567890" style="visibility: hidden">');
document.writeln('<img id="AJAX_IMG1234567890" width="0" height="0">');
document.writeln('</div>');


// Returns an object reference to one or more strings
// ignore the fact that there are no arguments to this method -- javascript doesn't care how many you send (not strongly typed)
// The method checks the actual # of arguments -- returns a single object or an array
function $() {
    var elements = new Array();

    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];

        if (typeof element == 'string')
            element = document.getElementById(element);

        if (arguments.length == 1)
            return element;

        elements.push(element);
    }

    return elements;
}

/*
 * AJAXRequest: An encapsulated AJAX request. To run, call
 * new AJAXRequest( method, url, async, process, data )
 *
 */

function executeReturn( AJAX ) {
    if (AJAX.readyState == 4) {
        if (AJAX.status == 200) {
		    if ( AJAX.responseText ) {
				eval(AJAX.responseText);
			}
		}
    }
}

function AJAXTimeout( AJAX) {
    AJAX.abort();
}
    
function AJAXRequest( method, url, data, process, async, dosend) {
    // self = this; creates a pointer to the current function
    // the pointer will be used to create a "closure". A closure
    // allows a subordinate function to contain an object reference to the
    // calling function. We can't just use "this" because in our anonymous
    // function later, "this" will refer to the object that calls the function 
    // during runtime, not the AJAXRequest function that is declaring the function
    // clear as mud, right?
    // Java this ain't
    
    var self = this;

    // check the dom to see if this is IE or not
    if (window.XMLHttpRequest && navigator.appVersion.toLowerCase().indexOf('msie') == -1) {
	// Not IE
        self.AJAX = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
	// Hello IE!
        // Instantiate the latest MS ActiveX Objects
        if (_ms_XMLHttpRequest_ActiveX != "") {
            self.AJAX = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
        } else {
	    // loops through the various versions of XMLHTTP to ensure we're using the latest
	    var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
                        "Microsoft.XMLHTTP"];

            for (var i = 0; i < versions.length ; i++) {
                try {
		    // try to create the object
		    // if it doesn't work, we'll try again
		    // if it does work, we'll save a reference to the proper one to speed up future instantiations
                    self.AJAX = new ActiveXObject(versions[i]);

                    if (self.AJAX) {
                        _ms_XMLHttpRequest_ActiveX = versions[i];
                        break;
                    }
                }
                catch (objException) {
                // trap; try next one
                } ;
            }

            ;
        }
    }

	// Browser support AJAX
    if (self.AJAX) {
		// if no callback process is specified, then assing a default which executes the code returned by the server
		if (typeof process == 'undefined' || process == null) {
			process = executeReturn;
		}

		self.process = process;

		// create an anonymous function to log state changes
		self.AJAX.onreadystatechange = function( ) {
			//logger("AJAXRequest Handler: State =  " + self.AJAX.readyState);
			self.process(self.AJAX);
		}

		// if no method specified, then default to POST
		if (!method) {
			method = "POST";
		}

		method = method.toUpperCase();

		if (typeof async == 'undefined' || async == null) {
			async = true;
		}

	//    alert("AJAX Request: " + ((async) ? "Async" : "Sync") + " " + method + ": URL: " + url + ", Data: " + data);

        setTimeout(function(){AJAXTimeout(self.AJAX)}, 20000);

		self.AJAX.open(method, url, async);


		if (method == "POST") {
			self.AJAX.setRequestHeader("Connection", "close");
			self.AJAX.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			self.AJAX.setRequestHeader("Method", "POST " + url + "HTTP/1.1");
		}

		// if dosend is true or undefined, send the request
		// only fails is dosend is false
		// you'd do this to set special request headers
		if ( dosend || typeof dosend == 'undefined' ) {
			if ( !data ) data=""; 
			self.AJAX.send(data);
		}
	} else {
		document.getElementById('AJAX_IMG1234567890').src = url;
		self.AJAX = new Object();
		self.AJAX.responseText = '';
		self.AJAX.readyState = 4;
		self.AJAX.status = 200;
		if (process) process(self.AJAX);
    }
	
    return self.AJAX;
}