
$( window ).load(function() {

    // CMS: Location (state change on country selection)
    var stateHtml = $("#user_state").html(); 
    var countryVal = $("#user_country").val();
    var userId = $("#user_id").val();


    //var stateVal =  $("#user_state").val(); alert(stateVal);

    if(countryVal != 'US' && countryVal != 'IN' && $("#user_country").val() != 'CA' ){ 
        $("#user_state").parent().html("<input type='text' name='user_state' id='user_state' />&nbsp");
        $.get(mainUrl+'cmshelper/ajaxGetUserState/'+userId, function(result) {
            $('#user_state').val(result);
        });
    }else{ 
            $.get(mainUrl+'cmshelper/ajaxGetStatesByCountryUser/'+$("#user_country").val()+'/'+userId+'/edit', function(result) {
                $("#user_state").parent().html("<select name='user_state' id='user_state'>"+result+"</select>&nbsp");
            });
        }

    $("#user_country").live("change", function() {

        if($("#user_country").val() != 'US' && $("#user_country").val() != 'IN' && $("#user_country").val() != 'CA' ){
            $("#user_state").parent().html("<input type='text' name='user_state' id='user_state' />&nbsp");
            $.get(mainUrl+'cmshelper/ajaxGetUserState/'+userId, function(result) {
                $('#user_state').val(result);
            });
        }
        else{  
            $.get(mainUrl+'cmshelper/ajaxGetStatesByCountryUser/'+$("#user_country").val(), function(result) {
                $("#user_state").parent().html("<select name='user_state' id='user_state'>"+result+"</select>&nbsp ");
            });
        }
    });
    

});
