﻿
/* 将产品添加到用户收藏夹中 */
function AddFavorites(productId,productName)
{
    var member = MemberFavorites.GetCurrentUserId().value;
    if(member>0)
    {
        if(MemberFavorites.IsExist(member,productId).value)
        {
            alert('您已收藏了商品['+productName+']！');
            return;
        }
        if(MemberFavorites.Add(member,productId).value)
            alert('商品 '+productName+' 已成功添加到您的收藏夹中！');
        else
            alert('收藏商品 '+productName+' 失败，请稍候再试！');
    }
    else
    {
        alert('请您先登录本网站，再收藏您喜欢的商品！');
    }
}

/* 将产品添加到购物车中 用积分购买 */
function AddCart0(productId,qty)
{
    if(!ShoppingCart.IsLogined().value)
    {
        alert('您尚未登录，请登录后再使用积分兑换功能！');
    }
    else
    {
        qty = parseInt(qty,10);
        if(!isNaN(qty))
            ShoppingCart.Add(2,productId,qty);
        else
            ShoppingCart.Add(2,productId,1);
        //跳转到购物车
        window.open('/Sale/ShoppingCart.aspx');
        //window.location.href = "/Sale/ShoppingCart.aspx";
    }
}

/* 将产品添加到购物车中 */
function AddCart(productId,qty)
{
    qty = parseInt(qty,10);
    if(!isNaN(qty))
        ShoppingCart.Add(1,productId,qty);
    else
        ShoppingCart.Add(1,productId,1);
    //跳转到购物车
    window.open('/Sale/ShoppingCart.aspx');
    //window.location.href = "/Sale/ShoppingCart.aspx";
}

/* 将产品添加到购物车中 */
function ToPay(payMode,orderId)
{
    ShoppingCart.ToPay(payMode,orderId);
}

/* 搜索产品 */
function Search(event,url)
{
    var e;
    e = Edward.Web.FixEvent(event);
    if(e.keyCode==0 || e.keyCode==13)
    {
        var key = document.getElementById('txtSearchKey').value.Trim();
        if(key==""||key=="请输入关键词！如：贝亲 奶瓶")
        {
            alert('请输入搜索内容！');
            document.getElementById('txtSearchKey').focus();
            return false;
        }
        else
        {
            window.location = url.replace("{0}",encodeURI(key));
        }
    }
}
function Search1(url)
{
    var key = document.getElementById('txtSearchKey').value.Trim();
    if(key==""||key=="请输入关键词！如：贝亲 奶瓶")
    {
        alert('请输入搜索内容！');
        document.getElementById('txtSearchKey').focus();
        return false;
    }
    else
    {
        window.location = url.replace("{0}",encodeURI(key));
    }
}

/* 修改parentid容器内的所有checkbox的checked状态 */
function allChecked(parentid,checked)
{
    var el = document.getElementById(parentid).getElementsByTagName('input');
    var len = el.length;
    for(var i=0; i<len; i++)
    {
        if((el[i].type=="checkbox"))
        {
            el[i].checked = checked;
        }
    }     
}

/* 在线客服 */
function showService()
{
    if(document.getElementById("divOnlineService").style.display == "none")
        document.getElementById("divOnlineService").style.display = "block";
}
function hidService()
{
    var obj = event.toElement;
    while( obj!=null && obj.id!="divOnlineService" )
    {
        obj = obj.parentElement;
    }
    if( obj==null )
    {
        document.getElementById("divOnlineService").style.display = 'none'; 
    }
}
/* 在线客服 */

/* 显示用户信息 */
function showuserinfo()
{
    var html = "";
    var qty = ShoppingCart.GetProductCount().value;
    if(ShoppingCart.IsLogined().value==true)
    {
        var user = ShoppingCart.LoginedUser().value;
        html='您好，<strong><a href="'+ShoppingCart.MemberCenterUrl().value+'" class="Color_56A41E">'+user+'</a></strong>！';
        html+='[<a href="'+ShoppingCart.LogoutUrl().value+'">退出</a>]';
        //html+=' | <a href="/Sale/ShoppingCart.aspx">购物车 <strong class="Color_2265B9">'+qty+'</strong> 件商品</a>';
    }
    else
    {
        html='<strong><a href="'+ShoppingCart.LoginUrl().value+'" class="Color_FF6600">会员登录</a></strong>';
        html+=' <strong><a href="'+ShoppingCart.RegsiterUrl().value+'" class="Color_084093">注册</a></strong>';
        //html+=' | <a href="/Sale/ShoppingCart.aspx">购物车 <strong class="Color_2265B9">'+qty+'</strong> 件商品</a>';
    }
    $('spanLogin').innerHTML = html;
    $('spanCartQty').innerHTML = qty;
}
/* 显示用户信息 */


// 转换为数字 
function intval(v) 
{ 
    v = parseInt(v); 
    return isNaN(v) ? 0 : v; 
} 

function getPos(e) 
{ 
    var l = 0; 
    var t  = 0; 
    var w = intval(e.style.width); 
    var h = intval(e.style.height); 
    var wb = e.offsetWidth; 
    var hb = e.offsetHeight; 
    while (e.offsetParent){ 
        l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0); 
        t += e.offsetTop  + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0); 
        e = e.offsetParent; 
    } 
    l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0); 
    t  += e.offsetTop  + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0); 
    return {x:l, y:t, w:w, h:h, wb:wb, hb:hb}; 
} 

function getScroll()  
{ 
    var t, l, w, h; 
     
    if (document.documentElement && document.documentElement.scrollTop) { 
        t = document.documentElement.scrollTop; 
        l = document.documentElement.scrollLeft; 
        w = document.documentElement.scrollWidth; 
        h = document.documentElement.scrollHeight; 
    } else if (document.body) { 
        t = document.body.scrollTop; 
        l = document.body.scrollLeft; 
        w = document.body.scrollWidth; 
        h = document.body.scrollHeight; 
    } 
    return { t: t, l: l, w: w, h: h }; 
} 

function scroller(el, duration) 
{ 
    if(typeof el != 'object') { el = document.getElementById(el); } 

    if(!el) return; 

    var z = this; 
    z.el = el; 
    z.p = getPos(el); 
    z.s = getScroll(); 
    z.clear = function(){window.clearInterval(z.timer);z.timer=null}; 
    z.t=(new Date).getTime(); 

    z.step = function(){ 
        var t = (new Date).getTime(); 
        var p = (t - z.t) / duration; 
        if (t >= duration + z.t) { 
            z.clear(); 
            window.setTimeout(function(){z.scroll(z.p.y, z.p.x)},13); 
        } else { 
            st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t; 
            sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l; 
            z.scroll(st, sl); 
        } 
    }; 
    z.scroll = function (t, l){window.scrollTo(l, t)}; 
    z.timer = window.setInterval(function(){z.step();},13); 
} 

function txtSearchKey_focus(obj)
{
    if(obj.value == "请输入关键词！如：贝亲 奶瓶")
    {
        obj.value = "";
        obj.className="textbox";
    }
}

function txtSearchKey_blur(obj)
{
    if(obj.value == "")
    {
        obj.value = "请输入关键词！如：贝亲 奶瓶";
        obj.className="textbox searchBox";
    }
}

function autoplay(divs,a,mtime)
{ 
    //t为要应用此函数的id，a初始化时显示的项目的顺序数 
    this.start = a-1;//初始项目 
    this.speed = mtime;//切换间隔 
    this.setid = null;//定时器 
    var loop = function()
    { //自动切换 
        this.obj="";
        ids = divs.split(",");
        for (var i=0;i<ids.length; i++)
        {
            document.getElementById(ids[i]).style.display = "none";
        }
        document.getElementById(ids[start]).style.display = "block";
        setid=setTimeout(loop,speed); 
        start++; 
        if(start==ids.length){ 
            start=0; 
            //clearTimeout(setid);//只循环一次 
        } 
    };
    loop(); 
} 


