//* UW-Platteville Polling *//

var http_request = false;

function submitOpinion()
{
	changeContent(showPoll);
	var cookie_name = document.pollform.filename.value;
	cookie_name = cookie_name.replace(/.xml/, "");
	
	var check_cookie = getCookie(cookie_name);
	if(check_cookie == null)
	{
		document.getElementById("button_div").innerHTML = "<span>Please wait.</span>";
		document.getElementById("pollform").submit();
		setCookie(cookie_name,"ThanksForVoting",4);
	}
	else
	{
		changeContent(showResults);
	}
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
		{
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1)
			{ 
				c_start=c_start + c_name.length+1 
				c_end=document.cookie.indexOf(";",c_start)
				if (c_end==-1) c_end=document.cookie.length
				return unescape(document.cookie.substring(c_start,c_end))
			} 
		}
	return null
}

function setCookie(c_name,value,expirehours)
{
	var exdate=new Date()
	exdate.setTime(exdate.getTime()+(expirehours*3600*1000))
	document.cookie=c_name+ "=" +escape(value)+((expirehours==null) ? "" : "; expires="+exdate)
}


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function changeContent(function_call, disable)
{

   if (window.XMLHttpRequest) //non-Microsoft browser 
   { 
   	http_request = new XMLHttpRequest();
   }   
   else if (window.ActiveXObject) //IE
   {
   	try 
   	{
      	http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e) 
      {
      	try 
      	{
         	http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } 
         catch (e) {}
      }
	}
	
   if (!http_request) 
   {
      return false;
   }
   function_call+"("+disable+")";
   var file_name = document.pollform.filename.value;
   http_request.open('GET', file_name, true);
   http_request.onreadystatechange = function_call;
   http_request.send(null);
}

function showPoll()
{
	if(http_request.readyState == 4)
	{
		if(http_request.status == 200)
		{
   		var xmldoc = http_request.responseXML;
			document.getElementById("question").innerHTML = xmldoc.getElementsByTagName("question")[0].firstChild.nodeValue;
   		var display_data = "<ul>";   																									
   		var i = 0;																				
   		var option_name = xmldoc.getElementsByTagName("option")[i].getElementsByTagName("value")[0].firstChild.nodeValue;
   		display_data += '<li><label><input type="radio" name="poll" value="' + option_name + '" />' + option_name + '</label></li>';
			i++;																						
   		var loop_condition = xmldoc.getElementsByTagName("option").length;
			while(i < loop_condition)																
			{																							
				try																					
				{																						
					option_name = xmldoc.getElementsByTagName("option")[i++].getElementsByTagName("value")[0].firstChild.nodeValue;																				
				   display_data += '<li><label><input type="radio" name="poll" value="' + option_name + '" />' + option_name + '</label></li>';
				}																						
				catch(e)																				
				{																						
					loopCondition = false;														
				}
			}																							
			display_data += '</ul>';
			display_data += '<div id="button_div">';
			display_data += '<input type="button" class="button" id = "opinionSubmit" name="opinionSubmit" value="Submit Opinion" onclick="submitOpinion()"/><br />';
			display_data += '<input type="button" class="button" id="viewResults" name="viewResults" value="View Poll Results" onclick="changeContent(showResults)"/>';
			display_data += '</div>';
			document.getElementById("changingContent").innerHTML = display_data;
		}
	}
}

function showResults()
{
	if(http_request.readyState == 4)
	{
		if(http_request.status == 200)
		{
			
			var xmldoc = http_request.responseXML;
			document.getElementById("question").innerHTML = xmldoc.getElementsByTagName("question")[0].firstChild.nodeValue;
			var i = 0;
			var numParticipants = 0;
			var option_name = xmldoc.getElementsByTagName("option")[i].getElementsByTagName("value")[0].firstChild.nodeValue;
			var option_value = xmldoc.getElementsByTagName("option")[i].getElementsByTagName("votes")[0].firstChild.nodeValue;
			var display_data = "<ul><li><span>" + option_value + "</span>"  + option_name + "</li>";
			numParticipants += parseInt(option_value);
			i++;
			var loop_condition = xmldoc.getElementsByTagName("option").length;
			while(i < loop_condition)
			{
				try
				{
					option_name = xmldoc.getElementsByTagName("option")[i].getElementsByTagName("value")[0].firstChild.nodeValue;
					option_value = xmldoc.getElementsByTagName("option")[i].getElementsByTagName("votes")[0].firstChild.nodeValue;
					i++;
					numParticipants += parseInt(option_value);
					display_data += "<li><span>" + option_value + "</span>" + option_name + "</li>";
				}
				catch(e)
				{
					loopCondition = false;
				}
			}
			display_data += '<li class="last"><span>' + numParticipants + '</span>' + ' people have taken this poll.</li>';
			display_data += "</ul>";
			
			
			display_data += '<div id="button_div">';
			display_data += '<input type="button" class="button" id="viewOptions" name="viewOptions" value="Return to Survey" onclick="changeContent(showPoll)"/>';
			display_data += '</div>';
			document.getElementById("changingContent").innerHTML = display_data;
		}
	}
}

function makePoll(xmlName)
{
	document.write('<form action="https://cgi.uwplatt.edu/cgi-bin/UWP/poll/poll.pl" id="pollform" name="pollform" method="post">');
	document.write('<fieldset>');
	document.write('<h3 id="question"></h3>');
	document.write('<input type="hidden" id="filename" name="filename" value=' + xmlName + ' />');
	document.write('<div id="changingContent"></div>');
	document.write('</fieldset>');
	document.write('</form>');
	var my_url = document.URL;
	if (my_url.indexOf('?results=results') != -1)
		changeContent(showResults);
	else
		changeContent(showPoll);
}

document.write('<style type="text/css">');
document.write('div#changingContent ul {padding:0;margin:0;}');
document.write('div#changingContent ul li {list-style-type:none;}');
document.write('div#changingContent input.button {width:125px;}');
document.write('div#changingContent ul li span {font-weight:bold;width:2em;padding:0 .5em;color:#f00;}');
document.write('div#button_div {text-align:center;}');
document.write('div#button_div span {font-weight:bold;font-size:2em;}');
document.write('li.last{border-top-style:solid;}');
document.write('</style>');