function xmlhttpPost(str,Url,tipo) {
	var xmlHttpReq = false;
	var self = this;
	var strURL = Url;
	self.opt=tipo;
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		try
	    {
	        self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
	    }   
	    catch (err)
	    {
	        try
	        {
	            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	        }
	        catch (err2)
	        {
	        }
	    }
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function(tipo) {
		if (self.xmlHttpReq.readyState == 4) {

			switch (self.opt){
				case 1:	mostrarResultadoNoticiaFavorita(self.xmlHttpReq.responseText);
				break;
				case 2:	updateVotos(self.xmlHttpReq.responseText);
				break;
				case 3:	updateSuscrito(self.xmlHttpReq.responseText);
				break;
				case 4:	updateFormAgregarNoticiaAGrupo(self.xmlHttpReq.responseText);
				break;
				case 5: processPOST(self.xmlHttpReq);
				break;
				case 6: processPOSTComentario(self.xmlHttpReq);
				break;
				case 7: processDeleteRecord(self.xmlHttpReq);
				break;
				case 8: proccessVotoComentario(self.xmlHttpReq);
				break;
				case 9: actualizarContenidoDenuncia(self.xmlHttpReq.responseText);
				break;
				case 10: actualizarContenidoAlerta(self.xmlHttpReq.responseText);
				break;
				case 11: actualizarContenidoAmbitoPais(self.xmlHttpReq.responseText);
				break;
				case 12: mostrarResultadoUsuarioFavorito(self.xmlHttpReq.responseText);
				break;
				case 13: mostrarResultadoBorrarUsuarioFavorito(self.xmlHttpReq.responseText);
				break;
				case 14: mostrarResultadoSumarEnvioNoticia(self.xmlHttpReq.responseText);
				break;
			}
		}
	}
	self.xmlHttpReq.send(str);
}

// anadir una noticia como favorita
function anadirNoticiaFavorita(codNoticia) {
	qstr = 'codNoticia=' + codNoticia;
	xmlhttpPost(qstr, '/ajax-calls/anadirNoticiaFavorita.phtml', 1);
}

function mostrarResultadoNoticiaFavorita(res) {
	if (res == 1) {
		document.getElementById('capaFavoritaNot').innerHTML = '<span class="mv_favoritos_quitar">A&ntilde;adida</span>';
	}
}

// anadir un usuario como favorito
function anadirUsuarioFavorito(codUsuario) {
	qstr = 'codFavorito=' + codUsuario;
	xmlhttpPost(qstr, '/ajax-calls/anadirFavoritosUsuarioSidebar.phtml', 12);
}

function mostrarResultadoUsuarioFavorito(res) {
	if (res) {
		document.getElementById('spriteUsuarioFavorito').innerHTML = res;
		document.getElementById('li-resp-usuario').style.display='';
	}
}

// eliminar un usuario como favorito
function borrarUsuarioFavorito(codUsuario) {
	qstr = 'codFavorito=' + codUsuario;
	xmlhttpPost(qstr, '/ajax-calls/borrarFavoritosUsuarioSidebar.phtml', 13);
}

function mostrarResultadoBorrarUsuarioFavorito(res) {
	if (res) {
		document.getElementById('spriteUsuarioFavorito').innerHTML =  res;
		document.getElementById('li-resp-usuario').style.display='none';
	}
}

function estilos(){
	
}


function updateSuscrito(str){
	document.getElementById("DIV-suscribirse").innerHTML = str;
}

function updateVotos(str){
	if (str=='Error') {
		document.getElementById("DIV-NVOTOSUP").innerHTML = str;
	}
	/*else {
		document.getElementById("DIV-NVOTOSUP").innerHTML = str;
		document.getElementById("DIV-NVOTOSDOWN").innerHTML = str;
	}*/
}

function suscribirse(codUsuario){
	qstr = 'codUsuario=' + codUsuario + '&tipo=1';  
	xmlhttpPost(qstr,"/verNoticia/suscribirse.phtml",3);
}
function quitarSuscrito(codUsuario){
	qstr = 'codUsuario=' + codUsuario + '&tipo=2';  
	xmlhttpPost(qstr,"/verNoticia/suscribirse.phtml",3);
}
function votoPlus(codNoticia){
	var noVotar='';
	noVotar+='<img src="/img/ico_voto_up_off.gif" /><img src="/img/ico_voto_down_off.gif" />';
	document.getElementById("DIV-VOTARUP").innerHTML = noVotar;
	document.getElementById("DIV-NVOTOSUP").innerHTML++ ;
	document.getElementById("DIV-CAJAVOTOSUP").className="votada" ;
	qstr = 'codNoticia='+codNoticia+'&voto=1';
	xmlhttpPost(qstr,"/verNoticia/votoNoticia.phtml",2);
}
function votoMinus(codNoticia){
	var noVotar='';
	noVotar+='<img src="/img/ico_voto_up_off.gif" /><img src="/img/ico_voto_down_off.gif" />';
	document.getElementById("DIV-VOTARUP").innerHTML = noVotar;
	document.getElementById("DIV-NVOTOSUP").innerHTML-- ;
	document.getElementById("DIV-CAJAVOTOSUP").className="votada" ;
	qstr = 'codNoticia='+codNoticia+'&voto=0';
	xmlhttpPost(qstr,"/verNoticia/votoNoticia.phtml",2);
}

function votoPlusPerfil(codNoticia){
	var noVotar='';
	noVotar+='<img src="/img/ico_voto_up_off.gif" /><img src="/img/ico_voto_down_off.gif" />';
	document.getElementById("DIV-VOTARUP").innerHTML = noVotar;	
	document.getElementById("DIV-NVOTOSUP").innerHTML++ ;	
	document.getElementById("DIV-CAJAVOTOSUP").className="votada" ;	
	qstr = 'codNoticia='+codNoticia+'&voto=1';
	xmlhttpPost(qstr,"/verNoticia/votoNoticia.phtml",2);
}
function votoMinusPerfil(codNoticia){
	var noVotar='';
	noVotar+='<img src="/img/ico_voto_up_off.gif" /><img src="/img/ico_voto_down_off.gif" />';
	document.getElementById("DIV-VOTARUP").innerHTML = noVotar;	
	document.getElementById("DIV-NVOTOSUP").innerHTML-- ;	
	document.getElementById("DIV-CAJAVOTOSUP").className="votada" ;	
	qstr = 'codNoticia='+codNoticia+'&voto=0';
	xmlhttpPost(qstr,"/verNoticia/votoNoticia.phtml",2);
}
function sumarEnvio(codNoticia, tipo){
	qstr = 'codNoticia=' + codNoticia + '&tipo=' + tipo;
	xmlhttpPost(qstr,"/verNoticia/sumarEnvio.phtml",14);
}
function mostrarResultadoSumarEnvioNoticia(res){
	
}

var codNoticia;
var enlace;
var registrado;
var pagina=1;

function init(noticia) {
	codNoticia=noticia;
	estilos();
}

function getXMLget(url) {
	var xmlHttpReq = false;
	var self = this;
	var strURL = url+'?'+crearQueryString();;
	if (window.XMLHttpRequest) self.xmlHttpReq = new XMLHttpRequest();
	else if (window.ActiveXObject) {
		try {self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");}   
	    catch (err){
	        try {self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");}
	        catch (err2){}
	    }
	}
	self.xmlHttpReq.open('GET', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			processXML(self.xmlHttpReq);
		}
	}
	
	self.xmlHttpReq.send(null);
}


function crearQueryString(){
	var str;
	str="coments=1";
	str+="&codNoticia="+codNoticia;
	str+="&pag="+pagina;
	return str;
}
function crearQueryStringPost(padre,comentario){
	var str;
	//comentario=comentario.replace("ñ","HV_ENE"); 
	str="codPadre="+padre;
	str+="&comentario="+encodeURIComponent(comentario);
	str+="&codNoticia="+codNoticia;
	//str=str.replace("HV_ENE","ñ"); 
	return str;
}
function crearQueryStringEliminar(cod){
	var str;
	str="id="+cod;
	return str;
}

function paginar(num){
	pagina = num;
	getXMLget("/verNoticia/comentarios.phtml");
}

function lTrim(sStr){
 while (sStr.charAt(0) == " ") 
  sStr = sStr.substr(1, sStr.length - 1);
 return sStr;
}

function rTrim(sStr){
 while (sStr.charAt(sStr.length - 1) == " ") 
  sStr = sStr.substr(0, sStr.length - 1);
 return sStr;
}

function Trim(sStr){
 return rTrim(lTrim(sStr));
}

function yaVotado(registrado){
	if (registrado){
		alert('Ya has votado este noticia');
	}
	else{
		alert("Registrate o logueate si deseas volver a votar.");
	}
}

function processDeleteRecord(){
	getXMLget("/verNoticia/comentarios.phtml");
}

function popUp(URL) {
	id = "popUpNoticia";
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=620,height=440');");
}

// popUp de enviar noticia
function popEnviar(codNoticia){
	window.open("/enviarNoticia/index.phtml?codNoticia="+codNoticia,"EnviarNoticia","width=780,height=580,resizable=no,scrollbars=no,status=0");
}

function popEnviarPost(idPost){
	window.open("/ext/enviarNoticia/"+idPost+"/","EnviarNoticia","width=780,height=580,resizable=no,scrollbars=no,status=0");
}

function deshabilitado(){
	alert('Función deshabilitada por tareas de mantenimiento.');
}

function activarCapa(id){
	desactivarTodasLasCapas();
	document.getElementById(id).className='current';
	document.getElementById('div-'+id).style.display='';
}

function activarBloque(id){
	desactivarTodosLosBloques();
	document.getElementById(id).className='current';
	document.getElementById('div-'+id).style.display='';
}

function desactivarTodosLosBloques(){
	document.getElementById('leido').className='';
	document.getElementById('votado').className='';
	document.getElementById('comentado').className='';
	document.getElementById('div-leido').style.display='none';
	document.getElementById('div-votado').style.display='none';
	document.getElementById('div-comentado').style.display='none';

}	

function desactivarTodasLasCapas(){
	document.getElementById('relacionados').className='';
	document.getElementById('masapirel').className='';
	document.getElementById('div-relacionados').style.display='none';
	document.getElementById('div-masapirel').style.display='none';
}

//DENUNCIAS
function cambioDeDenuncia(obj){
	capaModal=document.getElementById('ventana-modal-ventana');
	elementos=capaModal.getElementsByTagName('tr');
	for (var i=0 ;i< elementos.length; i++) {
		capa=elementos[i];
		if (capa.id=='div-urlDenuncia'){
			if (obj.selectedIndex == 3 || obj.selectedIndex == 5) capa.style.display='';
			else capa.style.display='none';
		}
	}
}

function obtenerElementoDeCapaModalPorId(capaModal, tipo, id){
	elementos=capaModal.getElementsByTagName(tipo);
	for (var i=0 ;i< elementos.length; i++) {
		elem=elementos[i];
		if (elem.id==id) return elem;		
	}
}

function actualizarContenidoDenuncia(request) {
	capaModal=document.getElementById('ventana-modal-ventana');
	contenido=obtenerElementoDeCapaModalPorId(capaModal,'td','contenidoCM');
	contenido.innerHTML=request;
}

function enviarDenuncia(codNoticia){
	capaModal=document.getElementById('ventana-modal-ventana');
	tipo=obtenerElementoDeCapaModalPorId(capaModal, 'select', 'tipoDenuncia')
	comentario=obtenerElementoDeCapaModalPorId(capaModal, 'textarea', 'comentarioDenuncia')
	url=obtenerElementoDeCapaModalPorId(capaModal, 'input', 'urlDenuncia')
	var url = "/verNoticia/denunciar.phtml";
	var querystring = "codNoticia="+codNoticia+"&tipoDenuncia="+tipo.value+"&url="+encodeURIComponent(url.value);
	querystring+="&comentario="+encodeURIComponent(comentario.innerHTML);
	xmlhttpPost(querystring,url,9);
}

function suscribirAlertas(codSeudonimo){
	capaModal=document.getElementById('ventana-modal-ventana');
	email=obtenerElementoDeCapaModalPorId(capaModal, 'input', 'email');
	var url = "/alertas-email/index.phtml";
	var querystring = "accion=suscribirse&email="+email.value+"&seudonimo="+codSeudonimo;
	xmlhttpPost(querystring,url,10);
}

function desuscribirAlertas(codSeudonimo,email){
	capaModal=document.getElementById('ventana-modal-ventana');
	var url = "/alertas-email/index.phtml";
	var querystring = "accion=desuscribirse&email="+email+"&seudonimo="+codSeudonimo;
	xmlhttpPost(querystring,url,10);
}

function actualizarContenidoAlerta(request){
	capaModal=document.getElementById('ventana-modal-ventana');
	contenido=obtenerElementoDeCapaModalPorId(capaModal,'td','contenidoCM');
	contenido.innerHTML=request;
}

//PARA ABRIR LOS ENLACES EN UNA VENTANA NUEVA
function strpos( haystack, needle, offset){
	// Finds position of first occurrence of a string within another  
	var i = (haystack+'').indexOf( needle, offset ); 
	return i===-1 ? false : i;
}

function openTargetBlank(e){
	if (!e) var e = window.event;
	
	if (e['button'] == '0' || e['button'] == '1') {
		var clickedObj = e.target ? e.target : e.srcElement;
		if(clickedObj.nodeName == 'A' ) var dest=clickedObj.href;
		else if(clickedObj.parentNode.nodeName=='A') var dest=clickedObj.parentNode.href;
		else return;
		if(strpos(dest,'http://',0)!==false && strpos(dest,'globedia.com',0)===false){
			window.open(dest);
			return false;
		}
	}
}

function mostrarInfo(codCapa, numCapas){
	for(i=0 ; i<numCapas;i++) if (i!=codCapa) document.getElementById('IDINFO'+i).style.display='none'
	if (document.getElementById('IDINFO'+codCapa).style.display=='') document.getElementById('IDINFO'+codCapa).style.display='none';
	else document.getElementById('IDINFO'+codCapa).style.display='';
}

function cambiarLeyenda(txt){
	document.getElementById('div-cab-leyenda').innerHTML=txt;
}
   
document.onclick = openTargetBlank;
