﻿
var webUrl = "/";
var DelErrorInfo = ["删除失败", "删除成功"];
var SaveErrorInfo = ["保存购物车失败", "保存购物车成功"];
var AddErrorInfo = ["添加商品失败", "添加商品成功", "超过购物车最大数量"];
var UpdateErrorInfo = ["修改商品失败", "修改商品成功"];

function QuickBuy(PId) {
    var PCount=$("#buyNO").val();
    var PColor=$("#HidProductColor").val();
    var PSize=$("#HidProductSize").val();
    PCount =PCount > 0 ? PCount: 1;
    AddShopCartProduct(PId,PCount,PColor,PSize);  
}

function BuyProduct(PId,PCount) {
	if(PCount!="")
	{
       PCount = PCount > 0 ? PCount : 1;
	}
   AddShopCartProduct(PId ,PCount,"","");
}

function AddShopCartProductList(PLId,PLCount)
{
    var strUrl ="http://member.yiliu88.com/ajax/initcart.aspx?ptype=1";
    if(PLId!="")
    {
      strUrl+="&plid="+escape(PLId);
    }
    if(PLCount!="")
    {
      strUrl+="&plcount="+escape(PLCount);
    }
    var tmpUrl=window.location.href;
    if(tmpUrl.toLowerCase().indexOf('/mycars.aspx')>-1)
    {window.location= strUrl;
       
        }
        else
        {
         window.open( strUrl);
        }
}

function AddShopCartProduct(PId,PCount,PColor,PSize)
{
    var strUrl ="http://member.yiliu88.com/ajax/initcart.aspx?pid="+PId+"&pcount="+PCount+"&ptype=1";
    if(PColor!="")
    {
      strUrl+="&pcolor="+escape(PColor);
    }
    if(PSize!="")
    {
      strUrl+="&psize="+escape(PSize);
    }
    var tmpUrl=window.location.href;
    if(tmpUrl.toLowerCase().indexOf('/mycars.aspx')>-1)
    {window.location= strUrl;
       
        }
        else
        {
         window.open( strUrl);
        }
}

function AddProduct(strPid) {
    var strUrl ="/Cart.aspx";
    pNo = $("#buyNO").val() > 0 ? $("#buyNO").val() : 1;
    var strData = "Action=AddProductToCart&pid=" + strPid + "&pNo=" + pNo + "&Cache=" + Math.random();
    jQuery.ajax({
        url: strUrl,
        type: "POST",
        data: strData,
        dataType: "html",
        async: false,
        success: function(html) {

            alert("商品成功加入购物车！");
            BindShopCartDataNew();
        },
        error: function() {
            alert("商品加入购物车失败！")
        }
    });
}




function toShowCart(){
	
    $("#shopCartList").show();
}

function toCloseCart(){
    $("#shopCartList").hide();
}

function DelProductNew(strPid) {

    var strUrl = "http://member.yiliu88.com/Ajax/Default.aspx?";
    var strData = "action=DELCARTPRODUCT&pid="+strPid+"&r=" + Math.random()+"&callback=?";
    jQuery.getJSON(strUrl+strData,function(data) {
    BindShopCartDataNew();
    alert(DelErrorInfo[data.tmpReturn])
    });
}

function getListHtmlNew(){
    var strHtml="";

    strHtml+="<li>";
    strHtml+="<a href=\"http://www.yiliu88.com/product/{c_Purl}.html\" target=\"_blank\">{c_PName}</a><br/>";
    strHtml+="{c_PNo}*{c_PPrice}={TotalPrice}<a onclick=\"DelProductNew({c_PId})\" style=\"cursor:pointer;\"><img src=\"http://www.yiliu88.com/images/headfoot/delete_btn.gif\" align=\"absmiddle\"  alt=\"点击删除\" /></a>";
    strHtml+="</li>";
    
    return strHtml;
}
function getDivHtmlNew(){
    var strHtml="";
    strHtml+="<div class=\"mycarpro\">";
    strHtml+="<h6>我的购物车商品列表</h6><ul>";
    strHtml+="{c_Plist}";
    strHtml+="</ul></div>";
    strHtml+="<div class=\"mcMoney\">总金额：<b>{c_SumPrice}</b>元</div>";
    return strHtml;
}

function BindShopCartDataNew(){
    var strUrl = "http://member.yiliu88.com/Ajax/Default.aspx?";
    var strData = "action=INITSHOPCART&r=" + Math.random()+"&callback=?";
    var strTempList=getListHtmlNew();
    var strTempDiv=getDivHtmlNew();
    jQuery.getJSON(strUrl+strData,function(html) {
            if (html != "" && html["CartItem"].length > 0) {
                var strOneList = "";
                var Price = 0;
                var No = 0;
                var htmlCode="";
                var pageCart;

                $.each(html["CartItem"],
                function(i, oneRow) {
                      strOneList=strTempList;
                      
                      strOneList=strOneList.replace(/{c_PName}/g,unescape(oneRow.Name));
                      strOneList=strOneList.replace("{c_Purl}",oneRow.KeyId);
                      strOneList=strOneList.replace(/{c_PPrice}/g,oneRow.Price);
                      strOneList=strOneList.replace(/{c_PNo}/g,oneRow.NO);
                      strOneList=strOneList.replace(/{c_PId}/g,oneRow.Id);
                      strOneList=strOneList.replace(/{TotalPrice}/g,oneRow.ToTalPrice);
                     // Price += (parseFloat(oneRow.Price) * (oneRow.BaseNum * oneRow.NO));
                      
                      Price += (parseFloat(oneRow.Price) * parseFloat(oneRow.NO));
                      
                      No = parseInt(oneRow.NO) + parseInt(No);
                      htmlCode+=strOneList;
                });
                
                $("#cartProductNO").html(html["CartItem"].length);
                strTempDiv=strTempDiv.replace("{c_Plist}",htmlCode);
                strTempDiv=strTempDiv.replace("{c_SumPrice}",Price);

            } else {
                $("#cartProductNO").html("0");
                strTempDiv=strTempDiv.replace("{c_Plist}","<dl><dt>您的购物车中暂无商品！</dt><dd></dd></dl>");
                strTempDiv=strTempDiv.replace("{c_SumPrice}","0");
            }
            
         
			$("#shopCartList").html(strTempDiv);
			
            $("#shopCartList").hide();
        },
       function(xmlHttpRequest) {
            
        }
        );
}

function OpenShopCart()
{
    var url="http://member.yiliu88.com/mycars.aspx";
    var focuswindow=window.open(url,"shopcart");
    focuswindow.focus();
}

//收藏夹
function AddFavorites(PId,PUrl,PTitle) {
    var strUrl = "http://member.yiliu88.com/Ajax/Default.aspx?";
    var strData = "action=ADDFAVORITES&pid="+PId+"&r=" + Math.random()+"&callback=?";
    jQuery.getJSON(strUrl+strData,function(data) {
    switch (data.tmpReturn) {
                case "00":
                    CreateFavoritesMessage("您已经收藏过该商品！");
                    break;
                case "01":
                if(PTitle=="")
                {
                PTitle=document.title;
                }
                if(PUrl=="")
                {
                PUrl=document.location.href;
                }
                   WindowFavorites(PUrl,PTitle);
                    break;
                case "10":
                    CreateFavoritesMessage("收藏商品成功！");
                    break;
            }
    });
}

function CreateFavoritesMessage(tmpMessage)
{
  var strFavoritesMsg="<div id=\"divFavorites\"  style=\"display:none;\">"
+"<div class=\"collectbg\" id=\"divFavoritesBg\" ></div>"
+"<div class=\"joinCollect\">"
+"<div class=\"myCollect\" id=\"divFavoritesMsg\">"
+"<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">"
+"<tr style=\"background:url(http://member.yiliu88.com/images/nm_td_bg2.gif) 0 0 repeat-x; font-size:14px;\">"
+"<td height=\"27\" width=\"97%\" style=\"padding-left:2%;\"><b>加入收藏</b></td>"
+"<td width=\"1%\" valign=\"top\"><a href=\"javascript:CloseFavorites();\"><img src=\"http://member.yiliu88.com/images/nm_close_btn_2.gif\" align=\"absmiddle\" alt=\"关闭\" /></a></td></tr>"
+"<tr>"
+"<td height=\"100\" colspan=\"2\" align=\"center\" style=\" line-height:40px;\">"
+"<span class=\"redTxt\">√ "+tmpMessage+"</span><br />"
+"<input type=\"button\" onclick=\"BrowserFavorites()\" value=\"查看\" style=\"border:0; width:40px; height:20px; background:url(http://member.yiliu88.com/images/nm_btn_bg4.gif) 0 0 repeat-x; color:#fff; font-size:12px;\" class=\"inputTxtS\" />&nbsp;&nbsp;<input type=\"button\" onclick=\"CloseFavorites()\"  value=\"确定\" style=\" font-size:12px;border:0; width:40px; height:20px; background:url(http://member.yiliu88.com/images/nm_btn_bg4.gif) -40px 0 no-repeat; color:#fff;\" class=\"inputTxtS\" />"
+"</td></tr>"
+"</table>"
+"</div>"
+"</div>"
+"</div>";
$(document.body).append(strFavoritesMsg);
$("#divFavoritesBg").height($(document).height());
$("#divFavoritesMsg").css("margin-top",($(document).scrollTop()+($(window).height()/2-100))+"px");
$("#divFavorites").show();
}

function CloseFavorites()
{
$("#divFavorites").hide();
$("#divFavorites").remove();
}

function BrowserFavorites()
{
  $("#divFavorites").hide();
  $("#divFavorites").remove();
  var strUrl="http://member.yiliu88.com/MyFavorites.aspx";
  window.open(strUrl);
}

function Share(ShareUrl,ShareTitle)
{
   var tmpShareUrl="",tmpShareTitle="";
   if(ShareUrl!=""&&ShareUrl!=undefined)
   {
     tmpShareUrl=ShareUrl;
   }
   else
   {
      tmpShareUrl=document.location.href,document.title;
   }
   if(ShareTitle!=""&&ShareTitle!=undefined)
   {
     tmpShareTitle=ShareTitle;
   }
   else
   {
      tmpShareTitle=document.title;
   }
   var strUrl="http://www.yiliu88.com/Share.aspx?shareurl="+tmpShareUrl+"&sharetitle="+escape(tmpShareTitle) ;
   window.open(strUrl);
}