/*
    Copyright (c) 2006, SpatialPoint, LLC.
    
    All rights reserved.
    
    http://www.spatialpoint.com
*/

function SpatialPoint_GeocodeClient(init)
{
    // Server references.
    this.host = null;
    this.handler = "AtlasMapRequestHandler.ashx";
	
    this.validationKey = null;
    
    // UI controls.
    this.control = null;
    
    // Events and event support.    
    this.onGeocodeResponse = null;
    
    if (init)
    { 
        this.name = init.name;
        
        if (!init.name)
        {
            throw("Missing required property: name. The global name of this object instance.");
        }

        if (init.host)
        {
            this.host = init.host;
        }
        
        if (init.handler)
        {
            this.handler = init.handler;
        }

        if (init.validationKey)
        {
            this.validationKey = init.validationKey;
        }

        if (init.onGeocodeResponse)
        {
            this.onGeocodeResponse = init.onGeocodeResponse;
        }

        onprogress = null;
        if (init.onProgress)
        {
            onprogress = init.onProgress;
        }
        
	    this.ajaxClient = new AjaxClient(function(request) { eval(request.responseText); }, onprogress);
    }
    else
    {
        alert("Can't do much without initialization parameters");
    }
}

SpatialPoint_GeocodeClient.prototype.getParameters = function(params)
{
    var parameters = params || new Array();
    
    parameters.push("T=" + this.name);
    parameters.push("VK=" + this.validationKey);
    
    var queryString = "";
    
    for (var i = 0; i < parameters.length; i++)
    {
        queryString += parameters[i] + "&";
    }
        
    return queryString;
}

SpatialPoint_GeocodeClient.prototype.getUrl = function(params)
{
    return "?" + this.getParameters(params);
}

SpatialPoint_GeocodeClient.prototype.getRequest = function(params)
{
    // GET version.
    return this.host + this.handler + this.getUrl(params);

    // POST version.
    //return [this.host + this.handler, this.getParameters(params)];
}

SpatialPoint_GeocodeClient.prototype.geocode = function(location)
{
    var param = "Geocode=" + encodeURIComponent(location.street + ";" + location.city + ";" + location.state + ";" + location.zip + ";" + location.country);
    
    // GET version.
    this.ajaxClient.addRequest(this.getRequest([param]));       

    // POST version.
    //this.ajaxClient.addRequest(this.host + this.handler, this.getParameters([param]));       
}

SpatialPoint_GeocodeClient.prototype.setServerResponse = function(responseText)
{
    if (this.onServerResponse)
    {
        this.onServerResponse(responseText);
    }
    else
    {
        alert(responseText);
    }
}

SpatialPoint_GeocodeClient.prototype.setGeocodeResponse = function(response)
{
    if (this.onGeocodeResponse)
    {
        this.onGeocodeResponse(response);
    }
}
