using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;
using System.IO;
using xabaraCOM;

public partial class inClude_area : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ipAddress ip = new ipAddress();
            sqlServer sql = new sqlServer();

            DataTable dt;
            long p = 0;   //省份
            long c = 0;   //市级
            long a = 0;   //县区级
            string pathFlie = HttpContext.Current.Server.MapPath("/xabara.config");
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(pathFlie);
            p = xabara.isNum(xmlDoc.DocumentElement.SelectSingleNode("//add[@key='province']").Attributes["value"].Value);
            c = xabara.isNum(xmlDoc.DocumentElement.SelectSingleNode("//add[@key='city']").Attributes["value"].Value);
            a = xabara.isNum(xmlDoc.DocumentElement.SelectSingleNode("//add[@key='area']").Attributes["value"].Value);
            xmlDoc = null;
            string city = "全国";

            if (Request.Cookies["xabaraWhere"] != null)  //cookies检查
            {
                city = HttpUtility.UrlDecode(Request.Cookies["xabaraWhere"]["city"].ToString().Trim());
                Response.Write("document.write(\"" + city + "\");");                
            }
            else
            {
                if (p < 1)
                {
                    city = ip.ipCity();

                    if (!string.IsNullOrEmpty(city as string))
                    {
                        dt = sql.getDataTable("select * from selectArea where areaTxt='" + city + "'");
                        if (dt.Rows.Count > 0)
                        {
                            a = xabara.isNum(dt.Rows[0]["areaID"].ToString().Trim());
                            c = xabara.isNum(dt.Rows[0]["aFather"].ToString().Trim());

                            dt = sql.getDataTable("select * from selectCity where cityID=" + c.ToString());
                            if (dt.Rows.Count > 0)
                            {
                                p = xabara.isNum(dt.Rows[0]["cFather"].ToString().Trim());
                            }
                        }
                        else
                        {
                            dt = sql.getDataTable("select * from selectCity where cityTxt='" + city + "'");
                            if (dt.Rows.Count > 0)
                            {
                                c = xabara.isNum(dt.Rows[0]["cityID"].ToString().Trim());
                                p = xabara.isNum(dt.Rows[0]["cFather"].ToString().Trim());
                            }
                        }
                        dt.Dispose();
                    }
                }
                else
                {
                    if (c > 0)
                    {
                        dt = sql.getDataTable("select * from selectCity where cityID=" + c.ToString());
                        if (dt.Rows.Count > 0)
                        {
                            city = dt.Rows[0]["cityTxt"].ToString().Trim();
                        }
                        dt.Dispose();
                    }
                    else
                    {
                        dt = sql.getDataTable("select * from selectProvince where provinceID=" + p.ToString());
                        if (dt.Rows.Count > 0)
                        {
                            city = dt.Rows[0]["provinceTxt"].ToString().Trim();
                        }
                        dt.Dispose();
                    }
                }

                Response.Write("document.write(\"" + city + "\");");

                HttpCookie ipCookies = new HttpCookie("XabaraWhere");  //cookies写入磁盘
                ipCookies.Values.Add("p", p.ToString());
                ipCookies.Values.Add("c", c.ToString());
                ipCookies.Values.Add("a", a.ToString());
                ipCookies.Values.Add("city", HttpUtility.UrlEncode(city));
                ipCookies.Expires = DateTime.Now.AddDays(1);
                ipCookies.Domain = xml.read("xabara.config", "cookiesDomain", 180);
                ipCookies.Path = "/";
                Response.AppendCookie(ipCookies);
            }
        }
    }    
}