// JScript 文件
//定义变量
//用户帐户WebSite

//var ajaxUrl = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'mywww.vjia.com/';
var ajaxUrl = document.location.protocol + "//www.vjia.com/";
if (typeof WebSite == "undefined") {
    if (ajaxUrl.indexOf("https") > -1) {
        WebSite = ajaxUrl.replace("https", "http");
        WebSiteHttps = ajaxUrl;
    } else {
        WebSite = ajaxUrl;
        WebSiteHttps = ajaxUrl.replace("http", "https");
    }
}
$(document).ready(function () {
    //设置用户信息
    setShoppingCar();
});
function setLoginInfo() {
    if (hasLogin()) {
        setWelcome();
    }
}
//判断用户是否登录
function hasLogin() {
    return getCookie("UserLogin") != "";
}
//异步获取用户登录名称.
function setWelcome() {
    var webUrl = ajaxUrl + "Usercenter/GetUserName.ashx?rand=" + Math.random();
    $.getScript(webUrl, function () {
        if (typeof (data) != undefined && data != "") {
            $("#login").html("<a class='top'  href='" + WebSiteHttps + "/login/UserLoginOut.aspx' target='_parent' >退出登录</a>");
            if (isLogin) {
                $("#welcome").html("您好，<a class='top'  href='" + WebSiteHttps + "/UserCenter/OrderList.aspx' >" + (data.length > 10 ? data.substr(0, 10) + "..." : data) + "</a>。<a class='top'  href='" + WebSiteHttps + "/Login/UserLoginOut.aspx' target='_parent' >退出登录</a>");
            } else {
                $("#welcome").html("您好，" + (data.length > 10 ? data.substr(0, 10) + "..." : data) + "。<a class='top'  href='" + WebSiteHttps + "Login/Login.aspx' target='_parent' >请登录</a> [ <a class='top'  href='" + WebSiteHttps + "Login/Reg.aspx' target='_parent' >免费注册</a> ]");
            }
        }
        else {
            return;
        }
    });
};

//获取购物车内的物品数
function setShoppingCar() {
    var shoppingInfoCookie = getShoppingCookie("spci", false);
    if (shoppingInfoCookie != null && shoppingInfoCookie != "undefined" && shoppingInfoCookie.length > 0 && shoppingInfoCookie.indexOf("undefined") < 0 && shoppingInfoCookie.indexOf("null") < 0) {
        var shoppingCartInfoArray = shoppingInfoCookie.split('&');
        if (shoppingCartInfoArray.length > 1) {
            var isUpdateShoppingCartInfo = shoppingCartInfoArray[1];
            if (isUpdateShoppingCartInfo == "true") {
                var shoppingCount = shoppingCartInfoArray[0];
                $("#prdCount").html(shoppingCount);
            } else {
                GetShoppingCountAndSetCookie();
            }
        } else {
            GetShoppingCountAndSetCookie();
        }
    } else {
        GetShoppingCountAndSetCookie();
    }
}
//从服务器端获取购物数量并回写COOKIE
function GetShoppingCountAndSetCookie() {
    $.getScript(ajaxUrl + "/Usercenter/GetUserShoppingCarInfo.ashx", function () {
        if (typeof carInfo != 'undefined' && carInfo != "") {
            $("#prdCount").html(carInfo.TotCount);
            var exp = new Date();
            exp.setTime(exp.getTime() + 15 * 24 * 60 * 60 * 1000);
            setShoppingCookie("spci", carInfo.TotCount + "&true", exp, false);
        }
    });
}

function getShoppingCookie(cName, doEsc) {
    if (document.cookie.length > 0) {
        var cStart = document.cookie.indexOf(cName + "=");
        if (cStart != -1) {
            cStart = cStart + cName.length + 1;
            var cEnd = document.cookie.indexOf(";", cStart);
            if (cEnd == -1) cEnd = document.cookie.length;
            var value = document.cookie.substring(cStart, cEnd);
            return doEsc ? unescape(value) : value;
        }
    }
    return "";
}

function setShoppingCookie(cName, value, expires, doEsc) {
    document.cookie = cName + "=" + (doEsc ? escape(value) : value) +
        ((expires == null) ? ";" : ";expires=" + expires + ";") + "path=/;domain=.vjia.com;";
}



