var votando = 0;

function enviarVoto() {

	id = $('#id_encuesta').val();

	if(votando == 0) {
		//votando = 1;
		val = validarVoto('respuesta');
		if(val) {
			$.post('encuestas/enviar-voto', {respuesta: val, id_encuesta: id}, function(data) {
				$('#respuestas').html(data);		
			});
		}
	}

}

function enviarAmigo() {

		$.post('amigos', $(':input'), function(data) {
			$('#friend_box').html(data);		
		});
			
		return false;

}

function votar() {

	id = $('#id_encuesta').val();

	$.get('encuestas/ver-encuesta/'+id, null, function(data) {
		$('#respuestas').html(data);
	});
	
	return false;

}

function verResultados() {

	id = $('#id_encuesta').val();

	$.get('encuestas/resultados/'+id, null, function(data) {
		$('#respuestas').html(data);
	});
	
	return false;

}

function validarVoto(name) {
	result = isRadioChecked(name);
	if(!result) alert("Debe seleccionar una opcion para votar");
	return result;
}

function validarUsuario() {
	usuario = document.getElementById('usuario');
	contrasena = document.getElementById('contrasena');
	
	if((usuario.value == "") || (contrasena.value == "")) {
		alert("Debe completar los campos usuario y contrasena");
		return false;
	}

}

function isRadioChecked(name) {
	result = false;
	radios = document.getElementsByName(name);
	nombre = i;
	n = radios.length;
	for(var i = 0; i < n; i++) {
		if(radios[i].checked) {
	  	result = radios[i].value;
	    break;
	  }
	}
	return result;
}