/// JavaScript Document

// MEN� DESPLEGABLE
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// ABRE VENTANA
function abreVentana(url,target,width,height){
	var h=(screen.height-height)/2;
	var w=(screen.width-width)/2;
	var woptions='toolbar=no,directories=no,menubar=no,location=no,resizable=no,top='+h+',left='+w+',scrollbars=yes,status=no,width=' + width + ',height=' + height;
	var FLOAT=open(url,target,woptions);
	if (FLOAT.focus!=null) FLOAT.focus();
}


// AJAX
function nuevoAjax(){
	var xmlhttp=false;
	 try {
	 xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	 }
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	 xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

// COMENTAR NOTICIA
function comentarNoticia(){
	var des, nom, email, comentario, id_curso, area, enviado;
	enviado = document.getElementById('enviado');
	nom = document.getElementById('nombre').value;
	email = document.getElementById('email').value;
	comentario = document.getElementById('comentario').value;
	id_curso = document.getElementById('id_curso').value;
	area = document.getElementById('area').value;
	if( (nom=="") || (email=="") || (comentario=="") ){
		enviado.innerHTML = "<p>:: Falta completar datos ::</p>";
	}else{
		ajax=nuevoAjax();
		ajax.open("POST", "contenidos/enviar_comentario.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==1) {
				enviado.innerHTML = '<img src=img/precarga.gif />'
			}
	
			else if (ajax.readyState==4) {
				enviado.innerHTML = ajax.responseText
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		//ajax.setRequestHeader("Accept-Charset", "ISO-8859-1;q=0.7");
		ajax.send("nom="+nom+"&email="+email+"&comentario="+comentario+"&id_curso="+id_curso+"&area="+area)
	}
}

// COMENTAR POST
function comentarPost(){
	var id_tema, nom, email, url,comentario, enviado;
	
	id_tema = document.getElementById('id_tema');
	nom = document.getElementById('nombre');
	email = document.getElementById('email');
	comentario = document.getElementById('comentario');
	enviado = document.getElementById('respuesta');
	
	
	if( (nom.value=="") || (email.value=="") || (comentario.value=="") ){
		
		enviado.style.backgroundColor = "#FFE6E7";
		enviado.style.border = "solid 1px #A5ACB2";
		enviado.style.fontSize = "11px";
		enviado.innerHTML = "Ha habido omisiones al completar el formulario.<br />Complet� todos los campos que te son indicados abajo.";

		
	}else{
		ajax=nuevoAjax();
		ajax.open("POST", "contenidos/enviar_comentario_foro.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==1) {
				enviado.innerHTML = '<img src=img/precarga.gif />'
			}
	
			else if (ajax.readyState==4) {
				enviado.style.backgroundColor = "#E6FFEC";
				enviado.style.border = "solid 1px #A5ACB2";
				enviado.style.fontSize = "11px";
				enviado.innerHTML = ajax.responseText
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("nom="+nom.value+"&email="+email.value+"&comentario="+comentario.value+"&id_tema="+id_tema.value)
	}
}

// CALIFICAR NOTICIA
function calificarNoticia(){
	var calificacion, id_curso, area, enviado, resultados, lk;
	enviado = document.getElementById('enviado');
	resultados = document.getElementById('calificarNoticia');
	calificacion = document.getElementsByName("cali");
	lk = document.getElementById('lk_cali');
	id_curso = document.getElementById('id_curso').value;
	area = document.getElementById('area').value;
	for(var i=0;i<calificacion.length;i++){
		if(calificacion[i].checked){
			var cal = calificacion[i].value;
			break;
		} else {
			var cal = 0;
		}
	}
	
	if(cal==0){
		enviado.innerHTML = "<p>:: Por favor, califique la noticia ::</p>";
	}else{
		ajax=nuevoAjax();
		ajax.open("POST", "contenidos/enviar_calificacion.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==1) {
				enviado.innerHTML = '<img src=img/precarga.gif /><br>Guardando...'
			}
	
			else if (ajax.readyState==4) {
				lk.href="javascript:void(0)";
				lk.innerHTML = "Usted ya voto";
				resultados.innerHTML = ajax.responseText
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("calificacion="+cal+"&id_curso="+id_curso+"&area="+area)
	}
}


// RECOMENDAR NOTICIA
function recomendarNoticia(){
	var des, nom, enviado;
	enviado = document.getElementById('enviado');
	nom = document.getElementById('nombre').value;
	des = document.getElementById('destinatario').value;
	if( (nom=="") || (des=="") ){
		enviado.innerHTML = "<p class='rojo'>:: Falta completar datos ::</p>";
	}else{
		ajax=nuevoAjax();
		ajax.open("POST", "contenidos/enviar_nota.php",true);
		ajax.onreadystatechange=function() {
			if (ajax.readyState==1) {
				enviado.innerHTML = '<img src=img/precarga.gif />'
			}
	
			else if (ajax.readyState==4) {
				enviado.innerHTML = ajax.responseText
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax.send("nom="+nom+"&des="+des)
	}
}

// MOSTRAR COMENTARIOS
function mostrarComentarios(id_curso){
	var respuesta;
	respuesta = document.getElementById('mostrarComentarios');
	ajax=nuevoAjax();
	ajax.open("POST", "contenidos/mostrar_comentarios.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==1) {
			respuesta.innerHTML = "<p align='center'><br /><img src=img/precarga.gif /><br>Cargando comentarios...</p>"
		}

		else if (ajax.readyState==4) {
			respuesta.innerHTML = ajax.responseText
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id_curso="+id_curso)
}

//INICIALIZAR BOX DE NOTICIAS
function inicializarBoxNoticia(id_area,id_categoria){
	var respuesta;
	respuesta = document.getElementById('boxNoticiasInteriorCuerpo');
	
	ajax=nuevoAjax();
	ajax.open("POST", "contenidos/listar_box_noticias.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==1) {
			respuesta.innerHTML = "<p align='center'><br /><img src=img/precarga.gif /><br>Cargando Noticias...</p>"
		}

		else if (ajax.readyState==4) {
			respuesta.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id_categoria="+id_categoria+"&idArea="+id_area)
}


//BOX DE NOTICIAS
function cambiarBoxNoticia(elem,id_area,id_categoria){
	var respuesta;
	respuesta = document.getElementById('boxNoticiasInteriorCuerpo');
	
	var elemLI = elem.parentNode.getElementsByTagName('li');
	for(i=0; i < elemLI.length; i++){
		if(elemLI[i].id=="selected"){
			elemLI[i].id = "";
		}
		elem.id="selected";
	}
	
	ajax=nuevoAjax();
	ajax.open("POST", "contenidos/listar_box_noticias.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==1) {
			respuesta.innerHTML = "<p align='center'><br /><img src=img/precarga.gif /><br>Cargando Noticias...</p>"
		}

		else if (ajax.readyState==4) {
			respuesta.innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id_categoria="+id_categoria+"&idArea="+id_area)
}

// BUSQUEDA GENERAL
function borrarInputBusqueda(){
	var inputBus;
	inputBus = document.getElementById('busquedaGral');
	inputBus.value = "";
}

function volverInputBusqueda(){
	var inputBus;
	inputBus = document.getElementById('busquedaGral');
	if(inputBus.value==""){
		inputBus.value = "Buscar...";
	}
}

function busquedaGral(){
	var inputBus;
	inputBus = document.getElementById('busquedaGral');
	if(inputBus.value!="Buscar..."){
		location.href = "index.php?contenido=busquedaGral.php&busqueda="+inputBus.value;
	}
	
}

function cambiarEstrellita(elem,tipo){
	var estrellita;
	estrellita = elem.id.split("-");
	
	if(tipo=='N'){
		for(var i=0;i<estrellita[1];i++){
			var elemSel;
			elemSel = "estrellita-"+(i+1);
			document.getElementById(elemSel).src = "img/estrellitaNaranja.jpg";
		}
	} else {
		for(var i=0;i<estrellita[1];i++){
			var elemSel;
			elemSel = "estrellita-"+(i+1);
			document.getElementById(elemSel).src = "img/estrellitaGris.jpg";
		}
	}
	
}

function tamFuente(tam){
	var cuerpo, font, vFont;
	
	cuerpo = document.getElementById("contenidosCuerpoEditorHTML");
	font = parseInt( cuerpo.style.fontSize.substring(0,2) );

	if(tam=='A'){
		if(cuerpo.style.fontSize==""){
			cuerpo.style.fontSize = "14px";
		} else if(font < 18){
			cuerpo.style.fontSize = (font+1)+"px";
		}	
	} else {
		if(cuerpo.style.fontSize==""){
			cuerpo.style.fontSize = "13px";
		} else if(font > 13){
			cuerpo.style.fontSize = (font-1)+"px";
		}	
	}
	
	
}

function teclaEnter(e){
	var key;
	key = e.keyCode;
	if (key == 13) {
		busquedaGral();
	}	
}

function showContent(vThis)
{
// http://www.javascriptjunkie.com
// alert(vSibling.className + " " + vDef_Key);
vParent = vThis.parentNode;
vSibling = vParent.nextSibling;
while (vSibling.nodeType==3) { // Fix for Mozilla/FireFox Empty Space becomes a TextNode or Something
vSibling = vSibling.nextSibling;
};
if(vSibling.style.display == "none")
{
vThis.src="img/coment.png";
vThis.alt = "Cerrar";
vSibling.style.display = "block";
} else {
vSibling.style.display = "none";
vThis.src="img/coment.png";
vThis.alt = "ver";
}
return;
}

//CONTROLAR COMENTAR NOTICIA
function cnControl(){
	var form, cn_autor, cn_autor_email, cn_comentario;
	form = document.getElementById('formCN');
	cn_autor = document.getElementById('cn_autor');
	cn_autor_email = document.getElementById('cn_autor_email');
	cn_comentario = document.getElementById('cn_comentario');
	
	if(cn_autor.value==""){
		cn_autor.focus();
	}else if(cn_autor_email.value==""){
		cn_autor_email.focus();
	}else if(cn_comentario.value==""){
		cn_comentario.focus();
	}else{
		form.submit();	
	}
}



