﻿head(function() {
    function updateAgentAndCounties(el) {
        var zip = $(el).val();
        var url = $("#ajax-helper").attr("data-localagents");
        var silhoutte = $("#ajax-helper").attr("data-silhoutte");

        if (zip != null && zip.length == 5) {
            $.ajax({
                url: url,
                type: "GET",
                cache: false,
                data: { zipcode: zip },
                beforeSend: function () {
                    $('#agent-image').hide();
                },
                success: function (result) {
                    $("#agent-name").html(result.Name);
                    $("#agent-location").html(result.City + ", " + result.State);
                    $("#agent-image").attr("src", result.PictureSrc).show();
                    $("#County > option").remove();

                    $.each(result.Counties, function (val, county) {
                        $('#County').append($('<option></option>').val(county).html(county));
                    });
                    $("#County").attr("title", "Please Select Your County");
                    $("#AgentId").val(result.AgentId);
                    Cufon.replace('#agent-name, #agent-location', { fontFamily: 'Classic' });
                }
            });
        }
        else {
            $("#agent-name").html("Enter your zip to find your agent");
            $("#agent-location").html(" ");

            $("#agent-image").attr("src", silhoutte).show();
            $("#County > option").remove();
            $('#County').append($('<option></option>').val(' ').html('Enter your zip code first'));
            $("#AgentId").val(0);
            Cufon.replace('#agent-name, #agent-location', { fontFamily: 'Classic' });
        }
    }

    function updateNeutralCounties(el) {
        var zip = $(el).val();
        var url = $("#ajax-helper").attr("data-localagents");

        if (zip != null && zip.length == 5) {
            $.ajax({
                url: url,
                type: "GET",
                cache: false,
                data: { zipcode: zip },
                success: function (result) {
                    $("#Contact_County > option").remove();
                    $.each(result.Counties, function (val, county) {
                        $('#Contact_County').append($('<option></option>').val(county).html(county));
                    });
                    $("#Contact_County").attr("title", "Please Select Your County");
                }
            });
        }
        else {
            $("#Contact_County > option").remove();
            $('#Contact_County').append($('<option></option>').val(' ').html('Enter your zip code first'));
        }
    }

    //    $(document).ready(function() {

        // Local Agent Search
        updateAgentAndCounties($(".zipcode-ajax"));

        var previousValue = '';

        $(".zipcode-ajax").keyup(function() {
            $(this).change();
        }).change(function() {
            var currentValue = $(this).val();
            if (previousValue != currentValue) {
                updateAgentAndCounties(this);
                previousValue = currentValue;
            }
        });

        var neutralValue = '';

        $("#Contact_Zip").keyup(function() {
            $(this).change();
        }).change(function() {
            var currentValue = $(this).val();
            if (neutralValue != currentValue) {
                updateNeutralCounties(this);
                neutralValue = currentValue;
            }
        });
//    });//end doc ready
});
