﻿// Returns a request from a server to a callback function with an overload
// Callback(xmlFeed, status, url)
// xmlFeed = the xml stream
// status = number
// url = the url for the ajax request
// miscData = data you want to transfer
//
// status 0 = success
// status 1 = no request created
// status 2 = request.status != 200
// use responseXML or responseText to get content
function dataRequest(url, callbackFunction, miscData)
{
    var request = false;

    // Create the request for the different browsers
    if (window.XMLHttpRequest)
    {
        request = new XMLHttpRequest();

        if (request.overrideMimeType)
            request.overrideMimeType('text/xml');
    }
    else if (window.ActiveXObject)
    {
        try
        {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            request = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    // Escape if no request has been created
    if (!request)
    {
        if (callbackFunction)
            callbackFunction(null, 1, url, miscData);

        return false;
    }

    // Add listner
    request.onreadystatechange = function()
    {
        // Test if the request has finshed
        if (request.readyState == 4)
        {
            // Test if it has been a succes
            if (request.status == 200)
            {
                if (callbackFunction)
                    callbackFunction(request, 0, url, miscData);
            }
            else
            {
                if (callbackFunction)
                    callbackFunction(null, 2, url, miscData);
            }
        }
    }

    // Execute the request
    request.open('GET', url + "&random=" + returnCacheHackString(16, true, true), true);
    request.send("");
}

function returnCacheHackString(length, doNumb, doAlpha)
{
    var numb = "1234567890";
    var alpha = "qwertyuioplkjhgfdsazxcvbnm";

    var string = "";
    var bla = "";
    var type, rand;

    while (string.length < length)
    {
        type = Math.floor(Math.random() * 2);

        if (type == 0 && doNumb)
        {
            rand = Math.floor(Math.random() * numb.length);
            string += numb.substring(rand, rand + 1);
        }

        if (type == 1 && doAlpha)
        {
            rand = Math.floor(Math.random() * alpha.length);
            string += alpha.substring(rand, rand + 1);
        }
    }

    rand = Math.floor(Math.random() * numb.length);
    return string;
}
