var FanPoll = {};
FanPoll = function(PollId, OptionsArray) {
	this.elmSubmitBtn;
	this.Answers = OptionsArray;
	this.PollId = PollId;
	this.Initialize();
}

FanPoll.prototype = {
	Initialize : function () {
		this.elmSubmitBtn   = document.getElementById("fanPollSubmit");

		//WireUp Functions
		var pointer = this;
		this.elmSubmitBtn.onclick = function () { return pointer.elmSubmitBtn_OnClick(); };
	},
	elmSubmitBtn_OnClick : function () {
		var OkToContinue = false;
		var SelectedValue;
		for(i = 0; i < this.Answers.length; i++){
			var OptionItem = document.getElementById(this.Answers[i]);
			if(OptionItem.checked){
				SelectedValue = OptionItem.value;
				OkToContinue = true;
			}
		}
		if(OkToContinue){
			var pointer = this;
			var InsertAndGetResults = new Ajax();
			InsertAndGetResults.makeRequest(
					'updatepoll.php?pollid=' + this.PollId + '&answerid=' + SelectedValue, 
					'GET', '', 
					function(req) { pointer.Success(req); }, 
					function() {pointer.Failure();} );
		} else {
			alert("Please select an answer before submitting the poll.");
		}
	},
	Success : function(req) {
		document.getElementById("fanPollContainer").innerHTML = req.responseText;
	},
	Failure : function () {
	}
}

// AJAX PROTOTYPE

if (typeof(Ajax) == "undefined")
	Ajax = {};

Ajax = function (){
	this.req = {};
}

Ajax.prototype.makeRequest = function (url, meth, params, onComp, onErr){
	if (meth != "POST")
		meth = "GET";
	
	this.onComplete = onComp;
	this.onError = onErr;
	
	var pointer = this;
	
	// branch for native XMLHttpRequest object
	if (window.XMLHttpRequest)
		this.req = new XMLHttpRequest();
	// branch for IE/Windows ActiveX version
	else if (window.ActiveXObject)
		this.req = new ActiveXObject("Microsoft.XMLHTTP");

	if (this.req)	{
		this.req.onreadystatechange = function () { pointer.processReqChange() };
		this.req.open(meth, url, true);
		this.req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
		this.req.send((params) ? params : " ");
	}
}

Ajax.prototype.processReqChange = function(){
	// only if req shows "loaded"
	if (this.req.readyState == 4)
		// only if "OK"
		if (this.req.status == 200)
			this.onComplete( this.req );
		else
			this.onError( this.req );
}