

//When loaded Jquery, update poll results
$(document).ready(function(){
	//updateResults();
});


function submit_vote(){
	if(get_radio_value() == undefined){
		alert('You must select a choice before submitting a vote.');
	} else {
		sendVote();
	}
}


function sendVote(){

	poll_id = document.poll_vote.poll_id.value;
	answer_id = get_radio_value();

	//document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
	//Submits vote to server
	var ajaxRequest = ajaxFunction()
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){

			var date = new Date();
			days = 365;
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = 'pollvoting_'+ poll_id +'='+ answer_id + expires +'; path=/'

			document.getElementById('polls_results_container').innerHTML = ajaxRequest.responseText;

			document.getElementById('polls').style.display = "none";
			document.getElementById('polls_results').style.display = "block";
			document.getElementById('polls_nav_vote').style.display = "none";
			document.getElementById('polls_nav_view').style.display = "none";
		}
	}

	var url='index.php?page=polls&action=submitvote&ajax=1&poll_id='+poll_id+'&ans_id='+answer_id;
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null);	

}


function updateResults() {

	full_width = document.getElementById("polls_results").offsetWidth;

	for (var i=0; i < poll_id.length; i++){
		new_width = full_width * poll_percentages[i];
		//alert(new_width);
		element_name = "polls_answer_" + poll_id[i];
		//document.getElementById(element_name).style.width = parseInt(new_width) + "px";
		percent = poll_percentages[i] * 100;
		document.getElementById(element_name).style.width = parseInt(percent) + "%";
	}

}

function showViewLink(){
	document.getElementById('polls_nav_vote').style.display = "none";
	document.getElementById('polls_nav_view').style.display = "block";
	document.getElementById('polls').style.display = "block";
	document.getElementById('polls_results').style.display = "none";
}

function showVoteLink(){
	document.getElementById('polls_nav_vote').style.display = "block";
	document.getElementById('polls_nav_view').style.display = "none";
	document.getElementById('polls').style.display = "none";
	document.getElementById('polls_results').style.display = "block";
}


function get_radio_value(){
	form_element = document.poll_vote.poll;
	for (var i=0; i < form_element.length; i++){
	   if (form_element[i].checked){
			  var rad_val = form_element[i].value;
		}
	}
	return rad_val;
}


//Determines which ajax request to use
function ajaxFunction(method){
	var ajaxVariable;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxVariable = new XMLHttpRequest();
		//Overide if using POST method
		if (method == 'post') {
			if (ajaxVariable.overrideMimeType) {
         	// set type accordingly to anticipated content type
				ajaxVariable.overrideMimeType('text/html');
			}
		}
		//alert("Opera 8.0+, Firefox, Safari");
	} catch (e){
		// Internet Explorer Browsers :(
		try{
			ajaxVariable = new ActiveXObject("Msxml2.XMLHTTP");
							//alert("Explorer 2");
		} catch (e) {
			try{
				ajaxVariable = new ActiveXObject("Microsoft.XMLHTTP");
								//alert("Explorer 1");
			} catch (e){
				// Something went wrong
				alert("XMLHttp is not supported.");
				ajaxVariable = false;
			}
		}
	}
	return ajaxVariable;
}
