var do_req;

function DoHTTPRequest(url, process_handler, post_parameter_array, force_sync)
{
	// set default handler
	if(process_handler == null)
	{
		var process_handler = HTTPRequestHandler;
	}
	
	if(force_sync == null)
		force_sync = false;
	else
		force_sync = true;
	
	if(url.indexOf('?') == -1)
		url = url + '?';

	// append date to avoid pages being cached
	cDate = new Date;
	url = url + "&" + cDate.getTime() + "=1&";
	
	// append no template flag to not re-show template
	url = url + "&no_template=1&";
	
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest)
	{
		do_req = new XMLHttpRequest();
 	}
	else if (window.ActiveXObject)      // branch for IE/Windows ActiveX version
		do_req = new ActiveXObject("Microsoft.XMLHTTP");

	if (do_req)
	{
		if(process_handler)
			do_req.onreadystatechange = process_handler;
		do_req.open("POST", url, !force_sync);
		do_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		do_req.send(ConvertParameterArrayToURLEncodedString(post_parameter_array));
	}
}

function TestPost()
{
	var post_parameter_array = Array();
	post_parameter_array[0] = Array("test 1 key","test 1 data");
	post_parameter_array[1] = Array("test 2 key","test 2 data");
	post_parameter_array[2] = Array("test 3 key","test 3 & data");
	
	DoHTTPRequest("index.php?param=jforum&operation=get_forum_topics&area_id=1&", 
			ProcessForumDisplay, post_parameter_array);
}

function ConvertParameterArrayToURLEncodedString(post_parameter_array)
{
	var post_string = "";
	
	if(typeof(post_parameter_array) == "object" || typeof(post_parameter_array) == "array")
	{
		for(cur_index in post_parameter_array)
		{
			post_string += escape(post_parameter_array[cur_index][0]) + "=" + escape(post_parameter_array[cur_index][1]) + "&";
		}
		return post_string;
	}
	else
	{
		return null;
	}
	
}

function DisplayReadyState(code)
{
	var lmb = document.getElementById("loading_box");
	if(lmb)
	{
		if(code == "0")
		{
			lmb.innerHTML = "Initializing...";
			lmb.style.display = "block";
		}
		else if(code == "1")
		{
			lmb.innerHTML = "Loading...";
			lmb.style.display = "block";
		}
		else if(code == "2")
		{
			lmb.innerHTML = "Processing...";
			lmb.style.display = "block";
		}
		else if(code == "3")
		{
			lmb.innerHTML = "Displaying...";
			lmb.style.display = "block";
		}
		else if(code != null)
		{
			lmb.innerHTML = code;
			lmb.style.display = "block";
		}
		else if(code == null)
		{
			lmb.style.display = "none";
		}
	}	
}

// the "do nothing" handler
function HttpDoNothingRequestHandler()
{
	// do nothing.
}

// default handler
function HTTPRequestHandler()
{
	var http_output;
	// only if req shows "complete"
	if (do_req.readyState == 4)
	{
		// only if "OK"
		if (do_req.status == 200)
		{
			http_output = do_req.responseText;
			alert(http_output);
		}
		else
		{
			alert("There was a problem retrieving the HTTPRequestHandler data:" + do_req.statusText);
		}
	}
	
	DisplayReadyState((do_req.readyState==4?null:do_req.readyState));
}

function GetSelectOptionTextByValue(selectBoxId, value)
{
	var selectObj = document.getElementById(selectBoxId);
	
	// get the array of options so that we don't have to worry about changing status values everywhere we use them
	var optionObjArray = selectObj.options;
	
	// change value back to original value
	for(var i=0; i<optionObjArray.length; i++)
	{
		if(optionObjArray[i].value == value)
		{
			return optionObjArray[i].text
		}
	}
}

// sets the value of a select box (identified by selectBoxId) to the option
// with value = value
function SetSelectBoxToIndexByValue(selectBoxId, value)
{
	var selectObj = document.getElementById(selectBoxId);
	
	// get the array of options so that we don't have to worry about changing status values everywhere we use them
	var optionObjArray = selectObj.options;
	
	// change value back to original value
	for(var i=0; i<optionObjArray.length; i++)
	{
		if(optionObjArray[i].value == value)
		{
			selectObj.selectedIndex = i;
			break;
		}
	}
}
