var poll_enableDebug = false;

function poll_send(postData) {
	var pollRequest = createXMLHttpRequest();

	pollRequest.open("POST", "index.php");
	pollRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	pollRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	// kts. http://www.alistapart.com/articles/getoutbindingsituations
	pollRequest.onreadystatechange=common_createBoundedWrapper(pollRequest, poll_readyStateChange);

	debugPrint("postData "+postData, "poll");
	pollRequest.send(postData);
	return false;
}

function poll_action(action, id) {
	var postData = "action=poll&poll-action="+action;
	postData += "&poll-id="+id;
	poll_send(postData);
	return false;
}

function poll_vote(form) {
/*-
	var pollRequest = createXMLHttpRequest();

	pollRequest.open("POST", "index.php");
	pollRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	pollRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	// kts. http://www.alistapart.com/articles/getoutbindingsituations
	pollRequest.onreadystatechange=poll_createBoundedWrapper(pollRequest, poll_readyStateChange);
*/
	var postData = "action=poll&poll-action=vote";
	postData += "&poll-id="+form.elements['poll-id'].value;
	var choices = form.elements['poll-choice'];
	if(choices) {
		for(var i=0; i < choices.length; i++) {
			if(choices[i].checked) {
				postData += "&poll-choice[]=" + choices[i].value;
			}
		}
	}
/*
	debugPrint("postData "+postData, "poll");
	pollRequest.send(postData);
*/

	poll_send(postData);
	return false;
}

function poll_readyStateChange() {
	debugPrint("readystate: this="+this+" this.readyState="+this.readyState, "poll");
	if(this.readyState == 4) {
		debugPrint("status: "+this.status, "poll");

		if(this.responseText) {
			try {
				var response = eval('(' + this.responseText + ')');
			} catch(err) {
				json_handleEvalError(this, err);
				return;
			}

			if(response.errors) {
				var msg = response.errors.join("\n");
				if(msg) alert(msg);
			}

			if(response.replacements) {
				for(index in response.replacements) {
					var el = document.getElementById(index);
					if(el) el.innerHTML = response.replacements[index];
				}
			}
			if(response.messages) {
				var msg = response.messages.join("\n");
				if(msg) alert(msg);
			}
			if(response.checkCookie && response.pollId) {
				// on äänestetty. Lähetetään tarkistusrequesti menikö cookie perille. Ääni rekisteröidään vasta tarkistuksen mentyä läpi.
				poll_confirmVote(response.pollId, response.checkCookie);
			}
			if(response.reload) poll_action("view", response.pollId);
		}
	}
}

function poll_confirmVote(pollId, cookieValue) {
/*
	var pollRequest = createXMLHttpRequest();

	pollRequest.open("POST", "index.php");
	pollRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	pollRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	// kts. http://www.alistapart.com/articles/getoutbindingsituations
	pollRequest.onreadystatechange=poll_createBoundedWrapper(pollRequest, poll_readyStateChange);
*/
	var postData = "action=poll&poll-action=confirmVote";
	postData += "&poll-id="+pollId;
	postData += "&poll-cookie="+cookieValue;
/*
	debugPrint("postData "+postData, "poll");
//	alert("confirm: "+postData);

	pollRequest.send(postData);
*/
	poll_send(postData);
	return false;
}

function poll_removeCookie(form) {
/*
	var pollRequest = createXMLHttpRequest();

	pollRequest.open("POST", "index.php");
	pollRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	pollRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	// kts. http://www.alistapart.com/articles/getoutbindingsituations
	pollRequest.onreadystatechange=poll_createBoundedWrapper(pollRequest, poll_readyStateChange);
*/
	var postData = "action=poll&poll-action=removeCookie";
	postData += "&poll-id="+form.elements['poll-id'].value;
/*
	debugPrint("postData "+postData, "poll");
	pollRequest.send(postData);
*/
	poll_send(postData);
	return false;
}

function poll_reload(id) {
/*
	var pollRequest = createXMLHttpRequest();

	pollRequest.open("POST", "index.php");
	pollRequest.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	pollRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

	// kts. http://www.alistapart.com/articles/getoutbindingsituations
	pollRequest.onreadystatechange=poll_createBoundedWrapper(pollRequest, poll_readyStateChange);
*/
	var postData = "action=poll&poll-action=view";
	postData += "&poll-id="+id;
/*
	debugPrint("postData "+postData, "poll");
	pollRequest.send(postData);
*/
	poll_send(postData);
	return false;
}

