/*
Each instance of ckAjaxObj will have a corresponding ckAjaxDebugObj so that multiple asynchronous actions can
occur on the page and each occurence can show their own debug info
*/
function ckAjaxDebugObj() {
	this.debugSendWindowContainer = null;
	this.debugReceiveWindowContainer = null;

	//determine if we're active by seeing if the debug window is showing. If so, that means another ajax debugger turned it on, so this instance should automatically start debugging	
	this.bActive = (document.getElementById("ajax.debugReceiveWindowContainer"));
	
	return this;
	
}

ckAjaxDebugObj.prototype.setActive=function(bActive) {
	this.bActive = bActive; 
	if (this.bActive) this.showDebugWindows();
}


/*-------------------------------------------------------------------------------------------
Debug
-------------------------------------------------------------------------------------------*/

ckAjaxDebugObj.prototype.setDebugSend = function(ajaxUniqueID,sURL,msgBody,aHeaders) {
	if(this.bActive) {
		try {	
			if (!this.debugSendWindowContainer)  this.showDebugWindows();
		
			//With multiple ajax objects on page, any one of them could have created debug windows, so need to check for page element each time
			if (this.debugSendWindowContainer = (this.debugSendWindowContainer || document.getElementById("ajax.debugSendWindowContainer"))) {
				var sText = "";
				if (msgBody) {
					sText = msgBody;
					sText = sText.replace(/\n/g,"<br/>");
					sText = sText.replace(/\</g,"&lt;");
					sText = sText.replace(/\>/g,"&gt;");
					sText = sText.replace(/\&/g,"<br/>&");
				} else sText = "[NULL Body]";
				sText = '<b style="color:#0000FF">SEND (' + ajaxUniqueID + '):</b><br/><b>URL:' + sURL + '</b><br/><br/>' + sText + "<hr>";
				this.debugSendWindowContainer.innerHTML += sText;
			}
		} catch (err) {
			throwConsoleErroror("ckAjaxDebugObj.setDebugSend",err);
		}
	}
}
ckAjaxDebugObj.prototype.setDebugReceive = function(ajaxUniqueID,sText) {
	if (this.bActive) {
		if (!this.debugReceiveWindowContainer) this.showDebugWindows();
	
		try {
			if (this.debugReceiveWindowContainer = (this.debugReceiveWindowContainer || document.getElementById("ajax.debugReceiveWindowContainer"))) {
				sText = sText.replace(/</g,"&lt;").replace(/>/g,"&gt;");
				sText = sText.replace(/\n/g,"<br/>");
				sText = '<b style="color:#00FF00">RECEIVE (' + ajaxUniqueID + '):</b><br/>' + sText;
				sText = "<hr>" + sText + "<hr>"; //separate since multiple receive instances may 
				this.debugReceiveWindowContainer.innerHTML += sText;
			}
	
		} catch (err) {
			throwConsoleErroror("ckAjaxDebugObj.seetDebugReceive",err)
		}
	}	
}
ckAjaxDebugObj.prototype.showDebugWindows = function() {
	if (this.bActive) {
		//intended primarily to be called manually from the address bar when needed. Only create and clear the content if not already created.
		//Instead of checking this.debugSendWindowContainer, need to check for the actual element on the page since multiple Ajax objects could be created.  All should use the same debug windows.

		var checkSendContainer = document.getElementById("ajax.debugSendWindowContainer");
		var checkReceiveContainer = document.getElementById("ajax.debugReceiveWindowContainer");
	
		if (checkSendContainer) this.debugSendWindowContainer = checkSendContainer;
		if (checkReceiveContainer) this.debugReceiveWindowContainer = checkReceiveContainer;
	
		if ((!this.debugSendWindowContainer) || (!this.debugReceiveWindowContainer)) {
			this.__createDebugWindows();
			if (this.debugSendWindowContainer) {
				this.debugSendWindowContainer.innerHTML = '<b>Ajax POST data will appear here.</b><hr/>';
				this.debugSendWindowContainer.style.display="block";
				this.debugSendWindowContainer.style.zIndex = 100;
			} else throwConsoleError("ckAjaxDebugObj.showDebugWindows","Debug send window could not be created");
			
			if (this.debugReceiveWindowContainer) {
				this.debugReceiveWindowContainer.innerHTML = '<b>Ajax response data will appear here.</b><hr/>';
				this.debugReceiveWindowContainer.style.display="block";
				this.debugReceiveWindowContainer.style.zIndex = 101;
			} else throwConsoleError("ckAjaxDebugObj.showDebugWindows","Debug receive window could not be created");
		}
	}
}

/*-------------------------------------------------------------------------------------------

PRIVATE

-------------------------------------------------------------------------------------------*/
ckAjaxDebugObj.prototype.__createDebugWindows = function() {
	//debug windows may already exist on screen from other instances of this object.  Do not recreat if so.
	this.debugSendWindowContainer = document.getElementById("ajax.debugSendWindowContainer");
	if (!this.debugSendWindowContainer) {
		var oDiv = document.createElement('DIV');
		if (oDiv) {
			oDiv.id = "ajax.debugSendWindowContainer";
			oDiv.style.height = "300px";
			oDiv.style.overflowY = "auto";
			oDiv.style.border="1px solid #000099";
			oDiv.style.backgroundColor="#FFFFFF";
			oDiv.style.margin="5px";
			oDiv.style.padding="10px";
			document.body.appendChild(oDiv);
			this.debugSendWindowContainer = document.getElementById("ajax.debugSendWindowContainer");
		} else throw new Error("Unable to create debug send container");
	}

	this.debugReceiveWindowContainer = document.getElementById("ajax.debugReceiveWindowContainer");
	if (!this.debugReceiveWindowContainer) {
		var oDiv = document.createElement('DIV');
		if (oDiv) {
			oDiv.id = "ajax.debugReceiveWindowContainer";
			oDiv.style.height = "300px";
			oDiv.style.overflowY = "auto";
			oDiv.style.border="1px solid #009900";
			oDiv.style.backgroundColor="#FFFFFF";
			oDiv.style.margin="5px";
			oDiv.style.padding="10px";
			document.body.appendChild(oDiv);
			this.debugReceiveWindowContainer = document.getElementById("ajax.debugReceiveWindowContainer");
		} else throw new Error("Unable to create debug receive container");
		
	}

}

