addEvent(window,'load',inicializarEventos,false);

function inicializarEventos() {
  var ob;
  ob = document.getElementById('newrandom');
  addEvent(ob, 'click', presionEnlace, false);
}

function presionEnlace(e) {
  if (window.event) {
    window.event.returnValue = false;
    var url = window.event.srcElement.getAttribute('href');
    loadRandomImage(url);
  } else if (e) {
      e.preventDefault();
      var url = e.target.getAttribute('href');
      loadRandomImage(url);     
    }
}


var conexion1;
function loadRandomImage(url) {
  if (url == '')
    return;
  conexion1 = crearXMLHttpRequest();
  conexion1.onreadystatechange = procesarEventos;
  conexion1.open("GET", url, true);
  conexion1.send(null);
}

function procesarEventos() {
  var detalles = document.getElementById("randomimage");
  if(conexion1.readyState == 4)
    detalles.innerHTML = conexion1.responseText;
  else 
    detalles.innerHTML = 'Cargando...';
}

//***************************************
//Funciones comunes a todos los problemas
//***************************************
function addEvent(elemento, nomevento, funcion, captura) {
  if (elemento.attachEvent) {
    elemento.attachEvent('on' + nomevento, funcion);
    return true;
  } else if (elemento.addEventListener) {
    elemento.addEventListener(nomevento, funcion, captura);
    return true;
  }
  return false;
}

function crearXMLHttpRequest() {
  var xmlHttp = null;
  if (window.ActiveXObject) 
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  else if (window.XMLHttpRequest) 
    xmlHttp = new XMLHttpRequest();
  return xmlHttp;
}



function ajaxRequest()
{
    
    var xmlHttp;
    
    try
    {
        xmlHttp = new XMLHttpRequest();
    }
    catch(e)
    {
        var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0',
                                        'MSXML2.XMLHTTP.5.0',
                                        'MSXML2.XMLHTTP.4.0',
                                        'MSXML2.XMLHTTP.3.0',
                                        'MSXML2.XMLHTTP',
                                        'Microsoft.XMLHTTP');
        
        for (i = 0; i < XmlHttpVersions.length && !xmlHttp; i++)
        {
            try
            {
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            }
            catch(e) {}
        }
    }
    
    if (!xmlHttp)
    {
        alert("Your browser may not support XML Requests");
    }
    else
    {
        return xmlHttp;
    }
}

// Get the random image
function getRandomImage()
{
    // The id of the div you want your images to be displayed in
    var imageDisplayDiv = "randomimage";
    // The name of the file the images will come from
    var imageFile = "http://www.pikabit.net/randomimg.php";
	
    // Starts a new request
    var ajax = ajaxRequest();
    
    // Gets the out put of the image file by sending nothing and just getting its finished product
    ajax.open("GET", imageFile, true);
    ajax.send(null);    
    
    // displayes a loading text so user knows the script is working
    document.getElementById(imageDisplayDiv).innerHTML = "<span style=\"font-weight:bold;\">Cargando...</span>";    
    
    // Cycles through the ajax state change
    ajax.onreadystatechange = function()
    {
        // if the ajax request is ready
        if (ajax.readyState == 4)
        {
            // Checks if it really is ready by checking status number , 200 = done
//            if (ajax.status == 200)
//            {
            // writes the image files output to the div
            document.getElementById(imageDisplayDiv).innerHTML = ajax.responseText;
//            }
        }
    }
}

// Runs the script
function runRandomImage() {
	getRandomImage(); // Runs image function once
	setTimeout("runRandomImage()",60000);
}

function loadRoutine() {
	runRandomImage();
}
