function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

var whitespace = " \t\n\r";

function isEmpty(s)
{
   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function checkLogin(form)
{
							
	if ((form.msg.value == "") || (isWhitespace(form.msg.value))) {
		form.msg.focus();
		return false;
	}	
	if(form.msg.value.length > 500){
		alert("El mensaje es demasiado extenso. Por favor, trata de reducirlo.");
		form.msg.focus();
		return false;		
	}
	
	doLogin();
	return false;
}

function recuerdaLogin()
{
							
	alert("Recuerda que para participar en el foro debes estar registrado e iniciar la sesión.");
	return false;		

}

function abreVentana(theURL,winName,features) { //v2.0
	var winl = (screen.width - 450) / 2;
	var wint = (screen.height - 350) / 2;
	win = window.open(theURL,winName,features+',left='+winl+',top='+wint);
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }  
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function visibilidadCapa(d, op){
	//d - capa
	//op - 1 mostrar, 0 ocultar
	if(op==1){
		if (document.all){  
			document.all[d].style.display="block"; 
			document.all[d].style.visibility="visible";    			    
		}     
		if (document.layers){  
			document.layers[d].display="block";   
		}       
		document.getElementById(d).style.display="block";
		document.getElementById(d).style.visibility="visible";
	} else {
		if (document.all){  
			document.all[d].style.display="none";  			    
		}     
		if (document.layers){  
			document.layers[d].display="none";   
		}       
		document.getElementById(d).style.display="none" 	
	}
}

function agradecer(pid){
	document.getElementById("thxTron"+pid).innerHTML="<img src='imgschat/ajax-loader.gif'>Espere...";	
		
	var xhr = createXHR();
	xhr.onreadystatechange=function(){ 
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				document.getElementById("thxTron"+pid).innerHTML="<img src='iconos/graciasOk.gif'>";					
			}				
            else {
                document.getElementById("thxTron"+pid).innerHTML = "<img src='imgschat/hand.gif'> ¡Error!";
            }			
		} 
	}; 
	 
	xhr.open("GET", "gracias.asp?idp="+pid+"&aleatorio="+Math.random(), true);
	xhr.setRequestHeader("Accept-Charset","ISO-8859-1");
	xhr.send(null); 	
}

function doLogin(){
	document.getElementById("accion").innerHTML="<img src='imgschat/ajax-loader.gif'>";	
	var Formulario = document.getElementById('loginForm');
	var longitudFormulario = Formulario.elements.length;
	var cadenaFormulario = "";
	var sepCampos = "";
	var valor = "";
	for (var i=0; i < longitudFormulario;i++)
	{
		valor = Formulario.elements[i].value;
		valor = escape(valor);
		cadenaFormulario += sepCampos+Formulario.elements[i].name+'='+valor;
		sepCampos="&";
	}	
	var aleatorio=Math.random();
	cadenaFormulario+="&aleatorio="+aleatorio;	

var xhr = createXHR();
	xhr.onreadystatechange=function(){ 
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				document.getElementById("loginForm").msg.value="";	
				doRefresh();		
			}				
            else if(xhr.status==404){
				document.getElementById("accion").innerHTML="";	
                document.getElementById("mensajes").innerHTML = "<p class='texto'><img src='imgschat/hand.gif'> No hay dialogos abiertos...</p>";
            }
            else{
				document.getElementById("accion").innerHTML="";	
                document.getElementById("mensajes").innerHTML = "<img src='imgschat/hand.gif'> Error: "+xhr.status;
            }			
		} 
	}; 
	 
	xhr.open("POST", "doLogin.asp", true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.setRequestHeader("Accept-Charset","ISO-8859-1");
	xhr.send(cadenaFormulario); 	
}

function doRefresh(){
	var xhr = createXHR();
	xhr.onreadystatechange=function(){ 
		if(xhr.readyState == 4){
			if(xhr.status == 200){
				 var xml  = xhr.responseXML.documentElement; 
				 var pantalla = "";
				 //Mostramos el mínimo aceptable por el área de visión: 4
				 Total=xml.getElementsByTagName('mensaje').length-1
				 for (i = Total; i >=Total-2 ; i--){ 
				 	 var mensaje = "";
					 var item = xml.getElementsByTagName('mensaje')[i];			 
					   mensaje+="<DIV id='mensajeBox' style='float:left'>";
					   mensaje+="<div id='imgnick' style='float:left'><img src='imgnicks/"+item.getElementsByTagName('imgnick')[0].firstChild.nodeValue+"'></div>";
					   mensaje+="<DIV style='float:left'><span class='fechaHora'><b>"+item.getElementsByTagName('hora')[0].firstChild.nodeValue+" | </b>"+item.getElementsByTagName('nick')[0].firstChild.nodeValue+ " dice:</span>";
					   mensaje+="<span class='mensaje'>"+item.getElementsByTagName('cuerpo')[0].firstChild.nodeValue+"</span>";
					   mensaje+="</DIV></DIV>";
						
					   pantalla+=mensaje;				   
				 } 
				document.getElementById("mensajes").innerHTML="<p class='texto'>"+pantalla+"</p>";	
				document.getElementById("accion").innerHTML=""
			}
            else if(xhr.status==404){
                document.getElementById("mensajes").innerHTML = "<p class='texto'><img src='imgschat/hand.gif'> No hay dialogos abiertos...</p>";
            }
            else{
                document.getElementById("mensajes").innerHTML = "<img src='imgschat/hand.gif'> Error: "+xhr.status;
            }			
		} 
	}; 
	
	var currentTime = new Date();
	
	var month = currentTime.getMonth() + 1;
	if (month < 10) month = "0" + month;
	
	var day = currentTime.getDate();
	if (day < 10) day = "0" + day;
	
	var year = currentTime.getFullYear();
	
	logFile=day.toString()+month.toString()+year.toString()+"_log.xml";
	xhr.open("GET", logFile+"?aleatorio="+Math.random(), false);
	xhr.send(null); 
	setTimeout(doRefresh,5000);		
}

function setCookie(c_name,value,expireHours)
{
  var exhour=new Date(); //create the current date object
  exhour.setHours(exhour.getHours()+1);
  document.cookie=c_name+ "=" +escape(value)+
  ((expireHours==null) ? "" : ";expires="+exhour.toGMTString());
}

function getCookie(c_name)
{
  if (document.cookie.length>0)
  {
	c_start=document.cookie.indexOf(c_name + "=");
	if (c_start!=-1)
	{ 
	  c_start=c_start + c_name.length+1; 
	  c_end=document.cookie.indexOf(";",c_start);
	  if (c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
	} 
  }
  return "";
}
	
function initSB(){
	doRefresh();
}
