var show_dialog = function(name, more_config, context, query_string, force_generate)
{
    var dialog;
    
    //Forcing the generation of a brand new dialog, instead of re-using the previous one from the DOM.
    if (show_dialog.dialogs[name] && force_generate)
    {
        show_dialog.dialogs[name].destroy();
        show_dialog.dialogs[name] = null;
    }
    
    var config = {
        id: '_show_dialog_' + name, 
        //autoCreate:true,
        width:500,
        height:300,
        shadow:true,
        minWidth:300,
        minHeight:250,
        draggable: true,
        modal: true,
        shim: true,
        collapsible: false,
        closable: false,
        text: 'Submit',          // used for the DefaultButton
        closeText: 'Close'		 // used for the close button
    };
    
    for (var i in show_dialog.config[name])
    {
        config[i] = show_dialog.config[name][i];
    }
    
    for (var i in more_config)
    {
        config[i] = more_config[i];
    }
    
    dialog = new Ext.Window(config);
    
    if (config.onSubmit)
    {
        var submitFunc  = function()
        {
            if (config.onSubmit(context, dialog))
            {
                dialog.destroy(); 
            }
        }

        /*dialog.setDefaultButton(*/dialog.addButton(config.text, submitFunc, dialog)//);
        dialog.addButton('Close', function(){this.destroy();}, dialog);
    }
    else
    {	
       	dialog.addButton(config.closeText, function(){this.destroy();}, dialog);
    }
    
    show_dialog.dialogs[name] = dialog;
    
    dialog.on('show', function(dlg)
    {
	    dlg.getKeyMap().addBinding({key: Ext.EventObject.ENTER, fn: submitFunc, scope: dlg});
	    dlg.getKeyMap().addBinding({key: Ext.EventObject.ESC, fn: function(){this.destroy();}, scope: dlg});
	    
        var el  = Ext.get(dlg.body);
        var mgr = el.getUpdater();
        
        var update_func = function(el, o)
        {
            var inputs = el.dom.getElementsByTagName('input');
            
            // focus first empty input
            for (var i = 0; i < inputs.length; i++)
            {
                if (! inputs[i].value)
                {
                    inputs[i].focus();
                    break;
                }
            }
            
            if (config.autoSize)
            {
                dialog.setSize(el.first().dom.scrollWidth  + dlg.getFrameWidth(),
                               el.first().dom.scrollHeight + dlg.getFrameHeight() + 10);
            }
            
			var viewport_height = 0;
			if( typeof( window.innerWidth ) == 'number' )
			{
			  //Non-IE
			  viewport_height = window.innerHeight;
			}
			else if
			( document.documentElement && (document.documentElement.clientHeight ) )
			{
			  //IE 6+ in 'standards compliant mode'
			  viewport_height = document.documentElement.clientHeight;
			}
            
			//GT-2031: Remove repositioning it to the top of the page.
//            if (dialog.getPosition()[1] < 5 || dialog.getBox().height > viewport_height)
//            {
//                dialog.setPagePosition(dialog.getPosition()[0], 5);
//            }
            
            mgr.un('update', update_func);
        }
        
        mgr.on('update', update_func);
        
        if (!query_string)
        {
            query_string = '';
        }
        
        mgr.update({url: '/common/ajax/show_dialog/' + name + query_string, scripts: true});
    });
    
    dialog.show();
};

show_dialog.get = function(name)
{
    return this.dialogs[name];
}

show_dialog.dialogs = new Object();
show_dialog.config  = new Object();


//----- dialog config -----//


show_dialog.config.new_topic = {
    title:      'Add a new topic box',
    onSubmit:   function()
                {
                    var tag;
                    if (tag = Ext.get('news_tags_select').getValue())
                    {
                        add_widget('TagWidget', tag);
                    }
                    return true;
                },
    width:      300
};


show_dialog.config.new_link = {
    title:      'Add a new link to My Web Links',
    onSubmit:   function()
    {
        bookmark(Ext.get('new_link_url').getValue(), true, Ext.get('new_link_text').getValue());
        return true;
    },
    height:     200,
    minHeight:  150,
    width:      400,
    minWidth:   400
};


show_dialog.config.new_feed = {
    title:      'Add a new feed',
    onSubmit:   function() { add_widget('FeedWidget', Ext.get('new_feed_url').getValue()); return true;},
    height:     150,
    minHeight:  150,
    width:      400,
    minWidth:   400
};


show_dialog.config.login = {
    title:      'Login to MyGT',
    onSubmit:   function()
    {
        var login_forms = document.getElementsByName('login_form');
        
        for(var i = 0; i < login_forms.length; i++)
        {
            //Not hidden
            if (login_forms[i].offsetHeight > 0 && login_forms[i].offsetWidth > 0)
            {
                login_user(login_forms[i], Ext.WindowMgr.get('_show_dialog_login'));
                break;
            }
        }
    },
    text:       'Login',
    width:      670,
    height:     285
};

show_dialog.config.security_code = {
    title:      'Security Code Information',
    height:     350
};

show_dialog.config.bookmark_not_logged_in = {
    title:      'Not logged in',
    height:     150
};

show_dialog.config.rc_not_logged_in = {
    title:      'Not logged in',
    height:     150
};

show_dialog.config.registration_not_logged_in = {
    title:      'Not logged in',
    height:     150
};

show_dialog.config.briefcase_not_logged_in = {
    title:      'Not logged in',
    height:     150
};

show_dialog.config.email = {
    onSubmit:   send_email,
    height:     350
};

show_dialog.config.govtech_account = {
    title:      'Do you have a Govtech.com Account?',
    height:     350
};

show_dialog.config.sub_renewal = {
    height:     550
};

show_dialog.config.comment = {
    onSubmit:   create_comment
};

show_dialog.config.comment_form = {
    title:      'Post a Comment',
    text:       'Login',
    height:     570,
    width:      350
};
show_dialog.config.twenty_years_in_review = {
    title:      '20 Years in Review',
    width:      950,
    height:     600
};

show_dialog.config.asset_reg = {
    title:      'Registration Required ',
    width:      570,
    height:     640,
    autoScroll: true,
    autoSize:   true
};

show_dialog.config.mpc_gateway_rc_asset_reg = {
    title:      'Registration Required',
    text:       'Login',
    width:      600,
    height:     600
};