﻿var appPath = '/'

function GetCityAutoComplete( state, city, zip, prev ) 
{
    var len = $('#' + city).val().length;
    var cur = $('#' + prev).val()
    if( len >= 2 && len > cur ) 
    {
        $.ajax({ type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: appPath + "WebServices/AJAXHelper.asmx/GetCityAutoComplete"
            , data: "{ 'city' : '" + $('#' + city).val() + "', 'stateID' : '" + $('#' + state).val() + "' }"
            , dataType: "text"
            , dataFilter: function(data) { var msg; if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') { msg = JSON.parse(data); } else { msg = eval('(' + data + ')'); } if (msg.hasOwnProperty('d')) { return msg.d; } else { return msg; } }
            , success: function(msg) {
                var r = eval((msg));
                $('#' + city).autocomplete(r, {
                    formatItem: function(item) {
                        return item.c;
                    }
                  , max: 50
                }).result(function(event, item) {
                    if (zip == null) {
                        parent.SetZipFromZipFinder(item.z);
                    } else {
                        $('#' + zip).val(item.z);
                    }
                });
            }
            , error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('error');
            }
        });
    }
    
    $('#' + prev).val(len);        
}
