/// <summary>
    /// Handles the Click event of the cmdUpload control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void cmdUpload_Click(object sender, EventArgs e)
    {
        try
        {
            WorkItem workItem             = PageWorkItem;
            bool     bIsActivityInsert    = IsActivityInsert();
            bool     bIsHistoryInsert     = IsHistoryInsert();
            string   strTempAssociationID = string.Empty;

            if (bIsActivityInsert || bIsHistoryInsert)
            {
                if (workItem != null)
                {
                    object oStrTempAssociationID = workItem.State["TempAssociationID"];
                    if (oStrTempAssociationID != null)
                    {
                        strTempAssociationID = oStrTempAssociationID.ToString();
                    }
                }
            }

            string attachPath;
            if (!bIsActivityInsert && !bIsHistoryInsert)
            {
                attachPath = Rules.GetAttachmentPath();
            }
            else
            {
                /* When in Insert mode we keep the attachments in a special location, in case the operation gets cancelled out. */
                attachPath = Rules.GetTempAttachmentPath();
            }

            if (!Directory.Exists(attachPath))
            {
                Directory.CreateDirectory(attachPath);
            }

            if (!String.IsNullOrEmpty(txtInsertURL.Text))
            {
                if (String.IsNullOrEmpty(txtInsertDesc.Text))
                {
                    return;
                }

                string url    = "URL=";
                Random random = new Random();
                //string urlFile = random.Next(9999) + "-" + txtInsertDesc.Text + "-" + random.Next(9999) + ".URL";
                string urlFile = Guid.NewGuid().ToString() + ".URL";
                string path    = attachPath + "/" + urlFile;
                if (!txtInsertURL.Text.Contains("://"))
                {
                    url += "http:\\\\";
                }
                FileStream   newFile      = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamWriter streamWriter = new StreamWriter(newFile);
                streamWriter.WriteLine("[InternetShortcut]");
                streamWriter.WriteLine(url + txtInsertURL.Text);
                streamWriter.WriteLine("IconIndex=0");
                streamWriter.Close();
                newFile.Close();

                IAttachment attachment = EntityFactory.Create <IAttachment>();
                attachment.Description = txtInsertDesc.Text;
                attachment.FileName    = urlFile;

                if (!bIsActivityInsert && !bIsHistoryInsert)
                {
                    attachment.InsertURLAttachment(path);
                }
                else
                {
                    if (string.IsNullOrEmpty(strTempAssociationID) ||
                        (workItem != null) && (System.Convert.ToString(workItem.State["TempAssociationID"]) == strTempAssociationID))
                    {
                        /* Save to get an ID used for temp purposes. */

                        IUserService userServ = ApplicationContext.Current.Services.Get <IUserService>();
                        strTempAssociationID = userServ.UserId;
                        //attachment.Save();

                        if (workItem != null)
                        {
                            workItem.State["TempAssociationID"] = strTempAssociationID;
                        }
                    }
                    Rules.InsertTempURLAttachment(attachment, EntityContext.EntityType, strTempAssociationID, path);
                }

                txtInsertURL.Text  = String.Empty;
                txtInsertDesc.Text = String.Empty;
                LoadAttachments();
            }
            else if (uplInsertUpload.UploadedFiles.Count > 0)
            {
                UploadedFile file = uplInsertUpload.UploadedFiles[0];
                if (file != null)
                {
                    IAttachment attachment = EntityFactory.Create <IAttachment>();
                    attachment.Description = txtInsertDesc.Text;
                    if (String.IsNullOrEmpty(txtInsertDesc.Text))
                    {
                        attachment.Description = file.GetNameWithoutExtension();
                    }
                    if (!bIsActivityInsert && !bIsHistoryInsert)
                    {
                        attachment.InsertFileAttachment(WriteToFile(file));
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(strTempAssociationID) ||
                            (workItem != null) && (System.Convert.ToString(workItem.State["TempAssociationID"]) == strTempAssociationID))
                        {
                            attachment.FileName = Path.GetFileName(file.FileName); //ie sends up the full path, ff sends up just the filename
                            /* Save to get an ID used for temp purposes. */
                            attachment.Save();
                            IUserService userServ = ApplicationContext.Current.Services.Get <IUserService>();
                            strTempAssociationID = userServ.UserId; // If we use the attachment id it fails when they try to insert an item with 2 attachments
                            if (workItem != null)
                            {
                                workItem.State["TempAssociationID"] = strTempAssociationID;
                            }
                        }
                        Rules.InsertTempFileAttachment(attachment, EntityContext.EntityType, strTempAssociationID, WriteToFile(file));
                    }
                    txtInsertDesc.Text = String.Empty;
                    LoadAttachments();
                }
            }
            else
            {
                DialogService.ShowMessage(GetLocalResourceObject("Error_UnableToLoad_lz").ToString());
                log.Warn(GetLocalResourceObject("Error_UnableToLoad_lz").ToString());
            }
            hideAdds();
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
            DialogService.ShowMessage(ex.Message);
        }
    }