jQuery(function($) {
        $.extend({
                setFormValueFromRequest : function(json) {
                        $('select').each(function(){
                                var s = this;
                                $(s).children().each(function(){
                                        if (  typeof json[$(s).attr('name')] != 'undefined' && $(this).attr('value') == json[$(s).attr('name')] ) {
                                                $(this).attr('selected','true');
                                        }
                                });
                        });
                        $('input').each(function(){
                                if ( $(this).attr('type') == 'text' ) {
                                        if ( typeof json[$(this).attr('name')] != 'undefined' ) {
                                                $(this).attr('value',json[$(this).attr('name')]);
                                        }
                                }
                                if ( $(this).attr('type') == 'radio' ) {
                                        if ( typeof json[$(this).attr('name')] != 'undefined' && $(this).attr('value') == json[$(this).attr('name')] ) {
                                                $(this).attr('checked','true');
                                        }
                                }
                                if ( $(this).attr('type') == 'checkbox' ) {
                                        if ( typeof json[$(this).attr('name')] != 'undefined' && $(this).attr('value') == json[$(this).attr('name')] ) {
                                                $(this).attr('checked','true');
                                        } else if ( typeof json[$(this).attr('name').replace('[]','')] != 'undefined') {
                                                if ( $.inArray($(this).attr('value'),json[$(this).attr('name').replace('[]','')]) != -1 ) {
                                                        $(this).attr('checked','true');
                                                }
                                        }

                                }
                        });
                },
                setHiddenValueFromRequest : function(json) {
                        for( key in json ) {
                                var is_exists = false;
                                (function(n) {
                                        for ( i in n ) {
                                                if ( typeof n[i] == 'string' ) {
                                                        $(n[i]).each(function(){
                                                                if ( $(this).attr('name') && $(this).attr('name').replace('[]','') == key ) {
                                                                        is_exists = true;
                                                                }
                                                        });
                                                }
                                        }
                                })(['input','select','textarea']);

                                if ( is_exists == false && typeof json[key] != 'undefined' ) {
                                        if ( typeof json[key] == 'object' && json[key].length) {
                                                for ( j in json[key] ) {
                                                        if ( typeof json[key][j] != 'undefined' && typeof json[key][j] != 'function' ) {
                                                                $('form').append('<input type="hidden" name="' + key + '[]" value="' + json[key][j] + '">');
                                                        }
                                                }
                                        } else if ( typeof json[key] != 'undefined' && typeof json[key] != 'function' ) {
                                                $('form').append('<input type="hidden" name="' + key + '" value="' + json[key] + '">');
                                        }
                                }
                        }
                }
        });

});
