//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, callback, post )
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        req.onreadystatechange = callback;
        if( ! post.length )
        {
            req.open("GET", url, true);
            req.send(null);
        }
        else
        {
            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 = callback;
            if( ! post.length )
            {
                req.open("GET", url, true);
                req.send();
            }
            else
            {
                req.open("POST", url, true);
                req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
                req.send( post );
            }
        }
    }
}

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
              eval(method + '(\'\', result, success)');
           }
           else
           {
              alert("Server returned no AJAX response");
           }

        } 
        else if (req.status == 0)
        {
           // This state is reached when there is stacked up AJAX requests mixed
           // with a revote submit - Ignoring for now.
        }
        else 
        {
           alert("Sorry. There was a problem retrieving the XML data (code: " + req.status + "):\n" + req.statusText);
        }
    } 
    else 
    {
	   // if readystate is 0,1,2, or 3, (not complete) print the status to the reporting HTML element
	   //document.getElementById(reportElement).className = 'loading';
    
	   //if 
	   //printText(reportElement,loadingMessage);
	}	
}

function showAjaxError(input, results, success)
{
   alert("Ajax error:\n\n" + results[0].getElementsByTagName('errortext')[0].firstChild.data);
}