Index: /CKEditor/trunk/CHANGES.html
===================================================================
--- /CKEditor/trunk/CHANGES.html	(revision 4538)
+++ /CKEditor/trunk/CHANGES.html	(revision 4539)
@@ -78,4 +78,5 @@
 		<li><a href="http://dev.fckeditor.net/ticket/4672">#4672</a> : Fixed unable to redo the insertion of horizontal line.</li>
 		<li><a href="http://dev.fckeditor.net/ticket/4677">#4677</a> : Fixed 'Tab' key is trapped by hidden dialog elements.</li>
+		<li><a href="http://dev.fckeditor.net/ticket/4073">#4073</a> : Fixed insert template with replace option could result in empty document.</li>
 	<h3>
 		CKEditor 3.0.1</h3>
Index: /CKEditor/trunk/_source/plugins/dialog/plugin.js
===================================================================
--- /CKEditor/trunk/_source/plugins/dialog/plugin.js	(revision 4538)
+++ /CKEditor/trunk/_source/plugins/dialog/plugin.js	(revision 4539)
@@ -593,6 +593,10 @@
 		show : function()
 		{
-			if ( this._.editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
-				this._.editor.getSelection().lock();
+			var editor = this._.editor;
+			if ( editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
+			{
+				var selection = editor.getSelection();
+				selection && selection.lock();
+			}
 
 			// Insert the dialog's element to the root document.
@@ -780,5 +784,8 @@
 
 				if ( editor.mode == 'wysiwyg' && CKEDITOR.env.ie )
-					editor.getSelection().unlock( true );
+				{
+					var selection = editor.getSelection();
+					selection && selection.unlock( true );
+				}
 			}
 			else
Index: /CKEditor/trunk/_source/plugins/templates/dialogs/templates.js
===================================================================
--- /CKEditor/trunk/_source/plugins/templates/dialogs/templates.js	(revision 4538)
+++ /CKEditor/trunk/_source/plugins/templates/dialogs/templates.js	(revision 4539)
@@ -82,4 +82,15 @@
 		if( isInsert )
 		{
+			// Everything should happen after the document is loaded (#4073).
+			editor.on( 'contentDom', function( evt )
+			{
+				evt.removeListener();
+				dialog.hide();
+
+				// Place the cursor at the first editable place.
+				var range = new CKEDITOR.dom.range( editor.document );
+				range.moveToElementEditStart( editor.document.getBody() );
+				range.select( true );
+			} );
 			editor.setData( html );
 		}
@@ -87,7 +98,6 @@
 		{
 			editor.insertHtml( html );
+			dialog.hide();
 		}
-
-		dialog.hide();
 	}
 
