/********** SAMPLE ***************

var aji = new AJInterface();

function function() {

	aji.onComplete = function(response, note) {

	};
	aji.onStart = function() {

	};

	aji.setVar("a", "a");
	aji.send("inc/functions.php", false);
	
	
}


*******************************************/



var thisObj;
function AJInterface() {
	this.xmlHttp;
	this.sendVars = Array();

	// provide a way for anonymous functions to refer to this
	thisObj = this;
	
	// create the xmlHttp object
	try {
		// Firefox, Opera 8.0+, Safari
		this.xmlHttp = new XMLHttpRequest();
	} catch (e) {

		// Internet Explorer
		try {
			this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			
			try {
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

			} catch (e) {
				alert("Your browser does not support AJAX. Please update your browser.");
				return false;
			}
		}
	}
	
	
	// call the user defined functions on state changes
	this.xmlHttp.onreadystatechange = this.stateChange;


}


AJInterface.prototype.send = function(href, async) {
	// determine if it's asynchronous or not
	if(async == undefined) {
		async = true;
	}
	
	var query = "?";
	for(var i in this.sendVars) {
		query += (i + "=" + this.sendVars[i] + "&");
	}
	
	this.xmlHttp.open("GET", href+query, async);
	this.xmlHttp.send(null);		

	if(!async) {
		this.stateChange();
	}
	
}


AJInterface.prototype.setVar = function(variable, value) {
	
	this.sendVars[variable] = value;
	
}

AJInterface.prototype.clearVars = function() {
	this.sendVars = Array();
}

AJInterface.prototype.stateChange = function() {
	
	// Get the data from the server's response
	if((thisObj.xmlHttp.readyState < 4) && (typeof(thisObj.onStart) == "function")) {
		thisObj.onStart();
	} else if((thisObj.xmlHttp.readyState == 4) && (typeof(thisObj.onComplete) == "function")) {
		
		var response = thisObj.xmlHttp.responseText;
		var note = response.slice(0, response.indexOf("\n"));
		var output = response.substr(response.indexOf("\n"));
		
		thisObj.onComplete(output, note);
		thisObj.clearVars();
	}

}
