// Ajax utility object for LoveYourLoftAgain
var Transport = function(){
	this.method =  "GET";
	this.enc =  "";
	this.headers =  {};
	this.data =  null;
	this.transport  =  this.getHttpRequestObj();
	this.responseType =  "xml";
	this.callback = "";
	this.failureCallback = function() { alert("Sorry, something went wrong"); };
	this.responseData = "";
	this.vars = {};
	this.loader = new Image();
	this.parent = "";
};
Transport.prototype.setParent = function(obj) {
	this.parent = obj;	
}
Transport.prototype.addLoader = function(graphic, location) {
	this.loader.src = graphic;
	this.loader.id = "ajaxLoaderGraphic";
	this.loader.onload = function() {
		document.getElementById(location).appendChild(this);	
	}
}
Transport.prototype.removeLoader = function() {
	if (document.getElementById("ajaxLoaderGraphic")) {
		document.getElementById("ajaxLoaderGraphic").parentNode.removeChild(document.getElementById("ajaxLoaderGraphic"));
	}
}
Transport.prototype.getHttpRequestObj = function() {
	var http = false;
	//Use IE's ActiveX items to load the file.
	if(typeof ActiveXObject != 'undefined') {
		try {
			http = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				http = false;
			}
		}
	} else if (XMLHttpRequest) {
		try {
			http = new XMLHttpRequest();
		} catch (e) {
			http = false;
		}
	}
	return http;
}
Transport.prototype.setFailureCallback = function(callback) {
	this.failureCallback = callback;	
}
Transport.prototype.setEndpoint = function(endpoint) {
	this.endpoint = endpoint;	
}
Transport.prototype.setMethod = function(method) {
	this.method = method.toUpperCase();	
}
Transport.prototype.setResponseType = function(rtype) {
	this.responseType = rtype;	
}
Transport.prototype.setData = function(dataString) {
	this.data = dataString;	
}
Transport.prototype.setCallback = function(func) {
	this.callback = func;	
}
Transport.prototype.addHeader = function(headerName, headerValue) {
	this.headers.headerName = headerValue;
}
Transport.prototype.despatch = function() {
	if (this.method=="GET") {
		this.endpoint += "?";
		for (var n in this.vars) {
			this.endpoint += n+"="+this.vars[n]+"&";
		}
	}
	this.transport.open(this.method, this.endpoint, true);
	for (var h in this.headers) {
		this.transport.setRequestHeader(h, this.headers[h]);	
	}
	var that = this;
	this.transport.onreadystatechange = function() {
		if (that.transport.readyState==4) {
			that.removeLoader();
			//alert("Server responded with "+that.transport.status);	
			if (that.transport.status >= 200 && that.transport.status<300) {
				if (that.responseType=="xml") {
					that.responseData = that.transport.responseXML;
					that.doCallback(that.transport.responseXML);
				} else {
					that.responseData = that.transport.responseText;
					that.doCallback(that.transport.responseText);
				}
			} else {
//				alert("Server responded with "+that.transport.status+", "+that.transport.statusText);	
				that.failureCallback();
			}
		}
	}
	this.transport.send(this.data);
}
Transport.prototype.addVariable = function(varName, varValue) {
	this.vars[varName] = varValue;	
}
Transport.prototype.testResponse =  function(response) {
		//alert(response);
}
Transport.prototype.traceDocumentElement = function(forXML) {
	//alert(forXML.documentElement.firstChild.firstChild.nodeName);
}
Transport.prototype.doCallback = function(data) {
	this.callback(data);
	//alert(data);	
}

