var numimages=0;
var imagenes=new Array();
var messageObj=null;

//Funciones para el manejo de cabecera
function getNombre (name) {
	return(unescape(name.substring(0,name.indexOf("&"))));
}

function getNumero (name) {
	var aux = name.indexOf("&");
	var aux2 = name.indexOf("&",aux+1);
	return(unescape(name.substring(aux+1,aux2)));
}
function parte_usuario () {
var usuario=getCookie('VandalUser');
if(usuario) {
	mostrar_opciones_usuario(getNombre(usuario),getNumero(usuario));
}
else {
	mostrar_opciones_login();
}
}

function almacenar_visita_mensaje (numeromensaje) {
	var mensajesvisitas=getCookie('mensajesvisitados');
	var mensajes2=new Array();
	var mensajes=mensajesvisitas.split("m");
	var ahora=instante();
	var i=0;
	var tamanho=mensajes.length;
	for (i=0;i<mensajes.length;i++) {
		var datosm=mensajes[i].split("-");
		if(datosm[0] && datosm[0]!= numeromensaje) {
			mensajes2.push(datosm.join("-"));
		}
	}
	mensajes2.unshift(numeromensaje+"-"+ahora);
	if(mensajes2.length>50) {
		mensajes2.pop();
	}
	setCookie("mensajesvisitados",mensajes2.join("m"),20160);
}

function procesar_novedades () {
	var novedadesi=getCookie('novedadesforo');
	if(novedadesi != 'no') {
		escribir_novedades();
	}
	else {
		activar_desactivar('novedadesno');
	}
}

function escribir_novedades () {
	var mensajesvisitas=getCookie('mensajesvisitados');
	var mensajes=mensajesvisitas.split("m");
	var resultado="";
	var contador=0;
	var coloresquetocan = new Array ("#e0e0e0","#F7F7F7");
	for(var i in mensajes) {
		var datosm=mensajes[i].split("-");
		if(document.getElementById('filamensaje'+datosm[0]) == null) {
			continue;
		}
		if(document.getElementById('filamensaje'+datosm[0]).title > datosm[1]) {
			resultado+='<tr bgcolor="'+coloresquetocan[contador%2]+'" class="texto2tt">' + document.getElementById('filamensaje'+datosm[0]).innerHTML + '</tr>';
			contador++;
		}
		if(contador==10) {
			break;
		}
	}
	if(resultado!="") {
		resultado='<table width="889" cellpadding="0" cellspacing="0" border="0" style="border-bottom:1px solid #9d9d9d;"><tr><td style="background-image:url(\'http://img.vandal.net/smilies/fondocats.gif\');" class="texto"> </td><td style="background-image:url(\'http://img.vandal.net/smilies/fondocats.gif\');"></td><td class="texto" align="center" height="22" style="width:514; background-image:url(\'http://img.vandal.net/smilies//fondocats.gif\');text-align:left;">&nbsp;<b>Mensaje</b></td><td class="textott" align="center" style="width:80px;background-image:url(\'http://img.vandal.net/smilies/fondocats.gif\');" height="22"><b>Réplicas</b></td><td class="textott" align="center" style="width:160px; text-align:right;background-image:url(\'http://img.vandal.net/smilies/fondocats.gif\');" height="22" ><b>Última</b>&nbsp;&nbsp;</td></tr>' + resultado + '</table>';
	}
	if(contador>0) {
		document.getElementById('novedadesdata').innerHTML=resultado;
		activar_desactivar('novedades');
	}
}

function abrir_novedades () {
	setCookie("novedadesforo","si",200160);
	activar_desactivar('novedadesno');
	escribir_novedades();
}

function cerrar_novedades () {
	setCookie("novedadesforo","no",200160);
	activar_desactivar('novedades');
	activar_desactivar('novedadesno');
}

function dameforo () {
	var foro =getCookie("foro");
	if(foro) {
		return(foro);
	}
	else {
		return(foros[0]);
	}
}

function linkeditar (mensaje,post,foro) {
	document.write('<a href="/foro/cgi-bin/foro.cgi\?accion=form_editarpost\&amp;mensaje='+mensaje+'\&amp;numeropost='+post+'\&amp;foro='+foro+'">Editar Post</a>');
}

function denunciar(mensaje,post) {
	if(post==null) {
		post="";
	}
	if(confirm("¿Estás seguro de que quieres avisar al moderador?")) { 
		location="http://www.vandal.net/foro/cgi-bin/foro.cgi?accion=avisarmoderador&mensaje="+mensaje+"&post="+post;
	}
}

function opcionesmoderador(numeropost,numerousuario,numeroreplica) {
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=mirarip&mensaje='+numeropost+'\&replica=' + numeroreplica +'">Mirar IP</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=quitaravatar&usuario=' + numerousuario + '" target="_blank">Quitar Avatar</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=quitarfirma&usuario=' + numerousuario + '" target="_blank">Quitar Firma</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=quitarimagenes&usuario=' + numerousuario + '" target="_blank">Quitar imágenes</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=quitarmultiforo&usuario=' + numerousuario + '" target="_blank">Quitar multiforo</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=expulsar&usuario=' + numerousuario + '" target="_blank">Expulsar</a><br>');
	if (numeroreplica != 1) {
		document.write('<a href="/foro/cgi-bin/foro.cgi?accion=borrarpost&mensaje='+numeropost+'\&post=' + numeroreplica + '" target="_blank">Borrar Post</a><br>');
	}
	document.write('<br><a href="/foro/cgi-bin/foro.cgi?accion=devolveravatar&usuario=' + numerousuario +'" target="_blank">Devolver Avatar</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=devolverfirma&usuario=' + numerousuario + '" target="_blank">Devolver Firma</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=desexpulsar&usuario=' + numerousuario + '" target="_blank">Desexpulsar</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=devolverimagenes&usuario=' + numerousuario + '" target="_blank">Devolver imágenes</a><br>');
	document.write('<a href="/foro/cgi-bin/foro.cgi?accion=devolvermultiforo&usuario=' + numerousuario + '" target="_blank">Devolver multiforo</a><br>');
}

function opcionesmoderador2(numeromensaje) {
	document.write('<p class="texto2" align=center><a href="/foro/cgi-bin/foro.cgi?accion=abrirmensaje&mensaje='+numeromensaje+'">Abrir Tema</a> <a href="/foro/cgi-bin/foro.cgi?accion=cerrarmensaje&mensaje='+numeromensaje+'">Cerrar Tema</a> <a href="/foro/cgi-bin/foro.cgi?accion=indestacarmensaje&mensaje='+numeromensaje+'">Indestacar Tema</a> <a href="/foro/cgi-bin/foro.cgi?accion=destacarmensaje&mensaje='+numeromensaje+'">Destacar Tema</a> <a href="/foro/cgi-bin/foro.cgi?accion=forosmensaje1&mensaje='+numeromensaje+'">Mover a foros</a> <a href="/foro/cgi-bin/foro.cgi?accion=borrarmensaje&mensaje='+numeromensaje+'">Eliminar Tema</a></p>');
}

function opcionesusuario2(numeromensaje) {
	document.write('<p class="texto2" align=center><a href="#" onclick="denunciar('+numeromensaje+',\'\'); return false;"><img src="http://img.vandal.net/smilies/moderador.jpg" border=0></a></p>');
}

function instante (){
	var fecha = new Date();
	return (fecha.getFullYear()*10000+(fecha.getMonth()+1)*100+fecha.getDate()+fecha.getHours()*0.01+fecha.getMinutes()*0.0001);
}


function marca_foro (elemento) {
	var ele = document.getElementById(elemento);
	if(ele.className == 'foroseleccionado') {
		ele.className='';
	}
	else {
		ele.className='foroseleccionado';
	}
	/*var check= document.getElementById('foroopcion'+elemento);
	if(check.checked==true) {
		check.checked=false;
	}
	else {
		check.checked=true;
	}*/
}

function activar_desactivar(elemento) {
	var ele = document.getElementById(elemento);
	if((ele.style.display == 'none') || (ele.style.display == '')) {
		ele.style.display='block';
	} 
	else {
		ele.style.display='none';
	}
}
function ponerimagen (url) {
	//pone una imagen del tamanho adecuado
	imagenes[numimages]=new Image();
	imagenes[numimages].src=url;
	document.write('<div id="imagen'+numimages+'"></div>');
	setTimeout("ponerimagen2("+numimages+")",2000);
	numimages++;
}

function ponerimagen2 (img) {
	if(imagenes[img].complete) {
	if(imagenes[img].width>640) {
		document.getElementById("imagen"+img).innerHTML='<img src="'+imagenes[img].src+'" width=640><br><span class="texto2"><a href="#" onclick="ampliar(\''+imagenes[img].src+'\'); return false;">Ampliar</a></span>';
	}
	else {
		document.getElementById("imagen"+img).innerHTML='<img src="'+imagenes[img].src+'">';
	}
}
else {setTimeout("ponerimagen2("+img+")",2000);
}
}

function generar_ajax () {
	var req = false;
	if (window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		} catch (e) {
			req = false;
		}
	} 
	else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				req = false;
			}
		}
	}
	return req;
}

function generar_aa (numero) {
	var req = generar_ajax();
	var url="http://" + document.domain + "/cgi-bin/amigos_ajax.pl?accion=generarform&usuario="+numero;
	req.onreadystatechange= function () {
		if (req.readyState != 4) {
    		return;
  		}
  		if (req.status == 200) {
    		document.getElementById("ventanamp").innerHTML=req.responseText;
    		document.getElementById("ventanamp").innerHTML+='<br>La ventana se cerrará en tres segundos.';
    		setTimeout("cerrarmodal()",3000);
  		} 
  		else { alert("Error");}
		req = null;
	}
	req.open("GET", url, true);
	var encoded = "";
	req.send(encoded);
	ventanamodal('<div id="ventanamp" class="tn"></div>', (document.body.clientWidth-300)/2, (document.body.clientHeight-300)/2 ,300, 300);
	
}

function generar_mp (numero) {
	var req = generar_ajax();
	var url="http://" + document.domain + "/cgi-bin/mensaje_ajax.pl?accion=generarform&usuario="+numero;
	req.onreadystatechange= function () {
		if (req.readyState != 4) {
    		return;
  		}
  		if (req.status == 200) {
    		document.getElementById("ventanamp").innerHTML=req.responseText;
  		} 
  		else { alert("Error");}
		req = null;
	}
	req.open("GET", url, true);
	var encoded = "";
	req.send(encoded);
	ventanamodal('<div id="ventanamp" class="tn"></div>', (document.body.clientWidth-300)/2, (document.body.clientHeight-300)/2 ,300, 300);
	
}

function enviar_mp (numero, mensaje) {
	var req = generar_ajax();
	var url="http://" + document.domain + "/cgi-bin/mensaje_ajax.pl?accion=enviarmensaje&usuario="+numero;
	req.onreadystatechange= function () {
		if (req.readyState != 4) {
    		return;
  		}
  		if (req.status == 200) {
    		document.getElementById("ventanamp").innerHTML=req.responseText;
    		document.getElementById("ventanamp").innerHTML+='<br>La ventana se cerrará en tres segundos.';
    		setTimeout("cerrarmodal()",3000);
  		} 
  		else { alert("Error");}
		req = null;
	}
	req.open("POST", url, true);
	var encoded = "";
	encoded = "mensaje=" + escape(mensaje);
	req.send(encoded);
}

function puntuar_post(voto,numeropost,numeromensaje) {
	var req = generar_ajax();
	var url="http://" + document.domain + "/foro/cgi-bin/foro.cgi?accion=puntuar_post";
	req.onreadystatechange= function () {
		if (req.readyState != 4) {
    		return;
  		}
  		if (req.status == 200) {
    		document.getElementById("votos"+numeropost).innerHTML=req.responseText;	    		
  		} 
  		else { alert("Error");}
		req = null;
	}
	req.open("POST", url, true);
	var encoded = "";
	encoded = "post=" + escape(numeropost) + "&mensaje="+ escape(numeromensaje)+ "&voto="+ escape(voto);
	req.send(encoded);
	document.getElementById("votos"+numeropost).innerHTML="...</span>";
}

function ampliar(url) {
	//amplia el tamanho de la imagen
	var img = new Image();
	img.src=url;
	var ancho=img.width+60;
	var alto = img.height+40;
	if(alto>document.body.clientHeight) {
		alto=document.body.clientHeight;
	}
	if(ancho>document.body.clientWidth) {
		ancho=document.body.clientWidth;
	}
	var posx= Math.round((document.body.clientWidth-ancho)/2);
	var posy= Math.round((document.body.clientHeight-alto)/2);
	ventanamodal('<img src="'+url+'">', posx, posy ,ancho, alto);
}

function cerrarmodal() {
	messageObj.close();
}
function ventanamodal (contenido,posicionx,posiciony,ancho,alto) {
	if(messageObj==null) {
		messageObj = new DHTML_modalMessage(); 
	}
	messageObj.setHtmlContent(contenido+'<br/><span class="tn"><a href="#" onclick="cerrarmodal();return false">Cerrar</a></span>');
	messageObj.setSize(ancho,alto);
	if(alto==document.body.clientHeight) {
		
	}
	messageObj.display();
}

/**
* @constructor
*/

DHTML_modalMessage = function()
{
	var url;								// url of modal message
	var htmlOfModalMessage;					// html of modal message
	
	var divs_transparentDiv;				// Transparent div covering page content
	var divs_content;						// Modal message div.
	var iframe;								// Iframe used in ie
	var layoutCss;							// Name of css file;
	var width;								// Width of message box
	var height;								// Height of message box
	
	var existingBodyOverFlowStyle;			// Existing body overflow css
	var dynContentObj;						// Reference to dynamic content object
	var cssClassOfMessageBox;				// Alternative css class of message box - in case you want a different appearance on one of them
	var shadowDivVisible;					// Shadow div visible ? 
	var shadowOffset; 						// X and Y offset of shadow(pixels from content box)
	var MSIE;
		
	this.url = '';							// Default url is blank
	this.htmlOfModalMessage = '';			// Default message is blank
	this.layoutCss = 'modal-message.css';	// Default CSS file
	this.height = 200;						// Default height of modal message
	this.width = 400;						// Default width of modal message
	this.cssClassOfMessageBox = false;		// Default alternative css class for the message box
	this.shadowDivVisible = true;			// Shadow div is visible by default
	this.shadowOffset = 5;					// Default shadow offset.
	this.MSIE = false;
	if(navigator.userAgent.indexOf('MSIE')>=0) this.MSIE = true;
	

}

DHTML_modalMessage.prototype = {	
	setSource : function(urlOfSource)
	{
		this.url = urlOfSource;
		
	}	
	,
	setHtmlContent : function(newHtmlContent)
	{
		this.htmlOfModalMessage = newHtmlContent;
		
	}
	,
	setSize : function(width,height)
	{
		if(width)this.width = width;
		if(height)this.height = height;		
	}
	,		
	setCssClassMessageBox : function(newCssClass)
	{
		this.cssClassOfMessageBox = newCssClass;
		if(this.divs_content){
			if(this.cssClassOfMessageBox)
				this.divs_content.className=this.cssClassOfMessageBox;
			else
				this.divs_content.className='modalDialog_contentDiv';	
		}
					
	}
	,	
	display : function()
	{
		if(!this.divs_transparentDiv){
			this.__createDivs();
		}	
		
		// Redisplaying divs
		this.divs_transparentDiv.style.display='block';
		this.divs_content.style.display='block';
		this.divs_shadow.style.display='block';		
		if(this.MSIE)this.iframe.style.display='block';	
		this.__resizeDivs();
		
		/* Call the __resizeDivs method twice in case the css file has changed. The first execution of this method may not catch these changes */
		window.refToThisModalBoxObj = this;		
		setTimeout('window.refToThisModalBoxObj.__resizeDivs()',150);
		
		this.__insertContent();	// Calling method which inserts content into the message div.
	}
	// }}}		
	,
	setShadowDivVisible : function(visible)
	{
		this.shadowDivVisible = visible;
	}
	,
	close : function()
	{
		this.divs_transparentDiv.style.display='none';
		this.divs_content.style.display='none';
		this.divs_shadow.style.display='none';
		if(this.MSIE)this.iframe.style.display='none';
		
	}	
	,
	addEvent : function(whichObject,eventType,functionName,suffix)
	{ 
	  if(!suffix)suffix = '';
	  if(whichObject.attachEvent){ 
	    whichObject['e'+eventType+functionName+suffix] = functionName; 
	    whichObject[eventType+functionName+suffix] = function(){whichObject['e'+eventType+functionName+suffix]( window.event );} 
	    whichObject.attachEvent( 'on'+eventType, whichObject[eventType+functionName+suffix] ); 
	  } else 
	    whichObject.addEventListener(eventType,functionName,false); 	    
	} 
	// }}}	
	,	
	__createDivs : function()
	{
		// Creating transparent div
		this.divs_transparentDiv = document.createElement('DIV');
		this.divs_transparentDiv.className='modalDialog_transparentDivs';
		this.divs_transparentDiv.style.left = '0px';
		this.divs_transparentDiv.style.top = '0px';
		
		document.body.appendChild(this.divs_transparentDiv);
		// Creating content div
		this.divs_content = document.createElement('DIV');
		this.divs_content.className = 'modalDialog_contentDiv';
		this.divs_content.id = 'DHTMLSuite_modalBox_contentDiv';
		this.divs_content.style.zIndex = 100000;
		
		if(this.MSIE){
			this.iframe = document.createElement('<IFRAME src="about:blank" frameborder=0>');
			this.iframe.style.zIndex = 90000;
			this.iframe.style.position = 'absolute';
			document.body.appendChild(this.iframe);	
		}
			
		document.body.appendChild(this.divs_content);
		// Creating shadow div
		this.divs_shadow = document.createElement('DIV');
		this.divs_shadow.className = 'modalDialog_contentDiv_shadow';
		this.divs_shadow.style.zIndex = 95000;
		document.body.appendChild(this.divs_shadow);
		window.refToModMessage = this;
		this.addEvent(window,'scroll',function(e){ window.refToModMessage.__repositionTransparentDiv() });
		this.addEvent(window,'resize',function(e){ window.refToModMessage.__repositionTransparentDiv() });
	}
	,
	__getBrowserSize : function()
	{
    	var bodyWidth = document.documentElement.clientWidth;
    	var bodyHeight = document.documentElement.clientHeight;
    	
		var bodyWidth, bodyHeight; 
		if (self.innerHeight){ // all except Explorer 
		 
		   bodyWidth = self.innerWidth; 
		   bodyHeight = self.innerHeight; 
		}  else if (document.documentElement && document.documentElement.clientHeight) {
		   // Explorer 6 Strict Mode 		 
		   bodyWidth = document.documentElement.clientWidth; 
		   bodyHeight = document.documentElement.clientHeight; 
		} else if (document.body) {// other Explorers 		 
		   bodyWidth = document.body.clientWidth; 
		   bodyHeight = document.body.clientHeight; 
		} 
		return [bodyWidth,bodyHeight];		
		
	}
	,
    __resizeDivs : function()
    {
    	
    	var topOffset = Math.max(document.body.scrollTop,document.documentElement.scrollTop);

		if(this.cssClassOfMessageBox)
			this.divs_content.className=this.cssClassOfMessageBox;
		else
			this.divs_content.className='modalDialog_contentDiv';	
			    	
    	if(!this.divs_transparentDiv)return;
    	
    	// Preserve scroll position
    	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
    	var sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
    	
    	window.scrollTo(sl,st);
    	setTimeout('window.scrollTo(' + sl + ',' + st + ');',10);

    	this.__repositionTransparentDiv();
    	

		var brSize = this.__getBrowserSize();
		var bodyWidth = brSize[0];
		var bodyHeight = brSize[1];
    	
      	this.divs_content.style.width = this.width + 'px';
    	this.divs_content.style.height= this.height + 'px';  	
    	
    	var tmpWidth = this.divs_content.offsetWidth;	
    	var tmpHeight = this.divs_content.offsetHeight;
    	
       	this.divs_content.style.left = Math.ceil((bodyWidth - tmpWidth) / 2) + 'px';;
    	this.divs_content.style.top = (Math.ceil((bodyHeight - tmpHeight) / 2) +  topOffset) + 'px';
    	
 		if(this.MSIE){
 			this.iframe.style.left = this.divs_content.style.left;
 			this.iframe.style.top = this.divs_content.style.top;
 			this.iframe.style.width = this.divs_content.style.width;
 			this.iframe.style.height = this.divs_content.style.height;
 		}
 		
    	this.divs_shadow.style.left = (this.divs_content.style.left.replace('px','')/1 + this.shadowOffset) + 'px';
    	this.divs_shadow.style.top = (this.divs_content.style.top.replace('px','')/1 + this.shadowOffset) + 'px';
    	this.divs_shadow.style.height = tmpHeight + 'px';
    	this.divs_shadow.style.width = tmpWidth + 'px';
    	
    	
    	
    	if(!this.shadowDivVisible)this.divs_shadow.style.display='none';	// Hiding shadow if it has been disabled
    	
    	
    }
    ,
   
    __repositionTransparentDiv : function()
    {
    	this.divs_transparentDiv.style.top = Math.max(document.body.scrollTop,document.documentElement.scrollTop) + 'px';
    	this.divs_transparentDiv.style.left = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft) + 'px';
		var brSize = this.__getBrowserSize();
		var bodyWidth = brSize[0];
		var bodyHeight = brSize[1];
    	this.divs_transparentDiv.style.width = bodyWidth + 'px';
    	this.divs_transparentDiv.style.height = bodyHeight + 'px';		
		   	
    }
	,

    __insertContent : function()
    {
		if(this.url){	// url specified - load content dynamically
			//ajax_loadContent('DHTMLSuite_modalBox_contentDiv',this.url);
		}else{	// no url set, put static content inside the message box
			this.divs_content.innerHTML = this.htmlOfModalMessage;	
		}
    }		
}
