/*

Customised version of this class built to work with the transitions class.

*/



function LucidSimpleComms(transify) {
	this.postbody = "";
	this.debug = false;
	var foo = this;
	
	var divsandurls = [];
	
	this.remoteQuery = function(url) {
		this.getData(url, true);
	}
	
	this.remotePost = function(url, formvars) {
		this.postbody = formvars
		this.getData(url, true);
	}
	
	this.remoteContent = function(url,div) {
		divsandurls[url] = div;
		this.getData(url, false);
	}
	
	this.remotePostMultipart = function(url, formvars) {
		
	}
	
	this.newXMLHttpRequest = function() {
		var xmlreq = false;
		if (window.XMLHttpRequest) {
			// Create XMLHttpRequest object in non-Microsoft browsers
			xmlreq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			// Create XMLHttpRequest via MS ActiveX
			try {
				// Try to create XMLHttpRequest in later versions
				// of Internet Explorer
				xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
	    	} catch (e1) {
				// Failed to create required ActiveXObject
				try {
					// Try version supported by older versions
					// of Internet Explorer
					xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e2) {
					// Unable to create an XMLHttpRequest with ActiveX
				}
			}
		}
		return xmlreq;
	}
	
	this.getReadyStateHandler = function(url, req, responseXmlHandler, type)
	{
		// Return an anonymous function that listens to the
		// XMLHttpRequest instance
		return function () {
			// If the request's status is "complete"
			if (req.readyState == 4) {
				// Check that a successful server response was received
				if (req.status == 200) {
					// Pass the XML payload of the response to the
					// handler function
					if (type == "XML") {
						responseXmlHandler(req.responseXML, url);
					} else {
						responseXmlHandler(req.responseText, url);
					}
				} else {
					// An HTTP problem has occurred
					if (this.debug == 1) {
						logError("HTTP error: "+req.status+req.responseText);
					}
				}
			}
		}
	}
	
	this.getData = function(url, useXMLResponse) {
		if (this.debug == 2) {
			logError("sending request for processing at: "+url)
		}
	    var req = this.newXMLHttpRequest();
		if (useXMLResponse) {
			var handlerFunction = this.getReadyStateHandler(url, req, this.processReturnXML, "XML");
		} else {
			var handlerFunction = this.getReadyStateHandler(url, req, this.processReturn, "plaintext");
		}
	    req.onreadystatechange = handlerFunction;
	
	    req.open("POST", url, true);
	
	    req.setRequestHeader("Content-Type",
	                       "application/x-www-form-urlencoded");
		req.send(this.postbody);
		this.postbody = ""
	}
	
	this.processReturn = function(data, url) {
		
		if (divsandurls[url]) {
			divsandurls[url].innerHTML = data;
			transify.setupLinks();
			//alert(data);
		} else {
			alert("There was a problem loading this page");
			alert(data);
		}
		
		//document.getElementById(replacediv).innerHTML = data;
		if (data.indexOf("<script>") != -1) {
			if (this.debug == 2) {
				logError("processing returned javascript: "+data);
			}
			// process any script sent by the browser
			scripttagstart = data.indexOf("<script>")+8
			scripttagend = data.indexOf("</script>")
			eval(data.slice(scripttagstart,scripttagend))
		}
		
		transify.ajaxloadingfinished.fire(url);
	}
	
	this.processReturnXML = function(data, url) {
		//fire an event and pass the XML object as the parameter
		eventsManager.ajaxRequestReturned.fire(data, url);
	}
	
	this.iframeProcessReturnXML = function(iframeid, url) {
		//alert("got in here!");
		data = this.getContentDocument(document.getElementById(iframeid));
		//alert(data);
		if (document.all) {
			data = data.XMLDocument;
		}
		eventsManager.ajaxRequestReturned.fire(data, url);
	}
	
	this.getContentDocument = function(inIFrameObj) {
		if (inIFrameObj) {
		 	if (inIFrameObj.contentDocument) // For NS6
			{
		    	// debug('<b>getContentDocument</b>:contentDocument');
		    	return(inIFrameObj.contentDocument);
		  	}
		  	else if (inIFrameObj.contentWindow) // For IE5.5 and IE6
		  	{
		    	// debug('<b>getContentDocument</b>:contentWindow');
		    	return(inIFrameObj.contentWindow.document);
		  	}
		  	else if (inIFrameObj.document) // For IE5
		  	{
		    	// debug('<b>getContentDocument</b>:document');
		    	return(inIFrameObj.document);
		  	}
		 	else
		  	{
		    	// debug('<b>getContentDocument</b>:undefined');
		    	return(undefined);
		  	}
		}
	}
}

