/// <summary>
        /// Retrieves the selected metadata (in search result listbox) from CSW catalog. 
        /// It handles including GUI validation and metadata retrieveal. 
        /// </summary>
        /// <remarks>
        /// Reused in View Summary, View Detail, and Download buttons. 
        /// </remarks>
        /// <returns>A XMLDocument object if successfully retrieved metadata. Null if otherwise.</returns>
        private XmlDocument RetrieveSelectedMetadataFromCatalog(bool bApplyTransform)
        {
            if (cmbCswCatalog.SelectedIndex == -1)
            {
                MessageBox.Show(resourceManager.GetString("catalogNotSelected"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }

            CswCatalog catalog = (CswCatalog)cmbCswCatalog.SelectedItem;
            if (catalog == null) { throw new NullReferenceException(resourceManager.GetString("catalogNotSpecified")); }

            CswRecord record = (CswRecord)lstSearchResults.SelectedItem;
            if (record == null) throw new NullReferenceException(resourceManager.GetString("searchNotSelected"));

            // connect to catalog if not connected already
            if (!catalog.IsConnected())
            {
                string errMsg = "";
                try { catalog.Connect(); }
                catch (Exception ex) { errMsg = ex.ToString(); }
                if (!catalog.IsConnected())
                {
                    MessageBox.Show(resourceManager.GetString("catalogConnectFailed"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }
            }
            bool isTransformed = false;
            // retrieve metadata doc by its ID
            CswSearchRequest searchRequest = new CswSearchRequest();
            searchRequest.Catalog = catalog;
            try
            {
                //MessageBox.Show(record.ID);         
                isTransformed = searchRequest.GetMetadataByID(record.ID, bApplyTransform);
                _mapServerUrl = searchRequest.GetMapServerUrl();
                //MessageBox.Show(record.FullMetadata);
            }
            catch (Exception ex)
            {
                FormMessageBox frmMessageBox = new FormMessageBox();
                frmMessageBox.Init("Failed to retrieve metadata from service: " + ex.Message,
                                    "Response from CSW service:\r\n" + searchRequest.GetResponse().ResponseXML,
                                    "Error");
                frmMessageBox.ShowDialog(this);
                return null;
            }

            CswSearchResponse response = searchRequest.GetResponse();
            CswRecord recordMetadata = response.Records[0];

            if (!isTransformed)
            {
                XmlDocument xmlDoc = new XmlDocument();
                try { xmlDoc.LoadXml(recordMetadata.FullMetadata); }
                catch (XmlException xmlEx)
                {
                    MessageBox.Show(resourceManager.GetString("loadXMLException"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return null;
                }
                return xmlDoc;
            }
            else
            {
                styledRecordResponse = recordMetadata.FullMetadata;
                return null;
            }
        }
        /// <summary>
        /// Retrieves the selected metadata (in search result listbox) from CSW catalog. Exception shall be thrown.
        /// </summary>
        /// <remarks>
        /// Called in View Metadata, Download Metadata, and Add to Map
        /// </remarks>
        /// <returns>A XMLDocument object if metadata was retrieved successfully. Null if otherwise.</returns>
        private void RetrieveAddToMapInfoFromCatalog()
        {
            try
            {
                // validate
                if (cmbCswCatalog.SelectedIndex == -1)
                {
                    MessageBox.Show(resourceManager.GetString("catalogNotSelected"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (lstSearchResults.SelectedIndex == -1) { throw new Exception(resourceManager.GetString("searchNotSelected")); }
                CswCatalog catalog = (CswCatalog)cmbCswCatalog.SelectedItem;
                if (catalog == null) { throw new NullReferenceException(resourceManager.GetString("catalogNotSpecified")); }
                CswRecord record = (CswRecord)lstSearchResults.SelectedItem;
                if (record == null) throw new NullReferenceException(resourceManager.GetString("searchNotSelected"));

                // connect to catalog if needed
                if (!catalog.IsConnected())
                {
                    string errMsg = "";
                    try { catalog.Connect(); }
                    catch (Exception ex) { errMsg = ex.Message; }

                    // exit if still not connected
                    if (!catalog.IsConnected())
                    {
                        MessageBox.Show(resourceManager.GetString("catalogConnectFailed"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                CswSearchRequest searchRequest = new CswSearchRequest();
                searchRequest.Catalog = catalog;
                try
                {
                    searchRequest.GetAddToMapInfoByID(record.ID);
                    _mapServerUrl = searchRequest.GetMapServerUrl();

                }
                catch (Exception ex)
                {
                    FormMessageBox frmMessageBox = new FormMessageBox();
                    frmMessageBox.Init("Failed to retrieve metadata from service: " + ex.Message,
                                        "Response from CSW service:\r\n" + searchRequest.GetResponse().ResponseXML,
                                        "Error");
                    frmMessageBox.ShowDialog(this);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(resourceManager.GetString("loadXMLException"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
 /// <summary>
 /// Display a error messagebox with details.
 /// </summary>
 /// <param name="message">error message</param>
 /// <param name="detailedMessage">details</param>
 private void ShowDetailedErrorMessageBox(string message, string details)
 {
     FormMessageBox frmMessageBox = new FormMessageBox();
     frmMessageBox.Init(message, details, StringResources.ErrorMessageDialogCaption);
     frmMessageBox.ShowDialog(this);
 }