/*Script para mostrar caja de dialogo y luego cargar archivo PHP que modifica la categoria en la tabla de foro
Fecha: 14.12.2011
Autor: Sebastian P.*/


var conexionCargaForos;
var	conexionFavorito;
var conexionMsgp;
var imagenCargando="<img src=\"img/loading-mid.gif\" alt=\"Cargando\">";
var imagenCargandoSmall="<img src=\"img/loading-small.gif\">";
var ultimofiltro=0;
var foroFav;
var div_msgp;

function confirmar_cambio_cat(idforo,idtema,nombretema)
{
var r=confirm("¿Desea cambiar la categoria a "+nombretema+"?");
if (r==true)
  {
  htmlCat = xloadS("cambio_categoria.php?idforo="+idforo+"&idtema="+idtema);
  var mailusuario=confirm("El thread a cambiado a la categoria "+nombretema+". ¿Desea notificarle al creador del Thread?");
  if (mailusuario==true)
	{
	xloadS("envio_mail_cambio_cat.php?avisouser=si&idforo="+idforo+"&idtema="+idtema);
	}
  else
	{
	xloadS("envio_mail_cambio_cat.php?avisouser=no&idforo="+idforo+"&idtema="+idtema);
	}
  cambiarTextoEnDiv('ruta_categoria',htmlCat);
  }
else
  {
  return;
  }
}

function xloadA(pag) { //Asíncrono
    if (typeof window.ActiveXObject != 'undefined' ) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        req.onreadystatechange = xres ;
    } else {
        req = new XMLHttpRequest();
        req.onload = xres ;
    }
    req.open( "GET", pag, true );
    req.send( null );
}

function xloadS(pag) { //Síncrono
    if (typeof window.ActiveXObject != 'undefined' ) req = new ActiveXObject("Microsoft.XMLHTTP");
    else req = new XMLHttpRequest();
    req.open("GET", pag, false);
    req.send(null);
    return req.responseText;
}

function xres() {
    if (req.readyState != 4) return ;
    alert(req.responseText);
}

function cargarForosxTema(idTema){
	if (ultimofiltro==idTema){
		idTema='hm';
	}
	if (isNaN(idTema)){
		ultimofiltro=0;
	}else{
		ultimofiltro=idTema;
	}
	conexionCargaForos=crearXMLHttpRequest();
	conexionCargaForos.onreadystatechange = procesarCargaForos;
	conexionCargaForos.open("GET", 'cargar_foros.php?idtema='+idTema, true);
	conexionCargaForos.send(null);
	destacarFiltro(idTema);
}

function addFav(foro,add){
	foroFav=foro;
	conexionFavorito=crearXMLHttpRequest();
	conexionFavorito.onreadystatechange = procesarAddFav;
	conexionFavorito.open("GET", 'favorito.php?idforo='+foro+'&a='+add, true);
	conexionFavorito.send(null);
}

function confirmar_elimina_msgp(mensaje){
	var r=confirm("¿Está seguro que desea eliminar el mensaje?");
	if (r==true){
		div_msgp=document.getElementById("perfil_mensaje_"+mensaje).parentNode;
		conexionMsgp=crearXMLHttpRequest();
		conexionMsgp.onreadystatechange = procesarEliminaMsgp;
		conexionMsgp.open("GET", 'borra_msgp.php?idmsg='+mensaje, true);
		conexionMsgp.send(null);
	}
	else{
	}
}
function procesarEliminaMsgp(){
  var divDeCambio = div_msgp;
  if(conexionMsgp.readyState == 4)
  {
    divDeCambio.innerHTML = conexionMsgp.responseText;
  } 
  else 
  {
    divDeCambio.innerHTML = imagenCargandoSmall;
  }
}

function cambiarTextoEnDiv(idDiv,textoNuevo){
	var div = document.getElementById(idDiv);
	div.innerHTML = textoNuevo;
}

function procesarAddFav()
{
  var divDeCambio = document.getElementById("boton_fav_"+foroFav);
  if(conexionFavorito.readyState == 4)
  {
    divDeCambio.innerHTML = conexionFavorito.responseText;
  } 
  else 
  {
    divDeCambio.innerHTML = imagenCargandoSmall;
  }
}

function procesarCargaForos()
{
  var divDeCambio = document.getElementById("foro_home");
  if(conexionCargaForos.readyState == 4)
  {
    divDeCambio.innerHTML = conexionCargaForos.responseText;
  } 
  else 
  {
    divDeCambio.innerHTML = imagenCargando;
  }
}

function crearXMLHttpRequest() 
{
  var xmlHttp=null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else 
    if (window.XMLHttpRequest) 
      xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}

