
// Ajax Request By Razor

// Basically runs through all the types of xml request untill it finds one that the browser supports
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 name of the file the images will come from

    var imageFile = "random.php";
    var secondimageFile = "random.php";


    // Starts a new request
    var ajax = ajaxRequest();
    var ajax2 = 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);    
    

    
    // 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("div1").innerHTML = ajax.responseText;
            }
        }
    }


    ajax2.open("GET", secondimageFile, true);
    ajax2.send(null);  


    // Cycles through the ajax state change
    ajax2.onreadystatechange = function()
    {
        // if the ajax request is ready
        if (ajax2.readyState == 4)
        {
            // Checks if it really is ready by checking status number , 200 = done
            if (ajax2.status == 200)
            {
            // writes the image files output to the div
            document.getElementById("hidden_div").innerHTML = ajax2.responseText;
            }
        }
    }




}

// Get the random image
function getnextRandomImage()
{
    // The id of the div you want your images to be displayed in
    var imageDisplayDiv = "hidden_div";

    document.getElementById("div1").innerHTML = document.getElementById("hidden_div").innerHTML;

    // The name of the file the images will come from
    var imageFile = "random.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);  

  

    
    // 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("hidden_div").innerHTML = ajax.responseText;
            }
        }
    }





}

// Runs the script
function runRandomImage()
{
getRandomImage(); // Runs image function once
//setTimeout("runRandomImage()",5000);
setTimeout("subsequentRandomImages()",10000);
}


function subsequentRandomImages()
{
getnextRandomImage();
setTimeout("subsequentRandomImages()",5000);
}
