/// ----------------------------------------------------------------------------- 
 /// <summary> 
 /// adds an object to the database 
 /// </summary> 
 /// <remarks> 
 /// </remarks> 
 /// <param name="objtopcmm_123flashchat">The topcmm_123flashchatInfo object</param> 
 /// <history> 
 /// </history> 
 /// ----------------------------------------------------------------------------- 
 public void Addtopcmm_123flashchat(topcmm_123flashchatInfo objtopcmm_123flashchat)
 {
     if (objtopcmm_123flashchat.Content.Trim() != "")
     {
         DataProvider.Instance().Addtopcmm_123flashchat(objtopcmm_123flashchat.ModuleId, objtopcmm_123flashchat.Content, objtopcmm_123flashchat.CreatedByUser);
     }
 }
        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// Page_Load runs when the control is loaded 
        /// </summary> 
        /// ----------------------------------------------------------------------------- 
        protected void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                errorMessage = "";

                topcmm_123flashchatController objtopcmm_123flashchats = new topcmm_123flashchatController();// One23FlashChatController();
                List<topcmm_123flashchatInfo> coltopcmm_123flashchats;

                // get the content from the One23FlashChat table
                coltopcmm_123flashchats = objtopcmm_123flashchats.Gettopcmm_123flashchats(ModuleId);

                if (coltopcmm_123flashchats.Count == 0)
                {
                    // add the content to the One23FlashChat table
                    topcmm_123flashchatInfo objtopcmm_123flashchat = new topcmm_123flashchatInfo();// One23FlashChatInfo();
                    objtopcmm_123flashchat.ModuleId = ModuleId;
                    objtopcmm_123flashchat.Content = System.Web.HttpUtility.HtmlDecode(Localization.GetString("DefaultContent", LocalResourceFile));
                    objtopcmm_123flashchat.CreatedByUser = this.UserId;
                    objtopcmm_123flashchats.Addtopcmm_123flashchat(objtopcmm_123flashchat);
                    // get the content from the One23FlashChat table
                    coltopcmm_123flashchats = objtopcmm_123flashchats.Gettopcmm_123flashchats(ModuleId);
                }
                foreach (topcmm_123flashchatInfo a in coltopcmm_123flashchats)
                {
                    //if(a.Content.Contains ("<")&&a.Content.Contains ("</"
                    string strContent = System.Web.HttpUtility.HtmlDecode(a.Content);
                    if (System.Text.RegularExpressions.Regex.IsMatch(strContent, @"<.*>.*</.*>"))
                    {
                        a.Content = strContent;
                    }

                    else
                        a.Content = "<room type=\"1\" value=\"1\">default room</room>";

                }

                id = coltopcmm_123flashchats[0].ItemId.ToString ();
                HyperLink1.NavigateUrl = EditUrl("ItemID", id);

                XmlDocument xmlD = new XmlDocument();
                xmlD.LoadXml(coltopcmm_123flashchats[0].Content);
                XmlElement root = xmlD.DocumentElement;

                typeID = root.Attributes["type"].Value;
                if (typeID == "1")
                {
                    string[] strsInfor = RoomsAndChaters.GetInformation(RoomsAndChaters.GetServerDataPath());
                    if (strsInfor.Length == 1||strsInfor.Length ==0)
                    {
                        outPutStr = "Error to read information of 123flashchat.Please check up configuration in the Edit Page";
                        hlkEach.Visible = false;
                    }
                    else
                    {

                        roomCount = strsInfor[2];
                        connections = strsInfor[0];
                        loginUser = strsInfor[1];
                        userList = RoomsAndChaters.GetChatterList(RoomsAndChaters.GetServerDataPath());

                        outPutStr = "There are <b>" + roomCount + "</b> rooms <br/>" +
                                           "There are <b>" + connections + "</b> connections <br/>" +
                                           "There are <b>" + loginUser + "</b> logon users<br/>" +
                                           "User List:" + userList;
                        hlkEach.NavigateUrl = ResolveUrl("123FlashChatPanel.aspx");
                    }
                }
                else if (typeID == "2")//free
                {
                    outPutStr = root.Attributes["value"].Value.Substring (root.Attributes["value"].Value.LastIndexOf ("/")+1);
                    hlkEach.NavigateUrl = ResolveUrl("123FlashChatHostAndFree.aspx") + "?initurl=" + root.Attributes["value"].Value + "&type=2";
                }
                else if (typeID == "3")//host
                {
                    outPutStr = root.Attributes["value"].Value;
                    hlkEach.NavigateUrl = ResolveUrl("123FlashChatPanel.aspx")  + "?type=3";
                }
                HyperLink1.Visible = IsEditable;
                Image1.Visible = IsEditable;

            }

            catch (System.Net.WebException eWeb)
            {
                Exceptions.ProcessModuleLoadException("friendly",this,eWeb  ,false );
                errorMessage = "Can't connect to 123flashchat server.Please try again later";

            }
            catch (Exception exc)
            {
                //Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// cmdUpdate_Click runs when the update button is clicked 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            try
            {

                topcmm_123flashchatController objtopcmm_123flashchats = new topcmm_123flashchatController();

                topcmm_123flashchatInfo objtopcmm_123flashchat = new topcmm_123flashchatInfo();

                XmlDocument xmlD = new XmlDocument();
                xmlD.LoadXml("<room></room>");

                XmlElement root = xmlD.DocumentElement;

                if (ddlType.SelectedValue == "1")
                {

                    XmlAttribute xatNodeValue = xmlD.CreateAttribute("value");

                    XmlAttribute xatNodeType = xmlD.CreateAttribute("type");
                    xatNodeType.Value = ddlType.SelectedValue;

                    root.Attributes.Append(xatNodeValue);
                    root.Attributes.Append(xatNodeType);

                }
                else if (ddlType.SelectedValue == "2") //free
                {
                    root.InnerText = tbxFreeName.Text.Trim();
                    XmlAttribute xatNodeValue = xmlD.CreateAttribute("value");
                    xatNodeValue.Value = "http://free.123flashchat.com/room/" + tbxFreeName.Text.Trim();//tbxFree.Text.Trim();

                    XmlAttribute xatNodeType = xmlD.CreateAttribute("type");
                    xatNodeType.Value = ddlType.SelectedValue;
                    root.Attributes.Append(xatNodeValue);
                    root.Attributes.Append(xatNodeType);

                }
                else if (ddlType.SelectedValue == "3") //host
                {
                    root.InnerText = tbxHostName.Text.Trim();
                    XmlAttribute xatNodeValue = xmlD.CreateAttribute("value");
                    string valueUrl = tbxHost.Text.Trim();
                    if (valueUrl[valueUrl.Length - 1] == '/')
                    {
                        valueUrl = valueUrl.Substring(0, valueUrl.Length - 1);
                    }

                    xatNodeValue.Value = valueUrl;

                    WebClient wc = new WebClient();
                    string result = wc.DownloadString(valueUrl);

                    Match matchEmbed = Regex.Match(result, "(?<=<EMBED\\s+src=\").*?(?=\")", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    Match matchParam = Regex.Match(result, "(?<=<PARAM.*VALUE=\")http.*(?=\")", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    Match matchvar = Regex.Match(result, "(?<=var.*=\")http.*(?=\")", RegexOptions.IgnoreCase | RegexOptions.Multiline);

                    if (matchParam.Captures.Count > 0)
                    {
                        string urlParam = matchParam.Value;

                        RoomsAndChaters.setChatUrl(urlParam);
                    }
                    else if (matchEmbed.Captures.Count > 0)
                    {
                        string urlEmbed = matchEmbed.Value;

                        RoomsAndChaters.setChatUrl(urlEmbed);
                    }
                    else if (matchvar.Captures.Count > 0)
                    {
                        string urlVar = matchvar.Value;
                        RoomsAndChaters.setChatUrl(urlVar);
                    }

                    XmlAttribute xatNodeType = xmlD.CreateAttribute("type");
                    xatNodeType.Value = ddlType.SelectedValue;
                    root.Attributes.Append(xatNodeValue);
                    root.Attributes.Append(xatNodeType);

                }

                objtopcmm_123flashchat.ModuleId = ModuleId;
                objtopcmm_123flashchat.ItemId = ItemId;
                objtopcmm_123flashchat.Content = xmlD.InnerXml;

                objtopcmm_123flashchat.CreatedByUser = this.UserId;

                if (Null.IsNull(ItemId))
                {
                    // add the content within the topcmm_123flashchat table
                    objtopcmm_123flashchats.Addtopcmm_123flashchat(objtopcmm_123flashchat);
                }
                else
                {
                    // update the content within the topcmm_123flashchat table
                    objtopcmm_123flashchats.Updatetopcmm_123flashchat(objtopcmm_123flashchat);
                }

                Response.Redirect(Globals.NavigateURL(), true);

            }
            catch (WebException we)
            {
                labelHost.Text = "Sorry,can't connect to this address";
            }
            catch (Exception exc)
            {
                //Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }
 /// ----------------------------------------------------------------------------- 
 /// <summary> 
 /// ImportModule implements the IPortable ImportModule Interface 
 /// </summary> 
 /// <remarks> 
 /// </remarks> 
 /// <param name="ModuleID">The Id of the module to be imported</param> 
 /// <param name="Content">The content to be imported</param> 
 /// <param name="Version">The version of the module to be imported</param> 
 /// <param name="UserId">The Id of the user performing the import</param> 
 /// <history> 
 /// </history> 
 /// ----------------------------------------------------------------------------- 
 public void ImportModule(int ModuleID, string Content, string Version, int UserId)
 {
     XmlNode xmltopcmm_123flashchats = Globals.GetContent(Content, "topcmm_123flashchats");
     foreach (XmlNode xmltopcmm_123flashchat in xmltopcmm_123flashchats.SelectNodes("topcmm_123flashchat"))
     {
         topcmm_123flashchatInfo objtopcmm_123flashchat = new topcmm_123flashchatInfo();
         objtopcmm_123flashchat.ModuleId = ModuleID;
         objtopcmm_123flashchat.Content = xmltopcmm_123flashchat.SelectSingleNode("content").InnerText;
         objtopcmm_123flashchat.CreatedByUser = UserId;
         Addtopcmm_123flashchat(objtopcmm_123flashchat);
     }
 }