// Returns a request from a server to a callback function with an overload
// Callback(xmlFeed, status, url)
// xmlFeed = the xml stream
// status = number 
// 0 = success
// 1 = no request created
// 2 = request.status != 200
// url = the url for the ajax request
// use responseXML or responseText to get content
function dataRequest(url, callbackFunction) 
{ 
	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);
			
		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);
			}
			else
			{
				if(callbackFunction)
					callbackFunction(null, 2, url);
			}
		} 
	}
   
	// Execute the request
    // alert(url);
	request.open('GET', url + "&random=" + returnCacheHackString(16, true, true), true); 
	request.send(""); 
}

function insureSeverUpdate(xmlFeed, status, url)
{
	if(status != 0)
	{
		if(confirm("Update failed.\r\n\r\nDo you want to try again?"))
		{
			dataRequest(url, insureSeverUpdate);
		}
	}
	else
	{
	    var xmlDoc = xmlFeed.responseXML;
	    
        if(xmlDoc.documentElement.nodeName == "trunk")
        {
            if(xmlDoc.documentElement.attributes[0].name == "relogon")
            {
                if(xmlDoc.documentElement.attributes[0].value == "true")
                    location.href = "LogOn.aspx";
            }
        }
	}
}

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;
}