
//Contenido
var http_request = false;
function obtenerRequest() {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) { http_request.overrideMimeType('text/xml'); }
    }
    else if (window.ActiveXObject) { // IE
        try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); }
        catch (e) {
            try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e) {}
        }
    }
    if (!http_request) {
        alert('Se ha producido un error. (objeto XMLHTTP)');
        return false;
    }
    return http_request;
}

function campo_erroneo(obj,img) {
    obj.style.borderColor = "red";
    obj.style.background  = "#ffdddd";
    document.getElementById(img).src = "foro/imagenes/cross.gif";
}

function campo_correcto(obj,img) {
    obj.style.borderColor = "#bbbbbb";
    obj.style.background  = "white";
    document.getElementById(img).src = "foro/imagenes/blanco.gif";
}

//Validacion campos de formularios

function validar_campo_texto(obj,img) {
    var valor = obj.value;
    if (valor == "") { campo_erroneo(obj,img);  }
    else             { campo_correcto(obj,img); }
}

function crear_ventana_foro() {
    foro = new Window(Math.round(Math.random()*999).toString(), {className: "alphacube", title: "Foro FesTVal", draggable:true, width:800, height:560, top:30, left:200});
}

function addForumContent() {
    if (http_request.readyState == 4) {
        texto  = unescape(http_request.responseText);
        texto2 = texto.replace(/\+/gi," ");
        foro.getContent().innerHTML = texto2;
    }
}

function abrir_foro() {
    crear_ventana_foro();
    http_request                    = obtenerRequest();
    http_request.onreadystatechange = addForumContent;
    http_request.open('GET', 'foro_temas.php', true);
    http_request.send(null);
    foro.show();
}

function ver_respuestas(tema,pagina) {
    foro.getContent().innerHTML = '<br><br><br><br><br><br><br><br><center><img src="foro/imagenes/processbar.gif"><br><b>Cargando...</b></center>';
    http_request                    = obtenerRequest();
    http_request.onreadystatechange = addForumContent;
    http_request.open('GET', 'foro_respuestas.php?tema=' + tema + '&pag=' + pagina, true);
    http_request.send(null);
}

function ver_temas(pagina) {
    foro.getContent().innerHTML = '<br><br><br><br><br><br><br><br><center><img src="foro/imagenes/processbar.gif"><br><b>Cargando...</b></center>';
    http_request                    = obtenerRequest();
    http_request.onreadystatechange = addForumContent;
    http_request.open('GET', 'foro_temas.php?pag=' + pagina, true);
    http_request.send(null);
}


//INSERTAR TEMA Y RESPUESTA



function insertar_tema(pag) {
    texto = '<div id="ins_tema_msg">&nbsp;</div><br>';
    texto += '<table border="0" width="90%" align="center">';
    texto += '<tr><td style="width:10px;"><b>Alias:</b></td><td align="left"><input type="text" size="30" maxlength="30" id="alias" name="alias" onBlur="validar_campo_texto(this,&quot;alias_img1&quot;);"/>&nbsp;<img id="alias_img1" src="foro/imagenes/blanco.gif"></td></tr>';
    texto += '<tr><td style="width:10px;"><b>Tema:</b></td><td align="left"><input type="text" size="50" id="tema" name="tema" onBlur="validar_campo_texto(this,&quot;tema_img1&quot;);"/>&nbsp;<img id="tema_img1" src="foro/imagenes/blanco.gif"></td></tr>';
    texto += '</table>';
    texto += '<iframe name="frame_tema" id="frame_tema" src="foro_insertar_tema.php" width="500px" height="260px" frameborder="0"></iframe>';
    Dialog.confirm(texto, {windowParameters: {className:"alphacube", width:500, Height:250 }, 
             okLabel: "Aceptar",
             cancelLabel: "Cancelar",
             ok:function(win){
             	 if (($('tema').value == "") || ($('alias').value == "")) {
                       $('ins_tema_msg').className = 'error';
                       $('ins_tema_msg').innerHTML = 'El alias y el tema no pueden estar vacios.';
                       new Effect.Shake(Windows.focusedWindow.getId());
                       return false;
             	 }
             	else{
                   t0 = window.parent.frames[0].document.getElementById('alias');
             	   t0.value = $('alias').value;
             	   t1 = window.parent.frames[0].document.getElementById('tema');
             	   t1.value = $('tema').value;
             	   t2 = window.parent.frames[0].document.getElementById('formu');
             	   t2.submit();
                   alert("El tema ha sido introducido correctamente");
                   window.location.href='foro_temas.php';
             	   return true;
             	}
             }
            });
}



function insertar_resp(tema,pag) {
    texto = '<div id="ins_resp_msg">&nbsp;</div><br>';
    texto += '<table border="0" width="90%" align="center">';
    texto += '<tr><td style="width:10px;"><b>Alias:</b></td><td align="left"><input type="text" size="30" maxlength="30" id="alias" name="alias" onBlur="validar_campo_texto(this,&quot;alias_img1&quot;);"/>&nbsp;<img id="alias_img1" src="foro/imagenes/blanco.gif"></td></tr>';
    texto += '</table>';
    texto += '<iframe name="frame_resp" id="frame_resp" src="foro_insertar_respuesta.php?tema=' + tema + '" width="500px" height="260px" frameborder="0"></iframe>';
    Dialog.confirm(texto, {windowParameters: {className:"alphacube", width:500, Height:260 }, 
             okLabel: "Aceptar",
             cancelLabel: "Cancelar",
             ok:function(win){
                 if ($('alias').value == "") {
                       $('ins_resp_msg').className = 'error';
                       $('ins_resp_msg').innerHTML = 'El alias no puede estar vacio.';
                       new Effect.Shake(Windows.focusedWindow.getId());
                       return false;
             	 }
                 else{
                   t0 = window.parent.frames[0].document.getElementById('alias');
             	   t0.value = $('alias').value;
             	   t1 = window.parent.frames[0].document.getElementById('formu');
             	   t1.submit();
                   alert("La respuesta ha sido introducida correctamente");
                   window.location.href='foro_respuestas.php?tema='+tema+'&pag='+pag;
             	   return true;
                 }
             }
            });
}


function foro_tema_borrar(tema,autor,pagina) {
    if(confirm("¿Desea eliminar el tema introducido por " + autor + "?")) { 
        ajax = obtenerRequest();
        ajax.open("POST", "foro_tema_borrar.php", true);
        ajax.onreadystatechange=function() {
           if (ajax.readyState==4) {
              if (ajax.status == 200) {
                  window.location= 'foro_temas.php?pag='+pagina;
               }
            }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("tema=" + tema);
    }
}


function foro_respuesta_borrar(tema, respuesta, autor, pagina) {
    if(confirm("¿Desea eliminar la respuesta introducida por " + autor + "?")) { 
        ajax = obtenerRequest();
        ajax.open("POST", "foro_respuesta_borrar.php", true);
        ajax.onreadystatechange=function() {
           if (ajax.readyState==4) {
              if (ajax.status == 200) {
                  window.location= 'foro_respuestas.php?tema='+tema+'&pag='+pagina;
               }
            }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("tema=" + tema + "&respuesta=" + respuesta);
    }
}


// Taller

function validar_taller(formulario) {
  if (formulario.nombre.value.length == 0) {
    alert("El nombre es un campo obligatorio.");
    formulario.nombre.focus();
    return (false);
  }
  if (formulario.apellidos.value.length == 0) {
    alert("Los apellidos son un campo obligatorio.");
    formulario.apellidos.focus();
    return (false);
  }
  if (formulario.edad.value.length == 0) {
    alert("La edad es un campo obligatorio.");
    formulario.edad.focus();
    return (false);
  }
  if (formulario.telefono.value.length == 0) {
    alert("El teléfono es un campo obligatorio.");
    formulario.telefono.focus();
    return (false);
  }
  return (true);
}


//NOTICIAS

function noticia_borrar(identificador, noticia) {
    if(confirm("¿Desea eliminar la noticia: " + noticia + "?")) { 
        ajax = obtenerRequest();
        ajax.open("POST", "noticia_borrar.php", true);
        ajax.onreadystatechange=function() {
           if (ajax.readyState==4) {
              if (ajax.status == 200) {
                  window.location= 'noticias_listado.php';
               }
            }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("id="+identificador);
    }
}

function validar_noticia_insertar(formulario) {
  if (formulario.titulo.value.length == 0) {
    alert("El titulo es un campo obligatorio.");
    formulario.titulo.focus();
    return (false);
  }
  return (true);
}

//GALERIAS

function videos_galerias_borrar(id,galeria) {
    if(confirm("¿Desea eliminar la galeria: " + galeria + "?")) { 
        ajax = obtenerRequest();
        ajax.open("POST", "videos_galerias_borrar.php", true);
        ajax.onreadystatechange=function() {
           if (ajax.readyState==4) {
              if (ajax.status == 200) {
                  window.location= 'videos_galerias_listado.php';
               }
            }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("id="+id+"&galeria="+galeria);
    }
}

function validar_videos_galerias_insertar(formulario) {
  if (formulario.nombre.value.length == 0) {
    alert("El nombre es un campo obligatorio.");
    formulario.nombre.focus();
    return (false);
  }
  return (true);
}

// VIDEOS

function videos_borrar(id,galeria_id,galeria,video) {
    if(confirm("¿Desea eliminar el video?")) { 
        ajax = obtenerRequest();
        ajax.open("POST", "videos_borrar.php", true);
        ajax.onreadystatechange=function() {
           if (ajax.readyState==4) {
              if (ajax.status == 200) {
                  window.location= 'videos_listado.php?id='+galeria_id + '&galeria=' + galeria;
               }
            }
        }
        ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        ajax.send("id="+id+"&galeria="+galeria+"&video="+video);
    }
}

function validar_videos_insertar(formulario) {
  if (formulario.imagen.value.length == 0) {
    alert("La imagen es un campo obligatorio.");
    formulario.imagen.focus();
    return (false);
  }
  if (formulario.url.value.length == 0) {
    alert("El enlace es un campo obligatorio.");
    formulario.url.focus();
    return (false);
  }
  if (formulario.video.value.length == 0) {
    alert("El video es un campo obligatorio.");
    formulario.video.focus();
    return (false);
  }
  return (true);
}
