////////////////////////////////////////////
// PostBack and AjaxPostBack
////////////////////////////////////////////

	qcodo.postBack = function(strForm, strControl, strEvent, strParameter) {
		var objForm = document.getElementById(strForm);
		objForm.Qform__FormControl.value = strControl;
		objForm.Qform__FormEvent.value = strEvent;
		objForm.Qform__FormParameter.value = strParameter;
		objForm.Qform__FormCallType.value = "Server";
		objForm.Qform__FormUpdates.value = this.formUpdates();
		objForm.Qform__FormCheckableControls.value = this.formCheckableControls(strForm, "Server");
		objForm.submit();
	};

	qcodo.formUpdates = function() {
		var strToReturn = "";
		for (var intIndex = 0; intIndex < qcodo.controlModifications.length; intIndex++) {
			strToReturn += qcodo.controlModifications[intIndex] + "\n";
		};

		qcodo.controlModifications = new Array();
		return strToReturn;
	};
	
	qcodo.formCheckableControls = function(strForm, strCallType) {
		var objForm = document.getElementById(strForm);
		var strToReturn = "";

		for (var intIndex = 0; intIndex < objForm.elements.length; intIndex++) {
			if (((objForm.elements[intIndex].type == "checkbox") ||
				 (objForm.elements[intIndex].type == "radio")) &&
				((strCallType == "Ajax") ||
				(!objForm.elements[intIndex].disabled)))
				strToReturn += " " + objForm.elements[intIndex].id;
		};

		if (strToReturn.length > 0)
			return strToReturn.substring(1);
		else
			return "";
	};

	qcodo.ajaxQueue = new Array();

	qcodo.postAjax = function(strForm, strControl, strEvent, strParameter, strWaitIconControlId, blnUploadFile) {
		// Figure out if Queue is Empty
		var blnQueueEmpty = false;
		if (qcodo.ajaxQueue.length == 0)
			blnQueueEmpty = true;

		// Enqueue the AJAX Request
		qcodo.ajaxQueue.push(new Array(strForm, strControl, strEvent, strParameter, strWaitIconControlId, blnUploadFile));

		// If the Queue was originally empty, call the Dequeue
		if (blnQueueEmpty)
			qcodo.dequeueAjaxQueue();
	};
	
	qcodo.clearAjaxQueue = function() {
		qcodo.ajaxQueue = new Array();
	};

	qcodo.objAjaxWaitIcon = null;

	qcodo.dequeueAjaxQueue = function() {
		if (qcodo.ajaxQueue.length > 0) {
			strForm = this.ajaxQueue[0][0];
			strControl = this.ajaxQueue[0][1];
			strEvent = this.ajaxQueue[0][2];
			strParameter = this.ajaxQueue[0][3];
			strWaitIconControlId = this.ajaxQueue[0][4];
			blnUploadFile = this.ajaxQueue[0][5];

			// Display WaitIcon (if applicable)
			if (strWaitIconControlId) {
				this.objAjaxWaitIcon = this.getWrapper(strWaitIconControlId);
				if (this.objAjaxWaitIcon)
					this.objAjaxWaitIcon.style.display = 'inline';
			};

			var objForm = document.getElementById(strForm);
			objForm.Qform__FormControl.value = strControl;
			objForm.Qform__FormEvent.value = strEvent;
			objForm.Qform__FormParameter.value = strParameter;
			objForm.Qform__FormCallType.value = "Ajax";
			objForm.Qform__FormUpdates.value = qcodo.formUpdates();
			
			if(blnUploadFile) {
				objForm.Qform__FormCheckableControls.value = this.formCheckableControls(strForm, "Server");
				
		        var id = objForm.id + "_iframe";
				
				var d = document.createElement('DIV');   
        		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+id+'" name="'+id+'" onload="qcodo.onIframeLoad(this);"></iframe>';   
        		document.body.appendChild(d);

				objForm.target = id;
				objForm.submit();
				
				return;
			} else
				objForm.Qform__FormCheckableControls.value = this.formCheckableControls(strForm, "Ajax");

			var strPostData = "";
			for (var i = 0; i < objForm.elements.length; i++) {
				switch (objForm.elements[i].type) {
					case "checkbox":
					case "radio":
						if (objForm.elements[i].checked) {
							var strTestName = objForm.elements[i].name + "_";
							if (objForm.elements[i].id.substring(0, strTestName.length) == strTestName)
								strPostData += "&" + objForm.elements[i].name + "=" + objForm.elements[i].id.substring(strTestName.length);
							else
								strPostData += "&" + objForm.elements[i].id + "=" + "1";
						};
						break;

					case "select-multiple":
						var blnOneSelected = false;
						for (var intIndex = 0; intIndex < objForm.elements[i].options.length; intIndex++)
							if (objForm.elements[i].options[intIndex].selected) {
								strPostData += "&" + objForm.elements[i].name + "=";
								strPostData += objForm.elements[i].options[intIndex].value;
							};
						break;

					default:
						strPostData += "&" + objForm.elements[i].id + "=";

						// For Internationalization -- we must escape the element's value properly
						var strPostValue = objForm.elements[i].value;
						if (strPostValue) {
							strPostValue = strPostValue.replace(/&/g, escape('&'));
							strPostValue = strPostValue.replace(/\+/g, "%2B");
						};
						strPostData += strPostValue;
						break;
				};
			};

			var strUri = objForm.action;

			var objRequest;
			if (window.XMLHttpRequest) {
				objRequest = new XMLHttpRequest();
			} else if (typeof ActiveXObject != "undefined") {
				objRequest = new ActiveXObject("Microsoft.XMLHTTP");
			};

			if (objRequest) {
				objRequest.open("POST", strUri, true);
				objRequest.setRequestHeader("Method", "POST " + strUri + " HTTP/1.1");
				objRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

				objRequest.onreadystatechange = function() {
					if (!qcodo.unloadFlag && objRequest.readyState == 4) {
						qcodo.processResponse(objRequest.responseText, objRequest.responseXML);
					};
				};

				objRequest.send(strPostData);
			};
		};
	};
	
	qcodo.onIframeLoad = function(frame) {
		try {
			var doc;
			if(frame.contentDocument)
				doc = frame.contentDocument;
			else if(frame.contentWindow)
				doc = frame.contentWindow.document;
			else
				doc = window.frames[frame.id].document;
			
			if(doc.location.href == "about:blank")
				return;
			
			var responseText, responseXML;
			if(doc && doc.firstChild)
				responseText = doc.firstChild.innerHTML;
			if(doc && doc.XMLDocument)
				responseXML = doc.XMLDocument;
			else
				responseXML = doc;
			
			qcodo.processResponse(responseText, responseXML);
		} catch(e) {
			alert("An error occurred during AJAX IFRAME Response parsing.");
		};
		
		setTimeout(function(){document.body.removeChild(frame.parentNode);}, 100);
	};
	
	qcodo.processResponse = function(responseText, objXmlDoc) {
		try {
//			qcodo.logMessage(responseText, true);
//			alert('AJAX Response Received');

			if ((!objXmlDoc) || (!objXmlDoc.documentElement) || (objXmlDoc.getElementsByTagName('response').length == 0)) {
				alert("An error occurred during AJAX Response parsing.\r\n\r\nThe error response will appear in a new popup.");
				var objErrorWindow = window.open('about:blank', 'qcodo_error','menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,width=1000,height=700,left=50,top=50');
				objErrorWindow.focus();
				objErrorWindow.document.write(responseText);
				return;
			} else {
				var intLength = 0;

				// Go through Controls
				var objXmlControls = objXmlDoc.getElementsByTagName('control');
				intLength = objXmlControls.length;

				for (var intIndex = 0; intIndex < intLength; intIndex++) {
					var strControlId = objXmlControls[intIndex].attributes.getNamedItem('id').nodeValue;

					var strControlHtml = "";
					if (objXmlControls[intIndex].firstChild)
						strControlHtml = objXmlControls[intIndex].firstChild.nodeValue;
					if (qcodo.isBrowser(qcodo.FIREFOX))
						strControlHtml = objXmlControls[intIndex].textContent;

					// Perform Callback Responsibility
					if (strControlId == "Qform__FormState") {
						var objFormState = document.getElementById(strControlId);
						objFormState.value = strControlHtml;							
					} else {
						var objSpan = document.getElementById(strControlId + "_ctl");
						if (objSpan)
							objSpan.innerHTML = strControlHtml;
					};
				};

				// Go through Commands
				var objXmlCommands = objXmlDoc.getElementsByTagName('command');
				intLength = objXmlCommands.length;

				for (var intIndex = 0; intIndex < intLength; intIndex++) {
					if (objXmlCommands[intIndex] && objXmlCommands[intIndex].firstChild) {
						var strCommand = "";
						intChildLength = objXmlCommands[intIndex].childNodes.length;
						for (var intChildIndex = 0; intChildIndex < intChildLength; intChildIndex++)
							strCommand += objXmlCommands[intIndex].childNodes[intChildIndex].nodeValue;
						eval(strCommand);
					};
				};
			};
		} catch (objExc) {
			alert(objExc.message + "\r\non line number " + objExc.lineNumber + "\r\nin file " + objExc.fileName);
			alert("An error occurred during AJAX Response handling.\r\n\r\nThe error response will appear in a new popup.");
			var objErrorWindow = window.open('about:blank', 'qcodo_error','menubar=no,toolbar=no,location=no,status=no,scrollbars=yes,resizable=yes,width=1000,height=700,left=50,top=50');
			objErrorWindow.focus();
			objErrorWindow.document.write(responseText);
			return;
		};

		// Perform the Dequeue
		qcodo.ajaxQueue.reverse();
		qcodo.ajaxQueue.pop();
		qcodo.ajaxQueue.reverse();
		
		// Hid the WaitIcon (if applicable)
		if (qcodo.objAjaxWaitIcon)
			qcodo.objAjaxWaitIcon.style.display = 'none';
			
		// Clean up after any elements that got removed
		for (var strGroupingId in qcodo.dropZoneGrouping)
			for (var strControlId in qcodo.dropZoneGrouping[strGroupingId])
				if(!document.getElementById(strControlId))
					qcodo.dropZoneGrouping[strGroupingId][strControlId] = false;

		// If there are still AjaxEvents in the queue, go ahead and process/dequeue them
		if (qcodo.ajaxQueue.length > 0)
			qcodo.dequeueAjaxQueue();
	};



//////////////////
// Qcodo Shortcuts
//////////////////

	qc.pB = qcodo.postBack;
	qc.pA = qcodo.postAjax;
