
var state;
var visitor = new Cookie(document,"polls",2400);
var previous;
visitor.load();
var elmnt = document.poll;
var len = elmnt.length;

function checkCookie() {
  if (document.cookie == "") {
    alert("In order to participate in this poll, you need to enable session cookies in your browser.\n");
  } else {
    elmnt.cookieexists.value = "true"
    state = elmnt.cookieexists.value;
    return true;
  }
  return false;
}
function checkRadioAnswer(upIndex, numberOfElements) {
	for(var j=upIndex-numberOfElements+1; j<upIndex+1; j++) {
		if (elmnt.elements[j].checked==true) {
			return 1;
		}
	}
	return 0;
}
function validate() {
	var i,j,j0,ef,radioName="",elmName="",wasChecked=0,loop=0;
	for (i=0;i<len;i++) {
		ef = elmnt.elements[i];
		wasChecked = 0;
		if(ef.type == "radio") {
			radioName = ef.name;
			elmName = radioName;
			j = i+1;
			while ((elmName==radioName) && (loop==0)) {
				ef = elmnt.elements[j];
				radioName = ef.name;
				j += 1;
				if (j==len) {loop=1;}
			}
			if (loop=="0") {j -= 1;}
			j0=j-i;
			i=j-1;
			wasChecked = checkRadioAnswer(i,j0);
			if (wasChecked==0) {
				return false;
			}
		}
		if (loop==1) {break;}
	}
	return true;
}
function save() {
	if(validate()==false) {
		alert("In order to vote, you have to select an answer.\n")
		return;
	}
	var newpoll = 1;
	var id = document.poll.poll.value;
	if (visitor.count == null || visitor.count == 0) {
		visitor.count = 0;
	}
	else {
		var a = visitor.str.split('%');
		for (var i=0;i<a.length;i++) {
			if (a[i] == id) {
				newpoll = 0;
				document.poll.vote.value = "false";
				return;
			}
		}
	}
	if (newpoll == 1) {
		visitor.count++;
		if (visitor.str == null) {
			visitor.str = id;
		}
		else {
			previous = visitor.str;
			visitor.str += '%' + id.toString();
		}
	}
	visitor.store();
	if (!checkCookie()) {
		visitor.str = previous;
		visitor.count--;
		document.poll.vote.value = "false";
	}
	elmnt.action.value=1;
	elmnt.submit();
	return 1;
}
function send2() {
	elmnt.action.value=2;
	elmnt.submit();
	return 1;
}

