//TODO, move this somewhere more generic, its a nice func :)
if(typeof(addOnloadEvent) != 'function'){
	function addOnloadEvent(fnc){
		if (typeof window.addEventListener != "undefined") {
			window.addEventListener("load", fnc, false);
		}else if (typeof window.attachEvent != "undefined") {
			window.attachEvent("onload", fnc);
		} else {
			if ( window.onload != null ) {
				var oldOnload = window.onload;
				window.onload = function (e) {
					oldOnload(e);
					window[fnc]();
				};
			} else {
				window.onload = fnc;
			}
		}
	}
}

/*
* Some requirements for this func:
* options need to be named: "pollopt-[poll id]-[count]" where count starts from 0
*/
function checkVoteForm(pollId, min, max) {
	//if submit button, disabled it (no double submits stupid users)
	var subButton = null;
	if(subButton = document.getElementById("poll-submit-"+pollId)) {
		subButton.disabled = true;
	}
	
	//make sure our vars are correct types
	pollId = parseInt(pollId);
	min = parseInt(min);
	max = parseInt(max);
	
	allow = false; //flag if user can make vote
	var selOpts = []; //the list of options chosen
	
	if(min == 1 && max == 1) { //radio
		var box;
		var cnt = 0;
		while(box = document.getElementById("pollopt-"+pollId+"-"+cnt)) {
			if(box.checked) {
				allow = true;
				selOpts[0] = box.value;
				break;
			}
			cnt++;
		}
		
		if(!allow) {
			errorMsg = "Please select 1 option before clicking 'Vote'.";
		}
	}else{
		var box;
		var cnt = 0;
		var selCnt = 0;
		while(box = document.getElementById("pollopt-"+pollId+"-"+cnt)) {
			if(box.checked) {
				selOpts[selCnt] = box.value;
				selCnt++
			}
			cnt++;
		}
		
		if(selCnt >= min && selCnt <= max) {
			allow = true
		}else{
			if(min == max) {
				errorMsg = "Please select "+min+" options before clicking 'Vote'.";
			}else{
				errorMsg = "Please select "+min+" - "+max+" options before clicking 'Vote'.";
			}
		}
	}
	
	if(allow) { //seems good, lets try save it
		if (window.XMLHttpRequest) {
			var ajaxRequest = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			var ajaxRequest = new ActiveXObject('Microsoft.XMLHTTP');
		}else{
			var ajaxRequest = null;
		}
		
		if(ajaxRequest) { //if ajax found, try use it to post
			pollAjaxSend(pollId, ajaxRequest, selOpts, subButton);
			return false;
		}else{ //no ajax, post form in normal way
			return true;
		}
	}else{ //nope, js says form not validating, let user know
		var errBox;
		if(errBox = document.getElementById("poll-alert-"+pollId)) {//if div exists, show inline error
			errBox.innerHTML = errorMsg;
			errBox.style.display = "";
		}else{ //or go with crappy alert
			alert(errorMsg);
		}
		
		//if we have that submit button disabled, re-enabled it
		if(subButton) { 
			subButton.disabled = false;
		}
		return false;
	}
}

function pollAjaxSend(pollId, ajaxRequest, selOpts, subButton) {
	var url = "/polls.php?action=ajaxSubmit&pollId="+pollId;
	for(i=0; i<selOpts.length; i++) {
		url += "&votes["+i+"]="+selOpts[i];
	}
	ajaxRequest.open('GET', url, true); //dont need post cause the security tests are run server side
	ajaxRequest.onreadystatechange = function() {
		if (ajaxRequest.readyState == 4) {
			var html = ajaxRequest.responseText;
			if(html.substr(0, 11) == "ERROR_CODE:") {
				var code = html.substr(11, 4);
				var msg = html.substr(16);
		
				var errBox;
				if(errBox = document.getElementById("poll-alert-"+pollId)) {//if div exists, show inline error
					errBox.innerHTML = msg;
					errBox.style.display = "";
				}else{ //or go with crappy alert
					alert(msg);
				}
				if(subButton) { 
					subButton.disabled = false;
				}
			}else{
				document.getElementById("wc-poll-"+pollId).innerHTML = html;
			}
		}
	}
	ajaxRequest.send(null);				
}

