using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Net;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using System.Text;
using System.Drawing;
using System.Xml;
using System.Text.RegularExpressions;

public partial class etwuser_update : System.Web.UI.Page
{
    public string usernameid = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie ChatCookie = Request.Cookies["cookChatuser"];
        HttpCookie GetCookie = Request.Cookies["cookEtwuser"];
        if (Request.Cookies["cookChatuser"] == null)
        {
            if (Request.Cookies["cookEtwuser"] == null)
            {
                Response.Redirect("../index.aspx");
            }
            else
            {
                DataSet ds = DbHelperSQL.Query("select userid from etw_user where username='" + GetCookie["username"] + "'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    usernameid = ds.Tables[0].Rows[0]["userid"].ToString();
                }
            }
        }
        else
        {
            DataSet ds1 = DbHelperSQL.Query("select id,userid from chat_user where username='" + ChatCookie["username"] + "'");
            if (ds1.Tables[0].Rows.Count > 0)
            {
                usernameid = ds1.Tables[0].Rows[0]["userid"].ToString();
            }
        }
        etw_data();
        this.Response.Redirect("index.aspx");
    }
    public void etw_data()
    {
        etw.etw_onlines lead = new etw.etw_onlines();
        XmlDocument xmldoc = new XmlDocument();
        string strUrl = "http://center.etwservice.com:8080/reset_order_xml_etw/" + usernameid + ".xml";
        if (GetServerFileExists(strUrl))
        {
            xmldoc.Load("http://center.etwservice.com:8080/reset_order_xml_etw/" + usernameid + ".xml");
            XmlNodeList topM = xmldoc.SelectNodes("//channel");
            foreach (XmlElement element in topM)
            {
                if (element.Name.ToLower() == "channel")
                {
                    XmlNodeList nodelist = element.ChildNodes;
                    for (int i = 0; i < nodelist.Count; i++)
                    {
                        string yid = element.GetElementsByTagName("id")[i].InnerText;
                        DataSet dss = DbHelperSQL.Query("select yid from etw_onlines where yid=" + yid + "");
                        if (dss.Tables[0].Rows.Count == 0)
                        {
                            lead.yid = Convert.ToInt32(element.GetElementsByTagName("id")[i].InnerText);
                            lead.title = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("title")[i].InnerText));
                            lead.contacter = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("contacter")[i].InnerText));
                            lead.telephone = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("telephone")[i].InnerText));
                            lead.fax = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("fax")[i].InnerText));
                            lead.email = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("email")[i].InnerText));
                            lead.address = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("address")[i].InnerText));
                            lead.content = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("content")[i].InnerText));
                            lead.senddate = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("senddate")[i].InnerText));
                            lead.url = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("url")[i].InnerText));
                            lead.usernameid = Convert.ToInt32(element.GetElementsByTagName("usernameid")[i].InnerText);
                            lead.ip = element.GetElementsByTagName("ip")[i].InnerText;
                            lead.language = Fun.Encode(Server.UrlDecode(element.GetElementsByTagName("languages")[i].InnerText));
                            lead.Add();
                        }
                    }
                }
            }
        }
    }
    private bool GetServerFileExists(string URL)
    {
        try
        {
            System.Net.WebRequest s = System.Net.WebRequest.Create(URL);
            s.Timeout = 16000;
            System.Net.WebResponse a = s.GetResponse();
            a.Close();
            s.Abort();
            return true;
        }
        catch
        {
            return false;
        }
    }
}
