// JScript 文件
var Cookie = new Object();
Cookie.setCookie = function (name, value, expireHours) {
    var str = name + "=" + escape(value) + ";path=/";
    if (expireHours != null) {
        var date = new Date();
        var ms = expireHours * 3600 * 1000;
        date.setTime(date.getTime() + ms);
        str += "; expire=" + date.toGMTString();


    }

    document.cookie = str;

}
Cookie.getCookie = function (name) {
    var cookieArray = document.cookie.split("; ");
    for (var i = 0; i < cookieArray.length; i++) {
        var arr = cookieArray[i].split("=");
        if (arr[0] == name) {
            return unescape(arr[1]);
        }
    }
    return "";

}
Cookie.deleteCookie = function (name) {
    this.setCookie(name, "", -1000);

}



function SaveClass(className, classID) {
    var oldClass = Cookie.getCookie("Howave_ChooseClass");
    var newValue = "";

    if (oldClass != 'undefined' && oldClass != "" && oldClass != null) {
        var ClassArray = oldClass.split('|');
        for (var i = 0; i < ClassArray.length; i++) {
            var childClassArray = ClassArray[i].split(',');
            if (childClassArray[0] == className) {
                return;
            }

        }
        var classLength = ClassArray.length;
        var _start = 0;
        if (classLength >= 10) {
            ClassArray.shift();
            oldClass = "";
            for (var j = 0; j < ClassArray.length; j++) {
                if (j < ClassArray.length - 1) {
                    oldClass += ClassArray[j] + "|";
                }
                else {
                    oldClass += ClassArray[j];
                }


            }
        }
        newValue = oldClass + "|" + className + "," + classID;

    }
    else {
        newValue = className + "," + classID;

    }
    if (newValue != "") {
        Cookie.setCookie("Howave_ChooseClass", newValue);
    }

}

function SaveLastVisit(p_id, p_img, p_name, p_price) {
    var oldClass = Cookie.getCookie("Howave_LastVisitedProduct");
    var newValue = "";
    p_name = unescape(p_name);

    if (oldClass != 'undefined' && oldClass != "" && oldClass != null) {
        var ClassArray = oldClass.split('|');

        for (var i = 0; i < ClassArray.length; i++) {
            var childClassArray = ClassArray[i].split('$');
            if (childClassArray[0] == p_id) {

                return;
            }

        }
        var classLength = ClassArray.length;
        var _start = 0;
        if (classLength >= 6) {
            ClassArray.shift();
            oldClass = "";
            for (var j = 0; j < ClassArray.length; j++) {
                if (j < ClassArray.length - 1) {
                    oldClass += ClassArray[j] + "|";
                }
                else {
                    oldClass += ClassArray[j];
                }

            }
        }
        newValue = oldClass + "|" + p_id + "$" + p_img + "$" + p_name + "$" + p_price;

    }
    else {

        newValue = p_id + "$" + p_img + "$" + p_name + "$" + p_price;
    }


    if (newValue != "") {

        Cookie.setCookie("Howave_LastVisitedProduct", newValue, 2400);
    }
}





function $(id) {
    return document.getElementById(id);

}

function ShowClassSelect(id) {
    var str = Cookie.getCookie("Howave_ChooseClass");
    if (str != 'undefined' && str != '' && str != null) {

        var selectObj = $(id);
        if (selectObj == null)
            return;
        var _tips = document.createTextNode("选择常用分类：");
        var _select = document.createElement("select");
        var _clearButton = document.createElement("input");
        _clearButton.id = "clear_class_button";
        _clearButton.setAttribute("type", "button");
        _clearButton.setAttribute("value", "清除");
        AddEvent(_clearButton, ClearCustomClass, "onclick");
        _select.id = "customer_select_Class";
        AddEvent(_select, SelectCustomerClass, "onchange");
        _select.options[0] = new Option('请选择', '');

        if (selectObj != null) {

            str = str.split('|');
            str = str.sort();
            for (var i = 0; i < str.length; i++) {
                var childClass = str[i].split(',');
                _select.options[i + 1] = new Option(childClass[0], childClass[1]);

            }
        }
        selectObj.appendChild(_tips);
        selectObj.appendChild(_select);
        selectObj.appendChild(_clearButton);


    }

}

function SelectCustomerClass() {
    var _selectObj = $("customer_select_Class");
    var _selectDisplay = $("WhatYouChoose");
    var _selectClassHiddenObj = $("h_c_p_classID");
    var button = $("ChooseSuccesButton");
    if (_selectObj != null && _selectObj.selectedIndex != 0) {

        var _select_name = _selectObj.options[_selectObj.selectedIndex].text;
        var _select_value = _selectObj.value


        if (_selectDisplay != null && _selectClassHiddenObj != null) {

            _selectDisplay.innerHTML = _select_name;
            button.disabled = false;
            _selectClassHiddenObj.value = _select_value;
        }


    }
    else {
        _selectDisplay.innerHTML = "";
        _selectClassHiddenObj.value = "";
        button.disabled = true;
    }
}

function AddEvent(Obj, func, action) {

    if (Obj != null) {
        if (Obj.addEventListener) {

            Obj.addEventListener(action, func, false);
        }
        else {

            Obj.attachEvent(action, func, false);
        }
    }

}
function ShowLastVisited() {
    var str = Cookie.getCookie("Howave_LastVisitedProduct");
    
    if (str != 'undefined' && str != '' && str != null) {
        var StrproductID = "";
        var product_array = str.split('|');
        var _divHTML = "<div id='RecentlyViewed'  class='SideBox' style='width:auto;'>";
        // _divHTML += "<ul>";
        _divHTML += "<div><table style='float:left;width:auto;margn-top:15px;text-align:center'><tr>";
        for (var i = 0; i < product_array.length; i++) {
            var product_detail = product_array[i].split('$');
            var p_id = product_detail[0];
            var p_img = product_detail[1];
            var p_name = product_detail[2];
            var p_price = product_detail[3];
            StrproductID += "," + product_detail[0];
            escape(p_name);
            var image = "<img src='" + p_img + "' style='width:140px;height:140px; display:block;' />"; //图片
            var price = "<br><i>" + "￥" + p_price + "</i>";
            var pid = "<input type='hidden' name='cb_id' value='" + p_id + "'/>";
            var linkImage = "<a href='ShowProduct.aspx?ProductID=" + p_id + "' target='_blank'  title='" + p_name + "'>" + image + "</a>";
            //  _divHTML  +="<li style='float:left;width:80px;margn-top:15px;text-align:center'><a href='ShowProduct.aspx?ProductID=" + p_id + "' target='_blank'  title='" + p_name + "'><img src='"+p_img+"' style='width:40px;height:40px; display:block;' /></a><i>" + "￥"+p_price + "</i><input type='hidden' name='cb_id' value='" + p_id + "'/></li>";

            var str = linkImage + "<br>" + p_name + price + pid;
            _divHTML += "<td style='width:auto;text-align:center'>" + str + "</td>";
            if ((i + 1) % 6 == 0) {
                _divHTML += "<tr></tr>";
            }
        }
        //
        _divHTML += "</tr><tr><td colspan='6'>&nbsp;<hr style='border-top:solid 1px gray; width;'></td></tr></table></div>";
        // _divHTML += "<div class='ButtonBox' style='border-top:solid 1px gray;clear:both;text-align:center'><a href='ProductCompare.aspx?ProductID=" + StrproductID + "' target='_blank'>对比</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onClick='ClearLastVisit();'>清除</a>";
        _divHTML += "</ul><div class='ButtonBox' style='clear:both;text-align:center;'><a href='ProductCompare.aspx?ProductID=" + StrproductID + "' target='_blank'>对比</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='javascript:void(0)' onClick='ClearLastVisit();'>清除</a>";
       
        _divHTML += "</div>";
        _divHTML += "</div>";
        document.writeln(_divHTML);
    }
    else {
        alert("最近您还没有浏览过商品信息！");
        document.location.href = "ProductList.aspx";
    }
}
function ClearLastVisit() {
    Cookie.deleteCookie("Howave_LastVisitedProduct");
    var lastViewObj = $('RecentlyViewed');
    var compareFormObj = $('compareForm');

    if (compareFormObj != null && lastViewObj != null) {
        compareFormObj.removeChild(lastViewObj);
    }
}

function ClearCustomClass() {


    var _selectObj = $("useful_select_class");
    var _parentNode = _selectObj.parentNode;
    if (_selectObj != null && _parentNode != null) {
        _parentNode.removeChild(_selectObj);
        Cookie.deleteCookie("Howave_ChooseClass");
        alert("清除成功");

    }



}
