var aleatorio = 0;
var http = getHTTPObject();
var nAleatorio = creaAleatorio();

function creaAleatorio()
{
	aleatorio = Math.random() * 1000;
	return aleatorio; 
}

function getHTTPObject() 
{
  var xmlhttp;
  try 
   {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) 
   {
     try 
      {
	 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
	 xmlhttp = false;
      }
   }
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
   {
       try 
	 {
	    xmlhttp = new XMLHttpRequest();
	 } catch (e)
	 {
	    xmlhttp = false;
	 }
  }
  return xmlhttp;
}

function callPeticionesR() {
        http.open("GET","/modules/mod_ostwitterbox/mod_ostwitterbox_ajax.php?param=" + nAleatorio, true);
        nAleatorio = creaAleatorio();  
        http.onreadystatechange = tratarResultados;
        http.send(null);
}

function tratarResultados(){
	var cadena='';
	 if (http.readyState != 4) {
	 		
	 }else if (http.readyState == 4) {
		if (http.status == 200) {
			document.getElementById('id_mensajes_tweets').innerHTML='';
	    	var respuesta = http.responseXML;
			var raiz = respuesta.documentElement;
			var mensajes = raiz.getElementsByTagName('status');
			for(var i=0; i<mensajes.length; i++)
			{
				cadena+='<div class="tweet">';
				var mensaje = mensajes[i];
				//Sat Apr 03 17:37:33 +0000 2010
				//Sábado, 03 Abril 2010
				var fecha = mensaje.getElementsByTagName("created_at")[0].firstChild.nodeValue;
				var partes_fecha = fecha.split(' ');
				var fecha = partes_fecha[0]+', '+partes_fecha[2]+' '+partes_fecha[1]+' '+partes_fecha[5]
				var texto = mensaje.getElementsByTagName("text")[0].firstChild.nodeValue;
				var usuario = mensaje.getElementsByTagName('user')[0];
				var nombre_usuario = usuario.getElementsByTagName('screen_name')[0].firstChild.nodeValue;
				cadena+='<b>'+texto+'</b> - <i>'+fecha+'</i> ';
				cadena+='</div>';
			}
			//cadena+='<div class="author" style="margin-top: 10px;"><a href="http://www.twitter.com/fmoviedohc" style="color: rgb(0, 0, 0);"><img src="/./images/twitter.png" title="Síguenos en Twitter" alt="Síguenos en Twitter" style="vertical-align: middle;" width="30">Síguenos en Twitter</a></div>';
			document.getElementById('id_mensajes_tweets').innerHTML=cadena;
		}else if (http.status == 404) {
		 		alert("Dirección URL no encontrada");
                     }else if (http.status == 403){
                                    alert("Acceso denegado");
                        }
    }
}
