/*
    Copyright (c) 2006, SpatialPoint, LLC.
    
    All rights reserved.
    
    http://www.spatialpoint.com
*/

function AjaxClient(onreceiveresponse, onprogress)
{
    this.requestQueue = new Array();
    this.requestActive = false;
    this.xmlHttp = this.getXmlHttp();
    this.onreceiveresponse = onreceiveresponse;
    this.onprogress = onprogress;
}

AjaxClient.prototype.getXmlHttp = function()
{
    try 
    {
        this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (ex) 
    {
        try 
        {
            this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (ex) 
        {
            this.xmlHttp = null;
        }
    }
    
    if (!this.xmlHttp && typeof XMLHttpRequest != "undefined") 
    {
        this.xmlHttp = new XMLHttpRequest();
    }
    
    return this.xmlHttp;
}

AjaxClient.prototype.addRequest = function(url, data) 
{
    // Put the request URL and data on the queue.
    this.requestQueue.unshift([url, data]);

    this.sendNextRequest();
}

AjaxClient.prototype.sendNextRequest = function()
{
    if (this.requestActive) 
    {
        return;
    }

    var queue = this.requestQueue;
    
    if (!queue || queue.length == 0) 
    {
        return;
    }
    
    var apiCall = queue.pop();
    
    if (apiCall)
    {
        var request = AjaxClient.prototype.getXmlHttp();
        
        if (request) 
        {
            try
            {
                //var method = "GET"; //(apiCall.length == 2) ? "POST" : "GET";
                var method = apiCall[1] ? "POST" : "GET";
                
                request.open(method, apiCall[0], true);

                if (method == "POST")
                {
                    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
                    request.setRequestHeader("Content-Length", apiCall[1].length);
                }
                
                // Allow the onreadystatechange closure to have access to this instance.                
                var self = this;
                
                request.onreadystatechange = function()
				{
                    if (self.onprogress)
                    {
                        self.onprogress(request.readyState);
                    }
                    
				    if (request.readyState == 4) 
				    {
				        self.requestActive = false;
				
				        if (self.onreceiveresponse)
				        {
				            self.onreceiveresponse(request);
				        }
				        else
				        {
				            var code = request.responseText;
				    
				            try 
				            {
				                alert(code);
				            }
				            catch (ex) 
				            {
				                if (request.status != 200) 
				                {
				                    alert("Could not process your search request due to a communication error. Errors of this type can be caused by an incorrectly configured control or by network connection issues. Contact the site owner if this problem persists.");
				                }
				            }
				        }
				            
				        self.sendNextRequest();
				    }
				}


                this.requestActive = true;
                
                var data = (apiCall.length == 2) ? apiCall[1] : "";

                request.send(data);
            }
            catch (ex)
            {
                alert(ex.message); 
                
                throw ex;
            }
        }
    }
    else
    {
        alert("invalid API call");
    }
}

AjaxClient.prototype.getTimeStamp = function()
{
    var d = new Date();

    var delta = d.getUTCHours() + '.'
        + d.getUTCMinutes() + '.' 
        + d.getUTCSeconds() + '.' 
        + d.getUTCMilliseconds();

    return delta;       
}

function getXmlDoc(xmlText)
{
	var xmlDoc;
	
	if (window.DOMParser)
	{
		var parser = new DOMParser();
		xmlDoc = parser.parseFromString(xmlText, "text/xml");			
	}	
	else
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async = "false";
		xmlDoc.loadXML(xmlText);
	}
	
	return xmlDoc;
}
