﻿// JScript 文件
//购物车对像
var ShopCart=new Object();

ShopCart.Continue=function()
{
    history.go(-1);
}

ShopCart.Submit=function(Destination)
{
    window.location.replace("/CheckOutCart.aspx?Destination="+Destination);
}

ShopCart.ShowCounts=function()
{
    var GetVal = AjaxAction.GetOrderItem();
    var item = GetVal.value;
    if(item)
    {
        var items = eval(item);
        if(document.getElementById("CartItems")!=null)
        {
            document.getElementById("CartItems").innerHTML=items.counts;
        }
        if(document.getElementById("CartItems").innerHTML!=null)
        {
            document.getElementById("CartMoney").innerHTML=items.money;
        }
    }
}

//只是购物车和购物车下面的页面调用
ShopCart.ShowCartInfo=function()
{
   var GetVal = AjaxAction.GetOrderItem();
   var item = GetVal.value;
   if(item!="")
   {
       var items = eval(item);
       document.getElementById("Weight").style.color="red";
       document.getElementById("Quantity").style.color="red";
       document.getElementById("Quantity").innerHTML = items.counts;
       document.getElementById("Weight").innerHTML = items.weight;
       document.getElementById("CartItems").innerHTML = items.counts;
       document.getElementById("CartMoney").innerHTML = items.money;
       document.getElementById("TotalPrice").innerHTML = items.money;
   }
}

//函数名：ShopCart.CountryListChange
//功能介绍
ShopCart.CountryListChange = function(obj)
{ 
     var GetVal = AjaxAction.CountryListChange(obj);
     document.getElementById("ShippingMethod").innerHTML = GetVal.value;
}

// 函数名：ShopCart.jump();
//功能介绍：把国家的ID传给BillingInfo页面以获取邮费价格
ShopCart.jump=function()
{ 
    var Country=document.getElementById("countries_select").value;
    location.href="/BillingInfo.html?Country="+Country;
}

ShopCart.AddCart=function(pid,counts,attribute,url)
{   
    if(attribute!="")
    {
        CartProduct(pid,counts,attribute);
    }
    else
    {
        var product=new Product(pid,counts,url);
        product.FindAttribute(pid,counts,url)
    }
}

function Product(pid,count,url)
{  
    this.FindAttribute=function(pid,counts,url)
    {
        Ajax=InitAjax();
        Ajax.open("GET","/havaAttributes.aspx?pid="+pid,true)
        Ajax.send(null)
        Ajax.onreadystatechange = function()
        {
            if(Ajax.readyState == 4 && Ajax.status==200)
            {   
               if(parseInt(Ajax.responseText)>0)
               {
                  location.href=url;
               }
               else
               {
                  CartProduct(pid,counts,'');
               }
            }
        }
    }
}

function CartProduct(pid,counts,attribute)
{
    try{
        var Product="{pid:"+pid+",count:"+counts+",attribute:'"+attribute+"'}";
        var CartProduct;
        var cookie=GetCookie("WebCart");
        if(cookie==null)
        {
            var String=new StringBuffer()
            String.append("[");
            String.append(Product);
            String.append("]");
            SetCookie("WebCart",String.toString());
        }
        else
        {
            var CartArr=eval(cookie);
            var i=0;var flag=false;
            for(i=0;i<CartArr.length;i++)
            {
                CartProduct=eval(CartArr[i]);
                if(CartProduct.pid==pid && CartProduct.attribute)
                {
                    CartProduct.count=parseInt(CartProduct.count)+1;
                    Product="{pid:"+CartProduct.pid+",count:"+CartProduct.count+",attribute:'"+CartProduct.attribute+"'}";
                    CartArr[i]=Product;
                    flag=true;
                    break;
                }
                
            }
            if(flag==true)
            {   
                var i=0;
                var String=new StringBuffer();
                String.append("[");
                
                for(i=0;i<CartArr.length;i++)
                {   
                    if(typeof(CartArr[i])!="object")
                    {
                        CartProduct=eval("("+CartArr[i]+")");
                    }
                    else
                    {
                        CartProduct=eval(CartArr[i]);
                    }
                    String.append("{pid:"+CartProduct.pid+",count:"+CartProduct.count+",attribute:'"+CartProduct.attribute+"'}");
                    if(i!=parseInt(CartArr.length-1))
                    {
                        String.append(",");
                    }
                }
                String.append("]");
                SetCookie("WebCart",String.toString());
            }
            else
            {   
                var String=new StringBuffer();
                String.append("[");
                for(i=0;i<CartArr.length;i++)
                {   
                    if(typeof(CartArr[i])!="object")
                    {
                        CartProduct=eval("("+CartArr[i]+")");
                    }
                    else
                    {
                        CartProduct=eval(CartArr[i]);
                    }
                    String.append("{pid:"+CartProduct.pid+",count:"+CartProduct.count+",attribute:'"+CartProduct.attribute+"'}");
                    if(i!=parseInt(CartArr.length-1))
                    {
                        String.append(",");
                    }
                }
                String.append(",");
                String.append(Product)
                String.append("]");
                SetCookie("WebCart",String.toString());
            }
        }
        return true;
    }
    catch(err)
    {
        return false;
    }
}

function AddCart(form)
{
    var spec=document.getElementById("Spec").value;
    var attribute=document.getElementById("attribute").value;
    var arrayAttribute=new Array();
    if(attribute!="")
    {
        arrayAttribute=attribute.split(",");
    }
    var arraySpec=new Array();
    if(spec!="")
    {
        arraySpec=spec.split("|");
    }
    if(arrayAttribute.length!=arraySpec.length)
    {
      alert("check attribute");
    }
    else
    {
        document.getElementById(form).submit();
    }
}
