
/*
 * Renders a poll ajax-style, whose state is appropriate for the client browser.
 */
 function renderPoll() {
 	try {
 		// Get an AJAX object
 		var ajaxRequest = getAjaxObject(pollUrl, "");
 		
 		// send the request
 		ajaxRequest.send("");
 		
 		// prevent default from occuring
 		return false;
 	}
 	catch (exception) {
 		alert(exception);
 	}
 }
 
/* 
 * Submits a poll ajax-style, and replaces that poll form with the poll's results page.
 * Requires: previous definition of script var "containerId" (the ID attibute of the poll form's container element.)
 * Requires: previous definition of script var "formId" (the ID attribute of the poll form)
 * Required: previous definition of script var "submitId" (the ID attribute of the poll form's submit button)
 */
function submitPoll() {

	// Get the form that was submitted
	var theForm = document.getElementById(formId);
	
	// Make sure the form has an action specified
	var theAction = theForm.getAttribute("action");

	// Build the submitted data
	var submittedData = getSubmittedData(theForm);

	// Get an AJAX object
	var ajaxRequest = getAjaxObject(theAction, submittedData);

	// send the request.
	ajaxRequest.send(submittedData);

	// prevent default from occuring
	return false;
}

/*
 * Retrieves a (ready-to-use) ajax object.
 */
function getAjaxObject(action, data) {
	var ajaxObject = null;
	if(window.XMLHttpRequest) ajaxObject = new XMLHttpRequest();
	else if(new ActiveXObject('Msxml2.XMLHTTP')) ajaxObject = new ActiveXObject('Msxml2.XMLHTTP');
	else if(new ActiveXObject('Microsoft.XMLHTTP')) ajaxObject = new ActiveXObject('Microsoft.XMLHTTP');
	ajaxObject.open("POST", action, true);
	ajaxObject.onreadystatechange = function() { 
		if(ajaxObject.readyState==4) {
			displayResults(ajaxObject.responseText);
		}
	}
	ajaxObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxObject.setRequestHeader("Content-length", data.length);
	ajaxObject.setRequestHeader("Connection", "close");
	return ajaxObject;
}

/*
 * Callback function to display results.
 */
function displayResults(response) {
	try {
		document.getElementById(containerId).innerHTML = response;
	}
	catch (exception) {
		document.getElementById(parentContainerId).innerHTML = response;
	}
}


/*
 * Retrieves the data expected by the poll handler.
 */
function getSubmittedData(theForm) {
	var theData = "";
	for (i=0; i<theForm.childNodes.length; i++) {
		if (theForm.childNodes[i].tagName == "INPUT") {
			if (theForm.childNodes[i].type == "text"  ||  theForm.childNodes[i].type == "hidden") {
				theData += theForm.childNodes[i].name + "=" + theForm.childNodes[i].value + "&";
			}
			if (theForm.childNodes[i].type == "checkbox") {
				if (theForm.childNodes[i].checked) {
					theData += theForm.childNodes[i].name + "=" + theForm.childNodes[i].value + "&";
				} 
				else {
					theData += theForm.childNodes[i].name + "=&";
				}
			}
			if (theForm.childNodes[i].type == "radio") {
				if (theForm.childNodes[i].checked) {
					theData += theForm.childNodes[i].name + "=" + theForm.childNodes[i].value + "&";
				}
			}
		}   
		if (theForm.childNodes[i].tagName == "SELECT") {
			var sel = theForm.childNodes[i];
			theData += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
		}
	}
	return theData;
}

/*
 * Retrieves a poll's results without requiring user input.
 */
function getResults() {
	document.getElementById(formId).saveRMI.value='false';
	document.getElementById(formId).defaultResponse.checked=true;
	return submitPoll();
}
