//Poll component
var PollComponentInstances = new Array();
function PollComponent(){
	this.xmlHttp = null;
	this.url = "";
	this.pollCompId = null;
	PollComponentInstances.push(this);
}
PollComponent.prototype.Init = function(){
		if(this.pollCompId != null){
			this.CreateXmlHttp();
			
			var formObj = document.getElementById(this.pollCompId);
			if(formObj != null){
				
				formObj.setAttribute("obj", "PollComponent" + this.pollCompId);
				formObj.onsubmit = this.PostPoll;
			}
		}
	};
	PollComponent.prototype.CreateXmlHttp = function(){
		if(typeof XMLHttpRequest != "undefined"){
			this.xmlHttp = new XMLHttpRequest();
		}
		else if(typeof window.ActiveXObject != "undefined"){
			try{
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
			}
			catch(e){
				try{
					this.xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
				}
				catch(e){
					try{
						this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){
						this.xmlHttp = null;
					}
				}
			}
		}
	};
	PollComponent.prototype.ClearXmlHttp = function(){
		this.xmlHttp = null;
	};
	/*PollComponent.prototype.MakePollRequest = function(){
		this.xmlHttp.open("GET", this.url + "?id=" + this.pollCompId + "&__xsl=/templates/components/poll-results.xsl&showresults=yes&__toolbar=0", true);
		
		var obj = this;
		this.xmlHttp.onreadystatechange = function (){
			if(obj.xmlHttp && obj.xmlHttp.readyState == 4){
				obj.FetchContent();
			}
		}
		
		this.xmlHttp.send(null);
		
	};*/
	PollComponent.prototype.PostPoll = function(){
		var obj = eval(this.getAttribute("obj"));
		var formObj = this;
		
		var inputObjs = formObj.getElementsByTagName("input");
		var qStr = "?";
		for(var i=0;i<inputObjs.length;i++){
			if(inputObjs[i].name == "poll_choice"){
				if(inputObjs[i].checked)
				qStr += "poll_choice=" + inputObjs[i].value; 
			}
			else{
				qStr += "&" + inputObjs[i].name + "=" + inputObjs[i].value;
			}
		}
	
		obj.xmlHttp.open("GET", obj.url + qStr , true);
		
		obj.xmlHttp.onreadystatechange = function (){
			if(obj.xmlHttp && obj.xmlHttp.readyState == 4){
				obj.FetchContent();
				//obj.MakePollRequest();
			}
		}
		
		obj.xmlHttp.send(null);
		
		return false;
	};
	PollComponent.prototype.FetchContent = function(){
		document.getElementById(this.pollCompId).style.display = "none";
		document.getElementById("poll-result-content" + this.pollCompId).innerHTML = this.xmlHttp.responseText;
		
		//set cookie!
		var date = new Date();
		date.setTime(date.getTime()+(8*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		document.cookie = "poll-"+this.pollCompId+"=1"+expires+"; path=/";
		
		this.ClearXmlHttp();
	};

	function RunPollCollInits(){
		for(var i=0;i<PollComponentInstances.length;i++){
			PollComponentInstances[i].Init();
		}
	};

SafeOnload.AddFunction(RunPollCollInits);
