function FormEncuestaValidator(Form){

	//primero validamos que se haya seleccionado una opción
	var respuesta = validarOpcion(Form);
	if (!respuesta){
		alert("Seleccione una opción");
		return (false);
	}
	
	var id = Form.idencuesta.value;
	var cookie=LeerCookie(id);
	
	//Comprobamos si existe la cookie
	if ((cookie == "?")) {	
		//no existe la creamos, con nombre y valor el id y con caducidad de un dia
		_crearCookieTemporal(id, id, 1);
		
	}else{
		//Si ya existe, navegamos al detalle de la encuesta si existe el campo toURL o sino, navegamos a la misma página que estamos, caso de estar ya en el detalle
		var tourl="";
		if (Form.toUrl!=null){
			tourl = Form.toUrl.value;
		}else{
			tourl = document.location;
		}
		
		document.location.href=tourl;  
		return(false);
	}
	
	return (true);
}

function validarOpcion(Form){
	for ( var i = 0; i < Form.respuesta.length; i++ ) {
			if ( Form.respuesta[i].checked ) {
				return true;
			}
	}
}

function _crearCookieTemporal(Nombre, Valor, caduca) {
	var Dia = new Date();
	Dia.setDate(Dia.getDate() + caduca);
	document.cookie = Nombre + "=" + Valor + "; expires=" + Dia.toGMTString() + "; path=/";
}

function obtener_encuesta(idContenido){
	var lectorXML = new LectorXML('http://www.lavozdegalicia.es/SSEE/xsltExtension?metodo=obtenerResultadoEncuesta&idContenido='+idContenido);
	var xml = lectorXML.xml;
	pintarResultado(xml);
}

function pintarResultado(xml){
	if (xml!=null){
		xmldoc = xmlParse(xml);
		if (xmldoc!=null){
			// Obtiene los datos del XML
			var numeroVotos=0;
			//obtenemos el nodo root
			var root = xmldoc.getElementsByTagName('Votos')[0];
			//Primero contamos los votos:
			for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
				var node = root.childNodes[iNode];
				if (node!=null){
					var valor=node.firstChild.nodeValue;
					if (!isNaN(valor)){
						numeroVotos+=parseInt(valor);
					}
				}
			}
			//Ahora sustituimos los valores correspondientes a cada id con los recuperados
			for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
				var node = root.childNodes[iNode];
				if (node!=null){
					var valor=node.firstChild.nodeValue;
					var atributo = node.attributes[0].nodeName;
					var respuesta = node.attributes[0].nodeValue;
					//sustituimos la capa que representa el numero de votos para la respuesta
					if (document.getElementById(atributo+"_"+respuesta)!=null){
						//ponemos el valor
						document.getElementById(atributo+"_"+respuesta).innerHTML=valor;
					}
					//calculamos el porcentaje
					var porcentaje =Math.round(100*valor/numeroVotos);
					//Sustituimos la capa que representa el porcentaje para la respuesta
					if (document.getElementById(atributo+"_percent_"+respuesta)!=null){
						//ponemos el valor
						document.getElementById(atributo+"_percent_"+respuesta).innerHTML="("+porcentaje+"%)";
					}
					//el tamaño de la barra
					if (document.getElementById("percent_"+respuesta)!=null){
						document.getElementById("percent_"+respuesta).style.width=porcentaje+"%";
					}
				}
			}
			//finalmente indicamos el total de votos
			if (document.getElementById("total_votos")!=null){
				document.getElementById("total_votos").innerHTML=numeroVotos;
			}
			
		}
	}

}

function getOpinaComents(idOpina,tipocontenido,pagina,div){
   stat_counter();
	rss('int', '/SSEE/getComents?IdContenido='+idOpina + '&tipoC=' + tipocontenido + '&Plantilla=LVDG_Comentarios&pagina='+pagina, "", div);
}
