using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

public partial class NewLink : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOK_ServerClick(object sender, EventArgs e)
    {
        string sname = this.sname.Text.Trim();
        string surl = this.surl.Text.Trim();
        string sdesc = this.sdesc.Text.Trim();
        string semail = this.semail.Text.Trim();
        if (string.IsNullOrEmpty(sname))
        {
            this.RegistScript("<script>alert('网站名称不能为空！');</script>"); return;
        }
        if (string.IsNullOrEmpty(surl))
        {
            this.RegistScript("<script>alert('网站URL不能为空！');</script>"); return;
        }
        if (string.IsNullOrEmpty(sdesc))
        {
            this.RegistScript("<script>alert('网站描述不能为空！');</script>"); return;
        }
        sname = sname.Replace("'", "").Replace("\"","").Replace(",","") ;
        surl = surl.Replace("'", "").Replace("\"", "").Replace(",", "");
        sdesc = sdesc.Replace("'", "").Replace("\"", "").Replace(",", "");
        semail = semail.Replace("'", "").Replace("\"", "").Replace(",", "");
        try
        {
            string s = "SELECT COUNT(ID) FROM LINKS WHERE SURL =@url";
            OleDbParameter[] dbParms = new OleDbParameter[] { new OleDbParameter("@url", OleDbType.VarChar, 200)};
            dbParms[0].Value = this.surl.Text.Trim();
            int sc = (int)SqlHelper.GetSingle(s,dbParms);
            if (sc > 0)
                this.RegistScript("<script>alert('您的网站已申请，请勿重复申请，做好本站连接后点入即可显示');</script>");
            else
            {
                surl = surl.Replace("http://", "");
                string sql = string.Format("INSERT INTO LINKS(SNAME,SURL,SDESC,SEMAIL,sip,scount,soutcount,stime) values(@SNAME,@SURL,@SDESC,@SEMAIL,'{0}',{1},{2},'{3}')",  WebClass.GetClientIP(), 0, 0, DateTime.Now);
                OleDbParameter[] dbParms1 = new OleDbParameter[] { 
                    new OleDbParameter("@SNAME", OleDbType.VarChar, 50),
                    new OleDbParameter("@SURL",OleDbType.VarChar,200),
                     new OleDbParameter("@SDESC",OleDbType.VarChar,200),
                     new OleDbParameter("@SEMAIL",OleDbType.VarChar,200)
                };
                dbParms1[0].Value = sname;
                dbParms1[1].Value = surl;
                dbParms1[2].Value = sdesc;
                dbParms1[3].Value = semail;
                int k = (int)SqlHelper.GetSingle(sql,dbParms1);
                if (k > 0)
                {
                    WebClass.ReMoveCacheByKey("AllLinks");
                    this.RegistScript("<script>alert('申请成功，请做好本站连接');location.href='index.aspx'</script>");
                }
                else

                    this.RegistScript("<script>alert('申请失败！请联系管理员');</script>");
            }
        }
        catch (Exception ex)
        {
            this.RegistScript("<script>alert('申请失败！" + ex.Message + "');</script>"); 
        }
    }
    void RegistScript(string str)
    {
        Page.ClientScript.RegisterStartupScript(typeof(string), Guid.NewGuid().ToString(), str);
    }
}
