﻿// JScript File

function SetCityText( ddl, txt ) 
{
    $('#' + txt).val(ddl.value);
}
function ClearMovingInputs( txtc, ddlc, zipc )
{
    $('#' + txtc).show();
    $('#' + txtc).val('');
    $('#' + txtc).unautocomplete();
    $('#' + ddlc).hide();
    $('#' + zipc).show();
    $('#' + zipc).val('');
}
        
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: "/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) {
                  $('#' + zip).val(item.z);
                });

            }
            , error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert('error');
            }
    });
    }
    
    $('#' + prev).val(len);
        
}