﻿// 用户信息
function WebUser()
{
	this.getCookiesForUser = function (name)
	{
	    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	    if(arr != null) 
	        return arr[2];
	    return null;
	}
	this.getArray = function (key,cookiedata)
	{
		var user = cookiedata;
		if(user==null)	return "";
		var list = user.split("&");
		var str ="";
		var n = -1;
		if(list.length>0)
		{
			for(var i =0;i<list.length;i++)
			{
				n = list[i].indexOf(key);
				if(n>-1)
				{
					str = list[i].substring(key.length +1 );
					str = decodeURI(str);
					break;
				}
			}
		}
		return str;
	}
	this.getArrayToInt32 = function (key,cookiedata)
	{
		var user = cookiedata;
		if(user==null)
			return 0;
		var list = user.split("&");
		var str ="0";
		var n = -1;
		if(list.length>0)
		{
			for(var i =0;i<list.length;i++)
			{
				n = list[i].indexOf(key);
				if(n>-1)
				{
					str =  list[i].substring(key.length +1 );
					break;
				}
			}
		}
		return parseInt(str,10);
	}	
//---------	
	this.CookieData = this.getCookiesForUser("Hnehome_Net_User");//Hnehome_Net_User //User
	this.UserID =  this.getArrayToInt32("ID",this.CookieData);	// UserID
	this.UserName = this.getArray("UserName",this.CookieData);	// UserName
	this.RegTime = this.getArray("RegTime",this.CookieData);	// RegTime
	this.Account = this.getArray("PetName",this.CookieData);	// PetName
	this.LoginIP = this.getArray("LoginIP",this.CookieData);	// LoginIP
	this.CityNo = this.getArray("CityNo",this.CookieData);	    // CityNo		
	this.CityName = this.getArray("CityName",this.CookieData);	// CityName				
	this.ShopID =  this.getArrayToInt32("ShopID",this.CookieData);	// UserID
		
	this.Show=function(){
	    var x =this.UserName
	    if(x)
	        return x;
	    else
	        return "不存在了" + x;    
	    }
}
//显示
function GetUserLoginInfo(id)
{
    var user = new WebUser();
    // 0:未开通,1:开通;-1:关闭     
    var dpStatus = parseInt( CheckDpStatus(),10);
    var _dp_Text = "开通";
    
    switch(dpStatus)
    {
    	case -1:_dp_Text="我的";break;
    	case 0:_dp_Text="开通";break;
    	case 1:_dp_Text="我的";break;
    	default : break;
    }
	var dpUrl = '<A style="cursor:pointer" onclick="CheckLogin(\'http://www.hnehome.net/Shop.aspx'+ ((user.ShopID>0)?("?ShopID="+user.ShopID):"")  +'\')" target=\"_blank\">'+ _dp_Text +'店铺</A>';
    if(user.UserID>0)//会员已登陆
    {
        $("#tdbtn").html("");
     var str = '欢迎您： <a href = "http://www.hnehome.net/MemberIndex.aspx" target="_blank">' + user.UserName + '</a>'
        	+'&nbsp;'
        	+ dpUrl	
        	+'&nbsp;'
        	+'<a href="http://www.hnshq.net/home/space.php" target="_blank">我的空间</a>'
        	+'&nbsp;'
            +'<a href="http://www.hnshq.net/memcp.php" target="_blank">个人中心</a>'
            +'&nbsp;'            
            + '<a style="color:#4169E1" href="/DeleteCookies.aspx?t="'+Math.random()+'>退出</a>';
            document.getElementById(id).innerHTML = str;
    }
        
}
//登陆/注册前的来源网址处理
function GetReferrer(toUrl)
{
    var url = document.location.href; 
    var d = new Date();
    $.cookie("RegLoginReferrer",url);//设置有效期
    location.href = toUrl;
}
function CheckDpStatus()
{
	var CountReForError = 10;
	var i=0;
    var user = new WebUser();
    if(user.UserID<1) return 0;
    var dpStatus = $.cookie("Cookie_dpStatus_" + user.UserID );
    if(($.trim(dpStatus).length<1))
    {
        var x = $.ajax({
            type: "POST",
            url: "http://www.hnehome.net/Ajax/GetDpOpenStatus.aspx",
            data: "", 
            success: function(return_data,textStatus)
            {                	
                dpStatus = return_data;
            }
        });
     }
     return dpStatus;
}
function CheckLogin(url,IsShowMsg)
{
	if(IsShowMsg==null) IsShowMsg = true;
    var u = new WebUser();
    if(u.UserID>0)
    {
        //如果是店铺,则需进行进一步判断.
        var x = url.toLowerCase ();
        var x1 = location.href.toLowerCase ();
        
        var y1 =(x.indexOf("shop")>-1 || x.indexOf("dphdgl")>-1 );//店铺活动不处理
        var y =(x1.indexOf("shop")>-1 || x1.indexOf("dpxxgl")>-1 || x1.indexOf("dphdgl")>-1 );

        if(y1)//验证所有与店铺打开有关的页面
        {
            //查看店铺是否开通.
            var dpStatus = $.cookie("Cookie_dpStatus_" + u.UserID );
            
            //如果Cookie不存在，则读取并写入Cookie。
            if(dpStatus.length<1)
            {

                var x = $.ajax({
                    type: "POST",
                    url: "http://www.hnehome.net/Ajax/GetDpOpenStatus.aspx",
                    data: "",                     
                    success: function(return_data,textStatus)
                    {
                        dpStatus = return_data;
                        window.status = "0:" + dpStatus;
                        
                        CheckAndAlert(dpStatus,url);
                        
                    }
                });
                
            }
            else
            {
                CheckAndAlert(dpStatus,url);
            }
        }
        else
        {        
            document.location.href = url;
        }
    }
    else
    {
    	GetReferrer('http://www.hnehome.net/MemberReg.aspx');
    	document.location.href = "http://www.hnehome.net/MemberReg.aspx";
    }
}
function CheckAndAlert(dpStatus,url,y)
{
    if(!y)
    {
        var x1 = location.href.toLowerCase ();
        y =(x1.indexOf("shop")>-1 || x1.indexOf("dpxxgl")>-1 || x1.indexOf("dphdgl")>-1 );
    }
    if(dpStatus.length<1)
    {
        if(confirm("你可能尚未开通店铺，是否马上开通?"))
        {
            document.location.href = "http://www.hnehome.net/dpxxgl.aspx";
        }
        else
        {
            if(y)
                document.location.href = "http://www.hnehome.net/index.shtml";
            return ;
        }
    }
    //返回值：0-未开通；1-开通；-1-开通但因种种原因给关闭了。
    else if(parseInt(dpStatus,10)==0)
    {
            document.location.href ="http://www.hnehome.net/dpxxgl.aspx";
    }
    else if(parseInt(dpStatus,10)==1) //已开通，则允许进入。
    {
            document.location.href = url; //"shop.aspx";
    }
    else if(parseInt(dpStatus,10)==-1)
    {      
        if(confirm("您的店铺已被关闭,请重新提交信息申请开通。"))
        {
            document.location.href = "http://www.hnehome.net/dpxxgl.aspx";;
        }
        else
        {
            if(y) document.location.href = "http://www.hnehome.net/index.shtml";
            return ;
        }
        return ;
    }
}
//广告统计
function CP(No,Pos)
{
  $.ajax({
    type: "POST",
    url: "http://www.hnehome.net/Ajax/CP.aspx?AdNo="+No+"&Pos="+Pos,
    data: "",                     
    success: function(return_data,textStatus)
    {
    }
});
}
