function vote_poll(poll_id){
    ajax = new sack();
    ajax.method = "POST";
	ajax.requestFile = '/poll/'+poll_id+'/vote/';
	var choice_id=0;
	for (var i=0; i<document.poll_form.choices.length;i++)
	{
		if (document.poll_form.choices[i].checked)
			choice_id = document.poll_form.choices[i].value;
	}
	if (choice_id)
	{
		ajax.setVar('choice', choice_id);
		ajax.onCompletion = function(){ load_poll(ajax.response); };	
		ajax.onError = function(){ alert('AJAX error');alert(ajax.responseStatus) };
		ajax.runAJAX();
	}
	else
	{
        answer_el = document.getElementById("user_answer");
        if (answer_el)
        {
            answer = answer_el.value;
            if (answer.length<5)
            {
                alert("Please enter the answer!")
            }
            else
            {
                ajax.setVar('answer', answer);
                ajax.onCompletion = function(){ load_poll(ajax.response); };	
                ajax.onError = function(){ alert('AJAX error');alert(ajax.responseStatus) };
                ajax.runAJAX();
            }
        }
        else
        {
        	alert('Select the answer!');
        }
	}
	return false;
}

function switch_results(poll_id, dest)
{
	ajax = new sack();
    ajax.method = "POST";
	ajax.requestFile = '/poll/'+poll_id+'/vote/';
	if (dest=='results')
		ajax.setVar('results', true);
	ajax.onCompletion = function(){ load_poll(ajax.response); };	
	ajax.onError = function(){ alert('AJAX error');alert(ajax.responseStatus) };
	ajax.runAJAX();
}

function load_poll(response){
    poll_block = document.getElementById("poll");
    poll_block.innerHTML = response;
}

function hide_edit()
{
    add = document.getElementById("add_choice");
	add.style.display = "block";
	add_input_block = document.getElementById("add_input");
	add_input_block.style.display = "none"
}

function show_edit(poll_id){
	var add
	var add_input_block
	var add_input
    for (var i=0; i<document.poll_form.choices.length;i++)
	{
		if (document.poll_form.choices[i].checked)
			document.poll_form.choices[i].checked = false;
		document.poll_form.choices[i].onclick = function(event){return hide_edit()};
			//document.poll_form.choices[i].disabled = true
	}
	add = document.getElementById("add_choice");
	add.style.display = "none";
	add_input_block = document.getElementById("add_input");
	add_input_block.style.display = "block";
	add_input = document.getElementById("user_answer");
	add_input.focus();
}

function check_input(e, poll_id)
{
    if (!e) e = window.event || null; //IE 
    var keynum = e.keyCode?e.keyCode:e.charCode;
    if (keynum == 13)
    {
    	//submit_answer(poll_id);
    	return true;
    }
    else
    	return true;
}

function submit_answer(poll_id){
	answer = document.getElementById("user_answer").value
	if (answer.length<5)
	{
		alert('Input the answer please')
	}
	else
	{
		ajax = new sack();
	    ajax.method = "POST";
		ajax.requestFile = '/poll/'+poll_id+'/add_choice/';
		ajax.setVar('answer', answer);
		ajax.onCompletion = function(){ load_poll(ajax.response); };	
		ajax.onError = function(){ alert('AJAX error');alert(ajax.responseStatus) };
		ajax.runAJAX();
	}
}
