﻿// JScript 文件
//购物车对像
var ShopCart=new Object();

ShopCart.Continue=function()
{
    history.go(-1);
}

ShopCart.Submit=function(Destination)
{
    window.location.replace("/CheckOutCart.aspx?Destination="+Destination);
}
//只是购物车和购物车下面的页面调用
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("TotalPrice").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.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("CartMoney")!=null)
        {
           document.getElementById("CartMoney").innerHTML=items.money;
        }
    }
}
//函数名：ShopCart.CountryListChange
//功能介绍
ShopCart.CountryListChange = function(obj)
{ 
     var GetVal = AjaxAction.CountryListChange(obj);
     document.getElementById("ShippingMethod").innerHTML = GetVal.value;
}


ShopCart.ContinueShopping=function()
{
  window.history.go(-1);
}
// 函数名：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;
    }
}

ShopCart.ReceivingEmail=function()
{
  var num = document.getElementById("emailtxt").value;
  if(num!="")
  {
      if(!Utils.isEmail(num))
      {
          alert("Please fill in the correct mail account");
      }
      else
      {
        var GetVal = AjaxAction.getMember(num);
        if(GetVal.value=="1")
        {
          alert("You are already a site member and receive messages within the station site, please check your e-mail");
        }
        else if (GetVal.value=="2")
        {
          alert("You are already a site member and receive messages within the station site, please check your e-mail default password is six 1");
        }
        else if(GetVal.value=="3")
        {
          alert("I'm sorry you have a membership site in 24 hours can not be continuous registration");
        }
        else
        {
         alert("Operation failed!");
        }
      }
  }
  else
  {
    alert("Please enter your email account!"); 
  }
}

ShopCart.AjaxAddCart=function(pid,e)
{
    stime = 5;
    var GetVal = AjaxAction.InCart(pid,'1','');
    ShopCart.ShowCounts();
    if(GetVal.value=="1")
    {       
        autoClose();
        var obj =  document.getElementById("divcart");
        obj.style.display="";
        e = e||event;
        obj.style.left = (document.body.offsetWidth)/2 +"px";
        obj.style.top = document.documentElement.scrollTop  + e.clientY - 100 +"px";
    }
    else
    {
       alert("The product has benn pulled off the shelves");
      document.getElementById("divcart").style.display="none";
    }
}

function autoClose()
{
    stime;
    if(stime>=0)
    {
        setTimeout(autoClose,1000);
    }
    else
    {
        document.getElementById("divcart").style.display="none";
        stime = 5;
    }      
}

ShopCart.DivCart=function()
{
    document.getElementById("divcart").style.display="none";
    ShopCart.ShowCounts();
} 
 
//优惠活动列表显示 耿新阳 2010-8-23 10:13:04
ShopCart.GetSpecialOffers=function()
{
    var GetVal = AjaxAction.GetSpecialOffers();
    if(GetVal.value!="")
    {
        document.getElementById("Special_Offers").innerHTML = GetVal.value;
        ShopCart.GetPid();
    }
    else
    {
        if(document.getElementById("Sp_Of")!=null)
        {
            document.getElementById("Sp_Of").style.display="none";
        }
    }
}
 
 // 优惠产品放入购物车
  
ShopCart.Preferential=function(Pid ,Price)
{
     var Sid=document.getElementById("Promotionid").value;
     var Amout=document.getElementById("Amount").value;
     var GetVal = AjaxAction.CheckPromotion(Sid);
     if(GetVal.value=="1")//代表用户可以参加优惠活动
     {
         
          if(parseInt(AjaxAction.GetPrePidCount().value)>=Amout)
          {
           if(confirm ("优惠商品已经发放完毕，再次放入购物车需按商品价格收费！"))
           {
             if(AjaxAction.InCart(Pid,'1','').value=="1")
               {
                  if(confirm ("产品放入购物车，是否查看购物车？"))
                   {
                        SetCookie("Gifts",Pid);
                        location.href="/ShowCart.html";
                        alert(GetCookie("Gifts"));
                   }
               }
           }
             
          }
          else
          {
               if(AjaxAction.InCart(Pid,'1','').value=="1")
               {
                 AjaxAction.ModifyPreferential(Price,Pid,Amout);
                  if(confirm ("产品放入购物车，是否查看购物车？"))
                   {
                        SetCookie("Gift",Pid);
                        location.href="/ShowCart.html";
                   }
               }
          }
       
     }
     else if(GetVal.value=="0")
     {
        alert("你无法参加此优惠活动！");
     }
}
  
//选择促销方式
ShopCart.CheckRadion=function(Oid)
{
    AjaxAction.SetPid(Oid);
    document.getElementById(Oid).checked = true;
}

ShopCart.GetPid=function()
{
    var str=AjaxAction.GetPid();
    if(str.value!="0")
    {
        if(document.getElementById(str.value).disabled==false)
        {
            document.getElementById(str.value).checked = true;
        }
    }
}
