// JavaScript Document

var request;
var queryString; //will hold the POSTed data

/* Initialize a Request object that is already constructed
reqType: The HTTP request type such as "GET" or "POST." 
url: The URL of the server program.
isAsynch: Whether to send the request asynchronously or not. */
function initReq(reqType,url,isAsynch){
	/* Specify the function that will handle the HTTP response */
	request.onreadystatechange=handleResponse;
	request.open(reqType,url,isAsynch);
	/* set the Content-Type header for a POST request */
	request.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded; charset=UTF-8");
	request.send(queryString);
}

/* Wrapper function for constructing a Request object.
 Parameters:
 reqType: The HTTP request type such as GET or POST.
 url: The URL of the server program.
 asynch: Whether to send the request asynchronously or not. */

function httpRequest(reqType,url,asynch){
	//Mozilla-based browsers
	if(window.XMLHttpRequest) {
		request = new XMLHttpRequest();
		initReq(reqType,url,asynch);
	} else if (window.ActiveXObject) {
		request=new ActiveXObject("Msxml2.XMLHTTP");
		if (! request) {
			request=new ActiveXObject("Microsoft.XMLHTTP");
		}
		if(request) {
		initReq(reqType,url,asynch);
		/* Unlikely to branch here, as IE uses will be able to use either 
		one of the constructors*/
		} else {
		alert("Your browser does not permit the use of all "+"of this application's features!");}
	} else {
		alert("Your browser does not permit the use of all "+"of this application's features!");}
}


function sendData() {
	var url="ajax_process.php";
	httpRequest("POST",url,true);
}

//event handler for XMLHttpRequest
function handleResponse(){
	if(request.readyState == 4) {
		if(request.status == 200) {
			alert(request.responseText);
		} else {
			alert("A problem occurred with communicating between "+"the XMLHttpRequest object and the server program.");
		}
	}//end outer if
}



function setQueryString(div,ID){
	//initialize the top-level variable; also reset the variable to cover when
	//the user clicks multiple times
	
	var yes = confirm('You are about to submit your vote… are you sure you want to vote for this category ?');
	
	if(yes==true) {
		
		queryString="";
		UID="&UID="+ID; 
		var frm = document.getElementById(div);
		var numberElements = frm.elements.length;
		for(var i = 0; i < numberElements; i++) {
			if(frm[i].type=="radio") {
				if(frm[i].checked) {
					queryString = "vote="+frm.elements[i].value;
				}
	
			}
		}
		
		queryString += UID;
		
	}
	
	
	
}






