function Poll () {

	this.poll_id=-1;
	this.poll_mode="radio";
	this.poll_display_mode="";
	this.poll_selected_value=-1;
	this.poll_max_options=5;

	this.setPollId = function(i) {
		this.poll_id=i;
	}
	
	this.getPollId = function() {
		return this.poll_id;
	}

	this.findAndSetSelectedValue = function (pollChoice) {
		// pollChoice refers to the field object from the frmPost form
		if (this.poll_mode=="radio") {
			// radio buttons, so run through the radiobutton list - it should be a nodeList in pollChoice
			for(i=0;i<pollChoice.length;i++) {
				if (pollChoice[i].checked) {					
					this.poll_selected_value=pollChoice[i].value;
				}
			}
		}
	}

	this.submitPoll = function() {
		// get choice value from form
		if (this.poll_id==-1) {
			alert('No poll selected');
		} else {
			// get the field object in poll choice
			pollChoice=eval("document.frmPoll_" + this.poll_id+".poll_choice");
			this.poll_display_mode=eval("document.frmPoll_" + this.poll_id+".poll_display_mode").value;
			if (typeof(pollChoice)=="undefined") {
				alert("Couldn't get poll value");
			} else {
				// findAndSetSelectedValue will set the poll_selected_value property
				this.findAndSetSelectedValue(pollChoice)
				// make an ajax call to store data
				this.submitCall();
			}
		}
	}
	
	this.goBack = function() {
		// go back to questions
		if (this.poll_id==-1) {
			alert('No poll selected');
		} else {
			sUrl="main.php?poll_id="+this.poll_id+"&name=poll&poll_display_mode="+this.poll_display_mode;
			// we can use the submitCallback for displaying the poll
			simpleRequest(sUrl,this.submitCallback,this.poll_id);
		}
	}
	
	this.submitCall = function () {
		// call poll template to register vote
		sUrl="main.php?poll_id="+this.poll_id+"&name=poll&poll_display_mode="+this.poll_display_mode+"&poll_choice=" + this.poll_selected_value;
		simpleRequest(sUrl,this.submitCallback,this.poll_id);
	}
	
	this.submitCallback = function(xmlhttp,poll_id) {
		// vote is registered, now, if applicable, paste the text of the results into the container div
		sResult=xmlhttp.responseText;
		this.poll_id=poll_id;
		pollContainer=document.getElementById("poll_container_" + poll_id);
		if (typeof(pollContainer)!="undefined") {
			pollContainer.innerHTML=sResult;
		}
	}
	
	this.displayResults = function () {
		// call poll template for results
		sUrl="main.php?poll_id="+this.poll_id+"&name=poll&poll_action=display_results&poll_display_mode="+this.poll_display_mode;
		simpleRequest(sUrl,this.displayResultsCallBack,this.poll_id);
	}
	
	this.displayResultsCallBack = function(xmlhttp,poll_id) {
		// f applicable, paste the text of the results into the container div
		sResult=xmlhttp.responseText;
		this.poll_id=poll_id;
		pollContainer=document.getElementById("poll_container_" + poll_id);
		if (typeof(pollContainer)!="undefined") {
			pollContainer.innerHTML=sResult;
		}
	}
	
	this.mouseOverOption = function (iOption,sMode) {		
		// set all to default:
		for(i=1;i<=this.poll_max_options;i++) {
			try {
				oDefault=document.getElementById("poll_" + this.poll_id +"_choice_default_" + i);
				oMouseover=document.getElementById("poll_" + this.poll_id +"_choice_mouse_over_" + i);
				oDefault.style.display='block';
				oMouseover.style.display='none';
			} catch(e) {
				// probably, this one wasn't defined.
			}			
		}
		
		oDefault=document.getElementById("poll_" + this.poll_id +"_choice_default_" + iOption);
		if (typeof(oDefault)=="undefined") {
			alert('No default element found.');
			return;
		}		
		oMouseover=document.getElementById("poll_" + this.poll_id +"_choice_mouse_over_" + iOption);
		if (typeof(oMouseover)=="undefined") {
			alert('No mouse over element found.');
			return;
		}
		
		if (sMode=="mouse_over") {
			// hide the default text and display the full option
			//oDefault.style.display='none';
			oMouseover.style.display='block';
		} else if (sMode=="default") {
			oMouseover.style.display='none';
			//oDefault.style.display='block';
		}
	}

}

// build a global poll variable
var oPoll = new Poll();


