function checkVote() {
chosen = ""
len = document.rForm.rChoice.length ;
vTable = document.getElementById("voteTable").value;


for (i = 0; i <len; i++) {
if (document.rForm.rChoice[i].checked) {
chosen = document.rForm.rChoice[i].value
}
}

if (chosen == "") {
    //No choice selected.
    document.getElementById("snipMsg").innerHTML ="<h4>Nothing selected, try again.</h4>";
    return;
}
else {
var a = "WOCVote";
var b = getCookie(a);      //B is the cookie in string form
if(b!=""){
    document.getElementById("snipMsg").innerHTML ="<h4>You already voted for " + b + "</h4>";
}
//Recover database parameters
var dbList = "";
var n = "";
for (i=1; i<7; i++){
    n="ajax"+i;
    dbList = dbList + "&" + n + "=" + escape(document.getElementById(n).value);
}

dbList = dbList + " &Choice=" + chosen + "&questions=";
dbList = dbList.slice(1);

//Clears cookie for test.  Comment for production.****************************************
//setCookie(a,"");
//alert(chosen);
//alert (vTable);//***********************************************************************

//Generate the question list
questionArray = new Array();
var ques = "";
var q = document.getElementById("questionTot").value;
//alert(q);
for (i=0; i<q; i++){
    qNum = "question" + i;
   ques = ques + ","+ document.getElementById(qNum).value;
   questionArray[i]= document.getElementById(qNum).value;
}
ques = ques.slice(1);
dbList = dbList + ques;
//alert (dbList);
//Record the vote
ajaxFunction(dbList);
}
}

//Gets a cookie named "n" returns a cookie string.
function getCookie(n) {
  var search = n + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) {
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

//Writes a cookie named "n" to the document and adds "w" to it.
function setCookie(n,w){
var x = getCookie(n);
document.cookie= n+"="+w;
}

//This function sends and receives ajax data. Need to change variables in ajax message and php file url.  These are located in the fifth and sixth libes from the end.
//Browser Support Code
function ajaxFunction(passTo){
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert( "Sorry, but your browser does not support Ajax.  We cannot continue." );
				return false;
			}
		}
	}//End of browser catch function

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
            var ajaxDisplay = document.getElementById('formDiv');
            var tableData = ajaxRequest.responseText;
            //alert (tableData);
            var tableArray = tableData.split(",");
            var len = tableArray.length;
            var pollTable = "<table width='100%'><tbody>";
            var halfLen = len/2;

            //Build the table
            for (i=0; i<halfLen; i++){
            pollTable = pollTable + "<tr><td width='50%' style='min-height: 18px'>" + questionArray[i] + "</td><td width='50%' style='min-height: 16px'><div align='left' style='background-color: #FF0000; width: " + tableArray[i] + "%'><font color='#000000'><center>" + tableArray[i+halfLen] + "</center></font></div></td></tr>";
            }//End of for
            pollTable = pollTable + "</tbody></table>";
            //alert (pollTable);
            ajaxDisplay.innerHTML = pollTable;
            document.getElementById("snipMsg").innerHTML ="<h4>Thank you for voting!</h4>";
            setCookie("WOCVote","");
		}//End of response function
	}//End of ready state function

    //Set up ajax message
	ajaxRequest.open("POST", "storeVote.php", true);

    //Send the proper header information along with the request
    ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.send(passTo);
}//End of AJAX function
