﻿(function($)
{
    $.fn.LoadCountries = function(jsonCountryList)
    {
        var countrySelect = $(this);
        $.each(jsonCountryList, function(level1Object, level2Object)
        {
            var country = level2Object.Country;
            countrySelect.append($('<option></option>').val(country).html(country));
        });
    }

    $.fn.LoadStates = function(stateDropDownElement, jsonStateList)
    {
        var countrySelect = $(this);
        var selectedCountry = $(this).val();

        if (!selectedCountry)
        {
            return;
        }

        ////////////////////////////////

        //remove any existing options first
        stateDropDownElement.empty();

        stateDropDownElement.append($('<option></option>').val('').html('----Make Selection----'));
        $.each(jsonStateList, function(level1Object, level2Object)
        {
            var stateLong = level2Object.StateLongName;
            var stateShort = level2Object.StateShortName;
            stateDropDownElement.append($('<option></option>').val(stateShort).html(stateLong));

        });

        //reselect any previously choosen state
        var stateCode = $.cookie('state_cookie');
        stateDropDownElement.children(' option[value=' + stateCode + ']').attr('selected', 'selected');        

        $("#stateRow").show("slow");
        stateDropDownElement.show("slow");

        ////////////////////////////////

    }

})(jQuery);
