function getHTTPObject()
{
	var xhr = false;
	if (window.XMLHttpRequest)
	{
		xhr = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				xhr = false;
			}
		}
	}
	return xhr;
}

function grabFile(loc)
{
	var request = getHTTPObject();
	if (request)
	{
		request.onreadystatechange = function()
		{
			parseResponse(request);
		};
		request.open("GET", loc, true);
		request.send(null);
	}
}

function parseResponse(request)
{
	if (request.readyState == 4)
	// Probably redundant, since it's not called by parsePoll until readyState = 4.
	{
		if (request.status == 200 || request.status == 304)
		{
			var responseObj = eval('(' + request.responseText + ')');
			var errors = responseObj.errors;
			if (typeof errors != "undefined")
			{
				var errorAlert = "We have an error or two:\n";
				for (i = 0; i < errors.length; i++)
				{
					errorAlert += errors[i].error + "\n";
				}
				alert (errorAlert);
				return false;
			} else
			{
				return responseObj;
			}
		}
		else
		{
			alert("Cannot reach data: please try again later.");
			return false;
		}
	}
}
/*
TODO: add flexibility to structuring of data returned.
Structure has to predetermined atm. Can this be passed in as an array? such as:

("tag:li","objText:optionName","tag:b","objText:optionVotes","text:%","tag:span","func:classFromVal/optionVotes","parent:poll_results_list")

cos if it could, that'd be super hot.

Would reverse polish notation help me here? (Just a random idea I had over the weekend...)

Needs some other instructions, maybe.
createTag
createText
append
addClass
...that kind of thing. Definitely needs fleshing out.
*/
function parsePoll(request)
{
	if (request.readyState == 4)
	{
		if (request.status == 200 || request.status == 304)
		{
			pollResultsObj = parseResponse(request);
			var lis = pollResultsObj.poll_results;
			if (typeof pollResultsObj == "object")
			{
				pollResultsList = document.getElementById("poll_results_list");
				while (pollResultsList.childNodes.length > 0)
				{
					pollResultsList.removeChild(pollResultsList.childNodes[0]);
				}
				for (i = 0; i < lis.length; i++)
				{
					newListItem = document.createElement("li");
					newPollValue = document.createTextNode(lis[i].optionName + " ");
					newListItem.appendChild(newPollValue);
					newBoldText = document.createElement("b");
					percentage = lis[i].optionVotes.toString();
					newBoldText.appendChild(document.createTextNode(percentage + "%"));
					newSpan = document.createElement("span");
					percentClass = ("oo" + percentage).substring(percentage.length-1,percentage.length+2);
					newSpan.className = percentClass;
					newBoldText.appendChild(newSpan);
					newListItem.appendChild(newBoldText);
					document.getElementById("poll_results_list").appendChild(newListItem);
				}
			}
		} else
		{
			alert("Error retrieving content. Please vote later when bogosity levels have decreased.");
			return false;
		}
	} else
	{
		// show "Loading..." image here, or something.
		// Suggestion: create layer over existing layer, which is specified.
		// Created layer will be given the same height and width of the specified layer,
		// and take on a specified class, allowing us to pick from a handful of
		// "loading alert" styles for different areas on the page.
if(request.readyState == 0) { alertText = "Sending Request."; }
if(request.readyState == 1) { alertText = "Loading content."; }
if(request.readyState == 2) { alertText = "Content loaded."; }
if(request.readyState == 3) { alertText = "Almost done."; }
		alert("Wait! " + alertText);
	}
}

function ajaxPoll()
{
	if ((document.getElementById) && (document.getElementsByTagName))
	{
		ajaxObj = getHTTPObject();
		if (ajaxObj != false)
		{
			theForm = document.forms["POLL_ID"];
			for (i=0;i<theForm.elements["poll_response"].length;i++)
			{
				if (theForm.elements["poll_response"][i].checked)
				{
					poll_response = theForm.elements["poll_response"][i].value;
				}
			}
			ajaxObj.onreadystatechange = function()
			{
				parsePoll(ajaxObj);
			};
			ajaxObj.open("GET", "/handlers/ajax/handler.php", true);
			ajaxObj.send(null);
			
			return false;
		}
	}
}
