protected void btnUpload_Click(object sender, EventArgs e)
        {
                AudioUpload[] audioUploads = AudioUpload.Load(null, CurrentUserSession.Username, null, null);

                if (audioUploads.Length >= CurrentUserSession.BillingPlanOptions.MaxAudioUploads.Value
                    && (CurrentUserSession.Level != null && audioUploads.Length >= CurrentUserSession.Level.Restrictions.MaxAudioUploads))
                {
                    ((PageBase)Page).StatusPageMessage = Lang.Trans("You cannot upload more audio files!");
                    Response.Redirect("ShowStatus.aspx");
                    return;
                }

            if (!fileAudio.HasFile)
            {
                lblError.Text = Lang.Trans("Please select audio file!");
                return;
            }

            string title = txtTitle.Text.Length > 0 ? txtTitle.Text : Path.GetFileNameWithoutExtension(fileAudio.FileName);

            //string tempfile = Path.GetTempFileName();
            //fileAudio.SaveAs(tempfile);

            var audioUpload = new AudioUpload(CurrentUserSession.Username);
            if (CurrentUserSession != null)
            {
                if (CurrentUserSession.BillingPlanOptions.AutoApproveAudioUploads.Value
                    || CurrentUserSession.Level != null && CurrentUserSession.Level.Restrictions.AutoApproveAudioUploads)
                {
                    audioUpload.Approved = true;
                }
            }
            audioUpload.Title = title;
            audioUpload.IsPrivate = cbPrivateAudio.Checked;
            audioUpload.Save(); // Save to get new ID

            if (audioUpload.Approved && !audioUpload.IsPrivate)
            {
                #region Add NewFriendAudioUpload Event

                Event newEvent = new Event(audioUpload.Username);

                newEvent.Type = Event.eType.NewFriendAudioUpload;
                NewFriendAudioUpload newFriendAudioUpload = new NewFriendAudioUpload();
                newFriendAudioUpload.AudioUploadID = audioUpload.Id;
                newEvent.DetailsXML = Misc.ToXml(newFriendAudioUpload);

                newEvent.Save();

                string[] usernames = Classes.User.FetchMutuallyFriends(audioUpload.Username);

                foreach (string friendUsername in usernames)
                {
                    if (Config.Users.NewEventNotification)
                    {
                        string text = String.Format("Your friend {0} has uploaded a new audio".Translate(),
                                              "<b>" + audioUpload.Username + "</b>");
                        int imageID = 0;
                        try
                        {
                            imageID = Photo.GetPrimary(audioUpload.Username).Id;
                        }
                        catch (NotFoundException)
                        {
                            User user = null;
                            try
                            {
                                user = User.Load(audioUpload.Username);
                                imageID = ImageHandler.GetPhotoIdByGender(user.Gender);
                            }
                            catch (NotFoundException) { break; }
                        }
                        string thumbnailUrl = ImageHandler.CreateImageUrl(imageID, 50, 50, false, true, true);
                        User.SendOnlineEventNotification(audioUpload.Username, friendUsername,
                                                                 text, thumbnailUrl,
                                                                 UrlRewrite.CreateShowUserUrl(audioUpload.Username));
                    }
                }

                #endregion
            }

            string userFilesPath = "~/UserFiles/" + CurrentUserSession.Username;
            string userFilesDir = Server.MapPath(userFilesPath);
            if (!Directory.Exists(userFilesDir))
            {
                Directory.CreateDirectory(userFilesDir);
            }

            fileAudio.SaveAs(userFilesDir + @"\audio_" + audioUpload.Id + ".mp3");

            //File.Move(tempfile, userFilesDir + @"\audio_" + audioUpload.Id + ".mp3");
        }
        protected void dgPendingAudio_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            if (!HasWriteAccess)
                return;

            AudioUpload audioUpload = AudioUpload.Load(Convert.ToInt32(e.CommandArgument));

            if (e.CommandName == "approve")
            {
                if (audioUpload != null)
                {
                    audioUpload.Approved = true;
                    audioUpload.Save();

                    if (!audioUpload.IsPrivate)
                    {
                        #region Add NewFriendAudioUpload Event

                        Event newEvent = new Event(audioUpload.Username);

                        newEvent.Type = Event.eType.NewFriendAudioUpload;
                        NewFriendAudioUpload newFriendAudioUpload = new NewFriendAudioUpload();
                        newFriendAudioUpload.AudioUploadID = audioUpload.Id;
                        newEvent.DetailsXML = Misc.ToXml(newFriendAudioUpload);

                        newEvent.Save();

                        string[] usernames = Classes.User.FetchMutuallyFriends(audioUpload.Username);

                        foreach (string friendUsername in usernames)
                        {
                            if (Config.Users.NewEventNotification)
                            {
                                string text = String.Format("Your friend {0} has uploaded a new audio".TranslateA(),
                                                      "<b>" + audioUpload.Username + "</b>");
                                int imageID = 0;
                                try
                                {
                                    imageID = Photo.GetPrimary(audioUpload.Username).Id;
                                }
                                catch (NotFoundException)
                                {
                                    User user = null;
                                    try
                                    {
                                        user = Classes.User.Load(audioUpload.Username);
                                        imageID = ImageHandler.GetPhotoIdByGender(user.Gender);
                                    }
                                    catch (NotFoundException) { break; }
                                }
                                string thumbnailUrl = ImageHandler.CreateImageUrl(imageID, 50, 50, false, true, true);
                                Classes.User.SendOnlineEventNotification(audioUpload.Username, friendUsername,
                                                                         text, thumbnailUrl,
                                                                         UrlRewrite.CreateShowUserUrl(audioUpload.Username));
                            }
                        }

                        #endregion
                    }

                    Classes.User.AddScore(audioUpload.Username, Config.UserScores.ApprovedAudio,
                        "ApprovedAudio");

                    #region e-mail notification

                    try
                    {
                        Classes.User user = Classes.User.Load(audioUpload.Username);

                        MiscTemplates.ApproveAudioMessage approveAudioMessageTemplate =
                            new MiscTemplates.ApproveAudioMessage(user.LanguageId);
                        Message.Send(Config.Users.SystemUsername, user.Username, approveAudioMessageTemplate.Message, 0);
                    }
                    catch (NotFoundException ex)
                    {
                        Log(ex);
                    }

                    #endregion

                    PopulateDataGrid();
                }
            }
            else if (e.CommandName == "reject")
            {
                if (audioUpload != null)
                {
                    AudioUpload.Delete(Convert.ToInt32(e.CommandArgument));

                    Classes.User.AddScore(audioUpload.Username, Config.UserScores.RejectedAudio,
                        "RejectedAudio");

                    #region e-mail notification

                    try
                    {
                        Classes.User user = Classes.User.Load(audioUpload.Username);

                        MiscTemplates.RejectAudioMessage rejectAudioMessageTemplate =
                            new MiscTemplates.RejectAudioMessage(user.LanguageId);
                        Message.Send(Config.Users.SystemUsername, user.Username, rejectAudioMessageTemplate.Message, 0);
                    }
                    catch (NotFoundException ex)
                    {
                        Log(ex);
                    }

                    #endregion

                    PopulateDataGrid();
                }
            }
        }