// JavaScript Document
/* --------------------------  Desplegables  ---------------------------- */
function abrirVentana (URL){ 

   window.open (URL,"ventana1","width=600,height=450,scrollbars=yes,resizable=no") 

}

function desplegar(Identificador,num_bloques)
{
    for (i2=1;i2<=num_bloques;i2++)
	{
		
	document.getElementById('menos'+i2).style.display="none";
    document.getElementById('mas'+i2).style.display="block";
	}
	
	var i;
	document.getElementById('mas'+Identificador).style.display="none";
    document.getElementById('menos'+Identificador).style.display="block";
	
	for (i=1;i<=num_bloques;i++)
	{
		var actual = document.getElementById('d'+i);
		if (Identificador == i)
		{   
			actual.style.display = "block";
		}
		else
			actual.style.display = "none";
	}
}

function desplegar_menos(Identificador)
{
	var i;
	i=Identificador;
	document.getElementById('menos'+i).style.display="none";
	document.getElementById('mas'+i).style.display="block";
		var actual = document.getElementById('d'+i);
			actual.style.display = "none";

}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

/*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}
/////// AJAX
function createRequestObject(){
	  var peticion;
	  var browser = navigator.appName;
			if(browser == "Microsoft Internet Explorer"){
				  peticion = new ActiveXObject("Microsoft.XMLHTTP");
			}else{
				  peticion = new XMLHttpRequest();
}
return peticion;
}

var http = new Array();
function ObtDatos(url)
{
	var act = new Date();
	http[act] = createRequestObject();
	http[act].open('get', url);
	http[act].onreadystatechange = function() 
	{
		if (http[act].readyState == 4) 
		{
			if (http[act].status == 200 || http[act].status == 304) 
			{
				var texto;
				texto = http[act].responseText;
				var DivDestino = document.getElementById("encuesta_back_fondo");
				if (texto != "Error")
					DivDestino.innerHTML = '<div>'+texto+'</div>';
			}
		}
	}
	http[act].send(null);
}
 
function encuesta(accion) {
	
	  var voto1 = getCheckedValue(document.formencuesta.voto1);
	  
	  var DivDestino = document.getElementById("encuesta_back_fondo");
	  
		document.getElementById("botonvotar").style.display = "none";
		document.getElementById("botonresultados").style.display = "none";  
		document.getElementById("botonvolver").style.display = "block";  
		
	  DivDestino.innerHTML = "<div></div>";
	  if (accion) 
	  {
		  	ObtDatos("ajax_encuesta.php?accion="+accion+"&voto1="+voto1);
	  } 
}

/// RECOMENDAR AMIGO
function ObtDatosRecomendar(url)
{
	var act = new Date();
	http[act] = createRequestObject();
	http[act].open('get', url);
	http[act].onreadystatechange = function() 
	{
		if (http[act].readyState == 4) 
		{
			if (http[act].status == 200 || http[act].status == 304) 
			{
				var texto;
				texto = http[act].responseText;
				var DivDestino = document.getElementById("contenedor_form");
				var largo = texto.length;
				if (texto.substr(largo-2) == "OK")
					DivDestino.innerHTML = '<div>Recomendación enviada</div>';
				else
					DivDestino.innerHTML = '<div>Ha habido algún error enviando tu recomendación. Por favor, inténtalo e nuevo</div>';
			}
		}
	}
	http[act].send(null);
}
 
function recomendar() {
	  var nombre = document.form_recomendar.nombre.value;
  	  var amigo1 = document.form_recomendar.amigo1.value;
  	  var amigo2 = document.form_recomendar.amigo2.value;
   	  var amigo3 = document.form_recomendar.amigo3.value;
	  var email1 = document.form_recomendar.email1.value;
	  var email2 = document.form_recomendar.email2.value;
  	  var email3 = document.form_recomendar.email3.value;
  	  var comentarios = document.form_recomendar.comentarios.value;
	  var url = document.form_recomendar.url.value;
	  var esNoticia = document.form_recomendar.esNoticia.value;
	  
	  //alert(nombre+" "+amigo1+" "+email1);
	  var DivDestino = document.getElementById("contenedor_form");
	  	 
	  if (nombre && amigo1 && email1)
	  {
 	    DivDestino.innerHTML = "<div><img src=\"img/loading.gif\" alt=\"Enviando\" /></div>";
	  	ObtDatosRecomendar("ajax_recomendar.php?nombre="+nombre+"&amigo1="+amigo1+"&amigo2="+amigo2+"&amigo3="+amigo3+"&email1="+email1+"&email2="+email2+"&email3="+email3+"&comentarios="+comentarios+"&url="+url+"&esNoticia="+esNoticia);
	  }
	  else
	  	alert("Debes escribir tu nombre y el de un amigo, junto con su email");

}