// JavaScript Document
function submitPoll() {
	
	// argument formId can be the id or name attribute value of the
// HTML form, or an HTML form object.



var callback =
	{
	  success: function(o) {
		  			var poll_container = document.getElementById("poll_container");
					//document.cookie="poll_id="+document.getElementById("poll_id").value+";path=/";
		  			poll_container.innerHTML = o.responseText;
					
					
					
		  		},
	  failure: function(o) {/*failure handler code*/},
	  timeout: 5000
	}
	
	var qsParam = new Array();
	var query = window.location.search.substring(1);
	var params = query.split('&');
	for(var i=0; i<params.length;i++){
		var pos = params[i].indexOf('=');
		if(pos>0){
			var key = params[i].substring(0,pos);
			var value = params[i].substring(pos+1);
			qsParam[key]=value;
		}
	}
	
	
	var formObject = document.getElementById('poll_form');
	YAHOO.util.Connect.setForm(formObject);
	//alert(document.getElementById("poll_id").value);
	document.cookie="poll_id="+document.getElementById("poll_id").value+'_'+qsParam['a']+";path=/";
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/common/scripts/poll_control.php', callback, 'a='+qsParam['a']);


	return false;
}

function validateForm(thisform) {
	// place any other field validations that you require here
	// validate myradiobuttons
	myOption = -1;
	for (i=thisform.poll_option.length-1; i > -1; i--) {
		if (thisform.poll_option[i].checked) {
			myOption = i; 
			i = -1;
		}
	}
	if (myOption == -1) {
		alert("You must select a radio button");
		return false;
	}
	
	//alert("You selected button number " + myOption + " which has a value of " + thisform.poll_option[myOption].value);

	// place any other field validations that you require here
	submitPoll(); // this line submits the form after validation
}

	



function viewTally(poll_id) {
	
	// argument formId can be the id or name attribute value of the
// HTML form, or an HTML form object.

var callback =
	{
	  success: function(o) {
		  			var poll_container = document.getElementById("poll_container");
		  			poll_container.innerHTML = o.responseText;
					
					
		  		},
	  failure: function(o) {/*failure handler code*/},
	  timeout: 5000
	}
	
	
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/common/scripts/poll_control.php', callback, "poll_id="+poll_id+"&view_tally=1");


	return false;
}

function viewPoll(poll_id) {
	
	// argument formId can be the id or name attribute value of the
// HTML form, or an HTML form object.

var callback =
	{
	  success: function(o) {
		  			var poll_container = document.getElementById("poll_container");
		  			poll_container.innerHTML = o.responseText;
					
					
		  		},
	  failure: function(o) {/*failure handler code*/},
	  timeout: 5000
	}
	
	
	var cObj = YAHOO.util.Connect.asyncRequest('POST', '/common/scripts/poll_control.php', callback, "poll_id="+poll_id+"&view_poll=1");


	return false;
}

/***********************************************************/
/***** Handle onpening and closing of archived polls  *****/


function expandPoll(poll_id, controller){
	
	//var region = YAHOO.util.Dom.getRegion(synopsis_id);
	//var synopsis_height = region.bottom - region.top;
	controller.onclick=function(){shrinkPoll(poll_id, controller);};
	 //YAHOO.util.Dom.setStyle(article_id, 'display', 'block'); 
	 
	 for(var i =0; i<result_heights.length; i++){
			if (result_heights[i].id == poll_id){
				var height = parseInt(result_heights[i].height);
				if(BrowserDetect.browser=="Explorer" && BrowserDetect.version<=6) 			
					height += 10;
				break;
			}
	 }
	 
	 
	
	
	
	
	
    var anim = new YAHOO.util.Anim(poll_id, {height: { from: 0, to: height }}, 1.0, YAHOO.util.Easing.easeBothStrong);
	anim.onComplete.subscribe(resetContentBackground);
	anim.animate(); 
	
}

function shrinkPoll(poll_id, controller){
	
	
	controller.onclick=function(){expandPoll(poll_id, controller);};
	
	var anim = new YAHOO.util.Anim(poll_id, {height: { to:0 }}, 1.0, YAHOO.util.Easing.easeBothStrong);
	anim.onComplete.subscribe(resetContentBackground);
	anim.animate(); 
	
	
}

var result_heights = new Array()
function init() { 
		var ec= new Array();
		ec = YAHOO.util.Dom.getElementsByClassName('archive_poll_results');
		
			for (var i=0; i<ec.length; i++){
				
				var id = ec[i].id;
				var region = YAHOO.util.Dom.getRegion(ec[i]);
				result_heights.push( {id:ec[i].id, height: (region.bottom-region.top)}); 
				
			}
				
	   YAHOO.util.Dom.setStyle(ec, "height", "0"); 
	   
	   //openArticle();
	   
	  resetContentBackground()
	} 
	YAHOO.util.Event.onDOMReady(init);