var Axmlhttp=new XMLHttpRequest();
var Bxmlhttp=new XMLHttpRequest();
var last_id;
var pollId;

function poll()
{
	for (index=0; index < document.frmPoll.Option.length; index++) {
		if (document.frmPoll.Option[index].checked) {
			x = index + 1;
			eval('document.frmPoll.Option_' + x +"_Count.value = 1");	
		}
		else {
			x = index + 1;
			eval('document.frmPoll.Option_' + x +"_Count.value = 0");
		}			
	}
} 

//Get new posts after adding new discussion
function update_poll() {
	Axmlhttp=GetXmlHttpObject();
	if (Axmlhttp==null)  {
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	
	var op1 = 0;
	var op2 = 0;
	var op3 = 0;
	var op4 = 0;
	var op5 = 0;
	var op6 = 0;
	var op7 = 0;
	var op8 = 0;
	var op9 = 0;
	var op10 = 0;	
	pollId = document.getElementById("Poll_ID").value;
	
	if(document.getElementById("Option_1_Count")) {
		op1 = document.getElementById("Option_1_Count").value;
	}
	if(document.getElementById("Option_2_Count")) {
		op2 = document.getElementById("Option_2_Count").value;
	}
	if(document.getElementById("Option_3_Count")) {
		op3 = document.getElementById("Option_3_Count").value;
	}
	if(document.getElementById("Option_4_Count")) {
		op4 = document.getElementById("Option_4_Count").value;
	}
	if(document.getElementById("Option_5_Count")) {
		op5 = document.getElementById("Option_5_Count").value;
	}
	if(document.getElementById("Option_6_Count")) {
		op6 = document.getElementById("Option_6_Count").value;
	}
	if(document.getElementById("Option_7_Count")) {
		op7 = document.getElementById("Option_7_Count").value;
	}
	if(document.getElementById("Option_8_Count")) {
		op8 = document.getElementById("Option_8_Count").value;
	}
	if(document.getElementById("Option_9_Count")) {
		op9 = document.getElementById("Option_9_Count").value;
	}
	if(document.getElementById("Option_10_Count")) {
		op10 = document.getElementById("Option_10_Count").value;
	}
	
	x = document.getElementById('poll_question').height;
	document.getElementById('divVote').innerHTML = '<div align="center"><img src="Assets/loading1.gif" /></div>'; 
	document.getElementById('poll_question').height = x;
	
	var url="code_Polls-submit_poll.php"; 
	url=url+"?op1="+op1+"&op2="+op2+"&op3="+op3+"&op4="+op4+"&op5="+op5+"&op6="+op6+"&op7="+op7+"&op8="+op8+"&op9="+op9+"&op10="+op10+"&pollId"+pollId+"&sid="+Math.random();
	Axmlhttp.onreadystatechange=stateChanged_update_poll; 
	Axmlhttp.open("GET",url,true);
	Axmlhttp.send(null);
}

function stateChanged_update_poll() {
	if (Axmlhttp.readyState==4 && Axmlhttp.status == 200)  {
		get_poll(pollId)
	} 
}

//Get new posts after adding new discussion
function get_poll(id) {
	Bxmlhttp=GetXmlHttpObject();
	if (Bxmlhttp==null)  {
	  alert ("Your browser does not support XMLHTTP!");
	  return;
	}
	
	document.getElementById('poll_result').innerHTML = '<div align="center"><img src="Assets/loading1.gif" /></div>';
	
	y = document.getElementById(id);
	y.removeAttribute("href");
	y.className = 'bold';
		
	
	if(document.getElementById(last_id)) {
		y = document.getElementById(last_id);
		z = "javascript:get_poll('" + last_id + "')";
		y.setAttribute('href',z);
		y.className = '';
	}
		
	last_id = id;	
	
	var url="code_Polls-get_poll_results.php"; 
	url=url+"?id="+id+"&sid="+Math.random();
	Bxmlhttp.onreadystatechange=stateChanged_get_poll; 
	Bxmlhttp.open("GET",url,true);
	Bxmlhttp.send(null);
}

function stateChanged_get_poll() {
	if (Bxmlhttp.readyState==4 && Bxmlhttp.status == 200)  {
		document.getElementById('poll_result').innerHTML = Bxmlhttp.responseText;	
		document.getElementById('poll_question').style.display = 'none';
		document.getElementById('poll_result').style.display = 'table-cell';
	} 
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest)  {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)  {
	  // code for IE6, IE5
	  return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


