 
var poll_pollUrl = '';

function poll_reload(url) {
	var poll_id = document.getElementById("poll_id").value;
	params = {id: poll_id};
	
	if (document.getElementById("poll_block")) {
		//document.getElementById("poll_block").innerHTML = "...";
		$.ajax({ 
			method: "GET",
			type: "GET",
			url: poll_pollUrl,
			data: params, 
			timeout: 10000,
			beforeSend: function(){
			}, //show loading just when link is clicked 
			complete: function(){ 
			}, //stop showing loading when the process is complete 
			success: function(html) {
				document.getElementById("poll_block").innerHTML = html;
			},
			error: function(responseObject, request) {
				document.getElementById("poll_block").innerHTML = "Failed to vote !!";
			}
			});
	} 
}

function poll_vote(title, url) {
	// vote for the current poll
	
	var vote_id = 0;
	var answer_ids = document.getElementById("answer_ids").value.split(",");
	var poll_id = document.getElementById("poll_id").value;
	
	for (var i=0; i < answer_ids.length; i++) {
		if (document.getElementById("poll_" + answer_ids[i])) {
			if (document.getElementById("poll_" + answer_ids[i]).checked) {
				vote_id = answer_ids[i];
			}
		}
	}
	if (vote_id == 0) {
		alert("Duidt aan hoe jij erover denkt en druk dan pas op \"stem\" !");	
	} else {
		url = url + '/answer_id/' + vote_id + '/id/' + poll_id + '/';
		tb_show(title, url, false);
	}
}

function poll_reload_extended(id, item_id, url) {
	params = {id: item_id};
	
	if (document.getElementById(id)) {
		$.ajax({ 
			method: "GET",
			type: "GET",
			url: url,
			data: params, 
			timeout: 10000,	
			success: function(html) {
				var obj = document.getElementById(id);
				if (obj) {
					obj.innerHTML = html;
				} 
			},
			error: function(responseObject, request) {
				document.getElementById(id).innerHTML = "Failed to vote !!";
			}
		});
	}
}

function poll_vote_extended(id, url) {
	var vote_id = 0;
	var answer_ids = document.getElementById("answer_ids_"+id).value.split(",");
	var poll_id = document.getElementById("poll_id_"+id).value;
	
	for (var i=0; i < answer_ids.length; i++) {
		if (document.getElementById("poll_"+ answer_ids[i])) {
			if (document.getElementById("poll_"+ answer_ids[i]).checked) {
				vote_id = answer_ids[i];
			}
		}
	}
	if (vote_id == 0) {
		alert("Duidt aan hoe jij erover denkt en druk dan pas op \"stem\" !");	
	} else {
		url = url + '/answer_id/' + vote_id + '/id/' + poll_id + '/';
		tb_show("", url, false);
	}
}


var Poll = function() {  
	var translateTags = {};
	
	function getFieldValues(fields){
		var i = fields.length;
		var params = {};
		while(i--){
			var elm = $("#"+fields[i]);
			var domEl = elm.get(0);
			if (domEl){
				if (domEl.tagName == "INPUT"){
					if (elm.attr("type").toLowerCase() == "checkbox"){
						if (elm.attr("checked")){
							params[fields[i]] = 1;
						}
						else {
							params[fields[i]] = 0;
						}
					}
					else {
						params[fields[i]] = elm.val();
					}
				}
				else {
					params[fields[i]] = elm.val();
				}
			}
		}
		return params;
	}
	
	function postForm(params, url, fnc){
		$.ajax({ 
			method: "POST",
			type: "POST",
			url: url,
			data: params, 
			timeout: 20000,
			beforeSend: function(){
			}, //show loading just when link is clicked 
			complete: function(){ 
			}, //stop showing loading when the process is complete 
			success: fnc.success,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
					/*if (textStatus == "timeout"){
						alert(translateTags.errorAjaxTimeOut);
					}
					else {
						alert(translateTags.errorAjax);
					}*/
				}
			});
			/*
			function(html){ //so, if data is retrieved, store it in html 
				$("#TB_ajaxContent").html(html);
				} 
			*/
	}
	
	return {
		vote : function(fields, frm_id){
			var url = $("#"+frm_id).attr("action");
			fields.push("captcha-input");
			fields.push("captcha-id");
			var params = getFieldValues(fields);
			params.submit = 1;
			var fnc = {success: function(html){ //so, if data is retrieved, store it in html 
				$("#TB_ajaxContent").html(html);
				}
			};
			postForm(params, url, fnc);
		},
		setTranslateTags: function(tags){
			translateTags = tags;
		}
	};  
}();
		
