protected void SubmitButton_Command(object sender, CommandEventArgs e)
        {
            InitializeWithDefaultValues();

            Page.Validate("TrackProtectValidation");

            bool atleastOneTaggingSelected = false;

            if (ViewState["Genre1"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["Genre2"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["Genre3"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["SubGenre1"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["SubGenre2"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["SubGenre3"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["SoundTag1"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["SoundTag2"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;
            else if (ViewState["SoundTag3"] != null && atleastOneTaggingSelected == false)
                atleastOneTaggingSelected = true;

            if (!FileUpload1.HasFile)
            {
                CustomValidationShow(Resources.Resource.SelectMp3File);
            }
            else if (FileUpload1.FileContent.Length > 12582912)
            {
                CustomValidationShow(Resources.Resource.FileSizeExceed);
            }
            else if (atleastOneTaggingSelected == false)
            {
                CustomValidationShow(Resources.Resource.SelectOneTag);
            }
            else if (Session["soundCloudAccessToken"] == null && (cbxSendToFacebook.Checked == true || cbxSendToTwitter.Checked == true))
            {
                CustomValidationShow(Resources.Resource.SoundCloudCredRequired);
            }
            else if (cbxSendToSoundCloud.Checked == false && (cbxSendToFacebook.Checked == true || cbxSendToTwitter.Checked == true))
            {
                CustomValidationShow(Resources.Resource.SelectSoundcloud);
            }

            if (Page.IsValid)
            {
                bool failed = false;
                long managedUserId = 0;
                if (Session["managed.userid"] != null)
                    managedUserId = Convert.ToInt64(Session["managed.userid"]);

                if (!FileUpload1.HasFile)
                {
                    // No file for the first option, need one to proceed or need a check to indicate
                    // only songtext and sheet music will be registered.
                    Response.Redirect(string.Format("~/Member/ErrorPage.aspx?title={0}&message={1}&returnurl={2}",
                        Resources.Resource.FirstFileRequiredTitle,
                        Resources.Resource.FirstFileRequiredMessage,
                        Uri.EscapeDataString(Request.RawUrl)), false);
                }

                long registerId = 0;

                string trackName = TrackNameText.Text;
                string doc1 = string.Empty, doc2 = string.Empty, doc3 = string.Empty;
                string uploadPath = string.Empty;
                using (Database db = new MySqlDatabase())
                {
                    UserInfo ui = db.GetUser(Util.UserId);

                    uploadPath = db.GetUserDocumentPath(managedUserId);

                    string isrcCode = string.Empty;
                    if (!string.IsNullOrEmpty(IsrcHandle.Text) && !string.IsNullOrEmpty(IsrcPostfix.Text))
                        isrcCode = IsrcHandle.Text + "-" + IsrcPostfix.Text;

                    if (!failed && FileUpload1.HasFile)
                    {
                        doc1 = FileUpload1.FileName;
                        try
                        {
                            FileUpload1.SaveAs(Path.Combine(uploadPath, Path.GetFileName(doc1)));
                            StatusInfo1.Text = string.Format(Resources.Resource.FileStoreSuccess, Path.GetFileName(doc1));
                        }
                        catch (Exception)
                        {
                            StatusInfo1.Text = string.Format(Resources.Resource.FileStoreFailed, Path.GetFileName(doc1));
                            failed = true;
                        }
                    }
                    if (!failed && FileUpload2.HasFile)
                    {
                        doc2 = FileUpload2.FileName;
                        try
                        {
                            FileUpload2.SaveAs(Path.Combine(uploadPath, Path.GetFileName(doc2)));
                            StatusInfo2.Text = string.Format(Resources.Resource.FileStoreSuccess, Path.GetFileName(doc2));
                        }
                        catch (Exception)
                        {
                            StatusInfo2.Text = string.Format(Resources.Resource.FileStoreFailed, Path.GetFileName(doc2));
                            failed = true;
                        }
                    }
                    if (!failed && FileUpload3.HasFile)
                    {
                        doc3 = FileUpload3.FileName;
                        try
                        {
                            FileUpload3.SaveAs(Path.Combine(uploadPath, Path.GetFileName(doc3)));
                            StatusInfo3.Text = string.Format(Resources.Resource.FileStoreSuccess, Path.GetFileName(doc3));
                        }
                        catch (Exception)
                        {
                            StatusInfo3.Text = string.Format(Resources.Resource.FileStoreFailed, Path.GetFileName(doc3));
                            failed = true;
                        }
                    }
                    if (!failed)
                    {
                        // Continue registration
                        string f1 = string.Empty, f2 = string.Empty, f3 = string.Empty;
                        if (!string.IsNullOrEmpty(doc1))
                            f1 = Path.Combine(uploadPath, Path.GetFileName(doc1));
                        if (!string.IsNullOrEmpty(doc2))
                            f2 = Path.Combine(uploadPath, Path.GetFileName(doc2));
                        if (!string.IsNullOrEmpty(doc3))
                            f3 = Path.Combine(uploadPath, Path.GetFileName(doc3));

                        // Get the cooperating artists to add to the certificate
                        CoopArtistList coopArtists = new CoopArtistList();
                        foreach (DataRow row in CoArtistsTable.Rows)
                        {
                            string name = Convert.ToString(row[0]);
                            string role = Convert.ToString(row[1]);
                            coopArtists.Add(new CoopArtist(name, role));
                        }

                        string certfilename = CreateCertificate(managedUserId, f1, f2, f3, isrcCode, trackName, coopArtists);
                        //long registerId = db.CreateRegistry(managedUserId, certfilename, f1);
                        registerId = db.CreateRegistry(Util.UserId, managedUserId, certfilename, f1, trackName.Trim(), isrcCode,
                            (Genre1.SelectedItem.Text == "--Select--" ? "" : Genre1.SelectedItem.Text),
                            (Genre2.SelectedItem.Text == "--Select--" ? "" : Genre2.SelectedItem.Text),
                            (Genre3.SelectedItem.Text == "--Select--" ? "" : Genre3.SelectedItem.Text),
                            (SubGenre1.SelectedItem.Text == "--Select--" ? "" : SubGenre1.SelectedItem.Text),
                            (SubGenre2.SelectedItem.Text == "--Select--" ? "" : SubGenre2.SelectedItem.Text),
                            (SubGenre3.SelectedItem.Text == "--Select--" ? "" : SubGenre3.SelectedItem.Text),
                            (Tag1.SelectedItem.Text == "--Select--" ? "" : Tag1.SelectedItem.Text),
                            (Tag2.SelectedItem.Text == "--Select--" ? "" : Tag2.SelectedItem.Text),
                            (Tag3.SelectedItem.Text == "--Select--" ? "" : Tag3.SelectedItem.Text), StageNameText.Text.Trim());

                        if (registerId > 0)
                        {
                            if (!failed && !string.IsNullOrEmpty(f1))
                                failed = (RegisterDoc(registerId, f1) == 0);

                            if (!failed && !string.IsNullOrEmpty(f2))
                                failed = (RegisterDoc(registerId, f2) == 0);

                            if (!failed && !string.IsNullOrEmpty(f3))
                                failed = (RegisterDoc(registerId, f3) == 0);
                            if (!failed && CoArtistsTable.Rows.Count > 0)
                            {
                                foreach (DataRow row in CoArtistsTable.Rows)
                                {
                                    string name = Convert.ToString(row[0]);
                                    string role = Convert.ToString(row[1]);
                                    long clientId = Convert.ToInt64(row[2]);
                                    string id = clientId.ToString();
                                    RegisterCoArtist(registerId, name, id, role);
                                }
                            }
                            if (!failed)
                            {
                                StatusInfo4.Text = Resources.Resource.RegistrationSuccessful;
                                db.DecrementCredits(Util.UserId);
                                db.AddCreditHistory(Util.UserId, 0L, 1, 0);

                                string certdocfile = Path.ChangeExtension(certfilename, ".pdf");
                                Util.SendRegistration(Convert.ToInt64(Session["managed.userid"]), uploadPath, trackName, certfilename, certdocfile);
                            }
                            else
                            {
                                StatusInfo4.Text = Resources.Resource.RegistrationFailed;
                            }
                        }
                        else
                        {
                            // Registration failed
                            StatusInfo4.Text = Resources.Resource.RegistrationFailed;
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(doc1))
                            DeleteFile(Path.Combine(uploadPath, Path.GetFileName(doc1)));
                        if (!string.IsNullOrEmpty(doc2))
                            DeleteFile(Path.Combine(uploadPath, Path.GetFileName(doc2)));
                        if (!string.IsNullOrEmpty(doc3))
                            DeleteFile(Path.Combine(uploadPath, Path.GetFileName(doc3)));
                    }
                }

                if (failed == false)
                {
                    Session["TrackProtected"] = true;

                    if (cbxSendToSoundCloud.Checked == false)
                        Session["SoundCloud.Response"] = "Deactive";
                    else if (cbxSendToSoundCloud.Checked && !File.Exists(Path.Combine(uploadPath, doc1)))
                        Session["SoundCloud.Response"] = "Error";
                    else if (cbxSendToSoundCloud.Checked && File.Exists(Path.Combine(uploadPath, doc1)))
                        Session["SoundCloud.Response"] = Publish.SoundCloudPublish(Convert.ToString(Session["soundCloudAccessToken"]), Path.Combine(uploadPath, doc1), trackName + System.Environment.NewLine + SoundCloudMsg.Text.Trim());

                    if (Session["SoundCloud.Response"] != null)
                    {
                        if (!Convert.ToString(Session["SoundCloud.Response"]).Contains("Deactive") && !Convert.ToString(Session["SoundCloud.Response"]).Contains("Error"))
                        {
                            string shortUrl =
                                BitlyApi.GetShortUrl(ConfigurationManager.AppSettings["SiteNavigationLink"] + "/idlookup.aspx?TrackID="
                                + EncryptionClass.Encrypt(Convert.ToString(registerId)));

                            string stream_url = string.Empty;

                            try
                            {
                                var obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(Convert.ToString(Session["SoundCloud.Response"]));

                                stream_url = Convert.ToString(obj["permalink_url"]) + "/" + Convert.ToString(obj["secret_token"]);
                            }
                            catch { }

                            #region Facebook

                            if (cbxSendToFacebook.Checked == false)
                                Session["Facebook.Response"] = "Deactive";
                            else if (cbxSendToFacebook.Checked)
                            {
                                String messageToPost = string.Empty;
                                messageToPost += FacebookMsg.Text.Trim() + System.Environment.NewLine + System.Environment.NewLine;
                                messageToPost += "Trackname : " + TrackNameText.Text.Trim() + System.Environment.NewLine;
                                messageToPost += "Trackprotect ID : " + shortUrl + System.Environment.NewLine;
                                //messageToPost += "Protected by: " + ConfigurationManager.AppSettings["SiteNavigationLink"] + System.Environment.NewLine;

                                if (cbxSendToGenreCommunityPage.Checked == false)
                                    Session["FacebookGenre.Response"] = "Deactive";
                                else if (cbxSendToGenreCommunityPage.Checked)
                                    Session["FacebookGenre.Response"] = postToFacebook(messageToPost, stream_url, Convert.ToString(ViewState["GenrePageToken"]), Convert.ToString(ViewState["GenrePageID"]));

                                if (cbxSendToSubGenreCommunityPage.Checked == false)
                                    Session["FacebookSubGenre.Response"] = "Deactive";
                                else if (cbxSendToSubGenreCommunityPage.Checked)
                                    Session["FacebookSubGenre.Response"] = postToFacebook(messageToPost, stream_url, Convert.ToString(ViewState["SubGenrePageToken"]), Convert.ToString(ViewState["SubGenrePageID"]));

                                if (cbxShareToUserWall.Checked == false)
                                    Session["Facebook.Response"] = "Deactive";
                                else if (cbxShareToUserWall.Checked)
                                    Session["Facebook.Response"] = postToFacebook(messageToPost, stream_url, Convert.ToString(Session["FBACC"]), Convert.ToString(Session["FBID"]));

                                if (cbxShareToFriends.Checked == false)
                                    Session["ShareToFriends"] = "Deactive";
                                else if (cbxShareToFriends.Checked)
                                {
                                    IList<string> friendList = getFriendIDs();

                                    foreach (var friendId in friendList)
                                        Session["ShareToFriends"] =
                                            postToFacebook(messageToPost, stream_url, Convert.ToString(Session["FBACC"]), friendId);
                                }

                                if (cbxShareToPages.Checked == false)
                                    Session["ShareToPages"] = "Deactive";
                                else if (cbxShareToPages.Checked)
                                {
                                    Dictionary<string, string> pageList = getPageIDs();

                                    foreach (var pageId in pageList)
                                        Session["ShareToPages"] = postToFacebook(messageToPost, stream_url, pageId.Value, pageId.Key);
                                }

                                if (cbxShareToMyWall.Checked == false)
                                    Session["FacebookOwnPage.Response"] = "Deactive";
                                else if (cbxShareToMyWall.Checked)
                                    Session["FacebookOwnPage.Response"] = postToFacebook(messageToPost, stream_url, Convert.ToString(Session["FBOwnACC"]), Convert.ToString(Session["FBOwnID"]));

                                if (cbxShareToOwnFriends.Checked == false)
                                    Session["ShareToOwnFriends"] = "Deactive";
                                else if (cbxShareToOwnFriends.Checked)
                                {
                                    IList<string> friendList = getOwnFriendIDs();

                                    foreach (var friendId in friendList)
                                        Session["ShareToOwnFriends"] = postToFacebook(messageToPost, stream_url, Convert.ToString(Session["FBOwnACC"]), friendId);
                                }

                                if (cbxShareToOwnPages.Checked == false)
                                    Session["ShareToOwnPages"] = "Deactive";
                                else if (cbxShareToOwnPages.Checked)
                                {
                                    Dictionary<string, string> pageList = getOwnPageIDs();

                                    foreach (var pageId in pageList)
                                        Session["ShareToOwnPages"] = postToFacebook(messageToPost, stream_url, pageId.Value, pageId.Key);
                                }
                            }

                            #endregion

                            #region Twitter

                            if (cbxSendToTwitter.Checked == false)
                                Session["Twitter.Response"] = "Deactive";
                            else if (cbxSendToTwitter.Checked)
                                Session["Twitter.Response"] = tweetToTwitter(shortUrl, stream_url);

                            #endregion
                        }
                    }
                }
                else
                    Session["TrackProtected"] = false;

                string continueUrl = "~/Member/UploadConfirmation.aspx?id=" + Convert.ToInt64(Session["managed.userid"]);

                if (HttpContext.Current.Session["ReturnUrl"] != null)
                    continueUrl = HttpContext.Current.Session["ReturnUrl"] as string;

                Response.Redirect(continueUrl, false);
            }

            if (cbxSendToFacebook.Checked)
                divFBSharing.Attributes.Add("style", "display:block");
            else
                divFBSharing.Attributes.Add("style", "display:none");
        }
 private string CreateCertificate(long userid, string doc1, string doc2, string doc3, string isrcCode, string trackName, CoopArtistList coopArtists)
 {
     ClientInfo ci = null;
     using (Database db = new MySqlDatabase())
     {
         ci = db.GetClientInfo(Util.UserId);
     }
     string ret = string.Empty;
     using (CertificateManager mgr = new CertificateManager(userid, string.Empty))
     {
         mgr.AddTrackName(trackName);
         if (!string.IsNullOrEmpty(doc1))
             mgr.AddDocument(doc1);
         if (!string.IsNullOrEmpty(doc2))
             mgr.AddDocument(doc2);
         if (!string.IsNullOrEmpty(doc3))
             mgr.AddDocument(doc3);
         if (!string.IsNullOrEmpty(isrcCode))
             mgr.AddIsrcCode(isrcCode);
         foreach (CoopArtist coop in coopArtists)
             mgr.AddCoopArtist(coop.Artist, coop.Role);
         if (ci != null)
             mgr.Agent = ci.GetFullName();
         mgr.CreateCertificate(string.Format("{0}.cer", DateTime.UtcNow.ToString("yyyyMMddHHmmss")));
         ret = mgr.CertificateFilename;
     }
     return ret;
 }
 private string CreateCertificate(long userid, string password, string doc1, string doc2, string doc3, string isrcCode, string trackName, CoopArtistList coopArtists)
 {
     string ret = string.Empty;
     using (CertificateManager mgr = new CertificateManager(userid, password))
     {
         mgr.AddTrackName(trackName);
         if (!string.IsNullOrEmpty(doc1))
             mgr.AddDocument(doc1);
         if (!string.IsNullOrEmpty(doc2))
             mgr.AddDocument(doc2);
         if (!string.IsNullOrEmpty(doc3))
             mgr.AddDocument(doc3);
         if (!string.IsNullOrEmpty(isrcCode))
             mgr.AddIsrcCode(isrcCode);
         foreach (CoopArtist coop in coopArtists)
             mgr.AddCoopArtist(coop.Artist, coop.Role);
         mgr.CreateCertificate(string.Format("{0}.cer", DateTime.UtcNow.ToString("yyyyMMddHHmmss")));
         ret = mgr.CertificateFilename;
     }
     return ret;
 }