

function LocationService()
{
    if (!AjaxObj)
    {
        throw "The Ajax Library is required";
    }    
    this.Ajax = new AjaxObj();
    this.Initialized = false;
}

LocationService.prototype.__postalQuery  = "get_postal_codes";
LocationService.prototype.__regionQuery  = "get_regions";
LocationService.prototype.__cityQuery    = "get_cities";
LocationService.prototype.__countryQuery = "get_countries";

LocationService.prototype.QueryPostal = function(e, textObj)
{
    if (!this.Initialized)
        throw "The LocationService object is not initialized.";


    this.CountryObject = document.getElementById(this.CountryObjectId);
    this.RegionObject = document.getElementById(this.RegionObjectId);
    this.CityObject = document.getElementById(this.CityObjectId);
    this.PostalObject = document.getElementById(this.PostalObjectId);
    this.AnimationObject = document.getElementById(this.AnimationObjectId);
    this.ChangeCityObject = document.getElementById(this.ChangeCityObjectId);
    
    var _this = this;
    
    var reqComplete = function(req)
    {
        if (_this.AnimationObject!=null)
        {
            st = _this.AnimationObject.style ? _this.AnimationObject.style : _this.AnimationObject;
            st.display = "none";
        }
        _this.cityIndex = 0;
        var result = eval("("+req.responseText+")");
        if (result.length<=0)
            return;
        changeSt = _this.ChangeCityObject.style ? _this.ChangeCityObject.style : _this.ChangeCityObject;
        if (result.length>1) { changeSt.display = "inline"; } else { changeSt.display="none";  };
        _this.SetLastResult(result);
        _this.RegionObject.value = result[0].StateAbbr;
        _this.CityObject.value = result[0].CityName;
//        _this.RegionObject.disabled = 'disabled';
//        _this.CityObject.disabled = 'disabled';
    }
    
    if (textObj.value.length>=5)
    {
        if (this.AnimationObject!=null)
        {
            st = this.AnimationObject.style ? this.AnimationObject.style : this.AnimationObject;
            st.display = "block";
        }
        var url = this.LocalServiceUrl+"?q="+this.__regionQuery+"&value="+textObj.value;
        this.Ajax.LoadUrl(url, reqComplete);
    }
}

LocationService.prototype.SetLastResult = function(LastResult)
{
    this.LastResult = LastResult;
}

LocationService.prototype.NextCity = function()
{
    if (this.cityIndex==null)
        this.cityIndex = -1;
    if (this.LastResult==null)
        return;
    if (this.LastResult.length==1)
    {
        this.CityObject.disabled = '';
        return;
    }
    this.cityIndex++;
    //this.CityObject.disabled = 'disabled';
    if (this.cityIndex>=this.LastResult.length)
    {
        this.cityIndex=-1;
        this.CityObject.disabled = '';
        return;
    }
    this.CityObject.value = this.LastResult[this.cityIndex].CityName;
    
}

LocationService.prototype.Initialize = function(LocalServiceUrl, 
                            CountryObject, 
                            RegionObject, 
                            CityObject, 
                            PostalObject,
                            ChangeCityObject,
                            AnimationObject)
{
    this.LocalServiceUrl = LocalServiceUrl;
    this.CountryObjectId = CountryObject;
    this.RegionObjectId = RegionObject;
    this.CityObjectId = CityObject;
    this.ChangeCityObjectId = ChangeCityObject;
    this.PostalObjectId = PostalObject;
    this.AnimationObjectId = AnimationObject;
    this.Initialized = true;
}