//$Id: ajax.js 4848 2011-08-30 21:53:57Z ssundheim $

//HTTP REQUEST STUFF


function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) 
    {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } 
    else if (window.ActiveXObject) 
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) 
        {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function loadXMLDocPost(url, post)
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
        req.send( post );
    // branch for IE/Windows ActiveX version
    } 
	else if (window.ActiveXObject) 
	{
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) 
		{
            req.onreadystatechange = processReqChange;;
            req.open("POST", url, true);
            req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
            req.send( post );
        }
    }
	else
	{
		alert('It does not appear that you have a browser that is currently capable of processing AJAX');
	}
}

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) 
    {
		// only if "OK"
        if (req.status == 200) 
        {
			// ...processing statements go here...
			if (req.responseXML != null)
			{
				var response  = req.responseXML.documentElement;
				var method  = response.getElementsByTagName('method')[0].firstChild.data;
				var result  = response.getElementsByTagName('results');
				var success = response.getElementsByTagName('success')[0].firstChild.data;
				
				//This performs the JS function ("method") indicated by the returned XML
				if (method != 'null')
				{
					eval(method + '(\'\', result, success)');
				}
           }
           else
           {
              alert('The server did not return a response. Please try again or contact support by clicking on the Report a Bug link below');
           }

        } 
        else if (req.status != 0)
        {
           /* Bandaid solution to resolve issue where the ajax requests cause a 502 Bad Gateway Error 20% of the time
           document.getElementById("page").style.border = "1px solid #990000";
           location.reload(true);
		   */
           alert("Sorry. There was a problem retrieving the XML data. We are currently working on resolving this issue. In the meantime, please refresh this page. (Code: " + req.status + "):\n" + req.statusText);

        }
    } 
}

function loadAynomXMLDoc(url) 
{   
	// calling aynomously, not wanting a response
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) 
    {
        req = new XMLHttpRequest();
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } 
    else if (window.ActiveXObject) 
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) 
        {
            req.open("GET", url, true);
            req.send();
        }
    }
}

function handleAJAXRedirect(input, results, success)
{
	var redirectURI = results[0].getElementsByTagName('redirecturi')[0].firstChild.data;
	window.location = redirectURI;
}

