var QueAjax = false;
var ajQ = new Array();
var lastOutputID;

function getHTTPObject() {
	if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else {
		alert ('Your Browser Does Not Support Ajax...');
	}
}

function ajax(url,qStr,outputID,loadImgFileName) {
	if(QueAjax == true) {
		ajaxQue(url,qStr,outputID,loadImgFileName);
		return false;
	}
	QueAjax = true
	document.getElementById(outputID).value = "<img src=\"/images/loading_circle.gif\" align=\"absmiddle\" width=\"20\" height=\"20\">";
	window.outputID = outputID;
	httpObject      = getHTTPObject();
	
	if (httpObject != null) {
		httpObject.open("GET", url + "?" + qStr + "&zqz="+Math.random(), true);
		httpObject.send(null);
		httpObject.onreadystatechange = ajaxOutputHtml;
	}
}

function ajaxQue(url,qStr,outputID,loadImgFileName) {
	ajqStr = url+"|"+qStr+"|"+outputID+"|"+loadImgFileName;
	if (outputID == lastOutputID) {
		ajQ.pop();
	}
	lastOutputID = outputID;
	ajQ.push(ajqStr);
}

function processAjQ() {
	ajqStr = ajQ.shift();
	try {
		ex = ajqStr.split("|");
		ajax(ex[0],ex[1],ex[2],ex[3]);
	} catch(err) {
		return false;
	}
}

function ajaxOutputHtml() {
	
	outputID = window.outputID;
//	alert(httpObject.readyState);
	if (httpObject.readyState == 1 || httpObject.readyState == 2 || httpObject.readyState == 3) {
		document.getElementById(outputID).innerHTML = "<img src=\"/images/loading_circle.gif\" align=\"absmiddle\" width=\"20\" height=\"20\">";
	} else if (httpObject.readyState == 4) {
		var response  = httpObject.responseText;
		document.getElementById(outputID).innerHTML = response;
		QueAjax = false;
		processAjQ();
	}
	
}

function create_request_string(theForm) {
	var reqStr = "";

	for(i=0; i < theForm.elements.length; i++) {
		isFormObject = false;
		
		if (theForm.elements[i].tagName == "INPUT") {
			switch (theForm.elements[i].type) {
				case "text":
				case "hidden":
					var theValue = theForm.elements[i].value;
					var toSend   = theValue.replace('&',' and ');
					reqStr += theForm.elements[i].name + "=" + toSend;
					isFormObject = true;
				break;
				
				case "checkbox":
					if (theForm.elements[i].checked) {
						reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
					} else {
						reqStr += theForm.elements[i].name + "=";
					}
					isFormObject = true;
				break;
				
				case "radio":
					if (theForm.elements[i].checked) {
						reqStr += theForm.elements[i].name + "=" + theForm.elements[i].value;
						isFormObject = true;
					}
			}
		}
		
		if (theForm.elements[i].tagName == "SELECT") {
			var sel = theForm.elements[i];
			reqStr += sel.name + "=" + sel.options[sel.selectedIndex].value;
			isFormObject = true;
		}
		
		if (theForm.elements[i].tagName == "TEXTAREA") {
			var txt = theForm.elements[i];
			reqStr += txt.name + "=" + txt.value.replace(/\n/g,'<br>');
			isFormObject = true;
		}
		
		if ((isFormObject) && ((i+1)!= theForm.elements.length)) {
			reqStr += "&";
		}
	}
	return reqStr;
}
