﻿
recordPositionY = 0;
function FixPosition()
{
 var recordScrollTop = 0;
 if(document.documentElement && document.documentElement.scrollTop)
 {
 recordScrollTop = document.documentElement.scrollTop;
 }
 else
 {
 if(document.body)
 {
 recordScrollTop = document.body.scrollTop;
 }
 }
 var movePixY = 0.2 * (recordScrollTop - recordPositionY);
 if(movePixY > 0)
 {
 movePixY = Math.ceil(movePixY);
 }
 else
 {
 movePixY = Math.floor(movePixY);
 }
 obj=document.getElementById("CompareBasket");
 var basketTop = obj.style.pixelTop;
 obj.style.pixelTop = basketTop + movePixY;
 recordPositionY = recordPositionY + movePixY;
}
window.setInterval("FixPosition()",2);

// unicode 编码程序
function BasketEncodeCookie(InputString)
{
 var strRtn="";
 for (var i=InputString.length-1;i>=0;i--)
 {
 strRtn+=InputString.charCodeAt(i);
 if (i) strRtn+="a"; // 用 a 作分隔符
 }
 return strRtn;
}

// unicode 解码程序
function BasketDecodeCookie(InputString)
{
 var strArr;
 var strRtn="";

 strArr=InputString.split("a");

 for (var i=strArr.length-1;i>=0;i--)
 strRtn+=String.fromCharCode(eval(strArr[i]));

 return strRtn;
}

// 读 Cookie
function BasketGetCookie(name)
{
 var strArg=name+"=";
 var nArgLen=strArg.length;
 var nCookieLen=document.cookie.length;
 var nEnd;
 var i=0;
 var j;

 while (i<nCookieLen)
 {
 j=i+nArgLen;
 if (document.cookie.substring(i,j)==strArg)
 {
 nEnd=document.cookie.indexOf (";",j);
 if (nEnd==-1) nEnd=document.cookie.length;
 return BasketDecodeCookie(unescape(document.cookie.substring(j,nEnd)));
 }
 i=document.cookie.indexOf(" ",i)+1;
 if (i==0) break;
 }
 return null;
}

// 写 Cookie
function BasketSetCookie(name,value,expires)
{
 var exp = new Date();
 exp.setTime(exp.getTime()+expires*60*60*1000);
 document.cookie=name+"="+escape(BasketEncodeCookie(value))+";expires="+exp.toGMTString();
}

// 判断 Cookie 是否存在并写产品对比 Cookie
function BasketCheckSetCookieValue(name,value,expires)
{
 var nameCookieValue = BasketGetCookie(name);
 if ((nameCookieValue == "") || (nameCookieValue == null))
 {
 var exp = new Date();
 exp.setTime(exp.getTime()+expires*60*60*1000);
 document.cookie=name+"="+escape(BasketEncodeCookie(value))+";expires="+exp.toGMTString();
 }
 else
 {
 if (nameCookieValue.indexOf(value) == -1)
 {
 var arrCookies = nameCookieValue.split("；");
 var ValueNum = arrCookies.length;
 if (ValueNum > 4)
 {
 nameCookieValue = "";
 for (loop=0; loop < 4; loop++)
 {
 nameCookieValue += arrCookies[loop] + "；";
 }
 nameCookieValue = nameCookieValue.substring(0,nameCookieValue.length - 1);
 }

 var exp = new Date();
 exp.setTime(exp.getTime()+expires*60*60*1000);
 document.cookie=name+"="+escape(BasketEncodeCookie(value+"；"+nameCookieValue))+";expires="+exp.toGMTString();
 }
 }
}

function hide()
{
 obj=document.getElementById("CompareBasket");
 obj.style.display='none';
}

function show()
{
 obj=document.getElementById("CompareBasket");
 obj.style.display='block';
}

function Empty()
{
 BasketSetCookie("POP_SubCategory","",24);
 BasketSetCookie("POP_CompareProducts","",24);
 redraw();
}

// 判断选择的产品和以前选择的产品是否同类同类产品
function CheckCategoryChange(SubCategory)
{
    var SubCategorySN = BasketGetCookie("POP_SubCategory");
    if ((SubCategory == null) || (SubCategory == ""))
    {
    BasketSetCookie("POP_SubCategory",SubCategory,24);
    }
    else
    {
    if (SubCategory != SubCategorySN)
    {
    BasketSetCookie("POP_SubCategory",SubCategory,24);
    BasketSetCookie("POP_CompareProducts","",24);
    }
    }
}

function AddCompareProduct(ProductSN,ProductName,ProductLink,ProductImg,SubCategory)
{
 CheckCategoryChange(SubCategory);
 var CompareProducts = BasketGetCookie("POP_CompareProducts");
 if ((CompareProducts != "") && (CompareProducts != null))
 {
 var arrCookies = CompareProducts.split("；");
 if (arrCookies.length < 4)
 {
 if (CompareProducts.indexOf(ProductSN) != -1)
 {
 alert("（" + ProductName + "）已经被选择了！");
 }
 else
 {
 BasketCheckSetCookieValue("POP_CompareProducts",ProductSN + "|" + ProductName + "|" + ProductLink + "|" + ProductImg,24);
 }
 }
 else
 {
 alert("对不起！您只能选择四款同类产品进行比较。");
 }
 }
 else
 {
 BasketCheckSetCookieValue("POP_CompareProducts",ProductSN + "|" + ProductName + "|" + ProductLink + "|" + ProductImg,24);
 }
 redraw();
 ShowLayer();
}

function DelProduct(ProductSN)
{
 var nameCookieValue = BasketGetCookie("POP_CompareProducts");
 if ((nameCookieValue != null) && (nameCookieValue != ""))
 {
 if (nameCookieValue.indexOf(ProductSN) > -1)
 {
 var arrCookies = nameCookieValue.split("；");
 var ValueNum = arrCookies.length;
 nameCookieValue = "";
 for (i=0; i < ValueNum; i++)
 {
 if (arrCookies[i].indexOf(ProductSN) == -1)
 {
 nameCookieValue += arrCookies[i] + "；";
 }
 }
 nameCookieValue = nameCookieValue.substring(0,nameCookieValue.length - 1);
 BasketSetCookie("POP_CompareProducts",nameCookieValue,24);
 }
 redraw();
 }
}

function redraw()
{
 //FixPosition();
 var CompareProducts = BasketGetCookie("POP_CompareProducts");
 if (CompareProducts == null)
 {
 CompareProducts = "";
 }
 var arrCookies = CompareProducts.split("；");
 var CompareTable = "<table width='100%' border='0' cellpadding='1' cellspacing='1' bgcolor='#92BCDC' style='font-size:12px'>"
 + "<tr>"
 + "<td height='22' align='center' bgcolor='#E8F5FD'><b style='font-size:12px; color:red'>产品对比栏</b></td>"
 + "</tr>"
 + "<tr>"
 + "<td align='center' bgcolor='#E8F5FD'>";

 for (i = 0; i < arrCookies.length; i++)
 {
 if ((arrCookies[i] != "") && (arrCookies[i] != null))
 {
 if (arrCookies[i].indexOf("|") >= 0)
 {
 var ProductInfo = arrCookies[i].split("|");
 if (ProductInfo.length = 4)
 {
 var ProductSN = ProductInfo[0];
 var ProductName = ProductInfo[1];
 var ProductLink = ProductInfo[2];
 var ProductImg = ProductInfo[3];
 if (ProductSN == null)
 {
 ProductSN = "";
 }
 if (ProductName == null)
 {
 ProductName = "";
 }
 if (ProductLink == null)
 {
 ProductLink = "";
 }
 if (ProductImg == null)
 {
 ProductImg = "";
 }
 if ((ProductName != "") && (ProductSN != "") && (ProductLink != "") && (ProductImg != ""))
 {
 CompareTable = CompareTable + "<font color='red'>|</font>"
 + "<table border='0' align='center' cellpadding='0' cellspacing='1' bgcolor='black'>"
 + "<tr>"
 + "<td bgcolor='white'><a href='" + ProductLink + "' target='_blank'><img src='" + ProductImg + "' width='41' height='76' alt='" + ProductName + "'></a></td>"
 + "</tr>"
 + "</table>"
 + "<table width='80' align='center' border='0' cellpadding='0' cellspacing='0'>"
 + "<tr>"
 + "<td height='2'></td>"
 + "</tr>"
 + "</table>"
 + "<input type='button' name='Product" + i + "' value='" + ProductName + "' style='border:1px solid; border-color:#92BCDC; background-color:white; height:22px; width:111px; cursor:hand; color:black; font-size:12px' onClick=DelProduct('" + ProductSN + "');>"
 + "<br>";
 }
 }
 }
 }
 }

 CompareTable = CompareTable + "<table width='100%' border='0' cellpadding='0' cellspacing='0' style='font-size:12px'>"
 + "<tr>"
 + "<td height='25' align='center' valign='bottom'><a onClick='Empty()' style='color:blue;cursor: hand'>清空</a>&nbsp;&nbsp;&nbsp;<a onClick='HideLayer()' style='color:blue;cursor: hand'>隐藏</a></td>"
 + "</tr>"
 + "</table>"
 + "</td>"
 + "</tr>"
 + "<tr>"
 + "<td align='center' bgcolor='#E8F5FD'><input type='button' value='产品对比' class='button1' name='CompareBtn' style='cursor:hand; width:80px; font-size:12px' onClick=CompareCheck()></td>"
 + "</tr>"
 + "</table>";
 document.getElementById('CompareBasket').innerHTML = CompareTable;
}

function IsNumber(inputVal)
{
 var inputStr = inputVal.toString();
 var i = 0;
 for (i =0; i<inputStr.length; i++)
 {
 var oneChar = inputStr.charAt(i)
 if (oneChar < "0" || oneChar> "9")
 {
 return false;
 }
 }
 return true;
}

function CompareCheck()
{
    var URL = "../PhoDB/PhoBjn.aspx";
    var CompareProductsValue = BasketGetCookie("POP_CompareProducts");
    if ((CompareProductsValue != "") && (CompareProductsValue != null))
    {
        var arrValues = CompareProductsValue.split("；");
        var ValuesNum = arrValues.length;
        if (ValuesNum < 2)
        {
            alert ('请至少选择两款同类别产品进行对比！');
        }
        else
        {
            var SubCategorySN = BasketGetCookie("POP_SubCategory");
            if ((SubCategorySN != "") && (SubCategorySN != null))
            {
                if (IsNumber(SubCategorySN))
                {
                    var QueryString = "ids=";

                    for (i = 0; i < ValuesNum; i++)
                    {
                        var ProductInfo = arrValues[i].split("|");
                        if (IsNumber(ProductInfo[0]))
                        {
                            QueryString += ProductInfo[0]+",";
                        }
                    }
                    QueryString = QueryString.substr(0,QueryString.length-1);
                    window.open(URL + "?" + QueryString);
                }
            }
        }
    }
}

redraw();

function HideLayer()
{
 BasketSetCookie("POP_HiddenCompare","Hide",24);
 hide();
}

function ShowLayer()
{
 BasketSetCookie("POP_HiddenCompare","Show",24);
 show();
}

if ((BasketGetCookie("POP_HiddenCompare") == "Hide") || (BasketGetCookie("POP_CompareProducts") == "") || (BasketGetCookie("POP_CompareProducts") == null))
{
 HideLayer();
}
else
{
 ShowLayer();
}