var req;


/*
function registrar_preguntas(lista){
	preguntas = lista.split(',');
	
	var1 = "respuestas=0";
	var2 = "valores=0";
	for(i=1 ; i<preguntas.length; i++){
		campo = eval('document.registro_preguntas.respuesta'+preguntas[i]);
		for(j=0; j<campo.length; j++){
			if(campo[j].checked){
				var1 += "," + i; 
				var2 += "," + campo[j].value;
			}
		}
	}
	valores = var1 + "&" + var2;
	url="/CFC/registro_preguntas_accion.cfm";
	
	if (window.XMLHttpRequest) {
	   		req = new XMLHttpRequest();
			req.onreadystatechange = resultado_preguntas;
	        req.open("POST", url, true);
			req.setRequestHeader("Method", "POST " + url + " HTTP/1.1"); 
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
			req.send(valores);
		} else if (window.ActiveXObject) {
		    req = new ActiveXObject("Microsoft.XMLHTTP");
	    	if (req) {
					req.onreadystatechange = resultado_preguntas;
	        	req.open("POST", url, true);
				req.setRequestHeader("Method", "POST " + url + " HTTP/1.1"); 
				req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
			    req.send(valores);
	        }
		}	
	
	

}
*/

function resultado_preguntas(){

 if (req.readyState == 4) {	
		if (req.status == 200) {
			if(req.responseText == '-1'){ // Ha fallado			
				alert('Ha fallado el proceso');
			}else if(req.responseText == '-2'){ // No esta logado			
				alert('Debe estar registrado el usuario');	
			}else{// Todo correcto
				for(i = 1; i<10 ; i++){
					if(document.getElementById('preguntas'+i)) {
						document.getElementById('preguntas'+i).style.display = 'none';
					}
				}
				document.getElementById('registroResultado').style.display = 'block';			
			}				
        }
    }	
}


// Votaciones de la home

var respuestas_;
var txt_respuesta;
var inner_resultado;
function encuesta_insertar(valor,respuestas){
	respuestas_=respuestas
	//alert(respuestas_)
	url = '/CFC/votar_encuesta.cfm' ; 
	valores ="respuestas="+encodeURIComponent(respuestas);
	valores +="&valor="+encodeURIComponent(valor);	
		
	if (window.XMLHttpRequest) {
   		req = new XMLHttpRequest();
		req.onreadystatechange = encuesta_insertar_carga;
        req.open("POST", url, true);
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		req.send(valores);
	} else if (window.ActiveXObject) {
	    req = new ActiveXObject("Microsoft.XMLHTTP");
    	if (req) {
			req.onreadystatechange = encuesta_insertar_carga;
        	req.open("POST", url, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
		    req.send(valores);
        }
	}	
}

function encuesta_insertar_carga(){
	if (req.readyState == 4) {	
		
		if (req.status == 200) {
			// Cargar el contenido en la capa
			matriz_r=respuestas_.split(",")
			//alert(matriz_r)
			
			matriz_corte=req.responseText.split("|");
			
			
			txt_respuesta = matriz_corte[0];
			
			document.getElementById("total_votaciones").innerHTML = "Total de participaciones: " + matriz_corte[1];
			
			matriz_1=matriz_corte[2].split("_");
			for (j=1;j<matriz_r.length;j++){
				document.getElementById("respuesta_"+matriz_r[j]).innerHTML = "0 %";
			}			
			for (i=0;i<matriz_1.length-1;i++){
				matriz_2=matriz_1[i].split(";")
				document.getElementById("respuesta_"+matriz_2[1]).innerHTML = matriz_2[0]+" %";				
			}
			
			if(txt_respuesta.length == 0){ 
				document.getElementById("votar").innerHTML = '';
			}else{
				document.getElementById("votar").innerHTML = '<a href="javascript:Ver_Respuestas();"  class="txt_rojo">|Ver respuesta|</a>';	
			}	
		}
	}
}

function Ver_Respuestas(){
	inner_resultado = document.getElementById("respuestas_pregunta_votacion").innerHTML;
	document.getElementById("respuestas_pregunta_votacion").innerHTML = '<font class="txt_cont_b">' + txt_respuesta + '</font>';
	document.getElementById("votar").innerHTML = '<a href="javascript:Volver_Respuestas();"  class="txt_rojo">|Volver|</a>';
}

function Volver_Respuestas(){
	document.getElementById("respuestas_pregunta_votacion").innerHTML = inner_resultado;
	document.getElementById("votar").innerHTML = '<a href="javascript:Ver_Respuestas();"  class="txt_rojo">|Ver respuesta|</a>';
}

function votar_encuesta(){
	var respuestas = '0'; 
	var valor;
	for(i = 0; i <document.encuesta.idrespuesta.length; i++){
		respuestas += ',' + document.encuesta.idrespuesta[i].value;
		if(document.encuesta.idrespuesta[i].checked){
			valor = document.encuesta.idrespuesta[i].value;
		}
	}//alert(valor)
	if(valor){
		encuesta_insertar(valor,respuestas)
	}else{
		alert('Debe seleccionar alguna respuesta');
	}	
}
