﻿//购买事件
function BuyProduct()
{
    this.DivCartCountId="sp_cartprocount";//头部购物车商品个数容器
    this.FunReturn=null;//购买完成后要执行的方法
    var buyc=new BuyProductCompletion();
    /*
    添加商品
    id 商品ID
    warrantyid 延保ID(如果没有延保该ID为:0)
    divid 提示容器ID
    funreturn 购买成功后执行的方法
    */
    this.BuyItemsProduct=function(id,warrantyid,obj){
        var a=new Ajax();
        a.PageUrl="/AjaxWeb/BuyProduct.aspx";
        a.Method="post";
        a.PostString="type=0&pid="+id+"&warrantyid="+warrantyid;
        var FunReturn=this.FunReturn;
        buyc.DivCartCountId=this.DivCartCountId;
        buyc.BottomNavShowHtml();
        var BuyBotClick=null;
        a.FunLodings=function(){
            //让购买的按钮不可点
            if(BuyBotClick!=null)return;
            if(obj==null)return;
            BuyBotClick=$(obj).attr("onclick");
            $(obj).removeAttr("onclick");;
            $(obj).css({filter:"alpha(opacity=30)",opacity:"0.3"});
        }    
        a.FunReturn=function(result){
            buyc.showBottomNav(0,id,warrantyid,result);
            buyc.showCartCount(obj,BuyBotClick);
        }
        a.FunLodingsClose=function(){
            
        }
        a.OnReadyStateChange();
    }
    
    this.BuyPackage=function(promainid,warrantyid,iteamids,obj){
        /*
        添加套餐
        promainid 主商品ID
        warrantyid 主商品延保ID 如果主商品无延保该值为 0
        iteamids 附属商品IDS集合
        divid 提示容器ID
        funreturn 添加成功后执行方法
        */
        var a=new Ajax();
        a.PageUrl="/AjaxWeb/BuyProduct.aspx";
        a.Method="post";
        a.PostString="type=4&pid="+promainid+"&iteamids="+iteamids+"&warrantyid="+warrantyid;
        var FunReturn=this.FunReturn;
        var BuyBotClick=null;
        buyc.DivCartCountId=this.DivCartCountId;
        buyc.BottomNavShowHtml();//显示底部导航
        a.FunLodings=function(){
            //让购买的按钮不可点
            if(BuyBotClick!=null)return;
            if(obj==null)return;
            BuyBotClick=$(obj).attr("onclick");
            $(obj).removeAttr("onclick");
            $(obj).css({filter:"alpha(opacity=30)",opacity:"0.3"});
        }
        a.FunReturn=function(result){
            buyc.showBottomNav(1,promainid,warrantyid,result);
            buyc.showCartCount(obj,BuyBotClick);
        }
        a.FunLodingsClose=function(){
            
        }
        a.OnReadyStateChange();
    }
}

//完成后其他动作
//完成购买动作显示底部导航
function BuyProductCompletion()
{
    this.DivCartCountId=null;//头部购物车个数显示容器ID
    this.showBottomNav=function(_type,_id,_warrantyid,_state)//显示底部导航
    {
        var a=new Ajax();
        a.PageUrl="/AjaxWeb/NavBottom.aspx";
        a.Method="post";
        a.PostString="type="+_type+"&id="+_id+"&warrantyid="+_warrantyid+"&state="+_state;
        a.FunLodings=function(){
           
        }
        a.FunReturnXml=function(xmldom)
        {
            var root = xmldom.documentElement; 
            var state = root.getElementsByTagName("state");
            var imgs=root.getElementsByTagName("imgs");
            var proname=root.getElementsByTagName("proname");
            if(state.length>0)
            {
                var mag=imgs[0].firstChild.data+" "+state[0].firstChild.data.replace("{0}","<span style=\"font-size:12px;font-weight:bold;color:#B90005\">"+proname[0].firstChild.data+"</span>");
                mag=mag.replace("{1}",_id);
                mag=mag.replace("[","<").replace("]",">");
                $("#TdBuyProInfo").html(mag);
                $("#DivBottomNav").show();
            }
            else
                $("#TdBuyProInfo").html("ajax 获取xml信息错误!");
        }
        a.FunLodingsClose=function()
        {
            //让购买的按钮可点
        }
        a.OnReadyStateChange();
    }
    this.showCartCount=function(_obj,_buybotclick)//显示购物车商品个数
    {
        if(this.DivCartCountId==null)return;
            DivCartCountId=this.DivCartCountId;
        var a=new Ajax();
        a.PageUrl="/AjaxWeb/ShowCart.aspx";
        a.Method="post";
        a.PostString="show=0";
        a.FunLodings=function()
        {
            $("#"+DivCartCountId).html("-");
            $("#SpanNavBotCartCount").html("-");
        }
        a.FunReturn=function(count)
        {
            $("#"+DivCartCountId).html(count);
            $("#SpanNavBotCartCount").html(count);
        }
        a.FunLodingsClose=function(){
            if(_obj==null)return;
            $(_obj).css({filter:"alpha(opacity=100)",opacity:"1"});
            $(_obj).click(function(){
                _buybotclick();
            }); 
        }
        a.OnReadyStateChange();
    }
    
    this.BottomNavShowHtml=function()
    {
        if($("#DivBottomNav").length>0)
            $("#DivBottomNav").remove();
                
        var html=
        "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" background=\"/images/webicon/b_lanline.gif\">"+
        "  <tr>"+
        "    <td><table width=\"100%\" height=\"34\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
        "      <tr>"+
        "        <td>&nbsp;</td>"+
        "        <td align=\"center\"><table width=\"960\" height=\"34\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">"+
        "          <tr>"+
        "            <td width=\"862\" valign=\"bottom\" background=\"/images/webicon/b_lan11.gif\"><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"+
        "              <tr>"+
        "                <td width=\"160\" height=\"34\" align=\"left\" style=\"padding-left:45px; font-size:12px; color:#FFFFFF\">当前购物车里有 <a href=\"/cart/cart.aspx\" style=\"color:#fff\"><span id=\"SpanNavBotCartCount\">-</span></a> 件商品</td>"+
        "               <td align=\"center\" style=\"font-size:12px; color:#000000; font-weight:200;  \"><div id=\"TdBuyProInfo\" style=\"width:630px;text-align:left;line-height:30px;height:30px\"><img src='/images/webicon/loading.gif' /> <span style=\"font-size:12px;color:#666666;font-weight:100;\">正在添加商品进入购物车,请稍候...</span></div></td>"+
        "              </tr>"+
        "            </table></td>"+
        "            <td height=\"34\" align=\"left\" background=\"/images/webicon/b_lan2.gif\" style=\"font-size:14px; color:#FFFFFF; font-weight:bold;\"><div style=\"margin:6px 0 0 5px\"><a href=\"/cart/orderinfo.aspx\" class=\"white14cl\" title=\"去结算中心\">去结算</a></div></td>"+
        "            <td width=\"23\" align=\"right\" valign=\"top\" background=\"/images/webicon/b_lan2.gif\" style=\"font-size:14px; color:#FFFFFF; font-weight:bold\"><img id=\"botnavimgx\" onclick=\"$('#DivBottomNav').remove()\" onmousemove=\"$('#botnavimgx').attr('src','/images/webicon/b_lanx1.gif')\" onmouseout=\"$('#botnavimgx').attr('src','/images/webicon/b_lanx.gif')\" alt=\"关闭底部导航\" src=\"/images/webicon/b_lanx.gif\" width=\"23\" height=\"17\"  style=\"padding-top:2px;cursor:pointer;\"/></td>"+
        "          </tr>"+
        "        </table></td>"+
        "        <td>&nbsp;</td>"+
        "      </tr>"+
        "    </table></td>"+
        "  </tr>"+
        "</table>";
        var newDiv=document.createElement("div");
        newDiv.id="DivBottomNav";
        newDiv.className="base";
        newDiv.style.display="none";
        newDiv.innerHTML=html;
        document.body.appendChild(newDiv);
        $("#DivBottomNav").fadeIn("slow");
        $("#SpanNavBotCartCount").html($("#"+this.DivCartCountId).html());
    }
    
    document.write(
            "<style type=\"text/css\">"+
            "    .base{"+
            "        position:fixed;"+
            "        bottom:0px;"+
            "        height:auto;"+
            "        width:100%;"+
            "        overflow: hidden;"+
            //"        filter:Alpha(Opacity=85);"+
            //"        Opacity:0.85;"+
            "    }"+
            "    </style>"+
            "    <!--[if lte IE 6]>"+
            "    <style type=\"text/css\">"+
            "    html{"+
            "        background:url(/images/webicon/skin.png);"+
            "        background-repeat: no-repeat;"+
            "        background-position: -9999px -9999px;"+
            "        }"+
            "    .base{"+
            "        position: absolute;"+
            "        top:expression(eval(document.compatMode &&"+
            "        document.compatMode=='CSS1Compat') ?"+
            "        documentElement.scrollTop"+
            "        +(documentElement.clientHeight-this.clientHeight)"+
            "        : document.body.scrollTop"+
            "        +(document.body.clientHeight-this.clientHeight));}"+
            "    </style>"+
            "    <![endif]-->"
            );
    
}