Index: /CKEditor/branches/prototype/_source/plugins/forms/dialogs/select.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/forms/dialogs/select.js	(revision 2837)
+++ /CKEditor/branches/prototype/_source/plugins/forms/dialogs/select.js	(revision 2838)
@@ -31,4 +31,53 @@
 	};
 
+	var resetDialog = function( dialog ){
+		var names = dialog.getContentElement( 'info', 'cmbName' );
+		var values = dialog.getContentElement( 'info', 'cmbValue' );
+		var namesOptions = CKEDITOR.plugins.forms.select.getOptions( names );
+		var valuesOptions = CKEDITOR.plugins.forms.select.getOptions( values );
+
+		// Reset dialog.
+		for ( var i = namesOptions.length-1 ; i >=0 ; i-- )
+		{
+			namesOptions[i].parentNode.removeChild( namesOptions[i] );
+			valuesOptions[i].parentNode.removeChild( valuesOptions[i] );
+		}
+		dialog.getContentElement( 'info', 'txtOptName' ).setValue( '' );
+		dialog.getContentElement( 'info', 'txtOptValue' ).setValue( '' );
+	};
+	
+	// Function called in onShow to load selected element.
+	var loadElements = function( editor, selection, ranges, element )
+	{
+		var dialog = this;
+
+		//Reset (after checking selection)
+		resetDialog( dialog );
+
+		var names = dialog.getContentElement( 'info', 'cmbName' );
+		var values = dialog.getContentElement( 'info', 'cmbValue' );
+			
+		dialog.editObj = element;
+		dialog.editMode = true;
+		dialog.saveSelection();
+
+		dialog.getContentElement( 'info', 'txtName' ).setValue( dialog.editObj.getAttribute( "name" ) );
+		dialog.getContentElement( 'info', 'txtSize' ).setValue( dialog.editObj.getAttribute( "size" ) );
+		dialog.getContentElement( 'info', 'chkMulti' ).setValue( dialog.editObj.getAttribute( "multiple" ) );
+
+		var objOptions = CKEDITOR.plugins.forms.select.getOptions( dialog.editObj );
+
+		//Load Options into dialog.
+		for ( var i = 0 ; i < objOptions.length ; i++ )
+		{
+			var oText	= objOptions[i].innerHTML;
+			var oValue	= objOptions[i].value;
+			if ( objOptions[i].getAttribute( 'selected' ) == 'selected' )
+				dialog.getContentElement( 'info', 'txtValue' ).setValue( objOptions[i].value );		
+			var oTOption = CKEDITOR.plugins.forms.select.addOption( names, oText, oText, editor.document );
+			var oVOption = CKEDITOR.plugins.forms.select.addOption( values, oValue, oValue, editor.document );
+		}
+	}
+
 	return {
 		title : editor.lang.select.title,
@@ -38,47 +87,34 @@
 		onShow : function()
 		{
-			var parentEditor = this.getParentEditor();
 			this.editMode = false;
 
-			/*
-			Check selection.
-			Fill this.editObj if the SELECT element is selected.
-			*/
-
-			var names = this.getContentElement( 'info', 'cmbName' );
-			var values = this.getContentElement( 'info', 'cmbValue' );
-			var namesOptions = CKEDITOR.plugins.forms.select.getOptions( names );
-			var valuesOptions = CKEDITOR.plugins.forms.select.getOptions( values );
-
-			// Reset dialog.
-			for ( var i = namesOptions.length-1 ; i >=0 ; i-- )
+			// IE BUG: Selection must be in the editor for getSelection() to work.
+			this.restoreSelection();
+
+			var editor = this.getParentEditor(),
+				selection = editor.getSelection(),
+				ranges = selection.getRanges();
+
+			// Check selection. Fill in all the relevant fields if there's already one link selected.
+			if ( ranges.length == 1 )
 			{
-				namesOptions[i].parentNode.removeChild( namesOptions[i] );
-				valuesOptions[i].parentNode.removeChild( valuesOptions[i] );
-			}
-
-
-			if ( this.editMode == true )		//Load existing object.
+				ranges[0].enlarge( CKEDITOR.ENLARGE_ELEMENT );
+				rangeRoot = ranges[0].getCommonAncestor( true );
+				var element = rangeRoot.getAscendant( 'select', true );	
+				if ( element )
+				{
+					loadElements.apply( this, [ editor, selection, ranges, element ] );
+					selection.selectElement( element );
+					this.saveSelection();
+				}
+			}
+
+			if ( this.editMode == false )		//New object.
 			{
-				this.getContentElement( 'info', 'txtOptValue' ).setValue( this.editObj.getAttribute( "name" ) );
-				this.getContentElement( 'info', 'txtOptValue' ).setValue( this.editObj.getAttribute( "value" ) );
-				this.getContentElement( 'info', 'txtSize' ).setValue( this.editObj.getAttribute( "size" ) );
-				this.getContentElement( 'info', 'chkMulti' ).setValue( this.editObj.getAttribute( "multiple" ) );
-
-				var objOptions = CKEDITOR.plugins.forms.select.getOptions( this.editObj );
-
-				//Load Options into dialog.
-				for ( var i = 0 ; i < objOptions.length ; i++ )
-				{
-					var oText	= objOptions[i].inerHTML;
-					var oValue	= objOptions[i].value;
-
-					var oTOption = CKEDITOR.plugins.forms.select.addOption( names, oText, oText, parentEditor.document );
-					var oVOption = CKEDITOR.plugins.forms.select.addOption( values, oValue, oValue, parentEditor.document );
-				}
-			}
-			else 	//New object.
+				resetDialog( this );	//Reset (after checking selection)
 				this.editObj = this.getParentEditor().document.createElement( 'select' );
-
+			}
+
+			this.pushDefault();
 			this.getContentElement( 'info', 'txtName' ).focus();
 			this.changedAttibutes = new Array();
@@ -86,10 +122,10 @@
 		onOk : function()
 		{
-
 			var objOptions = CKEDITOR.plugins.forms.select.getOptions( this.editObj );
 
 			// Remove all options.
-			for ( var i = objOptions.length-1 ; i >= 0 ; i-- )
-				objOptions[i].parentNode.removeChild( objOptions[i] );
+			for ( var i = objOptions.length-1 ; i >= 0 ; i-- ){
+				objOptions[0].parentNode.removeChild( objOptions[0] );
+			}
 
 			var selectValue = this.getContentElement( 'info', 'txtValue' ).getValue();
@@ -98,6 +134,5 @@
 
 			//Set added/changed attributes
-			for( var name in this.changedAttibutes )
-				this.editObj.setAttribute(name, this.changedAttibutes[ name ] );
+			this.editObj.setAttributes( this.changedAttibutes );
 
 			var parentEditor = this.getParentEditor();
@@ -227,4 +262,5 @@
 									//Add new option.
 									var dialog = this.getDialog();
+									var parentEditor = dialog.getParentEditor();
 									var name = dialog.getContentElement( 'info', 'txtOptName' );
 									var value = dialog.getContentElement( 'info', 'txtOptValue' );
@@ -233,6 +269,6 @@
 									var values = dialog.getContentElement( 'info', 'cmbValue' );
 
-									CKEDITOR.plugins.forms.select.addOption(names, name.getValue(), name.getValue() );
-									CKEDITOR.plugins.forms.select.addOption(values, value.getValue(), value.getValue() );
+									CKEDITOR.plugins.forms.select.addOption(names, name.getValue(), name.getValue(), parentEditor.document );
+									CKEDITOR.plugins.forms.select.addOption(values, value.getValue(), value.getValue(), parentEditor.document );
 
 									name.setValue( "" );
@@ -255,6 +291,9 @@
 
 									var iIndex = CKEDITOR.plugins.forms.select.getSelectedIndex( names );
-									CKEDITOR.plugins.forms.select.modifyOption( names, iIndex, name.getValue(), name.getValue() );
-									CKEDITOR.plugins.forms.select.modifyOption( values, iIndex, value.getValue(), value.getValue() );
+									if ( iIndex >= 0 )
+									{
+										CKEDITOR.plugins.forms.select.modifyOption( names, iIndex, name.getValue(), name.getValue() );
+										CKEDITOR.plugins.forms.select.modifyOption( values, iIndex, value.getValue(), value.getValue() );
+									}
 								}
 							}
Index: /CKEditor/branches/prototype/_source/plugins/forms/plugin.js
===================================================================
--- /CKEditor/branches/prototype/_source/plugins/forms/plugin.js	(revision 2837)
+++ /CKEditor/branches/prototype/_source/plugins/forms/plugin.js	(revision 2838)
@@ -108,7 +108,6 @@
 		addOption : function( combo, optionText, optionValue, documentObject, index )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, false );
 			var oOption;
-
 			if ( documentObject )
 				oOption = documentObject.createElement( "OPTION" );
@@ -116,20 +115,32 @@
 				oOption = document.createElement( "OPTION" );
 
-			if ( oOption.tagName && oOption.tagName.toLowerCase() == 'option' )
-			{
-				oOptionObj = oOption;
-			}
-			else if (oOption.$ && oOption.$.tagName && oOption.$.tagName.toLowerCase() == 'option' )
-			{
-				oOptionObj = oOption.$;
-			}
-			if ( !isNaN( parseInt( index, 10 ) ) )
-				combo.options.add( oOptionObj, index );
-			else
-				combo.options.add( oOptionObj );
-
-			oOptionObj.innerHTML = optionText.length > 0 ? CKEDITOR.tools.htmlEncode( optionText ) : '';
-			oOptionObj.value     = optionValue;
-
+			if ( combo.$ && oOption.$ && oOption.$.tagName )
+			{
+				combo.append( oOption );
+				oOption.setText( optionText.length > 0 ? CKEDITOR.tools.htmlEncode( optionText ) : '' );
+				oOption.setValue( optionValue );
+				oOptionObj = oOption;	
+			}
+			else if ( combo )
+			{
+				if ( combo.$ )			//TODO: improve this case
+					combo = combo.$;
+
+				if ( oOption.$ && oOption.$.tagName )
+					oOptionObj = oOption.$;
+				else
+					oOptionObj = oOption;
+	
+				if ( !isNaN( parseInt( index, 10 ) ) )
+					combo.options.add( oOptionObj, index );
+				else
+					combo.options.add( oOptionObj );
+					
+				oOptionObj.innerHTML = optionText.length > 0 ? CKEDITOR.tools.htmlEncode( optionText ) : '';
+				oOptionObj.value     = optionValue;
+			}
+			else 
+				return false;
+				
 			return oOptionObj;
 		},
@@ -138,5 +149,5 @@
 		removeSelectedOptions : function ( combo )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, true );
 
 			// Save the selected index
@@ -147,7 +158,5 @@
 			// Remove all selected options.
 			for ( var i = oOptions.length - 1 ; i >= 0 ; i-- )
-			{
 				if ( oOptions[i].selected ) combo.remove(i);
-			}
 
 			// Reset the selection based on the original selected index.
@@ -162,5 +171,5 @@
 		modifyOption : function( combo, index, title, value )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, true  );
 			if ( index < 0 ) return ;
 
@@ -172,5 +181,5 @@
 		changeOptionPosition : function ( combo, steps )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, true  );
 			var iActualIndex = combo.selectedIndex;
 
@@ -200,13 +209,12 @@
 		getSelectedIndex : function( combo )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, true );
 
 			var iIndex = combo.selectedIndex;
-			if ( iIndex < 0 ) return ;
 			return iIndex;
 		},
 		setSelectedIndex : function( combo, index )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, true );
 
 			if ( index < 0 ) return ;
@@ -217,16 +225,23 @@
 		getOptions : function ( combo )
 		{
-			combo = this._getObject( combo );
+			combo = this._getObject( combo, true );
 			return combo.options;
 		},
-		_getObject : function ( obj )
+		_getObject : function ( obj, getReal )
 		{
 			if ( obj ){
 				if ( obj.tagName && obj.tagName.toLowerCase() == 'select' )
 					return obj;
+				else if ( obj.$ )
+					if ( getReal )
+						return obj.$;
+					else
+						return obj;
 				else if ( obj.domId && obj.getInputElement().$ )
-					return obj.getInputElement().$;
-				else if ( obj.$ )
-					return obj.$;
+					if ( getReal )
+						return obj.getInputElement().$;
+					else
+						return obj.getInputElement();
+				return false;
 			}
 			else
