/*******************************************************************
 * 该js包含了对cookie的基本处理，同时含有对产品历史浏览记录的设置和获得等处理  *
 * @author yongtree                                                *
 *******************************************************************/
function GetCookieVal(offset){
    var endstr = document.cookie.indexOf(";", offset);
    if (endstr == -1) 
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function SetCookie(name, value, expir)//�设置cookie，name-key，value-value，expir-失效时间��ʱ��
{
    var expdate = new Date();
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : "/";
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    if (expires != null) 
        expdate.setTime(expdate.getTime() + (expires * 1000));
    document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : (";  expires=" + expdate.toGMTString())) +
    ((path == null) ? "" : (";  path=" + path)) +
    ((domain == null) ? "" : (";  domain=" + domain)) +
    ((secure == true) ? ";  secure" : "");
    
}

function DelCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = GetCookie(name);
    document.cookie = name + "=" + cval + ";  expires=" + exp.toGMTString();
}

function GetCookie(name)//��通过key得到value的值。ֵ
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) 
            return GetCookieVal(j);
        i = document.cookie.indexOf("", i) + 1;
        if (i == 0) 
            break;
    }
    return null;
}

/**
 * 将产品的名称、价格、产品页面URL的写入到cookie中
 * @author yongtree
 * @param name 产品名称
 * @param price 产品价格 格式：特价-v0.00，促销价-s0.00，会员价-m0.00
 * @param url 产品页面路径
 * @param number 设置历史产品的数量
 */
function addProductStory(pname, price, url, number){
    var value;
    var maxTime = 30 * 24 * 60 * 60;
    value = pname + ',' + price + ',' + url;
    var newValue;
    if (GetCookie('productStory') == null) {
        SetCookie('productStory', value, maxTime);
    }
    else {
        var oldValue;
        oldValue = GetCookie('productStory');
        var products = oldValue.split('|');
        if (products.length >= number) {
            oldValue.substring(oldValue.lastIndexOf(','), products.length);
        }
        if (oldValue.indexOf(pname) != -1) {
            newValue = oldValue;
        }
        else {
            newValue = value + '|' + oldValue;
        }
        SetCookie('productStory', newValue, maxTime);
    }
}

/**
 * 得到历史浏览记录，并在页面上显示，通过修改该部分的代码来修改在页面上的显示效果
 */
function getProductStory(){
    document.write('<div id="left_four_top" class="top_title">历史浏览记录</div> ');
    
    var value;
    value = GetCookie('productStory');
    if (value != null) {
        document.write('<div id="left_four_content" class="left_content_bg"> <ul class="left_content_History">');
        var products = value.split('|');
        var pLength;
        
        if (products.length > 8) {
            pLength = 8;
        }
        else {
            pLength = products.length;
        }
        
        for (i = 0; i < pLength; i++) {
        
            var product = products[i];
            var item = product.split(',');
            var pname = item[0];
            var price = item[1];
            var url = item[2];
            var priceStr = '￥' + price.substring(1, price.length) + '元';
            if (price.charAt(0) == 'c') {
                document.write("<li><img src='/images2/cuxiao_icon.gif' width='20' height='18' align='absmiddle' /><a href=" + url + ">" + pname + "(" + priceStr + ")</a></li>");
            }
            else {
                document.write("<li><a href=" + url + ">" + pname + "(" + priceStr + ")</a></li>");
            }
        }
        
        document.write('</ul></div>');
    }
    
    
}


/**
 * 得到历史浏览记录，并在页面上显示，通过修改该部分的代码来修改在页面上的显示效果
 */
function getGiftStory(){



    var value;
    value = GetCookie('productStory');
    if (value != null) {
        document.write('<div class="nav_content" style="padding-left:10px; width:194px;"> <ul class="history_content">');
        var products = value.split('|');
        var pLength;
        
        if (products.length > 8) {
            pLength = 8;
        }
        else {
            pLength = products.length;
        }
        
        for (i = 0; i < pLength; i++) {
        
            var product = products[i];
            var item = product.split(',');
            var pname = item[0];
            var price = item[1];
            var url = item[2];
            var priceStr = '￥' + price.substring(1, price.length) + '元';
            if (price.charAt(0) == 'c') {
                document.write("<li><img src='/images2/cuxiao_icon.gif' width='20' height='18' align='absmiddle' /><a href='" + url + "'>" + pname + "(" + priceStr + ")</a></li>");
            }
            else {
                document.write("<li><a href='" + url + "'>" + pname + "(" + priceStr + ")</a></li>");
            }
        }
        
        document.write('</ul></div>');
    }
    
    
}






