/*
 * Author:		Earl W. Foote (Schoktra)
 * Filename:	poll.js
 * Content:		poll javascript functions
 * Copyright:	2009 Earl W. Foote
 * Project:		EP-Dev PollEase
 * License:		LGPLv3
 * Creation:	February 28, 2009 00:07 US/Central Time
 * Last update:	March 7, 2009 17:12 US/Central Time
 * Description:	A polling script that is easy to install
 * 				and use in any existing web application.
 * Features:	<Coming Soon>
 *
 */

// Create a new poll object
pollObj = new Object();

handleURL = "plugins/PollEase/handle.php";
/*
 * Returns a pageRequest object that should be of an
 * XML type.
 * 
 * @return pageRequest;
 */
pollObj.getPage = function ()
{
	var pageRequest = false;

	// If this value is true we create this object.
	if(window.XMLHttpRequest)
	{
		pageRequest = new XMLHttpRequest();
	}
	// Otherwise, we check for this to be true
	else if(window.ActiveXObject)
	{
		pageRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return pageRequest;
}

/**
 * Displays the poll the when the visitor first
 * comes to the page. Accepts a string poll name
 * parameter.
 * 
 * @param pollName
 */
pollObj.display = function (pollName)
{
	var pageRequest = pollObj.getPage();

	/*
	 * If we successfully returned our page object
	 * create a url with the specified get parameters,
	 * open the page, and display the conent to the
	 * browser.
	 */
	if (pageRequest)
	{
		var sendParams = "?action=display&base=" + pollName;
		var url = "plugins/PollEase/handle.php" + sendParams;

		pageRequest.open('GET',url,false);
		pageRequest.send(null);

		document.write("<form onsubmit=\"return pollObj.submit('" + pollName + "');\">\n");
		document.write(pageRequest.responseText);
		document.write("</form>\n");
	}	
}

pollObj.repDiv = function (pollName,url)
{
	var pageRequest = pollObj.getPage();
	var oldDiv = document.getElementById(pollName);
	var form = oldDiv.parentNode;
	var newDiv = "";

	if (pageRequest)
	{
		pageRequest.open('GET',url,false);
		pageRequest.send(null);

		newDiv = pageRequest.responseText;
	}
	else
	{
		newDiv = "<div><h1>Error getting page request</h1></div>";
	}

	form.innerHTML = newDiv;
}

pollObj.results = function (pollName)
{
	var sendParams = "?action=result&base=" + pollName;
	var url = handleURL + sendParams;

	pollObj.repDiv(pollName,url);

	return false;
}

pollObj.submit = function (pollName)
{
	var pageRequest = pollObj.getPage();

	/*
	 * If we successfully create the page object, we
	 * create the base of our get parameters, use our
	 * div object to find the parenting form object,
	 * find the choice selected to finish out the get
	 * parameters, create the url necessary, try to
	 * open it, and create new content from the value
	 * returned, and change the inside of the form to
	 * match the new content so the results are shown
	 * after the vote is cast without refresshing the
	 * page.
	 */
	if (pageRequest)
	{
		var sendParams = "?action=addVote&base=" + pollName + "&choice=";
		var form = document.getElementById(pollName).parentNode;
		/*
		 * Loop through the elements to find which one is a radio
		 * button that is checked, we assign this value to the
		 * send parameters of the url.
		 */
		for (var i=0; i<form.elements.length-1;i++)
		{
			if (form.elements[i].type == "radio" && form.elements[i].checked)
				sendParams += form.elements[i].value;
		}

		var url = "plugins/PollEase/handle.php" + sendParams;

		pollObj.repDiv(pollName,url);
	}

	// Return false so that the browser will not try
	// to actually submit the form.
	return false;
}
