/**
* Stronglky modified by: Carlos Leopoldo Magaña Zavala
* From http://www.lesterchan.net
* Copyright (c) 2006 Lester "GaMerZ" Chan
**/

// Variables
var polls = new sack(site_url + '/index.php');
var poll_id = 0;
var poll_answer_id = 0;
var is_being_voted = false;

// When User Vote For Poll
function poll_vote(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_form = document.getElementById('polls_form_' + poll_id);
		poll_answer = eval("poll_form.poll_" + poll_id);
		poll_answer_id = 0;
		for(i = 0; i < poll_answer.length; i++) {
			if (poll_answer[i].checked) {
				poll_answer_id = poll_answer[i].value;
			}
		}
		if(poll_answer_id > 0) {
			poll_loading_text();
			poll_process();
		} else {
			alert("Elige una opción.");
		}
	} else {
		alert("Espera, no se ha procesado tu voto anterior ...");
	}
}

// When User View Poll's Result
function poll_result(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_loading_text();
		poll_process_result();
	} else {
		alert("Tu voto se está procesando, espera...");
	}
}

// When User View Poll's Voting Booth
function poll_booth(current_poll_id) {
	if(!is_being_voted) {
		is_being_voted = true;
		poll_id = current_poll_id;
		poll_loading_text();
		poll_process_booth();
	} else {
		alert("Tu voto se está procesando, espera...");
	}
}

// Poll Loading Text
function poll_loading_text() {
	document.getElementById('polls-' + poll_id + '-loading').style.display = 'block';
}

// Poll Finish Loading Text
function poll_unloading_text() {
	document.getElementById('polls-' + poll_id + '-loading').style.display = 'none';
	is_being_voted = false;
}

// Process The Poll
function poll_process() {
		polls.setVar("vote", true);
		polls.setVar("poll_id", poll_id);
		polls.setVar("poll_" + poll_id, poll_answer_id);
		polls.method = 'POST';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_unloading_text;
		polls.runAJAX();
}

// Process Poll's Result
function poll_process_result() {
		polls.setVar("pollresult", poll_id);
		polls.method = 'GET';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_unloading_text;
		polls.runAJAX();
}

// Process Poll's Voting Booth
function poll_process_booth() {
		polls.setVar("pollbooth", poll_id);
		polls.method = 'GET';
		polls.element = 'polls-' + poll_id + '-ans';
		polls.onCompletion = poll_unloading_text;
		polls.runAJAX();
}