﻿//  ------------------------------------增加浏览过的商品Cookie设置开始
function CreateViewProductLogCookie() {
    var productid = $get("ItemID").value;
    var current = getOLCookie("ViewProductLog");
    if (productid.length != 0) {
        var pid = "";
        var pidarr = "," + current + ",";
        if (pidarr.indexOf("," + productid + ",") < 0) {
            if (current.length != 0) {
                pid = productid + "," + current;
                var arr = pid.split(",");
                if (arr.length > 10) {
                    pid = "";
                    for (var i = 0; i < 10; i++) {
                        if (pid.length == 0) {
                            pid = arr[i];
                        }
                        else {
                            pid = pid + "," + arr[i];
                        }
                    }
                }
            }
            else {
                pid = productid;
            }
            setOLCookie("ViewProductLog", pid, 30, "/", "", false);
        }
    }
    else {
        return "";
    }
}

function setOLCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));
    document.cookie = name + '=' + escape(value) +
   ((expires) ? ';expires=' + expires_date.toGMTString() : '') + // expires.toGMTString()
   ((path) ? ';path=' + path : '') +
   ((domain) ? ';domain=' + domain : '') +
   ((secure) ? ';secure' : '');
}
function getOLCookie(name) {
    if (document.cookie.length > 0) {
        cookieStart = document.cookie.indexOf(name + "=")
        if (cookieStart != -1) {
            cookieStart = cookieStart + name.length + 1
            cookieEnd = document.cookie.indexOf(";", cookieStart)
            if (cookieEnd == -1) cookieEnd = document.cookie.length
            return unescape(document.cookie.substring(cookieStart, cookieEnd));
        }
    }
    return "";
}
//清除浏览记录
function deleteCookie(name) {
    setOLCookie("ViewProductLog", "", -30, "/", "", false);
    showBrowsePro(10, 0);
}
//  ------------------------------------增加个性定制的商品Cookie设置开始
function CreateTailorCookie(picName, proID, showNum) {
    var urlName = picName;
    if (urlName.indexOf("20") != -1) {
        var strIdName = proID + "@" + urlName;
        var current = getOLCookie("OLCustomizationPicUrl");
        var pidarr = current;
        //排除刷新页面
        if (pidarr.indexOf(strIdName) < 0) {
            strIdName += "@1";
            var pid = "";
            if (current.length != 0) {
                //保存的图片数量
                var intNum = showNum - 1;
                var newCurrent = "";
                var nameArr = current.split("$");
                if (nameArr.length > intNum) {
                    for (var i = 0; i < intNum; i++) {
                        strSplit = (i < intNum - 1) ? "$" : "";
                        newCurrent += nameArr[i].replace("@1", "@0") + strSplit;
                    }
                }
                else {
                    newCurrent = current;
                }
                pid = strIdName + "$" + newCurrent.replace("@1", "@0");
            }
            else {
                pid = strIdName;
            }
            setOLCookie("OLCustomizationPicUrl", pid, 30, "/Shop/", "", false);
        }
    }
}
function deleteTailorCookie(name) {
    setOLCookie("OLCustomizationPicUrl", "", -60, "/Shop/", "", false);
}
function showPicList(proID, proCount) {
    var picCookies = getOLCookie("OLCustomizationPicUrl");
    var picArr = picCookies.split("$");
    var picLen = picArr.length;
    if (picLen > 0) {
        var nhtml = "";
        var checkID = 0;
        for (var i = 0; i < picLen; i++) {
            var picGroup = "";
            picGroup = picArr[i].split("@");
            if (picGroup[0] == proID) {
                if (checkID == 0) {
                    nhtml += "<div class=\"arrowhead\"></div><div class=\"mypic_list\">";
                }
                nhtml += "<input type=\"checkbox\" name=\"" + proID + "Check\" class=\"checkBtn\" onclick=\"selectPic(" + proID + "," + checkID + "," + proCount + ", this.checked)\"";
                if (picGroup[2] == 1) {
                    nhtml += " checked=\"checked\"";
                }
                nhtml += " /><a href=\"/UploadImages/" + picGroup[1] + ".png\" target=\"_blank\"><img src=\"/UploadImages/" + picGroup[1] + ".png\" width=\"75\" border=\"0\" /></a>";
                if (checkID < 5) {
                    checkID++;
                } else {
                    break;
                }
                if (checkID == 0) {
                    nhtml += "</div>";
                }
            }
        }
    }
    if (nhtml.length == 0) {
        nhtml = "<div class=\"car_nothing\"></div>";
    }
    document.getElementById("name" + proID).innerHTML = nhtml;
}
function selectPic(proID, noId, proCount, bCheck) {
    var checkArr = document.getElementsByName(proID + "Check");
    var checkCount = checkArr.length;
    if (checkCount > 0) {
        var currentCookies = getOLCookie("OLCustomizationPicUrl");
        if (proCount == 1) {
            for (var i = 0; i < checkCount; i++) {
                checkArr[i].checked = false;
                updateTailorCookie(proID, i, "false");
            }
            updateTailorCookie(proID, noId, "true");
            checkArr[noId].checked = true;
        }
        else {
            var onCheckNum = 0;
            for (var j = 0; j < checkCount; j++) {
                if (checkArr[j].checked == true) {
                    onCheckNum++;
                }
            }
            if (onCheckNum > proCount) {
                checkArr[noId].checked = false;
                alert("根据该商品购买数量您最多能选择" + proCount + "张个性定制图片，\n\n请先取消不需要的图片然后再进行选择！\n")
                return;
            } else {
                bCheck == false ? updateTailorCookie(proID, noId, "false") : updateTailorCookie(proID, noId, "true");
                checkArr[noId].checked = bCheck;
            }
        }
    }
}
function updateTailorCookie(proID, checkIndex, bCheck) {
    var currentCookies = getOLCookie("OLCustomizationPicUrl");
    var newCookies = "";
    var picArr = currentCookies.split("$");
    var myPicCount = 0;
    var strSplit = "";
    for (var i = 0; i < picArr.length; i++) {
        var picParam = picArr[i].split("@");
        strSplit = (i < picArr.length - 1) ? "$" : "";
        //当前商品
        if (picParam[0] == proID) {
            if (myPicCount == checkIndex) {
                picParam[2] = (bCheck != "true") ? "0" : "1";
            }
            newCookies += picParam[0] + "@" + picParam[1] + "@" + picParam[2] + strSplit;
            myPicCount++;
        } else {
            newCookies += picArr[i] + strSplit;
        }
    }
    setOLCookie("OLCustomizationPicUrl", newCookies, 30, "/Shop/", "", false);
}

