function change(id,image)
{
	el = document.getElementById(id);
	el.style.background = "url("+image+")";
}

function restore(id,image)
{
	el = document.getElementById(id);
	el.style.background = "url("+image+")";
}

function findProvinciasBusqueda()
{
	var val = document.getElementById("pis").value;
	if (val != "")
	{
		elemento = document.getElementById('pcia');
		elemento.length = 0;
		opcionCargando = document.createElement("option"); 
		opcionCargando.value = 0; 			
		opcionCargando.innerHTML="Cargando ...";			
		elemento.appendChild(opcionCargando); 
		elemento.disabled = true;			
		dojo.io.bind({
			url: './admin/source/logic/json_provinciasXpais.php?id='+val,
			load: function(type,data,xhr)
			{				
				elemento.length = 0;
				opcionCargando = document.createElement("option"); 
				opcionCargando.value = 0; 			
				opcionCargando.innerHTML="Provincias ...";			
				elemento.appendChild(opcionCargando); 
				elemento.disabled = false;			
				for(i=0;i<data.length;i++)
				{
					object = data[i];			
					opcionCargando = document.createElement("option"); 
					opcionCargando.value = object.id; 			
					opcionCargando.innerHTML=object.value;			
					elemento.appendChild(opcionCargando); 			
				}				
			},
			error: function(type,err)
			{
				
			},
			mimetype: "text/json"
		});	
	}
}

function findProvinciasBusquedaAvanzada()
{
	var val = document.getElementById("pav").value;
	if (val != "")
	{
		elemento = document.getElementById('pra');
		elemento.length = 0;
		opcionCargando = document.createElement("option"); 
		opcionCargando.value = 0; 			
		opcionCargando.innerHTML="Cargando ...";			
		elemento.appendChild(opcionCargando); 
		elemento.disabled = true;			

		dojo.io.bind({
			url: './admin/source/logic/json_provinciasXpais.php?id='+val,
			load: function(type,data,xhr)
			{
				elemento.length = 0;
				opcionCargando = document.createElement("option"); 
				opcionCargando.value = 0; 			
				opcionCargando.innerHTML="Provincias ...";			
				elemento.appendChild(opcionCargando); 
				elemento.disabled = false;			
				for(i=0;i<data.length;i++)
				{
					object = data[i];			
					opcionCargando = document.createElement("option"); 
					opcionCargando.value = object.id; 			
					opcionCargando.innerHTML=object.value;			
					elemento.appendChild(opcionCargando); 			
				}				
			},
			error: function(type,err)
			{
				
			},
			mimetype: "text/json"
		});	
	}
}

function isEmailAddress(theElement )
{
	var s = theElement.value;
	var filter=/^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_\-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) 
		return true;
	if (filter.test(s))
		return true;
	return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function Login()
{
	var obj = new usuarios();

	var user = document.getElementById("logusuario").value;
	var pass = document.getElementById("logpass").value;

	if ((user == "") || (pass == ""))
	{
		alert('Introduzca un nombre de usuario y contraseña válido.');
	}
	else
	{
		obj.log_usuarios.call(user,pass);
		obj.log_usuarios.result = function(result) 
		{	
			if (result == 0)
			{
				alert('Nombre de usuario y contraseña incorrecto. Por favor, intente nuevamente.');
			}
			else
			{
				window.location = "./perfil.php";
			}
		}	
	}	
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}


function cerrarSesion()
{
		setCookie("trueknic","",3650)
		var url = "./admin/source/logic/json_cerrarsesion.php";
		
		bind(url,function(result)
		{
			window.location = "./index.php";
		},
		function(result)
		{
			alert("error "+result)
		});
}

function validarIntercambio(id, id_logueado)
{
	if (id == id_logueado)
		alert("No puede intercambiar juegos con usted mismo.");
	else
	{
		var obj = new usuarios();
		obj.is_authenticate.call();
		obj.is_authenticate.result = function(result) 
		{	
			if (result == true)
				window.location = "./intercambiar.php?us="+id;
			else
				alert("El intercambio de juegos es solo para usuario registrados.");
		}
	}
}

function bind(url,sucess_function,failed_function) 
{
	var req = null; 
	if(window.XMLHttpRequest)
		req = new XMLHttpRequest(); 
	else if (window.ActiveXObject)
		req  = new ActiveXObject("Microsoft.XMLHTTP"); 

	showCargando();
	req.onreadystatechange = function()
	{ 
		if(req.readyState == 4)
		{
			hideCargando();
			if(req.status == 200)
			{
				sucess_function(req.responseText);
			}	
			else	
			{
				failed_function(req.responseText);
			}	
		} 
	}; 
	req.open("GET", url, true); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	req.send(null);
}

function LoadPopUp(psFile) 
{
	var izq=screen.width/2-400; 
	var arriba=screen.height/2-300;	
	newwindow = window.open(psFile,"name","toolbar=no, location=no, status=no, menubar=no, scrollbars=yes, resizable=no, width=800, height=500,left="+izq+",top="+arriba+",");
	if (window.focus) 
	{
		newwindow.focus()
	}
}

function showRecordar()
{
	var email = document.getElementById("logusuario").value;
	if (email == "")
		alert("Para recordar su contraseña debe especificar su email.")
	else
	{
		var obj = new usuarios();
		obj.valid_email.call(email);
		obj.valid_email.result = function(result)
		{
			if (result == "ko")
				alert("Usuario no registrado en el sistema. Por favor, intente nuevamente.")
			else
			{
				window.location = "./recordar.php";
			}
		}
	}
}

function showCargando()
{
	var capa = document.createElement("div");
	capa.setAttribute("id","capa_oculta")
	capa.style.position = "absolute";
	capa.style.top = "0px";
	capa.style.left = "0px";
	capa.style.width = "100%";
	capa.style.height = "3000px";
	//capa.style.textAlign = "center";
	capa.style.backgroundColor = "#000000";
	capa.style.color = "#ffffff";
	capa.style.zIndex = "100";
	capa.style.filter="alpha(opacity=80)"; // Opacidad para IE
	capa.style.opacity="0.8"; // Ocapacidad para Netscape
	capa.style.display = "block";
	capa.innerHTML = "<div style='font-family:Verdana;font-weight:bold;font-size:12px;padding:5px 20px 2px 20px;width:130px;text-align:center;margin-top:"+(document.documentElement.scrollTop + document.body.scrollTop+300)+"px;margin-left:"+(screen.width/2-65)+"px'><img src='images/load.gif' align='top'>&nbsp;&nbsp;Cargando...</div>"
	var iframe = document.createElement("iframe");
	iframe.setAttribute("id","iframe_oculto")
	iframe.setAttribute("scrolling","no")
	iframe.setAttribute("frameborder","0")
	iframe.style.position = "absolute";
	iframe.style.top = "0px";
	iframe.style.left = "0px";
	iframe.style.display = "none";
	iframe.style.filter="alpha(opacity=0)"; // Opacidad para IE
	iframe.style.opacity="0.00"; // Ocapacidad para Netscape
	document.body.appendChild(capa);
	document.body.appendChild(iframe);
	

	// Definimos el ancho del IFRAME igual que el ancho de la capa, para que no se le vea
	iframe.style.width = capa.offsetWidth;
	iframe.style.height = capa.offsetHeight;
	// Posicionamos el IFRAME en la misma posición que la capa (para que esté siempre detras)
	iframe.style.top = capa.style.top;
	iframe.style.left = capa.style.left;
	// Bajamos de Z-index el IFRAME, para que esté debajo de la capa
	iframe.style.zIndex = capa.style.zIndex - 1;

	// Definimos que el contenido del IFRAME esté VISIBLE
	iframe.style.display = "block"; // ESTA ES LA LINEA MAS IMPORTANTE
}

function hideCargando()
{
	var capa = document.getElementById("capa_oculta");
	var iframe = document.getElementById("iframe_oculto");
	document.body.removeChild(capa);
	document.body.removeChild(iframe);
}

function entities(str)
{

alert(str)
var resultado = str; // Texto retornado
resultado = resultado.replace(/\+/g," "); // Substituimos por un espacio.
resultado = unescape(resultado); // Deshacer lo que hizo la función urlencode();.
alert(resultado)

}

function parejas(char, code){
 this.char = char;
 this.code = code;
}
var pares = new Array();
pares[0] = new parejas("Á", "&Aacute;");
pares[1] = new parejas("á", "&aacute;");
pares[2] = new parejas("É", "&Eacute;");
pares[3] = new parejas("é", "&eacute;");
pares[4] = new parejas("Í", "&Iacute;");
pares[5] = new parejas("í", "&iacute;");
pares[6] = new parejas("Ó", "&Oacute;");
pares[7] = new parejas("ó", "&oacute;");
pares[8] = new parejas("Ú", "&Uacute;");
pares[9] = new parejas("ú", "&uacute;");
pares[10] = new parejas("Ü", "&Uuml;");
pares[11] = new parejas("ü", "&uuml;");
pares[12] = new parejas("Ñ", "&Ntilde;");
pares[13] = new parejas("ñ", "&ntilde;");
pares[14] = new parejas("&", "&amp;");
pares[15] = new parejas("<", "&lt;");
pares[16] = new parejas(">", "&gt;");
pares[17] = new parejas("í", "&itilde;");
pares[18] = new parejas("", "&nbsp;");
pares[19] = new parejas("“", "&quot;");
pares[20] = new parejas("'", "&apos;");
pares[21] = new parejas("©", "&copy;");
pares[22] = new parejas("®", "&reg;");
pares[23] = new parejas("€", "&euro;");
pares[24] = new parejas("¼", "&frac14;");
pares[25] = new parejas("½", "&frac12;");
pares[26] = new parejas("¾", "&frac34;");

function htmlentities(txt) {
for (var i = 0, total = pares.length; i < total; i ++)
 txt = txt.replace(new RegExp(pares[i].code, "g"), pares[i].char)
 return txt;
}